Skip to content

feat(interp): add language-level closures with mutable upvalues#45

Merged
siyul-park merged 2 commits into
mainfrom
claude/closure-type-design-ofK1o
May 28, 2026
Merged

feat(interp): add language-level closures with mutable upvalues#45
siyul-park merged 2 commits into
mainfrom
claude/closure-type-design-ofK1o

Conversation

@siyul-park

Copy link
Copy Markdown
Owner

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

claude added 2 commits May 28, 2026 10:48
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
@siyul-park siyul-park merged commit 5568d64 into main May 28, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants