Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

96.7 KB
Raw Blame
//! The scene runtime of a render node. A game bundle (`tools/bundle-game.ts`: game code + the ThreeRT runtime + the
//! node host) runs in an embedded QuickJS engine in the renderer process, and drives the engine directly: the native
//! functions installed as `globalThis.__engine` create and edit the engine's world (world.rs) as the scene code's
//! binding (packages/scene/src/engine.ts) calls them. No scene document crosses between the two. `LiveCore::commit`
//! then applies what changed (GPU tables in place, or a rebuild).
//!
//! Sandbox: the engine has no I/O at all (no fs, network, timers or module loader); the host adds only a log sink, a
//! monotonic clock, the engine API and the host channel's queue (`__host_post`: messages for the app that embeds the
//! session, which may answer with `host_messages`; docs/RUNTIME.md "The host channel"). Memory is capped and every
//! call runs under a time budget (the interrupt handler aborts it).
//!
//! Promises: the engine has no event loop of its own, so every entry into the script (`ScriptHost::enter`) ends by
//! running the promise jobs it left (a `.then` callback, the code after an `await`), within the entry's budget, as a
//! browser does when a script returns. docs/RUNTIME.md "Promises".

use crate::world::World;
use anyhow::{anyhow, Result};
use rquickjs::{CatchResultExt, Coerced, Context, Ctx, Exception, Function, Module, Object, Persistent, Runtime, TypedArray, Value};
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Instant;

/// Web APIs the runtime and games use that QuickJS lacks (all pure).
const PRELUDE: &str = r#"
globalThis.console = {
  log: (...a) => __host_log("info", a.map(String).join(" ")),
  info: (...a) => __host_log("info", a.map(String).join(" ")),
  warn: (...a) => __host_log("warn", a.map(String).join(" ")),
  error: (...a) => __host_log("error", a.map(String).join(" ")),
  debug: () => {},
};
globalThis.performance = { now: () => __host_now() };
globalThis.structuredClone = (v) => (v === undefined ? undefined : JSON.parse(JSON.stringify(v)));
globalThis.TextEncoder = class TextEncoder {
  get encoding() { return "utf-8"; }
  encode(s = "") {
    s = String(s);
    const out = [];
    for (let i = 0; i < s.length; i++) {
      let c = s.charCodeAt(i);
      if (c >= 0xd800 && c < 0xdc00 && i + 1 < s.length) {
        const d = s.charCodeAt(i + 1);
        if (d >= 0xdc00 && d < 0xe000) { c = 0x10000 + ((c - 0xd800) << 10) + (d - 0xdc00); i++; }
      }
      if (c < 0x80) out.push(c);
      else if (c < 0x800) out.push(0xc0 | (c >> 6), 0x80 | (c & 63));
      else if (c < 0x10000) out.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 63), 0x80 | (c & 63));
      else out.push(0xf0 | (c >> 18), 0x80 | ((c >> 12) & 63), 0x80 | ((c >> 6) & 63), 0x80 | (c & 63));
    }
    return new Uint8Array(out);
  }
};
globalThis.atob = (s) => {
  const A = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  s = String(s).replace(/[\s=]/g, "");
  let out = "", buf = 0, bits = 0;
  for (let i = 0; i < s.length; i++) {
    const v = A.indexOf(s[i]);
    if (v < 0) throw new Error("atob: invalid character");
    buf = (buf << 6) | v; bits += 6;
    if (bits >= 8) { bits -= 8; out += String.fromCharCode((buf >> bits) & 255); }
  }
  return out;
};
"#;

/// Where the game's `console.*` lines go besides stderr (the client's log panel and dev server): `set_log_sink`.
static LOG_SINK: std::sync::RwLock<Option<fn(&str, &str)>> = std::sync::RwLock::new(None);

/// Also hands every game log line (`level`, `text`) to `sink` (one per process; None removes it).
pub fn set_log_sink(sink: Option<fn(&str, &str)>) {
    if let Ok(mut s) = LOG_SINK.write() {
        *s = sink;
    }
}

/// The game code a session runs: a one-script bundle (tools/bundle-game.ts), or a game module on the runtime module
/// (`ScriptHost::from_modules`, the cloud's layout).
pub enum GameCode<'a> {
    Bundle(&'a str),
    Modules { game: &'a str, runtime: &'a str },
}

/// Time budgets per entry point (ms): building a scene may take a while, events and frames may not.
const SETUP_MS: u64 = 120_000;
const EVENT_MS: u64 = 1_000;
const FRAME_MS: u64 = 50;

/// The host channel's queue (`__host_post`): the most messages it holds until the app takes them (a session nobody
/// drains, a render node's, keeps the first of them and drops the rest), and the most bytes of one.
const MAX_POSTS: usize = 1024;
const MAX_POST_BYTES: usize = 1 << 20;
/// A `store.write` carries a file (docs/RUNTIME.md "Files": at most 64 MiB, in base64): the most bytes of one, and of
/// the whole queue.
const MAX_STORE_WRITE_BYTES: usize = 96 << 20;
const MAX_QUEUE_BYTES: usize = 192 << 20;

/// The asset root scene code may read files under (`__engine.readAsset`): set by the host before a game loads.
static ASSET_ROOT: std::sync::RwLock<Option<std::path::PathBuf>> = std::sync::RwLock::new(None);

/// Where `__engine.readAsset` reads (the host's `--assets`).
pub fn set_asset_root(root: &std::path::Path) {
    *ASSET_ROOT.write().unwrap() = Some(root.to_path_buf());
}

/// `__engine.readAsset(path)`: a file under the asset root, whole, as an ArrayBuffer (a streamed city's tiles, baked data
/// a game reads as it goes). Relative paths only, no `..`: nothing outside the root.
fn read_asset(path: &str, range: Option<(u64, usize)>) -> anyhow::Result<Vec<u8>> {
    use std::io::{Read, Seek, SeekFrom};
    let p = std::path::Path::new(path);
    anyhow::ensure!(p.is_relative() && !p.components().any(|c| matches!(c, std::path::Component::ParentDir)), "readAsset: {path}: a relative path under the asset root");
    let root = ASSET_ROOT.read().unwrap().clone().unwrap_or_else(|| std::path::PathBuf::from("."));
    let full = root.join(p);
    match range {
        None => std::fs::read(&full).map_err(|e| anyhow::anyhow!("readAsset: {path}: {e}")),
        // (a part of it: a streamed cell's next meshes, without reading the rest)
        Some((offset, length)) => {
            let mut f = std::fs::File::open(&full).map_err(|e| anyhow::anyhow!("readAsset: {path}: {e}"))?;
            f.seek(SeekFrom::Start(offset))?;
            let mut buf = vec![0u8; length];
            f.read_exact(&mut buf).map_err(|e| anyhow::anyhow!("readAsset: {path} [{offset}, +{length}]: {e}"))?;
            Ok(buf)
        }
    }
}

/// `__host_base64_encode(bytes: Uint8Array): string`.
fn base64_encode<'js>(ctx: Ctx<'js>, bytes: Value<'js>) -> rquickjs::Result<String> {
    use base64::Engine;
    let a = TypedArray::<u8>::from_value(bytes).ok();
    let b = a.as_ref().and_then(|a| a.as_bytes()).ok_or_else(|| Exception::throw_type(&ctx, "__host_base64_encode: a Uint8Array"))?;
    Ok(base64::engine::general_purpose::STANDARD.encode(b))
}

/// `__host_base64_decode(text: string): Uint8Array`.
fn base64_decode<'js>(ctx: Ctx<'js>, text: String) -> rquickjs::Result<TypedArray<'js, u8>> {
    use base64::Engine;
    let bytes = base64::engine::general_purpose::STANDARD.decode(text.as_bytes()).map_err(|e| Exception::throw_type(&ctx, &format!("__host_base64_decode: {e}")))?;
    TypedArray::new(ctx, bytes)
}

/// The type of a message's JSON text, read without building the rest of it.
fn post_type(text: &str) -> Option<String> {
    #[derive(serde::Deserialize)]
    struct Typed {
        t: String,
    }
    serde_json::from_str::<Typed>(text).ok().map(|m| m.t)
}

/// The most engine time one entry into the script (a frame, an event, setup) is lent, µs: past it the time the engine
/// spends in voxel calls counts against the script's budget again. 250 ms: the largest single edit measured on the
/// benchmark level, the 30 x 30 m cut, takes 115 ms of engine time, so two of those fit; a frame that spends more in
/// the engine than that is a hitch of a quarter of a second whatever the script does, and a script that keeps asking
/// is then ended at its budget like one that spins.
const LEND_US: u64 = 250_000;

/// The script's clock for one entry: its deadline in µs since the host's epoch (0: none), checked by the interrupt
/// handler, and the engine time lent to it so far.
struct Budget {
    epoch: Instant,
    deadline: AtomicU64,
    lent: AtomicU64,
}

impl Budget {
    /// Past the deadline (with what was lent): the script's time is up.
    fn up(&self) -> bool {
        let d = self.deadline.load(Ordering::Relaxed);
        d != 0 && self.epoch.elapsed().as_micros() as u64 > d
    }
}

/// The engine's time inside a voxel call is not the script's: when the call returns (or throws), the deadline moves
/// by the time it took, as it was, up to `LEND_US` in all for the entry. (A cut through a hundred voxel bodies takes
/// 50 ms and more; without this the script that asked for it would be ended at its next statement, the rest of its
/// frame lost.) A script that spins, or loops on cheap calls, is ended as before.
///
/// A voxel call made when the time is already up throws "interrupted" at once: the script engine looks at its
/// deadline only every so many of its own operations, and a loop whose every turn is a call of some milliseconds
/// makes few of those (it ran for seconds past its budget before).
struct Lent<'a>(&'a Budget, Instant);

impl<'a> Lent<'a> {
    fn new(ctx: &Ctx<'_>, budget: &'a Budget) -> rquickjs::Result<Self> {
        if budget.up() {
            return Err(fail(ctx, anyhow!("interrupted (the script's time is up)")));
        }
        Ok(Lent(budget, Instant::now()))
    }
}

impl Drop for Lent<'_> {
    fn drop(&mut self) {
        let took = self.1.elapsed().as_micros() as u64;
        let give = took.min(LEND_US.saturating_sub(self.0.lent.load(Ordering::Relaxed)));
        // (0: no deadline is set, and none is made.)
        if give > 0 && self.0.deadline.try_update(Ordering::Relaxed, Ordering::Relaxed, |d| (d != 0).then_some(d + give)).is_ok() {
            self.0.lent.fetch_add(give, Ordering::Relaxed);
        }
    }
}

/// The promises rejected with no handler in the entry running now, each with its reason: one that gets a handler
/// before the entry's jobs have all run is taken off again, and what is left then is reported (`ScriptHost::enter`).
/// They are kept alive here, and none outlives its entry. Past `MAX_TRACKED` of them at once (a script that rejects
/// without end) the rest are only counted.
#[derive(Default)]
struct Rejections {
    list: Vec<(Persistent<Value<'static>>, Persistent<Value<'static>>)>,
    more: usize,
}
type Rejected = Rc<RefCell<Rejections>>;

/// The most unhandled rejections one entry reports by their reasons (the rest are counted), and the most it tracks.
const MAX_REJECTIONS: usize = 8;
const MAX_TRACKED: usize = 4096;

/// A line of the engine's about the game (a scene that does not compile as written): to the game's log, as `console.*`.
pub fn engine_log(level: &str, msg: &str) {
    game_log(level, msg);
}

/// A game log line (`console.*`, an unhandled rejection): stderr, and the sink if there is one.
fn game_log(level: &str, msg: &str) {
    eprintln!("[game{}] {msg}", if level == "info" { String::new() } else { format!(" {level}") });
    if let Some(sink) = LOG_SINK.read().ok().and_then(|s| *s) {
        sink(level, msg);
    }
}

/// A rejection's reason as one line: what `String(reason)` says (an error's name and message), and where an error
/// was thrown (the first line of its stack).
fn reason_text<'js>(ctx: &Ctx<'js>, reason: &Value<'js>) -> String {
    let Ok(Coerced(text)) = reason.get::<Coerced<String>>() else {
        // (Its `toString` threw, or the entry's time was up.)
        ctx.catch();
        return "a value that cannot be shown".into();
    };
    let stack = reason.as_object().and_then(|o| o.get::<_, Option<String>>("stack").unwrap_or_else(|_| {
        ctx.catch();
        None
    }));
    match stack.as_deref().and_then(|s| s.lines().map(str::trim).find(|l| !l.is_empty() && *l != text)) {
        Some(at) => format!("{text} ({at})"),
        None => text,
    }
}

