Skip to content

WASM ABI

WASM modules compiled from Rust (using cosmictron-sdk) import host functions from the Cosmictron server. These functions are provided at module load time by Wasmtime.

Host functions are organized into three import modules:

Import modulePurpose
datastoreDatabase read/write operations
cosmContext: identity, timestamp, scheduling
consoleLogging

All parameters and return values are either primitives (i32, i64) or pointers into WASM linear memory, where data is encoded in BSATN (binary format).


Insert a BSATN-encoded row into a table.

(func (import "datastore" "insert")
(param $table_id i32)
(param $row_ptr i32)
(param $row_len i32)
(result i32))

Returns 0 on success, non-zero error code on failure (e.g., unique constraint violation, type mismatch).

Delete a row by its primary key.

(func (import "datastore" "delete_by_pk")
(param $table_id i32)
(param $pk_ptr i32)
(param $pk_len i32)
(result i32))

Returns 0 on success, 1 if row not found.

Start a full table scan. Returns an iterator handle.

(func (import "datastore" "scan")
(param $table_id i32)
(result i64))

Returns an i64 iterator handle. Pass to iter_next to read rows.

Start a scan over an index range.

(func (import "datastore" "index_scan")
(param $table_id i32)
(param $index_id i32)
(param $range_ptr i32)
(param $range_len i32)
(result i64))

Advance an iterator and write the next row into WASM memory.

(func (import "datastore" "iter_next")
(param $iter_handle i64)
(param $out_ptr i32)
(param $out_len i32)
(result i32))

Returns bytes written, or 0 if the iterator is exhausted.

Release an iterator handle.

(func (import "datastore" "iter_close")
(param $iter_handle i64))

Look up a table ID by name (UTF-8 string in WASM memory).

(func (import "datastore" "table_id_from_name")
(param $name_ptr i32)
(param $name_len i32)
(result i32))

Write the current caller’s identity bytes into WASM memory.

(func (import "cosm" "sender_identity")
(param $out_ptr i32)
(param $out_len i32)
(result i32))

Get the current transaction timestamp (Unix microseconds).

(func (import "cosm" "timestamp")
(result i64))

Schedule a reducer to run at a future time.

(func (import "cosm" "schedule_reducer")
(param $reducer_ptr i32)
(param $reducer_len i32)
(param $args_ptr i32)
(param $args_len i32)
(param $delay_micros i64)
(result i64))

Returns a schedule handle. Pass to cancel_reducer to cancel.

Cancel a scheduled reducer.

(func (import "cosm" "cancel_reducer")
(param $handle i64))

Make an outbound HTTP request from a reducer.

(func (import "cosm" "http_request")
(param $req_ptr i32)
(param $req_len i32)
(param $out_ptr i32)
(param $out_len i32)
(result i32))

The request is BSATN-encoded HttpRequest { method, url, headers, body }. The response is written to out_ptr.


Write a log message.

(func (import "console" "log")
(param $level i32)
(param $msg_ptr i32)
(param $msg_len i32))

Log levels: 0=trace, 1=debug, 2=info, 3=warn, 4=error.


CodeMeaning
0Success
1Row not found
2Unique constraint violation
3Type mismatch
4Table not found
5Permission denied (RLS)
6Fuel exhausted
7Memory limit exceeded
8Transaction conflict (OCC)
9Invalid argument

BSATN is a compact binary encoding similar to MessagePack. Type prefixes:

TagType
0x00u8
0x01u16
0x02u32
0x03u64
0x08i8
0x09i16
0x0Ai32
0x0Bi64
0x10bool
0x11String (4-byte length prefix + UTF-8)
0x12bytes (4-byte length prefix)
0x20Option<T> (1-byte tag: 0x00 = None, 0x01 = Some)
0x30Vec<T> (4-byte length + elements)
0x40Struct (fields in declaration order, no tags)