NIF and WASM
How one set of Rust crates runs inside the BEAM and in the browser.
The crates in native/ do pure compute. They read no database, network, file or clock. Callers pass everything in, including the time, the zone and random bytes. That lets the same code run in two places. The two exceptions are the password scorer in cardinal_nif, which reads the current year, and cardinal_sandbox, which reads stdin and writes stdout.
| Build | Crate | Loaded by | Used for |
|---|---|---|---|
| NIF | cardinal_nif | CardinalCore.Native in every node | Metadata compile, record checks, naming, queries, permissions, schema plans, formats, sanitizing, route match, automation graphs |
| WASM | cardinal_wasm | @cardinal/wasm in the desk | Format and parse, expressions, record validate, the RecType definition check, name preview, automation graph check, QR and barcode SVG |
| Process | cardinal_sandbox | SandboxService, one process per pass | Template checks and renders |
Crate dependencies
| Crate | Builds on |
|---|---|
cardinal_format | nothing |
cardinal_expr | format |
cardinal_perm | expr |
cardinal_flow | expr |
cardinal_meta | format, expr, perm |
cardinal_query | format, expr, perm, meta |
cardinal_schema | meta |
cardinal_naming | format, meta |
cardinal_template | format, meta |
cardinal_sheet | format |
cardinal_sandbox | format, template |
cardinal_nif | every crate above but the sandbox |
cardinal_wasm | format, expr, meta, naming, flow, template |
NIF rules
- Calls are coarse: one call per Record or per batch, not per field.
- Heavy NIFs run on dirty schedulers, so a long compile does not block the BEAM.
- Records and queries cross the boundary as JSON in their wire forms. Decimals stay strings.
- Compiled metadata stays in Rust as a resource that Elixir holds a handle to.
WASM rules
- Every operation is one JSON call,
call(op, args), answered{ok: result}or{err: [problems]}. A decimal never becomes a JavaScript number. bun run build:wasminfrontend/builds the module withcargo rustc --profile wasm --target wasm32-unknown-unknownand copies it intofrontend/packages/wasm/. Rebuild it after any Rust change.- The browser runs no permission checks. The server decides every right.
- The module must stay under 500 KB gzipped. A test enforces it.
Templates run in their own process
cardinal_sandbox is a separate binary, not a NIF. Each template check or render pass starts it with one JSON request on stdin, under a 64 MB heap, 2 seconds of CPU and no core file. A runaway template ends that process, not the node. A node runs at most CARDINAL_SANDBOX_LIMIT (16) at once. A pass waits up to 2 seconds for a slot and then fails as busy.
The parity corpus
Server and browser must agree, so each large crate has one golden corpus:
cargo test writes and checks the expected outputs.
ExUnit replays the corpus through the NIF. For WASM operations, a test-only NIF build of cardinal_wasm (the nif feature) replays parity.jsonl.
bun test replays the same file through the WASM module and compares every output byte for byte.
A stale WASM module fails the parity test, which is why the frontend tests need bun run build:wasm first.