|
9 | 9 | */
|
10 | 10 | package org.truffleruby.core.array;
|
11 | 11 |
|
| 12 | +import com.oracle.truffle.api.dsl.Cached; |
| 13 | +import com.oracle.truffle.api.dsl.Specialization; |
12 | 14 | import com.oracle.truffle.api.profiles.ConditionProfile;
|
13 |
| -import org.truffleruby.core.cast.ArrayCastNode; |
14 | 15 | import org.truffleruby.language.RubyContextNode;
|
| 16 | +import org.truffleruby.language.dispatch.DispatchNode; |
15 | 17 |
|
16 |
| -/** Attempts converting its argument to an array, via {@link ArrayCastNode} (i.e. calling "to_ary"), or if that doesn't |
17 |
| - * work, by wrapping it inside a one-element array. */ |
18 |
| -public final class ArrayConvertNode extends RubyContextNode { |
| 18 | +/** Attempts converting its argument to an array by calling #to_ary, or if that doesn't work, by wrapping it inside a |
| 19 | + * one-element array. */ |
| 20 | +public abstract class ArrayConvertNode extends RubyContextNode { |
19 | 21 |
|
20 |
| - @Child ArrayCastNode arrayCast = ArrayCastNode.create(); |
21 |
| - @Child ArrayBuilderNode arrayBuilder = ArrayBuilderNode.create(); |
22 |
| - private final ConditionProfile cantCast = ConditionProfile.create(); |
| 22 | + public abstract RubyArray execute(Object value); |
23 | 23 |
|
24 |
| - public static ArrayConvertNode create() { |
25 |
| - return new ArrayConvertNode(); |
| 24 | + @Specialization |
| 25 | + protected RubyArray castArray(RubyArray array) { |
| 26 | + return array; |
26 | 27 | }
|
27 | 28 |
|
28 |
| - public RubyArray execute(Object object) { |
29 |
| - Object converted = arrayCast.execute(object); |
30 |
| - if (cantCast.profile(converted == nil)) { |
| 29 | + @Specialization(guards = "!isRubyArray(object)") |
| 30 | + protected RubyArray cast(Object object, |
| 31 | + @Cached ConditionProfile canCast, |
| 32 | + @Cached ArrayBuilderNode arrayBuilder, |
| 33 | + @Cached(parameters = "PRIVATE_RETURN_MISSING") DispatchNode toArrayNode) { |
| 34 | + final Object result = toArrayNode.call(object, "to_ary"); |
| 35 | + if (canCast.profile(result instanceof RubyArray)) { |
| 36 | + return (RubyArray) result; |
| 37 | + } else { |
31 | 38 | return ArrayHelpers.specializedRubyArrayOf(getContext(), getLanguage(), arrayBuilder, object);
|
32 | 39 | }
|
33 |
| - return (RubyArray) converted; |
34 | 40 | }
|
| 41 | + |
35 | 42 | }
|
0 commit comments