Skip to content

PackageToJS: Add WebAssembly namespace option to instantiate #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
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
5 changes: 5 additions & 0 deletions Plugins/PackageToJS/Templates/instantiate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export type ModuleSource = WebAssembly.Module | ArrayBufferView | ArrayBuffer |
* The options for instantiating a WebAssembly module
*/
export type InstantiateOptions = {
/**
* The WebAssembly namespace to use for instantiation.
* Defaults to the globalThis.WebAssembly object.
*/
WebAssembly?: typeof globalThis.WebAssembly,
/**
* The WebAssembly module to instantiate
*/
Expand Down
17 changes: 9 additions & 8 deletions Plugins/PackageToJS/Templates/instantiate.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export async function instantiateForThread(
async function _instantiate(
options
) {
const _WebAssembly = options.WebAssembly || WebAssembly;
const moduleSource = options.module;
/* #if IS_WASI */
const { wasi } = options;
Expand Down Expand Up @@ -98,23 +99,23 @@ async function _instantiate(
let module;
let instance;
let exports;
if (moduleSource instanceof WebAssembly.Module) {
if (moduleSource instanceof _WebAssembly.Module) {
module = moduleSource;
instance = await WebAssembly.instantiate(module, importObject);
instance = await _WebAssembly.instantiate(module, importObject);
} else if (typeof Response === "function" && (moduleSource instanceof Response || moduleSource instanceof Promise)) {
if (typeof WebAssembly.instantiateStreaming === "function") {
const result = await WebAssembly.instantiateStreaming(moduleSource, importObject);
if (typeof _WebAssembly.instantiateStreaming === "function") {
const result = await _WebAssembly.instantiateStreaming(moduleSource, importObject);
module = result.module;
instance = result.instance;
} else {
const moduleBytes = await (await moduleSource).arrayBuffer();
module = await WebAssembly.compile(moduleBytes);
instance = await WebAssembly.instantiate(module, importObject);
module = await _WebAssembly.compile(moduleBytes);
instance = await _WebAssembly.instantiate(module, importObject);
}
} else {
// @ts-expect-error: Type 'Response' is not assignable to type 'BufferSource'
module = await WebAssembly.compile(moduleSource);
instance = await WebAssembly.instantiate(module, importObject);
module = await _WebAssembly.compile(moduleSource);
instance = await _WebAssembly.instantiate(module, importObject);
}

swift.setInstance(instance);
Expand Down