Skip to content

Commit 4396cf2

Browse files
committed
wasm: enable JSPI
PR-URL: #59941 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 81af7b9 commit 4396cf2

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/node.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,9 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
782782

783783
v8_args.emplace_back("--js-source-phase-imports");
784784

785+
// WebAssembly JS Promise Integration
786+
v8_args.emplace_back("--experimental-wasm-jspi");
787+
785788
#ifdef __POSIX__
786789
// Block SIGPROF signals when sleeping in epoll_wait/kevent/etc. Avoids the
787790
// performance penalty of frequent EINTR wakeups when the profiler is running.

test/fixtures/wasm/jspi.wasm

85 Bytes
Binary file not shown.

test/parallel/test-wasm.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import '../common/index.mjs';
2+
import { strictEqual } from 'node:assert';
3+
import { readSync } from '../common/fixtures.mjs';
4+
5+
// Test Wasm JSPI
6+
{
7+
const asyncImport = async (x) => {
8+
await new Promise((resolve) => setTimeout(resolve, 10));
9+
return x + 42;
10+
};
11+
12+
/**
13+
* wasm/jspi.wasm contains:
14+
*
15+
* (module
16+
* (type (;0;) (func (param i32) (result i32)))
17+
* (import "js" "async" (func $async (;0;) (type 0)))
18+
* (export "test" (func $test))
19+
* (func $test (;1;) (type 0) (param $x i32) (result i32)
20+
* local.get $x
21+
* call $async
22+
* )
23+
* )
24+
*
25+
* Which is the JS equivalent to:
26+
*
27+
* import { async_ } from 'js';
28+
* export function test (val) {
29+
* return async_(val);
30+
* }
31+
*
32+
* JSPI then allows turning this sync style Wasm call into async code
33+
* that suspends on the promise.
34+
*/
35+
36+
const { instance } = await WebAssembly.instantiate(readSync('wasm/jspi.wasm'), {
37+
js: {
38+
async: new WebAssembly.Suspending(asyncImport),
39+
},
40+
});
41+
42+
const promisingExport = WebAssembly.promising(instance.exports.test);
43+
strictEqual(await promisingExport(10), 52);
44+
}

0 commit comments

Comments
 (0)