Skip to content
Axiom/SDK
SDK / The guest ABI

The guest ABI

A mod is a WebAssembly module. It imports the calls it uses from the host, exports on_frame, which the host calls once a frame, and optionally on_event, and nothing else crosses. This page is that boundary: the header a mod compiles against, how each value crosses, and what the host checks before a mod runs a line.

A whole mod

This is observer.c from the Axiom repository, the first mod written against the generated surface, shortened by the exports its tests read. It reads how many entities there are and which frame it is, and writes nothing.

OBSERVER.Ca complete mod
#include "surface.h"

static uint32_t s_Seen = 0;
static uint64_t s_Frame = 0;

__attribute__((export_name("on_frame"))) void OnFrame(void) {
  s_Seen = axiom_entity_query_count();
  s_Frame = axiom_frame_index();
}

It includes surface.h and nothing else: no C library, no C++ library, no sysroot. Linked, it is a module whose only imports are axiom:entity.query@1 count and axiom:frame@1 index.

Exports and imports

A mod exports at most two functions the host calls. Any other export is ignored by the host, which is how the Axiom repository's test mods hand values to their tests.

EXPORTSthe only two the host calls
/* Called once a frame. A mod without it runs nothing. */
__attribute__((export_name("on_frame"))) void OnFrame(void) {
}

/* Called after on_frame, once for each event waiting. Optional. */
__attribute__((export_name("on_event"))) void OnEvent(uint32_t id,
                                                      uint32_t size) {
}

Everything else a mod does is an import. Each call in the surface is imported from a module named for its capability, axiom:frame@1, under the call's name, index. surface.h writes those attributes for you, so a mod calls axiom_frame_index() like any C function. One import is part of the ABI rather than a capability: axiom_event_read, from axiom:event, which every mod may use without declaring it (Events).

A module gets one linear memory of its own, at most 64 MiB, and no table, global or memory from the host. It has no allocator unless it brings one: most mods keep their state in static arrays, and format numbers by hand, as the overlay page shows.

surface.h

The header is generated from the surface definition, idl/axiom.idl, and is the guest ABI and nothing more: fixed-width structs and the import declarations. It needs only <stdint.h> and <stddef.h>, which the compiler itself provides.

Download surface.h

It is generated from the same surface these pages are. Every call is named the same way:

In the IDL
In C
Imported as
entity.query@1 count
axiom_entity_query_count
axiom:entity.query@1 count
ui.overlay@1 panel
axiom_ui_overlay_panel
axiom:ui.overlay@1 panel
project@1 world_to_screen
axiom_project_world_to_screen
axiom:project@1 world_to_screen

The capability reference shows the C declaration under every call a mod can make. A capability for packs only has no declaration: it is absent from the header, which is how a mod is kept from importing it.

How a value crosses

A wasm import carries four types: i32, i64, f32 and f64. Everything richer is lowered by one table, which the guest header and the host's import list are both generated from, so the two cannot disagree.

Surface type
Crosses as
Notes
u32, s32, bool
i32
bool is 0 or 1; anything else is refused, not truncated
u64, s64
i64
f32, f64
f32, f64
str
i32 ptr, i32 len
UTF-8 in the mod's own memory; invalid UTF-8 traps
[T]
i32 ptr, i32 count
elements, not bytes
[str]
i32 ptr, i32 count
an array of AxiomSpan { Offset, Length }, each checked
a struct of scalars
its fields, flattened, in order
PanelId is two i32s
Result<T>
i32 status, T through an out pointer
0 is success; the rest are the capability's errors, by index

A pointer here is an offset into the mod's own linear memory, never an address in the game. The host checks every span against the size of that memory, in 64 bits so a length cannot wrap the sum round to something small, before it reads a byte.

CROSSINGfrom waypoints.c
AxiomEntityQueryEntityId ids[32];
uint32_t alive = axiom_entity_query_alive(ids, 32);

AxiomVec3 position;
if (alive > 0 &&
    axiom_entity_transform_position(ids[0].index, ids[0].generation,
                                    &position) != 0) {
  /* A stale id, or a pack that does not answer positions. */
}

The handle crosses as its two fields. The position comes back through the out pointer, and the status says whether it did.

Handles

An entity, a panel, a list node and a hook are each named by a handle: an index and a generation. The host owns the table behind them and checks both on every call, so a handle to something that has gone is refused as stale rather than resolving to whatever took its slot. Generations come from the operating system's random generator, so a mod cannot guess one it was never handed.

Building

Any clang with the wasm backend builds a mod. These are the flags the Axiom repository builds its own test mods with:

Terminal
$ clang --target=wasm32 -std=c17 -O2 -ffreestanding -nostdlib -I . -c mod.c -o mod.o
$ wasm-ld --no-entry --strip-all mod.o -o mod.wasm

Link without --allow-undefined. With it, a call to a function that does not exist becomes an import of one instead of a link error, and the first anyone hears of the typo is a mod that will not load on somebody else's machine.

What the host checks first

Before a mod runs, the host reads its import section and compares it with the manifest and with the import list generated from the surface. Every refusal happens here, not mid-frame.

Refused as
Because
Unknown
The surface offers a mod nothing by that name. A pack-only capability lands here.
Undeclared
The manifest never named the capability, and the manifest is what the person was shown.
Linkage
It asks the host for memory, a table or a global. The sandbox hands out none of them.

Each import must also have exactly the lowered signature the surface gives it, so a module that imports count with any other type fails to load. Declaring a capability the module never imports is not a refusal; it is reported as unused.

Each call the mod imports must also be one the backend answers. An import nothing answers is left unbound, so the module fails to load with the call named rather than trapping on its first use. The overview lists what each backend answers.

Once a mod runs, every call into the host is paid for in fuel, charged per crossing by the runtime rather than counted by the mod (Frames and fuel).

Not built yet

  • The Axiom repository's generator also writes C++, Rust and Lua bindings, and those do not follow the lowering above yet. C and C++ use surface.h, and Rust uses axiom.rs, which the site writes from it (Languages).
  • The ABI has no version of its own, separate from each capability's major.
  • axiom build packages a directory as it stands. Compiling the source first, with --release or --target, is the mod toolchain's, which does not exist yet, so those flags exit 3.