Expand description
Typed closure layout for v2 runtime.
A TypedClosure parallels TypedStruct: it has an 8-byte HeapHeader
followed by a function_id: u32 / type_id: u32 pair, then a compact
C-style capture area with compile-time-known offsets.
§Memory layout
Heap variant (escaping closure):
Offset Size Field
------ ---- -----
0 8 HeapHeader
8 4 function_id (u32)
12 4 type_id (u32, ClosureTypeId.0)
16+ .. captures[] (C-laid-out per ClosureLayout)
Stack variant (non-escaping closure, Cranelift StackSlot):
Offset Size Field
------ ---- -----
0 4 function_id (u32)
4 4 type_id (u32, ClosureTypeId.0)
8+ .. captures[]Captures start 8-byte aligned in both variants (HeapHeader and the function_id+type_id pair are both 8 bytes). The relative offset of each capture inside the captures area is the same for both variants — only the preceding header differs.
§Keying
ClosureTypeIds are minted per capture signature (Vec<ConcreteType>),
not per closure literal. The closure body is carried separately by
function_id. Two literals with identical captures (e.g. two |x| x + 1
expressions with no captures) share ClosureTypeId(0). See
docs/v2-closure-specialization.md §1.2.
Structs§
- Closure
Layout - Computed layout for a closure’s captures.
- Closure
Registry - Registry of closure capture layouts, keyed on capture signature AND per-capture kind.
- Shared
Cell - Interior-mutable cell backing a
CaptureKind::Sharedcapture. - Shared
Cell Guard - RAII guard returned by
SharedCell::lock. Releases the lock on Drop. Dereffs to the innerValueWord. - Stack
Closure - Stack-allocated closure. No
HeapHeader; captures follow thefunction_id/type_idpair at offset 8. - Typed
Closure Header - Heap-allocated closure. The
HeapHeaderis at offset 0; captures follow thefunction_id/type_idpair at offset 16.
Enums§
- Capture
Kind - Storage discipline for a closure capture.
Constants§
- HEAP_
CLOSURE_ HEADER_ SIZE - Byte size of the heap closure header:
HeapHeader (8) + function_id (4) + type_id (4). - SHARED_
CELL_ LOCKED - Locked state byte value.
- SHARED_
CELL_ STATE_ OFFSET - Byte offset of the lock state byte within
SharedCell. The JIT’s inline lock CAS targets this offset as a compile-time constant. - SHARED_
CELL_ UNLOCKED - Unlocked state byte value.
- SHARED_
CELL_ VALUE_ OFFSET - Byte offset of the value payload within
SharedCell. The JIT’s inline load/store targets this offset as a compile-time constant. - STACK_
CLOSURE_ HEADER_ SIZE - Byte size of the stack closure header:
function_id (4) + type_id (4).
Functions§
- native_
kind_ from_ concrete_ type - Map a
ConcreteTypeto the matchingNativeKindfor closure-capture kind tracking (ADR-006 §2.7.8 / Q10).