4.4 KiB
PR-6.3 — CALL_CLOSURE (Model B Hidden Arg0)
Briefing
Closures must be dynamically invokable.
Under Model B, invocation semantics are:
- The closure object itself becomes hidden
arg0. - User-supplied arguments become
arg1..argN. - Captures remain inside the closure and are accessed explicitly.
Target
Introduce opcode:
CALL_CLOSURE arg_count
Stack before call:
[..., argN, ..., arg1, closure_ref]
Execution steps:
- Pop
closure_ref. - Validate object is
ObjectKind::Closure. - Pop
arg_countarguments. - Read
fn_idfrom closure object. - Create new call frame:
- Inject
closure_refasarg0. - Append user arguments as
arg1..argN.
- Jump to function entry.
No environment copying into locals.
Work Items
- Add
CALL_CLOSUREopcode. - Implement dispatch logic.
- Integrate with call frame creation.
- Ensure stack discipline is preserved.
Acceptance Checklist
- CALL_CLOSURE implemented.
- closure_ref validated.
- arg_count respected.
- Hidden arg0 injected correctly.
- Errors thrown on non-closure call.
Tests
- Closure returning constant.
- Closure capturing value and using it.
- Calling non-closure results in trap.
- Nested closure calls work.
Junie Instructions
You MAY:
- Modify interpreter call logic.
- Add tests.
You MUST NOT:
- Change stack model.
- Introduce coroutine semantics.
- Modify GC.
If function signature metadata is insufficient to validate arg_count, STOP and ask.
Definition of Done
Closures can be dynamically invoked with hidden arg0 semantics.
PR-6.4 — GC Traversal for Closures (Model B)
Briefing
Closures introduce heap-to-heap references through their captured environments.
Under Model B, the closure object itself is passed at call time, but its environment remains stored in heap.
GC must traverse:
closure -> env -> inner HeapRefs
Target
Extend GC mark phase to handle ObjectKind::Closure:
When marking a closure:
- Iterate over env values.
- If a value contains HeapRef → mark referenced object.
No compaction. No relocation.
Work Items
- Extend mark traversal switch.
- Ensure safe iteration over env payload.
- Add regression tests.
Acceptance Checklist
- Closure env scanned.
- Nested closures retained.
- No regression in existing GC tests.
Tests
- Closure capturing another closure.
- Closure capturing heap object.
- Unreferenced closure collected.
Junie Instructions
You MAY:
- Modify mark traversal.
- Add tests.
You MUST NOT:
- Modify sweep policy.
- Introduce compaction.
If unsure whether Value variants can embed HeapRef, STOP and ask.
Definition of Done
GC correctly traverses closure environments under Model B semantics.
PR-6.5 — Verifier Support for Closures (Model B)
Briefing
The verifier must understand closure values and enforce safe invocation rules.
Under Model B:
CALL_CLOSUREinjects hiddenarg0.- User-visible arg_count excludes hidden arg.
- Captures are accessed via explicit instructions (future PR).
Target
Extend verifier to:
- Introduce stack type:
ClosureValue. - Validate MAKE_CLOSURE effects.
- Validate CALL_CLOSURE semantics:
- Ensure top of stack is ClosureValue.
- Ensure sufficient args present.
- Validate
arg_countmatches function signature expectations. - Account for hidden arg0 when checking callee arg arity.
- Validate ret_slots against function metadata.
Work Items
- Extend type lattice with ClosureValue.
- Define stack transitions for MAKE_CLOSURE.
- Define stack transitions for CALL_CLOSURE.
- Enforce strict failure on mismatch.
Acceptance Checklist
- ClosureValue type exists.
- Invalid CALL_CLOSURE rejected.
- Hidden arg0 accounted for.
- ret_slots validated.
- All verifier tests pass.
Tests
- Valid closure call passes verification.
- CALL_CLOSURE with wrong arg_count fails.
- CALL_CLOSURE on non-closure fails verification.
- Nested closure calls verify correctly.
Junie Instructions
You MAY:
- Extend verifier model.
- Add tests.
You MUST NOT:
- Weaken verification rules.
- Replace verifier checks with runtime-only traps.
If function metadata (arg_slots/ret_slots) is insufficient, STOP and request clarification.
Definition of Done
Verifier fully supports closure creation and invocation under Model B semantics.