Skip to content

Commit a467c23

Browse files
committed
[GR-45621] Add syntaxerror path
PullRequest: truffleruby/4213
2 parents f98cab6 + fe7d794 commit a467c23

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Compatibility:
8080
* Remove deprecated `Fixnum` and `Bignum` constants (#3039, @andrykonchin).
8181
* Add `rb_enc_interned_str_cstr` function (#3408, @goyox86, @thomasmarshall).
8282
* Add `rb_str_to_interned_str` function (#3408, @thomasmarshall).
83+
* Add `SyntaxError#path` method (#3039, @wasabigeek).
8384

8485
Performance:
8586

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1+1=2
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require_relative '../../spec_helper'
2+
3+
ruby_version_is "3.2" do
4+
describe "SyntaxError#path" do
5+
it "returns the file path provided to eval" do
6+
filename = "speccing.rb"
7+
8+
-> {
9+
eval("if true", TOPLEVEL_BINDING, filename)
10+
}.should raise_error(SyntaxError) { |e|
11+
e.path.should == filename
12+
}
13+
end
14+
15+
it "returns the file path that raised an exception" do
16+
expected_path = fixture(__FILE__, "syntax_error.rb")
17+
18+
-> {
19+
require_relative "fixtures/syntax_error"
20+
}.should raise_error(SyntaxError) { |e| e.path.should == expected_path }
21+
end
22+
23+
it "returns nil when constructed directly" do
24+
SyntaxError.new.path.should == nil
25+
end
26+
end
27+
end

spec/ruby/core/kernel/eval_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
it "includes file and line information in syntax error" do
136136
expected = 'speccing.rb'
137137
-> {
138-
eval('if true',TOPLEVEL_BINDING, expected)
138+
eval('if true', TOPLEVEL_BINDING, expected)
139139
}.should raise_error(SyntaxError) { |e|
140140
e.message.should =~ /#{expected}:1:.+/
141141
}
@@ -144,7 +144,7 @@
144144
it "evaluates string with given filename and negative linenumber" do
145145
expected_file = 'speccing.rb'
146146
-> {
147-
eval('if true',TOPLEVEL_BINDING, expected_file, -100)
147+
eval('if true', TOPLEVEL_BINDING, expected_file, -100)
148148
}.should raise_error(SyntaxError) { |e|
149149
e.message.should =~ /#{expected_file}:-100:.+/
150150
}

src/main/java/org/truffleruby/core/exception/SyntaxErrorNodes.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
import org.truffleruby.annotations.CoreMethod;
1313
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1414
import org.truffleruby.annotations.CoreModule;
15+
import org.truffleruby.core.encoding.Encodings;
1516
import org.truffleruby.core.klass.RubyClass;
1617
import org.truffleruby.annotations.Visibility;
1718
import org.truffleruby.language.objects.AllocationTracing;
1819

20+
import com.oracle.truffle.api.CompilerDirectives;
21+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1922
import com.oracle.truffle.api.dsl.Specialization;
23+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
2024
import com.oracle.truffle.api.object.Shape;
25+
import com.oracle.truffle.api.source.Source;
2126

2227
@CoreModule(value = "SyntaxError", isClass = true)
2328
public abstract class SyntaxErrorNodes {
@@ -35,4 +40,27 @@ RubySyntaxError allocateSyntaxError(RubyClass rubyClass) {
3540

3641
}
3742

43+
@CoreMethod(names = "path")
44+
public abstract static class PathNode extends CoreMethodArrayArgumentsNode {
45+
46+
@TruffleBoundary
47+
@Specialization
48+
Object path(RubySyntaxError syntaxError) {
49+
if (!syntaxError.hasSourceLocation()) {
50+
return nil;
51+
}
52+
53+
Source source;
54+
try {
55+
source = syntaxError.getSourceLocation().getSource();
56+
} catch (UnsupportedMessageException e) {
57+
throw CompilerDirectives.shouldNotReachHere(e);
58+
}
59+
60+
var path = getLanguage().getPathToTStringCache().getCachedPath(source);
61+
return createString(path, Encodings.UTF_8);
62+
}
63+
64+
}
65+
3866
}

0 commit comments

Comments
 (0)