C and C++
C makes the smallest and fastest modules: the summary example is 25 KB, its first call compiles in about 17 ms, and later calls load it from the cache in half a millisecond. The easiest compiler is zig cc: Zig ships clang and wasi-libc together, so there's no SDK to install.
Set up
brew install zig # or download it from ziglang.org
Clang with wasi-sdk works too: replace zig cc -target wasm32-wasi below with $WASI_SDK_PATH/bin/clang --target=wasm32-wasip1.
The module
example/summary/c counts the words of a text and returns the first twelve. C has no JSON in its standard library, so it takes the text as it is (your code can pass a string) and prints JSON:
// what `summary --describe` prints: manifest.json, built in (C23 #embed)
static const char manifest[] = {
#embed "manifest.json"
, 0};
static void fail(const char *code, const char *message) {
fprintf(stderr, "{\"code\":\"%s\",\"message\":\"%s\"}", code, message);
exit(1);
}
int main(int argc, char **argv) {
const char *function = argc > 1 ? argv[1] : "";
if (strcmp(function, "--describe") == 0) {
fputs(manifest, stdout);
return 0;
}
if (strcmp(function, "run") != 0) fail("no_such_function", "only run");
// read stdin, count the words, keep the first 12 ...
fputs("{\"text\":", stdout);
put_json_string(short_text, m);
printf(",\"words\":%zu}", words);
return 0;
}
To take JSON in, add a parser such as cJSON (one .c file and a header) beside main.c and list it on the command line.
Build
zig cc -target wasm32-wasi -std=c23 -Os -s -Wl,-z,stack-size=1048576 \
-o pb_hooks/summary.pb.wasm main.c
-Os -s: smallest code, no symbols.-Wl,-z,stack-size=1048576: a 1 MB stack. Without it,zig ccgives a WASI program a 16 MB stack, and every call would start with 16 MB of memory.-std=c23: for#embed. Without it, keep the manifest in a string instead.
Call it
An API route that saves a post with its summary and word count:
// pb_pages/posts.ts
export async function post(ctx: any) {
const { body } = ctx.req.body()
const out = await $wasm.run.async<{ text: string; words: number }>("summary", String(body ?? "")) // a string goes in as it is
const record = new Record(ctx.app.findCollectionByNameOrId("posts"))
record.load({ body, summary: out.text, words: out.words })
ctx.app.save(record)
return ctx.res.json(201, { id: record.id, summary: out.text, words: out.words })
}
C++
zig c++ builds C++ the same way, with -fno-exceptions added:
zig c++ -target wasm32-wasi -fno-exceptions -Os -s -Wl,-z,stack-size=1048576 \
-o pb_hooks/mymodule.pb.wasm main.cpp
WebAssembly exception handling doesn't run here, so code that throws, the standard library's included, fails to link (undefined symbol: __cxa_throw) until exceptions are off. What you use of the standard library sets the size: a program that uses std::string and printf is 20 KB, one that reads stdin with iostreams 204 KB.
Libraries
C libraries that only compute (parsers, codecs, compression, math) usually build as they are: add their .c files to the command. What WASI preview 1 doesn't have fails to build or fails at run time: threads, sockets, fork, and files (no folder is mounted, so fopen fails).
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.