garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/javascript-prim-scripts.md

raw


topic: javascript-prim-scripts triggers: [scripting/bun.rs, scripting/rules.rs, bun_tests.rs, js_tests.rs, tools/script-host, script-host.sh, cartopolis-script-host, COMPILED_HOST, script_host, script_host_dir, realm-runtime.js, rewrite.ts, ShadowRealm, "bun:jsc", heapSize, BunEngine] updated: 2026-11-16

Prim scripts in JavaScript, on Bun

Prim scripts run as JavaScript in a Bun process the simulator supervises (scripting::bun, tools/script-host) — the only engine since 2026-11-16, when Luau (prim-scripting.md) was removed. The question that note put to any replacement — can an over-budget script be suspended, or only killed? — is answered here by rewriting the script, not by the engine.

How the Luau guarantees are rebuilt

JavaScriptCore can stop a script, never suspend one. So rewrite.ts turns every author function into a generator, every call into yield* (inline, see below), and puts a fuel check at every loop iteration and function entry — the two places Luau's interrupt fires, so WORLD_FUEL_PER_TICK and the slice carry over as numbers. A callback a built-in calls (arr.map(f)) cannot be paused: it runs to its end and an overrun there faults, the same edge Luau has at C calls.

Memory has no allocator ceiling in one shared heap, so it is four layers:

  1. the fuel check reads resident memory every 1024 steps and stops a slice that grew it past sliceHeap;
  2. built-ins whose output size is an argument (repeat, padEnd, Array(n), fill, Array.from, …) refuse past maxLength;
  3. a heap-snapshot audit attributes the heap to each script and stops one over scriptRetained — seconds of pause, so only when the process grows;
  4. the supervisor (Rust) kills the process past script_memory_limit_mib or a tick past script_tick_timeout_ms, blames the prim that was running (a file both processes map) and reloads the rest — which loses their state.

One realm, frozen — and why not one realm each

All scripts share one ShadowRealm; every intrinsic is frozen before any script runs, the legacy RegExp.$1… statics and Symbol.for are deleted, and each script's state is a closure over its own instance (the SES compartment model). One realm per script was built first and measured (mac-1, Bun 1.4.4, 2026-11-15): the same firebird handler cost 89 µs a run spread over 500 realms and 9 µs in one — JavaScriptCore optimises per realm, so each copy stayed cold — and 2000 realms held 1.6 GB. Shared: 2000 content scripts in ~215 MB of process, an idle script ~3 KB of heap.

The cost of that choice is that isolation between scripts rests on the freeze being complete. tools/script-host/test/sandbox.test.ts ("one realm, scripts apart") tries each known route; a new intrinsic in a Bun release is the thing to re-check. Escaping the realm reaches only the script process, which holds nothing but scripts and starts with an empty environment.

Traps found, each silent

Measured against Luau

mac-1 (Apple M5 Pro VM), Bun 1.4.4 against the simulator's own host, each in its own process, 2026-11-15 (tools/script-host/bench):

BunLuau
2000 firebirds, ms per tick10.811.1
2000 footballs, ms per tick3.23.3
2000 content mix, ms per tick35.831.4
fuel bank of loops, ms0.462.6
per fib(15), ms~0.18~0.055
load 2000 scripts, ms20–32165–337

The content mix at 2000 is a message storm — the packs assume one firebird, and firebirds read each other's replies as a toggle — so it measures delivery.

Where the process comes from

deploy/build/script-host.sh compiles src/child.ts with bun build --compile --target=bun-linux-x64 into one executable, cartopolis-script-host (87 MB, Bun 1.4.4 inside; realm-runtime.js is imported as text so it travels in it). The Bun inside is Bun's release build, never the node's: Arch's asks for GLIBC_2.44, the deploy host has 2.43, and a host built with it could not start there (2026-11-17), so the script checks the ceiling as server.sh does. The simulator looks for it beside its own binary (COMPILED_HOST, SimConfig::script_host), so a server needs neither Bun nor node_modules: the server download carries it next to cartopolis-server and its updater swaps and rolls it back with the program; the hosted sim's deploy publishes it next to simulator. script_host names another; script_host_dir (with script_bun) runs a checkout's source; with neither and nothing beside the binary, a cargo run runs this checkout's under the bun on the path, installing its packages on first start. The script tests need that Bun and fail without it, since a pass by skipping would vouch for nothing; the gate also runs bun test here.

The same executable is the content pages' renderer, started as render <bundle> (src/render.ts, pages::render): a second process, never this one, because what it writes is the site (docs/mockups/cms, v3). A change to child.ts's arguments is a change to both.

A Luau script stored before the switch does not compile as JavaScript; the hosted world had none left when Luau went (2026-11-16, 64 scripts, all JS).

What is not done