Hooks
A hook is an event-typed slot: a host-declared plug-point whose accepts is the event-handler type (application/x-xript-hook), and whose fills are handlers the host calls when the event fires. Hooks are the reverse of bindings — bindings let scripts call the host; an event slot lets the host call scripts. Together, they form a bidirectional communication channel between the application and its mods.
This document covers hook declaration, lifecycle phases, registration, invocation, and the runtime conventions that govern hook behavior. Those conventions are the semantics of the event slot type; everything below applies whether an event is declared as a slot or, for back-compat, in the deprecated standalone hooks section.
The standalone
hooksconcept is deprecated. A lifecycle event is an event-typed slot, and firing it means calling that slot’s fills. Thehooksfield remains allowed (hosts still fire hooks and runtimes still dispatch them), but new manifests should declare events as slots and let mods fill them. The declaration form below documents the back-compat surface; the conventions it describes carry over unchanged to event slots.
Declaration
Section titled “Declaration”A host declares an event as a slot whose accepts is the event-handler type. The deprecated standalone form declares it in the manifest’s hooks section instead:
{ "hooks": { "playerDamage": { "description": "Fired when the player takes damage.", "params": [ { "name": "amount", "type": "number", "description": "Damage amount." }, { "name": "source", "type": "string", "description": "What caused the damage." } ] } }}Either form requires a description. Like bindings, if a modder can’t understand what an event does, it may as well not exist. A mod fills an event slot by naming a handler export ({ "handler": "onPlayerDamage" }); the script-side registration helpers below are the runtime’s convenience over that wiring.
Lifecycle Phases
Section titled “Lifecycle Phases”Hooks can optionally declare lifecycle phases. Phases let scripts intervene at specific points in a host operation:
{ "hooks": { "save": { "description": "Fired during the save lifecycle.", "phases": ["pre", "post", "done", "error"], "params": [ { "name": "data", "type": "SaveData", "description": "The save payload." } ] } }}The four standard phases are:
| Phase | When | Typical Use |
|---|---|---|
pre |
Before the operation | Validation, modification, cancellation |
post |
After the operation, can modify | Result transformation, interception |
done |
After all post-processing, sealed | Logging, notifications, observation |
error |
When the operation fails | Error recovery, fallback behavior |
Phases are optional. A hook without phases is a simple notification — it fires, handlers run, done. The host controls which phases it declares and in what order it fires them.
Registration (Script Side)
Section titled “Registration (Script Side)”Scripts register handlers via the hooks global object injected by the runtime.
Simple hooks (no phases):
Section titled “Simple hooks (no phases):”hooks.playerDamage((amount, source) => { log(`Player took ${amount} damage from ${source}`);});Phased hooks:
Section titled “Phased hooks:”hooks.save.pre((data) => { log("About to save: " + data.filename);});
hooks.save.post((data) => { log("Save complete: " + data.filename);});Multiple scripts can register handlers for the same hook (or phase). Handlers run in registration order.
Invocation (Host Side)
Section titled “Invocation (Host Side)”The host fires hooks through the runtime’s fireHook method:
// Simple hookconst results = runtime.fireHook("playerDamage", { amount: 25, source: "trap" });
// Phased hookconst preResults = runtime.fireHook("save", { phase: "pre", data: savePayload });const postResults = runtime.fireHook("save", { phase: "post", data: savePayload });fireHook returns an array of results from all registered handlers, in registration order. If no handlers are registered, it returns an empty array.
A Rust XriptPool fires a hook at every handler registered for it on any worker, whenever it was registered (by a mod’s entry, an export, another handler, or host code), and returns the results in the order one runtime would: every handler in registration order across the pool, then every hook fill in its mod’s load order.
Firing one phase
Section titled “Firing one phase”A fire with a phase runs only the handlers registered through hooks.<hook>.<phase>(fn), in registration order, and passes data as the JavaScript runtimes do: a plain object spreads to its values in key order, any other value is the one argument, and no data passes no arguments. A phase the hook does not declare, a hook with no phases, and an unknown hook all run nothing and return an empty array; the fire is refused, not an error. Hook fills are unphased and never run on a phased fire. An empty phase ("") is no phase: the fire runs exactly what an unphased fire runs, hook fills included.
Each runtime spells it with its own host idiom:
| Runtime | Phased fire |
|---|---|
| JavaScript, Node | runtime.fireHook(hook, { phase, data }) |
| Rust | runtime.fire_hook_phase(hook, phase, Option<&Value>), plus fire_hook_phase_tagged and _async twins; also on XriptHandle, BlockingHandle, and XriptPool |
| C# | runtime.FireHook(hook, new FireHookOptions { Phase, Data }) |
spec/hook-phase-tests.json is the conformance corpus every runtime asserts. What an unphased fire does with a phased hook’s handlers is not part of that contract.
Event Delivery
Section titled “Event Delivery”Hook dispatch and the top-level events catalog share one fan-out contract: handlers run in registration order, an object payload spreads to positional arguments, any other payload is passed as a single argument, and a handler that throws contributes undefined without stopping the others. The events side is a full bus. Mods subscribe, cancel, emit the events the host lets them emit, and read who is listening, while the host emits, declares events at runtime, and clears one mod’s subscriptions.
The bus is one JavaScript source that every runtime installs verbatim into its sandbox. Its behaviour is the same in all four runtimes by construction, and spec/event-bus-tests.json pins it.
A Rust XriptPool is several runtimes with one bus. Its workers share the catalogue, the namespace claims, and every subscription’s record, so a declaration, a claim, a cancellation or a question about who is listening answers the same whichever worker asks. A handler runs on the worker that subscribed it. An emit, from the host or from a mod, walks the subscriptions in subscription order and has each handler’s worker run it, so it reaches every worker with the ordering and results of a single runtime. A worker waiting on another serves what is asked of it meanwhile, and the wait counts against the emitting call’s own budget.
How a worker waits depends on how the emit was made. A host emit on a worker built in RuntimeMode::Async awaits each answer from another worker as a parked call, so the worker’s thread keeps running its other calls meanwhile. A mod’s events.emit is a synchronous call that returns the handlers’ results, so on any worker the thread waits until the answer arrives, serving requests addressed to it while it waits. A mod that must not hold the worker uses events.emitAsync, which delivers the same results as a promise and, on a RuntimeMode::Async worker, awaits each answer from another worker the way a host emit does.
A payload a handler on another worker receives, and the result it returns, cross between the workers as JSON with the special-value markers always on, whatever value_fidelity says, since that hop never reaches the host. undefined (an own property holding it included), NaN, the infinities and -0 therefore arrive exactly as they left. A function cannot leave the interpreter that holds it, so it arrives as undefined: no callback it stands for could be called where it lands, and the handler never sees a marker as data. The same holds for a function in a result crossing back. A payload JSON cannot hold at all, such as one with a cycle, cannot reach a handler on another worker, and the emit throws.
Subscribing
Section titled “Subscribing”const sub = events.on("player.damaged", (amount, source) => { log(`took ${amount} damage from ${source}`);});
sub.id; // a storable string tokensub.event; // "player.damaged"sub.cancel(); // { cancelled: true, spent: false }
events.subscribe("level.loaded", () => { /* alias of events.on */ });events.once("level.loaded", () => { /* runs at most once */ });A handler receives an object payload’s values as positional arguments, in the order the payload object holds its keys: the host above emitted { amount, source }, so the handler takes (amount, source). A payload that is not a plain object arrives as one argument, and no payload as none. Typegen types handlers the same way, from the payload type’s declared fields in order, so a host that builds each payload in its declared field order matches the generated types. The Rust runtime currently orders an object payload’s keys alphabetically, whatever order the host wrote them in.
events.on(id, handler, options?) and events.once(id, handler, options?) return a frozen { id, event, cancel }. id is a string the mod may store and pass to events.cancel later, and cancel() is shorthand for that. once spends its subscription before the handler runs, so a handler that throws does not resubscribe.
options.match filters by payload: the handler runs only when every listed top-level payload key is strictly equal (===) to the given value. An empty match always runs; a payload that is not an object never satisfies a non-empty one. When the event declares keys, a match key outside them is refused at subscribe time.
events.on("stat.changed", (instanceId, value) => redraw(value), { match: { instanceId: "hero" } });Subscribing refuses, with a BindingError, an empty id, a non-function handler, an id that resolves to no declared entry, and a family shape such as stat.<instanceId>.changed (subscribe to a concrete id instead). An event whose entry declares a capability admits a subscriber only when the granted set satisfies it via grantedSatisfies (mode lattice plus prefix subsumption, see Capability Model); otherwise subscribing throws a CapabilityDeniedError. An event with no capability admits any subscriber.
Ownership
Section titled “Ownership”Every subscription belongs to the mod that made it: the runtime’s caller context at the moment on ran. The runtime enters a mod’s context while it runs that mod’s entry script, an export invoked in that mod’s namespace, or a handler that mod subscribed. A call that awaits and resumes keeps its owner across the await, whatever other calls ran meanwhile (see Caller Identity). Code no mod owns, such as host-run execute, a continuation host-run code queued, or a continuation whose queuing mod the runtime cannot tell apart (see Caller Identity), has no owner, and its subscriptions are untracked. Having no owner grants no privilege: untracked code is treated like any other owner.
events.cancel(idOrSubscription)returns{ cancelled, spent }. The first cancel answers{ cancelled: true, spent: false }and a repeat answers{ cancelled: false, spent: false }. For aoncethat has already fired it answers{ cancelled: false, spent: true }. Cancelling another owner’s subscription needs theevents.offcapability.events.off(id)drops the caller’s own listeners on that event and returns how many. Withevents.offit drops everyone’s.events.cancelOwned()cancels every subscription the caller owns and returns how many.events.cancelOwned(owner)names another owner, which needsevents.off;nullnames the untracked listeners.
The host side has cancelOwned(modId) (cancel_owned in Rust, CancelOwned in C#). It re-runs one mod’s scripts without unloading the mod: cancel what it subscribed, then run its scripts again.
Emitting
Section titled “Emitting”The host emits with emit, the sibling of fireHook:
const results = runtime.emit("player.damaged", { amount: 25, source: "trap" });The id may be any concrete id that resolves to a declared entry, and an id that does not resolve delivers to nobody and returns []. Handlers run in subscription order against a snapshot taken when the emit starts. A listener cancelled during the dispatch is skipped, and one added during it waits for the next emit. Results come back in order.
A subscriber can return a promise, and the host emits two ways that differ only in how they read one, exactly as a hook fires two ways (see Async Hooks):
| Runtime | Synchronous emit | Asynchronous emit |
|---|---|---|
@xriptjs/runtime, @xriptjs/runtime-node |
emit |
emitAsync |
xript-runtime (Rust) |
emit, emit_tagged |
emit_async, emit_tagged_async |
Xript.Runtime (C#) |
Emit |
EmitAsync |
The asynchronous emit runs every subscriber as the synchronous emit does, then awaits each result that is a native promise before reading it, keeping subscription order; a promise that rejects reads as undefined and its reason goes to the handler-failure sink with kind event. The synchronous emit never hands the host a promise’s internals: in @xriptjs/runtime and @xriptjs/runtime-node the result reads as undefined and the sink hears an AsyncHandlerError naming emitAsync(); Rust and C# settle it once the job queue has run, and their asynchronous emit does the same: a promise that rejected reads as undefined and is reported like a throw, and one still pending (waiting on something the host settles later, such as an async binding) reads as undefined and is reported as an AsyncHandlerError, so an emit never waits on the host. spec/hook-error-tests.json pins both emits (emitAsync steps, and promise: true on an emit step).
A mod may emit only an event declared with "emit": "mods":
const results = events.emit("note.posted", { text: "hello" });events.emit follows the same fan-out rules and returns the handlers’ results. Emitting a host-only event, an undeclared id, or a family shape throws a BindingError.
events.emitAsync(id, data?) is emit delivered as a promise:
const results = await events.emitAsync("note.posted", { text: "hello" });It takes the same arguments and makes the same checks, runs the handlers in the same order against the same snapshot, under the same budget, and resolves to exactly the array emit would return. A refused emit does not throw: the promise rejects with the BindingError emit would have thrown, naming events.emitAsync. Where every handler can run without waiting on another runtime, which is everywhere except a Rust XriptPool worker built in RuntimeMode::Async, the handlers run before emitAsync returns and the promise is already resolved. On such a pool worker a handler held by another worker is awaited rather than blocked on, so the worker’s thread keeps running other calls meanwhile, and a call whose budget runs out while it waits rejects with a BindingError (the call itself fails with its timeout or cancellation, as any parked call does). A promise settles when the runtime runs its job queue, as any other promise in that runtime does.
Resolution
Section titled “Resolution”events.describe(id) returns the catalogue entry an id resolves to, or null. It looks for an exact match on an entry’s id first. If none matches, it walks the catalogue in order and tests every pattern id and every addressed form segment by segment. Segments are the .-separated parts of the id, and a <name> segment matches exactly one non-empty segment. on and emit accept any concrete id that resolves this way, and the listeners of each concrete id are kept apart.
{ "id": "stat.changed", "description": "Fired after any stat changes.", "addressed": ["stat.<instanceId>.changed"] }Here stat.hero.changed resolves to stat.changed, and a subscriber to stat.hero.changed hears only the hero. The host emits the generic and the addressed form side by side.
Limitation. A
<name>covers one segment, so a subject id that itself contains a.does not fit:stat.hero.hp.changeddoes not resolve againststat.<instanceId>.changed. Pick subject ids without dots, or declare a shape with as many segments as the subject has.
The catalogue at runtime
Section titled “The catalogue at runtime”events.declare(entry) adds an event to the catalogue. The entry has the same shape as a manifest events entry, and a mod needs the events.declare capability to call it. Once declared, the event can be subscribed to, emitted (for an "emit": "mods" entry), and described. The declarer may declare the same id again to replace its entry, but no one may redeclare an id the manifest declares or another owner declared. The host has declareEvent(entry) (declare_event, DeclareEvent) with no capability check. A malformed entry is refused, including one that breaks the id grammar, has an unknown field, gives an emit other than "host" or "mods", or disagrees between pattern and <name> segments.
events.catalogue() returns every entry, manifest-declared first and then runtime-declared, in declaration order. Typegen types the manifest-declared ones; an id declared at runtime is untyped.
Namespaces
Section titled “Namespaces”events.claim(prefix) claims an event-id namespace for the calling mod and needs events.claim. It answers true when the caller now holds the prefix (or already did), and false when another mod holds it. Untracked code cannot claim. events.namespaces() lists every claim as { prefix, owner }, longest prefix first. events.ownerOf(id) returns the owner of the most specific claim covering the id, or null. A claim covers an id when the id equals the prefix or continues it at a . boundary, so stat covers stat.hero.changed and does not cover status.changed. Claims are declarative and never checked at emit; they tell mods who speaks for a namespace.
Introspection
Section titled “Introspection”events.names(): every event id currently holding a listener, sortedevents.listenerCount(id?): listeners on one event, or on all of themevents.subscribers(id):{ event, total, owners: [{ owner, count }], repeated, untracked }, with owners sorted.repeatedlists the owners holding more than one subscription on the event anduntrackedcounts the listeners with no owner
Bus capabilities
Section titled “Bus capabilities”| Capability | Lets a mod |
|---|---|
events.off |
cancel, off, or cancelOwned another owner’s subscriptions |
events.claim |
claim an event-id namespace |
events.declare |
declare an event at runtime |
A host declares these in its capabilities to make them grantable. Prefix subsumption applies as usual, so a grant of events covers all three.
A host’s own bus
Section titled “A host’s own bus”A host that already runs an event bus, with its own catalogue and its own answer shapes, can give mods that bus as events instead of this one. It declares the bus as a top-level binding named events (a namespace or a function) and constructs the runtime in host events mode: hostEvents: true in @xriptjs/runtime and @xriptjs/runtime-node, RuntimeOptions::host_events in Rust, RuntimeOptions.HostEvents in C#.
- The binding is
events. The runtime installs it as the script-visibleeventsglobal and does not install the built-in bus there. It is an ordinary binding in every other respect: capability-gated per member, called with the caller’s identity, values crossing as for any binding, and sealed once the runtime is built, so no mod can reassign, redefine, or delete it or any of its members. - Construction is refused with a host events error when the manifest declares no top-level
eventsbinding. - A catalogue is allowed. A manifest
eventscatalogue can sit beside the binding to document the host’s events. It is wired to no script-visible bus, and the binding is stilleventsin every runtime. - The built-in bus’s host API is refused. No script can subscribe to the built-in bus in this mode, so a host
emit,declareEvent, orcancelOwned(in Rust also the tagged and async forms, on a runtime, a handle, or a pool) throws a host events error instead of answering an empty list that looks like success. The host emits on its own bus. - Hooks are untouched.
hooks, hook fills, and event-typed slots dispatch through the hook path, never through the bus, and behave as before. - Typegen types the binding. Whenever the manifest declares a top-level
eventsbinding,xript typegen(host-facing and--ambient) typeseventsas that binding and emits none of the built-in bus’s API. A catalogue beside it still yields theXriptEventsid-to-payload map. - Off by default. Without the option every runtime installs the built-in bus as
eventsover a host’seventsbinding, with or without a catalogue, and raisesbinding-shadows-builtin(see Built-in Globals).
spec/host-events-tests.json pins the mode in all four runtimes.
Capability Gating
Section titled “Capability Gating”Hooks can require capabilities, the same model as bindings:
{ "hooks": { "save": { "description": "Fired during the save lifecycle.", "phases": ["pre", "post", "done", "error"], "capability": "persistence" } }}A script without the persistence capability cannot register handlers for this hook. Attempting to register throws a CapabilityDeniedError.
Async Hooks
Section titled “Async Hooks”Hooks can be declared async, controlled by the host:
{ "hooks": { "dataSync": { "description": "Fired when data synchronization occurs.", "async": true } }}A handler (or a hook fill’s export) can return a promise whether or not the hook declares async. Every runtime fires a hook two ways, and they differ only in how they read a promise result:
| Runtime | Synchronous fire | Asynchronous fire |
|---|---|---|
@xriptjs/runtime, @xriptjs/runtime-node |
fireHook |
fireHookAsync |
xript-runtime (Rust) |
fire_hook, fire_hook_phase |
fire_hook_async, fire_hook_phase_async |
Xript.Runtime (C#) |
FireHook |
FireHookAsync |
The asynchronous fire runs every handler exactly as the synchronous fire does, in registration order and all within the fire, then awaits each result that is a native promise before reading it. Results keep registration order whatever order the promises settle in. A promise that rejects reads as undefined, and its reason goes to the handler-failure sink (see Handler Errors). A promise still pending once nothing is left that could settle it (the job queue has run, and no async binding a handler is waiting on has host work outstanding) reads as undefined and is reported as an AsyncHandlerError, so a fire never waits on a promise that can never settle; one waiting on an async binding is awaited until that binding settles and its continuations have run. The asynchronous emit does the same. A thenable that is not a native promise is read as the object it is, as it always was.
The synchronous fire never hands the host a promise’s internals: a promise used to cross as an empty object, which carried nothing. What it does instead follows what each runtime’s synchronous execute does with a promise result (see execution.md and spec/async-tests.json):
- A runtime that cannot settle a promise on a synchronous call (
@xriptjs/runtime,@xriptjs/runtime-node) reads that handler’s result asundefinedand reports anAsyncHandlerErrorto the handler-failure sink, naming the asynchronous fire to use instead. - A runtime that settles promise results on its synchronous entry points (Rust, C#) settles this one too. A settled promise that rejected reads as
undefinedand is reported like a throw. Both read a promise still pending once the job queue has drained asundefinedand report anAsyncHandlerError.
spec/hook-error-tests.json pins both fires (promise: true steps).
Execution Limits
Section titled “Execution Limits”A hook or event fire runs under an execution budget: the script timeout, fresh for each fire and shared by the handlers that fire runs. A fire that overruns stops with the execution-limit error, whichever handler was running. Hooks can override the manifest-level limits:
{ "hooks": { "frameTick": { "description": "Fired every frame.", "limits": { "timeout_ms": 5 } } }}Per-hook limits are useful when different hooks have different performance requirements. A frame tick handler needs a tight timeout; a save handler can take longer.
An event slot declares its budget the same way, in the slot’s own limits. A fire’s budget is the hook’s or slot’s limits.timeout_ms where it declares one and the manifest’s timeout_ms otherwise, clamped by the embedder’s hard limit either way; no other slot’s budget enters into it. An event slot’s fills are calls into that slot’s fills, so each runs under the slot’s budget too (see execution.md).
Where the runtimes stand:
| Runtime | Fire budget | A hook’s or event slot’s own limits.timeout_ms |
|---|---|---|
@xriptjs/runtime, @xriptjs/runtime-node |
opt-in: create the runtime with budgetHandlers: true. Without it handlers run unbudgeted, and the first handler to run past the script timeout raises the unbudgeted-handler deprecation (see deprecations.md) |
honoured |
xript-runtime (Rust) |
always | honoured |
Xript.Runtime (C#) |
always | honoured |
Error Handling
Section titled “Error Handling”Handler Errors
Section titled “Handler Errors”When a hook handler throws, the runtime catches the error, gives that handler’s result as undefined, and runs the remaining handlers. One handler’s error does not prevent other handlers from running. The same holds for a hook fill whose export throws, an events subscriber that throws, and a fragment lifecycle handler that throws (see fragments.md).
Nothing is logged by default. A host that wants to hear these failures passes a handler-failure sink when it creates the runtime:
| Runtime | Option | Notice |
|---|---|---|
@xriptjs/runtime, @xriptjs/runtime-node |
onHookError |
HookError |
xript-runtime (Rust) |
RuntimeOptions::on_hook_error |
HookError |
Xript.Runtime (C#) |
RuntimeOptions.OnHookError |
HookError |
The sink is called once for each failure, as it happens, with:
kind:hookfor a hook handler or hook fill,eventfor aneventssubscriber,fragmentfor a fragment lifecycle handlerid: the hook id, the event id the subscriber subscribed to, or the fragment idphase: the phase a phased fire named, or the fragment lifecycle (mount,update, …); otherwise nullmod: the mod that registered the handler (or filled the slot), or null for host codename,message,stack: the thrown value, read the same way in every runtime.nameis itsnamewhen that is a string and"Error"otherwise;messageis itsmessagewhen that is a string and""otherwise;stackis itsstackwhen that is a string and null otherwise, and its text is engine-specific. A getter that throws while being read leaves that field at its default. A thrown non-object reads as"Error", its text, and a null stack
This is how a capability refusal inside a handler becomes visible: the handler’s CapabilityDeniedError arrives at the sink with the mod that raised it. A hook fill’s failure is reported with the hook as its id and the filling mod as its mod; its name may be the runtime’s export-invocation error rather than the one the export threw, and its message carries the thrown message.
The sink changes nothing about the fire: results are the same with it, without it, and with a sink that throws, which the runtime absorbs. Without a sink no thrown value is ever read, so a getter on an error object runs exactly as often as it did before the sink existed. spec/hook-error-tests.json is the conformance corpus every runtime asserts, running each case all three ways.
The fire itself can still fail, and then the host hears about it rather than getting an empty result. Hook data or an event payload that cannot cross into the sandbox, such as a retained callback the host already released, fails the fire with the callback error (reason released) before any handler runs. A fire stopped by its budget fails with the execution-limit error. Every runtime surfaces both; spec/callback-tests.json pins the first.
Commands do the opposite, deliberately. Swallowing an error is right for a fire-and-forget lifecycle notification and wrong for a user-triggered action, so a failing command handler propagates its error to the caller rather than contributing undefined. See commands.md.
Unhandled Rejections
Section titled “Unhandled Rejections”A promise in the sandbox can reject with nobody listening: an async binding whose promise the script never awaits, a then callback that throws with no catch after it, an async function called and dropped. Every runtime contains such a rejection, so it cannot stop the host, and by default it is silent. A host that wants to hear it passes a rejection sink:
| Runtime | Option | Notice |
|---|---|---|
@xriptjs/runtime |
onUnhandledRejection |
never called (the QuickJS-WASM build has no rejection tracker; see below) |
@xriptjs/runtime-node |
onUnhandledRejection |
UnhandledRejection |
xript-runtime (Rust) |
RuntimeOptions::on_unhandled_rejection |
UnhandledRejection |
Xript.Runtime (C#) |
RuntimeOptions.OnUnhandledRejection |
UnhandledRejection |
The notice carries mod, the mod whose code was running when the promise rejected (null for host code or where the runtime cannot know), and the reason’s name, message, and stack, read as a handler’s thrown value is read.
A rejection is unhandled when it still has no handler after the host call that queued it has returned with its job queue drained. A promise rejected and then given a handler by a later job in the same call is never reported. Each unhandled rejection is reported once, in the order the promises rejected. A sink that throws changes nothing. spec/rejection-tests.json is the conformance corpus.
Engine fidelity. Tracking a rejection needs the engine to say when a rejected promise gains its first handler, and the runtimes get that from different places:
xript-runtimeuses the QuickJS host promise rejection tracker thatrquickjsexposes, collects rejections during each call with the calling mod, and reports what is left as the call ends.Xript.Runtimeuses Jint’sPromiseRejectionTrackerthe same way. Its async bindings settle before the script sees them, so a rejected async binding throws at the call instead of handing back a rejected promise.@xriptjs/runtime-nodeshares the host’s V8 isolate and hears rejections from the process’sunhandledRejectionevent, which arrives after the call has returned. It cannot know the mod, somodis always null, and it reads the reason without running any script accessor, sostackis null.@xriptjs/runtimecannot track rejections. The QuickJS-WASM build it runs on does not expose the engine’s rejection tracker, and nothing a script can observe stands in for it (awaitattaches a handler without calling anything a sandbox could watch). It acceptsonUnhandledRejectionso a host written for every runtime still type-checks, warns once at creation through the console handler that the sink will never be called, and never calls it. It skipsspec/rejection-tests.json.
Error Types
Section titled “Error Types”| Error | When Thrown |
|---|---|
CapabilityDeniedError |
Registration attempted without the required capability |
AsyncHandlerError |
Never thrown. The name the handler-failure sink reports for a handler’s promise that a synchronous fire could not read |
Hook Granularity
Section titled “Hook Granularity”Filtering and scoping are the host’s responsibility, not the hook system’s. Instead of one save hook with filter parameters, hosts should expose granular hooks when distinction matters:
{ "hooks": { "save": { "description": "Any save operation.", "phases": ["pre", "post", "done", "error"] }, "autosave": { "description": "Automatic background saves.", "phases": ["pre", "post", "done"] }, "manualSave": { "description": "Player-initiated saves.", "phases": ["pre", "post", "done", "error"] } }}Scripts register for what they care about. The host controls granularity by how many hooks it exposes.
Naming Conventions
Section titled “Naming Conventions”Hook names should follow the same conventions as binding names:
- camelCase (
playerDamage,onSave,frameTick) - Noun or event-oriented (
playerDamage,levelComplete,inventoryChange) - Not verbs unless describing an action (
save,loadare fine as they describe the operation the hook wraps)
Manifest-to-TypeScript Mapping
Section titled “Manifest-to-TypeScript Mapping”The typegen tool generates handler registration types from hooks:
Simple hook:
Section titled “Simple hook:”declare namespace hooks { function playerDamage(handler: (amount: number, source: string) => void): void;}Phased hook:
Section titled “Phased hook:”declare namespace hooks { namespace save { function pre(handler: (data: SaveData) => void): void; function done(handler: (data: SaveData) => void): void; function post(handler: (data: SaveData) => void): void; function error(handler: (data: SaveData) => void): void; }}Async hook:
Section titled “Async hook:”Async hooks wrap the handler return type in Promise:
declare namespace hooks { function dataSync(handler: (payload: SyncPayload) => Promise<void>): void;}Events:
Section titled “Events:”An event’s handler arguments come from its payload the way the runtimes deliver them: a payload type with declared fields becomes those fields, positionally, in declaration order; a primitive or array payload is one argument; no payload is none. emit and emitAsync still take the payload as one object.
interface XriptEventArgs { "player.damaged": [amount: number, source: string]; [id: `stat.${string}.changed`]: [instanceId: string, value: number];}
declare namespace events { function on<K extends XriptEventId>(id: K, handler: (...args: XriptEventArgs[K]) => unknown, options?: XriptSubscribeOptions): XriptSubscription; function emit<K extends XriptModEventId>(id: K, data?: XriptEvents[K]): unknown[]; function emitAsync<K extends XriptModEventId>(id: K, data?: XriptEvents[K]): Promise<unknown[]>;}