pub struct ScriptHost {
    rt: Runtime,
    ctx: Context,
    /// The deadline of the entry running now and the engine time lent to it (`Budget`).
    budget: Arc<Budget>,
    epoch: Instant,
    /// `describe()`: handlers, keys, whether the game has a frame script.
    pub info: serde_json::Value,
    /// The engine's world the scene code edits through `__engine`.
    pub world: Rc<RefCell<World>>,
    /// The voxel engine's objects (`__engine.voxelLevel` / `voxelExplode`; renderer/src/voxel.rs).
    pub voxels: Rc<RefCell<crate::voxel::VoxelWorld>>,
    /// The physics world (`__engine.physics`; renderer/src/physics.rs): None until scene code makes a physics call.
    pub physics: Rc<RefCell<Option<crate::physics::PhysicsWorld>>>,
    rendering: crate::live::RenderingControl,
    /// What the game posted for the app (`host.post`: JSON texts), until `take_posts`.
    posts: Rc<RefCell<Vec<String>>>,
    /// The promises rejected with no handler in the entry running now (`Rejected`); empty between entries.
    rejected: Rejected,
}

impl Drop for ScriptHost {
    fn drop(&mut self) {
        // (Values of the script engine must be gone before its runtime is.)
        self.rejected.borrow_mut().list.clear();
    }
}

pub struct EventOut {
    pub log: Vec<String>,
    pub seq: i64,
    pub ms: f64,
}

fn host<'js>(ctx: &Ctx<'js>) -> rquickjs::Result<Object<'js>> {
    ctx.globals().get("__threert")
}

/// A JS value as JSON (descriptor objects of the engine API).
fn to_json(v: &Value) -> rquickjs::Result<serde_json::Value> {
    use rquickjs::Type;
    Ok(match v.type_of() {
        Type::Bool => serde_json::Value::Bool(v.as_bool().unwrap_or(false)),
        Type::Int => serde_json::json!(v.as_int().unwrap_or(0)),
        Type::Float => serde_json::Number::from_f64(v.as_float().unwrap_or(0.0)).map_or(serde_json::Value::Null, serde_json::Value::Number),
        Type::String => serde_json::Value::String(v.as_string().map(|s| s.to_string()).transpose()?.unwrap_or_default()),
        Type::Array => {
            let a = v.as_array().expect("array");
            serde_json::Value::Array(a.iter::<Value>().map(|x| x.and_then(|x| to_json(&x))).collect::<rquickjs::Result<_>>()?)
        }
        Type::Object => {
            let o = v.as_object().expect("object");
            let mut m = serde_json::Map::new();
            for k in o.keys::<String>() {
                let k = k?;
                let x: Value = o.get(&k)?;
                if !x.is_undefined() {
                    m.insert(k, to_json(&x)?);
                }
            }
            serde_json::Value::Object(m)
        }
        _ => serde_json::Value::Null,
    })
}

fn parse<T: serde::de::DeserializeOwned>(ctx: &Ctx, what: &str, v: &Value) -> rquickjs::Result<T> {
    serde_json::from_value(to_json(v)?).map_err(|e| Exception::throw_type(ctx, &format!("engine.{what}: {e}")))
}

fn fail(ctx: &Ctx, e: anyhow::Error) -> rquickjs::Error {
    Exception::throw_message(ctx, &format!("{e:#}"))
}

/// A failed engine call as the exception scene code catches (`api::Error`).
fn thrown(ctx: &Ctx, e: crate::api::Error) -> rquickjs::Error {
    match e {
        crate::api::Error::Type(m) => Exception::throw_type(ctx, &m),
        crate::api::Error::Other(e) => fail(ctx, e),
    }
}

/// The engine API (`globalThis.__engine`), see packages/scene/src/engine.ts: each call's arguments out of QuickJS,
/// then what the call does (api.rs: the same code a browser's exports run, renderer/web).
fn install_engine<'js>(ctx: &Ctx<'js>, world: &Rc<RefCell<World>>, voxels: &Rc<RefCell<crate::voxel::VoxelWorld>>, physics: &Rc<RefCell<Option<crate::physics::PhysicsWorld>>>, budget: &Arc<Budget>, rendering: &crate::live::RenderingControl) -> rquickjs::Result<()> {
    use crate::api;
    let e = Object::new(ctx.clone())?;
    // Rendering settings (live/rendering.rs): a request as JSON, applied from the next frame; the last frame's status.
    let control = rendering.clone();
    e.set("setRendering", Function::new(ctx.clone(), move |ctx: Ctx, json: String| -> rquickjs::Result<()> {
        let r = serde_json::from_str(&json).map_err(anyhow::Error::from).and_then(|v| crate::live::RenderingRequest::from_json(&v))
            .map_err(|e| Exception::throw_type(&ctx, &format!("{e:#}")))?;
        control.request(r);
        Ok(())
    })?)?;
    let control = rendering.clone();
    e.set("renderingStatus", Function::new(ctx.clone(), move || control.status().to_string())?)?;
    // Physics (renderer/src/physics.rs): one call, `physics(op, args)`, answering in JSON. The world is made by the
    // first call; the engine steps it, scene code cannot.
    let (p, w) = (physics.clone(), world.clone());
    e.set("physics", Function::new(ctx.clone(), move |ctx: Ctx<'js>, op: String, args: Value<'js>| -> rquickjs::Result<String> {
        let a: serde_json::Value = if args.is_undefined() || args.is_null() { serde_json::Value::Null } else { parse(&ctx, "physics", &args)? };
        api::physics(&mut p.borrow_mut(), &mut w.borrow_mut(), &op, &a).map_err(|e| thrown(&ctx, e))
    })?)?;
    // The poses of the objects that bodies drive, by the slot the binding gave each body (7 floats: position, rotation
    // xyzw, as scene code reads an object's): one copy for all a frame reads. Empty without a world.
    let p = physics.clone();
    e.set("physicsPoses", Function::new(ctx.clone(), move |ctx: Ctx<'js>| -> rquickjs::Result<TypedArray<'js, f32>> { TypedArray::new(ctx, api::physics_poses(&p.borrow()).to_vec()) })?)?;
    // The voxel engine's level and explosions (renderer/src/voxel.rs): the level is loaded natively once the session
    // knows the asset root; an explosion returns what it did as JSON.
    let v = voxels.clone();
    e.set("voxelLevel", Function::new(ctx.clone(), move |ctx: Ctx, d: Value| -> rquickjs::Result<()> { let json = to_json(&d)?; api::voxel_level(&mut v.borrow_mut(), &json).map_err(|e| thrown(&ctx, e)) })?)?;
    let (v, w, p, d) = (voxels.clone(), world.clone(), physics.clone(), budget.clone());
    e.set("voxelExplode", Function::new(ctx.clone(), move |ctx: Ctx, x: f64, y: f64, z: f64, r: f64, push: rquickjs::function::Opt<f64>| -> rquickjs::Result<String> { let _lent = Lent::new(&ctx, &d)?; api::voxel_explode(&mut v.borrow_mut(), &mut w.borrow_mut(), &mut p.borrow_mut(), x, y, z, r, push.0).map_err(|e| thrown(&ctx, e)) })?)?;
    // An edit of the level (`voxelEdit({ op, sphere | box | capsule, material })`; voxel.rs `EditOp`), and the level's
    // hash (16 hex digits; "" before a level): the same commands give the same hash.
    let (v, w, p, dl) = (voxels.clone(), world.clone(), physics.clone(), budget.clone());
    e.set("voxelEdit", Function::new(ctx.clone(), move |ctx: Ctx<'js>, d: Value<'js>| -> rquickjs::Result<String> { let json = to_json(&d)?; let _lent = Lent::new(&ctx, &dl)?; api::voxel_edit(&mut v.borrow_mut(), &mut w.borrow_mut(), &mut p.borrow_mut(), &json).map_err(|e| thrown(&ctx, e)) })?)?;
    // The voxel bodies (ROADMAP § 6 step 8), as JSON: [{ body, collider, voxels, asleep, position, rotation }]. `body`
    // is the physics world's id: `physics("impulse", { body, ... })` and the other calls take it.
    let (v, p) = (voxels.clone(), physics.clone());
    e.set("voxelBodies", Function::new(ctx.clone(), move || -> String { api::voxel_bodies(&v.borrow(), &p.borrow()) })?)?;
    // One voxel body's edit (its object's `add` / `paint` / `carve`), its contacts on or off, the bodies gone since
    // the last call (step 10: voxel bodies as objects).
    let (v, w, p, d) = (voxels.clone(), world.clone(), physics.clone(), budget.clone());
    e.set("voxelBodyEdit", Function::new(ctx.clone(), move |ctx: Ctx<'js>, body: f64, d2: Value<'js>| -> rquickjs::Result<String> { let json = to_json(&d2)?; let _lent = Lent::new(&ctx, &d)?; api::voxel_body_edit(&mut v.borrow_mut(), &mut w.borrow_mut(), &mut p.borrow_mut(), body, &json).map_err(|e| thrown(&ctx, e)) })?)?;
    let (v, p) = (voxels.clone(), physics.clone());
    e.set("voxelContacts", Function::new(ctx.clone(), move |ctx: Ctx, body: f64, on: bool| -> rquickjs::Result<()> { api::voxel_contacts(&mut v.borrow_mut(), &mut p.borrow_mut(), body, on).map_err(|e| thrown(&ctx, e)) })?)?;
    let v = voxels.clone();
    e.set("voxelGone", Function::new(ctx.clone(), move || -> String { api::voxel_gone(&mut v.borrow_mut()) })?)?;
    // The world's anchor (step 10 c1: a fixed body with no collider; a joint to the level holds to it), -1 before a level
    // that collides.
    let v = voxels.clone();
    e.set("voxelLevelBody", Function::new(ctx.clone(), move || -> f64 { v.borrow().level_body().map_or(-1.0, |b| b as f64) })?)?;
    let v = voxels.clone();
    e.set("voxelHash", Function::new(ctx.clone(), move || -> String { api::voxel_hash(&v.borrow()) })?)?;
    let v = voxels.clone();
    e.set("voxelDebris", Function::new(ctx.clone(), move |ctx: Ctx<'js>| -> rquickjs::Result<TypedArray<'js, f32>> { TypedArray::new(ctx, v.borrow_mut().take_debris()) })?)?;
    let (v, w, d) = (voxels.clone(), world.clone(), budget.clone());
    e.set("voxelFrame", Function::new(ctx.clone(), move |ctx: Ctx, dt: f64| -> rquickjs::Result<String> { let _lent = Lent::new(&ctx, &d)?; api::voxel_frame(&mut v.borrow_mut(), &mut w.borrow_mut(), dt).map_err(|e| thrown(&ctx, e)) })?)?;
    // readAsset(path, offset?, length?): the file, or `length` bytes of it from `offset`
    e.set("readAsset", Function::new(ctx.clone(), move |ctx: Ctx<'js>, path: String, offset: rquickjs::function::Opt<f64>, length: rquickjs::function::Opt<f64>| -> rquickjs::Result<rquickjs::ArrayBuffer<'js>> {
        let range = offset.0.zip(length.0).map(|(o, l)| (o as u64, l as usize));
        let bytes = read_asset(&path, range).map_err(|e| fail(&ctx, e))?;
        rquickjs::ArrayBuffer::new(ctx, bytes)
    })?)?;
    // preloadModel(uri, nodeSets?): the model file parsed on a worker thread into the process's parse cache (its
    // buffers, images and tangents), and the triangles of each node set (JSON: [["a", "b"], ...]; null: the whole
    // model) read, so a still GLTFModel of them added later in place does neither in that frame's commit (a streamed
    // city's far tiles: ~30-90 ms each). False for a path outside the asset root or node sets that are no such list.
    e.set("preloadModel", Function::new(ctx.clone(), move |uri: String, sets: rquickjs::function::Opt<String>| -> bool {
        let p = std::path::Path::new(&uri);
        if !p.is_relative() || p.components().any(|c| matches!(c, std::path::Component::ParentDir)) {
            return false;
        }
        let sets: Vec<Option<Vec<String>>> = match sets.0 {
            Some(j) => match serde_json::from_str(&j) {
                Ok(v) => v,
                Err(_) => return false,
            },
            None => vec![],
        };
        let root = ASSET_ROOT.read().unwrap().clone().unwrap_or_else(|| std::path::PathBuf::from("."));
        rayon::spawn(move || {
            if crate::scene::model_gltf(&uri, &root).is_err() {
                return;
            }
            for nodes in sets {
                let _ = crate::scene::prepare_triangles(&uri, &root, nodes);
            }
        });
        true
    })?)?;
    let w = world.clone();
    // (…, groups?, colors?): the optional arguments as the rest (rquickjs takes seven parameters at most).
    e.set("geometry", Function::new(ctx.clone(), move |ctx: Ctx<'js>, id: u32, p: TypedArray<f32>, n: TypedArray<f32>, uv: TypedArray<f32>, idx: TypedArray<u32>, rest: rquickjs::function::Rest<Value<'js>>| -> rquickjs::Result<()> {
        let opt = |k: usize| rest.0.get(k).filter(|v| !v.is_null() && !v.is_undefined());
        let groups = opt(0).map(|v| TypedArray::<u32>::from_value(v.clone())).transpose()?;
        let colors = opt(1).map(|v| TypedArray::<f32>::from_value(v.clone())).transpose()?;
        api::geometry(&mut w.borrow_mut(), id, p.as_ref(), n.as_ref(), uv.as_ref(), idx.as_ref(), groups.as_ref().map(|g| g.as_ref()), colors.as_ref().map(|c| c.as_ref())).map_err(|e| thrown(&ctx, e))
    })?)?;
    let w = world.clone();
    e.set("texture", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, d: Value, data: Option<TypedArray<u8>>| -> rquickjs::Result<()> {
        let json = to_json(&d)?;
        api::texture(&mut w.borrow_mut(), id, &json, data.as_ref().map(|d| d.as_ref())).map_err(|e| thrown(&ctx, e))
    })?)?;
    let w = world.clone();
    e.set("shader", Function::new(ctx.clone(), move |name: String, code: String, helpers: String| w.borrow_mut().set_shader(&name, code, helpers))?)?;
    let w = world.clone();
    e.set("particleFunction", Function::new(ctx.clone(), move |name: String, code: String, helpers: String| w.borrow_mut().set_particle_fn(&name, code, helpers))?)?;
    let w = world.clone();
    e.set("particleProgram", Function::new(ctx.clone(), move |name: String, emit: String, update: String, helpers: String| w.borrow_mut().set_particle_program(&name, emit, update, helpers))?)?;
    let w = world.clone();
    e.set("particlesSpawn", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, data: TypedArray<f32>| -> rquickjs::Result<()> { w.borrow_mut().spawn_particles(id, data.as_ref()).map_err(|e| fail(&ctx, e)) })?)?;
    // Density grids (ROADMAP § 6 step 5b): a grid program's WGSL, a grid (its size, channels, program and data), and
    // the sources the game gives a volume's grid program.
    let w = world.clone();
    e.set("gridProgram", Function::new(ctx.clone(), move |name: String, code: String, helpers: String| w.borrow_mut().set_grid_program(&name, code, helpers))?)?;
    let w = world.clone();
    e.set("densityGrid", Function::new(ctx.clone(), move |ctx: Ctx<'js>, id: u32, d: Value<'js>, data: Value<'js>| -> rquickjs::Result<()> {
        let j: serde_json::Value = parse(&ctx, "densityGrid", &d)?;
        let data = if data.is_null() || data.is_undefined() { None } else { Some(TypedArray::<f32>::from_value(data)?) };
        api::density_grid(&mut w.borrow_mut(), id, &j, data.as_ref().map(|d| d.as_ref())).map_err(|e| thrown(&ctx, e))
    })?)?;
    let w = world.clone();
    e.set("gridSources", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, data: TypedArray<f32>| -> rquickjs::Result<()> { w.borrow_mut().add_grid_sources(id, data.as_ref()).map_err(|e| fail(&ctx, e)) })?)?;
    let w = world.clone();
    e.set("volumeFunction", Function::new(ctx.clone(), move |name: String, code: String, helpers: String| w.borrow_mut().set_volume_fn(&name, code, helpers))?)?;
    let w = world.clone();
    e.set("geometryFunction", Function::new(ctx.clone(), move |name: String, code: String, helpers: String| w.borrow_mut().set_geometry_fn(&name, code, helpers))?)?;
    let w = world.clone();
    e.set("procedural", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, function: String, boxes: TypedArray<f32>, data: TypedArray<u32>| -> rquickjs::Result<()> {
        w.borrow_mut().set_proc_geometry(id, function, AsRef::<[f32]>::as_ref(&boxes).to_vec(), AsRef::<[u32]>::as_ref(&data).to_vec()).map_err(|e| fail(&ctx, e))
    })?)?;
    let w = world.clone();
    e.set("proceduralAsset", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, function: String, uri: String| -> rquickjs::Result<()> {
        w.borrow_mut().set_proc_asset(id, function, uri).map_err(|e| fail(&ctx, e))
    })?)?;
    let w = world.clone();
    e.set("material", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, d: Value| -> rquickjs::Result<()> { let json = to_json(&d)?; api::material(&mut w.borrow_mut(), id, &json).map_err(|e| thrown(&ctx, e)) })?)?;
    let w = world.clone();
    e.set("object", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, d: Value| -> rquickjs::Result<()> { let json = to_json(&d)?; api::object(&mut w.borrow_mut(), id, &json).map_err(|e| thrown(&ctx, e)) })?)?;
    let w = world.clone();
    e.set("transform", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, t: TypedArray<f32>| -> rquickjs::Result<()> { api::transform(&mut w.borrow_mut(), id, t.as_ref()).map_err(|e| thrown(&ctx, e)) })?)?;
    // An instanced mesh's instances: count drawn, then matrices (16 numbers each), colours (3) and per-instance data
    // (4) for its whole capacity, each null to keep what it had (world.rs `set_instances` checks them).
    let w = world.clone();
    e.set("instances", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, count: u32, m: Option<TypedArray<f32>>, c: Option<TypedArray<f32>>, d: Option<TypedArray<f32>>| -> rquickjs::Result<()> {
        let v = |a: Option<TypedArray<f32>>| a.map(|a| AsRef::<[f32]>::as_ref(&a).to_vec());
        w.borrow_mut().set_instances(id, count, v(m), v(c), v(d)).map_err(|e| fail(&ctx, e))
    })?)?;
    let w = world.clone();
    e.set("visible", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, v: bool| w.borrow_mut().set_visible(id, v).map_err(|e| fail(&ctx, e)))?)?;
    let w = world.clone();
    e.set("power", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, p: f64| w.borrow_mut().set_power(id, p as f32).map_err(|e| fail(&ctx, e)))?)?;
    let w = world.clone();
    e.set("customData", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, v: Vec<f64>| api::custom_data(&mut w.borrow_mut(), id, &v).map_err(|e| thrown(&ctx, e)))?)?;
    let w = world.clone();
    e.set("sway", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, d: Value| -> rquickjs::Result<()> {
        let j = if d.is_null() || d.is_undefined() { None } else { Some(to_json(&d)?) };
        api::sway(&mut w.borrow_mut(), id, j.as_ref()).map_err(|e| thrown(&ctx, e))
    })?)?;
    let w = world.clone();
    e.set("light", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, d: Value| -> rquickjs::Result<()> { let json = to_json(&d)?; api::light(&mut w.borrow_mut(), id, &json).map_err(|e| thrown(&ctx, e)) })?)?;
    let w = world.clone();
    e.set("camera", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, d: Value| -> rquickjs::Result<()> { let json = to_json(&d)?; api::camera(&mut w.borrow_mut(), id, &json).map_err(|e| thrown(&ctx, e)) })?)?;
    let w = world.clone();
    e.set("animation", Function::new(ctx.clone(), move |ctx: Ctx, id: u32, d: Value| -> rquickjs::Result<()> {
        let j = if d.is_null() || d.is_undefined() { None } else { Some(to_json(&d)?) };
        api::animation(&mut w.borrow_mut(), id, j.as_ref()).map_err(|e| thrown(&ctx, e))
    })?)?;
    let w = world.clone();
    let p = physics.clone();
    e.set("remove", Function::new(ctx.clone(), move |id: u32| api::remove(&mut w.borrow_mut(), &mut p.borrow_mut(), id))?)?;
    let w = world.clone();
    e.set("environment", Function::new(ctx.clone(), move |ctx: Ctx, d: Value| -> rquickjs::Result<()> { let json = to_json(&d)?; api::environment(&mut w.borrow_mut(), &json).map_err(|e| thrown(&ctx, e)) })?)?;
    let w = world.clone();
    e.set("settings", Function::new(ctx.clone(), move |d: Value| -> rquickjs::Result<()> {
        let json = to_json(&d)?;
        api::settings(&mut w.borrow_mut(), &json);
        Ok(())
    })?)?;
    ctx.globals().set("__engine", e)
}

