WebAssembly

zig build wasm builds the interpreter for the browser and for Node.js. The module runs Tcl scripts with an exact compute limit and a memory limit, so a page can run scripts that its users write. The playground uses this build.

The WebAssembly build has the interpreter only, with no JIT. It has no command that reaches the host, and it has no capability that an embedder can turn on: no files, sockets, exec, clock, interp, or event loop. coroutine is present and returns an error. The point command is not installed. The build ztcl-host.wasm adds clock, the event loop, and dom when the page allows them: Tcl in a web page has a live example.

Install from npm

The package @beetlebug/ztcl has ztcl.wasm, the JS module, the worker, and the TypeScript types. It works with a bundler, in Node.js, and from a CDN with no build step. See Install.

npm install @beetlebug/ztcl
import { Interp } from "@beetlebug/ztcl";

const tcl = await Interp.create({ computeLimit: 1_000_000, memLimit: 64 << 20 });
console.log(tcl.eval("expr {6 * 7}").result); // 42

With no source, Interp.create and WorkerInterp load the ztcl.wasm file next to ztcl.mjs.

Build

zig build wasm

The result is zig-out/lib/ztcl.wasm. It builds in ReleaseSmall. With Zig 0.16.0, the file is 825 KB, and 287 KB with gzip -9. The module has no imports.

Load the module

web/ztcl.mjs is a small ES module over the exports. It runs in a page, in a Web Worker, and in Node.js 22 or later. Copy web/ztcl.mjs, web/ztcl-worker.mjs, and ztcl.wasm to your site.

In a page:

<script type="module">
import { Interp } from "./ztcl.mjs";

const tcl = await Interp.create("./ztcl.wasm", {
  computeLimit: 1_000_000,
  memLimit: 64 << 20,
  onOutput: (text) => console.log(text),
});
const r = tcl.eval("puts hello; expr {6 * 7}");
// r is { ok: true, result: "42", output: "hello\n" }
</script>

Serve the file with the type application/wasm. Then compile uses WebAssembly.compileStreaming.

In Node.js, give a file path or a file: URL:

import { Interp } from "./web/ztcl.mjs";

const tcl = await Interp.create("zig-out/lib/ztcl.wasm", { computeLimit: 1_000_000 });
console.log(tcl.eval("string repeat ab 3").result); // ababab
tcl.close();

The JS module

