SCUA

How-to

Run SCUA under WebAssembly

Every SCUA release includes scua-X.Y.Z-wasm32-wasi.wasm next to the macOS, Linux and Windows archives. It is the scua command built for WebAssembly (WASI), in a single file. A WASI host such as wasmtime or Node runs it on any operating system and processor. The language is the same, and so are the options and the capability flags. The module can only reach the folders that the host hands it.

Use it to run SCUA where no native build exists, or to put a script in a sandbox that the runtime enforces as well as SCUA. It is slower than the native build because it has no JIT, and it leaves out the features listed at the end of this page.

#Download it

Download the .wasm from the release page, the same way you download an archive. The release's SHA256SUMS covers it, so you can check it like the archives:

shasum -a 256 -c SHA256SUMS --ignore-missing   # sha256sum on Linux

The rest of this page calls the file scua.wasm. Rename yours or use its full name.

#Run it with wasmtime

wasmtime run scua.wasm --version
scua X.Y.Z

wasmtime's own options come before the .wasm file. Everything after it goes to scua, so scua's options, the script and the script's arguments are in the usual order.

A WebAssembly module starts with no files at all, including the script. --dir=.::. gives it the current folder (the host's ., which the module also sees as .):

-- hello.scua
print("hello from WebAssembly")
wasmtime run --dir=.::. scua.wasm hello.scua
hello from WebAssembly

Arguments, standard input and the exit code work as they do natively:

echo "a line" | wasmtime run --dir=.::. scua.wasm tool.scua first second
echo $?

#Read and write files

Two grants control file access, and a script needs both of them. wasmtime's --dir decides which folder the module can see at all. SCUA's --allow-fs lets the script use fs inside that folder:

-- notes.scua
import fs

print(fs.read_text("notes.txt")?)
fs.write("seen.txt", "yes")?
wasmtime run --dir=.::. scua.wasm --allow-fs notes.scua

To give the module a different folder, name it on the host side of --dir: --dir=/path/to/project::. runs with /path/to/project as the folder that the script and its files come from. Paths in fs work as they do natively: they are relative to that folder, and .. cannot leave it. Under wasmtime, WebAssembly adds a second wall: the module has no way to name a file outside the folder it was given, whatever SCUA's own checks do.

A program's per-user folder is a second folder that you hand over the same way. Running under a WASI host shows the flags.

Environment variables need both sides too. wasmtime passes one with --env NAME=value, and the script reads it with --allow-env:

wasmtime run --dir=.::. --env GREETING=hi scua.wasm --allow-env greet.scua

#Run it with Node

Node 20 and later include a WASI host, node:wasi. Save this as run-scua.mjs:

// run-scua.mjs: node run-scua.mjs scua.wasm [scua options] script.scua [args...]
import { readFile } from "node:fs/promises";
import { WASI } from "node:wasi";

const [wasmPath, ...args] = process.argv.slice(2);
const wasi = new WASI({
  version: "preview1",
  args: ["scua", ...args],
  preopens: { ".": process.cwd() }, // the module's "." is the folder you run this in
  returnOnExit: true,
});
const module = await WebAssembly.compile(await readFile(wasmPath));
const instance = await WebAssembly.instantiate(module, wasi.getImportObject());
process.exitCode = wasi.start(instance);
node --no-warnings run-scua.mjs scua.wasm --allow-fs notes.scua

--no-warnings hides Node's notice that WASI is experimental. To pass environment variables, add env: { GREETING: "hi" } to the options.

Node's WASI is not a sandbox. Node's documentation says so: it does not have the file system protections that wasmtime has. SCUA still refuses .. and absolute paths, but a symbolic link inside the folder can lead out of it under Node. Use Node to run your own scripts, and use wasmtime for a script you do not trust.

#What the WebAssembly build leaves out

WebAssembly has no sockets, no processes and no threads, and SCUA does not generate machine code there. Each missing feature stops with an error that you can see. Nothing fails silently.

  • No JIT. Every script runs in the interpreter. --jit is accepted, but nothing compiles, and --jit-stats reports compiled=0. Code that the JIT speeds up natively, such as a hot loop, runs at interpreter speed.
  • One thread. Partitions, actors, spawn, ask, tell and timers all work, but they take turns on a single thread. --workers=N adds no speed, and scua warns when you set it above 1.
  • No network. import http and import net load with --allow-net, but every connection fails: http.get and http.post return Error({ kind = client, ... }), and net.dial returns Error("could not connect"). http.serve returns an Error that says it is not available, scua serve cannot listen, and --io=async is refused. --allow-udp stops the run with exit code 2. A cluster table works within the one process, but cluster.join fails, because a cluster needs sockets to reach other nodes.
  • No commands. exec.run, exec.spawn and the rest return Error({ kind = unsupported, ... }).
  • sys.readline holds the program while it waits. With one thread, the read waits in place, so spawned tasks, timers and partitions pick up again once the line arrives or input ends.
  • No file locks. fs.lock returns Error({ kind = unsupported, ... }). Because the per-user folder cannot be locked either, scua warns when it opens that folder.
  • No platform per-user folder. There is no home folder to find one in, so --allow-user alone stops with E-USER-NOBASE. Hand the folder over with --user-dir and a second --dir.
  • No ffi. Native libraries cannot be loaded into WebAssembly. The native scua command cannot load them either, because only an embedding host can grant ffi.
  • Less memory for crypto.argon2id. WebAssembly addresses at most 4 GiB, so max_memory_kib can be raised to 2 GiB (2,097,152) at most, where the native build allows 4 TiB. The defaults, and every hash they accept, are the same.

Everything else is the same as the native build: the language, the standard library, fs inside the granted folder, sandbox, sys.readline and sys.stdin, scua test, and the clock and timers.