Skip to content

Commit ba5d299

Browse files
committed
Store Arity and ArgumentsDescriptor[] directly in RubyProc
* That way RubyProc#getSharedMethodInfo() is always consistent with the RubyRootNode#sharedMethodInfo.
1 parent fae9df9 commit ba5d299

File tree

11 files changed

+66
-39
lines changed

11 files changed

+66
-39
lines changed

src/main/java/org/truffleruby/core/fiber/FiberManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public FiberManager(RubyLanguage language, RubyContext context) {
5858
}
5959

6060
public void initialize(RubyFiber fiber, boolean blocking, RubyProc block, Node currentNode) {
61-
final SourceSection sourceSection = block.sharedMethodInfo.getSourceSection();
61+
final SourceSection sourceSection = block.getSharedMethodInfo().getSourceSection();
6262
fiber.sourceLocation = RubyLanguage.fileLine(sourceSection);
6363
fiber.blocking = blocking;
6464

@@ -99,7 +99,7 @@ private void fiberMain(RubyContext context, RubyFiber fiber, RubyProc block, Nod
9999
assertNotEntered("Fibers should start unentered to avoid triggering multithreading");
100100

101101
final FiberPoolThread thread = (FiberPoolThread) Thread.currentThread();
102-
final SourceSection sourceSection = block.sharedMethodInfo.getSourceSection();
102+
final SourceSection sourceSection = block.getSharedMethodInfo().getSourceSection();
103103
final String oldName = thread.getName();
104104
thread.setName(NAME_PREFIX + " id=" + thread.getId() + " from " + RubyLanguage.fileLine(sourceSection));
105105

src/main/java/org/truffleruby/core/hash/library/HashStoreLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected Object yieldPair(RubyProc block, Object key, Object value,
135135
@Cached ConditionProfile arityMoreThanOne) {
136136
// MRI behavior, see rb_hash_each_pair()
137137
// We use getMethodArityNumber() here since for non-lambda the semantics are the same for both branches
138-
if (arityMoreThanOne.profile(block.sharedMethodInfo.getArity().getMethodArityNumber() > 1)) {
138+
if (arityMoreThanOne.profile(block.arity.getMethodArityNumber() > 1)) {
139139
return yieldNode.yield(block, key, value);
140140
} else {
141141
return yieldNode.yield(block, createArray(new Object[]{ key, value }));

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ private RubySymbol defineMethod(RubyModule module, String name, RubyProc proc,
13341334
MaterializedFrame callerFrame) {
13351335
final RootCallTarget callTargetForLambda = proc.callTargets.getCallTargetForLambda();
13361336
final RubyLambdaRootNode rootNode = RubyLambdaRootNode.of(callTargetForLambda);
1337-
final SharedMethodInfo info = proc.sharedMethodInfo.forDefineMethod(module, name);
1337+
final SharedMethodInfo info = proc.getSharedMethodInfo().forDefineMethod(module, name);
13381338
final RubyNode body = rootNode.copyBody();
13391339
final RubyNode newBody = new CallMethodWithLambdaBody(isSingleContext() ? proc : null,
13401340
callTargetForLambda, body);

src/main/java/org/truffleruby/core/proc/ProcNodes.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.truffleruby.language.dispatch.DispatchNode;
4141
import org.truffleruby.language.locals.FindDeclarationVariableNodes.FindAndReadDeclarationVariableNode;
4242
import org.truffleruby.language.methods.Arity;
43-
import org.truffleruby.language.methods.SharedMethodInfo;
4443
import org.truffleruby.language.objects.AllocationTracing;
4544
import org.truffleruby.language.objects.LogicalClassNode;
4645
import org.truffleruby.language.yield.CallBlockNode;
@@ -119,7 +118,8 @@ protected RubyProc procSpecial(VirtualFrame frame, RubyClass procClass, Object[]
119118
procClass,
120119
getLanguage().procShape,
121120
block.type,
122-
block.sharedMethodInfo,
121+
block.arity,
122+
block.argumentDescriptors,
123123
block.callTargets,
124124
block.callTarget,
125125
block.declarationFrame,
@@ -157,7 +157,8 @@ protected RubyProc dup(RubyProc proc) {
157157
logicalClass,
158158
getLanguage().procShape,
159159
proc.type,
160-
proc.sharedMethodInfo,
160+
proc.arity,
161+
proc.argumentDescriptors,
161162
proc.callTargets,
162163
proc.callTarget,
163164
proc.declarationFrame,
@@ -187,7 +188,7 @@ public abstract static class BindingNode extends CoreMethodArrayArgumentsNode {
187188
@Specialization
188189
protected RubyBinding binding(RubyProc proc) {
189190
final MaterializedFrame frame = proc.declarationFrame;
190-
final SourceSection sourceSection = proc.sharedMethodInfo.getSourceSection();
191+
final SourceSection sourceSection = proc.getSharedMethodInfo().getSourceSection();
191192
return BindingNodes.createBinding(getContext(), getLanguage(), frame, sourceSection);
192193
}
193194
}
@@ -247,7 +248,7 @@ public abstract static class ParametersNode extends CoreMethodArrayArgumentsNode
247248
@TruffleBoundary
248249
@Specialization
249250
protected RubyArray parameters(RubyProc proc) {
250-
final ArgumentDescriptor[] argsDesc = proc.sharedMethodInfo.getArgumentDescriptors();
251+
final ArgumentDescriptor[] argsDesc = proc.getArgumentDescriptors();
251252
final boolean isLambda = proc.type == ProcType.LAMBDA;
252253
return ArgumentDescriptorUtils
253254
.argumentDescriptorsToParameters(getLanguage(), getContext(), argsDesc, isLambda);
@@ -263,7 +264,7 @@ public abstract static class SourceLocationNode extends CoreMethodArrayArguments
263264
@TruffleBoundary
264265
@Specialization
265266
protected Object sourceLocation(RubyProc proc) {
266-
SourceSection sourceSection = proc.sharedMethodInfo.getSourceSection();
267+
SourceSection sourceSection = proc.getSharedMethodInfo().getSourceSection();
267268

268269
final String sourcePath = getLanguage().getSourcePath(sourceSection.getSource());
269270
if (!sourceSection.isAvailable() || sourcePath.endsWith("/lib/truffle/truffle/cext.rb")) {
@@ -286,8 +287,9 @@ public abstract static class ProcCreateSameArityNode extends PrimitiveArrayArgum
286287

287288
@Specialization
288289
protected RubyProc createSameArityProc(RubyProc userProc, RubyProc block) {
289-
final RubyProc composedProc = block.withSharedMethodInfo(
290-
userProc.sharedMethodInfo,
290+
final RubyProc composedProc = block.withArity(
291+
userProc.arity,
292+
userProc.argumentDescriptors,
291293
coreLibrary().procClass,
292294
getLanguage().procShape);
293295
AllocationTracing.trace(composedProc, this);
@@ -299,17 +301,16 @@ protected RubyProc createSameArityProc(RubyProc userProc, RubyProc block) {
299301
public abstract static class ProcSpecifyArityNode extends PrimitiveArrayArgumentsNode {
300302
@Specialization
301303
protected RubyProc specifyArityProc(RubyProc block, int argc) {
302-
Arity oldArity = block.sharedMethodInfo.getArity();
303304
final Arity newArity;
304305
if (argc <= -1) {
305306
newArity = new Arity(-(argc + 1), 0, true);
306307
} else {
307308
newArity = new Arity(argc, 0, false);
308309
}
309310

310-
SharedMethodInfo newSharedMethodInfo = block.sharedMethodInfo.withArity(newArity);
311-
final RubyProc composedProc = block.withSharedMethodInfo(
312-
newSharedMethodInfo,
311+
final RubyProc composedProc = block.withArity(
312+
newArity,
313+
null,
313314
coreLibrary().procClass,
314315
getLanguage().procShape);
315316
AllocationTracing.trace(composedProc, this);
@@ -322,8 +323,8 @@ public abstract static class ProcSymbolToProcSymbolNode extends PrimitiveArrayAr
322323

323324
@Specialization
324325
protected Object symbolToProcSymbol(RubyProc proc) {
325-
if (proc.sharedMethodInfo.getArity() == SymbolNodes.ToProcNode.ARITY) {
326-
return getSymbol(proc.sharedMethodInfo.getBacktraceName());
326+
if (proc.arity == SymbolNodes.ToProcNode.ARITY) {
327+
return getSymbol(proc.getSharedMethodInfo().getBacktraceName());
327328
} else {
328329
return nil;
329330
}
@@ -364,7 +365,7 @@ protected Object singleBlockArg(VirtualFrame frame,
364365
public abstract static class ProcRuby2KeywordsNode extends PrimitiveArrayArgumentsNode {
365366
@Specialization
366367
protected Object ruby2Keywords(RubyProc proc) {
367-
return MethodRuby2KeywordsNode.ruby2Keywords(proc.sharedMethodInfo, proc.callTarget);
368+
return MethodRuby2KeywordsNode.ruby2Keywords(proc.getSharedMethodInfo(), proc.callTarget);
368369
}
369370
}
370371
}

src/main/java/org/truffleruby/core/proc/ProcOperations.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public static RubyProc createRubyProc(
8282
rubyClass,
8383
procShape,
8484
type,
85-
sharedMethodInfo,
85+
sharedMethodInfo.getArity(),
86+
sharedMethodInfo.getRawArgumentDescriptors(),
8687
holder,
8788
callTargetForType,
8889
declarationFrame,
@@ -101,7 +102,7 @@ public static RubyProc convertBlock(RubyContext context, RubyLanguage language,
101102
context.getCoreLibrary().procClass,
102103
language.procShape,
103104
type,
104-
block.sharedMethodInfo,
105+
block.getSharedMethodInfo(),
105106
block.callTargets,
106107
block.declarationFrame,
107108
block.declarationVariables,

src/main/java/org/truffleruby/core/proc/RubyProc.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import org.truffleruby.interop.ForeignToRubyArgumentsNode;
1717
import org.truffleruby.language.Nil;
1818
import org.truffleruby.language.RubyDynamicObject;
19+
import org.truffleruby.language.RubyRootNode;
1920
import org.truffleruby.language.control.FrameOnStackMarker;
21+
import org.truffleruby.language.methods.Arity;
2022
import org.truffleruby.language.methods.DeclarationContext;
2123
import org.truffleruby.language.methods.InternalMethod;
2224
import org.truffleruby.language.methods.SharedMethodInfo;
@@ -33,26 +35,33 @@
3335
import com.oracle.truffle.api.library.ExportMessage;
3436
import com.oracle.truffle.api.object.Shape;
3537
import com.oracle.truffle.api.source.SourceSection;
38+
import org.truffleruby.parser.ArgumentDescriptor;
3639

3740
@ExportLibrary(InteropLibrary.class)
3841
public class RubyProc extends RubyDynamicObject implements ObjectGraphNode {
3942

40-
public final ProcType type;
41-
public final SharedMethodInfo sharedMethodInfo;
42-
public final ProcCallTargets callTargets;
43+
// Accessed for calling a RubyProc
4344
public final RootCallTarget callTarget;
4445
public final MaterializedFrame declarationFrame;
45-
public final SpecialVariableStorage declarationVariables;
4646
public final InternalMethod method;
47-
public final Object block;
4847
public final FrameOnStackMarker frameOnStackMarker;
4948
public final DeclarationContext declarationContext;
49+
// Not accessed for calling a RubyProc
50+
public final ProcType type;
51+
public final ProcCallTargets callTargets;
52+
/** Can differ from {@link SharedMethodInfo#getArity()} */
53+
public final Arity arity;
54+
/** Can differ from {@link SharedMethodInfo#getRawArgumentDescriptors()} */
55+
public final ArgumentDescriptor[] argumentDescriptors;
56+
public final SpecialVariableStorage declarationVariables;
57+
public final Object block;
5058

5159
public RubyProc(
5260
RubyClass rubyClass,
5361
Shape shape,
5462
ProcType type,
55-
SharedMethodInfo sharedMethodInfo,
63+
Arity arity,
64+
ArgumentDescriptor[] argumentDescriptors,
5665
ProcCallTargets callTargets,
5766
RootCallTarget callTarget,
5867
MaterializedFrame declarationFrame,
@@ -64,7 +73,8 @@ public RubyProc(
6473
super(rubyClass, shape);
6574
assert block instanceof Nil || block instanceof RubyProc : StringUtils.toString(block);
6675
this.type = type;
67-
this.sharedMethodInfo = sharedMethodInfo;
76+
this.arity = arity;
77+
this.argumentDescriptors = argumentDescriptors;
6878
this.callTargets = callTargets;
6979
this.callTarget = callTarget;
7080
this.declarationFrame = declarationFrame;
@@ -75,12 +85,14 @@ public RubyProc(
7585
this.declarationContext = declarationContext;
7686
}
7787

78-
public RubyProc withSharedMethodInfo(SharedMethodInfo newSharedMethodInfo, RubyClass newRubyClass, Shape newShape) {
88+
public RubyProc withArity(Arity newArity, ArgumentDescriptor[] newArgumentDescriptors, RubyClass newRubyClass,
89+
Shape newShape) {
7990
return new RubyProc(
8091
newRubyClass,
8192
newShape,
8293
type,
83-
newSharedMethodInfo,
94+
newArity,
95+
newArgumentDescriptors,
8496
callTargets,
8597
callTarget,
8698
declarationFrame,
@@ -108,6 +120,18 @@ public boolean isProc() {
108120
return type == ProcType.PROC;
109121
}
110122

123+
public SharedMethodInfo getSharedMethodInfo() {
124+
return RubyRootNode.of(callTarget).getSharedMethodInfo();
125+
}
126+
127+
public int getArityNumber() {
128+
return arity.getArityNumber(type);
129+
}
130+
131+
public ArgumentDescriptor[] getArgumentDescriptors() {
132+
return argumentDescriptors == null ? arity.toAnonymousArgumentDescriptors() : argumentDescriptors;
133+
}
134+
111135
// region SourceLocation
112136
@ExportMessage
113137
public boolean hasSourceLocation() {
@@ -116,14 +140,10 @@ public boolean hasSourceLocation() {
116140

117141
@ExportMessage
118142
public SourceSection getSourceLocation() {
119-
return sharedMethodInfo.getSourceSection();
143+
return getSharedMethodInfo().getSourceSection();
120144
}
121145
// endregion
122146

123-
public int getArityNumber() {
124-
return sharedMethodInfo.getArity().getArityNumber(type);
125-
}
126-
127147
// region Executable
128148
@ExportMessage
129149
public boolean isExecutable() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ protected Object initialize(VirtualFrame frame, RubyThread thread) {
423423

424424
@TruffleBoundary
425425
private Object init(RubyThread thread, RubyProc block, ArgumentsDescriptor descriptor, Object[] args) {
426-
final SourceSection sourceSection = block.sharedMethodInfo.getSourceSection();
426+
final SourceSection sourceSection = block.getSharedMethodInfo().getSourceSection();
427427
final String info = RubyLanguage.fileLine(sourceSection);
428428
final String sharingReason = "creating Ruby Thread " + info;
429429

src/main/java/org/truffleruby/extra/TruffleGraalNodes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ protected RubyProc copyCapturedLocals(RubyProc proc) {
125125
coreLibrary().procClass,
126126
getLanguage().procShape,
127127
ProcType.LAMBDA,
128-
proc.sharedMethodInfo,
128+
proc.arity,
129+
proc.argumentDescriptors,
129130
new ProcCallTargets(newCallTarget),
130131
newCallTarget,
131132
newDeclarationFrame,

src/main/java/org/truffleruby/language/methods/SharedMethodInfo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public Arity getArity() {
113113
return arity;
114114
}
115115

116+
public ArgumentDescriptor[] getRawArgumentDescriptors() {
117+
return argumentDescriptors;
118+
}
119+
116120
public ArgumentDescriptor[] getArgumentDescriptors() {
117121
return argumentDescriptors == null ? arity.toAnonymousArgumentDescriptors() : argumentDescriptors;
118122
}

src/main/java/org/truffleruby/language/objects/shared/SharedObjects.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static void shareContextRoots(RubyLanguage language, RubyContext context
8282

8383
public static void shareDeclarationFrame(RubyLanguage language, RubyProc block) {
8484
if (language.options.SHARED_OBJECTS_DEBUG) {
85-
final SourceSection sourceSection = block.sharedMethodInfo.getSourceSection();
85+
final SourceSection sourceSection = block.getSharedMethodInfo().getSourceSection();
8686
RubyLanguage.LOGGER.info("sharing decl frame of " + RubyLanguage.fileLine(sourceSection));
8787
}
8888

0 commit comments

Comments
 (0)