Skip to content

Commit 107a66f

Browse files
committed
[GR-17457] Fix Method#source_location for core methods
PullRequest: truffleruby/2034
2 parents e751e66 + d342d71 commit 107a66f

23 files changed

+81
-93
lines changed

spec/ruby/core/method/source_location_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@ def f
8686
method.source_location[1].should == line
8787
end
8888

89+
it "works for core methods where it returns nil or <internal:" do
90+
loc = method(:__id__).source_location
91+
if loc == nil
92+
loc.should == nil
93+
else
94+
loc[0].should.start_with?('<internal:')
95+
loc[1].should be_kind_of(Integer)
96+
end
97+
98+
loc = method(:tap).source_location
99+
if loc == nil
100+
loc.should == nil
101+
else
102+
loc[0].should.start_with?('<internal:')
103+
loc[1].should be_kind_of(Integer)
104+
end
105+
end
106+
89107
describe "for a Method generated by respond_to_missing?" do
90108
it "returns nil" do
91109
m = MethodSpecs::Methods.new

spec/ruby/core/thread/backtrace/location/absolute_path_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@
3636
end
3737
end
3838

39+
context "when used in a core method" do
40+
it "returns nil" do
41+
location = nil
42+
tap { location = caller_locations(1, 1)[0] }
43+
location.label.should == "tap"
44+
if location.path.start_with?("<internal:")
45+
location.absolute_path.should == nil
46+
else
47+
location.absolute_path.should == File.realpath(__FILE__)
48+
end
49+
end
50+
end
51+
3952
context "canonicalization" do
4053
platform_is_not :windows do
4154
before :each do

spec/truffle/thread/backtrace/location/absolute_path_spec.rb

Lines changed: 0 additions & 30 deletions
This file was deleted.

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

0 commit comments

Comments
 (0)