Skip to content
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

Support file and line arguments in eval #2138

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/natalie/compiler/macro_expander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def macro_load(expr:, current_path:, **)
def macro_eval(expr:, current_path:, locals:, **)
args = expr.arguments&.arguments || []
node = args.first
$stderr.puts 'FIXME: binding passed to eval() will be ignored.' if args.size > 1
$stderr.puts 'FIXME: binding passed to eval() will be ignored.' if args.size > 1 && args[1].type != :nil_node
if node.type == :interpolated_string_node && node.parts.all? { |subnode| subnode.type == :string_node }
node = Prism::StringNode.new(
nil,
Expand All @@ -170,9 +170,24 @@ def macro_eval(expr:, current_path:, locals:, **)
node.location,
)
end
line = nil
if node.type == :string_node
if args.size > 2
if args[2].is_a?(Prism::StringNode)
current_path = args[2].unescaped
else
$stderr.puts 'FIXME: passed file to eval() will be ignored'
end
end
if args.size > 3
if args[3].is_a?(Prism::IntegerNode)
line = args[3].value
else
$stderr.puts 'FIXME: passed line to eval() will be ignored'
end
end
begin
Natalie::Parser.new(node.unescaped, current_path, locals: locals).ast
Natalie::Parser.new(node.unescaped, current_path, line: line, locals: locals).ast
rescue Parser::ParseError => e
drop_error(:SyntaxError, e.message, location: node.location)
end
Expand Down
5 changes: 3 additions & 2 deletions lib/natalie/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ class ParseError < StandardError
class IncompleteExpression < ParseError
end

def initialize(code_str, path, locals: [])
def initialize(code_str, path, line: nil, locals: [])
@code_str = code_str
@path = path
@line = line
@locals = locals
end

Expand All @@ -74,7 +75,7 @@ def tokenize
end

def result
@result ||= Prism.parse(@code_str, filepath: @path, scopes: [@locals])
@result ||= Prism.parse(@code_str, filepath: @path, line: @line, scopes: [@locals])
end

def source = result.source
Expand Down
7 changes: 4 additions & 3 deletions spec/core/proc/binding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def obj.test_binding(some, params)

lambdas_binding = obj.test_binding(1, 2).binding

# NATFIXME: binding passed to eval() will be ignored.
# eval("some", lambdas_binding).should == 1
# eval("params", lambdas_binding).should == 2
NATFIXME 'binding passed to eval() will be ignored.', exception: NoMethodError, message: "undefined method `some' for main" do
eval("some", lambdas_binding).should == 1
eval("params", lambdas_binding).should == 2
end
end
end
5 changes: 5 additions & 0 deletions test/natalie/eval_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ class C
a
RUBY
end

it 'can handle file and line argument' do
eval('"#{__FILE__}:#{__LINE__}"', nil, 'foo.rb').should == 'foo.rb:1'
eval('"#{__FILE__}:#{__LINE__}"', nil, 'foo.rb', 1234).should == 'foo.rb:1234'
end
end
Loading