Skip to content

Commit 8484c42

Browse files
committed
Introduce RubyContext#getSourcePath() and use it where possible
1 parent 7d8f274 commit 8484c42

20 files changed

+44
-60
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ private void dispose() {
495495
}
496496

497497
if (options.COVERAGE_GLOBAL) {
498-
coverageManager.print(System.out);
498+
coverageManager.print(this, System.out);
499499
}
500500
}
501501

@@ -792,7 +792,9 @@ public WeakValueCache<RegexpCacheKey, Regex> getRegexpCache() {
792792
return regexpCache;
793793
}
794794

795-
/** Returns the path of a Source. Returns the short, potentially relative, path for the main script. Note however
795+
/** {@link #getSourcePath(Source)} should be used instead whenever possible (i.e., when we can access the context).
796+
*
797+
* Returns the path of a Source. Returns the short, potentially relative, path for the main script. Note however
796798
* that the path of {@code eval(code, nil, filename)} is just {@code filename} and might not be absolute. */
797799
public static String getPath(Source source) {
798800
final String path = source.getPath();
@@ -806,6 +808,18 @@ public static String getPath(Source source) {
806808
}
807809
}
808810

811+
/** {@link RubyContext#getPath(Source)} but also handles core library sources. Ideally this method would be static
812+
* but for now the core load path is an option and it also depends on the current working directory. Once we have
813+
* Source metadata in Truffle we could use that to identify core library sources without needing the context. */
814+
public String getSourcePath(Source source) {
815+
final String path = RubyContext.getPath(source);
816+
if (path.startsWith(coreLibrary.coreLoadPath)) {
817+
return "<internal:core> " + path.substring(coreLibrary.coreLoadPath.length() + 1);
818+
} else {
819+
return RubyContext.getPath(source);
820+
}
821+
}
822+
809823
public String getPathRelativeToHome(String path) {
810824
if (path.startsWith(rubyHome) && path.length() > rubyHome.length()) {
811825
return path.substring(rubyHome.length() + 1);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.jcodings.specific.USASCIIEncoding;
1919
import org.jcodings.specific.UTF8Encoding;
2020
import org.truffleruby.Layouts;
21-
import org.truffleruby.RubyContext;
2221
import org.truffleruby.RubyLanguage;
2322
import org.truffleruby.builtins.CoreMethod;
2423
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
@@ -785,7 +784,7 @@ public abstract static class SourceFileNode extends CoreMethodArrayArgumentsNode
785784
@Specialization
786785
protected RubyString sourceFile() {
787786
final SourceSection sourceSection = getTopUserSourceSection("rb_sourcefile");
788-
final String file = RubyContext.getPath(sourceSection.getSource());
787+
final String file = getContext().getSourcePath(sourceSection.getSource());
789788

790789
return makeStringNode.executeMake(file, UTF8Encoding.INSTANCE, CodeRange.CR_UNKNOWN);
791790
}

src/main/java/org/truffleruby/core/binding/BindingNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ protected Object sourceLocation(RubyBinding binding,
384384
return nil;
385385
} else {
386386
final RubyString file = makeStringNode.executeMake(
387-
RubyContext.getPath(sourceSection.getSource()),
387+
getContext().getSourcePath(sourceSection.getSource()),
388388
UTF8Encoding.INSTANCE,
389389
CodeRange.CR_UNKNOWN);
390390
return createArray(new Object[]{ file, sourceSection.getStartLine() });

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ protected RubyString getCallerPath(RubyString feature,
301301
coreExceptions().loadError("cannot infer basepath", featureString, this));
302302
}
303303

304-
String sourcePath = RubyContext.getPath(sourceSection.getSource());
304+
String sourcePath = getContext().getSourcePath(sourceSection.getSource());
305305

306306
sourcePath = getContext().getFeatureLoader().canonicalize(sourcePath);
307307

src/main/java/org/truffleruby/core/method/MethodNodes.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.truffleruby.core.method;
1111

1212
import org.jcodings.specific.UTF8Encoding;
13-
import org.truffleruby.RubyContext;
1413
import org.truffleruby.RubyLanguage;
1514
import org.truffleruby.builtins.CoreMethod;
1615
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
@@ -189,7 +188,7 @@ protected Object sourceLocation(RubyMethod method) {
189188
return nil;
190189
} else {
191190
RubyString file = makeStringNode.executeMake(
192-
RubyContext.getPath(sourceSection.getSource()),
191+
getContext().getSourcePath(sourceSection.getSource()),
193192
UTF8Encoding.INSTANCE,
194193
CodeRange.CR_UNKNOWN);
195194
return createArray(new Object[]{ file, sourceSection.getStartLine() });

src/main/java/org/truffleruby/core/method/UnboundMethodNodes.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.truffleruby.core.method;
1111

1212
import org.jcodings.specific.UTF8Encoding;
13-
import org.truffleruby.RubyContext;
1413
import org.truffleruby.RubyLanguage;
1514
import org.truffleruby.builtins.CoreMethod;
1615
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
@@ -188,7 +187,7 @@ protected Object sourceLocation(RubyUnboundMethod unboundMethod) {
188187
return nil;
189188
} else {
190189
RubyString file = makeStringNode.executeMake(
191-
RubyContext.getPath(sourceSection.getSource()),
190+
getContext().getSourcePath(sourceSection.getSource()),
192191
UTF8Encoding.INSTANCE,
193192
CodeRange.CR_UNKNOWN);
194193
Object[] objects = new Object[]{ file, sourceSection.getStartLine() };

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.truffleruby.core.proc;
1111

1212
import org.jcodings.specific.UTF8Encoding;
13-
import org.truffleruby.RubyContext;
1413
import org.truffleruby.RubyLanguage;
1514
import org.truffleruby.builtins.CoreMethod;
1615
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
@@ -239,12 +238,12 @@ public abstract static class SourceLocationNode extends CoreMethodArrayArguments
239238
protected Object sourceLocation(RubyProc proc) {
240239
SourceSection sourceSection = proc.sharedMethodInfo.getSourceSection();
241240

242-
if (!sourceSection.isAvailable() ||
243-
RubyContext.getPath(sourceSection.getSource()).endsWith("/lib/truffle/truffle/cext.rb")) {
241+
final String sourcePath = getContext().getSourcePath(sourceSection.getSource());
242+
if (!sourceSection.isAvailable() || sourcePath.endsWith("/lib/truffle/truffle/cext.rb")) {
244243
return nil;
245244
} else {
246245
final RubyString file = makeStringNode.executeMake(
247-
RubyContext.getPath(sourceSection.getSource()),
246+
sourcePath,
248247
UTF8Encoding.INSTANCE,
249248
CodeRange.CR_UNKNOWN);
250249

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public PathToRopeCache(RubyContext context) {
3535

3636
@TruffleBoundary
3737
public Rope getCachedPath(Source source) {
38-
final String path = RubyContext.getPath(source);
38+
final String path = context.getSourcePath(source);
3939

4040
final Lock readLock = lock.readLock();
4141
readLock.lock();

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.truffleruby.core.string.StringNodes;
2121
import org.truffleruby.core.string.StringOperations;
2222
import org.truffleruby.language.backtrace.Backtrace;
23-
import org.truffleruby.language.backtrace.BacktraceFormatter;
2423

2524
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
2625
import com.oracle.truffle.api.TruffleStackTraceElement;
@@ -56,8 +55,8 @@ protected RubyString absolutePath(RubyBacktraceLocation threadBacktraceLocation,
5655
return coreStrings().UNKNOWN.createInstance(getContext());
5756
} else {
5857
final Source source = sourceSection.getSource();
59-
final String path = RubyContext.getPath(source);
6058
if (source.getPath() != null) { // A normal file
59+
final String path = getContext().getSourcePath(source);
6160
final String canonicalPath = getContext().getFeatureLoader().canonicalize(path);
6261
final Rope cachedRope = getContext()
6362
.getRopeCache()
@@ -84,15 +83,7 @@ protected RubyString path(RubyBacktraceLocation threadBacktraceLocation,
8483
if (sourceSection == null) {
8584
return coreStrings().UNKNOWN.createInstance(getContext());
8685
} else {
87-
final Rope path;
88-
if (BacktraceFormatter.isCore(getContext(), sourceSection)) {
89-
path = StringOperations.encodeRope(
90-
BacktraceFormatter.formatCorePath(getContext(), sourceSection),
91-
UTF8Encoding.INSTANCE);
92-
} else {
93-
path = getContext().getPathToRopeCache().getCachedPath(sourceSection.getSource());
94-
}
95-
86+
final Rope path = getContext().getPathToRopeCache().getCachedPath(sourceSection.getSource());
9687
return makeStringNode.fromRope(path);
9788
}
9889
}

src/main/java/org/truffleruby/debug/TruffleDebugNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected RubyHandle setBreak(RubyString file, int line, RubyProc block) {
108108
final SourceSectionFilter filter = SourceSectionFilter
109109
.newBuilder()
110110
.mimeTypeIs(TruffleRuby.MIME_TYPE)
111-
.sourceIs(source -> source != null && RubyContext.getPath(source).equals(fileString))
111+
.sourceIs(source -> source != null && getContext().getSourcePath(source).equals(fileString))
112112
.lineIs(line)
113113
.tagIs(StandardTags.StatementTag.class)
114114
.build();

0 commit comments

Comments
 (0)