Function or field Use
compile(source) Compiles ztcl.wasm. source is a URL, a file path in Node.js, a Response, bytes, or a WebAssembly.Module. With no source, it is the ztcl.wasm file next to ztcl.mjs.
Interp.create(source, options) Makes an interpreter in a new WebAssembly instance. source is as for compile, and it can be left out: Interp.create(options). Compile once and pass the module to make many interpreters.
options.computeLimit The compute units of each eval. null is no limit, and it is the default.
options.memLimit The memory limit in bytes. The default is 2 GiB.
options.onOutput(text) Gets the text of puts when each eval ends. With ztcl-host.wasm, it gets the text of each puts at the time of the call.
interp.eval(script) Runs a script. Returns { ok, result, output }. result is the value of the script, or the error message when ok is false.
interp.computeLimit, interp.memLimit Change the limits for the next eval.
interp.memorySize The size of the linear memory in bytes. The memory never shrinks, so this is the peak.
interp.close() Frees the interpreter.
options.allow A list of capabilities. ztcl.wasm has only "file", for the files of a bundle. See Bundle a script. With ztcl-host.wasm, a group of the Web APIs such as "web:serial" turns on the commands web::.
options.web The module web/ztcl-web.mjs, for a group of web: in allow.
options.files Files for allow: ["file"], in place of the files of a bundle: an object of names and bytes (Uint8Array or ArrayBuffer). WorkerInterp takes it too.
interp.setFiles(files) Gives the scripts the files of files, as options.files does.
interp.fileNames() and interp.readFile(name) With ztcl-shell.wasm: the names of the files that the scripts wrote, and a copy of the bytes of one, or null. WorkerInterp.eval(script, { collect: true }) gives them in files.
options.webRoot The global object of the paths of web::root, globalThis by default.
interp.runEvents(now) With ztcl-host.wasm and allow: ["event"]: runs the events (after, after idle) that are due at now, as update runs them. now is in milliseconds on the clock of options.now (Date.now by default), and it can be left out. Returns { ok, result, output }. After each eval and each run, a timer of setTimeout calls it at the time of the next event. vwait and after ms with no script give an error, because a call into wasm cannot wait for the page.
options.onEvents(r) Gets the { ok, result, output } of each run of the events from the timer.
interp.run() Runs the scripts of a bundle. Returns { ok, result, output }.
interp.bundleSize The bytes of the scripts and files of a bundle, or 0.
run(source, options) Makes an interpreter for the bundle source, runs its scripts, and frees it. Returns a promise of { ok, result, output }.
new WorkerInterp(wasmUrl, options) Runs scripts in a Web Worker (ztcl-worker.mjs). The options are as for Interp.create. wasmUrl can be left out. When ztcl.mjs comes from another origin, such as a CDN, the worker starts from a blob script that imports ztcl-worker.mjs.
wasmURL() The URL of the ztcl.wasm file next to ztcl.mjs.
worker.eval(script) Returns a promise of { ok, result, output, memorySize }.
worker.reset() Makes a new interpreter in the worker for the next eval.
worker.stop() Ends the worker and the script in it. A pending eval gives { ok: false, result: "stopped" }. The next eval starts a new worker.

One interpreter keeps its procs and variables from one eval to the next. Each Interp has its own instance and its own linear memory, so close and a new Interp give the memory back to the host.

The exports

ztcl.mjs uses these exports of src/wasm.zig. A host in another language can call them directly.

Export Use
ztcl_new() Makes an interpreter. Returns a handle, or 0. The memory limit is 2 GiB, and there is no compute limit.
ztcl_delete(h) Frees the interpreter.
ztcl_alloc(len), ztcl_free(ptr, len) Allocate and free memory for the text of a script.
ztcl_eval(h, ptr, len) Runs the UTF-8 script at ptr. Returns 0 on success and 1 on an error.
ztcl_result_ptr(h), ztcl_result_len(h) The result or the error message. It is valid until the next ztcl_eval.
ztcl_output_ptr(h), ztcl_output_len(h), ztcl_output_clear(h) The text of puts since the last clear.
ztcl_set_compute_limit(h, n) Sets the compute units that are left, as a u64 (a BigInt in JS). Each script uses compute units from this count.
ztcl_set_mem_limit(h, bytes) Sets the memory limit.
ztcl_set_gc_stress(h, n) A test mode: a collection at each Nth safe point. 0 turns it off.
ztcl_bundle_size() The bytes of the payload of a bundle, or 0.
ztcl_bundle_run(h) Runs the scripts of a bundle in order. Returns 0 on success and 1 on an error.
ztcl_allow(h, bits) Turns on capabilities. This module has only file (16). Returns 0 on success, 1 when the memory ends, and 2 for another bit.
ztcl_files_set(h, ptr, len) Gives the interpreter a bundle payload (the form of src/wasmbundle.zig) in place of the bundle of the module. The interpreter keeps a copy. Returns 0, or 1 when the memory ends.
ztcl_file_names(h) and ztcl_file_get(h, name, len) Set the result to the names of the files that the scripts wrote in memory, or to the bytes of one. ztcl_file_get returns 1 for no such file.
ztcl_events_allow(h) In ztcl-host.wasm only: turns on event with the clock of the host. Returns 0, or 1 when the memory ends.
ztcl_run_events(h, now_ms) In ztcl-host.wasm only: runs the events that are due at now_ms (an f64). Returns 0 on success and 1 on an error.
ztcl_next_event(h, now_ms) In ztcl-host.wasm only: the milliseconds from now_ms to the next event (an f64), 0 for a due event, and -1 for no event.
memory The linear memory.