/// Entry of a game given as ES modules: the game (or a scene module) on the node host of the runtime module.
const MODULE_ENTRY: &str = r#"
import game from "game.js";
import { defineGame, installNodeHost } from "threert-runtime.js";
installNodeHost(typeof game === "function" ? defineGame({ state: {}, setup: () => game(), handlers: {} }) : game);
"#;

impl ScriptHost {
    /// Loads a game bundle: one script, game + runtime (tools/bundle-game.ts); it installs `globalThis.__threert`.
    pub fn new(bundle: &str) -> Result<ScriptHost> {
        Self::load(|ctx| ctx.eval::<(), _>(bundle))
    }

    /// Loads a game as ES modules: `game.js` (agent code, importing `./threert-runtime.js`) and the runtime module
    /// (out/threert-runtime.js, `node tools/build-runtime.ts`).
    pub fn from_modules(game: &str, runtime: &str) -> Result<ScriptHost> {
        Self::load(|ctx| {
            Module::declare(ctx.clone(), "threert-runtime.js", runtime)?;
            Module::declare(ctx.clone(), "game.js", game)?;
            Module::evaluate(ctx.clone(), "threert-entry.js", MODULE_ENTRY)?.finish::<()>()
        })
    }

    fn load(code: impl for<'js> FnOnce(&Ctx<'js>) -> rquickjs::Result<()>) -> Result<ScriptHost> {
        let rt = Runtime::new()?;
        // 1 GiB, or THREERT_SCRIPT_MB (a game that streams a city keeps its resident tiles' arrays)
        let mb = std::env::var("THREERT_SCRIPT_MB").ok().and_then(|v| v.parse::<usize>().ok()).unwrap_or(1024);
        rt.set_memory_limit(mb << 20);
        // QuickJS collects cycles first at 256 KB of heap, then whenever the heap is half as large again as at the last
        // collection; a collection walks every object (a streamed city: ~0.5 M of them, ~45 ms). The scene package
        // makes no cycles of its own (its objects are freed as they go), so the first collection waits for 256 MB, or
        // THREERT_SCRIPT_GC_MB; after it, QuickJS's own rule.
        let gc_mb = std::env::var("THREERT_SCRIPT_GC_MB").ok().and_then(|v| v.parse::<usize>().ok()).unwrap_or(256);
        rt.set_gc_threshold(gc_mb << 20);
        rt.set_max_stack_size(4 << 20);
        let epoch = Instant::now();
        let budget = Arc::new(Budget { epoch, deadline: AtomicU64::new(0), lent: AtomicU64::new(0) });
        let b = budget.clone();
        rt.set_interrupt_handler(Some(Box::new(move || b.up())));
        // A promise rejected with no handler is noted, and taken off again if it gets one (`then` / `catch` / `await`
        // on it later in the entry: `async` code that throws before its caller has the promise is such a case).
        let rejected = Rejected::default();
        let r = rejected.clone();
        rt.set_host_promise_rejection_tracker(Some(Box::new(move |ctx, promise, reason, handled| {
            let promise = Persistent::save(&ctx, promise);
            let mut r = r.borrow_mut();
            if handled {
                r.list.retain(|(p, _)| *p != promise);
            } else if r.list.len() < MAX_TRACKED {
                r.list.push((promise, Persistent::save(&ctx, reason)));
            } else {
                r.more += 1;
            }
        })));
        let ctx = Context::full(&rt)?;
        let world = Rc::new(RefCell::new(World::default()));
        let voxels = Rc::new(RefCell::new(crate::voxel::VoxelWorld::default()));
        let mut h = ScriptHost { rt, ctx, budget, epoch, info: serde_json::Value::Null, world, voxels, physics: Default::default(), rendering: Default::default(), posts: Default::default(), rejected };
        let (w, v, ph, d2, posts) = (h.world.clone(), h.voxels.clone(), h.physics.clone(), h.budget.clone(), h.posts.clone());
        let (info, rejections) = h.enter(SETUP_MS, |ctx| {
            let g = ctx.globals();
            g.set("__host_log", Function::new(ctx.clone(), |level: String, msg: String| game_log(&level, &msg))?)?;
            g.set("__host_now", Function::new(ctx.clone(), move || epoch.elapsed().as_secs_f64() * 1000.0)?)?;
            // The host channel, script to app: a message's JSON text onto the queue the app drains. False when the
            // queue is full (nobody drains it); a message past the size bound is the script's error.
            g.set("__host_post", Function::new(ctx.clone(), move |ctx: Ctx, text: String| -> rquickjs::Result<bool> {
                if text.len() > MAX_POST_BYTES && (text.len() > MAX_STORE_WRITE_BYTES || post_type(&text).as_deref() != Some("store.write")) {
                    let most = if text.len() > MAX_STORE_WRITE_BYTES { MAX_STORE_WRITE_BYTES } else { MAX_POST_BYTES };
                    return Err(Exception::throw_range(&ctx, &format!("host.post: a message of {} bytes (at most {most}; a store.write {MAX_STORE_WRITE_BYTES})", text.len())));
                }
                let mut q = posts.borrow_mut();
                let room = q.len() < MAX_POSTS && q.iter().map(String::len).sum::<usize>() + text.len() <= MAX_QUEUE_BYTES;
                if room {
                    q.push(text);
                }
                Ok(room)
            })?)?;
            // Bytes as base64 and back, natively: what the host channel carries a file as (docs/RUNTIME.md "Files"),
            // tens of megabytes that the script would take seconds over. packages/gameplay/src/files.ts uses them.
            g.set("__host_base64_encode", Function::new(ctx.clone(), base64_encode)?)?;
            g.set("__host_base64_decode", Function::new(ctx.clone(), base64_decode)?)?;
            install_engine(ctx, &w, &v, &ph, &d2, &h.rendering)?;
            ctx.eval::<(), _>(PRELUDE)?;
            code(ctx)?;
            host(ctx)?.get::<_, Function>("describe")?.call::<_, String>(())
        });
        // (A module that throws as it loads rejects its promise: the error returned says it, once.)
        let info = info?;
        for line in &rejections {
            game_log("error", line);
        }
        h.info = serde_json::from_str(&info)?;
        Ok(h)
    }

    /// Runs `f` in the engine under a time budget, then the promise jobs it left (`enter`); JS exceptions become
    /// errors with their message and stack, and the entry's unhandled rejections are error lines of the game's log.
    fn call<T>(&self, budget_ms: u64, f: impl for<'js> FnOnce(&Ctx<'js>) -> rquickjs::Result<T>) -> Result<T> {
        let (r, rejections) = self.enter(budget_ms, f);
        for line in &rejections {
            game_log("error", line);
        }
        r
    }

    pub fn attach_rendering(&self, core: &mut crate::live::LiveCore) {
        core.attach_rendering_control(self.rendering.clone());
    }

    /// One entry into the script: `f` under a time budget, then the promise jobs the entry left (`run_jobs`), within
    /// the same budget, whether `f` threw or not. Returns `f`'s result (its own error first, else the jobs': the entry
    /// fails when its time is up with jobs still to run, as it does when `f` overruns) and the entry's unhandled
    /// rejections as lines for its log.
    fn enter<T>(&self, budget_ms: u64, f: impl for<'js> FnOnce(&Ctx<'js>) -> rquickjs::Result<T>) -> (Result<T>, Vec<String>) {
        self.budget.lent.store(0, Ordering::Relaxed);
        self.budget.deadline.store(self.epoch.elapsed().as_micros() as u64 + budget_ms * 1000, Ordering::Relaxed);
        let r = self.ctx.with(|ctx| f(&ctx).catch(&ctx).map_err(|e| anyhow!("{e}")));
        let jobs = self.run_jobs();
        let rejections = self.take_rejections();
        self.budget.deadline.store(0, Ordering::Relaxed);
        (r.and_then(|v| jobs.map(|()| v)), rejections)
    }

    /// Runs the pending promise jobs until none is left: the callbacks of promises the entry settled, the code after
    /// an `await`, and the jobs those queue in turn. The script engine has no event loop, so without this nothing
    /// after a promise ever ran. What they changed in the scene is sent to the engine by the node host's `sync`
    /// (the entry's own sync ran before them), so the commit that follows the entry applies it.
    ///
    /// The entry's deadline holds: a job that spins is interrupted like any script, and a chain that queues itself
    /// for ever is ended here when the time is up. The jobs not run stay queued, and run after the next entry.
    fn run_jobs(&self) -> Result<()> {
        loop {
            let mut ran = false;
            while self.rt.is_job_pending() {
                if self.budget.up() {
                    return Err(anyhow!("interrupted (the script's time is up with promise jobs still to run)"));
                }
                // (A job fails only on what a script cannot catch, the interrupt: what a callback throws rejects its
                // promise.)
                if self.rt.execute_pending_job().is_err() {
                    return Err(self.ctx.with(|ctx| match Err::<(), _>(rquickjs::Error::Exception).catch(&ctx) {
                        Err(e) => anyhow!("{e}"),
                        Ok(()) => anyhow!("a promise job failed"),
                    }));
                }
                ran = true;
            }
            if !ran {
                return Ok(());
            }
            // (Bundles built before `sync` existed, the scripts of tests, and a bundle still loading have none.)
            self.ctx.with(|ctx| {
                let sync = || -> rquickjs::Result<()> {
                    let Some(host) = ctx.globals().get::<_, Option<Object>>("__threert")? else { return Ok(()) };
                    match host.get::<_, Option<Function>>("sync")? {
                        Some(f) => f.call(()),
                        None => Ok(()),
                    }
                };
                sync().catch(&ctx).map_err(|e| anyhow!("{e}"))
            })?;
        }
    }

    /// The entry's unhandled rejections (promises rejected that no `then`, `catch` or `await` had taken up when the
    /// entry's jobs had run), each once, as error lines naming their reasons.
    fn take_rejections(&self) -> Vec<String> {
        let Rejections { list, more } = std::mem::take(&mut *self.rejected.borrow_mut());
        if list.is_empty() {
            return Vec::new();
        }
        self.ctx.with(|ctx| {
            let more = more + list.len().saturating_sub(MAX_REJECTIONS);
            let mut lines: Vec<String> = Vec::new();
            for (k, (_promise, reason)) in list.into_iter().enumerate() {
                if k < MAX_REJECTIONS {
                    lines.push(format!("unhandled rejection: {}", reason.restore(&ctx).map_or_else(|_| "a value that cannot be shown".into(), |r| reason_text(&ctx, &r))));
                }
            }
            if more > 0 {
                lines.push(format!("unhandled rejection: {more} more in the same entry"));
            }
            lines
        })
    }

    fn call_host<T: for<'js> rquickjs::FromJs<'js>>(&self, budget_ms: u64, name: &str, args: impl for<'js> rquickjs::function::IntoArgs<'js>) -> Result<T> {
        self.call(budget_ms, |ctx| host(ctx)?.get::<_, Function>(name)?.call(args))
    }

    pub fn has_frame(&self) -> bool {
        self.info["frame"].as_bool().unwrap_or(false)
    }

    /// The input the game asked for (`pointer` in its definition): "look" (the app captures the pointer) or "free".
    /// None: the game reads no input, and the app keeps its own camera controls.
    pub fn pointer(&self) -> Option<&str> {
        self.info["pointer"].as_str()
    }

    /// The messages the game posted for the app since the last call (`host.post`), as JSON values, in order.
    pub fn take_posts(&self) -> Vec<serde_json::Value> {
        std::mem::take(&mut *self.posts.borrow_mut()).iter().filter_map(|t| serde_json::from_str(t).ok()).collect()
    }

    /// The app's messages for the game, a batch in one entry (the node host's `hostMessages`): `input` is kept for
    /// the frame script, the others go to their `host.on` listeners. Bundles built before the channel have no such
    /// entry: nothing to do then.
    pub fn host_messages(&self, messages: &[serde_json::Value]) -> Result<()> {
        if messages.is_empty() {
            return Ok(());
        }
        let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |d| d.as_millis() as f64);
        let list = serde_json::Value::Array(messages.to_vec()).to_string();
        self.call(EVENT_MS, |ctx| match host(ctx)?.get::<_, Option<Function>>("hostMessages")? {
            Some(f) => f.call((list, ts)),
            None => Ok(()),
        })
    }

    /// Handler bound to a key ("R", "Space"), if any.
    pub fn handler_for_key(&self, key: &str) -> Option<String> {
        self.info["keys"][key].as_str().map(String::from)
    }

    /// The game handles events of this type: with a handler (a transaction on the state) or a world handler (`on`:
    /// it changes the scene on this node; packages/gameplay/src/world.ts).
    pub fn has_handler(&self, name: &str) -> bool {
        ["handlers", "on"].iter().any(|k| self.info[k].as_array().is_some_and(|h| h.iter().any(|x| x == name)))
    }

    /// The node host runs every frame (`frame`): the game has a frame script, or world handlers (they read the
    /// frame clock), or the scene has a physics world (its contacts and the bodies that left the world are delivered
    /// to their listeners once a frame, whoever registered them: a frame script, a world handler or setup). A scene
    /// with none of them pays nothing.
    pub fn frames(&self) -> bool {
        self.has_frame() || self.info["on"].as_array().is_some_and(|h| !h.is_empty()) || self.physics.borrow().is_some()
    }

    /// Builds and binds the scene (deterministic for a room id / seed); returns the time it took, ms.
    pub fn setup(&self, room: &str, seed: Option<u32>) -> Result<f64> {
        let t = Instant::now();
        let _: String = self.call_host(SETUP_MS, "setup", (room, seed))?;
        Ok(t.elapsed().as_secs_f64() * 1000.0)
    }

    /// One client event on this node's own store (local play: the node is the authority).
    pub fn event(&self, kind: &str, payload: &serde_json::Value) -> Result<EventOut> {
        let t = Instant::now();
        let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |d| d.as_millis() as f64);
        let (json, rejections) = self.enter(EVENT_MS, |ctx| host(ctx)?.get::<_, Function>("event")?.call::<_, String>((kind, payload.to_string(), ts)));
        let json = match json {
            Ok(json) => json,
            Err(e) => {
                for line in &rejections {
                    game_log("error", line);
                }
                return Err(e);
            }
        };
        let v: serde_json::Value = serde_json::from_str(&json)?;
        // (An event's unhandled rejections are lines of its log, as its handlers' errors are.)
        let mut log: Vec<String> = serde_json::from_value(v["log"].clone()).unwrap_or_default();
        log.extend(rejections.iter().map(|l| format!("✗ {l}")));
        Ok(EventOut { log, seq: v["seq"].as_i64().unwrap_or(-1), ms: t.elapsed().as_secs_f64() * 1000.0 })
    }

    /// State writes another authority (the room) committed.
    pub fn commit(&self, writes: &serde_json::Value, seq: i64) -> Result<()> {
        self.call_host::<()>(EVENT_MS, "commit", (writes.to_string(), seq))
    }

    /// Replaces the game state (a render of a room's current state).
    pub fn set_state(&self, state: &serde_json::Value, seq: i64) -> Result<()> {
        self.call_host::<()>(EVENT_MS, "setState", (state.to_string(), seq))
    }

    /// The committed game state and its sequence number.
    pub fn state(&self) -> Result<(serde_json::Value, i64)> {
        let s: String = self.call_host(EVENT_MS, "state", ())?;
        let v: serde_json::Value = serde_json::from_str(&s)?;
        Ok((v["state"].clone(), v["seq"].as_i64().unwrap_or(0)))
    }

    /// Sets the frame script's clock to `t` (s) and shows the scene settled at that moment (an offline render at a scene
    /// time; frames from there advance it as before).
    /// Bundles built before `seek` existed have none: nothing to do then.
    /// Under setup's budget, not a frame's: it is no frame anyone waits on, and its entry also syncs all that setup
    /// made (a streamed city's hundred cells at the radii an offline render asks for take more than a frame's 50 ms).
    pub fn seek(&self, t: f32) -> Result<()> {
        let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |d| d.as_millis() as f64);
        self.call(SETUP_MS, |ctx| match host(ctx)?.get::<_, Option<Function>>("seek")? {
            Some(f) => f.call((t as f64, now)),
            None => Ok(()),
        })
    }

    /// A frame of the physics world, if scene code made one: its ticks, and the objects its bodies drive placed
    /// (renderer/src/physics.rs). True if there is a world (the caller commits what moved).
    pub fn advance_physics(&self, dt: f32) -> bool {
        crate::api::physics_advance(&mut self.physics.borrow_mut(), &mut self.voxels.borrow_mut(), &mut self.world.borrow_mut(), dt as f64)
    }

    /// The physics world `ticks` ticks on (an offline render at a scene time).
    pub fn settle_physics(&self, ticks: u32) {
        crate::api::physics_settle(&mut self.physics.borrow_mut(), &mut self.voxels.borrow_mut(), &mut self.world.borrow_mut(), ticks);
    }

    /// Runs the frame script for one rendered frame.
    pub fn frame(&self, dt: f32) -> Result<()> {
        let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |d| d.as_millis() as f64);
        self.call_host::<()>(FRAME_MS, "frame", (dt as f64, now))
    }

    /// The live scene's objects (editor outliner), JSON.
    pub fn outline(&self) -> Result<String> {
        self.call_host(EVENT_MS, "outline", ())
    }

    /// One object's editable properties, JSON.
    pub fn inspect(&self, name: &str) -> Result<String> {
        self.call_host(EVENT_MS, "inspect", (name,))
    }

    /// Edits one property of a live object (`value` JSON).
    pub fn edit(&self, name: &str, prop: &str, value: &serde_json::Value) -> Result<()> {
        self.call_host::<()>(EVENT_MS, "edit", (name, prop, value.to_string()))
    }
}

