Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Truffle] Trying to add #to_ary to Array#replace #2689

Merged
merged 1 commit into from Mar 13, 2015

Conversation

bjfish
Copy link
Contributor

@bjfish bjfish commented Mar 12, 2015

@nirvdrum @chrisseaton Is this correct place to use a CreateCast? If not, why not?

Also trying to implement ToAryNode here.

@Specialization
public Object coerceObject(VirtualFrame frame, Object object) {
notDesignedForCompilation();
if (object instanceof RubyArray) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be its own specialization so in the common case of just being an array, you get a very small method body that could be inlined. You'll then need to guard the generic case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, one specialisation should be RubyArray, and then other should be Object and then also guarded !isRubyArray. Otherwise you could use the Object one first and then keep happily using it when you get a RubyArray as the type fits.

@nirvdrum
Copy link
Contributor

CreateCast is fine here. It's allowing you to normalize one argument of all specializations to a fixed type. I'm not sure how frequently ToAryNode will be used otherwise, but that's fine.

public abstract class ToAryNode extends RubyNode {

@Child
private CallDispatchHeadNode toAryNode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor style thing, but in every other case I've seen we have the annotation and the field declaration on the same line.

@nirvdrum
Copy link
Contributor

Your ToAryNode works fine in the case you've defined it for. If you'd like to able to use it in other places to provide a simple way to call to_ary on an object and return a RubyArray, you'll need to add the abstract execute* methods like in ToStrNode.

@bjfish bjfish force-pushed the truffle_array_replace_to_ary branch from af1b7c8 to fd32663 Compare March 13, 2015 00:46
@bjfish
Copy link
Contributor Author

bjfish commented Mar 13, 2015

@nirvdrum Thanks for all the feedback. I've updated the PR with a rebased commit that I believe addresses your comments.

throw e;
}
}
return coerced;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the other coercion nodes, they check the value of the return type. I think you need to do that here as well, since someone could define to_ary to return something other than a RubyArray. Here are the two cases I saw with Array#replace:

$ ruby -e 'class X; end; [].replace(X.new)'
-e:1:in `replace': no implicit conversion of X into Array (TypeError)
    from -e:1:in `<main>'

That's the missing to_ary implementation, which you've addressed in the code above. But there's also:

$ ruby -e 'class X; def to_ary; :hello; end; end; [].replace(X.new)'
-e:1:in `replace': can't convert X to Array (X#to_ary gives Symbol) (TypeError)
    from -e:1:in `<main>'

That's the case you're missing.

@nirvdrum
Copy link
Contributor

@bjfish No problem. If you can address my last comment about handling the case when to_ary returning the wrong type, I think this will be good to merge.

@bjfish bjfish force-pushed the truffle_array_replace_to_ary branch from fd32663 to 2b1f79b Compare March 13, 2015 13:02
@bjfish
Copy link
Contributor Author

bjfish commented Mar 13, 2015

@nirvdrum I've added to return type check, updated the return type to RubyArray (ToStrNode returns RubyString) , and fixed the guard spacing.

}

@Specialization
public Object coerceRubyArray(RubyArray rubyArray) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on the return type. This should be RubyArray, too.

@bjfish bjfish force-pushed the truffle_array_replace_to_ary branch from 2b1f79b to 5444ab7 Compare March 13, 2015 13:45
@bjfish
Copy link
Contributor Author

bjfish commented Mar 13, 2015

@nirvdrum I fixed the other return type.

@nirvdrum
Copy link
Contributor

Looks great.

nirvdrum added a commit that referenced this pull request Mar 13, 2015
[Truffle] Trying to add #to_ary to Array#replace
@nirvdrum nirvdrum merged commit e062ebd into jruby:master Mar 13, 2015
@chrisseaton chrisseaton modified the milestone: truffle-dev May 4, 2015
@enebo enebo added this to the Non-Release milestone Dec 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants