|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// issue #234 S3 — proves the effect-threaded async-boundary detection |
| 3 | +// end-to-end: the source uses `fetchThing` (a user-defined `/ {Async}` |
| 4 | +// extern, NOT `http_request_thenable`, NOT in codegen's hardcoded |
| 5 | +// `async_primitives`). The compiler still synthesised the #205/#199 |
| 6 | +// continuation purely from the typecheck effect side-table (ADR-016). |
| 7 | +// Same host scaffold as test_http_cps_base.mjs. |
| 8 | +import assert from 'node:assert/strict'; |
| 9 | +import { readFile } from 'node:fs/promises'; |
| 10 | + |
| 11 | +let inst = null; |
| 12 | +const _handles = new Map(); |
| 13 | +const _results = new Map(); |
| 14 | +let _next = 1; |
| 15 | +let contFired = 0; |
| 16 | +let contReturn = null; |
| 17 | +let savedCb = null; |
| 18 | + |
| 19 | +function wrapHandler(closurePtr) { |
| 20 | + return () => { |
| 21 | + const tbl = inst.exports.__indirect_function_table; |
| 22 | + const dv = new DataView(inst.exports.memory.buffer); |
| 23 | + const fnId = dv.getInt32(closurePtr, true); |
| 24 | + const envPtr = dv.getInt32(closurePtr + 4, true); |
| 25 | + const fn = tbl.get(fnId); |
| 26 | + const args = [envPtr]; |
| 27 | + while (args.length < fn.length) args.push(0); |
| 28 | + return fn(...args); |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +const imports = { |
| 33 | + wasi_snapshot_preview1: { fd_write: () => 0 }, |
| 34 | + env: { |
| 35 | + fetchThing: (_urlPtr) => { |
| 36 | + const h = _next++; |
| 37 | + _handles.set(h, Promise.resolve({ status: 200 })); |
| 38 | + return h; |
| 39 | + }, |
| 40 | + thingStatus: (tHandle) => { |
| 41 | + const v = _results.get(tHandle); |
| 42 | + return v && typeof v.status === 'number' ? v.status : -1; |
| 43 | + }, |
| 44 | + }, |
| 45 | + Http: { |
| 46 | + thenableThen: (tHandle, onSettlePtr) => { |
| 47 | + const cb = wrapHandler(onSettlePtr); |
| 48 | + savedCb = cb; |
| 49 | + Promise.resolve(_handles.get(tHandle)).then((v) => { |
| 50 | + _results.set(tHandle, v); |
| 51 | + contFired += 1; |
| 52 | + contReturn = cb(); |
| 53 | + }); |
| 54 | + return 1; |
| 55 | + }, |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +const buf = await readFile('./tests/codegen/effect_async_boundary.wasm'); |
| 60 | +inst = (await WebAssembly.instantiate(buf, imports)).instance; |
| 61 | + |
| 62 | +const disposable = inst.exports.launch(); |
| 63 | +assert.ok(Number.isInteger(disposable), 'launch() returns synchronously'); |
| 64 | +assert.equal(contFired, 0, 'continuation deferred until settlement'); |
| 65 | + |
| 66 | +await new Promise((r) => setTimeout(r, 0)); |
| 67 | +await Promise.resolve(); |
| 68 | + |
| 69 | +assert.equal(contFired, 1, 'continuation fired exactly once'); |
| 70 | +assert.equal( |
| 71 | + contReturn, |
| 72 | + 200, |
| 73 | + 'effect-detected boundary: continuation read the settled status', |
| 74 | +); |
| 75 | +assert.throws( |
| 76 | + () => savedCb(), |
| 77 | + (e) => e instanceof WebAssembly.RuntimeError, |
| 78 | + 'second continuation entry traps (ADR-013 once-resumption)', |
| 79 | +); |
| 80 | +console.log('test_effect_async_boundary.mjs OK'); |
0 commit comments