prometeu-runtime/docs/runtime/specs/02b-vm-function-values-and-closures.md
2026-03-24 13:40:47 +00:00

2.1 KiB

VM Function Values and Closures

Domain: VM closures and function values Function: normative

This chapter defines how the PVM models first-class function values.

1 First-Class Function Model

The PVM treats user function values as closures.

This means:

  • functions can be stored in variables;
  • functions can be passed as arguments;
  • functions can be returned from other functions;
  • function values carry explicit runtime representation.

Even functions without captures may be represented as closures with an empty capture set.

2 Closure Layout

Closures are heap objects.

Conceptual layout:

Closure {
    func_id
    captures[]
}

Captures may be:

  • copied values;
  • handles to heap objects.

Closure environments are part of the GC root graph as long as the closure remains reachable.

3 Direct and Indirect Calls

The PVM supports two forms of invocation:

Opcode Description
CALL Direct call by function id
CALL_CLOSURE Indirect call through a closure handle

For CALL_CLOSURE:

  1. the closure handle is read from the stack;
  2. the VM resolves the target function;
  3. captured environment state becomes available to the callee according to the closure calling convention.

4 Verification and Safety

The verifier ensures, at minimum:

  • closure call sites have coherent arity;
  • invalid closure call shapes are rejected or trapped according to the verifier/runtime boundary;
  • return-slot contracts remain valid.

Handle validity, heap reachability, and dynamic misuse remain subject to runtime checks where static proof is not possible.

5 Relationship to Memory and GC