Skip to content

Commit a3dcf3d

Browse files
committed
[GR-33075] Migrate to the new ContextReference and LanguageReference
PullRequest: truffleruby/2864
2 parents 7559266 + 07e6c50 commit a3dcf3d

File tree

145 files changed

+786
-1353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+786
-1353
lines changed

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.oracle.truffle.api.CompilerAsserts;
2828
import com.oracle.truffle.api.CompilerDirectives;
2929
import com.oracle.truffle.api.RootCallTarget;
30+
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
3031
import com.oracle.truffle.api.TruffleLogger;
3132
import com.oracle.truffle.api.nodes.EncapsulatingNodeReference;
3233
import com.oracle.truffle.api.nodes.Node;
@@ -157,6 +158,12 @@ public class RubyContext {
157158
private final AssumedValue<Boolean> warningCategoryDeprecated;
158159
private final AssumedValue<Boolean> warningCategoryExperimental;
159160

161+
private static final ContextReference<RubyContext> REFERENCE = ContextReference.create(RubyLanguage.class);
162+
163+
public static RubyContext get(Node node) {
164+
return REFERENCE.get(node);
165+
}
166+
160167
public RubyContext(RubyLanguage language, TruffleLanguage.Env env) {
161168
Metrics.printTime("before-context-constructor");
162169

@@ -535,7 +542,7 @@ public Hashing getHashing() {
535542

536543
public RubyLanguage getLanguageSlow() {
537544
CompilerAsserts.neverPartOfCompilation(
538-
"Use getLanguage() or @CachedLanguage instead, so the RubyLanguage instance is constant in PE code");
545+
"Use getLanguage() or RubyLanguage.get(Node) instead, so the RubyLanguage instance is constant in PE code");
539546
return language;
540547
}
541548

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.oracle.truffle.api.TruffleFile;
2525
import com.oracle.truffle.api.instrumentation.AllocationReporter;
2626
import com.oracle.truffle.api.instrumentation.Instrumenter;
27+
import com.oracle.truffle.api.nodes.Node;
2728
import com.oracle.truffle.api.object.Shape;
2829
import com.oracle.truffle.api.source.Source;
2930
import com.oracle.truffle.api.source.SourceSection;
@@ -269,6 +270,12 @@ public final class RubyLanguage extends TruffleLanguage<RubyContext> {
269270

270271
public final ThreadLocal<ParsingParameters> parsingRequestParams = new ThreadLocal<>();
271272

273+
private static final LanguageReference<RubyLanguage> REFERENCE = LanguageReference.create(RubyLanguage.class);
274+
275+
public static RubyLanguage get(Node node) {
276+
return REFERENCE.get(node);
277+
}
278+
272279
public RubyLanguage() {
273280
coreMethodAssumptions = new CoreMethodAssumptions(this);
274281
coreStrings = new CoreStrings(this);
@@ -456,13 +463,13 @@ protected void disposeContext(RubyContext context) {
456463
}
457464

458465
public static RubyContext getCurrentContext() {
459-
CompilerAsserts.neverPartOfCompilation("Use getContext() or @CachedContext instead in PE code");
460-
return getCurrentContext(RubyLanguage.class);
466+
CompilerAsserts.neverPartOfCompilation("Use getContext() or RubyContext.get(Node) instead in PE code");
467+
return RubyContext.get(null);
461468
}
462469

463470
public static RubyLanguage getCurrentLanguage() {
464-
CompilerAsserts.neverPartOfCompilation("Use getLanguage() or @CachedLanguage instead in PE code");
465-
return getCurrentLanguage(RubyLanguage.class);
471+
CompilerAsserts.neverPartOfCompilation("Use getLanguage() or RubyLanguage.get(Node) instead in PE code");
472+
return RubyLanguage.get(null);
466473
}
467474

468475
@Override
@@ -480,7 +487,7 @@ protected RootCallTarget parse(ParsingRequest request) {
480487
? ParserContext.TOP_LEVEL_FIRST
481488
: ParserContext.TOP_LEVEL;
482489
final LexicalScope lexicalScope = contextIfSingleContext.map(RubyContext::getRootLexicalScope).orElse(null);
483-
return RubyLanguage.getCurrentContext().getCodeLoader().parse(
490+
return getCurrentContext().getCodeLoader().parse(
484491
rubySource,
485492
parserContext,
486493
null,

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
import org.truffleruby.interop.TranslateInteropExceptionNode;
7171
import org.truffleruby.core.string.ImmutableRubyString;
7272
import org.truffleruby.language.LexicalScope;
73-
import org.truffleruby.language.RubyContextNode;
73+
import org.truffleruby.language.RubyBaseNode;
7474
import org.truffleruby.language.RubyDynamicObject;
7575
import org.truffleruby.language.RubyGuards;
7676
import org.truffleruby.language.RubyNode;
@@ -1016,7 +1016,7 @@ protected int size(Object string,
10161016

10171017
}
10181018

1019-
public abstract static class StringToNativeNode extends RubyContextNode {
1019+
public abstract static class StringToNativeNode extends RubyBaseNode {
10201020

10211021
public static StringToNativeNode create() {
10221022
return StringToNativeNodeGen.create();

src/main/java/org/truffleruby/cext/IDToSymbolNode.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@
1111

1212
import static org.truffleruby.core.symbol.CoreSymbols.idToIndex;
1313

14-
import com.oracle.truffle.api.dsl.CachedLanguage;
15-
import org.truffleruby.RubyContext;
16-
import org.truffleruby.RubyLanguage;
1714
import org.truffleruby.core.string.StringUtils;
1815
import org.truffleruby.core.symbol.CoreSymbols;
1916
import org.truffleruby.core.symbol.RubySymbol;
2017
import org.truffleruby.language.RubyBaseNode;
2118
import org.truffleruby.language.control.RaiseException;
2219

2320
import com.oracle.truffle.api.dsl.Cached;
24-
import com.oracle.truffle.api.dsl.CachedContext;
2521
import com.oracle.truffle.api.dsl.GenerateUncached;
2622
import com.oracle.truffle.api.dsl.ReportPolymorphism;
2723
import com.oracle.truffle.api.dsl.Specialization;
@@ -39,16 +35,14 @@ public static IDToSymbolNode create() {
3935

4036
@Specialization(guards = "isStaticSymbol(value)")
4137
protected Object unwrapStaticSymbol(long value,
42-
@CachedLanguage RubyLanguage language,
43-
@CachedContext(RubyLanguage.class) RubyContext context,
4438
@Cached BranchProfile errorProfile) {
4539
final int index = idToIndex(value);
46-
final RubySymbol symbol = language.coreSymbols.STATIC_SYMBOLS[index];
40+
final RubySymbol symbol = getLanguage().coreSymbols.STATIC_SYMBOLS[index];
4741
if (symbol == null) {
4842
errorProfile.enter();
4943
throw new RaiseException(
50-
context,
51-
context.getCoreExceptions().runtimeError(
44+
getContext(),
45+
coreExceptions().runtimeError(
5246
StringUtils.format("invalid static ID2SYM id: %d", value),
5347
this));
5448
}

src/main/java/org/truffleruby/cext/UnwrapNode.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,15 @@
1818
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
1919
import com.oracle.truffle.api.nodes.ExplodeLoop;
2020
import com.oracle.truffle.api.profiles.LoopConditionProfile;
21-
import org.truffleruby.RubyContext;
22-
import org.truffleruby.RubyLanguage;
2321
import org.truffleruby.cext.UnwrapNodeGen.NativeToWrapperNodeGen;
2422
import org.truffleruby.cext.UnwrapNodeGen.ToWrapperNodeGen;
2523
import org.truffleruby.cext.UnwrapNodeGen.UnwrapNativeNodeGen;
2624
import org.truffleruby.language.NotProvided;
2725
import org.truffleruby.language.RubyBaseNode;
28-
import org.truffleruby.language.RubyContextNode;
2926
import org.truffleruby.language.control.RaiseException;
3027

3128
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3229
import com.oracle.truffle.api.dsl.Cached;
33-
import com.oracle.truffle.api.dsl.CachedContext;
34-
import com.oracle.truffle.api.dsl.CachedLanguage;
3530
import com.oracle.truffle.api.dsl.Fallback;
3631
import com.oracle.truffle.api.dsl.GenerateUncached;
3732
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -78,10 +73,10 @@ protected long unwrapTaggedLong(long handle) {
7873

7974
@Specialization(guards = "isTaggedObject(handle)")
8075
protected Object unwrapTaggedObject(long handle,
81-
@CachedContext(RubyLanguage.class) RubyContext context,
82-
@CachedLanguage RubyLanguage language,
8376
@Cached BranchProfile noHandleProfile) {
84-
final ValueWrapper wrapper = context.getValueWrapperManager().getWrapperFromHandleMap(handle, language);
77+
final ValueWrapper wrapper = getContext()
78+
.getValueWrapperManager()
79+
.getWrapperFromHandleMap(handle, getLanguage());
8580
if (wrapper == null) {
8681
noHandleProfile.enter();
8782
raiseError(handle);
@@ -139,10 +134,8 @@ protected ValueWrapper unwrapTaggedLong(long handle) {
139134
}
140135

141136
@Specialization(guards = "isTaggedObject(handle)")
142-
protected ValueWrapper unwrapTaggedObject(long handle,
143-
@CachedContext(RubyLanguage.class) RubyContext context,
144-
@CachedLanguage RubyLanguage language) {
145-
return context.getValueWrapperManager().getWrapperFromHandleMap(handle, language);
137+
protected ValueWrapper unwrapTaggedObject(long handle) {
138+
return getContext().getValueWrapperManager().getWrapperFromHandleMap(handle, getLanguage());
146139
}
147140

148141
@Fallback
@@ -158,7 +151,7 @@ public static NativeToWrapperNode create() {
158151
}
159152

160153
@ImportStatic(ValueWrapperManager.class)
161-
public abstract static class ToWrapperNode extends RubyContextNode {
154+
public abstract static class ToWrapperNode extends RubyBaseNode {
162155

163156
public abstract ValueWrapper execute(Object value);
164157

@@ -198,7 +191,7 @@ protected int getCacheLimit() {
198191
}
199192

200193
@ImportStatic(ValueWrapperManager.class)
201-
public abstract static class UnwrapCArrayNode extends RubyContextNode {
194+
public abstract static class UnwrapCArrayNode extends RubyBaseNode {
202195

203196
public abstract Object[] execute(Object cArray);
204197

@@ -280,20 +273,19 @@ protected Object longToWrapper(long value,
280273
@Specialization(guards = { "!isWrapper(value)", "values.isPointer(value)" }, limit = "getCacheLimit()")
281274
protected Object unwrapGeneric(Object value,
282275
@CachedLibrary("value") InteropLibrary values,
283-
@CachedContext(RubyLanguage.class) RubyContext context,
284276
@Cached UnwrapNativeNode unwrapNativeNode,
285277
@Cached BranchProfile unsupportedProfile) {
286278
long handle;
287279
try {
288280
handle = values.asPointer(value);
289281
} catch (UnsupportedMessageException e) {
290282
unsupportedProfile.enter();
291-
throw new RaiseException(context, context.getCoreExceptions().argumentError(e.getMessage(), this, e));
283+
throw new RaiseException(getContext(), coreExceptions().argumentError(e.getMessage(), this, e));
292284
}
293285
return unwrapNativeNode.execute(handle);
294286
}
295287

296288
protected int getCacheLimit() {
297-
return RubyLanguage.getCurrentLanguage().options.DISPATCH_CACHE;
289+
return getLanguage().options.DISPATCH_CACHE;
298290
}
299291
}

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
2828
import com.oracle.truffle.api.dsl.Cached;
29-
import com.oracle.truffle.api.dsl.CachedContext;
30-
import com.oracle.truffle.api.dsl.CachedLanguage;
3129
import com.oracle.truffle.api.dsl.GenerateUncached;
3230
import com.oracle.truffle.api.dsl.Specialization;
3331
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -334,24 +332,22 @@ public abstract static class GetHandleBlockHolderNode extends RubyBaseNode {
334332

335333
@Specialization(guards = "cachedThread == currentJavaThread(wrapper)", limit = "getCacheLimit()")
336334
protected HandleThreadData getHolderOnKnownThread(ValueWrapper wrapper,
337-
@CachedContext(RubyLanguage.class) RubyContext context,
338335
@Cached("currentJavaThread(wrapper)") Thread cachedThread,
339-
@Cached("getBlockHolder(wrapper, context)") HandleThreadData threadData) {
336+
@Cached("getBlockHolder(wrapper)") HandleThreadData threadData) {
340337
return threadData;
341338
}
342339

343340
@Specialization(replaces = "getHolderOnKnownThread")
344-
protected HandleThreadData getBlockHolder(ValueWrapper wrapper,
345-
@CachedContext(RubyLanguage.class) RubyContext context) {
346-
return context.getValueWrapperManager().getBlockHolder();
341+
protected HandleThreadData getBlockHolder(ValueWrapper wrapper) {
342+
return getContext().getValueWrapperManager().getBlockHolder();
347343
}
348344

349345
protected static Thread currentJavaThread(ValueWrapper wrapper) {
350346
return Thread.currentThread();
351347
}
352348

353349
public int getCacheLimit() {
354-
return RubyLanguage.getCurrentLanguage().options.THREAD_CACHE;
350+
return getLanguage().options.THREAD_CACHE;
355351
}
356352

357353
public static GetHandleBlockHolderNode create() {
@@ -366,18 +362,14 @@ public abstract static class AllocateHandleNode extends RubyBaseNode {
366362

367363
@Specialization(guards = "!isSharedObject(wrapper)")
368364
protected long allocateHandleOnKnownThread(ValueWrapper wrapper,
369-
@CachedContext(RubyLanguage.class) RubyContext context,
370-
@CachedLanguage RubyLanguage language,
371365
@Cached GetHandleBlockHolderNode getBlockHolderNode) {
372-
return allocateHandle(wrapper, context, language, getBlockHolderNode.execute(wrapper), false);
366+
return allocateHandle(wrapper, getContext(), getLanguage(), getBlockHolderNode.execute(wrapper), false);
373367
}
374368

375369
@Specialization(guards = "isSharedObject(wrapper)")
376370
protected long allocateSharedHandleOnKnownThread(ValueWrapper wrapper,
377-
@CachedContext(RubyLanguage.class) RubyContext context,
378-
@CachedLanguage RubyLanguage language,
379371
@Cached GetHandleBlockHolderNode getBlockHolderNode) {
380-
return allocateHandle(wrapper, context, language, getBlockHolderNode.execute(wrapper), true);
372+
return allocateHandle(wrapper, getContext(), getLanguage(), getBlockHolderNode.execute(wrapper), true);
381373
}
382374

383375
protected static long allocateHandle(ValueWrapper wrapper, RubyContext context, RubyLanguage language,

src/main/java/org/truffleruby/cext/WrapNode.java

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import com.oracle.truffle.api.memory.MemoryFence;
1616
import org.jcodings.specific.UTF8Encoding;
1717
import org.truffleruby.Layouts;
18-
import org.truffleruby.RubyContext;
19-
import org.truffleruby.RubyLanguage;
2018
import org.truffleruby.core.encoding.Encodings;
2119
import org.truffleruby.core.rope.RopeOperations;
2220
import org.truffleruby.language.ImmutableRubyObject;
@@ -28,7 +26,6 @@
2826
import org.truffleruby.language.control.RaiseException;
2927

3028
import com.oracle.truffle.api.dsl.Cached;
31-
import com.oracle.truffle.api.dsl.CachedContext;
3229
import com.oracle.truffle.api.dsl.GenerateUncached;
3330
import com.oracle.truffle.api.dsl.ImportStatic;
3431
import com.oracle.truffle.api.dsl.Specialization;
@@ -48,41 +45,38 @@ public static WrapNode create() {
4845

4946
@Specialization
5047
protected ValueWrapper wrapLong(long value,
51-
@Cached BranchProfile smallFixnumProfile,
52-
@CachedContext(RubyLanguage.class) RubyContext context) {
48+
@Cached BranchProfile smallFixnumProfile) {
5349
if (value >= ValueWrapperManager.MIN_FIXNUM_VALUE && value <= ValueWrapperManager.MAX_FIXNUM_VALUE) {
5450
smallFixnumProfile.enter();
5551
long val = (value << 1) | LONG_TAG;
5652
return new ValueWrapper(null, val, null);
5753
} else {
58-
return context.getValueWrapperManager().longWrapper(value);
54+
return getContext().getValueWrapperManager().longWrapper(value);
5955
}
6056
}
6157

6258
@Specialization
63-
protected ValueWrapper wrapDouble(double value,
64-
@CachedContext(RubyLanguage.class) RubyContext context) {
65-
return context.getValueWrapperManager().doubleWrapper(value);
59+
protected ValueWrapper wrapDouble(double value) {
60+
return getContext().getValueWrapperManager().doubleWrapper(value);
6661
}
6762

6863
@Specialization
69-
protected ValueWrapper wrapBoolean(boolean value,
70-
@CachedContext(RubyLanguage.class) RubyContext context) {
71-
return value ? context.getValueWrapperManager().trueWrapper : context.getValueWrapperManager().falseWrapper;
64+
protected ValueWrapper wrapBoolean(boolean value) {
65+
return value
66+
? getContext().getValueWrapperManager().trueWrapper
67+
: getContext().getValueWrapperManager().falseWrapper;
7268
}
7369

7470
@Specialization
75-
protected ValueWrapper wrapUndef(NotProvided value,
76-
@CachedContext(RubyLanguage.class) RubyContext context) {
77-
return context.getValueWrapperManager().undefWrapper;
71+
protected ValueWrapper wrapUndef(NotProvided value) {
72+
return getContext().getValueWrapperManager().undefWrapper;
7873
}
7974

8075
@Specialization
81-
protected ValueWrapper wrapWrappedValue(ValueWrapper value,
82-
@CachedContext(RubyLanguage.class) RubyContext context) {
76+
protected ValueWrapper wrapWrappedValue(ValueWrapper value) {
8377
throw new RaiseException(
84-
context,
85-
context.getCoreExceptions().argumentError(
78+
getContext(),
79+
coreExceptions().argumentError(
8680
RopeOperations.encodeAscii("Wrapping wrapped object", UTF8Encoding.INSTANCE),
8781
Encodings.UTF_8,
8882
this));
@@ -135,14 +129,9 @@ protected ValueWrapper wrapValue(RubyDynamicObject value,
135129
}
136130

137131
@Specialization(guards = "isForeignObject(value)")
138-
protected ValueWrapper wrapNonRubyObject(Object value,
139-
@CachedContext(RubyLanguage.class) RubyContext context) {
132+
protected ValueWrapper wrapNonRubyObject(Object value) {
140133
throw new RaiseException(
141-
context,
142-
context.getCoreExceptions().argumentError("Attempt to wrap something that isn't an Ruby object", this));
143-
}
144-
145-
protected int getDynamicObjectCacheLimit() {
146-
return RubyLanguage.getCurrentLanguage().options.INSTANCE_VARIABLE_CACHE;
134+
getContext(),
135+
coreExceptions().argumentError("Attempt to wrap something that isn't an Ruby object", this));
147136
}
148137
}

0 commit comments

Comments
 (0)