Skip to main content

Module reactor

Module reactor 

Source
Expand description

Tier-2 reactor ABI for wasm32-unknown-unknown (Cloudflare Workers / V8 isolates). No WASI, no stdio/argv — the module exports functions a JS host calls over linear memory (docs/design/done/WASM_TIER2_PLAN.md A3):

plg_init (emitted by the generated module) → builds the Machine, hands it to plg_rt_set_machine plg_rt_alloc(len) → ptr host writes the query bytes here plg_rt_run_query(ptr,len,…) → u64 packed (len<<32 | ptr) of a bson buffer plg_rt_free(ptr,len) host frees the result (or the query buffer) plg_rt_atom_name(id) → u64 packed (name_ptr<<32 | len) — the host resolves bson term atom ids to names. Reads the runtime interner (program + query atoms).

Bson formatting and the query path are NOT duplicated here — both go through crate::core (solve) and crate::wire (the bson encoding), the single I/O-free core the WASI shell shares.

§Concurrency contract (D3 / WASM.md finding #2)

One in-flight query per isolate. The program Machine is a single static; a V8 isolate is single-threaded, but one Worker can interleave async tasks, so the host must not call plg_rt_run_query again before the prior call returns. This matches typical Worker use (a request maps to a query) and avoids threading per-request state through the ABI.

Functions§

plg_rt_alloc
Allocate a host-writable buffer in linear memory (query in / result out).
plg_rt_atom_name
Resolve an atom id to its name, packed (name_ptr << 32) | byte_len. The host reads len bytes at ptr to get the UTF-8 name. This reads the runtime interner, which holds program atoms AND query-introduced atoms (ids ≥ the compile-time table) — the static @plg_atom_strs table can’t resolve the latter. Returns 0 for an out-of-range id (defensive; should not occur — bson atom ids always come from this interner).
plg_rt_free
Safety
plg_rt_run_query
Run one query (UTF-8 at qptr..qptr+qlen) and return packed (len << 32) | ptr of a bson byte buffer the host reads then frees via plg_rt_free. The packed return assumes wasm32 (the pointer fits in the low 32 bits); wasm64 would need a wider/two-value result (WASM.md finding #7).
plg_rt_set_machine
Safety