Skip to content

Commit d69729e

Browse files
committed
[GR-33075] Migrate to the new ContextReference and LanguageReference
* Now RubyBaseNode has getLanguage() and getContext() without needing fields! * Remove RubyNode.WithContext as it is no longer needed.
1 parent 966f738 commit d69729e

File tree

15 files changed

+117
-294
lines changed

15 files changed

+117
-294
lines changed

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

Lines changed: 7 additions & 0 deletions
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

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

Lines changed: 7 additions & 0 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);

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,4 @@ protected ValueWrapper wrapNonRubyObject(Object value,
141141
context,
142142
context.getCoreExceptions().argumentError("Attempt to wrap something that isn't an Ruby object", this));
143143
}
144-
145-
protected int getDynamicObjectCacheLimit() {
146-
return RubyLanguage.getCurrentLanguage().options.INSTANCE_VARIABLE_CACHE;
147-
}
148144
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ protected Object appendFeatures(RubyModule features, RubyModule target,
396396
@GenerateUncached
397397
public abstract static class GeneratedReaderNode extends AlwaysInlinedMethodNode {
398398

399-
@Specialization(limit = "getCacheLimit()")
399+
@Specialization(limit = "getDynamicObjectCacheLimit()")
400400
protected Object reader(
401401
Frame callerFrame, RubyDynamicObject self, Object[] args, Object block, RootCallTarget target,
402402
@CachedLibrary("self") DynamicObjectLibrary objectLibrary) {
@@ -411,10 +411,6 @@ protected Object reader(
411411
protected Object notObject(Frame callerFrame, Object self, Object[] args, Object block, RootCallTarget target) {
412412
return nil;
413413
}
414-
415-
protected int getCacheLimit() {
416-
return RubyLanguage.getCurrentLanguage().options.INSTANCE_VARIABLE_CACHE;
417-
}
418414
}
419415

420416
@GenerateUncached

src/main/java/org/truffleruby/core/objectspace/WeakMapNodes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.truffleruby.core.objectspace;
1111

12+
import org.truffleruby.RubyContext;
1213
import org.truffleruby.builtins.CoreMethod;
1314
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1415
import org.truffleruby.builtins.CoreModule;
@@ -186,6 +187,7 @@ private static RubyWeakMap eachNoBlockProvided(YieldingCoreMethodNode node, Ruby
186187
if (map.storage.size() == 0) {
187188
return map;
188189
}
189-
throw new RaiseException(node.getContext(), node.coreExceptions().localJumpError("no block given", node));
190+
final RubyContext context = node.getContext();
191+
throw new RaiseException(context, context.getCoreExceptions().localJumpError("no block given", node));
190192
}
191193
}

src/main/java/org/truffleruby/core/symbol/SymbolNodes.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ protected long hash(RubySymbol symbol,
111111
protected boolean isPreInitializing(RubyContext context) {
112112
return context.isPreInitializing();
113113
}
114-
115-
protected int getIdentityCacheLimit() {
116-
return RubyLanguage.getCurrentContext().getLanguageSlow().options.IDENTITY_CACHE;
117-
}
118114
}
119115

120116
@CoreMethod(names = "hash")

src/main/java/org/truffleruby/interop/InteropNodes.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,6 @@ protected Object executeForeignCached(Object receiver, Object[] args,
540540
final Object foreign = InteropNodes.execute(receiver, convertedArgs, receivers, translateInteropException);
541541
return foreignToRubyNode.executeConvert(foreign);
542542
}
543-
544-
protected static int getInteropCacheLimit() {
545-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
546-
}
547543
}
548544

549545
@CoreMethod(names = "execute_without_conversion", onSingleton = true, required = 1, rest = true)
@@ -597,10 +593,6 @@ protected Object newCached(Object receiver, Object[] args,
597593

598594
return foreignToRubyNode.executeConvert(foreign);
599595
}
600-
601-
protected static int getInteropCacheLimit() {
602-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
603-
}
604596
}
605597
// endregion
606598

@@ -659,10 +651,6 @@ protected Object readArrayElement(Object receiver, long identifier,
659651

660652
return foreignToRubyNode.executeConvert(foreign);
661653
}
662-
663-
protected static int getInteropCacheLimit() {
664-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
665-
}
666654
}
667655

