9
9
*/
10
10
package org .truffleruby .core .cast ;
11
11
12
+ import com .oracle .truffle .api .CompilerDirectives ;
12
13
import com .oracle .truffle .api .dsl .Cached ;
13
14
import com .oracle .truffle .api .dsl .NodeChild ;
14
15
import com .oracle .truffle .api .dsl .Specialization ;
27
28
@ NodeChild (value = "child" , type = RubyNode .class )
28
29
public abstract class ToStringOrSymbolNode extends RubyNode {
29
30
30
- @ Child private CallDispatchHeadNode toStr = CallDispatchHeadNode . createPrivate () ;
31
+ @ Child private CallDispatchHeadNode toStr ;
31
32
32
33
@ Specialization (guards = "isRubySymbol(symbol)" )
33
34
public DynamicObject coerceRubySymbol (DynamicObject symbol ) {
@@ -44,7 +45,7 @@ public DynamicObject coerceObject(VirtualFrame frame, Object object,
44
45
@ Cached ("create()" ) BranchProfile errorProfile ) {
45
46
final Object coerced ;
46
47
try {
47
- coerced = toStr . call (object , "to_str" );
48
+ coerced = callToStr (object );
48
49
} catch (RaiseException e ) {
49
50
errorProfile .enter ();
50
51
if (Layouts .BASIC_OBJECT .getLogicalClass (e .getException ()) == coreLibrary ().getNoMethodErrorClass ()) {
@@ -61,4 +62,12 @@ public DynamicObject coerceObject(VirtualFrame frame, Object object,
61
62
throw new RaiseException (getContext (), coreExceptions ().typeErrorBadCoercion (object , "String" , "to_str" , coerced , this ));
62
63
}
63
64
}
65
+
66
+ private Object callToStr (Object object ) {
67
+ if (toStr == null ) {
68
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
69
+ toStr = insert (CallDispatchHeadNode .createPrivate ());
70
+ }
71
+ return toStr .call (object , "to_str" );
72
+ }
64
73
}
0 commit comments