From 33f5869497eb4fdd0ed3c06dcb69e3b0b07bfc8a Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 24 Aug 2026 16:25:34 +0200 Subject: [PATCH] Kernel#eval: a bare eval(str) sees the caller's local variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit eval(str) with no explicit binding ran against a fresh local scope, so it could not read or assign the caller's local variables — a reference to a caller local raised NameError, and eval("x = 1") did not write back to the caller's x. MRI evaluates a bare eval against the caller's binding. The compiler now rewrites a syntactic eval(str) (no receiver, no block, exactly one argument) to eval(str, binding), reusing the binding intrinsic that already captures the current frame, so the evaluated code resolves and mutates the caller's locals through the existing eval(str, binding) path. bindingEval is made transparent to __method__ / __callee__ (it now inherits the caller's method context, as the no-binding path did), so eval("__method__") inside a method still reports that method. Verified against ruby 4.0.6; core/kernel/eval_spec 20 -> 36. Whole-suite coverage gate stays at 100.0% and a full language+core sweep shows no regressions. Co-Authored-By: Claude Opus 4.8 --- internal/compiler/compiler.go | 10 +++++ internal/vm/binding_eval_open.go | 5 +++ .../vm/eval_caller_binding_internal_test.go | 42 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 internal/vm/eval_caller_binding_internal_test.go 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") + } + } +}