Skip to content

Commit 574d6bd

Browse files
author
Nicolas Laurent
committed
[GR-25981] Fix (format) ToLongNode not to use a RETURN_MISSING DispatchNode.
PullRequest: truffleruby/1956
2 parents fe736bb + 4921b85 commit 574d6bd

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/main/java/org/truffleruby/core/format/convert/ToLongNode.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
package org.truffleruby.core.format.convert;
1111

12+
import com.oracle.truffle.api.dsl.Cached;
13+
import com.oracle.truffle.api.profiles.BranchProfile;
1214
import org.truffleruby.core.format.FormatNode;
1315
import org.truffleruby.core.format.exceptions.CantConvertException;
1416
import org.truffleruby.core.format.exceptions.NoImplicitConversionException;
@@ -17,20 +19,18 @@
1719
import org.truffleruby.language.Nil;
1820
import org.truffleruby.language.dispatch.DispatchNode;
1921

20-
import com.oracle.truffle.api.CompilerDirectives;
2122
import com.oracle.truffle.api.dsl.NodeChild;
2223
import com.oracle.truffle.api.dsl.Specialization;
2324
import com.oracle.truffle.api.frame.VirtualFrame;
2425

25-
import static org.truffleruby.language.dispatch.DispatchConfiguration.PRIVATE_RETURN_MISSING;
26-
2726
@NodeChild("value")
2827
public abstract class ToLongNode extends FormatNode {
2928

30-
private final boolean errorIfNeedsConversion;
29+
protected final boolean errorIfNeedsConversion;
3130

32-
@Child private DispatchNode toIntNode;
33-
@Child private ToLongNode redoNode;
31+
public static ToLongNode create(boolean errorIfNeedsConversion) {
32+
return ToLongNodeGen.create(errorIfNeedsConversion, null);
33+
}
3434

3535
public ToLongNode(boolean errorIfNeedsConversion) {
3636
this.errorIfNeedsConversion = errorIfNeedsConversion;
@@ -65,25 +65,23 @@ protected long toLongNil(Nil nil) {
6565
}
6666

6767
@Specialization(
68-
guards = { "!isBoolean(object)", "!isRubyInteger(object)", "!isNil(object)" })
68+
guards = { "errorIfNeedsConversion", "!isBoolean(object)", "!isRubyInteger(object)", "!isNil(object)" })
6969
protected long toLong(VirtualFrame frame, Object object) {
70-
if (errorIfNeedsConversion) {
71-
throw new CantConvertException("can't convert Object to Integer");
72-
}
73-
74-
if (toIntNode == null) {
75-
CompilerDirectives.transferToInterpreterAndInvalidate();
76-
toIntNode = insert(DispatchNode.create(PRIVATE_RETURN_MISSING));
77-
}
78-
79-
final Object value = toIntNode.call(object, "to_int");
70+
throw new CantConvertException("can't convert Object to Integer");
71+
}
8072

81-
if (redoNode == null) {
82-
CompilerDirectives.transferToInterpreterAndInvalidate();
83-
redoNode = insert(ToLongNodeGen.create(true, null));
73+
@Specialization(
74+
guards = { "!errorIfNeedsConversion", "!isBoolean(object)", "!isRubyInteger(object)", "!isNil(object)" })
75+
protected long toLong(VirtualFrame frame, Object object,
76+
@Cached(parameters = "PRIVATE_RETURN_MISSING") DispatchNode toIntNode,
77+
@Cached("create(true)") ToLongNode redoNode,
78+
@Cached BranchProfile noConversionAvailable) {
79+
80+
Object result = toIntNode.call(object, "to_int");
81+
if (result == DispatchNode.MISSING) {
82+
noConversionAvailable.enter();
83+
throw new CantConvertException("can't convert Object to Integer");
8484
}
85-
86-
return redoNode.executeToLong(frame, value);
85+
return redoNode.executeToLong(frame, result);
8786
}
88-
8987
}

0 commit comments

Comments
 (0)