For the complete documentation index, see llms.txt. This page is also available as Markdown.

Wasm Engine

The +engine core implements the core WebAssembly interpreter engine. This runs module instantiation, function invocation, and the main evaluation loop for WebAssembly instructions.

+engine

++  engine
  =,  engine-sur
  =,  op-def
  |%
  ::  ...
  --

Exposes the +engine-sur and +op-def namespaces containing interpreter data types and instruction definitions.

+get-types

++  get-types
  |=  a=(list coin-wasm)
  ^-  (list valtype)
  %+  turn  a
  |=  c=coin-wasm
  ^-  valtype
  ?.  ?=([%ref r=*] c)
    -.c
  -.r.c

Extract $valtypes from a list of $coin-wasm values. Used primarily for function parameter type checking.

+mint

Generate initial values for local variables. Creates default zero values for numeric and vector types, and null references for reference types.

+make-export-map

Convert an $export-section into a map from export names to their $export-desc for efficient lookup.

+find-func-id

Find function index by export name. Looks up the function in the module's exports and returns its internal function index.

+conv

Convert a parsed $module into a $store suitable for execution. Restructures the module by:

  • Reorganizing imports with +import-upd.

  • Fusing function signatures with their code implementations.

  • Setting up the execution environment.

+import-upd

Reorganize imports from the flat list structure in parsed modules to the categorized structure needed by the engine's $import-section.

+prep

Prepare a parsed $module for execution. First converts it to the engine's internal representation, then instantiates it through the full WebAssembly instantiation process.

+instantiate

Main WebAssembly module instantiation process following the WebAssembly Core Specification. Executes the instantiation steps in order:

  1. Initialize global variables.

  2. Initialize tables with null references.

  3. Initialize table elements from element segments.

  4. Initialize memory if present.

  5. Initialize memory from data segments.

  6. Execute the start function if present.

Uses +wasm-bind to handle potential import blocking during initialization.

+init-globals

Initialize global variables according to their constant initializer expressions. Handles various constant types and imported globals, potentially blocking on unresolved imports.

+init-table

Initialize tables by creating arrays filled with null function references according to each table's minimum size $limits.

+init-elems

Initialize active element segments by copying their contents into tables at the specified offsets. Only processes active segments; passive segments are handled by explicit instructions during execution.

+init-mem

Initialize linear memory with the minimum number of pages specified in the memory section. WebAssembly currently supports only one memory per module.

+init-data

Initialize active data segments by copying their contents into linear memory at specified offsets. Uses the memory.init instruction implementation for the actual memory copying.

+start

Execute the start function if one is defined. The start function must take no parameters and return no values.

+wasm-need

Extract successful result from a $result. Crashes if the result indicates an error or blocking state.

+wasm-bind

Monadic bind operator for chaining $result computations. If the first result is successful, applies the continuation function; otherwise propagates the error or blocking state.

+invoke

Invoke an exported function by name with the given arguments. Looks up the function by name in the exports table, then delegates to +invoke-id.

+invoke-id

Invoke a function by its index with type checking. Converts the input $coin-wasm values to stack values, calls the function, and converts the results back to typed values.

+call

Call a function by ID within the interpreter. Handles both imported functions (which create import requests) and local functions (which are executed directly).

For local functions:

  1. Extracts parameters from the stack.

  2. Sets up local variables (parameters + declared locals).

  3. Evaluates the function body.

  4. Handles return values and control flow.

+eval

Evaluate a WebAssembly $expression (sequence of instructions) by applying each instruction in turn to the $local-state.

Short-circuits evaluation when:

  • The expression is empty.

  • There's an active branch signal (from br, return, etc.)

+dec-br

Decrement branch target labels when exiting nested control structures. Preserves absolute branching coordinates (trap, return, block) and safely decrements relative target indices. Used for control flow navigation when exiting blocks and loops.

+apply

Apply a single $instruction to the $local-state. Most instructions delegate to +fetch-gate:op-def, but complex control flow instructions are implemented directly:

  • %call: Direct function calls.

  • %block: Labeled blocks with proper value stack management.

  • %loop: Loops with branch-back semantics.

  • %call-indirect: Indirect calls through function tables with type checking.

  • %if: Conditional execution choosing between true/false branches.

Last updated