Skip to content

Commit 80c270d

Browse files
committed
feat(async): able to compile wasm asynchronously
📥 ability to call JS function inside Wasm code [ci skip]
1 parent bc75410 commit 80c270d

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

src/main.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ export default function(options: $Shape<Options> = {}) {
3939
return wrap(wasmCode).asWebAssembly.Instance;
4040
case 'module':
4141
return wrap(wasmCode).asWebAssembly.Module;
42-
case 'promise':
43-
return wrap(wasmCode).promiseWebAssembly;
42+
case 'promise' || 'async':
43+
return wrap(wasmCode).promiseWebAssembly.Both;
44+
case 'async-instance':
45+
return wrap(wasmCode).promiseWebAssembly.Instance;
46+
case 'async-module':
47+
return wrap(wasmCode).promiseWebAssembly.Module;
4448
}
4549
} else
4650
this.error(`only support wasm related target compile

src/wrapper.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,33 @@ export default function(buffer: Buffer) {
1515
Module:
1616
polyfill +
1717
`export default new WebAssembly.Module(
18-
Buffer.from([${data}])
19-
)`,
18+
Buffer.from([${data}])
19+
)`,
2020
Instance:
2121
polyfill +
2222
`export default new WebAssembly.Instance(
23-
new WebAssembly.Module(
24-
Buffer.from([${data}])
25-
)
26-
)`
23+
new WebAssembly.Module(
24+
Buffer.from([${data}])
25+
)
26+
)`
2727
},
28-
promiseWebAssembly:
29-
polyfill +
30-
`export default WebAssembly.instantiate(
31-
Buffer.from([${data}])
32-
)`
28+
promiseWebAssembly: {
29+
Module:
30+
polyfill +
31+
`module.exports = () => WebAssembly.compile(
32+
Buffer.from([${data}])
33+
)`,
34+
Instance:
35+
polyfill +
36+
`module.exports = importObject => WebAssembly.instantiate(
37+
new WebAssembly.Module(Buffer.from([${data}])),
38+
importObject
39+
)`,
40+
Both:
41+
polyfill +
42+
`module.exports = importObject => WebAssembly.instantiate(
43+
Buffer.from([${data}]), importObject
44+
)`
45+
}
3346
};
3447
}

0 commit comments

Comments
 (0)