Session Engine

July 23, 2026 ยท View on GitHub

runmat-core owns the host-agnostic execution session. A RunMatSession is the long-lived object that a REPL, notebook host, CLI command, or WebAssembly wrapper uses to submit MATLAB source, keep workspace variables alive, preserve user-defined functions, expose inspection data, handle input, and return structured execution results.

The session is not a second compiler or a second VM. It is the orchestration layer around the parser, HIR/MIR pipeline, VM, optional Turbine JIT, runtime builtins, GPU provider state, and host-facing ABI.

Execution Spine

flowchart TD
  Host["Host API<br/>CLI, script runner, WASM, notebook"]
  Req["ExecutionRequest<br/>source, compat mode, host policy, workspace"]
  Session["RunMatSession::execute_request"]
  Internal["execute_internal"]
  Compile["compile_input"]
  Parser["parser AST"]
  HIR["HIR lowering"]
  MIR["MIR lowering and analysis"]
  Bytecode["VM bytecode"]
  Workspace["workspace bridge<br/>workspace_values <-> variable_array"]
  Engine{"execution tier"}
  JIT["Turbine JIT<br/>when eligible"]
  VM["VM interpreter"]
  Runtime["runtime builtins<br/>streams, warnings, plots, input"]
  Outcome["ExecutionOutcome"]
  Payload["host payload"]

  Host --> Req --> Session --> Internal --> Compile
  Compile --> Parser --> HIR --> MIR --> Bytecode
  Bytecode --> Workspace --> Engine
  Engine --> JIT
  Engine --> VM
  JIT --> Runtime
  VM --> Runtime
  Runtime --> Workspace --> Outcome --> Payload

What The Session Owns

Field or subsystemPurpose
variable_arrayVM slot storage used during the current execution.
workspace_bindingsName-to-slot mapping plus stable ABI binding identity.
workspace_valuesDurable variable values retained between requests.
function_registryUser-defined semantic functions retained across interactive inputs.
source_poolInterned source text and names for diagnostics and call stacks.
interrupt_flagCooperative cancellation flag shared with the runtime and VM.
async_input_handlerHost callback for input, keypress, and pause-like interactions.
stats, profiling, telemetryExecution counters and optional host analytics hooks.
compat_mode, top_level_await_enabledPer-session defaults that can be overridden by a request.

RunMatSession rejects concurrent execution on the same session. Hosts that need parallel execution should create separate sessions or serialize requests through one session.

Page Map

PagePurpose
Execution RequestsThe structured request/result ABI around session execution.
Workspace StateHow variable slots, stable binding keys, deltas, removals, and ans work.
Variable InspectionWorkspace entries, previews, materialization, preview tokens, and GPU slices.
Workspace ReplayExporting and restoring live workspace state.
Interaction & StreamsConsole streams, warnings, async input, cancellation, diagnostics, and effects.
Host IntegrationCLI, WASM/TypeScript, notebook, editor, and lifecycle responsibilities.

For the compiler stages inside compile_input, see Compilation Pipeline. For bytecode execution details, see VM Interpreter & Bytecode. For native execution, see Turbine JIT Compiler.