Skip to content

Commit c5925bd

Browse files
committed
[Truffle] Save the default definee in the frame and use it to know where to define methods.
1 parent a56d58a commit c5925bd

23 files changed

+224
-57
lines changed
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
fails:A nested method definition creates an instance method when evaluated in an instance method
2-
fails:A nested method definition creates a class method when evaluated in a class method
3-
fails:A method definition inside an instance_eval creates a class method when the receiver is a class
4-
fails:A method definition in an eval creates an instance method
5-
fails:A method definition in an eval creates a class method
6-
fails:A method definition in an eval creates a singleton method
71
fails:An instance method with a default argument does not call a method with the same name as the local
82
fails:An instance method with a default argument shadows an existing method with the same name as the local

truffle/src/main/java/org/jruby/truffle/nodes/core/BasicObjectNodes.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import com.oracle.truffle.api.dsl.Specialization;
1616
import com.oracle.truffle.api.frame.VirtualFrame;
1717
import com.oracle.truffle.api.nodes.NodeUtil;
18+
import com.oracle.truffle.api.nodes.Node.Child;
1819
import com.oracle.truffle.api.object.DynamicObject;
1920
import com.oracle.truffle.api.source.SourceSection;
21+
2022
import org.jruby.runtime.Visibility;
2123
import org.jruby.truffle.nodes.RubyGuards;
2224
import org.jruby.truffle.nodes.RubyNode;
@@ -25,6 +27,7 @@
2527
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
2628
import org.jruby.truffle.nodes.dispatch.MissingBehavior;
2729
import org.jruby.truffle.nodes.dispatch.RubyCallNode;
30+
import org.jruby.truffle.nodes.methods.DeclarationContext;
2831
import org.jruby.truffle.nodes.methods.UnsupportedOperationBehavior;
2932
import org.jruby.truffle.nodes.objects.AllocateObjectNode;
3033
import org.jruby.truffle.nodes.objects.AllocateObjectNodeGen;
@@ -136,7 +139,7 @@ public abstract static class InstanceEvalNode extends CoreMethodArrayArgumentsNo
136139

137140
public InstanceEvalNode(RubyContext context, SourceSection sourceSection) {
138141
super(context, sourceSection);
139-
yield = new YieldDispatchHeadNode(context);
142+
yield = new YieldDispatchHeadNode(context, DeclarationContext.INSTANCE_EVAL);
140143
}
141144

142145
@CompilerDirectives.TruffleBoundary
@@ -153,17 +156,20 @@ public Object instanceEval(VirtualFrame frame, Object receiver, NotProvided stri
153156
}
154157