668656
@GenerateUncached
@@ -687,10 +675,6 @@ protected Object readArrayElement(Object receiver, long identifier,
687675
throw translateInteropException.execute(e);
688676
}
689677
}
690-
691-
protected static int getInteropCacheLimit() {
692-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
693-
}
694678
}
695679

696680
@GenerateUncached
@@ -718,10 +702,6 @@ protected Object write(Object receiver, long identifier, Object value,
718702

719703
return value;
720704
}
721-
722-
protected static int getInteropCacheLimit() {
723-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
724-
}
725705
}
726706

727707
@GenerateUncached
@@ -748,10 +728,6 @@ protected Nil readArrayElement(Object receiver, long identifier,
748728

749729
return Nil.INSTANCE;
750730
}
751-
752-
protected static int getInteropCacheLimit() {
753-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
754-
}
755731
}
756732

757733
@CoreMethod(names = "array_element_readable?", onSingleton = true, required = 2)
@@ -1245,11 +1221,6 @@ protected boolean isNull(Object receiver,
12451221
@CachedLibrary("receiver") InteropLibrary receivers) {
12461222
return receivers.isNull(receiver);
12471223
}
1248-
1249-
protected static int getInteropCacheLimit() {
1250-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
1251-
}
1252-
12531224
}
12541225
// endregion
12551226

@@ -1349,11 +1320,6 @@ protected Object readMember(Object receiver, Object identifier,
13491320
final Object foreign = InteropNodes.readMember(receivers, receiver, name, translateInteropException);
13501321
return foreignToRubyNode.executeConvert(foreign);
13511322
}
1352-
1353-
protected static int getInteropCacheLimit() {
1354-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
1355-
}
1356-
13571323
}
13581324

13591325
@GenerateUncached
@@ -1376,11 +1342,6 @@ protected Object readMember(Object receiver, Object identifier,
13761342
final String name = toJavaStringNode.executeToJavaString(identifier);
13771343
return InteropNodes.readMember(receivers, receiver, name, translateInteropException);
13781344
}
1379-
1380-
protected static int getInteropCacheLimit() {
1381-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
1382-
}
1383-
13841345
}
13851346

13861347
@GenerateUncached
@@ -1412,10 +1373,6 @@ protected Object write(Object receiver, Object identifier, Object value,
14121373

14131374
return value;
14141375
}
1415-
1416-
protected static int getInteropCacheLimit() {
1417-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
1418-
}
14191376
}
14201377

14211378
@CoreMethod(names = "remove_member", onSingleton = true, required = 2)
@@ -1461,10 +1418,6 @@ protected Object invokeCached(Object receiver, Object identifier, Object[] args,
14611418
final Object foreign = invoke(receivers, receiver, name, arguments, translateInteropException);
14621419
return foreignToRubyNode.executeConvert(foreign);
14631420
}
1464-
1465-
protected static int getInteropCacheLimit() {
1466-
return RubyLanguage.getCurrentLanguage().options.METHOD_LOOKUP_CACHE;
1467-
}
14681421
}
14691422

14701423
@CoreMethod(names = "member_readable?", onSingleton = true, required = 2)

src/main/java/org/truffleruby/interop/ToJavaStringNode.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,4 @@ protected int getLimit() {
103103
return RubyLanguage.getCurrentLanguage().options.INTEROP_CONVERT_CACHE;
104104
}
105105

106-
protected int getIdentityCacheLimit() {
107-
return RubyLanguage.getCurrentLanguage().options.IDENTITY_CACHE;
108-
}
109-
110106
}

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

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,22 @@
1616
import com.oracle.truffle.api.nodes.LoopNode;
1717
import com.oracle.truffle.api.nodes.Node;
1818
import com.oracle.truffle.api.profiles.LoopConditionProfile;
19+
import org.truffleruby.RubyContext;
1920
import org.truffleruby.RubyLanguage;
21+
import org.truffleruby.core.CoreLibrary;
22+
import org.truffleruby.core.array.ArrayHelpers;
2023
import org.truffleruby.core.array.ArrayUtils;
24+
import org.truffleruby.core.array.RubyArray;
25+
import org.truffleruby.core.encoding.RubyEncoding;
26+
import org.truffleruby.core.exception.CoreExceptions;
27+
import org.truffleruby.core.numeric.BignumOperations;
28+
import org.truffleruby.core.numeric.RubyBignum;
29+
import org.truffleruby.core.rope.Rope;
30+
import org.truffleruby.core.string.CoreStrings;
31+
import org.truffleruby.core.symbol.CoreSymbols;
32+
import org.truffleruby.core.symbol.RubySymbol;
33+
34+
import java.math.BigInteger;
2135

