Repository navigation
vm, compiler: make global var bindings properties of the global object - #720
Merged
Merged
Conversation
Global var and function bindings lived only in heap slots. The global object gained copies of them only after an indirect eval, so getOwnPropertyDescriptor(globalThis, name) saw no script vars. On top of that, hasOwnProperty answered true for let/const/class, and `var x;` in eval reset an existing x to undefined. Var and function slots are now mirrored onto the global object as data properties, kept current through the heap's slot watch: - script declarations are non-configurable - eval declarations and implicit globals are configurable Deleting a binding drops its mirror. Top-level classes initialize their slot like let, so they are no longer mistaken for implicit globals. Eval code declares globals through a new OpDeclareGlobal, which implements CreateGlobalVarBinding and CreateGlobalFunctionBinding. It first checks CanDeclareGlobalFunction and CanDeclareGlobalVar for every name, so a conflict throws before any binding is created. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nooga
force-pushed
the
fix/72-global-var-bindings
branch
from
October 17, 2026 21:26
8970902 to
2d62014
Compare
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.
Part of #72 (group A: global var bindings and the global object).
Problems
Object.getOwnPropertyDescriptor(globalThis, "x")returnedundefinedfor a plain scriptvar x.hasOwnPropertyansweredtruefor top-levellet/const/class.var x;inside eval (direct or indirect) reset an existing globalxtoundefined.Fix
pkg/vm/global_bindings.go).zz = 1) are configurable.OpSetGlobalInit, likelet, so they are no longer mistaken for implicit globals.OpDeclareGlobalimplements CreateGlobalVarBinding and CreateGlobalFunctionBinding for eval code. In check mode it is emitted for every name before any binding is created, so a non-definable name throws a TypeError and declares nothing.SyncHeapToGlobalObjectafter indirect eval is removed. It also copied lexical bindings onto the global object.Module top-level bindings are not mirrored.
Testing
tests/scripts/global_var_bindings_on_global_object.ts.TestScriptsis green.language+built-ins, vs main): +18, no regressions. This covers:eval-code/{direct,indirect}/var-env-*-init-global-*non-definable-*global-code/decl-{func,lex,var}🤖 Generated with Claude Code