Skip to content

Commit 66c2be3

Browse files
committed
Small code tidy up and refactor to avoid code duplication.
1 parent a9748eb commit 66c2be3

File tree

8 files changed

+20
-67
lines changed

8 files changed

+20
-67
lines changed

lib/truffle/truffle/cext_ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def rb_define_method(mod, name, function, argc)
3838
# We must set block argument if given here so that the
3939
# `rb_block_*` functions will be able to find it by walking the
4040
# stack.
41-
res = Primitive.cext_unwrap(Primitive.call_with_c_mutex_and_frame(function, args, Primitive.caller_special_variables_if_fast, block))
41+
res = Primitive.cext_unwrap(Primitive.call_with_c_mutex_and_frame(function, args, Primitive.caller_special_variables_if_available, block))
4242
Primitive.thread_set_exception(exc)
4343
res
4444
end

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.truffleruby.language.ReadOwnFrameAndVariablesNode;
3838
import org.truffleruby.language.RubyBaseNode;
3939
import org.truffleruby.language.RubyNode;
40-
import org.truffleruby.language.arguments.MaybeReadCallerVariablesNode;
40+
import org.truffleruby.language.arguments.ReadCallerVariablesIfAvailableNode;
4141
import org.truffleruby.language.arguments.ReadCallerVariablesNode;
4242
import org.truffleruby.language.arguments.RubyArguments;
4343
import org.truffleruby.language.control.RaiseException;
@@ -342,10 +342,10 @@ protected Object storage(VirtualFrame frame) {
342342
}
343343
}
344344

