cardinal

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.

BuildCrateLoaded byUsed for
NIFcardinal_nifCardinalCore.Native in every nodeMetadata compile, record checks, naming, queries, permissions, schema plans, formats, sanitizing, route match, automation graphs
WASMcardinal_wasm@cardinal/wasm in the deskFormat and parse, expressions, record validate, the RecType definition check, name preview, automation graph check, QR and barcode SVG
Processcardinal_sandboxSandboxService, one process per passTemplate checks and renders

Crate dependencies

CrateBuilds on
cardinal_formatnothing
cardinal_exprformat
cardinal_permexpr
cardinal_flowexpr
cardinal_metaformat, expr, perm
cardinal_queryformat, expr, perm, meta
cardinal_schemameta
cardinal_namingformat, meta
cardinal_templateformat, meta
cardinal_sheetformat
cardinal_sandboxformat, template
cardinal_nifevery crate above but the sandbox
cardinal_wasmformat, 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:wasm in frontend/ builds the module with cargo rustc --profile wasm --target wasm32-unknown-unknown and copies it into frontend/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.

On this page