use std::fmt::Write as _;
use crate::intrinsics::p3::async_future::AsyncFutureIntrinsic;
use crate::intrinsics::p3::async_stream::AsyncStreamIntrinsic;
use crate::intrinsics::p3::async_task::AsyncTaskIntrinsic;
use crate::intrinsics::p3::waitable::WaitableIntrinsic;
use crate::intrinsics::resource::ResourceIntrinsic;
use crate::intrinsics::{Intrinsic, RenderIntrinsicsArgs};
use crate::source::Source;
use crate::uwriteln;
use wasmtime_environ::Trap;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum ComponentIntrinsic {
GlobalInstanceFlagsMap,
GlobalStoreTrap,
StopInstance,
WhenIdle,
CheckIdle,
CallTracking,
ReportTrap,
GlobalStoreAsyncState,
CheckForDeadlock,
TrackHostOperation,
NormalizeCoreTrap,
PostReturnTrap,
GuardPostReturn,
CheckMayLeave,
GuardMayLeave,
RecordTrap,
GuardTrap,
GlobalAsyncStateMap,
GetOrCreateAsyncState,
BackpressureInc,
BackpressureDec,
ComponentAsyncStateClass,
ComponentStateSetAllError,
}
impl ComponentIntrinsic {
pub fn get_global_names() -> impl IntoIterator<Item = &'static str> {
[
"_postReturnTrap",
"_guardPostReturn",
"_stopInstance",
"_reportTrap",
"_whenIdle",
"_checkIdle",
"_isIdle",
"CALL_TRACKING",
]
}
pub fn name(&self) -> &'static str {
match self {
Self::GlobalInstanceFlagsMap => "INSTANCE_FLAGS",
Self::GlobalStoreTrap => "STORE_TRAP",
Self::StopInstance => "_stopInstance",
Self::ReportTrap => "_reportTrap",
Self::WhenIdle => "_whenIdle",
Self::CheckIdle => "_checkIdle",
Self::CallTracking => "CALL_TRACKING",
Self::GlobalStoreAsyncState => "STORE_ASYNC_STATE",
Self::CheckForDeadlock => "_checkForDeadlock",
Self::TrackHostOperation => "_trackHostOperation",
Self::NormalizeCoreTrap => "_normalizeCoreTrap",
Self::PostReturnTrap => "_postReturnTrap",
Self::GuardPostReturn => "_guardPostReturn",
Self::CheckMayLeave => "_checkMayLeave",
Self::GuardMayLeave => "_guardMayLeave",
Self::RecordTrap => "_recordTrap",
Self::GuardTrap => "_guardTrap",
Self::GlobalAsyncStateMap => "ASYNC_STATE",
Self::GetOrCreateAsyncState => "getOrCreateAsyncState",
Self::BackpressureInc => "backpressureInc",
Self::BackpressureDec => "backpressureDec",
Self::ComponentAsyncStateClass => "ComponentAsyncState",
Self::ComponentStateSetAllError => "_ComponentStateSetAllError",
}
}
pub fn render(&self, output: &mut Source, render_args: &RenderIntrinsicsArgs<'_>) {
match self {
Self::GlobalInstanceFlagsMap => {
let var_name = render_args.require_intrinsic(Self::GlobalInstanceFlagsMap);
uwriteln!(output, r#"const {var_name} = new Map();"#);
}
Self::GlobalStoreTrap => {
let var_name = render_args.require_intrinsic(Self::GlobalStoreTrap);
let on_trap = if render_args.instantiation_occurred {
"options?.onTrap ?? null"
} else {
"null"
};
uwriteln!(
output,
r#"const {var_name} = {{ error: null, stopped: false, resourceTables: [], stopWaiters: new Set(), disposed: new WeakSet(), onTrap: {on_trap}, trapsReported: new WeakSet() }};"#
);
}
Self::WhenIdle => {
let when_idle_fn = render_args.require_intrinsic(Self::WhenIdle);
render_args.require_intrinsic(Self::CheckIdle);
let scheduler = render_args.require_intrinsic(Self::GlobalStoreAsyncState);
output.push_str(&format!(
r#"
function {when_idle_fn}() {{
if (_isIdle()) {{ return Promise.resolve(); }}
return new Promise((resolve) => {{ {scheduler}.idleWaiters.add(resolve); }});
}}
"#
));
}
Self::CallTracking => {
let runtime_error =
render_args.require_intrinsic(Intrinsic::WebAssemblyRuntimeError);
let store = render_args.require_intrinsic(Self::GlobalStoreTrap);
let tasks =
render_args.require_intrinsic(AsyncTaskIntrinsic::GlobalAsyncCurrentTaskMap);
output.push_str(
&include_str!("call_tracking.js")
.replace("$STORE", store)
.replace("$TASKS", tasks)
.replace("$RUNTIME_ERROR", runtime_error),
);
}
Self::CheckIdle => {
let check_idle_fn = render_args.require_intrinsic(Self::CheckIdle);
let store = render_args.require_intrinsic(Self::GlobalStoreTrap);
let scheduler = render_args.require_intrinsic(Self::GlobalStoreAsyncState);
let tasks =
render_args.require_intrinsic(AsyncTaskIntrinsic::GlobalAsyncCurrentTaskMap);
let check_calls = if render_args.call_tracking {
let tracking = render_args.require_intrinsic(Self::CallTracking);
format!("{tracking}.check();")
} else {
String::new()
};
output.push_str(&format!(
r#"
function _isIdle() {{
if ({store}.error !== null) {{ return true; }}
for (const [componentIdx, metas] of {tasks}) {{
if (componentIdx !== -1 && metas.length > 0) {{ return false; }}
}}
return true;
}}
function {check_idle_fn}() {{
{check_calls}
if ({scheduler}.idleWaiters.size === 0 || !_isIdle()) {{ return; }}
const waiters = [...{scheduler}.idleWaiters];
{scheduler}.idleWaiters.clear();
for (const resolve of waiters) {{ resolve(); }}
}}
"#
));
}
Self::ReportTrap => {
let report_trap_fn = render_args.require_intrinsic(Self::ReportTrap);
let store = render_args.require_intrinsic(Self::GlobalStoreTrap);
let normalize_core_trap =
render_args.intrinsic_call(Self::NormalizeCoreTrap, "err");
let runtime_error_class =
render_args.require_intrinsic(Intrinsic::WebAssemblyRuntimeError);
output.push_str(&format!(
r#"
function {report_trap_fn}(err) {{
// Stopping an instance goes through the same path as a trap
if ({store}.onTrap === null || {store}.stopped) {{ return; }}
if (!(err instanceof {runtime_error_class}) || {store}.trapsReported.has(err)) {{ return; }}
{store}.trapsReported.add(err);
err = {normalize_core_trap};
// Not the runtime's to handle, whose own unwinding is under way
try {{ {store}.onTrap(err); }} catch (e) {{ queueMicrotask(() => {{ throw e; }}); }}
}}
"#
));
}
Self::StopInstance => {
let name = self.name();
let store = render_args.require_intrinsic(Self::GlobalStoreTrap);
let states = render_args.require_intrinsic(Self::GlobalAsyncStateMap);
let scheduler = render_args.require_intrinsic(Self::GlobalStoreAsyncState);
let tasks =
render_args.require_intrinsic(AsyncTaskIntrinsic::GlobalAsyncCurrentTaskMap);
let runtime_error =
render_args.require_intrinsic(Intrinsic::WebAssemblyRuntimeError);
let dispose = render_args.require_intrinsic(Intrinsic::SymbolDispose);
let own = render_args.require_intrinsic(ResourceIntrinsic::ResourceTableOwnFlag);
let flag = render_args.require_intrinsic(ResourceIntrinsic::ResourceTableFlag);
let streams = render_args.require_intrinsic(AsyncStreamIntrinsic::GlobalStreamMap);
let futures = render_args.require_intrinsic(AsyncFutureIntrinsic::GlobalFutureMap);
let rep_table = render_args.require_intrinsic(Intrinsic::RepTableClass);
uwriteln!(
output,
r#"
function {name}() {{
if ({store}.stopped) {{ return; }}
{store}.stopped = true;
const err = new {runtime_error}('component instance has been stopped');
{store}.error ??= err;
clearTimeout({scheduler}.deadlockCheck);
{scheduler}.deadlockCheck = null;
// Poison every component before releasing any continuation or
// calling host cleanup, which can reenter the instance.
for (const state of {states}.values()) {{ state.markTrapped(err); }}
for (const reject of {store}.stopWaiters) {{ reject(err); }}
{store}.stopWaiters.clear();
const pending = [...{tasks}.values()].flat().map(meta => meta.task);
for (const task of pending) {{ task.stop(err); }}
for (const state of {states}.values()) {{ state.stop(); }}
// Both ends of every stream and future, whichever side either end is on.
const errors = [];
for (const channels of [{streams}, {futures}]) {{
// data() is part of both inline and external table contracts.
for (let i = 2, data = channels.data(); i < data.length; i += 2) {{
const channel = data[i];
if (channel === undefined || channel === {rep_table}.FREE) {{ continue; }}
// (what a host has lifted is only a view of one of these)
try {{ channel.onInstanceStop?.(); }} catch (e) {{ errors.push(e); }}
}}
}}
// A capture table also contains borrowed resources. Only live
// owned handles belong to the guest; lifting an own back to the
// host removes its capture. Shared tables/reps are deduplicated.
const resources = new Set();
for (const {{ handles, captures }} of {store}.resourceTables) {{
for (let i = 2; i < handles.length; i += 2) {{
if ((handles[i] & {flag}) !== 0 || !(handles[i + 1] >= {own})) {{ continue; }}
const resource = captures.get(handles[i + 1] - {own});
if (resource !== undefined) {{ resources.add(resource); }}
}}
}}
for (const {{ captures }} of {store}.resourceTables) {{ captures.clear(); }}
{store}.resourceTables.length = 0;
for (const resource of resources) {{
// A guest holding it twice may have dropped the other handle.
if ({store}.disposed.has(resource)) {{ continue; }}
try {{ resource[{dispose}]?.(); }}
catch (error) {{ errors.push(error); }}
}}
if (errors.length) {{ throw new AggregateError(errors, 'failed to dispose stopped instance resources'); }}
}}
"#
);
}
Self::GlobalStoreAsyncState => {
let var_name = render_args.require_intrinsic(Self::GlobalStoreAsyncState);
uwriteln!(
output,
r#"const {var_name} = {{ deadlockCheck: null, pendingHostOperations: 0, idleWaiters: new Set() }};"#
);
}
Self::CheckForDeadlock => {
let check_for_deadlock_fn = render_args.require_intrinsic(Self::CheckForDeadlock);
let async_state_map = render_args.require_intrinsic(Self::GlobalAsyncStateMap);
let store_async_state = render_args.require_intrinsic(Self::GlobalStoreAsyncState);
let store_trap = render_args.require_intrinsic(Self::GlobalStoreTrap);
let runtime_error_class =
render_args.require_intrinsic(Intrinsic::WebAssemblyRuntimeError);
output.push_str(&format!(
r#"
function {check_for_deadlock_fn}() {{
if ({store_async_state}.deadlockCheck !== null || {store_trap}.error !== null) {{ return; }}
{store_async_state}.deadlockCheck = setTimeout(() => {{
{store_async_state}.deadlockCheck = null;
if ({store_trap}.error !== null || {store_async_state}.pendingHostOperations > 0) {{ return; }}
const suspendedTasks = new Set();
for (const state of {async_state_map}.values()) {{
if (state.hasPendingSchedulerWork()) {{
state.runTickLoop();
return;
}}
for (const meta of state.suspendedTaskMetas()) {{
suspendedTasks.add(meta.task);
}}
}}
const unresolvedRoots = new Set();
for (const task of suspendedTasks) {{
const root = task.getRootTask();
if (!root.isResolvedState()) {{ unresolvedRoots.add(root); }}
}}
if (unresolvedRoots.size === 0) {{ return; }}
const err = new {runtime_error_class}('wasm trap: deadlock detected: event loop cannot make further progress');
// Which tasks were waiting, and on whose behalf. The message stays
// exactly what the Canonical ABI calls for, so this rides alongside
// it: a deadlock reported from a real program is otherwise a bare
// sentence, and the state that produced it is gone by the time
// anyone reads the failure.
err.deadlockDetail = {{
pendingHostOperations: {store_async_state}.pendingHostOperations,
suspendedTasks: [...suspendedTasks].map((task) => ({{
taskID: task.id(),
componentIdx: task.componentIdx(),
state: task.taskState(),
rootTaskID: task.getRootTask().id(),
}})),
unresolvedRootTaskIDs: [...unresolvedRoots].map((root) => root.id()),
}};
{store_trap}.error = err;
for (const root of unresolvedRoots) {{
root.setErrored(err);
root.reject(err);
}}
for (const task of suspendedTasks) {{
if (!task.isResolvedState() && unresolvedRoots.has(task.getRootTask())) {{
task.setErrored(err);
task.reject(err);
}}
}}
// A deadlock is a trap of the whole store: no instance may be entered again.
for (const state of {async_state_map}.values()) {{ state.markTrapped(err); }}
for (const state of {async_state_map}.values()) {{ state.abandonLockWaiters(); }}
for (const state of {async_state_map}.values()) {{ state.abandonAdmissionWaiters(); }}
for (const state of {async_state_map}.values()) {{ state.runTickLoop(); }}
}}, 0);
}}
"#,
));
}
Self::TrackHostOperation => {
let track_host_operation_fn =
render_args.require_intrinsic(Self::TrackHostOperation);
let check_for_deadlock_fn = render_args.require_intrinsic(Self::CheckForDeadlock);
let async_state_map = render_args.require_intrinsic(Self::GlobalAsyncStateMap);
let store_async_state = render_args.require_intrinsic(Self::GlobalStoreAsyncState);
let symbol_dispose = render_args.require_intrinsic(Intrinsic::SymbolDispose);
let store = render_args.require_intrinsic(Self::GlobalStoreTrap);
output.push_str(&format!(
r#"
function {track_host_operation_fn}(operation) {{
if ({store}.stopped) {{ throw {store}.error; }}
const result = operation();
if (result === null ||
(typeof result !== 'object' && typeof result !== 'function') ||
typeof result.then !== 'function') {{
if ({store}.stopped) {{ throw {store}.error; }}
return result;
}}
{store_async_state}.pendingHostOperations++;
let rejectStopped;
const stopped = new Promise((_, reject) => {{ rejectStopped = reject; }});
{store}.stopWaiters.add(rejectStopped);
if ({store}.stopped) {{ rejectStopped({store}.error); }}
const tracked = Promise.race([result, stopped]).finally(() => {{
{store}.stopWaiters.delete(rejectStopped);
{store_async_state}.pendingHostOperations--;
if ({store_async_state}.pendingHostOperations < 0) {{
throw new Error('negative pending host operation count');
}}
for (const state of {async_state_map}.values()) {{ state.runTickLoop(); }}
{check_for_deadlock_fn}();
}});
// Forward disposal to the host's value, so it is notified if a guest
// discards the value (e.g. a future) produced from this operation
for (const sym of [Symbol.asyncDispose, {symbol_dispose}]) {{
if (typeof sym === 'symbol' && typeof result[sym] === 'function') {{
tracked[sym] = () => result[sym]();
}}
}}
return tracked;
}}
"#,
));
}
Self::NormalizeCoreTrap => {
let normalize_core_trap_fn = render_args.require_intrinsic(Self::NormalizeCoreTrap);
let runtime_error_class =
render_args.require_intrinsic(Intrinsic::WebAssemblyRuntimeError);
let unreachable = Trap::UnreachableCodeReached.to_string();
let memory_oob = Trap::MemoryOutOfBounds.to_string();
let integer_division_by_zero = Trap::IntegerDivisionByZero.to_string();
let integer_overflow = Trap::IntegerOverflow.to_string();
let bad_conversion = Trap::BadConversionToInteger.to_string();
let table_oob = Trap::TableOutOfBounds.to_string();
let bad_signature = Trap::BadSignature.to_string();
let stack_overflow = Trap::StackOverflow.to_string();
output.push_str(&format!(
r#"
const CORE_TRAP_MESSAGES = new Map([
['unreachable', {unreachable:?}],
['memory access out of bounds', {memory_oob:?}],
['divide by zero', {integer_division_by_zero:?}],
['remainder by zero', {integer_division_by_zero:?}],
['divide result unrepresentable', {integer_overflow:?}],
['float unrepresentable in integer range', {bad_conversion:?}],
['table index is out of bounds', {table_oob:?}],
['function signature mismatch', {bad_signature:?}],
['call stack exhausted', {stack_overflow:?}],
]);
function {normalize_core_trap_fn}(err) {{
if (!(err instanceof {runtime_error_class})) {{ return err; }}
const message = CORE_TRAP_MESSAGES.get(err.message);
if (message !== undefined) {{ err.message = message; }}
return err;
}}
"#,
));
}
Self::PostReturnTrap => {
let post_return_trap_fn = render_args.require_intrinsic(Self::PostReturnTrap);
let get_state = render_args.require_intrinsic(Self::GetOrCreateAsyncState);
let runtime_error_class =
render_args.require_intrinsic(Intrinsic::WebAssemblyRuntimeError);
let stack_overflow = Trap::StackOverflow.to_string();
let uncaught_exception = Trap::UncaughtException.to_string();
output.push_str(&format!(
r#"
function {post_return_trap_fn}(componentIdx, err) {{
// Core exceptions and stack exhaustion are fatal during cleanup.
if (!(err instanceof {runtime_error_class})) {{
const message = /call stack|stack overflow|too much recursion/i.test(err?.message ?? '')
? {stack_overflow:?} : {uncaught_exception:?};
err = new {runtime_error_class}(message, {{ cause: err }});
}}
{get_state}(componentIdx).markTrapped(err);
return err;
}}
"#,
));
}
Self::GuardPostReturn => {
let guard = render_args.require_intrinsic(Self::GuardPostReturn);
let trap = render_args.require_intrinsic(Self::PostReturnTrap);
output.push_str(&format!(
r#"
function {guard}(componentIdx, fn) {{
return (...args) => {{
try {{ return fn(...args); }}
catch (err) {{ throw {trap}(componentIdx, err); }}
}};
}}
"#,
));
}
Self::CheckMayLeave => {
let check_may_leave_fn = render_args.require_intrinsic(Self::CheckMayLeave);
let instance_flags = render_args.require_intrinsic(Self::GlobalInstanceFlagsMap);
let runtime_error_class =
render_args.require_intrinsic(Intrinsic::WebAssemblyRuntimeError);
output.push_str(&format!(
r#"
function {check_may_leave_fn}(componentIdx) {{
if ({instance_flags}.get(componentIdx)?.value !== 1) {{
throw new {runtime_error_class}('cannot leave component instance');
}}
}}
"#,
));
}
Self::GuardMayLeave => {
let guard_may_leave_fn = render_args.require_intrinsic(Self::GuardMayLeave);
let check_may_leave_fn = render_args.require_intrinsic(Self::CheckMayLeave);
output.push_str(&format!(
r#"
function {guard_may_leave_fn}(componentIdx, fn) {{
return function (...args) {{
{check_may_leave_fn}(componentIdx);
return fn.apply(this, args);
}};
}}
"#,
));
}
Self::RecordTrap => {
let record_trap_fn = render_args.require_intrinsic(Self::RecordTrap);
let get_or_create_async_state_fn =
render_args.require_intrinsic(Self::GetOrCreateAsyncState);
let global_current_thread_meta_obj =
render_args.require_intrinsic(Intrinsic::GlobalCurrentThreadMeta);
let runtime_error_class =
render_args.require_intrinsic(Intrinsic::WebAssemblyRuntimeError);
output.push_str(&format!(
r#"
function {record_trap_fn}(componentIdx, err, convertsRangeErrors = false) {{
// A built-in reading or writing guest memory past its end fails with a
// DataView RangeError: that is the Canonical ABI's out-of-bounds trap.
if (convertsRangeErrors && err instanceof RangeError) {{
err = new {runtime_error_class}('out of bounds memory access');
}}
if (err instanceof {runtime_error_class}) {{
// (a trampoline without a static instance records against the
// component whose guest code is running)
const idx = componentIdx ?? {global_current_thread_meta_obj}.current?.componentIdx;
if (idx !== undefined && idx !== null) {{
{get_or_create_async_state_fn}(idx).markTrapped(err);
}}
}}
return err;
}}
"#,
));
}
Self::GuardTrap => {
let guard_trap_fn = render_args.require_intrinsic(Self::GuardTrap);
let record_trap_fn = render_args.require_intrinsic(Self::RecordTrap);
output.push_str(&format!(
r#"
function {guard_trap_fn}(componentIdx, fn, convertsRangeErrors = false) {{
return function (...args) {{
try {{
return fn.apply(this, args);
}} catch (err) {{
throw {record_trap_fn}(componentIdx, err, convertsRangeErrors);
}}
}};
}}
"#,
));
}
Self::GlobalAsyncStateMap => {
let var_name = render_args.require_intrinsic(Self::GlobalAsyncStateMap);
uwriteln!(output, r#"const {var_name} = new Map();"#);
}
Self::BackpressureInc => {
let debug_log_fn = render_args.require_intrinsic(Intrinsic::DebugLog);
let backpressure_inc_fn = render_args.require_intrinsic(Self::BackpressureInc);
let get_or_create_async_state_fn =
render_args.require_intrinsic(Self::GetOrCreateAsyncState);
output.push_str(&format!(
r#"
function {backpressure_inc_fn}(componentIdx) {{
{debug_log_fn}('[{backpressure_inc_fn}()] args', {{ componentIdx }});
const state = {get_or_create_async_state_fn}(componentIdx);
if (!state) {{ throw new Error(`missing component state for component [${{componentIdx}}]`); }}
const newValue = state.incrementBackpressure();
{debug_log_fn}('[{backpressure_inc_fn}()] incremented', {{ componentIdx, newValue }});
}}
"#,
));
}
Self::BackpressureDec => {
let debug_log_fn = render_args.require_intrinsic(Intrinsic::DebugLog);
let backpressure_dec_fn = render_args.require_intrinsic(Self::BackpressureDec);
let get_or_create_async_state_fn =
render_args.require_intrinsic(Self::GetOrCreateAsyncState);
output.push_str(&format!(
"
function {backpressure_dec_fn}(componentIdx) {{
{debug_log_fn}('[{backpressure_dec_fn}()] args', {{ componentIdx }});
const state = {get_or_create_async_state_fn}(componentIdx);
const newValue = state.decrementBackpressure();
{debug_log_fn}('[{backpressure_dec_fn}()] decremented', {{ componentIdx, newValue }});
}}
"
));
}
Self::ComponentAsyncStateClass => {
let component_async_state_class = self.name();
let debug_log_fn = render_args.require_intrinsic(Intrinsic::DebugLog);
let rep_table_class = render_args.require_intrinsic(Intrinsic::RepTableClass);
let get_handle_index_space_fn = render_args.intrinsic_call(
Intrinsic::Resource(ResourceIntrinsic::GetHandleIndexSpace),
"this.#componentIdx",
);
let waitable_class =
render_args.require_intrinsic(WaitableIntrinsic::WaitableClass);
let promise_with_resolvers_fn =
render_args.require_intrinsic(Intrinsic::PromiseWithResolversPonyfill);
let runtime_error_class =
render_args.require_intrinsic(Intrinsic::WebAssemblyRuntimeError);
let normalize_core_trap_fn = render_args.require_intrinsic(Self::NormalizeCoreTrap);
let instance_flags = render_args.require_intrinsic(Self::GlobalInstanceFlagsMap);
let store_trap = render_args.require_intrinsic(Self::GlobalStoreTrap);
let check_for_deadlock_fn = render_args.require_intrinsic(Self::CheckForDeadlock);
let cannot_enter_component = Trap::CannotEnterComponent.to_string();
let report_trap_fn = render_args.require_intrinsic(Self::ReportTrap);
let check_idle_fn = render_args.require_intrinsic(Self::CheckIdle);
output.push_str(&format!(
r#"
class {component_async_state_class} {{
static EVENT_HANDLER_EVENTS = [ 'backpressure-change' ];
static TickResult = {{
// no suspended tasks remain
DONE: 'done',
// a suspended task was resumed (more may be ready)
RESUMED: 'resumed',
// suspended tasks remain but none were ready
IDLE: 'idle',
}};
#componentIdx;
#callingAsyncImport = false;
#syncImportWait = {promise_with_resolvers_fn}();
#lockHolderTaskID = null;
#lockWaiters = [];
#admissionQueue = [];
#lockHandoffScheduled = false;
#pendingTaskStarts = 0;
#parkedTasks = new Map();
// Suspended threads, oldest first (a Map iterates in insertion
// order, so no separate queue is kept for them).
#suspendedThreadsByRef = new Map();
#errored = null;
#trapped = false;
#trapError = null;
#backpressure = 0;
#backpressureWaiters = 0n;
#handlerMap = new Map();
#nextHandlerID = 0n;
// Cancels the scheduled tick while the tick loop runs.
#tickLoop = null;
// Runs the callback on the next macrotask, after the current microtasks have
// drained, as setTimeout(callback, 0) would, but without its minimum delay:
// Node.js clamps it to 1 ms and browsers to 4 ms once timers nest, which a
// guest that yields often pays on every yield. Returns a cancel function.
static #onNextMacrotask(callback) {{
if (typeof setImmediate === 'function') {{
const handle = setImmediate(callback);
return () => clearImmediate(handle);
}}
if (typeof MessageChannel === 'function') {{
const channel = new MessageChannel();
let cancelled = false;
channel.port1.onmessage = () => {{
channel.port1.close();
if (!cancelled) {{ callback(); }}
}};
channel.port2.postMessage(null);
return () => {{ cancelled = true; channel.port1.close(); }};
}}
const handle = setTimeout(callback, 0);
return () => clearTimeout(handle);
}}
static #afterDelay(callback, ms) {{
const handle = setTimeout(callback, ms);
return () => clearTimeout(handle);
}}
#onExclusiveReleaseHandlers = [];
#mayLeave = true;
handles;
subtasks;
threads;
constructor(args) {{
this.#componentIdx = args.componentIdx;
this.handles = new {rep_table_class}({{
target: `component [${{this.#componentIdx}}] handles (waitable objects)`,
space: {get_handle_index_space_fn},
}});
this.subtasks = new {rep_table_class}({{ target: `component [${{this.#componentIdx}}] subtasks` }});
this.threads = new {rep_table_class}({{ target: `component [${{this.#componentIdx}}] threads` }});
}};
componentIdx() {{ return this.#componentIdx; }}
get mayLeave() {{
const flags = {instance_flags}.get(this.#componentIdx);
return flags === undefined ? this.#mayLeave : flags.value === 1;
}}
set mayLeave(value) {{
if (typeof value !== 'boolean') {{ throw new TypeError('mayLeave must be a boolean'); }}
this.#mayLeave = value;
const flags = {instance_flags}.get(this.#componentIdx);
if (flags !== undefined) {{ flags.value = value ? 1 : 0; }}
}}
errored() {{ return this.#errored !== null; }}
setErrored(err) {{
{debug_log_fn}('[{component_async_state_class}#setErrored()] component errored', {{ err, componentIdx: this.#componentIdx }});
if (this.#errored) {{ return; }}
if (!err) {{
err = new Error('error elswehere (see other component instance error)')
err.componentIdx = this.#componentIdx;
}}
this.#errored = err;
}}
markTrapped(err) {{
if (!(err instanceof {runtime_error_class})) {{
return false;
}}
err = {normalize_core_trap_fn}(err);
this.#trapped = true;
if (this.#trapError === null) {{ this.#trapError = err; }}
{debug_log_fn}('[{component_async_state_class}#markTrapped()] component trapped', {{ err, componentIdx: this.#componentIdx }});
if ({store_trap}.error === null) {{ {store_trap}.error = err; }}
{report_trap_fn}(err);
{check_idle_fn}();
return true;
}}
isTrapped() {{ return this.#trapped; }}
// For work that was already under way or queued when the instance
// trapped: it fails with that trap (nothing runs after a trap).
throwIfInterrupted() {{
if ({store_trap}.stopped) {{ throw {store_trap}.error; }}
if (this.#trapped) {{
throw this.#trapError ?? new {runtime_error_class}({cannot_enter_component:?});
}}
}}
throwIfTrapped() {{
if ({store_trap}.stopped) {{ throw {store_trap}.error; }}
if (this.#trapped) {{
throw new {runtime_error_class}({cannot_enter_component:?});
}}
}}
callingSyncImport(val) {{
if (val === undefined) {{ return this.#callingAsyncImport; }}
if (typeof val !== 'boolean') {{ throw new TypeError('invalid setting for async import'); }}
const prev = this.#callingAsyncImport;
this.#callingAsyncImport = val;
if (prev === true && this.#callingAsyncImport === false) {{
this.#notifySyncImportEnd();
}}
}}
#notifySyncImportEnd() {{
const existing = this.#syncImportWait;
this.#syncImportWait = {promise_with_resolvers_fn}();
existing.resolve();
}}
async waitForSyncImportCallEnd() {{
await this.#syncImportWait.promise;
}}
setBackpressure(v) {{
this.#backpressure = v;
for (const handler of [...(this.#handlerMap.get('backpressure-change') ?? [])]) {{
handler.fn(v);
}}
return this.#backpressure;
}}
getBackpressure() {{ return this.#backpressure; }}
incrementBackpressure() {{
const current = this.#backpressure;
if (current < 0 || current > 2**16) {{
throw new Error(`invalid current backpressure value [${{current}}]`);
}}
const newValue = this.getBackpressure() + 1;
if (newValue >= 2**16) {{
throw new Error(`invalid new backpressure value [${{newValue}}], overflow`);
}}
return this.setBackpressure(newValue);
}}
decrementBackpressure() {{
const current = this.#backpressure;
if (current < 0 || current > 2**16) {{
throw new Error(`invalid current backpressure value [${{current}}]`);
}}
// Canonical ABI `backpressure.dec`: trap if the counter would go
// below zero.
const newValue = current - 1;
if (newValue < 0) {{
throw new {runtime_error_class}('backpressure counter underflow');
}}
return this.setBackpressure(newValue);
}}
hasBackpressure() {{ return this.#backpressure > 0; }}
waitForBackpressure() {{
if (!this.hasBackpressure()) {{ return Promise.resolve(null); }}
this.addBackpressureWaiter();
return new Promise((resolve) => {{
let pending = true;
const handlerID = this.registerHandler({{
event: 'backpressure-change',
fn: (bp) => {{
if (bp !== 0 || !pending) {{ return; }}
pending = false;
this.removeHandler({{ event: 'backpressure-change', handlerID }});
this.removeBackpressureWaiter();
resolve(null);
}},
}});
}});
}}
registerHandler(args) {{
const {{ event, fn }} = args;
if (!event) {{ throw new Error("missing handler event"); }}
if (!fn) {{ throw new Error("missing handler fn"); }}
if (!{component_async_state_class}.EVENT_HANDLER_EVENTS.includes(event)) {{
throw new Error(`unrecognized event handler [${{event}}]`);
}}
const handlerID = this.#nextHandlerID++;
let handlers = this.#handlerMap.get(event);
if (!handlers) {{
handlers = [];
this.#handlerMap.set(event, handlers)
}}
handlers.push({{ id: handlerID, fn, event }});
return handlerID;
}}
removeHandler(args) {{
const {{ event, handlerID }} = args;
const registeredHandlers = this.#handlerMap.get(event);
if (!registeredHandlers) {{ return; }}
const found = registeredHandlers.find(h => h.id === handlerID);
if (!found) {{ return; }}
this.#handlerMap.set(event, this.#handlerMap.get(event).filter(h => h.id !== handlerID));
}}
getBackpressureWaiters() {{ return this.#backpressureWaiters; }}
addBackpressureWaiter() {{ this.#backpressureWaiters++; }}
removeBackpressureWaiter() {{
this.#backpressureWaiters--;
if (this.#backpressureWaiters < 0) {{
throw new Error("unexepctedly negative number of backpressure waiters");
}}
}}
// The per-slice mutual-exclusion lock for guest execution in this
// component instance. Guest slices (callback invocations and
// sync-lifted bodies) must be atomic per component even across the
// JSPI suspensions jco introduces for host imports: wit-bindgen's
// executors publish per-task state in single linear-memory cells
// (the wasip3-task pointer, context-local storage discipline) that
// an interleaved slice of the same component corrupts
//
// The lock is *owned*: acquisition records the holder task and
// release is a no-op for anyone else, so a task exiting can no
// longer drop a hold it does not own (blind acquire/release-any
// was the previous discipline). Contended acquisition queues
// FIFO; release hands the lock to the next waiter directly.
isExclusivelyLocked() {{ return this.#lockHolderTaskID !== null; }}
exclusivelyLockedBy(taskID) {{ return this.#lockHolderTaskID === taskID; }}
exclusiveLock(taskID) {{
{debug_log_fn}('[{component_async_state_class}#exclusiveLock()]', {{
holder: this.#lockHolderTaskID,
requester: taskID,
componentIdx: this.#componentIdx,
}});
if (taskID === undefined || taskID === null) {{
throw new Error('exclusive lock requires the acquiring task id');
}}
if (this.#lockHolderTaskID !== null) {{
throw new Error(`component [${{this.#componentIdx}}] exclusive lock held by task [${{this.#lockHolderTaskID}}], requested by [${{taskID}}]`);
}}
this.#lockHolderTaskID = taskID;
}}
// Awaitable acquisition: takes the lock immediately when free,
// otherwise queues FIFO behind the current holder and earlier
// waiters. The resolved promise implies ownership.
acquireExclusiveLock(taskID) {{
if (taskID === undefined || taskID === null) {{
throw new Error('exclusive lock requires the acquiring task id');
}}
if (this.#lockHolderTaskID === null) {{
this.#lockHolderTaskID = taskID;
{debug_log_fn}('[{component_async_state_class}#acquireExclusiveLock()] acquired', {{
holder: taskID,
componentIdx: this.#componentIdx,
}});
return;
}}
if (this.#lockHolderTaskID === taskID) {{
throw new Error(`task [${{taskID}}] already holds the lock for component [${{this.#componentIdx}}]`);
}}
{debug_log_fn}('[{component_async_state_class}#acquireExclusiveLock()] waiting', {{
holder: this.#lockHolderTaskID,
requester: taskID,
componentIdx: this.#componentIdx,
queued: this.#lockWaiters.length,
}});
return new Promise((resolve) => {{
this.#lockWaiters.push({{ taskID, resolve }});
}});
}}
// Release every queued entry without granting ownership, so that
// calls waiting to enter a trapped store fail instead of hanging.
// FIFO admission of tasks entering this instance (Canonical ABI
// `inst.pending`): a task enters only after every task that asked to
// enter before it has entered or given up, so calls enter in the order
// they were made, whatever each waits for on the way.
takeAdmissionTicket(taskID) {{
const ticket = {{ taskID, ...{promise_with_resolvers_fn}() }};
this.#admissionQueue.push(ticket);
return ticket;
}}
hasPendingAdmissions() {{ return this.#admissionQueue.length > 0; }}
waitForAdmissionTurn(ticket) {{
if (this.#admissionQueue[0] === ticket) {{ return; }}
return ticket.promise;
}}
releaseAdmissionTicket(ticket) {{
const idx = this.#admissionQueue.indexOf(ticket);
if (idx === -1) {{ return; }}
this.#admissionQueue.splice(idx, 1);
if (idx === 0 && this.#admissionQueue.length > 0) {{
this.#admissionQueue[0].resolve();
}}
}}
// Release every queued entry (the instance trapped): each then fails
// in `enter()` instead of waiting forever.
abandonAdmissionWaiters() {{
const tickets = this.#admissionQueue.splice(0);
for (const ticket of tickets) {{ ticket.resolve(); }}
}}
abandonLockWaiters() {{
const waiters = this.#lockWaiters.splice(0);
for (const waiter of waiters) {{ waiter.resolve(); }}
}}
cancelExclusiveLockWaiter(taskID) {{
const idx = this.#lockWaiters.findIndex(waiter => waiter.taskID === taskID);
if (idx === -1) {{ return false; }}
const [waiter] = this.#lockWaiters.splice(idx, 1);
// Release the awaiting `enter()` continuation without granting
// ownership. It observes the task's resolved cancellation state
// before attempting to execute guest code.
waiter.resolve();
return true;
}}
exclusiveRelease(taskID) {{
{debug_log_fn}('[{component_async_state_class}#exclusiveRelease()] args', {{
holder: this.#lockHolderTaskID,
releaser: taskID,
componentIdx: this.#componentIdx,
}});
if (this.#lockHolderTaskID !== taskID) {{
// Ownerless releases were the historical behavior; a foreign
// release now leaves the hold intact
{debug_log_fn}('[{component_async_state_class}#exclusiveRelease()] ignoring foreign release', {{
holder: this.#lockHolderTaskID,
releaser: taskID,
componentIdx: this.#componentIdx,
}});
return false;
}}
// Make the release observable before handing the lock to the next
// asynchronous guest slice.
//
// Release handlers may expose a lifted value whose consumer immediately
// performs a synchronous call on the same component; that call must run
// while the instance is genuinely unlocked, not via enterSync's
// lock-free fallback code.
this.#lockHolderTaskID = null;
this.#onExclusiveReleaseHandlers = this.#onExclusiveReleaseHandlers.filter(v => !!v);
for (const [idx, f] of this.#onExclusiveReleaseHandlers.entries()) {{
try {{
this.#onExclusiveReleaseHandlers[idx] = null;
f();
}} catch (err) {{
{debug_log_fn}("error while executing handler for next exclusive release", err);
throw err;
}}
}}
this.#scheduleLockHandoff();
return true;
}}
#scheduleLockHandoff() {{
if (this.#lockHandoffScheduled || this.#lockWaiters.length === 0) {{ return; }}
this.#lockHandoffScheduled = true;
queueMicrotask(() => {{
this.#lockHandoffScheduled = false;
// A synchronous call triggered by a release handler gets the
// first opportunity to use the unlocked component. Its own
// release schedules the next handoff; re-queueing one here
// would spin the microtask queue for as long as the holder
// stays blocked (e.g. in a synchronous wait), starving every
// timer, the deadlock detector included.
if (this.#lockHolderTaskID !== null) {{ return; }}
const next = this.#lockWaiters.shift();
if (!next) {{ return; }}
this.#lockHolderTaskID = next.taskID;
next.resolve();
}});
}}
onNextExclusiveRelease(fn) {{
{debug_log_fn}('[{component_async_state_class}#()onNextExclusiveRelease] registering');
this.#onExclusiveReleaseHandlers.push(fn);
}}
async waitForExclusiveRelease() {{
while (this.isExclusivelyLocked()) {{
await new Promise(resolve => this.onNextExclusiveRelease(resolve));
}}
}}
// Callback drivers refer to the implicit thread by task ID.
// Stackful built-ins pass their current thread directly.
#getSuspendedTaskMeta(taskID) {{
const metas = [...this.#suspendedThreadsByRef.values()].filter(meta => meta.thread.taskID() === taskID);
return metas.find(meta => meta.cancellable && this.suspendedThreadReady(meta.thread)) ?? metas[0];
}}
suspendTask(args) {{
const {{ task, ...opts }} = args;
task.endFirstSlice();
return this.suspendThread({{ thread: task.defaultThread(), ...opts }});
}}
resumeTaskByID(taskID) {{
const meta = this.#getSuspendedTaskMeta(taskID);
return meta ? this.resumeThread(meta.thread) : false;
}}
suspendedTaskReady(taskID) {{
const meta = this.#getSuspendedTaskMeta(taskID);
return meta ? this.suspendedThreadReady(meta.thread) : false;
}}
suspendedTaskCancellable(taskID) {{
return !!this.#getSuspendedTaskMeta(taskID)?.cancellable;
}}
isTaskSuspended(taskID) {{
return !!this.#getSuspendedTaskMeta(taskID);
}}
*suspendedTaskMetas() {{
for (const meta of this.#suspendedThreadsByRef.values()) {{
yield {{ ...meta, task: meta.thread.task() }};
}}
}}
#removeSuspendedThreadMeta(thread) {{
const meta = this.#suspendedThreadsByRef.get(thread);
this.#suspendedThreadsByRef.delete(thread);
return meta;
}}
suspendThread(args) {{
const {{ thread, readyFn, cancellable, onResume, suspended = false }} = args;
{debug_log_fn}('[{component_async_state_class}#suspendThread()]', {{
threadID: thread.id(),
taskID: thread.taskID(),
componentIdx: this.#componentIdx,
}});
if (thread.componentIdx() !== this.#componentIdx) {{
throw new Error('assert: thread component idx should match async state');
}}
if (this.#suspendedThreadsByRef.has(thread)) {{
throw new Error(`thread [${{thread.id()}}] already suspended`);
}}
let promise;
let resume;
if (onResume) {{
resume = () => onResume(!thread.isCancelled() && !thread.isRejected() && !this.isTrapped());
}} else {{
const resolvers = {promise_with_resolvers_fn}();
promise = resolvers.promise;
resume = () => resolvers.resolve(!thread.isCancelled() && !thread.isRejected() && !this.isTrapped());
}}
if ({store_trap}.stopped) {{
this.markTrapped({store_trap}.error);
resume();
return promise;
}}
this.#suspendedThreadsByRef.set(thread, {{ thread, cancellable, readyFn, resume, suspended }});
thread.notifyProgress();
this.runTickLoop();
{check_for_deadlock_fn}();
return promise;
}}
resumeThread(thread) {{
const meta = this.#removeSuspendedThreadMeta(thread);
if (!meta) {{ return false; }}
meta.resume();
return true;
}}
markThreadReady(thread) {{
const meta = this.#suspendedThreadsByRef.get(thread);
if (!meta || !meta.suspended) {{ return false; }}
meta.suspended = false;
meta.readyFn = () => true;
this.runTickLoop();
return true;
}}
isThreadSuspended(thread) {{
return this.#suspendedThreadsByRef.get(thread)?.suspended ?? false;
}}
isThreadReady(thread) {{
const meta = this.#suspendedThreadsByRef.get(thread);
return !!meta && !meta.suspended && (meta.thread.isRejected() || meta.readyFn());
}}
suspendedThreadReady(thread) {{
const meta = this.#suspendedThreadsByRef.get(thread);
if (!meta) {{ return false; }}
if (!meta.readyFn) {{
throw new Error(`suspended thread [${{thread.id()}}] is missing a readiness function`);
}}
if (this.isTrapped() || meta.thread.isRejected()) {{ return true; }}
if (!meta.readyFn()) {{ return false; }}
const task = meta.thread.task();
return !task.needsExclusiveLock()
|| !this.isExclusivelyLocked()
|| this.exclusivelyLockedBy(task.id());
}}
addPendingTaskStart() {{ this.#pendingTaskStarts++; }}
removePendingTaskStart() {{ this.#pendingTaskStarts--; }}
hasPendingSchedulerWork() {{
if (this.#pendingTaskStarts > 0) {{ return true; }}
if (this.#lockHandoffScheduled) {{ return true; }}
for (const meta of this.#suspendedThreadsByRef.values()) {{
if (this.suspendedThreadReady(meta.thread)) {{ return true; }}
}}
return false;
}}
stop() {{
this.#tickLoop?.();
this.#tickLoop = null;
this.abandonLockWaiters();
this.abandonAdmissionWaiters();
for (const thread of [...this.#suspendedThreadsByRef.keys()]) {{
this.resumeThread(thread);
}}
}}
runTickLoop() {{
if ({store_trap}.stopped || this.#tickLoop !== null) {{ return; }}
const run = () => {{
if ({store_trap}.stopped) {{ this.#tickLoop = null; return; }}
const result = this.tick();
if ({store_trap}.stopped || result === {component_async_state_class}.TickResult.DONE) {{
this.#tickLoop = null;
return;
}}
if (result === {component_async_state_class}.TickResult.IDLE) {{
{check_for_deadlock_fn}();
}}
// Let resumed microtasks drain before the next slice;
// otherwise poll at the idle cadence.
this.#tickLoop = result === {component_async_state_class}.TickResult.RESUMED
? {component_async_state_class}.#onNextMacrotask(run)
: {component_async_state_class}.#afterDelay(run, 10);
}};
this.#tickLoop = {component_async_state_class}.#onNextMacrotask(run);
}}
tick() {{
// (resuming returns immediately below, so the map is never
// mutated while this loop is still iterating it)
for (const [thread, meta] of this.#suspendedThreadsByRef) {{
if (!meta || !meta.readyFn) {{
throw new Error(`missing/invalid thread meta despite thread [${{thread.id()}}] being present`);
}}
if (this.isTrapped() || meta.thread.isRejected()) {{
{debug_log_fn}('[{component_async_state_class}#tick()] detected thread rejection, leaving early', {{ meta }});
this.resumeThread(thread);
return {component_async_state_class}.TickResult.RESUMED;
}}
if (!this.suspendedThreadReady(thread)) {{ continue; }}
{debug_log_fn}('[{component_async_state_class}#tick()] resuming thread via tick', {{
threadID: thread.id(),
componentIdx: this.#componentIdx,
}});
this.resumeThread(thread);
// Resume only one thread per tick so its current-thread
// register window closes before another continuation runs.
return {component_async_state_class}.TickResult.RESUMED;
}}
const idle = this.#suspendedThreadsByRef.size > 0;
return idle
? {component_async_state_class}.TickResult.IDLE
: {component_async_state_class}.TickResult.DONE;
}}
createWaitable(args) {{
return new {waitable_class}({{ target: args?.target, }});
}}
}}
"#,
));
}
Self::GetOrCreateAsyncState => {
let get_state_fn = render_args.require_intrinsic(Self::GetOrCreateAsyncState);
let async_state_map = render_args.require_intrinsic(Self::GlobalAsyncStateMap);
let component_async_state_class =
render_args.require_intrinsic(Self::ComponentAsyncStateClass);
output.push_str(&format!(
r#"
function {get_state_fn}(componentIdx, init) {{
if (!{async_state_map}.has(componentIdx)) {{
const newState = new {component_async_state_class}({{ componentIdx }});
{async_state_map}.set(componentIdx, newState);
}}
return {async_state_map}.get(componentIdx);
}}
"#
));
}
Self::ComponentStateSetAllError => {
let debug_log_fn = render_args.require_intrinsic(Intrinsic::DebugLog);
let async_state_map = render_args.require_intrinsic(Self::GlobalAsyncStateMap);
let component_state_set_all_error_fn =
render_args.require_intrinsic(Self::ComponentStateSetAllError);
output.push_str(&format!(
r#"
function {component_state_set_all_error_fn}() {{
{debug_log_fn}('[{component_state_set_all_error_fn}()]');
for (const state of {async_state_map}.values()) {{
state.setErrored();
}}
}}
"#
));
}
}
}
}