155158
@CoreMethod(names = "instance_exec", needsBlock = true, rest = true)
156-
public abstract static class InstanceExecNode extends YieldingCoreMethodNode {
159+
public abstract static class InstanceExecNode extends CoreMethodArrayArgumentsNode {
160+
161+
@Child private YieldDispatchHeadNode yield;
157162

158163
public InstanceExecNode(RubyContext context, SourceSection sourceSection) {
159164
super(context, sourceSection);
165+
yield = new YieldDispatchHeadNode(context, DeclarationContext.INSTANCE_EVAL);
160166
}
161167

162168
@Specialization(guards = "isRubyProc(block)")
163169
public Object instanceExec(VirtualFrame frame, Object receiver, Object[] arguments, DynamicObject block) {
164170
CompilerDirectives.transferToInterpreter();
165171

166-
return yieldWithModifiedSelf(frame, block, receiver, arguments);
172+
return yield.dispatchWithModifiedSelf(frame, block, receiver, arguments);
167173
}
168174

169175
@Specialization

truffle/src/main/java/org/jruby/truffle/nodes/core/BindingNodes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static DynamicObject createBinding(RubyContext context, MaterializedFrame
4747
frame,
4848
null, RubyArguments.getSelf(arguments),
4949
RubyArguments.getBlock(arguments),
50+
RubyArguments.getDeclarationContext(arguments),
5051
RubyArguments.extractUserArguments(arguments)),
5152
new FrameDescriptor(context.getCoreLibrary().getNilObject()));
5253

truffle/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public Object evalNoBindingCached(
562562
final InternalMethod method = new InternalMethod(
563563
cachedRootNode.getRootNode().getSharedMethodInfo(),
564564
cachedRootNode.getRootNode().getSharedMethodInfo().getName(),
565-
getContext().getCoreLibrary().getObjectClass(),
565+
RubyArguments.getMethod(parentFrame.getArguments()).getDeclaringModule(),
566566
Visibility.PUBLIC,
567567
false,
568568
cachedCallTarget,
@@ -571,9 +571,11 @@ public Object evalNoBindingCached(
571571
return callNode.call(frame, RubyArguments.pack(
572572
method,
573573
parentFrame,
574-
null, callerSelf,
575574
null,
576-
new Object[]{}));
575+
callerSelf,
576+
null,
577+
RubyArguments.getDeclarationContext(parentFrame.getArguments()),
578+
new Object[] {}));
577579
}
578580

579581
@Specialization(guards = {

truffle/src/main/java/org/jruby/truffle/nodes/core/MethodNodes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.oracle.truffle.api.object.DynamicObject;
2323
import com.oracle.truffle.api.source.NullSourceSection;
2424
import com.oracle.truffle.api.source.SourceSection;
25+
2526
import org.jcodings.specific.UTF8Encoding;
2627
import org.jruby.runtime.ArgumentDescriptor;
2728
import org.jruby.runtime.Helpers;
@@ -32,6 +33,7 @@
3233
import org.jruby.truffle.nodes.core.BasicObjectNodes.ReferenceEqualNode;
3334
import org.jruby.truffle.nodes.methods.CallMethodNode;
3435
import org.jruby.truffle.nodes.methods.CallMethodNodeGen;
36+
import org.jruby.truffle.nodes.methods.DeclarationContext;
3537
import org.jruby.truffle.nodes.objects.ClassNode;
3638
import org.jruby.truffle.nodes.objects.ClassNodeGen;
3739
import org.jruby.truffle.runtime.RubyArguments;
@@ -113,6 +115,7 @@ private Object[] packArguments(DynamicObject method, InternalMethod internalMeth
113115
internalMethod.getDeclarationFrame(),
114116
null, Layouts.METHOD.getReceiver(method),
115117
procOrNullNode.executeProcOrNull(block),
118+
DeclarationContext.METHOD,
116119
arguments);
117120
}
118121

truffle/src/main/java/org/jruby/truffle/nodes/core/ModuleNodes.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.oracle.truffle.api.source.Source;
2222
import com.oracle.truffle.api.source.SourceSection;
2323
import com.oracle.truffle.api.utilities.ConditionProfile;
24+
2425
import org.jcodings.Encoding;
2526
import org.jcodings.specific.UTF8Encoding;
2627
import org.jruby.runtime.Visibility;
@@ -45,6 +46,7 @@
4546
import org.jruby.truffle.nodes.methods.AddMethodNode;
4647
import org.jruby.truffle.nodes.methods.CanBindMethodToModuleNode;
4748
import org.jruby.truffle.nodes.methods.CanBindMethodToModuleNodeGen;
49+
import org.jruby.truffle.nodes.methods.DeclarationContext;
4850
import org.jruby.truffle.nodes.methods.SetMethodDeclarationContext;
4951
import org.jruby.truffle.nodes.objects.*;
5052
import org.jruby.truffle.nodes.yield.YieldDispatchHeadNode;
@@ -605,7 +607,7 @@ public abstract static class ClassEvalNode extends CoreMethodArrayArgumentsNode
605607

606608
public ClassEvalNode(RubyContext context, SourceSection sourceSection) {
607609
super(context, sourceSection);
608-
yield = new YieldDispatchHeadNode(context);
610+
yield = new YieldDispatchHeadNode(context, DeclarationContext.CLASS_EVAL);
609611
}
610612

611613
protected DynamicObject toStr(VirtualFrame frame, Object object) {
@@ -685,7 +687,7 @@ public abstract static class ClassExecNode extends CoreMethodArrayArgumentsNode
685687

686688
public ClassExecNode(RubyContext context, SourceSection sourceSection) {
687689
super(context, sourceSection);
688-
yield = new YieldDispatchHeadNode(context);
690+
yield = new YieldDispatchHeadNode(context, DeclarationContext.CLASS_EVAL);
689691
}
690692

691693
public abstract Object executeClassExec(VirtualFrame frame, DynamicObject self, Object[] args, DynamicObject block);

truffle/src/main/java/org/jruby/truffle/nodes/core/ProcNodes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import com.oracle.truffle.api.object.Shape;
2323
import com.oracle.truffle.api.source.NullSourceSection;
2424
import com.oracle.truffle.api.source.SourceSection;
25+
2526
import org.jcodings.specific.UTF8Encoding;
2627
import org.jruby.runtime.ArgumentDescriptor;
2728
import org.jruby.runtime.Helpers;
2829
import org.jruby.truffle.nodes.RubyGuards;
2930
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
3031
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
32+
import org.jruby.truffle.nodes.methods.DeclarationContext;
3133
import org.jruby.truffle.nodes.objects.AllocateObjectNode;
3234
import org.jruby.truffle.nodes.objects.AllocateObjectNodeGen;
3335
import org.jruby.truffle.nodes.yield.YieldDispatchHeadNode;
@@ -51,6 +53,7 @@ public static Object[] packArguments(DynamicObject proc, Object... args) {
5153
Layouts.PROC.getDeclarationFrame(proc),
5254
null, Layouts.PROC.getSelf(proc),
5355
Layouts.PROC.getBlock(proc),
56+
DeclarationContext.BLOCK,
5457
args);
5558
}
5659

truffle/src/main/java/org/jruby/truffle/nodes/core/YieldingCoreMethodNode.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import com.oracle.truffle.api.frame.VirtualFrame;
1414
import com.oracle.truffle.api.object.DynamicObject;
1515
import com.oracle.truffle.api.source.SourceSection;
16+
1617
import org.jruby.truffle.nodes.RubyGuards;
1718
import org.jruby.truffle.nodes.cast.BooleanCastNode;
1819
import org.jruby.truffle.nodes.cast.BooleanCastNodeGen;
20+
import org.jruby.truffle.nodes.methods.DeclarationContext;
1921
import org.jruby.truffle.nodes.yield.YieldDispatchHeadNode;
2022
import org.jruby.truffle.runtime.RubyContext;
2123

@@ -26,7 +28,7 @@ public abstract class YieldingCoreMethodNode extends CoreMethodArrayArgumentsNod
2628

2729
public YieldingCoreMethodNode(RubyContext context, SourceSection sourceSection) {
2830
super(context, sourceSection);
29-
dispatchNode = new YieldDispatchHeadNode(context);
31+
dispatchNode = new YieldDispatchHeadNode(context, DeclarationContext.BLOCK);
3032
}
3133

3234
private boolean booleanCast(VirtualFrame frame, Object value) {
@@ -47,9 +49,4 @@ public boolean yieldIsTruthy(VirtualFrame frame, DynamicObject block, Object...
4749
return booleanCast(frame, yield(frame, block, arguments));
4850
}
4951

50-
public Object yieldWithModifiedSelf(VirtualFrame frame, DynamicObject block, Object self, Object... arguments) {
51-
assert block == null || RubyGuards.isRubyProc(block);
52-
return dispatchNode.dispatchWithModifiedSelf(frame, block, self, arguments);
53-
}
54-
5552
}

truffle/src/main/java/org/jruby/truffle/nodes/core/array/ArrayNodes.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.oracle.truffle.api.object.DynamicObject;
2525
import com.oracle.truffle.api.source.SourceSection;
2626
import com.oracle.truffle.api.utilities.BranchProfile;
27+
2728
import org.jcodings.specific.USASCIIEncoding;
2829
import org.jcodings.specific.UTF8Encoding;
2930
import org.jruby.truffle.nodes.RubyGuards;
@@ -41,6 +42,7 @@
4142
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
4243
import org.jruby.truffle.nodes.dispatch.MissingBehavior;
4344
import org.jruby.truffle.nodes.locals.ReadDeclarationVariableNode;
45+
import org.jruby.truffle.nodes.methods.DeclarationContext;
4446
import org.jruby.truffle.nodes.objects.*;
4547
import org.jruby.truffle.nodes.yield.YieldDispatchHeadNode;
4648
import org.jruby.truffle.pack.parser.PackParser;
@@ -2251,7 +2253,7 @@ public Object max(VirtualFrame frame, DynamicObject array) {
22512253

22522254
final InternalMethod method = RubyArguments.getMethod(frame.getArguments());
22532255
final VirtualFrame maximumClosureFrame = Truffle.getRuntime().createVirtualFrame(
2254-
RubyArguments.pack(method, null, null, array, null, new Object[] {}), maxBlock.getFrameDescriptor());
2256+
RubyArguments.pack(method, null, null, array, null, DeclarationContext.BLOCK, new Object[] {}), maxBlock.getFrameDescriptor());
22552257
maximumClosureFrame.setObject(maxBlock.getFrameSlot(), maximum);
22562258

22572259
final DynamicObject block = ProcNodes.createRubyProc(getContext().getCoreLibrary().getProcFactory(), ProcNodes.Type.PROC,
@@ -2356,7 +2358,7 @@ public Object min(VirtualFrame frame, DynamicObject array) {
23562358

23572359
final InternalMethod method = RubyArguments.getMethod(frame.getArguments());
23582360
final VirtualFrame minimumClosureFrame = Truffle.getRuntime().createVirtualFrame(
2359-
RubyArguments.pack(method, null, null, array, null, new Object[] {}), minBlock.getFrameDescriptor());
2361+
RubyArguments.pack(method, null, null, array, null, DeclarationContext.BLOCK, new Object[] {}), minBlock.getFrameDescriptor());
23602362
minimumClosureFrame.setObject(minBlock.getFrameSlot(), minimum);
23612363

23622364
final DynamicObject block = ProcNodes.createRubyProc(getContext().getCoreLibrary().getProcFactory(), ProcNodes.Type.PROC,

truffle/src/main/java/org/jruby/truffle/nodes/dispatch/CachedDispatchNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import com.oracle.truffle.api.nodes.DirectCallNode;
1515
import com.oracle.truffle.api.object.DynamicObject;
1616
import com.oracle.truffle.api.utilities.BranchProfile;
17+
1718
import org.jruby.truffle.nodes.RubyGuards;
19+
import org.jruby.truffle.nodes.methods.DeclarationContext;
1820
import org.jruby.truffle.runtime.RubyArguments;
1921
import org.jruby.truffle.runtime.RubyContext;
2022
import org.jruby.truffle.runtime.layouts.Layouts;
@@ -102,6 +104,7 @@ protected static Object call(DirectCallNode callNode, VirtualFrame frame, Intern
102104
method.getSharedMethodInfo().needsCallerFrame() ? frame.materialize() : null,
103105
receiver,
104106
block,
107+
DeclarationContext.METHOD,
105108
arguments));
106109
}
107110
}

0 commit comments

Comments
 (0)