Skip to content

Commit

Permalink
Add SyntaxError#path
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabigeek committed Mar 18, 2024
1 parent c062749 commit 0305938
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions spec/ruby/core/exception/fixtures/syntax_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1+1=2
26 changes: 26 additions & 0 deletions spec/ruby/core/exception/syntax_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require_relative '../../spec_helper'


ruby_version_is "3.2" do
describe "SyntaxError#path" do
it "returns the file path provided to eval" do
expected = 'speccing.rb'
-> {
eval('if true', TOPLEVEL_BINDING, expected)
}.should raise_error(SyntaxError) { |e|
e.path.should == expected
}
end

it "returns the file path that raised an exception" do
expected_path = fixture(__FILE__, 'syntax_error.rb')
-> {
require_relative 'fixtures/syntax_error'
}.should raise_error(SyntaxError) {|e| e.path.should == expected_path }
end

it "returns nil when constructed directly" do
SyntaxError.new.path.should == nil
end
end
end
4 changes: 2 additions & 2 deletions spec/ruby/core/kernel/eval_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
it "includes file and line information in syntax error" do
expected = 'speccing.rb'
-> {
eval('if true',TOPLEVEL_BINDING, expected)
eval('if true', TOPLEVEL_BINDING, expected)
}.should raise_error(SyntaxError) { |e|
e.message.should =~ /#{expected}:1:.+/
}
Expand All @@ -144,7 +144,7 @@
it "evaluates string with given filename and negative linenumber" do
expected_file = 'speccing.rb'
-> {
eval('if true',TOPLEVEL_BINDING, expected_file, -100)
eval('if true', TOPLEVEL_BINDING, expected_file, -100)
}.should raise_error(SyntaxError) { |e|
e.message.should =~ /#{expected_file}:-100:.+/
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/truffleruby/core/exception/SyntaxErrorNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
import org.truffleruby.annotations.CoreMethod;
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
import org.truffleruby.annotations.CoreModule;
import org.truffleruby.core.encoding.Encodings;
import org.truffleruby.core.klass.RubyClass;
import org.truffleruby.annotations.Visibility;
import org.truffleruby.language.objects.AllocationTracing;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.object.Shape;
import com.oracle.truffle.api.source.Source;

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

}

@CoreMethod(names = "path")
public abstract static class PathNode extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
Object path(RubySyntaxError syntaxError) {
if (!syntaxError.hasSourceLocation()) {
return nil;
}

Source source;
try {
source = syntaxError.getSourceLocation().getSource();
} catch (UnsupportedMessageException e) {
throw CompilerDirectives.shouldNotReachHere(e);
}

var path = getLanguage().getPathToTStringCache().getCachedPath(source);
return createString(path, Encodings.UTF_8);
}

}

}

0 comments on commit 0305938

Please sign in to comment.