feat(interp): add language-level closures with mutable upvalues#45
Merged
Conversation
Closures are runtime values that borrow a constant function's code and carry their own mutable upvalues. The closure value type shares *FunctionType with its function (captures never affect type equality); the capture contract lives on the function prototype (Function.Captures, parallel to Locals), mirroring clox/Lua/CPython. closure.new is operandless and takes the function ref from the stack top (like call), reading the capture count from the function at runtime and transferring ownership of the function ref and upvalues into the closure. upval.get/upval.set access upvalues by index; ref.new/ref.get/ref.set provide heap-boxed scalar cells for variables shared across closures. Frames now separate addr (template/code index for i.code/i.instrs and the profiler/JIT) from callee (the heap object released on return), since these diverge for closures. https://claude.ai/code/session_01M7fAcxPt5FjfKf9e3rRvW5
Rename Closure.Upvalues to Upvals (and frame.upvalues to upvals) to match the upval.get/upval.set opcodes, and rename the frame release-target field callee to ref, grouped next to the release flag it governs. https://claude.ai/code/session_01M7fAcxPt5FjfKf9e3rRvW5
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.
Closures are runtime values that borrow a constant function's code and
carry their own mutable upvalues. The closure value type shares
*FunctionType with its function (captures never affect type equality);
the capture contract lives on the function prototype (Function.Captures,
parallel to Locals), mirroring clox/Lua/CPython.
closure.new is operandless and takes the function ref from the stack top
(like call), reading the capture count from the function at runtime and
transferring ownership of the function ref and upvalues into the closure.
upval.get/upval.set access upvalues by index; ref.new/ref.get/ref.set
provide heap-boxed scalar cells for variables shared across closures.
Frames now separate addr (template/code index for i.code/i.instrs and the
profiler/JIT) from callee (the heap object released on return), since
these diverge for closures.
https://claude.ai/code/session_01M7fAcxPt5FjfKf9e3rRvW5