Interp.eval sets the compute limit before each call. So the limit applies to each eval, and not to the sum of all calls.

Limits

Bundle a script

ztcl build --wasm writes a copy of ztcl.wasm with Tcl scripts and read-only files in it. The command patches the module and needs no Zig toolchain.

zig build wasm
ztcl build --wasm --base zig-out/lib/ztcl.wasm --embed data -o app.wasm lib.tcl app.tcl
app.wasm: 1014418 bytes (ztcl.wasm: 1014098 bytes, bundle: 311 bytes)

Run the bundle with the JS module or the npm package:

import { Interp, run } from "@beetlebug/ztcl";

// Make an interpreter, run the scripts, and free the interpreter.
const r = await run(new URL("./app.wasm", import.meta.url), {
  computeLimit: 1_000_000,
  memLimit: 64 << 20,
  allow: ["file"],
  onOutput: (text) => console.log(text),
});

// Or keep the interpreter, and call procs of the bundle after the run.
const tcl = await Interp.create("app.wasm", { allow: ["file"] });
tcl.run();
tcl.eval("sq 9");
tcl.close();

node tools/wasm-bundle.mjs zig-out/bin/ztcl zig-out/lib/ztcl.wasm checks that the output of each bundle equals the output of ztcl, the limits, and the files. The npm package tests run a bundle in Node.js and in headless Chromium.

The bundle is level 1 of issue #3 in a short form, for a browser and Node.js. The WASI component of the next section is the full level 1. The virtual file system of #40, with glob, file, and zip archives, replaces the file table of the bundle.

Bundle a script as a WASI component

ztcl build --component writes a copy of the command component of WASI 0.3.2 (zig build wasi-component) with the scripts and the files in it. The payload is the payload of a native app of ztcl build: a zip archive of the scripts, the files of --embed, and a manifest. The command patches the core module of the component and needs no Zig toolchain and no wasm-tools.

zig build wasi-component
ztcl build --component --base zig-out/bin/ztcl.component.wasm --embed data.txt -o app.wasm app.tcl
wasmtime run app.wasm one two
app.wasm: 2888538 bytes (ztcl.component.wasm: 2888052 bytes, payload: 477 bytes)

tools/wasi-component.sh checks that a bundle gives the output and the exit code of the native app of the same scripts. The tests run under wasmtime only. A second component runtime is task 09 of the WASI tasks.

Compile procs to WebAssembly

ztcl compile --wasm makes a module with a WebAssembly function for each proc of a script. The runtime stays in the module for lists, strings, dicts, and each slow path. The command patches the module and needs no Zig toolchain.

zig build wasm-aot
ztcl compile --wasm -o app.wasm app.tcl
app.wasm: 6 procs compiled to 6 functions, 0 skipped

Speed in Node.js 26 on an x86-64 Linux machine with other load, the best of 5 runs of each eval, against tclsh 8.6.16 (the whole process, best of 5):

Script ztcl.wasm interpreter Compiled Compiled / interpreter tclsh
bench/sum.tcl 42.5 ms 11.8 ms 0.28 60.2 ms
bench/foreach.tcl 33.0 ms 17.8 ms 0.54 35.0 ms
bench/readloop.tcl 23.7 ms 13.4 ms 0.56 29.3 ms
bench/nested.tcl 77.6 ms 49.6 ms 0.64 75.6 ms
bench/fib.tcl 29.6 ms 19.8 ms 0.67 52.7 ms
bench/dict.tcl 51.7 ms 36.4 ms 0.70 50.5 ms
bench/dsum.tcl 92.3 ms 65.1 ms 0.71 98.4 ms
bench/strbuild.tcl 52.6 ms 38.7 ms 0.74 65.8 ms
bench/dictgrow.tcl 145.5 ms 128.7 ms 0.88 107.4 ms
kostya bf.tcl bench.b, one run 207.58 s 148.38 s 0.71 257.33 s
kostya base64/test.tcl, one run 1.77 s 1.80 s 1.01 4.35 s

