Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/js-component-bindgen/src/function_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,14 @@ impl Bindgen for FunctionBindgen<'_> {
uwriteln!(self.src, "const started = task.enterSync();",);
}

// A task that is only async for JSPI never publishes its completion
// promise, so the error must be thrown to reject the suspended guest.
let async_err_exit = if *async_ {
"return task.completionPromise();"
} else {
"throw err;"
};

// Build the JS expression that calls the callee
let (call_prefix, call_wrapper, call_err_cleanup) = if is_async
|| self.requires_async_porcelain
Expand All @@ -2424,7 +2432,7 @@ impl Bindgen for FunctionBindgen<'_> {
task.setErrored(err);
task.reject(err);
task.exit();
return task.completionPromise();
{async_err_exit}
"#
),
)
Expand Down
53 changes: 52 additions & 1 deletion packages/jco-transpile/test/jspi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mkdir, rm, symlink } from 'node:fs/promises';

import { fileURLToPath } from 'node:url';

import { suite, test, assert } from 'vitest';
import { suite, test, assert, expect } from 'vitest';

import { transpile } from '../src/index.js';

Expand Down Expand Up @@ -204,4 +204,55 @@ suite('Host Import Async (JSPI)', () => {
await rm(outFile);
} catch {}
});

// A failing JSPI-suspended import must trap the guest and reject the
// export, rather than leave the guest suspended forever.
test.concurrent('Transpile async, import rejects (NodeJS, JSPI)', async () => {
if (typeof WebAssembly?.Suspending !== 'function') {
return;
}
const tmpDir = await getTmpDir();
const modulesDir = resolve(tmpDir, 'node_modules', '@bytecodealliance');
await mkdir(modulesDir, { recursive: true });
await symlink(
fileURLToPath(new URL('../packages/preview2-shim', import.meta.url)),
resolve(modulesDir, 'preview2-shim'),
'dir',
);

const inducedError = new Error('induced host rejection');
const { instance, cleanup } = await setupAsyncTest({
asyncMode: 'jspi',
component: {
name: 'async_call',
path: resolve('test/fixtures/components/runtime/async_call.component.wasm'),
imports: {
'something:test/test-interface': {
callAsync: async () => {
throw inducedError;
},
callSync: () => 'called sync',
},
},
},
jco: {
transpile: {
extraArgs: {
asyncImports: ['something:test/test-interface#call-async'],
asyncExports: ['run-async'],
},
},
},
});

try {
const exportCall = instance.runAsync();
const timeout = new Promise((_, reject) =>
setTimeout(() => reject(new Error('export call timed out')), 5_000),
);
await expect(Promise.race([exportCall, timeout])).rejects.toBe(inducedError);
} finally {
await cleanup();
}
});
});
Loading