Skip to content

Commit 0305938

Browse files
committed
Add SyntaxError#path
1 parent c062749 commit 0305938

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1+1=2
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require_relative '../../spec_helper'
2+
3+
4+
ruby_version_is "3.2" do
5+
describe "SyntaxError#path" do
6+
it "returns the file path provided to eval" do
7+
expected = 'speccing.rb'
8+
-> {
9+
eval('if true', TOPLEVEL_BINDING, expected)
10+
}.should raise_error(SyntaxError) { |e|
11+
e.path.should == expected
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+
require_relative 'fixtures/syntax_error'
19+
}.should raise_error(SyntaxError) {|e| e.path.should == expected_path }
20+
end
21+
22+
it "returns nil when constructed directly" do
23+
SyntaxError.new.path.should == nil
24+
end
25+
end
26+
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)