The gain is in the bytecode of the procs. Work in the builtins, such as binary encode base64, runs at the same speed in both modules. The rows of dictgrow.tcl and base64/test.tcl are after the faster base64 code and the XxHash32 hash of dict keys on wasm32. Before them, base64/test.tcl ran in 4.54 s and dictgrow.tcl in 136.6 ms compiled. On wasm, dictgrow.tcl is still slower than tclsh: its time is in the hash table, the text of each key, and the dict command, and the native build of ztcl runs it in 73 ms against 108 ms for tclsh. node tools/wasm-aot-bench.mjs --kostya bench/*.tcl makes this table. tools/wasm-aot.mjs, tools/wasm-aot-suite.mjs, and tools/wasm-aot-fuzz.sh compare the compiled modules with the interpreter. The design has the details.

Compile a point proc

ztcl compile FILE -proc NAME -o OUT.wasm makes a small module from one point proc: no interpreter and no imports. A host such as a slicer runs it over columns of points in its own memory.

ztcl compile tests/point/fuzzy.tcl -proc fuzzy -o fuzzy.wasm -manifest fuzzy.json
node examples/point-wasm/run.mjs fuzzy.wasm
fuzzy: 2000 points, 35 compute units
offset: 0.000000 -0.007489 -0.017769 -0.031562 ...
Option What it does
-simd on|off on (the default) uses wasm SIMD, two points in each v128. off makes a module for a runtime with no SIMD.
-max-mem BYTES The size of the memory. The module cannot grow it, so cap, the most points for one call, comes from this size. The default is 16 MiB.
-features a,b,... The names of the feature numbers, in order.
-manifest FILE Writes a JSON fragment for the manifest of a host: the exports, the global of each column, the compute units, cap, the memory size, and an empty list of imports.

The module of ABI version 1 has these parts:

The host writes the columns at the offsets, calls run, and reads the outputs. A module keeps no state between calls. The outputs of a call are equal, bit for bit, to Program.run of the native interpreter: tools/point-wasm-diff.sh checks each fixed proc of tests/point/. The design has the details, and bench/point-wasm.tsv has the speed against the native Program.run. On an Intel Core i7-6700K with Node.js 26.12.0, a module is 1.7 to 2.7 times slower than the native Program.run for the procs branches, elseif_noise, and ops, about equal for fuzzy, and faster for int_zero.

Examples

The examples are in examples/web/. To run the browser examples, build the module and serve the root of the repository:

zig build wasm
python3 -m http.server --bind 127.0.0.1 8000

Then open http://127.0.0.1:8000/examples/web/repl.html, rules.html, or worker.html. node tools/wasm-web.mjs zig-out/lib/ztcl.wasm tests the JS module and the Node.js example.

A REPL

repl.html runs each line in one interpreter, so procs and variables stay. Up and Down go through the history. Each line can use 1,000,000 compute units.

const tcl = await Interp.create("../../zig-out/lib/ztcl.wasm", {
  computeLimit: 1_000_000,
  memLimit: 64 << 20,
  onOutput: (text) => show(text),
});
const r = tcl.eval(line.value);
if (!r.ok) show(r.result, "bad");

A rule engine

rules.html lets the user write a price proc in Tcl. The page calls it for each line of an order, with 10,000 compute units and 8 MiB for each call. Select "A rule that never ends" and the Price column shows stopped: compute limit reached. The page stays live.

const tcl = await Interp.create(module, { computeLimit: 100_000, memLimit: 8 << 20 });
const def = tcl.eval(ruleText);
tcl.computeLimit = 10_000;
for (const [item, qty, unit] of order) {
  const r = tcl.eval(`price ${Number(qty)} ${Number(unit)}`);
  cell.textContent = r.ok ? r.result : `stopped: ${r.result}`;
}

Put only numbers or quoted lists into the text of a command. A value from a user can contain [, $, or ;.

A Web Worker with Cancel

worker.html runs a loop of 50 million passes in a worker. A clock updates while it runs, and Cancel ends the worker.

const tcl = new WorkerInterp("../../zig-out/lib/ztcl.wasm", { memLimit: 64 << 20 });
const r = await tcl.eval(code);   // the page stays live
cancel.onclick = () => tcl.stop(); // r is { ok: false, result: "stopped" }

Node.js

run.mjs runs a script file with limits:

node examples/web/run.mjs --compute 1000000 --mem 16 examples/web/scripts/forever.tcl
error: compute limit reached
49.6 ms, 9.2 MiB of linear memory

The second line goes to stderr, with the time of the script. The exit code is 1 on an error of the script. --compute sets the compute limit and --mem sets the memory limit in MiB.

Point procs

ztcl-simd.wasm (zig build wasm-simd) and ztcl-host.wasm (zig build wasm-host) have point procs. ztcl.wasm does not, so that it stays in its size limit. With the option point: true, Interp.create adds point proc and the output commands. pointProc(name) gives the columns that the proc writes (outputs) and run, which runs the proc over typed arrays:

const tcl = await Interp.create("ztcl-simd.wasm", { point: true });
tcl.eval(`point proc fuzzy {x y z feature} {
    if {$feature ne "outer_wall"} { return }
    offset [expr {[noise3 $x $y [expr {$z * 4}]] * 0.3}]
}`);
const fuzzy = tcl.pointProc("fuzzy");
const n = 1000;
const offset = new Float64Array(n);
const units = fuzzy.run({ n, inputs: { x, y, z }, feature, outputs: { offset } });

inputs maps the names x y z width height speed flow layer path dist to Float64Array columns, feature is a Uint8Array of feature numbers, and outputs maps the names offset move_x move_y width flow speed skip to Float64Array columns with the start values. run writes the new values into outputs and gives the compute units. The compute limit of the Interp applies to each call. The results equal the native Program.run, bit for bit: node tools/wasm-point.mjs zig-out/lib/ztcl-simd.wasm checks the procs of tests/point/. ztcl-simd.wasm is built with the SIMD instructions of wasm (simd128), so the compiler can use them in the loops of Program.run. Point procs also work in a Zig program.

Threads

zig build wasm-threads makes two more modules with the capability thread. The JS module web/ztcl-threads.mjs (ztcl/threads in the npm package) loads them. Each ztcl thread runs in its own worker: a Web Worker in a browser, or a worker_threads worker in Node.js. The main interpreter runs in a worker too, so a wait never blocks the page.

import { ThreadInterp } from "./ztcl-threads.mjs";

const t = await ThreadInterp.create({ allow: ["thread", "event"], limits: { maxThreads: 4 } });
const r = await t.eval("set id [thread::create]; thread::send $id {expr {6 * 7}}");
// r.result is "42"
t.close();

The host picks one of two modes:

Mode When Module Commands
shared Node.js, or a page that is cross-origin isolated. ztcl-threads.wasm Each command of Threads, with tsv, the mutexes, the conditions, and tpool. The capabilities event, clock, package, and interp can also be on.
async A page with no cross-origin isolation. ztcl-async.wasm thread::create, thread::send -async, thread::broadcast, thread::wait in a thread, thread::id, thread::names, thread::exists, thread::preserve, thread::release, and tpool::create, tpool::post, tpool::get, tpool::names, tpool::preserve, and tpool::release.

tools/wasm-threads.mjs runs the 9 files of tests/thread/ that need no channel in Node.js and in headless Chromium on a server with the headers. It also runs tests/web/threads-async.tcl in Chromium on a server with no such headers.

More

The issues track the planned WebAssembly work, such as WASI components.