M2-T03: closures & lexical environments (closes #29) - #108
Merged
Conversation
- Env frames are Rc<RefCell<BTreeMap>>: a closure captures a LIVE view of
its lexical env (clone of the shared frame vector) - later writes to
captured bindings are visible (not a snapshot)
- Value::Function { params, body, captured }: calls bind params in a child
of the CAPTURED env; a closure called from another scope does not see
the caller's shadowing (lexical, not dynamic, scope)
- Parser gap: let/const inside block-bodied arrows now lower to scoped
assignments (previously parsed as expression statements -> unbound
'let' at eval); both parsers updated
- PartialEq manual: Function identity by captured-env pointer, Object by
Rc::ptr_eq - JS identity semantics
5 tests in tests/closures.rs; suite 181 green; clippy/fmt/audit clean.
Records: M2 in progress 3/15, 57/106.
Closes#29Uh oh!
There was an error while loading. Please reload this page.
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 freeto 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.
What
Closes#29. Correct JS closure semantics — not a value snapshot:
Envframes areRc<RefCell<...>>: a closure captures its lexical environment by reference (a clone of the shared frame vector). Later writes to captured bindings are visible (n = n + 1across calls works); the counter pattern is test-pinned.Value::Function { params, body, captured }: calls bind params in a child scope of the captured env — lexical scope, not dynamic (a caller's shadowing doesn't leak in).let/constinside block-bodied arrows was parsed as expression statements — an unboundletat eval time. Real JS closures routinely declare locals; both parsers now lower them to scoped assignments.PartialEqis manual — Function equal by captured-env pointer, Object byRc::ptr_eq(two closures/objects aren't equal unless the same value).Verification
5 tests in
tests/closures.rs: lexical vs caller shadowing, live writes across calls, nested closures chaining to outer params, cross-scope use, identity distinctness. Suite 181 green; clippy-D warningsclean; fmt clean; audit green. Records: M2 3/15 in progress, overall 57/106.