Appearance
Limits
Weft code runs inside limits that every backend enforces the same way. A screen can't freeze because a loop never ends, and user code can't take a server down.
Fuel
weft
export fn spin(n: number): number {
let mut steps = 0
for i of 0..n {
steps += 1
}
return steps
}spin(1000) spends 1,001 units: one for the call and one per iteration. spin(1e9) runs out of fuel.
Every call and every loop iteration spends one unit of fuel. A turn (one handler, timer callback or effect run) starts with 50,000,000 units by default, and the host may set another budget. Drawing an instance, meaning its derived values and markup expressions, has its own budget of 1,000,000 per update.
Fuel is counted the same way on every backend, so a program runs out at the same point in the browser and on the server. await spends nothing.
Scripts
A script, weft run of a program, runs without limits: no fuel, memory or deadline per turn, and up to 100,000 active calls, so a data migration or a report runs as long as it needs. WEFT_FUEL and WEFT_MEMORY_MB (in MB) set a limit, and 0 means none. Once a program starts serving, each request's turns get a server's budget: the defaults on this page and a 250 ms deadline (WEFT_DEADLINE_MS).
The deadline
A turn on the UI may also use at most 250 ms of engine time. Hosts can change this.
The stack
At most 4,000 Weft calls can be active at once. The call that would be the 4,001st stops the turn instead. A browser's own stack holds fewer than 10,000 small frames, so 4,000 is what every backend can promise. A server host may allow more.
Memory
A turn may allocate 32 MB by default, counted by a logical size that every backend computes the same way: 16 bytes per allocation, plus 2 per string code unit, 8 per list item, record field or set item, and 16 per map entry. s += t costs what it adds, so building a string in a loop costs what the string ends up holding. Strings and lists hold at most 2^28 code units or items; going past that panics with a RangeError.
Stops
main.weft
weft
import { memory, run } from "std/sandbox"
import type { RunError } from "std/sandbox"
import { log } from "std/log"
let trees = memory()
try await trees.save("loop", { files: [{ id: "main", source: "export fn main(caps: json, input: json): json {\n while true {}\n return null\n}\n" }] })
let result = try await run(trees.tree("loop"), { limits: { fuel: 100000 } }) else e => match e {
Stopped { limit } => `stopped: out of ${limit}`,
_ => "it failed",
}
log(result)text
$ weft run main.weft
stopped: out of fuelA stop inside a turn of your own program can't be handled, but user code run through std/sandbox is a turn of its own: the app's run fails with the RunError Stopped { limit, message }, and the app carries on.
Running out of fuel, memory or stack, passing the deadline, and effects that keep triggering each other for 100 rounds are stops, not errors:
- nothing handles them:
@trydoesn't see them, anddefers don't run; - every piece of state the turn wrote goes back to what was last drawn;
- the instance that owned the turn, and the ones below it, stay on screen as they were and run nothing more.
A stop is reported with what caused it. Panics (TypeError, RangeError) are different: a turn that panics keeps the writes it made before the panic, as in JavaScript, and @try in markup can show something in its place. Errors are different again: they are values the code handles (Errors).
The exact rules are in the reference: Limits.