diff --git a/Plugins/PackageToJS/Templates/instantiate.d.ts b/Plugins/PackageToJS/Templates/instantiate.d.ts index 11837aba..2d81ddde 100644 --- a/Plugins/PackageToJS/Templates/instantiate.d.ts +++ b/Plugins/PackageToJS/Templates/instantiate.d.ts @@ -93,12 +93,30 @@ export type InstantiateOptions = { /** * Add imports to the WebAssembly import object * @param imports - The imports to add + * @param context - The context object */ addToCoreImports?: ( imports: WebAssembly.Imports, - getInstance: () => WebAssembly.Instance | null, - getExports: () => Exports | null, + context: { + getInstance: () => WebAssembly.Instance | null, + getExports: () => Exports | null, + _swift: SwiftRuntime, + } ) => void + + /** + * Instrument the WebAssembly instance + * + * @param instance - The instance of the WebAssembly module + * @param context - The context object + * @returns The instrumented instance + */ + instrumentInstance?: ( + instance: WebAssembly.Instance, + context: { + _swift: SwiftRuntime + } + ) => WebAssembly.Instance } /** diff --git a/Plugins/PackageToJS/Templates/instantiate.js b/Plugins/PackageToJS/Templates/instantiate.js index 08351e67..4a3a3222 100644 --- a/Plugins/PackageToJS/Templates/instantiate.js +++ b/Plugins/PackageToJS/Templates/instantiate.js @@ -94,7 +94,11 @@ async function _instantiate( /* #endif */ }; instantiator.addImports(importObject); - options.addToCoreImports?.(importObject, () => instance, () => exports); + options.addToCoreImports?.(importObject, { + getInstance: () => instance, + getExports: () => exports, + _swift: swift, + }); let module; let instance; @@ -117,6 +121,7 @@ async function _instantiate( module = await _WebAssembly.compile(moduleSource); instance = await _WebAssembly.instantiate(module, importObject); } + instance = options.instrumentInstance?.(instance, { _swift: swift }) ?? instance; swift.setInstance(instance); instantiator.setInstance(instance); diff --git a/Plugins/PackageToJS/Templates/test.js b/Plugins/PackageToJS/Templates/test.js index 8c443249..b44b0d6e 100644 --- a/Plugins/PackageToJS/Templates/test.js +++ b/Plugins/PackageToJS/Templates/test.js @@ -171,8 +171,8 @@ export async function testBrowserInPage(options, processInfo) { // Instantiate the WebAssembly file return await instantiate({ ...options, - addToCoreImports: (imports) => { - options.addToCoreImports?.(imports); + addToCoreImports: (imports, context) => { + options.addToCoreImports?.(imports, context); imports["wasi_snapshot_preview1"]["proc_exit"] = (code) => { exitTest(code); throw new ExitError(code); diff --git a/Tests/prelude.mjs b/Tests/prelude.mjs index 419eb522..2501bd58 100644 --- a/Tests/prelude.mjs +++ b/Tests/prelude.mjs @@ -4,8 +4,9 @@ export function setupOptions(options, context) { setupTestGlobals(globalThis); return { ...options, - addToCoreImports(importObject, getInstance, getExports) { - options.addToCoreImports?.(importObject); + addToCoreImports(importObject, importsContext) { + const { getInstance, getExports } = importsContext; + options.addToCoreImports?.(importObject, importsContext); importObject["JavaScriptEventLoopTestSupportTests"] = { "isMainThread": () => context.isMainThread, }