/// A game running on this node with its live view: the scene built in the embedded runtime, events (local authority)
/// and the frame script committed to the renderer. Shared by the native window (`play`), the headless runner and the
/// native clients.
pub struct GameSession {
    pub host: ScriptHost,
    pub seq: i64,
    pub setup_ms: f64,
    /// Frame script + commit cost of the last frame, ms.
    pub frame_ms: f32,
    /// Of `frame_ms`, the frame script's own JavaScript (the rest: the physics' ticks, and applying what both changed,
    /// `LiveCore::commit`: `commit_ms`).
    pub js_ms: f32,
    pub commit_ms: f32,
    /// Last handler log line (window title).
    pub status: String,
    /// The frame script's clock (`f.time`).
    clock: GameClock,
    /// When the last click arrived, until its first frame is drawn (`click_shown`; THREERT_CLICK_LOG=1).
    clicked: Option<Instant>,
    /// Whether the last event's commit rebuilt the scene.
    rebuilt: bool,
}

/// THREERT_CLICK_LOG=1: a click's chain timed on stderr (`click: ...`): the pick, the script (handler, frame script,
/// binding; of which the physics world's calls), the commit, and the first frame after it.
fn click_log() -> bool {
    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ON.get_or_init(|| std::env::var("THREERT_CLICK_LOG").is_ok_and(|v| v == "1"))
}

