Repository navigation
Kernel#eval: a bare eval(str) sees the caller's local variables - #441
Merged
Merged
Conversation
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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
eval(str)with no explicit binding ran against a fresh local scope, soit could not read or assign the caller's local variables — a reference
to a caller local raised
NameError, andeval("x = 1")did not writeback to the caller's
x. MRI evaluates a bareevalagainst thecaller's binding.
The compiler now rewrites a syntactic
eval(str)(no receiver, noblock, exactly one argument) to
eval(str, binding), reusing thebinding 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.bindingEvalis made transparent to__method__/__callee__(it now inherits the caller's method context,as the no-binding path did), so
eval("__method__")inside a methodstill reports that method. An explicit-receiver form (
Kernel.eval(str))keeps the plain fresh-scope behaviour.
Verified against ruby 4.0.6; a full language+core sweep shows +39
(
core/kernel/eval_spec20 → 36,language/def_spec33 → 47,core/module/{private,protected,public}_spec+6, and more) with noregressions, and the whole-suite coverage gate stays at 100.0%.
🤖 Generated with Claude Code