diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index 7f8058bd..611e0f02 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -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 diff --git a/internal/vm/binding_eval_open.go b/internal/vm/binding_eval_open.go index 7363cd71..8cd4dec7 100644 --- a/internal/vm/binding_eval_open.go +++ b/internal/vm/binding_eval_open.go @@ -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) } diff --git a/internal/vm/eval_caller_binding_internal_test.go b/internal/vm/eval_caller_binding_internal_test.go new file mode 100644 index 00000000..4ac53489 --- /dev/null +++ b/internal/vm/eval_caller_binding_internal_test.go @@ -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") + } + } +}