/// The frame script's clock: the gameplay FrameRunner adds each frame's dt clamped to [0, 0.1] s, in f64. While a
/// frame script runs, the scene time is this clock, whatever advanced the scene between its frames (an editor
/// may animate the scene while editing, and run the frame script only in Play).
#[derive(Default)]
struct GameClock(f64);

impl GameClock {
    fn tick(&mut self, dt: f32) -> f32 {
        self.0 += (dt as f64).clamp(0.0, 0.1);
        self.0 as f32
    }
}

impl GameSession {
    /// Loads the bundle and builds the scene in the runtime.
    fn load(bundle: &std::path::Path, room: &str) -> Result<GameSession> {
        Self::load_code(GameCode::Bundle(&std::fs::read_to_string(bundle).map_err(|e| anyhow!("{}: {e}", bundle.display()))?), room)
    }

    /// Loads game code and builds the scene in the runtime, without a live view yet: a client can fetch the files the
    /// scene reads (`files`) before `start_loaded` / `open_loaded` build the GPU scene from them.
    pub fn load_code(code: GameCode, room: &str) -> Result<GameSession> {
        let host = match code {
            GameCode::Bundle(b) => ScriptHost::new(b)?,
            GameCode::Modules { game, runtime } => ScriptHost::from_modules(game, runtime)?,
        };
        let setup_ms = host.setup(room, None)?;
        host.world.borrow_mut().take_changes();
        eprintln!("game: setup {setup_ms:.0} ms in the node runtime, {} objects, handlers {}, frame script {}", host.world.borrow().objects.len(), host.info["handlers"], host.has_frame());
        Ok(GameSession { host, seq: 0, setup_ms, frame_ms: 0.0, js_ms: 0.0, commit_ms: 0.0, status: String::new(), clock: GameClock::default(), clicked: None, rebuilt: false })
    }

    /// Loads the bundle, builds the scene in the runtime and opens a live view of it. Models and file textures resolve
    /// against `assets`.
    pub fn start(bundle: &std::path::Path, room: &str, assets: &std::path::Path, g: crate::gpu::Gpu, presets: &std::path::Path, w: u32, h: u32) -> Result<(GameSession, crate::live::LiveCore)> {
        Self::load(bundle, room)?.start_loaded(assets, g, presets, w, h)
    }

    /// Loads the bundle into an existing live view (history cleared, camera from the scene).
    pub fn open(bundle: &std::path::Path, room: &str, assets: &std::path::Path, core: &mut crate::live::LiveCore) -> Result<GameSession> {
        Self::load(bundle, room)?.open_loaded(assets, core)
    }

    /// Files the built scene reads from the asset root (models, file textures, procedural assets) and the voxel level
    /// it asked for (`voxelLevel`, loaded later by `realize_voxels`), sorted, each once.
    pub fn files(&self) -> Vec<String> {
        let mut set: std::collections::BTreeSet<String> = self.host.world.borrow().files().into_iter().collect();
        set.extend(self.host.voxels.borrow().files());
        set.into_iter().collect()
    }

    /// Opens the first live view of a loaded session (`load_code`).
    pub fn start_loaded(self, assets: &std::path::Path, g: crate::gpu::Gpu, presets: &std::path::Path, w: u32, h: u32) -> Result<(GameSession, crate::live::LiveCore)> {
        self.realize_voxels(assets)?;
        let mut core = crate::live::LiveCore::new(g, &self.host.world.borrow(), assets, presets, w, h)?;
        self.host.attach_rendering(&mut core);
        Ok((self, core))
    }

    /// Shows a loaded session (`load_code`) in an existing live view (history cleared, camera from the scene).
    pub fn open_loaded(self, assets: &std::path::Path, core: &mut crate::live::LiveCore) -> Result<GameSession> {
        self.realize_voxels(assets)?;
        core.open(&self.host.world.borrow(), assets)?;
        self.host.attach_rendering(core);
        core.seq = 0;
        Ok(self)
    }

    /// Loads the voxel level the scene code asked for (`voxelLevel`) now that the asset root is known. The scene build
    /// that follows includes it, so its changes are dropped here (as the setup's are, `load`).
    fn realize_voxels(&self, assets: &std::path::Path) -> Result<()> {
        let mut w = self.host.world.borrow_mut();
        if let Some(r) = self.host.voxels.borrow_mut().realize(&mut w, &mut self.host.physics.borrow_mut(), assets)? {
            eprintln!("voxels: {r}");
            w.take_changes();
        }
        Ok(())
    }

    /// An editor edit of the live scene (see the node host's `edit`), applied to the view.
    pub fn edit(&mut self, core: &mut crate::live::LiveCore, name: &str, path: &str, value: &serde_json::Value) -> Result<bool> {
        self.host.edit(name, path, value)?;
        self.commit(core)
    }

    /// Applies what the scene code changed to the live view.
    pub fn commit(&mut self, core: &mut crate::live::LiveCore) -> Result<bool> {
        core.commit(&mut self.host.world.borrow_mut())
    }

    /// Runs a handler (and its follow-ups); the frame script updates the scene to the new state.
    pub fn event(&mut self, core: &mut crate::live::LiveCore, kind: &str, payload: serde_json::Value) -> Result<EventOut> {
        let e = self.host.event(kind, &payload)?;
        let t = Instant::now();
        let rebuilt = self.commit(core)?;
        self.rebuilt = rebuilt;
        self.seq = e.seq;
        core.seq = e.seq;
        for l in &e.log {
            eprintln!("game: {l} · {:.1} ms handler, {:.2} ms {}", e.ms, t.elapsed().as_secs_f64() * 1000.0, if rebuilt { "rebuild" } else { "commit" });
        }
        if let Some(l) = e.log.last() {
            self.status = l.clone();
        }
        Ok(e)
    }

    /// A click at (u, v) of the image: picks the object under it and sends `click {target, point, player}`, with
    /// `instanceId` for an instance of an instanced mesh (three.js's intersection field).
    pub fn click(&mut self, core: &mut crate::live::LiveCore, u: f32, v: f32) -> Result<Option<EventOut>> {
        if !self.host.has_handler("click") {
            return Ok(None);
        }
        let t0 = Instant::now();
        let Some((target, p, sub)) = core.pick(u, v) else { return Ok(None) };
        let pick_ms = t0.elapsed().as_secs_f64() * 1e3;
        let r = |x: f32| (x * 1e4).round() / 1e4;
        // (`ray`: the view ray through the click, for a `Raycaster` in scene code: `raycaster.set(origin, direction)`.)
        let (o, d) = core.ray(u, v);
        let mut payload = serde_json::json!({ "target": target, "point": [r(p.x), r(p.y), r(p.z)], "player": "you", "ray": { "origin": o.to_array(), "direction": d.to_array() } });
        if let Some(i) = sub {
            payload["instanceId"] = i.into();
        }
        if !click_log() {
            return self.event(core, "click", payload).map(Some);
        }
        if let Some(p) = self.host.physics.borrow_mut().as_mut() {
            p.take_call_ms();
        }
        let t1 = Instant::now();
        let e = self.event(core, "click", payload)?;
        let all = t1.elapsed().as_secs_f64() * 1e3;
        let physics = self.host.physics.borrow_mut().as_mut().map_or(0.0, |p| p.take_call_ms());
        eprintln!("click: pick {pick_ms:.3} ms, script {:.3} ms (the physics world's calls {physics:.3} ms), commit {:.3} ms ({})", e.ms, all - e.ms, if self.rebuilt { "rebuild" } else { "in place" });
        self.clicked = Some(t0);
        Ok(Some(e))
    }

    /// The app's messages for the game (the host channel: its input, what its sockets received, `launch`), a batch in
    /// one script entry; what the game's listeners changed in the scene is applied. True if that rebuilt the scene.
    /// Listeners that throw do not fail the batch (their errors are in the game's log).
    pub fn host_messages(&mut self, core: &mut crate::live::LiveCore, messages: &[serde_json::Value]) -> Result<bool> {
        if messages.is_empty() {
            return Ok(false);
        }
        let sent = self.host.host_messages(messages);
        let rebuilt = self.commit(core)?;
        sent?;
        Ok(rebuilt)
    }

    /// The messages the game posted for the app since the last call (`host.post`), in order: to be taken after each
    /// entry into the script, or once a frame.
    pub fn take_posts(&mut self) -> Vec<serde_json::Value> {
        self.host.take_posts()
    }

    /// A frame was drawn: the first one after a click is timed (THREERT_CLICK_LOG=1).
    pub fn click_shown(&mut self) {
        if let Some(t) = self.clicked.take() {
            eprintln!("click: first frame submitted {:.3} ms after the click", t.elapsed().as_secs_f64() * 1e3);
        }
    }

