Tools
The Nox compiler in the browser
The whole Nox compiler runs as WebAssembly: source code is compiled to .wasm in the browser (or in Node) and run there. No server, no noxc binary, no zig or clang installation is needed, and the code is sent nowhere. It powers the Playground and is meant for embedded code sandboxes and offline tools.
There are two parts:
- The front end (
noxc-frontend.wasm, about 2 MB): lexer, parser, type checker and LLVM IR generation. Thenox.*standard library is embedded in the module. It produces byte-for-byte the same IR as a nativenoxc build --target wasm32-*(it is the same code,compiler/frontend.zig). It checks and compiles a program in about 30 ms and returns syntax and type errors as readable text. - The back end (clang and wasm-ld, themselves WebAssembly): links the IR with the runtime objects and produces
.wasm. The default provider is@yowasp/clang(an optional dependency, about 25 MiB gzipped, loaded lazily on first use and kept in the browser cache); any other provider with a compatiblerunClang(argv, files)can be passed instead.
Usage#
JS
import { createNoxCompiler } from './wasm_compiler.mjs';
import { runNox } from './wasm_host.mjs';
const nox = await createNoxCompiler({
frontend: await (await fetch('noxc-frontend.wasm')).arrayBuffer(),
// runtime objects: noxrt-wasm32-freestanding.o, ...-sqlite3.o, compiler-rt-wasm32.a (the lib/ directory of a release archive)
asset: async (name) => new Uint8Array(await (await fetch(`lib/${name}`)).arrayBuffer()),
clang: () => import('@yowasp/clang'),
});
const r = await nox.compile('print("hello", 6 * 7)\n', { target: 'wasm32-freestanding' });
if (!r.ok) console.error(r.diagnostics); // for example "main.nox:1:9: syntax error: ..."
else await runNox(r.wasm, io); // io: write/read/clockMs/randomFill/sleepMs (see the WebAssembly page)nox.compileToIr(source, opts)runs only the front end ({ ok, ir, plan, diagnostics }): ideal for live error checking.opts.target:wasm32-freestanding(the default),-asyncify,-threads.opts.deny: compile-time capability bans (['fs', 'network'], see--deny-capability).opts.label: the file name shown in diagnostics. These options cannot contain control characters.nox.addFile('/stdlib/...' | path, text): adds an in-memory virtual file (adding the same path again replaces it).--asyncifytargets need a post-linkwasm-opt: in a browser usebinaryen(npm, Apache-2.0) withimport { binaryenAsyncify } from './wasm_asyncify.mjs'; createNoxCompiler({ ..., optimize: binaryenAsyncify(binaryen) }). Without it a clear error is returned. Thenox.httpclient (fetch) works only on this target.
When running, the host options on the WebAssembly page apply: the in-memory file system, optional persistence (vfs) and — only when explicitly given — the network (fetch). On pages that run untrusted code, restrict capabilities at compile time with deny and give runNox no fetch or vfs.
Distribution and limits#
- Next to the release archive a
nox-web-compiler-<version>.tar.gzis published:noxc-frontend.wasm,wasm_compiler.mjs,wasm_asyncify.mjs,wasm_host.mjsandlib/(the runtime objects). From source:zig build wasm-compiler wasm-rt. - The first compile loads clang/lld (hundreds of ms); later ones take about 0.3-1 s. Large programs and
nox.sqlite(an output of about 800 KB) take a little longer. - Native
extern def ... from "<library>"declarations cannot be linked in a browser (there are no native libraries); thenative,pluginandhpymodules are unavailable on WebAssembly anyway. - Verification:
scripts/wasm_frontend_corpus.pycompares the front end's IR with nativenoxcover the wholecodegen_casescorpus (467 identical, plus 16 rejected for the same reason);scripts/web_compiler_e2e.mjsruns the full pipeline (front end, clang, run) over the corpus and compares with.expected(wasm32-freestanding453 ok,wasm32-freestanding-asyncify458 ok with binaryen).