-
Notifications
You must be signed in to change notification settings - Fork 195
Add SyntaxError#path #3433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SyntaxError#path #3433
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1+1=2 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -35,4 +40,27 @@ RubySyntaxError allocateSyntaxError(RubyClass rubyClass) { | |
|
||
} | ||
|
||
@CoreMethod(names = "path") | ||
public abstract static class PathNode extends CoreMethodArrayArgumentsNode { | ||
|
||
@TruffleBoundary | ||
@Specialization | ||
Object path(RubySyntaxError syntaxError) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to handle RubyString / Nil - a brief search of the codebase suggested that this was how we handled varying return types, did I miss something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct. Looks great! |
||
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); | ||
eregon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.