    /// Runs the frame script and applies what it changed; true if that rebuilt the scene. A frame script (or its
    /// sync) that throws does not stop the world: the frame's physics ticks are made and what changed is committed all
    /// the same, then the error is returned (the window and `live` print it and go on).
    pub fn frame(&mut self, core: &mut crate::live::LiveCore, dt: f32) -> Result<bool> {
        if !self.host.frames() {
            // Wind sway and the physics follow the scene time with or without a frame script.
            let swayed = self.host.world.borrow_mut().apply_sway(core.scene_time());
            if self.host.advance_physics(dt) || swayed {
                return self.commit(core);
            }
            return Ok(false);
        }
        let t = Instant::now();
        // (A frame script's clock is the game's; a game without one keeps the view's own time, as before.)
        if self.host.has_frame() {
            core.set_time(self.clock.tick(dt));
        }
        self.host.world.borrow_mut().apply_sway(core.scene_time());
        let scripted = self.host.frame(dt);
        self.js_ms = t.elapsed().as_secs_f32() * 1000.0;
        // The physics' ticks after the frame script (its impulses and targets act in this frame's ticks).
        self.host.advance_physics(dt);
        let tc = Instant::now();
        let rebuilt = self.commit(core)?;
        self.commit_ms = tc.elapsed().as_secs_f32() * 1000.0;
        self.frame_ms = t.elapsed().as_secs_f32() * 1000.0;
        scripted?;
        Ok(rebuilt)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::world::TextureSource;
    use glam::Vec3;

    // A hand-written bundle: what the binding (packages/scene/src/engine.ts) does, reduced to engine calls.
    const GAME: &str = r#"
        let power = 1, frames = 0;
        const quad = (id) => __engine.geometry(id, new Float32Array([0,0,0, 1,0,0, 0,1,0]), new Float32Array([0,0,1, 0,0,1, 0,0,1]), new Float32Array([0,0, 1,0, 0,1]), new Uint32Array([0,1,2]));
        globalThis.__threert = {
            describe: () => JSON.stringify({ handlers: ["click"], frame: true, keys: { L: "click" } }),
            setup: (room, seed) => {
                quad(1);
                __engine.texture(2, { srgb: true, wrap: true, repeat: [1, 1], offset: [0, 0] }, new Uint8Array([1, 2, 3]).subarray(1));
                __engine.material(3, { name: "m", color: [1, 1, 1], opacity: 1, metalness: 0, roughness: 0.5, emissive: [1, 1, 1], emissiveIntensity: 10, doubleSided: false, map: 2 });
                __engine.object(4, { name: "sign", parent: null, castShadow: true, kind: "mesh", geometry: 1, material: 3 });
                __engine.object(5, { name: "cam", parent: null, castShadow: false, kind: "camera", camera: { fov: 50, near: 0.1, far: 100, exposure: { mode: "manual", ev100: 0 } } });
                __engine.transform(5, new Float32Array([0, 0, 5, 0, 0, 0, 1, 1, 1, 1]));
                __engine.environment({ sky: { type: "color", color: [0, 0, 0], intensity: 0, skyLightIntensity: 1 }, wetness: new TextEncoder().encode("é€").length / 10 });
                __engine.settings({ name: room + seed, preset: "cinematic", width: 64, height: 36, time: 0, camera: 5 });
                return "{}";
            },
            event: (type, payload, ts) => { power = 0; __engine.power(4, power); return JSON.stringify({ log: [type + " " + JSON.parse(payload).target], seq: 1 }); },
            commit: () => {},
            setState: () => {},
            frame: (dt) => { frames++; },
            outline: () => "[]", inspect: () => "{}", edit: () => {},
        };
        globalThis.spin = () => { for (;;) {} };
        globalThis.bad = () => __engine.material(9, { name: 7 });
    "#;

    #[test]
    fn game_clock_matches_the_frame_runner() {
        // packages/gameplay/test/frame.test.ts: the frame clock adds each dt clamped to [0, 0.1] s.
        let mut c = GameClock::default();
        let t: Vec<f32> = [1.0 / 60.0, 0.5, -1.0, 1.0 / 60.0].into_iter().map(|dt| c.tick(dt)).collect();
        let f = |x: f64| x as f32;
        assert_eq!(t, [f(1.0 / 60.0), f(1.0 / 60.0 + 0.1), f(1.0 / 60.0 + 0.1), f(1.0 / 60.0 + 0.1 + 1.0 / 60.0)]);
    }

    #[test]
    fn a_bundle_drives_the_engine() {
        let h = ScriptHost::new(GAME).unwrap();
        assert!(h.has_frame());
        assert_eq!(h.handler_for_key("L").as_deref(), Some("click"));
        h.setup("room", Some(7)).unwrap();
        {
            let mut w = h.world.borrow_mut();
            assert_eq!(w.settings.name, "room7");
            assert_eq!(w.environment.wetness, 0.5, "TextEncoder in the prelude");
            assert!(matches!(&w.textures[&2].source, TextureSource::Encoded(b) if b.as_slice() == [2, 3]), "typed-array views keep their offset");
            assert_eq!(w.objects[&4].name, "sign");
            assert_eq!(w.objects[&5].position, Vec3::new(0.0, 0.0, 5.0));
            assert!(w.take_changes().structural);
        }
        let e = h.event("click", &serde_json::json!({ "target": "sign" })).unwrap();
        assert_eq!(e.log, vec!["click sign"]);
        let c = h.world.borrow_mut().take_changes();
        assert!(!c.structural && c.powers.contains(&4), "power is a parameter change");
        h.frame(0.5).unwrap();
    }

    #[test]
    fn a_game_as_es_modules() {
        // The cloud's layout: agent code importing "./threert-runtime.js", the runtime as its own module.
        let runtime = r#"
            export const defineGame = (g) => g;
            export function installNodeHost(game) {
                globalThis.__threert = { describe: () => JSON.stringify({ handlers: Object.keys(game.handlers), frame: false, keys: {} }), setup: () => { game.setup(); return "{}"; } };
            }
            export const box = (id) => __engine.geometry(id, new Float32Array(9), new Float32Array(9), new Float32Array(6), new Uint32Array([0, 1, 2]));
        "#;
        let game = r#"
            import { box } from "./threert-runtime.js";
            export default { handlers: { hit() {} }, setup() { box(1); } };
        "#;
        let h = ScriptHost::from_modules(game, runtime).unwrap();
        assert_eq!(h.info["handlers"], serde_json::json!(["hit"]));
        h.setup("r", None).unwrap();
        assert!(h.world.borrow().geometries.contains_key(&1));
        let scene = "export default function () { __engine.geometry(2, new Float32Array(9), new Float32Array(9), new Float32Array(6), new Uint32Array([0, 1, 2])); }";
        let h = ScriptHost::from_modules(scene, &runtime.replace("const defineGame = (g) => g", "const defineGame = (g) => ({ ...g, handlers: {}, setup: g.setup })")).unwrap();
        assert_eq!(h.info["handlers"], serde_json::json!([]), "a scene module is a game without handlers");
        assert!(ScriptHost::from_modules("import x from './nope.js';", runtime).is_err());
    }

    /// A frame whose script throws (as the binding's sync does once when the engine took out a body whose state went
    /// non-finite, or a `physics` cannot be made a body): the error is returned, and the frame's physics tick and
    /// commit are made all the same. (Skipped without a GPU.)
    #[test]
    fn a_frame_script_that_throws_does_not_stop_the_world() {
        let g = match crate::gpu::Gpu::new() {
            Ok(g) => g,
            Err(e) => return eprintln!("skipped: {e}"),
        };
        // The game above with a weightless body that drives the sign at 6 m/s, and a frame script that throws in its
        // second frame.
        let game = GAME.replace("return \"{}\";", "__engine.physics('body', { id: 1, object: 4, gravityScale: 0, linvel: [0, 0, 6] }); __engine.physics('collider', { id: 1, body: 1, shape: { type: 'sphere', radius: 0.5 } }); return \"{}\";").replace("frame: (dt) => { frames++; },", "frame: (dt) => { frames++; if (frames === 2) throw new Error('the sync threw'); },").replace(", map: 2 }", " }");
        // (Without the sign's texture: its bytes are not an image.)
        assert!(game.contains("__engine.physics") && game.contains("the sync threw") && !game.contains("map: 2"));
        let bundle = std::env::temp_dir().join(format!("threert-frame-throws-{}.js", std::process::id()));
        std::fs::write(&bundle, game).unwrap();
        let started = GameSession::start(&bundle, "room", std::path::Path::new("."), g, &crate::default_presets_dir(), 64, 36);
        std::fs::remove_file(&bundle).ok();
        let (mut s, mut core) = started.unwrap();
        let z = |s: &GameSession| s.host.world.borrow().objects[&4].position.z;
        let ticks = |s: &GameSession| s.host.physics.borrow_mut().as_mut().unwrap().take_stats().ticks;
        s.frame(&mut core, 1.0 / 60.0).unwrap();
        assert_eq!(ticks(&s), 1);
        assert!((z(&s) - 0.1).abs() < 1e-4, "{}", z(&s));
        let e = s.frame(&mut core, 1.0 / 60.0).unwrap_err();
        assert!(format!("{e:#}").contains("the sync threw"), "{e:#}");
        assert_eq!(ticks(&s), 1, "the tick of the frame that threw");
        assert!((z(&s) - 0.2).abs() < 1e-4, "{}", z(&s));
        let c = s.host.world.borrow_mut().take_changes();
        assert!(c.moved.is_empty() && !c.structural, "and its commit");
        s.frame(&mut core, 1.0 / 60.0).unwrap();
        assert!((z(&s) - 0.3).abs() < 1e-4, "{}", z(&s));
    }

    /// The input path knows a game's world handlers (`on`) as it knows its handlers, and the node host runs every
    /// frame for a game that has them, or whose scene has a physics world, though it has no frame script.
    #[test]
    fn world_handlers_and_the_frames_of_a_game_without_a_frame_script() {
        let plain = GAME.replace("handlers: [\"click\"], frame: true", "handlers: [\"hit\"], frame: false");
        assert!(plain.contains("frame: false"));
        let h = ScriptHost::new(&plain).unwrap();
        assert!(h.has_handler("hit") && !h.has_handler("click") && !h.has_frame() && !h.frames(), "a scene with no script to run a frame for");
        let on = plain.replace("handlers: [\"hit\"],", "handlers: [\"hit\"], on: [\"click\"],");
        let h = ScriptHost::new(&on).unwrap();
        assert!(h.has_handler("click") && h.has_handler("hit") && !h.has_handler("drag"));
        assert!(!h.has_frame() && h.frames(), "world handlers read the frame clock");
        // A physics world (made by the scene's first physics call): its events go to their listeners once a frame.
        let bodies = plain.replace("return \"{}\";", "__engine.physics('gravity', { gravity: [0, -9.81, 0] }); return \"{}\";");
        let h = ScriptHost::new(&bodies).unwrap();
        assert!(!h.frames());
        h.setup("room", None).unwrap();
        assert!(h.frames());
    }

    /// A game without a frame script whose scene has bodies: the node host's `frame` runs every frame all the same
    /// (it delivers the contacts and the bodies that left the world to their listeners), after which the physics
    /// ticks; a scene with neither has no script run for it. (Skipped without a GPU.)
    #[test]
    fn a_scene_with_bodies_gets_its_frames_without_a_frame_script() {
        for (physics, expected) in [(true, 3), (false, 0)] {
            let g = match crate::gpu::Gpu::new() {
                Ok(g) => g,
                Err(e) => return eprintln!("skipped: {e}"),
            };
            let mut game = GAME.replace("handlers: [\"click\"], frame: true", "handlers: [\"click\"], frame: false").replace(", map: 2 }", " }");
            if physics {
                game = game.replace("return \"{}\";", "__engine.physics('body', { id: 1, object: 4, gravityScale: 0, linvel: [0, 0, 6] }); __engine.physics('collider', { id: 1, body: 1, shape: { type: 'sphere', radius: 0.5 } }); return \"{}\";");
            }
            let bundle = std::env::temp_dir().join(format!("threert-no-frame-script-{}-{physics}.js", std::process::id()));
            std::fs::write(&bundle, game).unwrap();
            let started = GameSession::start(&bundle, "room", std::path::Path::new("."), g, &crate::default_presets_dir(), 64, 36);
            std::fs::remove_file(&bundle).ok();
            let (mut s, mut core) = started.unwrap();
            for _ in 0..3 {
                s.frame(&mut core, 1.0 / 60.0).unwrap();
            }
            let frames: i32 = s.host.call(100, |ctx| ctx.eval("frames")).unwrap();
            assert_eq!(frames, expected, "physics {physics}");
            if physics {
                let z = s.host.world.borrow().objects[&4].position.z;
                assert!((z - 0.3).abs() < 1e-4, "three ticks at 6 m/s: {z}");
            }
        }
    }

    /// The engine's time in a voxel edit is not the script's: a frame's worth of script that spends 150 ms in edits
    /// (three times its budget) goes on to its end, and one that spins after its edits is still ended.
    #[test]
    fn the_engines_time_in_a_voxel_call_is_not_the_scripts() {
        let h = ScriptHost::new(GAME).unwrap();
        *h.voxels.borrow_mut() = crate::voxel::VoxelWorld::of_blocks(&[([0, 0, 0], [64, 16, 64], Some((0, 0)), true)], &mut h.world.borrow_mut(), &mut h.physics.borrow_mut());
        let edits = "let t = __host_now(), n = 0; while (__host_now() - t < 150) { __engine.voxelEdit({ op: 'carve', box: [0.5, 0.5, 0.5, 5.5, 1.5, 5.5] }); __engine.voxelEdit({ op: 'add', box: [0.5, 0.5, 0.5, 5.5, 1.5, 5.5], material: 1 }); n++; }";
        let n: i32 = h.call(FRAME_MS, |ctx| ctx.eval(format!("{edits} let s = 0; for (let i = 0; i < 100000; i++) s += i; n"))).unwrap();
        assert!(n >= 1);
        let t = Instant::now();
        let err = h.call(FRAME_MS, |ctx| ctx.eval::<(), _>(format!("{{ {edits} }} spin()"))).unwrap_err();
        assert!(format!("{err}").contains("interrupted"), "{err}");
        assert!(t.elapsed().as_millis() < 2000, "{} ms", t.elapsed().as_millis());
    }

    /// The engine's time is lent as it was, not rounded up, and at most `LEND_US` an entry (the review of #24: each
    /// call moved the deadline by a millisecond at least, so a frame script looping on cheap voxel calls, or on ones
    /// that throw, ran for ever). Each loop is ended within its budget, and real edits past what may be lent are too.
    #[test]
    fn a_script_looping_on_voxel_calls_is_ended() {
        let h = ScriptHost::new(GAME).unwrap();
        // Without a level: voxelFrame does nothing, voxelEdit and voxelExplode throw.
        for body in ["__engine.voxelFrame(0)", "try { __engine.voxelEdit({}) } catch (e) {}", "try { __engine.voxelExplode(0, 0, 0, 1) } catch (e) {}"] {
            let t = Instant::now();
            let err = h.call(FRAME_MS, |ctx| ctx.eval::<(), _>(format!("for (;;) {{ {body}; }}"))).unwrap_err();
            assert!(format!("{err}").contains("interrupted"), "{body}: {err}");
            assert!(t.elapsed().as_millis() < 200, "{body}: ended after {} ms", t.elapsed().as_millis());
        }
        // With a level, edits that each take real time, for ever: lent up to 250 ms, then the budget's 50.
        *h.voxels.borrow_mut() = crate::voxel::VoxelWorld::of_blocks(&[([0, 0, 0], [64, 16, 64], Some((0, 0)), true)], &mut h.world.borrow_mut(), &mut h.physics.borrow_mut());
        let t = Instant::now();
        let err = h.call(FRAME_MS, |ctx| ctx.eval::<(), _>("for (;;) { __engine.voxelEdit({ op: 'carve', box: [0.5, 0.5, 0.5, 5.5, 1.5, 5.5] }); __engine.voxelEdit({ op: 'add', box: [0.5, 0.5, 0.5, 5.5, 1.5, 5.5], material: 1 }); }")).unwrap_err();
        assert!(format!("{err}").contains("interrupted"), "{err}");
        let ms = t.elapsed().as_millis();
        assert!((250..800).contains(&ms), "ended after {ms} ms (50 of script and at most 250 lent)");
    }

    /// The engine's half of a voxel body gone under a held object (the review of #25): a level edit carves it away;
    /// every engine call on its id then throws a script error (which `VoxelBody` takes as its cue to find it gone),
    /// none panics, and the gone list names it with its reason.
    #[test]
    fn calls_on_a_voxel_body_the_level_carved_away_throw_cleanly() {
        let h = ScriptHost::new(GAME).unwrap();
        *h.voxels.borrow_mut() = crate::voxel::VoxelWorld::of_blocks(&[([0, 0, 0], [64, 4, 64], Some((0, 0)), true), ([28, 4, 28], [4, 24, 4], None, false)], &mut h.world.borrow_mut(), &mut h.physics.borrow_mut());
        h.voxels.borrow_mut().edit(&mut h.world.borrow_mut(), &mut h.physics.borrow_mut(), &crate::voxel::EditOp::parse(&serde_json::json!({ "op": "carve", "box": [2.7, 0.5, 2.7, 3.3, 0.7, 3.3] })).unwrap()).unwrap();
        let out: String = h
            .call(1000, |ctx| {
                ctx.eval(
                    r#"
                const id = JSON.parse(__engine.voxelBodies())[0].body;
                __engine.voxelEdit({ op: "carve", box: [2.0, 0.6, 2.0, 4.0, 4.0, 4.0] });
                const tries = {
                    state: () => __engine.physics("state", { body: id }),
                    impulse: () => __engine.physics("impulse", { body: id, impulse: [0, 1, 0] }),
                    contacts: () => __engine.voxelContacts(id, true),
                    edit: () => __engine.voxelBodyEdit(id, { op: "paint", material: 1, sphere: [3, 1, 3, 1] }),
                };
                const r = [__engine.voxelBodies()];
                for (const [k, f] of Object.entries(tries)) { try { f(); r.push(k + " ok"); } catch (e) { r.push(k + " threw"); } }
                r.push(__engine.voxelGone());
                r.push(String(id));
                JSON.stringify(r)
            "#,
                )
            })
            .unwrap();
        let r: Vec<String> = serde_json::from_str(&out).unwrap();
        let id = &r[6];
        assert_eq!(r[..6], ["[]".to_string(), "state threw".into(), "impulse threw".into(), "contacts threw".into(), "edit threw".into(), format!("[[{id},\"carved\"]]")]);
    }

    /// A descriptor whose getter calls the engine again (the review of #25: the engine was borrowed while the
    /// descriptor was read, and the second call panicked, "already mutably borrowed"): each entry that takes one reads
    /// it first. None panics; each answers or throws a script error.
    #[test]
    fn a_getter_in_a_descriptor_that_calls_the_engine_does_not_panic_it() {
        let h = ScriptHost::new(GAME).unwrap();
        *h.voxels.borrow_mut() = crate::voxel::VoxelWorld::of_blocks(&[([0, 0, 0], [64, 4, 64], Some((0, 0)), true), ([28, 4, 28], [4, 24, 4], None, false)], &mut h.world.borrow_mut(), &mut h.physics.borrow_mut());
        h.voxels.borrow_mut().edit(&mut h.world.borrow_mut(), &mut h.physics.borrow_mut(), &crate::voxel::EditOp::parse(&serde_json::json!({ "op": "carve", "box": [2.7, 0.5, 2.7, 3.3, 0.7, 3.3] })).unwrap()).unwrap();
        // (Every getter calls into the voxel world, the physics and the scene's world: whichever one the entry borrows.)
        let poke = "__engine.voxelBodies(); __engine.voxelHash(); __engine.physics('hash', {}); __engine.visible(1, true)";
        let cases = [
            ("voxelEdit", format!("__engine.voxelEdit({{ get op() {{ {poke}; return 'carve'; }}, sphere: [1, 0.3, 1, 0.2] }})")),
            ("voxelBodyEdit", format!("__engine.voxelBodyEdit(JSON.parse(__engine.voxelBodies())[0].body, {{ get op() {{ {poke}; return 'paint'; }}, material: 1, sphere: [3, 1.5, 3, 0.3] }})")),
            ("voxelLevel", format!("__engine.voxelLevel({{ get route() {{ {poke}; return 'mesh'; }}, palette: [], objects: [] }})")),
            ("material", format!("__engine.material(9, {{ get color() {{ {poke}; return [1, 1, 1]; }} }})")),
            ("object", format!("__engine.object(9, {{ get name() {{ {poke}; return 'x'; }}, kind: 'group' }})")),
            ("light", format!("__engine.light(9, {{ get type() {{ {poke}; return 'point'; }} }})")),
            ("camera", format!("__engine.camera(9, {{ get fov() {{ {poke}; return 50; }} }})")),
            ("environment", format!("__engine.environment({{ get kind() {{ {poke}; return 'color'; }} }})")),
            ("settings", format!("__engine.settings({{ get exposure() {{ {poke}; return 0; }} }})")),
            ("texture", format!("__engine.texture(9, {{ get uri() {{ {poke}; return 'x.png'; }} }})")),
            ("physics", format!("__engine.physics('ray', {{ get origin() {{ {poke}; return [0, 5, 0]; }}, direction: [0, -1, 0] }})")),
        ];
        for (name, js) in cases {
            let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| h.call(1000, |ctx| ctx.eval::<rquickjs::Value, _>(js.as_str()).map(|_| ()))));
            assert!(r.is_ok(), "{name}: the engine panicked");
        }
    }

    /// An id scene code read out of an answer goes back into a call: a ray's hit on a voxel body, its `body` into an
    /// impulse. (Parsed out of the answer the id is a double in the script engine, and comes back as `1073741837.0`:
    /// the bridge reads a whole number as the id it is.)
    #[test]
    fn an_id_a_script_read_from_a_hit_goes_into_a_call() {
        let h = ScriptHost::new(GAME).unwrap();
        // A slab and a bar standing on it, cut loose: 336 voxels of 1,000 kg/m³.
        let mut v = crate::voxel::VoxelWorld::of_blocks(&[([0, 0, 0], [64, 4, 64], Some((0, 0)), true), ([28, 4, 28], [4, 24, 4], None, false)], &mut h.world.borrow_mut(), &mut h.physics.borrow_mut());
        v.edit(&mut h.world.borrow_mut(), &mut h.physics.borrow_mut(), &crate::voxel::EditOp::parse(&serde_json::json!({ "op": "carve", "box": [2.7, 0.5, 2.7, 3.3, 0.7, 3.3] })).unwrap()).unwrap();
        assert_eq!(v.bodies(), 1);
        *h.voxels.borrow_mut() = v;
        let up: f64 = h.call(100, |ctx| ctx.eval(r#"
            const hit = JSON.parse(__engine.physics("ray", { origin: [3.05, 1.55, 10], direction: [0, 0, -1], max: 20 }));
            if (hit.body !== JSON.parse(__engine.voxelBodies())[0].body) throw new Error("the hit is not the body: " + JSON.stringify(hit));
            __engine.physics("impulse", { body: hit.body, impulse: [0, 500, 0] });
            JSON.parse(__engine.physics("state", { body: hit.body })).linvel[1]
        "#)).unwrap();
        assert!((up - 500.0 / 336.0).abs() < 0.05, "{up} m/s");
    }

    /// The host channel: what the script posts is the app's to take, in order, and bounded; the app's messages go to
    /// the node host's `hostMessages` as one JSON array; a bundle without that entry (built before the channel) is
    /// left alone.
    #[test]
    fn the_host_channel_queues_posts_and_delivers_batches() {
        let game = GAME.replace("frame: (dt) => { frames++; },", "frame: (dt) => { frames++; }, hostMessages: (list, ts) => { for (const m of JSON.parse(list)) __host_post(JSON.stringify({ t: 'echo', of: m, timed: ts > 0 })); },");
        assert!(game.contains("hostMessages"));
        let h = ScriptHost::new(&game).unwrap();
        assert!(h.take_posts().is_empty());
        h.host_messages(&[serde_json::json!({ "t": "net.opened", "id": "a" }), serde_json::json!({ "t": "input", "keys": ["W"] })]).unwrap();
        assert_eq!(h.take_posts(), vec![serde_json::json!({ "t": "echo", "of": { "t": "net.opened", "id": "a" }, "timed": true }), serde_json::json!({ "t": "echo", "of": { "t": "input", "keys": ["W"] }, "timed": true })]);
        assert!(h.take_posts().is_empty(), "taken once");
        // A queue nobody drains keeps its first messages and says no to the rest; a message past the bound throws.
        let kept: i32 = h.call(1000, |ctx| ctx.eval("let kept = 0; for (let i = 0; i < 3000; i++) if (__host_post(JSON.stringify({ t: 'n', i }))) kept++; kept")).unwrap();
        assert_eq!(kept as usize, MAX_POSTS);
        let posts = h.take_posts();
        assert_eq!((posts.len(), posts[0]["i"].as_u64(), posts[MAX_POSTS - 1]["i"].as_u64()), (MAX_POSTS, Some(0), Some(MAX_POSTS as u64 - 1)));
        let err = h.call(1000, |ctx| ctx.eval::<bool, _>("__host_post('x'.repeat(2 << 20))")).unwrap_err();
        assert!(format!("{err}").contains("host.post"), "{err}");
        assert!(h.take_posts().is_empty());
        // No `pointer` in the game's description: it asks for no input.
        assert_eq!(h.pointer(), None);
        assert_eq!(ScriptHost::new(&game.replace("frame: true,", "frame: true, pointer: 'look',")).unwrap().pointer(), Some("look"));
        // A bundle from before the channel.
        let old = ScriptHost::new(GAME).unwrap();
        old.host_messages(&[serde_json::json!({ "t": "launch", "params": {} })]).unwrap();
        assert!(old.take_posts().is_empty());
    }

    /// Files on the channel (docs/RUNTIME.md "Files"): base64 both ways, natively; a `store.write` may carry a file of
    /// 64 MiB where other messages stop at 1 MiB; and the largest file `files.read` answers with reaches the game and
    /// is decoded within an entry's budget.
    #[test]
    fn files_cross_the_host_channel_as_base64() {
        use base64::Engine;
        let b64 = base64::engine::general_purpose::STANDARD;
        let game = GAME.replace(
            "frame: (dt) => { frames++; },",
            "frame: (dt) => { frames++; }, hostMessages: (list, ts) => { for (const m of JSON.parse(list)) if (m.t === 'files.data') { const b = __host_base64_decode(m.data); __host_post(JSON.stringify({ t: 'got', n: b.length, first: b[0], last: b[b.length - 1] })); } },",
        );
        let h = ScriptHost::new(&game).unwrap();
        let text: String = h.call(1000, |ctx| ctx.eval("__host_base64_encode(new Uint8Array([9, 0, 1, 2, 255]).subarray(1))")).unwrap();
        assert_eq!(text, "AAEC/w==", "a view's own bytes");
        let back: Vec<u8> = h.call(1000, |ctx| ctx.eval::<TypedArray<u8>, _>("__host_base64_decode('AAEC/w==')").map(|a| a.as_bytes().unwrap().to_vec())).unwrap();
        assert_eq!(back, [0, 1, 2, 255]);
        assert!(h.call(1000, |ctx| ctx.eval::<(), _>("__host_base64_decode('**')")).is_err());
        assert!(h.call(1000, |ctx| ctx.eval::<(), _>("__host_base64_encode([1, 2])")).is_err());
        // A file of 64 MiB the game stores: one post (its text, 85 MiB) where any other message stops at 1 MiB.
        let t0 = Instant::now();
        let ok: bool = h.call(EVENT_MS, |ctx| ctx.eval("const big = new Uint8Array(64 << 20); big[0] = 7; __host_post(JSON.stringify({ t: 'store.write', id: 1, path: 'maps/a.glb', data: __host_base64_encode(big) }))")).unwrap();
        assert!(ok);
        let posts = h.take_posts();
        assert_eq!((posts[0]["t"].as_str(), b64.decode(posts[0]["data"].as_str().unwrap()).unwrap().len()), (Some("store.write"), 64 << 20));
        eprintln!("a 64 MiB store.write posted and taken in {:.0} ms", t0.elapsed().as_secs_f64() * 1e3);
        assert!(h.call(1000, |ctx| ctx.eval::<bool, _>("__host_post(JSON.stringify({ t: 'net.send', id: 'a', data: 'x'.repeat(2 << 20) }))")).is_err());
        assert!(h.call(1000, |ctx| ctx.eval::<bool, _>("__host_post(JSON.stringify({ t: 'store.write', id: 2, path: 'a', data: 'x'.repeat(100 << 20) }))")).is_err());
        // The largest file the app reads for the game, delivered in one batch and decoded under an event's budget.
        let mut file = vec![0u8; 64 << 20];
        (file[0], file[(64 << 20) - 1]) = (3, 5);
        let m = serde_json::json!({ "t": "files.data", "id": "r", "ok": true, "data": b64.encode(&file), "size": file.len() });
        let t0 = Instant::now();
        h.host_messages(&[m]).unwrap();
        eprintln!("a 64 MiB files.data delivered and decoded in {:.0} ms (an entry's budget: {EVENT_MS} ms)", t0.elapsed().as_secs_f64() * 1e3);
        assert_eq!(h.take_posts(), vec![serde_json::json!({ "t": "got", "n": 64 << 20, "first": 3, "last": 5 })]);
    }

    // The game above with what a game written with promises leaves behind in each entry: a `.then` chain and an
    // `async` function (`left`), and a request the app answers in a later message (`ask`: the host channel's
    // `net.fetch` as a library wraps it). `sync` is the node host's: the engine follows what the jobs changed.
    const PROMISES: &str = r#"
        const trace = [], waiting = new Map();
        let syncs = 0, dim = 1;
        const left = (name) => {
            Promise.resolve(1).then((v) => v + 1).then((v) => { trace.push(name + " then " + v); dim = dim / 2; });
            (async () => { await null; const v = await Promise.resolve(3); trace.push(name + " await " + v); })();
        };
        const ask = (id) => new Promise((resolve) => { waiting.set(id, resolve); __host_post(JSON.stringify({ t: "ask", id })); });
        Object.assign(globalThis.__threert, {
            event: (type, payload, ts) => {
                if (type === "ask") (async () => { const v = await ask("a"); trace.push("answered " + v); dim = 0.75; })();
                else if (type === "reject") Promise.resolve().then(() => { throw new TypeError("thrown in a job"); });
                else left(type);
                return JSON.stringify({ log: ["ok " + type], seq: 1 });
            },
            frame: (dt) => left("frame"),
            hostMessages: (list) => { for (const m of JSON.parse(list)) if (m.t === "answer") waiting.get(m.id)(m.value); else left(m.t); },
            sync: () => { syncs++; __engine.power(4, dim); },
        });
        globalThis.took = () => JSON.stringify([trace.splice(0), syncs]);
    "#;

    fn promises() -> ScriptHost {
        let h = ScriptHost::new(&format!("{GAME}{PROMISES}")).unwrap();
        h.setup("room", None).unwrap();
        h.world.borrow_mut().take_changes();
        h
    }

    /// What the script traced since the last look, and how often the node host's `sync` has run.
    fn took(h: &ScriptHost) -> (Vec<String>, u32) {
        serde_json::from_str(&h.call(100, |ctx| ctx.eval::<String, _>("took()")).unwrap()).unwrap()
    }

    /// The script engine has no event loop: each entry runs the promise jobs it left before it returns (a `.then`
    /// chain to its end, an `async` function past its awaits), then has the node host sync, so what the jobs changed
    /// in the scene is in the world the entry's commit applies. An entry that left none syncs nothing more.
    #[test]
    fn promise_jobs_run_in_the_entry_that_left_them() {
        let h = promises();
        let power = |h: &ScriptHost| {
            let mut w = h.world.borrow_mut();
            (w.objects[&4].power, w.take_changes().powers.contains(&4))
        };
        h.frame(1.0 / 60.0).unwrap();
        assert_eq!(took(&h), (vec!["frame then 2".to_string(), "frame await 3".into()], 1), "a frame script");
        assert_eq!(power(&h), (0.5, true), "what a job changed is synced after it");
        let e = h.event("click", &serde_json::json!({})).unwrap();
        assert_eq!(e.log, vec!["ok click"]);
        assert_eq!(took(&h), (vec!["click then 2".to_string(), "click await 3".into()], 2), "an event's handler");
        assert_eq!(power(&h), (0.25, true));
        h.host_messages(&[serde_json::json!({ "t": "note" }), serde_json::json!({ "t": "more" })]).unwrap();
        assert_eq!(took(&h), (["note then 2", "note await 3", "more then 2", "more await 3"].map(String::from).to_vec(), 3), "a batch's listeners: one sync for the batch");
        assert_eq!(power(&h), (0.0625, true));
        // Entries that leave no job: no second sync, nothing changed.
        h.commit(&serde_json::json!([]), 1).unwrap();
        h.set_state(&serde_json::json!({}), 1).unwrap();
        h.edit("sign", "node.visible", &serde_json::json!(true)).unwrap();
        assert_eq!(took(&h), (vec![], 3));
        assert_eq!(power(&h), (0.0625, false));
        // The loading of a bundle is an entry too (a module's top-level code, a library that starts with a promise).
        let h = ScriptHost::new(&format!("{GAME} globalThis.loaded = 0; Promise.resolve().then(() => {{ loaded = 1; }});")).unwrap();
        assert_eq!(h.call(100, |ctx| ctx.eval::<i32, _>("loaded")).unwrap(), 1);
    }

    /// No timers and no I/O: a promise only the app can resolve (its answer is a later host message) stays pending
    /// through the frames in between, and its awaiter runs in the entry of the message that resolves it.
    #[test]
    fn a_promise_a_later_host_message_resolves_resumes_its_awaiter_in_that_entry() {
        let h = promises();
        h.event("ask", &serde_json::json!({})).unwrap();
        assert_eq!(h.take_posts(), vec![serde_json::json!({ "t": "ask", "id": "a" })]);
        for _ in 0..3 {
            h.frame(1.0 / 60.0).unwrap();
        }
        let (trace, syncs) = took(&h);
        assert!(!trace.iter().any(|l| l.starts_with("answered")), "nothing resumes it between entries: {trace:?}");
        h.world.borrow_mut().take_changes();
        h.host_messages(&[serde_json::json!({ "t": "answer", "id": "a", "value": 7 })]).unwrap();
        assert_eq!(took(&h), (vec!["answered 7".to_string()], syncs + 1));
        assert_eq!(h.world.borrow().objects[&4].power, 0.75, "and what it changed is in the world when the entry returns");
        assert!(h.world.borrow_mut().take_changes().powers.contains(&4));
    }

    /// The jobs run within the entry's budget: a chain that queues itself for ever is ended when the time is up and
    /// the entry fails as one that spins does; so does a job that spins. The chain is still queued (the next entry
    /// runs into it) until the script stops it; then the host is as before.
    #[test]
    fn an_endless_chain_of_jobs_is_ended_by_the_budget() {
        let h = promises();
        let t = Instant::now();
        let err = h.call(FRAME_MS, |ctx| ctx.eval::<(), _>("globalThis.on = true; globalThis.turns = 0; const again = () => { turns++; if (on) Promise.resolve().then(again); }; again();")).unwrap_err();
        let ms = t.elapsed().as_millis();
        assert!(format!("{err}").contains("interrupted"), "{err}");
        assert!((FRAME_MS as u128..FRAME_MS as u128 + 500).contains(&ms), "ended after {ms} ms");
        let turns = |h: &ScriptHost| h.call(FRAME_MS, |ctx| ctx.eval::<f64, _>("turns"));
        assert!(format!("{}", turns(&h).unwrap_err()).contains("interrupted"), "the chain goes on after the next entry");
        h.call(FRAME_MS, |ctx| ctx.eval::<(), _>("on = false")).unwrap();
        let n = turns(&h).unwrap();
        assert!(n > 1000.0, "{n} turns");
        assert_eq!(turns(&h).unwrap(), n, "stopped");
        // A job that spins is interrupted where it is, and is gone: the next entry is as any other.
        for code in ["Promise.resolve().then(spin)", "(async () => { await null; spin(); })()"] {
            let t = Instant::now();
            let err = h.call(FRAME_MS, |ctx| ctx.eval::<(), _>(code)).unwrap_err();
            assert!(format!("{err}").contains("interrupted"), "{code}: {err}");
            assert!(t.elapsed().as_millis() < 1000, "{code}: ended after {} ms", t.elapsed().as_millis());
            h.frame(1.0 / 60.0).unwrap();
        }
        // An `async` function that never gets past its loop of awaits, left by a frame script.
        let h = ScriptHost::new(&format!("{GAME}{PROMISES}").replace("frame: (dt) => left(\"frame\"),", "frame: (dt) => { (async () => { for (;;) await null; })(); },")).unwrap();
        h.setup("room", None).unwrap();
        let t = Instant::now();
        let err = h.frame(1.0 / 60.0).unwrap_err();
        assert!(format!("{err}").contains("interrupted"), "{err}");
        assert!(t.elapsed().as_millis() < 1000, "ended after {} ms", t.elapsed().as_millis());
    }

    /// A promise rejected that nothing handles is one error line of the entry it was rejected in, naming its reason
    /// (an error thrown in a job is such a rejection). One that gets its handler before the entry's jobs have run is
    /// none, and no entry reports another's.
    #[test]
    fn an_unhandled_rejection_is_reported_once() {
        let h = promises();
        let lines = |code: &str| {
            let (r, lines) = h.enter(100, |ctx| ctx.eval::<(), _>(code));
            r.unwrap();
            lines
        };
        let one = |code: &str| {
            let l = lines(code);
            assert_eq!(l.len(), 1, "{code}: {l:?}");
            l.into_iter().next().unwrap()
        };
        assert!(one("Promise.reject(new Error('lost'))").starts_with("unhandled rejection: Error: lost"));
        assert!(one("Promise.resolve().then(() => { throw new TypeError('thrown in a job'); })").starts_with("unhandled rejection: TypeError: thrown in a job (at "));
        assert!(one("(async () => { await null; null.x; })()").contains("cannot read property"));
        assert_eq!(one("Promise.reject('only text')"), "unhandled rejection: only text");
        assert_eq!(one("Promise.reject({ toString() { throw 1; } })"), "unhandled rejection: a value that cannot be shown");
        assert_eq!(lines("1"), Vec::<String>::new(), "reported in its own entry only");
        // Handled: at once, by an await's try, by a job later in the same entry.
        for code in [
            "Promise.reject(new Error('caught')).catch(() => {})",
            "(async () => { try { await Promise.reject(new Error('caught')); } catch (e) {} })()",
            "const p = Promise.reject(new Error('caught')); Promise.resolve().then(() => p.catch(() => {}))",
            "const f = async () => { throw new Error('caught'); }; (async () => { try { await f(); } catch (e) {} })()",
        ] {
            assert_eq!(lines(&format!("{{ {code} }}")), Vec::<String>::new(), "{code}");
        }
        // Many in one entry: the first by their reasons, the rest counted.
        let many = lines("for (let i = 0; i < 20; i++) Promise.reject(new Error('n' + i))");
        assert_eq!(many.len(), MAX_REJECTIONS + 1);
        assert!(many[0].starts_with("unhandled rejection: Error: n0") && many[MAX_REJECTIONS] == "unhandled rejection: 12 more in the same entry", "{many:?}");
        // An event's are lines of its log, after its handlers' own.
        let e = h.event("reject", &serde_json::json!({})).unwrap();
        assert_eq!(e.log.len(), 2, "{:?}", e.log);
        assert!(e.log[0] == "ok reject" && e.log[1].starts_with("✗ unhandled rejection: TypeError: thrown in a job"), "{:?}", e.log);
        assert_eq!(h.event("click", &serde_json::json!({})).unwrap().log, vec!["ok click"]);
        // A module that throws while it loads is the load's error (and a module's top-level await is waited for).
        let runtime = "export const defineGame = (g) => g; export function installNodeHost(game) { globalThis.__threert = { describe: () => JSON.stringify({ handlers: [], frame: false, keys: {}, late: game.late }) }; }";
        let err = ScriptHost::from_modules("await null; throw new Error('no game'); export default {};", runtime).err().unwrap();
        assert!(format!("{err}").contains("no game"), "{err}");
        let h = ScriptHost::from_modules("const late = await Promise.resolve().then(() => 5); export default { late };", runtime).unwrap();
        assert_eq!(h.info["late"], 5);
    }

    #[test]
    fn budgets_and_errors() {
        let h = ScriptHost::new(GAME).unwrap();
        let t = Instant::now();
        let err = h.call(100, |ctx| ctx.eval::<(), _>("spin()")).unwrap_err();
        assert!(t.elapsed().as_millis() < 2000, "interrupted");
        assert!(format!("{err}").contains("interrupted"), "{err}");
        let err = h.call(100, |ctx| ctx.eval::<(), _>("null.x")).unwrap_err();
        assert!(format!("{err}").contains("cannot read property"), "{err}");
        let err = h.call(100, |ctx| ctx.eval::<(), _>("bad()")).unwrap_err();
        assert!(format!("{err}").contains("engine.material"), "{err}");
        // still usable afterwards
        assert!(h.setup("r", None).is_ok());
    }
}