345-
@Primitive(name = "caller_special_variables_if_fast")
345+
@Primitive(name = "caller_special_variables_if_available")
346346
public abstract static class GetCallerSpecialVariableStorageIfFast extends PrimitiveArrayArgumentsNode {
347347

348-
@Child MaybeReadCallerVariablesNode callerVariablesNode = new MaybeReadCallerVariablesNode();
348+
@Child ReadCallerVariablesIfAvailableNode callerVariablesNode = new ReadCallerVariablesIfAvailableNode();
349349

350350
@Specialization
351351
protected Object storage(VirtualFrame frame,

src/main/java/org/truffleruby/core/thread/TruffleThreadNodes.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.truffleruby.core.array.library.ArrayStoreLibrary;
1818
import org.truffleruby.core.kernel.TruffleKernelNodes.GetSpecialVariableStorage;
1919
import org.truffleruby.core.MarkingServiceNodes;
20+
import org.truffleruby.language.arguments.CallerDataReadingNode;
21+
import org.truffleruby.language.arguments.ReadCallerDataNode;
2022
import org.truffleruby.language.FrameAndVariablesSendingNode;
2123
import org.truffleruby.language.RubyNode;
2224

@@ -47,7 +49,7 @@ public FrameAndCallNode(Frame frame, Node callNode) {
4749

4850
@CoreMethod(names = "ruby_caller_special_variables", onSingleton = true, required = 1)
4951
@ImportStatic(ArrayGuards.class)
50-
public abstract static class FindRubyCallerSpecialStorage extends CoreMethodArrayArgumentsNode {
52+
public abstract static class FindRubyCallerSpecialStorage extends CoreMethodArrayArgumentsNode implements CallerDataReadingNode {
5153

5254
@TruffleBoundary
5355
@Specialization(limit = "storageStrategyLimit()")
@@ -65,30 +67,15 @@ protected Object findRubyCaller(RubyArray modules,
6567
if (data == null) {
6668
return nil;
6769
} else {
68-
notifyToStartSendingStorage(data.callNode);
70+
CallerDataReadingNode.notifyCallerToSendData(getContext(), data.callNode, this);
6971
Object variables = storageNode.execute(data.frame.materialize());
7072
getDataNode.execute().getExtensionCallStack().setVariables(variables);
7173
return variables;
7274
}
7375
}
7476

75-
private static boolean notifyToStartSendingStorage(Node callerNode) {
76-
if (callerNode instanceof DirectCallNode || callerNode instanceof IndirectCallNode) {
77-
Node parent = callerNode.getParent();
78-
while (parent != null) {
79-
if (parent instanceof FrameAndVariablesSendingNode) {
80-
((FrameAndVariablesSendingNode) parent).startSendingOwnVariables();
81-
return true;
82-
}
83-
if (parent instanceof RubyNode) {
84-
// A node with source info representing Ruby code, we could not find the FrameAndVariablesSendingNode
85-
return false;
86-
}
87-
parent = parent.getParent();
88-
}
89-
}
90-
91-
return false;
77+
public void startSending(FrameAndVariablesSendingNode node) {
78+
node.startSendingOwnVariables();
9279
}
9380
}
9481
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,6 @@ public Frame getNonJavaCoreCallerFrame(FrameAccess frameAccess) {
7474
}, frameAccess);
7575
}
7676

77-
@TruffleBoundary
78-
public Frame getCallerFrameNotInModules(FrameAccess frameAccess, Object[] modules) {
79-
final Memo<Boolean> skippedFirstFrameFound = new Memo<>(false);
80-
81-
return getCallerFrame(frameInstance -> {
82-
final InternalMethod method = tryGetMethod(frameInstance.getFrame(FrameAccess.READ_ONLY));
83-
if (method != null && !ArrayUtils.contains(modules, method.getDeclaringModule())) {
84-
if (skippedFirstFrameFound.get()) {
85-
return true;
86-
}
87-
skippedFirstFrameFound.set(true);
88-
}
89-
return false;
90-
}, frameAccess);
91-
}
92-
9377
@TruffleBoundary
9478
public <R> R iterateFrameNotInModules(Object[] modules, Function<FrameInstance, R> action) {
9579
final Memo<Boolean> skippedFirstFrameFound = new Memo<>(false);

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
import com.oracle.truffle.api.nodes.Node;
2121
import com.oracle.truffle.api.profiles.ConditionProfile;
2222

23+
import org.truffleruby.RubyContext;
2324
import org.truffleruby.language.CallStackManager;
2425
import org.truffleruby.language.FrameAndVariablesSendingNode;
2526
import org.truffleruby.language.NotOptimizedWarningNode;
2627
import org.truffleruby.language.RubyBaseNode;
2728
import org.truffleruby.language.RubyNode;
2829
import org.truffleruby.language.control.RaiseException;
2930

30-
public abstract class ReadCallerDataNode extends RubyBaseNode {
31+
public abstract class ReadCallerDataNode extends RubyBaseNode implements CallerDataReadingNode {
3132

3233
private final ConditionProfile callerDataProfile = ConditionProfile.create();
3334
@Child private NotOptimizedWarningNode notOptimizedNode = null;
@@ -46,7 +47,8 @@ public Object execute(Frame frame) {
4647

4748
@TruffleBoundary
4849
protected Object getCallerData() {
49-
if (!notifyCallerToSendData()) {
50+
final Node callerNode = getContext().getCallStack().getCallerNode(1, false);
51+
if (!CallerDataReadingNode.notifyCallerToSendData(getContext(), callerNode, this)) {
5052
// If we fail to notify the call node (e.g., because it is a UncachedDispatchNode which is not handled yet),
5153
// we don't want to deoptimize this CallTarget on every call.
5254
getNotOptimizedNode().warn("Unoptimized reading of caller data.");
@@ -70,27 +72,7 @@ protected Object getCallerData() {
7072

7173
protected abstract Object getDataFromFrame(MaterializedFrame frame);
7274

73-
private boolean notifyCallerToSendData() {
74-
final Node callerNode = getContext().getCallStack().getCallerNode(1, false);
75-
if (callerNode instanceof DirectCallNode || callerNode instanceof IndirectCallNode) {
76-
Node parent = callerNode.getParent();
77-
while (parent != null) {
78-
if (parent instanceof FrameAndVariablesSendingNode) {
79-
startSending((FrameAndVariablesSendingNode) parent);
80-
return true;
81-
}
82-
if (parent instanceof RubyNode) {
83-
// A node with source info representing Ruby code, we could not find the FrameAndVariablesSendingNode
84-
return false;
85-
}
86-
parent = parent.getParent();
87-
}
88-
}
89-
90-
return false;
91-
}
92-
93-
protected abstract void startSending(FrameAndVariablesSendingNode node);
75+
public abstract void startSending(FrameAndVariablesSendingNode node);
9476

9577
private NotOptimizedWarningNode getNotOptimizedNode() {
9678
if (notOptimizedNode == null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected MaterializedFrame getData(Frame frame) {
3131
}
3232

3333
@Override
34-
protected void startSending(FrameAndVariablesSendingNode node) {
34+
public void startSending(FrameAndVariablesSendingNode node) {
3535
node.startSendingOwnFrame();
3636
}
3737

src/main/java/org/truffleruby/language/arguments/MaybeReadCallerVariablesNode.java renamed to src/main/java/org/truffleruby/language/arguments/ReadCallerVariablesIfAvailableNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
*/
1010
package org.truffleruby.language.arguments;
1111

12-
public class MaybeReadCallerVariablesNode extends ReadCallerVariablesNode {
12+
public class ReadCallerVariablesIfAvailableNode extends ReadCallerVariablesNode {
1313

14-
public static MaybeReadCallerVariablesNode create() {
15-
return new MaybeReadCallerVariablesNode();
14+
public static ReadCallerVariablesIfAvailableNode create() {
15+
return new ReadCallerVariablesIfAvailableNode();
1616
}
1717

1818
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected SpecialVariableStorage getData(Frame frame) {
3333
}
3434

3535
@Override
36-
protected void startSending(FrameAndVariablesSendingNode node) {
36+
public void startSending(FrameAndVariablesSendingNode node) {
3737
node.startSendingOwnVariables();
3838
}
3939

0 commit comments

Comments
 (0)