Skip to content

Commit c8160f0

Browse files
committed
Move PathToRopeCache to language, update documentation and language access
1 parent 54da828 commit c8160f0

File tree

7 files changed

+34
-31
lines changed

7 files changed

+34
-31
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import org.truffleruby.core.proc.RubyProc;
5757
import org.truffleruby.core.regexp.RegexpCacheKey;
5858
import org.truffleruby.core.rope.NativeRope;
59-
import org.truffleruby.core.rope.PathToRopeCache;
6059
import org.truffleruby.core.symbol.RubySymbol;
6160
import org.truffleruby.core.thread.ThreadManager;
6261
import org.truffleruby.core.time.GetTimeZoneNode;
@@ -121,7 +120,7 @@ public class RubyContext {
121120
private final ObjectSpaceManager objectSpaceManager = new ObjectSpaceManager();
122121
private final SharedObjects sharedObjects = new SharedObjects(this);
123122
private final AtExitManager atExitManager = new AtExitManager(this);
124-
private final CallStackManager callStack = new CallStackManager(this);
123+
private final CallStackManager callStack;
125124
private final CoreExceptions coreExceptions;
126125
private final EncodingManager encodingManager;
127126
private final MetricsProfiler metricsProfiler = new MetricsProfiler(this);
@@ -138,7 +137,6 @@ public class RubyContext {
138137
private final Hashing hashing;
139138
@CompilationFinal private BacktraceFormatter defaultBacktraceFormatter;
140139
private final BacktraceFormatter userBacktraceFormatter;
141-
private final PathToRopeCache pathToRopeCache = new PathToRopeCache(this);
142140
@CompilationFinal private TruffleNFIPlatform truffleNFIPlatform;
143141
private final CoreLibrary coreLibrary;
144142
@CompilationFinal private CoreMethods coreMethods;
@@ -163,6 +161,7 @@ public RubyContext(RubyLanguage language, TruffleLanguage.Env env) {
163161

164162
this.logger = env.getLogger("");
165163
this.language = language;
164+
this.callStack = new CallStackManager(language, this);
166165
setEnv(env);
167166
this.preInitialized = preInitializing;
168167

@@ -633,10 +632,6 @@ public CoverageManager getCoverageManager() {
633632
return coverageManager;
634633
}
635634

636-
public PathToRopeCache getPathToRopeCache() {
637-
return pathToRopeCache;
638-
}
639-
640635
public CodeLoader getCodeLoader() {
641636
return codeLoader;
642637
}

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.truffleruby.core.range.RubyObjectRange;
5959
import org.truffleruby.core.regexp.RubyMatchData;
6060
import org.truffleruby.core.rope.CodeRange;
61+
import org.truffleruby.core.rope.PathToRopeCache;
6162
import org.truffleruby.core.rope.Rope;
6263
import org.truffleruby.core.rope.RopeCache;
6364
import org.truffleruby.core.string.CoreStrings;
@@ -170,6 +171,7 @@ public final class RubyLanguage extends TruffleLanguage<RubyContext> {
170171
@CompilationFinal private AllocationReporter allocationReporter;
171172

172173
private final AtomicLong nextObjectID = new AtomicLong(ObjectSpaceManager.INITIAL_LANGUAGE_OBJECT_ID);
174+
private final PathToRopeCache pathToRopeCache = new PathToRopeCache(this);
173175

174176
private static final RubyObjectType objectType = new RubyObjectType();
175177

@@ -285,7 +287,7 @@ public RubyContext createContext(Env env) {
285287
}
286288
if (this.options == null) {
287289
this.options = new LanguageOptions(env, env.getOptions());
288-
this.coreLoadPath = buildCoreLoadPath();
290+
this.coreLoadPath = buildCoreLoadPath(this.options.CORE_LOAD_PATH);
289291
this.corePath = coreLoadPath + File.separator + "core" + File.separator;
290292
primitiveManager.loadCoreMethodNodes(this.options);
291293
}
@@ -474,6 +476,10 @@ public long getNextObjectID() {
474476
return id;
475477
}
476478

479+
public PathToRopeCache getPathToRopeCache() {
480+
return pathToRopeCache;
481+
}
482+
477483
private static Shape createShape(Class<? extends RubyDynamicObject> layoutClass) {
478484
return Shape
479485
.newBuilder()
@@ -489,7 +495,7 @@ protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues n
489495
}
490496

491497
/** {@link RubyLanguage#getSourcePath(Source)} should be used instead whenever possible (i.e., when we can access
492-
* the context).
498+
* the language).
493499
*
494500
* Returns the path of a Source. Returns the short, potentially relative, path for the main script. Note however
495501
* that the path of {@code eval(code, nil, filename)} is just {@code filename} and might not be absolute. */
@@ -507,7 +513,7 @@ public static String getPath(Source source) {
507513

508514
/** {@link RubyLanguage#getPath(Source)} but also handles core library sources. Ideally this method would be static
509515
* but for now the core load path is an option and it also depends on the current working directory. Once we have
510-
* Source metadata in Truffle we could use that to identify core library sources without needing the context. */
516+
* Source metadata in Truffle we could use that to identify core library sources without needing the language. */
511517
public String getSourcePath(Source source) {
512518
final String path = getPath(source);
513519
if (path.startsWith(coreLoadPath)) {
@@ -517,19 +523,18 @@ public String getSourcePath(Source source) {
517523
}
518524
}
519525

520-
private String buildCoreLoadPath() {
521-
String path = options.CORE_LOAD_PATH;
526+
private static String buildCoreLoadPath(String coreLoadPath) {
522527

523-
while (path.endsWith("/")) {
524-
path = path.substring(0, path.length() - 1);
528+
while (coreLoadPath.endsWith("/")) {
529+
coreLoadPath = coreLoadPath.substring(0, coreLoadPath.length() - 1);
525530
}
526531

527-
if (path.startsWith(RubyLanguage.RESOURCE_SCHEME)) {
528-
return path;
532+
if (coreLoadPath.startsWith(RubyLanguage.RESOURCE_SCHEME)) {
533+
return coreLoadPath;
529534
}
530535

531536
try {
532-
return new File(path).getCanonicalPath();
537+
return new File(coreLoadPath).getCanonicalPath();
533538
} catch (IOException e) {
534539
throw CompilerDirectives.shouldNotReachHere(e);
535540
}

src/main/java/org/truffleruby/core/rope/PathToRopeCache.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.concurrent.locks.ReentrantReadWriteLock;
1616

1717
import org.jcodings.specific.UTF8Encoding;
18-
import org.truffleruby.RubyContext;
1918
import org.truffleruby.RubyLanguage;
2019
import org.truffleruby.core.string.StringOperations;
2120

@@ -26,17 +25,17 @@
2625
* Source is reachable. */
2726
public class PathToRopeCache {
2827

29-
private final RubyContext context;
28+
private final RubyLanguage language;
3029
private final ReadWriteLock lock = new ReentrantReadWriteLock();
3130
private final WeakHashMap<String, Rope> javaStringToRope = new WeakHashMap<>();
3231

33-
public PathToRopeCache(RubyContext context) {
34-
this.context = context;
32+
public PathToRopeCache(RubyLanguage language) {
33+
this.language = language;
3534
}
3635

3736
@TruffleBoundary
3837
public Rope getCachedPath(Source source) {
39-
final String path = context.getLanguageSlow().getSourcePath(source);
38+
final String path = language.getSourcePath(source);
4039

4140
final Lock readLock = lock.readLock();
4241
readLock.lock();
@@ -49,7 +48,7 @@ public Rope getCachedPath(Source source) {
4948
readLock.unlock();
5049
}
5150

52-
final Rope cachedRope = context.getLanguageSlow().ropeCache.getRope(
51+
final Rope cachedRope = language.ropeCache.getRope(
5352
StringOperations.encodeRope(path, UTF8Encoding.INSTANCE));
5453

5554
final Lock writeLock = lock.writeLock();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected Object absolutePath(RubyBacktraceLocation threadBacktraceLocation,
6666
return makeStringNode.fromRope(cachedRope);
6767
} else { // eval()
6868
return makeStringNode
69-
.fromRope(getContext().getPathToRopeCache().getCachedPath(source));
69+
.fromRope(getLanguage().getPathToRopeCache().getCachedPath(source));
7070
}
7171
}
7272
}
@@ -85,7 +85,7 @@ protected RubyString path(RubyBacktraceLocation threadBacktraceLocation,
8585
if (sourceSection == null) {
8686
return coreStrings().UNKNOWN.createInstance(getContext());
8787
} else {
88-
final Rope path = getContext().getPathToRopeCache().getCachedPath(sourceSection.getSource());
88+
final Rope path = getLanguage().getPathToRopeCache().getCachedPath(sourceSection.getSource());
8989
return makeStringNode.fromRope(path);
9090
}
9191
}

src/main/java/org/truffleruby/core/tracepoint/TraceBaseEventNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ protected RubyString getFile() {
4343
if (file == null) {
4444
CompilerDirectives.transferToInterpreterAndInvalidate();
4545
final Source source = eventContext.getInstrumentedSourceSection().getSource();
46-
file = StringOperations.createString(context, language, context.getPathToRopeCache().getCachedPath(source));
46+
file = StringOperations
47+
.createString(context, language, language.getPathToRopeCache().getCachedPath(source));
4748
}
4849
return file;
4950
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.function.Predicate;
1414

1515
import org.truffleruby.RubyContext;
16+
import org.truffleruby.RubyLanguage;
1617
import org.truffleruby.SuppressFBWarnings;
1718
import org.truffleruby.collections.Memo;
1819
import org.truffleruby.core.array.ArrayUtils;
@@ -37,9 +38,11 @@
3738

3839
public class CallStackManager {
3940

41+
private final RubyLanguage language;
4042
private final RubyContext context;
4143

42-
public CallStackManager(RubyContext context) {
44+
public CallStackManager(RubyLanguage language, RubyContext context) {
45+
this.language = language;
4346
this.context = context;
4447
}
4548

@@ -102,7 +105,7 @@ public InternalMethod getCallingMethod() {
102105

103106
@TruffleBoundary
104107
public SourceSection getTopMostUserSourceSection(SourceSection encapsulatingSourceSection) {
105-
if (BacktraceFormatter.isUserSourceSection(context.getLanguageSlow(), encapsulatingSourceSection)) {
108+
if (BacktraceFormatter.isUserSourceSection(language, encapsulatingSourceSection)) {
106109
return encapsulatingSourceSection;
107110
} else {
108111
return getTopMostUserSourceSection();

src/main/java/org/truffleruby/language/backtrace/BacktraceFormatter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public static String printableRubyBacktrace(Node node) {
8585
}
8686

8787
/** For debug purposes. */
88-
public static boolean isApplicationCode(RubyContext context, SourceSection sourceSection) {
89-
return isUserSourceSection(context.getLanguageSlow(), sourceSection) &&
90-
!context.getLanguageSlow().getSourcePath(sourceSection.getSource()).contains("/lib/stdlib/rubygems");
88+
public static boolean isApplicationCode(RubyLanguage language, SourceSection sourceSection) {
89+
return isUserSourceSection(language, sourceSection) &&
90+
!language.getSourcePath(sourceSection.getSource()).contains("/lib/stdlib/rubygems");
9191
}
9292

9393
public BacktraceFormatter(RubyContext context, RubyLanguage language, EnumSet<FormattingFlags> flags) {

0 commit comments

Comments
 (0)