2236
/** Base of all Ruby nodes */
2337
@TypeSystemReference(RubyTypes.class)
@@ -51,14 +65,14 @@ public static void profileAndReportLoopCount(Node node, LoopConditionProfile loo
5165
// Checkstyle: resume
5266
}
5367

54-
public void profileAndReportLoopCount(LoopConditionProfile loopProfile, int count) {
68+
protected void profileAndReportLoopCount(LoopConditionProfile loopProfile, int count) {
5569
// Checkstyle: stop
5670
loopProfile.profileCounted(count);
5771
LoopNode.reportLoopCount(this, count);
5872
// Checkstyle: resume
5973
}
6074

61-
public void profileAndReportLoopCount(LoopConditionProfile loopProfile, long count) {
75+
protected void profileAndReportLoopCount(LoopConditionProfile loopProfile, long count) {
6276
// Checkstyle: stop
6377
loopProfile.profileCounted(count);
6478
reportLongLoopCount(count);
@@ -82,8 +96,85 @@ protected Node getNode() {
8296
}
8397
}
8498

85-
// Prefixed with "base" so as not to conflict with RubyNode.WithContext#getRubyLibraryCacheLimit
86-
public int getRubyLibraryCacheLimit() {
87-
return RubyLanguage.getCurrentLanguage().options.RUBY_LIBRARY_CACHE;
99+
public final RubyLanguage getLanguage() {
100+
return RubyLanguage.get(this);
101+
}
102+
103+
public final RubyContext getContext() {
104+
return RubyContext.get(this);
105+
}
106+
107+
// Helpers methods for terseness. They are `final` so we ensure they are not needlessly redeclared in subclasses.
108+
109+
protected final RubyEncoding getLocaleEncoding() {
110+
return getContext().getEncodingManager().getLocaleEncoding();
111+
}
112+
113+
protected final RubyBignum createBignum(BigInteger value) {
114+
return BignumOperations.createBignum(value);
115+
}
116+
117+
protected final RubySymbol getSymbol(String name) {
118+
return getLanguage().getSymbol(name);
119+
}
120+
121+
protected final RubySymbol getSymbol(Rope name, RubyEncoding encoding) {
122+
return getLanguage().getSymbol(name, encoding);
123+
}
124+
125+
protected final CoreStrings coreStrings() {
126+
return getLanguage().coreStrings;
127+
}
128+
129+
protected final CoreSymbols coreSymbols() {
130+
return getLanguage().coreSymbols;
131+
}
132+
133+
protected final RubyArray createArray(Object store, int size) {
134+
return ArrayHelpers.createArray(getContext(), getLanguage(), store, size);
135+
}
136+
137+
protected final RubyArray createArray(int[] store) {
138+
return ArrayHelpers.createArray(getContext(), getLanguage(), store);
139+
}
140+
141+
protected final RubyArray createArray(long[] store) {
142+
return ArrayHelpers.createArray(getContext(), getLanguage(), store);
143+
}
144+
145+
protected final RubyArray createArray(Object[] store) {
146+
return ArrayHelpers.createArray(getContext(), getLanguage(), store);
147+
}
148+
149+
protected final RubyArray createEmptyArray() {
150+
return ArrayHelpers.createEmptyArray(getContext(), getLanguage());
151+
}
152+
153+
protected final CoreLibrary coreLibrary() {
154+
return getContext().getCoreLibrary();
155+
}
156+
157+
protected final CoreExceptions coreExceptions() {
158+
return getContext().getCoreExceptions();
159+
}
160+
161+
protected final int getIdentityCacheLimit() {
162+
return getLanguage().options.IDENTITY_CACHE;
163+
}
164+
165+
protected final int getDefaultCacheLimit() {
166+
return getLanguage().options.DEFAULT_CACHE;
167+
}
168+
169+
protected final int getDynamicObjectCacheLimit() {
170+
return getLanguage().options.INSTANCE_VARIABLE_CACHE;
171+
}
172+
173+
protected final int getInteropCacheLimit() {
174+
return getLanguage().options.METHOD_LOOKUP_CACHE;
175+
}
176+
177+
protected final int getRubyLibraryCacheLimit() {
178+
return getLanguage().options.RUBY_LIBRARY_CACHE;
88179
}
89180
}

0 commit comments

Comments
 (0)