Skip to content

Commit 20f6625

Browse files
committed
Refactor LocalVariableDefinedNode to remove CreateCast
1 parent 8256123 commit 20f6625

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/main/java/org/truffleruby/core/binding/BindingNodes.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
import java.util.Set;
1616

1717
import com.oracle.truffle.api.CompilerDirectives;
18+
import com.oracle.truffle.api.dsl.GenerateCached;
19+
import com.oracle.truffle.api.dsl.GenerateInline;
1820
import com.oracle.truffle.api.dsl.GenerateUncached;
1921
import com.oracle.truffle.api.dsl.Idempotent;
2022
import com.oracle.truffle.api.dsl.NeverDefault;
2123
import com.oracle.truffle.api.frame.FrameSlotKind;
24+
import com.oracle.truffle.api.nodes.Node;
2225
import com.oracle.truffle.api.strings.TruffleString;
2326
import org.truffleruby.Layouts;
2427
import org.truffleruby.RubyContext;
@@ -214,20 +217,32 @@ protected int getCacheLimit() {
214217
@CoreMethod(names = "local_variable_defined?", required = 1)
215218
@NodeChild(value = "bindingNode", type = RubyNode.class)
216219
@NodeChild(value = "nameNode", type = RubyBaseNodeWithExecute.class)
217-
public abstract static class LocalVariableDefinedNode extends CoreMethodNode {
220+
public abstract static class BindingLocalVariableDefinedNode extends CoreMethodNode {
218221

219-
@CreateCast("nameNode")
220-
protected RubyBaseNodeWithExecute coerceToString(RubyBaseNodeWithExecute name) {
221-
return NameToJavaStringNode.create(name);
222+
@Specialization
223+
protected boolean localVariableDefined(RubyBinding binding, Object nameObject,
224+
@Cached NameToJavaStringNode nameToJavaStringNode,
225+
@Cached LocalVariableDefinedNode localVariableDefinedNode) {
226+
final var name = nameToJavaStringNode.execute(nameObject);
227+
return localVariableDefinedNode.execute(this, binding, name);
222228
}
229+
}
230+
231+
232+
@ImportStatic({ BindingNodes.class, FindDeclarationVariableNodes.class })
233+
@GenerateCached(false)
234+
@GenerateInline
235+
public abstract static class LocalVariableDefinedNode extends RubyBaseNode {
236+
237+
public abstract boolean execute(Node node, RubyBinding binding, String name);
223238

224239
@Specialization(
225240
guards = {
226241
"name == cachedName",
227242
"!isHiddenVariable(cachedName)",
228243
"getFrameDescriptor(binding) == descriptor" },
229244
limit = "getCacheLimit()")
230-
protected boolean localVariableDefinedCached(RubyBinding binding, String name,
245+
protected static boolean localVariableDefinedCached(RubyBinding binding, String name,
231246
@Cached("name") String cachedName,
232247
@Cached("getFrameDescriptor(binding)") FrameDescriptor descriptor,
233248
@Cached("findFrameSlotOrNull(name, binding.getFrame())") FrameSlotAndDepth cachedFrameSlot) {
@@ -236,16 +251,16 @@ protected boolean localVariableDefinedCached(RubyBinding binding, String name,
236251

237252
@TruffleBoundary
238253
@Specialization(guards = "!isHiddenVariable(name)", replaces = "localVariableDefinedCached")
239-
protected boolean localVariableDefinedUncached(RubyBinding binding, String name) {
254+
protected static boolean localVariableDefinedUncached(RubyBinding binding, String name) {
240255
return FindDeclarationVariableNodes.findFrameSlotOrNull(name, binding.getFrame()) != null;
241256
}
242257

243258
@TruffleBoundary
244259
@Specialization(guards = "isHiddenVariable(name)")
245-
protected Object localVariableDefinedLastLine(RubyBinding binding, String name) {
260+
protected static boolean localVariableDefinedLastLine(Node node, RubyBinding binding, String name) {
246261
throw new RaiseException(
247-
getContext(),
248-
coreExceptions().nameError("Bad local variable name", binding, name, this));
262+
getContext(node),
263+
coreExceptions(node).nameError("Bad local variable name", binding, name, node));
249264
}
250265

251266
protected int getCacheLimit() {
@@ -484,7 +499,6 @@ public abstract static class AllocateNode extends CoreMethodArrayArgumentsNode {
484499
protected Object allocate(RubyClass rubyClass) {
485500
throw new RaiseException(getContext(), coreExceptions().typeErrorAllocatorUndefinedFor(rubyClass, this));
486501
}
487-
488502
}
489503

490504
@Primitive(name = "create_empty_binding")
@@ -493,7 +507,8 @@ public abstract static class CreateEmptyBindingNode extends PrimitiveArrayArgume
493507
protected RubyBinding binding(VirtualFrame frame) {
494508
// Use the current frame to initialize the arguments, etc, correctly
495509
final RubyBinding binding = BindingNodes
496-
.createBinding(getContext(), getLanguage(), frame.materialize(), getEncapsulatingSourceSection());
510+
.createBinding(getContext(), getLanguage(), frame.materialize(),
511+
getEncapsulatingSourceSection());
497512

498513
final MaterializedFrame newFrame = newFrame(binding, getLanguage().emptyDeclarationDescriptor);
499514
RubyArguments.setDeclarationFrame(newFrame, null); // detach from the current frame

0 commit comments

Comments
 (0)