garage44.eu/jeroen/frits

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

.. /docs/design.md

raw

Frits: design, and what is built

Frits is the scripting language of Pinnacle, meant to take over from TypeScript/Bun for apps, scripts and the shell, and from Bun for prim scripts in the Cartopolis simulator. This file holds the decisions and the state of the code; the reasoning behind each choice is in the 2026-11-26 brainstorm (session s851).

The four choices everything else follows from

  1. Every script is a process with its own heap. Processes share no mutable memory; a message is copied into the receiver. A memory ceiling is a number per heap, a collection is one process's business, and stopping a script is dropping its heap.
  2. Every script can be paused anywhere. The VM counts fuel per instruction. A process's whole execution state (registers, frames, program counter) is data in vm::Process, never the Rust stack, so there is no context in which a pause cannot happen. Luau's trap, a yield silently ignored outside a coroutine (Cartopolis docs/notes/prim-scripting.md), cannot occur.
  3. The outside world is reached through effects, and an effect is a right. A script names what it needs (script / Prim, Chat); a function names what it does (fn fly(t: Float) / Prim). The compiler refuses a call whose effect the caller lacks. The host still enforces the rights it hands out; the compiler is not a security boundary.
  4. Nothing is ambient. No global fetch, process or file-reading require: a platform declares every function a script can call.

The language as built

script / Prim, Chat, Timer

enum State { Patrol, Summoned, Hit(Int, Str) }

let CENTRE = { lat: 53.2194, lng: 6.5665 }      // records are structural
var state = Patrol
var restart_in: Option<Float> = None

fn fly(t: Float) / Prim {
    let a = t / 48 * 2 * math.pi                 // an Int literal beside a Float is a Float
    Prim.set_geo({ lat: CENTRE.lat + math.cos(a) / 1000, lng: CENTRE.lng })
}

on timer() {
    match state {                                // match must cover every variant
        Patrol => fly(Timer.now()),
        Summoned => {}
        Hit(n, who) => Chat.say("{who} hit it {n} times, {Timer.now():.1} s in"),
    }
    if let Some(t) = restart_in { restart_in = Some(t - 0.1) }
}

Libraries, messages and rights

Views

A script describes a user interface with markup, and the host draws it with its own components (on Pinnacle, the component library):

script / Ui

enum Msg { Inc, Dec }
var n = 0

fn draw() -> View<Msg> {
    <Column gap=8>
        <Text size=1.5>"Count: {n}"</Text>
        <Row>
            <Button on_press=Dec>"-"</Button>
            <Button on_press=Inc>"+"</Button>
        </Row>
    </Column>
}

on receive(m: Msg) {
    match m {
        Inc => n += 1,
        Dec => n -= 1,
    }
    render(draw())
}

Compiled ahead of time

frits aot [--platform <decl.frits>] <file> writes a script as Rust source: one native function per script function, each basic block straight Rust with its constants written in, on the same registers, heap and collector as the interpreter. A host builds it into its binary and attaches it with Process::set_natives(natives(), FINGERPRINT); natives generated from other code (another script, another compiler) are refused and the interpreter runs. crates/aot-check generates natives at build time and checks them against the interpreter.

Native code runs to completion: fuel does not count it and it cannot be paused, so a host call that suspends (Timer.wait) stops it with an error. It is for apps the device's owner installs, on a system that preempts threads (Pinnacle); uploaded scripts, such as the Cartopolis simulator's, stay interpreted.

Measured on vps-1 (release, 2026-11-26), crates/aot-check/scripts/work.frits (recursion, a 200,000-step loop allocating enum values, closures, maps, Result): interpreted 67.5 ms, compiled 32.8 ms, 2.1×. Allocation and collection are untouched by compilation and likely most of what is left.

Tools

The platform

A host describes what it offers in Frits, and binds Rust functions to it:

let mut p = Platform::<Prim>::new("
    type Geo = { lat: Float, lng: Float }
    effect Prim { fn geo() -> Geo  fn set_geo(g: Geo)  let fire: Particles }
    module geo { fn metres_per_degree(lat: Float) -> Geo }
    event timer()
")?;
p.bind("Prim.set_geo", |cx, a| { let f = cx.fields(a[0]); /* … */ Ok(Value::Unit) });

effect namespaces need the effect, module namespaces are pure, events are what on handlers answer. Vec3, math and the Random effect are in every platform (prelude.rs). The same script runs on another host by being compiled against its platform.

A host function can do three things besides answering:

Without std, and the same numbers everywhere

The std feature (on by default) is for the command line and the language server; the language needs only alloc, as a Pinnacle app has it (cargo check -p frits --lib --no-default-features --target aarch64-unknown-none). Its maps are B-trees, so what a script lists comes out in one order on every machine.

math is fmath.rs, written here, in every build: a no_std build has no sin, and taking the platform's would let one script compute different last bits on a server, in a browser (wasm) and on a device — and a view drawn from a number must be the same on all three for a browser to take a server's page over. fdlibm's algorithms; held to std's within 0 ulps (sqrt), 1 (exp, ln, atan), 2 (sin, cos, atan2, hypot); whole powers by squaring, so pow(2, 10) is 1024. Angles beyond a million radians are reduced modulo 2π as a double: the same everywhere, but not std's answer.

The interpreter builds for wasm32 unchanged (2026-11-27, Mithril's examples/frits-web: 929 KB of wasm with the compiler, 234 KB gzipped).

The runtime

Measured

mac-1 (Apple M5 Pro VM), 2026-11-26, cargo run --release -p frits-bench: the same scenarios as Cartopolis's Luau/Bun comparison (s693, docs/notes/javascript-prim-scripts.md there, also mac-1). Each scenario runs 200 ticks at 20 Hz, with a touch on prim 1 every 20 ticks.

FritsBun 1.4.4Luau
2000 firebirds, ms per tick0.9–1.110.811.1
2000 footballs, ms per tick0.46–0.693.23.3
fuel bank (10 × 20,000 instructions), ms0.320.462.6
per fib(15), ms0.037~0.18~0.055
load 2000 firebirds, ms520–32165–337
memory per firebird, runtime's count3.5 KB——
memory per firebird, resident12 KB3–33 KB160–435 KB
memory per idle script, resident0.7 KB~480 KB~440–510 KB

Read the content rows with care. The stand-in host here does less than the simulator's: no per-tick prim views sent across a thread, no command channel. The VM rows (fuel bank, fib) compare the engines alone; the content rows compare an engine plus a lighter host against an engine plus the real one.

The load and memory rows were measured again after compiled modules came to be shared per source (mac-2, under other sessions' load): loading 2000 firebirds went from ~220 ms to 5 ms, and resident memory per firebird from 22 KB to 12 KB. The same run, old build against new on that one node, put the busy loop at 2.4–2.55 ns per instruction before part A of the language extension and 2.9 ns after (calls and fib unchanged): about 18% slower dispatch, not yet looked into.

Not built yet

In the order they are likely to matter:

Known gaps in what is built: