Skip to content
Merged
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
10 changes: 10 additions & 0 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,16 @@ func (c *Compiler) compileCall(v *ast.Call) {
b.emit(bytecode.OpBinding, 0, 0)
return
}
// A bare eval(str) with no explicit binding evaluates against the caller's
// binding, so it can see (and assign) the caller's local variables — exactly
// like eval(str, binding). Rewrite it to that two-argument form using the
// binding intrinsic; the added `binding` argument compiles to OpBinding above.
if v.Recv == nil && v.Block == nil && v.Name == "eval" && len(v.Args) == 1 {
v2 := *v
v2.Args = []ast.Node{v.Args[0], &ast.Call{Name: "binding"}}
c.compileCall(&v2)
return
}
// A bare, zero-arg name that resolves to a local is a variable read, not a
// send. The parser emits a Call (not a VarRef) for an identifier whose
// assignment it never saw — which is exactly how an eval'd string references
Expand Down
5 changes: 5 additions & 0 deletions internal/vm/binding_eval_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (vm *VM) bindingEval(b *Binding, srcV object.Value) object.Value {
iseq, _ = compiler.CompileWithLocals(prog, b.names)
}
iseq.Name = "(eval)"
// eval is transparent to Kernel#__method__ / #__callee__: the evaluated code
// inherits the caller's method context (so `eval("__method__")`, which routes
// through the caller's binding, reports the enclosing method), matching the
// no-binding eval path.
vm.pendingMethodCtx = vm.currentMethodCtxPtr()
return vm.exec(iseq, b.self, nil, b.definee, "", b.env, nil, nil, nil)
}

Expand Down
42 changes: 42 additions & 0 deletions internal/vm/eval_caller_binding_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestEvalCallerBinding covers that a bare eval(str) — with no explicit binding
// — evaluates against the caller's binding: it reads and assigns the caller's
// local variables, works inside a method with method-local variables, and stays
// transparent to __method__ / __callee__. An explicit binding argument still
// works. Verified against ruby 4.0.6.
func TestEvalCallerBinding(t *testing.T) {
cases := []struct{ src, want string }{
// Reads a caller local.
{`x = 42; p eval("x")`, `42`},
{`a = 10; b = 20; p eval("a + b")`, `30`},
// Assigns back into the caller's scope.
{`x = 5; eval("x = 99"); p x`, `99`},
// Works with a method-local variable.
{`def m; y = 7; eval("y * 2"); end; p m`, `14`},
// Transparent to __method__ / __callee__.
{`def foo; eval("__method__"); end; p foo`, `:foo`},
{`def bar; eval("__callee__"); end; p bar`, `:bar`},
{`p eval("__method__")`, `nil`},
// A plain expression still evaluates.
{`p eval("1 + 1")`, `2`},
// An explicit binding argument is still honoured.
{`x = 3; p eval("x * x", binding)`, `9`},
// An explicit-receiver eval (Kernel.eval) runs against a fresh scope with no
// caller binding, exercising the plain compile-and-run path.
{`p Kernel.eval("3 + 4")`, `7`},
{`begin; Kernel.eval(5); rescue TypeError => e; p e.class; end`, `TypeError`},
{`begin; Kernel.eval("def ("); rescue SyntaxError => e; p e.class; end`, `SyntaxError`},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want+"\n" {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n")
}
}
}