Skip to content
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
22 changes: 20 additions & 2 deletions Plugins/PackageToJS/Templates/instantiate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
7 changes: 6 additions & 1 deletion Plugins/PackageToJS/Templates/instantiate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Plugins/PackageToJS/Templates/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions Tests/prelude.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down