Mods in C
C is the language the surface is written for. surface.h is a freestanding C header, the Axiom
repository's own test mods are C, and every example in these docs is C. This page is the setup, the
build and the habits that keep a C mod small and safe.
Set up
You need a clang with the WebAssembly backend, and wasm-ld.
The check should list wasm32. Then download surface.h into the mod's directory.
You do not need a C library, a sysroot or WASI.
A mod
"surface.h" #define LEN(s) (sizeof(s) - 1) __attribute__((export_name("on_frame"))) OnFrame() { AxiomUiOverlayPanelId panel; (axiom_ui_overlay_panel("Hello", LEN("Hello"), &panel) != 0) { /* No overlay on this backend: nothing to draw into. */ ; } axiom_ui_overlay_text(panel.index, panel.generation, "Drawn by a mod.", LEN("Drawn by a mod.")); axiom_ui_overlay_graph(panel.index, panel.generation, "Frame time (ms)", LEN("Frame time (ms)"), axiom_frame_delta() * 1000.0f, 120); }
A function the host calls is exported by name with export_name. Every call into the host is a plain
C function from surface.h; the header carries each one's wasm import module and name, so there is
nothing else to declare.
Build
Several source files are fine: compile each with the same flags and pass every .o to wasm-ld.
Without a C library
There is no strlen, printf, malloc or memcpy unless you write them.
- Strings cross as a pointer and a length. For a literal,
#define LEN(s) (sizeof(s) - 1)measures it at compile time. - Numbers are formatted by hand. This is the helper the overlay test mod uses:
/* Writes a number in decimal after a prefix, with no C library to do it. * Returns the length. */ uint32_t Number(char* out, char* prefix, uint32_t prefixLength, uint64_t value) { uint32_t at = 0; (; at < prefixLength; ++at) { out[at] = prefix[at]; } char digits[20]; uint32_t count = 0; do { digits[count++] = (char)('0' + (value % 10)); value /= 10; } while (value != 0 && count < sizeof(digits)); while (count > 0) { out[at++] = digits[--count]; } at; }
- Memory is static. Keep state in
staticvariables and fixed arrays sized for what a frame uses. A mod has at most 64 MiB, and a real one needs a few kilobytes. - The compiler may still emit a call to
memcpyormemsetfor a large struct copy or array initialisation. Ifwasm-ldreports one undefined, write a small one in the mod.
Events
Export on_event to take events, and read each payload with axiom_event_read:
uint8_t s_Payload[128]; __attribute__((export_name("on_event"))) OnEvent(uint32_t id, uint32_t size) { (size > sizeof(s_Payload) || axiom_event_read(s_Payload, sizeof(s_Payload)) != AXIOM_EVENT_READ_OK) { ; } (id == AXIOM_EVENT_UI_OVERLAY_PRESSED) { AxiomUiOverlayPressedEvent* pressed = ( AxiomUiOverlayPressedEvent*)s_Payload; /* The id the mod gave the button, at an offset into the payload. */ (pressed->id.Offset + pressed->id.Length <= size) { char* button = ( char*)s_Payload + pressed->id.Offset; OnPressed(button, pressed->id.Length); } ; } (id == AXIOM_EVENT_CONFIG_CHANGED) { AxiomConfigChangedEvent* changed = ( AxiomConfigChangedEvent*)s_Payload; (changed->key.Offset + changed->key.Length <= size) { char* key = ( char*)s_Payload + changed->key.Offset; OnChanged(key, changed->key.Length); } } }
Events has every event and its payload.
Checklist
- Check every status a call returns; a call that fails wrote nothing through its out pointer.
- Ask for this frame's handles each frame, and do not keep them.
- Keep every out pointer and buffer inside the mod's own memory: one outside is a fault.
- Pass only valid UTF-8 strings: anything else is a fault.