import assert from 'node:assert/strict'; /** Realloc must restore the guest's flags on success and failure. */ export async function testSyncIntrinsics({ load }) { const { guardSyncRealloc } = await load('intrinsics/sync'); const flags = { value: 3 }; const realloc = guardSyncRealloc(flags, (_ptr, _old, _align, size) => { assert.equal(flags.value, 0); if (size === 0) throw new Error('realloc failed'); return -1; }); assert.equal(realloc(0, 0, 1, 8), 0xffff_ffff); assert.equal(flags.value, 3); assert.throws(() => realloc(0, 0, 1, 0), /realloc failed/); assert.equal(flags.value, 3); } /** The two entries must observe the same task registry. */ export async function testSyncTaskIntrinsics({ load }) { const sync = await load('intrinsics/sync-task'); const task = await load('intrinsics/async-task'); const tasks = sync.initializeTasksByComponent(); const entry = { task: { id: () => 7n } }; tasks.set(1, [entry]); for (const lookup of [sync.getCurrentTask, task.getCurrentTask]) { assert.equal(lookup({ tasks, registers: {} }, 1, 7n), entry); } } /** Bindings must observe a stopped store before the runner tries to enter Wasm. */ export async function testSyncExportIntrinsics({ load }) { const { createSyncExportRunner } = await load('intrinsics/sync-export'); const stopped = new WebAssembly.RuntimeError('stopped'); const run = createSyncExportRunner({ lookup: () => ({ throwIfTrapped() { throw stopped; }, }), }); assert.throws( () => run({ componentIdx: 0 }, () => assert.fail()), (err) => err === stopped, ); }