Skip to content

Commit 4a27285

Browse files
PackageToJS: Add WebAssembly namespace option to instantiate
1 parent 253ab0b commit 4a27285

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Diff for: Plugins/PackageToJS/Templates/instantiate.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export type ModuleSource = WebAssembly.Module | ArrayBufferView | ArrayBuffer |
5656
* The options for instantiating a WebAssembly module
5757
*/
5858
export type InstantiateOptions = {
59+
/**
60+
* The WebAssembly namespace to use for instantiation.
61+
* Defaults to the globalThis.WebAssembly object.
62+
*/
63+
WebAssembly?: typeof globalThis.WebAssembly,
5964
/**
6065
* The WebAssembly module to instantiate
6166
*/

Diff for: Plugins/PackageToJS/Templates/instantiate.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export async function instantiateForThread(
6363
async function _instantiate(
6464
options
6565
) {
66+
const _WebAssembly = options.WebAssembly || WebAssembly;
6667
const moduleSource = options.module;
6768
/* #if IS_WASI */
6869
const { wasi } = options;
@@ -98,23 +99,23 @@ async function _instantiate(
9899
let module;
99100
let instance;
100101
let exports;
101-
if (moduleSource instanceof WebAssembly.Module) {
102+
if (moduleSource instanceof _WebAssembly.Module) {
102103
module = moduleSource;
103-
instance = await WebAssembly.instantiate(module, importObject);
104+
instance = await _WebAssembly.instantiate(module, importObject);
104105
} else if (typeof Response === "function" && (moduleSource instanceof Response || moduleSource instanceof Promise)) {
105-
if (typeof WebAssembly.instantiateStreaming === "function") {
106-
const result = await WebAssembly.instantiateStreaming(moduleSource, importObject);
106+
if (typeof _WebAssembly.instantiateStreaming === "function") {
107+
const result = await _WebAssembly.instantiateStreaming(moduleSource, importObject);
107108
module = result.module;
108109
instance = result.instance;
109110
} else {
110111
const moduleBytes = await (await moduleSource).arrayBuffer();
111-
module = await WebAssembly.compile(moduleBytes);
112-
instance = await WebAssembly.instantiate(module, importObject);
112+
module = await _WebAssembly.compile(moduleBytes);
113+
instance = await _WebAssembly.instantiate(module, importObject);
113114
}
114115
} else {
115116
// @ts-expect-error: Type 'Response' is not assignable to type 'BufferSource'
116-
module = await WebAssembly.compile(moduleSource);
117-
instance = await WebAssembly.instantiate(module, importObject);
117+
module = await _WebAssembly.compile(moduleSource);
118+
instance = await _WebAssembly.instantiate(module, importObject);
118119
}
119120

120121
swift.setInstance(instance);

0 commit comments

Comments
 (0)