Alternera.PluginWasmFramework.Runtime.Wasmex (alternera_plugin_wasm_framework v0.9.0)

Copy Markdown View Source

PluginWasmFramework.Runtime on wasmex 0.18.1, a precompiled NIF over Wasmtime. Experimental: the binding is chosen for good at the gate after the first language plugin, with numbers from the Observatory.

One engine per node (engine/0, Cranelift optimising for speed), every module compiled with it once (prepare/1). A call (call/5):

  1. spawns a worker process and takes a slot for it (PluginWasmFramework.Slots, the node's limit on guests running at once); the worker then creates a WASI store with nothing granted (no directories, no environment, no arguments; standard streams go nowhere) and the store memory limit memory_bytes, and instantiates the module in it, with alternera.host_call and alternera.body_read as its imports;
  2. calls _initialize when the module exports it (a reactor that was not pre-initialised), then alternera_alloc for the input, writes it, and calls the export with the input's pointer and length;
  3. reads the reply and the memory's size, and exits: the store and the instance go with the worker's references, at once rather than at the calling process's next garbage collection (the calling process is the request's, long-lived, and a store holds up to the call's whole memory limit).

wasmex delivers each import call to the process that made the instance, as {:invoke_callback, …}, and takes the answer through Wasmex.Native.instance_receive_callback_result/3. The worker reads the call's arguments out of guest memory, relays the request to the calling process, which serves it with the call's host functions (so a pipeline.call runs there), and writes the answer back: a buffer from the guest's own alternera_alloc, called through the callback's caller.

The timeout is wasmex's, per export call: on expiry it interrupts the guest at its next epoch check (its ticker runs every 10 ms) and sends no reply, so the worker's own receive … after is the answer. For 50 ms past the deadline the worker answers every host call with a failure, which unwinds the guest, rather than leave a store blocked on a callback nobody answers; anything later that reaches the dead worker's mailbox is dropped with it, and wasmex fails the import once the callback's token is collected. The calling process keeps its own deadline a second past the worker's and kills the worker when it is missed, then drops what the worker sent before it went (flush/1), as Script.Sandbox does for its call process.

Summary

Types

t()

A compiled module: the wasmex module and whether it needs _initialize.

Functions

The node's engine, made on first use. Two first calls at once make it once: a module compiled with an engine that lost the race cannot be instantiated in a store of the winner, and its epoch ticker stops when it is collected.

One guest fewer than wasmex's threads (the processors the node may use): a guest keeps its thread until the next epoch tick, which is a task on the same threads, so one stays free for the tick (Slots).

Types

t()

@type t() :: %Alternera.PluginWasmFramework.Runtime.Wasmex{
  initialize?: boolean(),
  module: Wasmex.Module.t()
}

A compiled module: the wasmex module and whether it needs _initialize.

Functions

engine()

@spec engine() :: Wasmex.Engine.t()

The node's engine, made on first use. Two first calls at once make it once: a module compiled with an engine that lost the race cannot be instantiated in a store of the winner, and its epoch ticker stops when it is collected.

max_running()

One guest fewer than wasmex's threads (the processors the node may use): a guest keeps its thread until the next epoch tick, which is a task on the same threads, so one stays free for the tick (Slots).