Skip to content

Commit 67254d9

Browse files
author
Nicolas Laurent
committed
refactor DispatchRespondToNode #execute method & replace its #doesRespondTo method
1 parent b4c6663 commit 67254d9

File tree

7 files changed

+30
-29
lines changed

7 files changed

+30
-29
lines changed

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,15 +1577,15 @@ protected boolean doesRespondToString(
15771577
final boolean ret;
15781578

15791579
if (ignoreVisibilityProfile.profile(includeProtectedAndPrivate)) {
1580-
ret = dispatchIgnoreVisibility.doesRespondTo(frame, toJavaString.executeToJavaString(name), object);
1580+
ret = dispatchIgnoreVisibility.execute(frame, object, toJavaString.executeToJavaString(name));
15811581
} else {
1582-
ret = dispatch.doesRespondTo(frame, toJavaString.executeToJavaString(name), object);
1582+
ret = dispatch.execute(frame, object, toJavaString.executeToJavaString(name));
15831583
}
15841584

15851585
if (isTrueProfile.profile(ret)) {
15861586
return true;
15871587
} else if (respondToMissingProfile
1588-
.profile(dispatchRespondToMissing.doesRespondTo(frame, "respond_to_missing?", object))) {
1588+
.profile(dispatchRespondToMissing.execute(frame, object, "respond_to_missing?"))) {
15891589
return respondToMissing(
15901590
frame,
15911591
object,
@@ -1606,15 +1606,15 @@ protected boolean doesRespondToSymbol(
16061606
final boolean ret;
16071607

16081608
if (ignoreVisibilityProfile.profile(includeProtectedAndPrivate)) {
1609-
ret = dispatchIgnoreVisibility.doesRespondTo(frame, toJavaString.executeToJavaString(name), object);
1609+
ret = dispatchIgnoreVisibility.execute(frame, object, toJavaString.executeToJavaString(name));
16101610
} else {
1611-
ret = dispatch.doesRespondTo(frame, toJavaString.executeToJavaString(name), object);
1611+
ret = dispatch.execute(frame, object, toJavaString.executeToJavaString(name));
16121612
}
16131613

16141614
if (isTrueProfile.profile(ret)) {
16151615
return true;
16161616
} else if (respondToMissingProfile
1617-
.profile(dispatchRespondToMissing.doesRespondTo(frame, "respond_to_missing?", object))) {
1617+
.profile(dispatchRespondToMissing.execute(frame, object, "respond_to_missing?"))) {
16181618
return respondToMissing(frame, object, name, includeProtectedAndPrivate);
16191619
} else {
16201620
return false;

src/main/java/org/truffleruby/core/objectspace/ObjectSpaceNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public abstract static class DefineFinalizerNode extends CoreMethodArrayArgument
202202
protected RubyArray defineFinalizer(VirtualFrame frame, RubyDynamicObject object, Object finalizer,
203203
@Cached BranchProfile errorProfile,
204204
@Cached WriteBarrierNode writeBarrierNode) {
205-
if (respondToCallNode.doesRespondTo(frame, "call", finalizer)) {
205+
if (respondToCallNode.execute(frame, finalizer, "call")) {
206206
if (getContext().getSharedObjects().isSharing()) {
207207
// Share the finalizer, as it might run on a different Thread
208208
writeBarrierNode.executeWriteBarrier(finalizer);

src/main/java/org/truffleruby/language/ImmutableRubyObject.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public Object getMembers(boolean internal,
113113
@ExportMessage
114114
public boolean isMemberReadable(String name,
115115
@Cached @Shared("definedNode") DispatchRespondToNode definedNode) {
116-
return definedNode.doesRespondTo(null, name, this);
116+
return definedNode.execute(null, this, name);
117117
}
118118

119119
@ExportMessage
@@ -123,7 +123,7 @@ public Object readMember(String name,
123123
@Cached @Exclusive DispatchNode dispatch,
124124
@Shared("errorProfile") @Cached BranchProfile errorProfile)
125125
throws UnknownIdentifierException {
126-
if (definedNode.doesRespondTo(null, name, this)) {
126+
if (definedNode.execute(null, this, name)) {
127127
Object rubyName = nameToRubyNode.executeConvert(name);
128128
return dispatch.call(this, "method", rubyName);
129129
} else {
@@ -135,7 +135,7 @@ public Object readMember(String name,
135135
@ExportMessage
136136
public boolean isMemberInvocable(String name,
137137
@Cached @Shared("definedNode") DispatchRespondToNode definedNode) {
138-
return definedNode.doesRespondTo(null, name, this);
138+
return definedNode.execute(null, this, name);
139139
}
140140

141141
@ExportMessage
@@ -158,8 +158,8 @@ public boolean isMemberInternal(String name,
158158
@Cached @Shared("definedNode") DispatchRespondToNode definedNode,
159159
@Exclusive @Cached(parameters = "PUBLIC") DispatchRespondToNode definedPublicNode) {
160160
// defined but not publicly
161-
return definedNode.doesRespondTo(null, name, this) &&
162-
!definedPublicNode.doesRespondTo(null, name, this);
161+
return definedNode.execute(null, this, name) &&
162+
!definedPublicNode.execute(null, this, name);
163163
}
164164
// endregion
165165
// endregion

src/main/java/org/truffleruby/language/RubyDynamicObject.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public Object readMember(String name,
396396
Object iVar = objectLibrary.getOrDefault(this, name, null);
397397
if (ivarFoundProfile.profile(iVar != null)) {
398398
return iVar;
399-
} else if (definedNode.doesRespondTo(null, name, this)) {
399+
} else if (definedNode.execute(null, this, name)) {
400400
return dispatch.call(this, "method", rubyName);
401401
} else {
402402
errorProfile.enter();
@@ -519,7 +519,7 @@ public boolean isMemberReadable(String name,
519519
if (ivarFoundProfile.profile(objectLibrary.containsKey(this, name))) {
520520
return true;
521521
} else {
522-
return definedNode.doesRespondTo(null, name, this);
522+
return definedNode.execute(null, this, name);
523523
}
524524
} else {
525525
return booleanCastNode.executeToBoolean(dynamic);
@@ -618,7 +618,7 @@ public boolean isMemberInvocable(String name,
618618
if (ivarFoundProfile.profile(iVar != null)) {
619619
return false;
620620
} else {
621-
return definedNode.doesRespondTo(null, name, this);
621+
return definedNode.execute(null, this, name);
622622
}
623623
} else {
624624
return booleanCastNode.executeToBoolean(dynamic);
@@ -643,8 +643,8 @@ public boolean isMemberInternal(String name,
643643
return true;
644644
} else {
645645
// defined but not publicly
646-
return definedNode.doesRespondTo(null, name, this) &&
647-
!definedPublicNode.doesRespondTo(null, name, this);
646+
return definedNode.execute(null, this, name) &&
647+
!definedPublicNode.execute(null, this, name);
648648
}
649649
} else {
650650
return booleanCastNode.executeToBoolean(dynamic);
@@ -686,7 +686,7 @@ public boolean hasMemberWriteSideEffects(String name,
686686
@ExportMessage
687687
public boolean isInstantiable(
688688
@Exclusive @Cached(parameters = "PUBLIC") DispatchRespondToNode doesRespond) {
689-
return doesRespond.doesRespondTo(null, "new", this);
689+
return doesRespond.execute(null, this, "new");
690690
}
691691

692692
@ExportMessage

src/main/java/org/truffleruby/language/arguments/ReadUserKeywordsHashNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private boolean respondToToHash(VirtualFrame frame, Object lastArgument) {
7070
CompilerDirectives.transferToInterpreterAndInvalidate();
7171
respondToToHashNode = insert(DispatchRespondToNode.create());
7272
}
73-
return respondToToHashNode.doesRespondTo(frame, "to_hash", lastArgument);
73+
return respondToToHashNode.execute(frame, lastArgument, "to_hash");
7474
}
7575

7676
private Object callToHash(VirtualFrame frame, Object lastArgument) {

src/main/java/org/truffleruby/language/arguments/ShouldDestructureNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Object execute(VirtualFrame frame) {
4545

4646
// TODO(cseaton): check this is actually a static "find if there is such method" and not a
4747
// dynamic call to respond_to?
48-
return respondToToAry.doesRespondTo(frame, "to_ary", firstArgument);
48+
return respondToToAry.execute(frame, firstArgument, "to_ary");
4949
}
5050

5151
}

src/main/java/org/truffleruby/language/dispatch/DispatchRespondToNode.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@
1212
import com.oracle.truffle.api.frame.VirtualFrame;
1313
import com.oracle.truffle.api.nodes.NodeCost;
1414
import org.truffleruby.core.klass.RubyClass;
15-
import org.truffleruby.core.proc.RubyProc;
1615
import org.truffleruby.language.RubyBaseNode;
1716
import org.truffleruby.language.methods.InternalMethod;
1817
import org.truffleruby.language.methods.LookupMethodNode;
1918
import org.truffleruby.language.methods.LookupMethodNodeGen;
2019
import org.truffleruby.language.objects.MetaClassNode;
2120
import org.truffleruby.language.objects.MetaClassNodeGen;
2221

22+
/** Determines if an objects "responds to" a method, meaning the method can be looked up and is
23+
* {@link InternalMethod#isDefined() defined} and {@link InternalMethod#isImplemented() implemented}.
24+
*
25+
* <p>
26+
* This does NOT call <code>respond_to_missing?</code> on the object, and as such is not a substitute for
27+
* {@link KernelNodes.RespondToNode} which implements the Ruby <code>Object#respond_to?</code>, and should be used in
28+
* almost all cases, especially when implementing Ruby methods with Java nodes. */
2329
public class DispatchRespondToNode extends RubyBaseNode {
2430

2531
// NOTE(norswap): cf. comment above static fields in DispatchNode to see why we need this field
@@ -58,11 +64,7 @@ protected DispatchRespondToNode(DispatchConfiguration config) {
5864
this(config, MetaClassNode.create(), LookupMethodNode.create());
5965
}
6066

61-
public boolean doesRespondTo(VirtualFrame frame, String methodName, Object receiver) {
62-
return (boolean) execute(frame, receiver, methodName, null, EMPTY_ARGUMENTS);
63-
}
64-
65-
public Object execute(VirtualFrame frame, Object receiver, String methodName, RubyProc block, Object[] arguments) {
67+
public boolean execute(VirtualFrame frame, Object receiver, String methodName) {
6668
final RubyClass metaclass = metaclassNode.execute(receiver);
6769
final InternalMethod method = methodLookup.execute(frame, metaclass, methodName, config);
6870
return method != null && method.isDefined() && method.isImplemented();
@@ -82,9 +84,8 @@ protected Uncached(DispatchConfiguration config) {
8284
}
8385

8486
@Override
85-
public Object execute(VirtualFrame frame, Object receiver, String methodName, RubyProc block,
86-
Object[] arguments) {
87-
return super.execute(null, receiver, methodName, block, arguments);
87+
public boolean execute(VirtualFrame frame, Object receiver, String methodName) {
88+
return super.execute(null, receiver, methodName);
8889
}
8990

9091
@Override

0 commit comments

Comments
 (0)