Skip to content

Commit 0fba93e

Browse files
committed
Add get*Node() methods in DispatchNode to reduce code duplication
1 parent 50995e1 commit 0fba93e

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,13 @@ public final Object dispatch(Frame frame, Object receiver, String methodName, Ob
299299
}
300300

301301
private Object callMethodMissing(Frame frame, Object receiver, String methodName, Object[] rubyArgs) {
302+
// profiles through lazy node creation
302303
final RubySymbol symbolName = nameToSymbol(methodName);
304+
303305
final Object[] newArgs = RubyArguments.repack(rubyArgs, receiver, 0, 1);
304306

305307
RubyArguments.setArgument(newArgs, 0, symbolName);
306-
final Object result = callMethodMissingNode(frame, receiver, newArgs);
308+
final Object result = getMethodMissingNode().dispatch(frame, receiver, "method_missing", newArgs);
307309

308310
if (result == MISSING) {
309311
methodMissingMissing.enter();
@@ -319,24 +321,30 @@ private Object callMethodMissing(Frame frame, Object receiver, String methodName
319321
}
320322

321323
protected Object callForeign(Object receiver, String methodName, Object[] rubyArgs) {
324+
// profiles through lazy node creation
325+
final CallForeignMethodNode callForeignMethodNode = getCallForeignMethodNode();
326+
327+
final Object block = RubyArguments.getBlock(rubyArgs);
328+
final Object[] arguments = RubyArguments.getPositionalArguments(rubyArgs, false);
329+
return callForeignMethodNode.execute(receiver, methodName, block, arguments);
330+
}
331+
332+
protected CallForeignMethodNode getCallForeignMethodNode() {
322333
if (callForeign == null) {
323334
CompilerDirectives.transferToInterpreterAndInvalidate();
324335
callForeign = insert(CallForeignMethodNode.create());
325336
}
326-
327-
final Object block = RubyArguments.getBlock(rubyArgs);
328-
final Object[] arguments = RubyArguments.getPositionalArguments(rubyArgs, false);
329-
return callForeign.execute(receiver, methodName, block, arguments);
337+
return callForeign;
330338
}
331339

332-
protected Object callMethodMissingNode(Frame frame, Object receiver, Object[] rubyArgs) {
340+
protected DispatchNode getMethodMissingNode() {
333341
if (callMethodMissing == null) {
334342
CompilerDirectives.transferToInterpreterAndInvalidate();
335343
// #method_missing ignores refinements on CRuby: https://bugs.ruby-lang.org/issues/13129
336344
callMethodMissing = insert(
337345
DispatchNode.create(DispatchConfiguration.PRIVATE_RETURN_MISSING_IGNORE_REFINEMENTS));
338346
}
339-
return callMethodMissing.dispatch(frame, receiver, "method_missing", rubyArgs);
347+
return callMethodMissing;
340348
}
341349

342350
protected RubySymbol nameToSymbol(String methodName) {
@@ -394,16 +402,13 @@ protected Uncached(DispatchConfiguration config) {
394402
}
395403

396404
@Override
397-
protected Object callForeign(Object receiver, String methodName, Object[] rubyArgs) {
398-
final Object block = RubyArguments.getBlock(rubyArgs);
399-
final Object[] arguments = RubyArguments.getPositionalArguments(rubyArgs, false);
400-
return CallForeignMethodNode.getUncached().execute(receiver, methodName, block, arguments);
405+
protected CallForeignMethodNode getCallForeignMethodNode() {
406+
return CallForeignMethodNode.getUncached();
401407
}
402408

403409
@Override
404-
protected Object callMethodMissingNode(Frame frame, Object receiver, Object[] rubyArgs) {
405-
return DispatchNode.getUncached(DispatchConfiguration.PRIVATE_RETURN_MISSING_IGNORE_REFINEMENTS)
406-
.dispatch(frame, receiver, "method_missing", rubyArgs);
410+
protected DispatchNode getMethodMissingNode() {
411+
return DispatchNode.getUncached(DispatchConfiguration.PRIVATE_RETURN_MISSING_IGNORE_REFINEMENTS);
407412
}
408413

409414
@Override

0 commit comments

Comments
 (0)