Skip to content
Axiom/SDK
SDK / Mods in C

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.

System
Install
Check
macOS
brew install llvm lld (Apple's own clang has no wasm backend)
$(brew --prefix llvm)/bin/clang -print-targets
Debian, Ubuntu
apt install clang lld
clang -print-targets
Windows
The LLVM installer from llvm.org
clang -print-targets

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

HELLO.Ca panel and a graph
#include "surface.h"

#define LEN(s) (sizeof(s) - 1)

__attribute__((export_name("on_frame"))) void OnFrame(void) {
  AxiomUiOverlayPanelId panel;
  if (axiom_ui_overlay_panel("Hello", LEN("Hello"), &panel) != 0) {
    /* No overlay on this backend: nothing to draw into. */
    return;
  }

  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

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
$ axiom check
$ axiom build
Flag
Why
--target=wasm32
A bare WebAssembly module, not WASI. A WASI import is refused.
-ffreestanding -nostdlib
No C library. surface.h needs only stdint.h and stddef.h, which the compiler provides.
--no-entry
A mod has no main; the host calls its exports.
--strip-all
Drops names the host does not need. A mod comes to a few hundred bytes.
no --allow-undefined
With it, a typo in a call becomes an import of a function that does not exist, and the mod fails to load on someone else's machine instead of failing to link on yours.

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:
NUMBERfrom overlaytest.c
/* Writes a number in decimal after a prefix, with no C library to do it.
 * Returns the length. */
static uint32_t Number(char* out, const char* prefix, uint32_t prefixLength,
                       uint64_t value) {
  uint32_t at = 0;
  for (; 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];
  }
  return at;
}
  • Memory is static. Keep state in static variables 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 memcpy or memset for a large struct copy or array initialisation. If wasm-ld reports one undefined, write a small one in the mod.

Events

Export on_event to take events, and read each payload with axiom_event_read:

EVENTS.Ca button press and a changed setting
static uint8_t s_Payload[128];

__attribute__((export_name("on_event"))) void OnEvent(uint32_t id,
                                                      uint32_t size) {
  if (size > sizeof(s_Payload) ||
      axiom_event_read(s_Payload, sizeof(s_Payload)) != AXIOM_EVENT_READ_OK) {
    return;
  }

  if (id == AXIOM_EVENT_UI_OVERLAY_PRESSED) {
    const AxiomUiOverlayPressedEvent* pressed =
        (const AxiomUiOverlayPressedEvent*)s_Payload;
    /* The id the mod gave the button, at an offset into the payload. */
    if (pressed->id.Offset + pressed->id.Length <= size) {
      const char* button = (const char*)s_Payload + pressed->id.Offset;
      OnPressed(button, pressed->id.Length);
    }
    return;
  }

  if (id == AXIOM_EVENT_CONFIG_CHANGED) {
    const AxiomConfigChangedEvent* changed =
        (const AxiomConfigChangedEvent*)s_Payload;
    if (changed->key.Offset + changed->key.Length <= size) {
      const char* key = (const 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.