Skip to content

Commit 42ead29

Browse files
Merge pull request #362 from swiftwasm/yt/extend-instantiation-hooks
PackageToJS: Extend instantiation hooks to allow instance instrumentation
2 parents 589b2d2 + cf58e0f commit 42ead29

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

Plugins/PackageToJS/Templates/instantiate.d.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,30 @@ export type InstantiateOptions = {
9393
/**
9494
* Add imports to the WebAssembly import object
9595
* @param imports - The imports to add
96+
* @param context - The context object
9697
*/
9798
addToCoreImports?: (
9899
imports: WebAssembly.Imports,
99-
getInstance: () => WebAssembly.Instance | null,
100-
getExports: () => Exports | null,
100+
context: {
101+
getInstance: () => WebAssembly.Instance | null,
102+
getExports: () => Exports | null,
103+
_swift: SwiftRuntime,
104+
}
101105
) => void
106+
107+
/**
108+
* Instrument the WebAssembly instance
109+
*
110+
* @param instance - The instance of the WebAssembly module
111+
* @param context - The context object
112+
* @returns The instrumented instance
113+
*/
114+
instrumentInstance?: (
115+
instance: WebAssembly.Instance,
116+
context: {
117+
_swift: SwiftRuntime
118+
}
119+
) => WebAssembly.Instance
102120
}
103121

104122
/**

Plugins/PackageToJS/Templates/instantiate.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ async function _instantiate(
9494
/* #endif */
9595
};
9696
instantiator.addImports(importObject);
97-
options.addToCoreImports?.(importObject, () => instance, () => exports);
97+
options.addToCoreImports?.(importObject, {
98+
getInstance: () => instance,
99+
getExports: () => exports,
100+
_swift: swift,
101+
});
98102

99103
let module;
100104
let instance;
@@ -117,6 +121,7 @@ async function _instantiate(
117121
module = await _WebAssembly.compile(moduleSource);
118122
instance = await _WebAssembly.instantiate(module, importObject);
119123
}
124+
instance = options.instrumentInstance?.(instance, { _swift: swift }) ?? instance;
120125

121126
swift.setInstance(instance);
122127
instantiator.setInstance(instance);

Plugins/PackageToJS/Templates/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ export async function testBrowserInPage(options, processInfo) {
171171
// Instantiate the WebAssembly file
172172
return await instantiate({
173173
...options,
174-
addToCoreImports: (imports) => {
175-
options.addToCoreImports?.(imports);
174+
addToCoreImports: (imports, context) => {
175+
options.addToCoreImports?.(imports, context);
176176
imports["wasi_snapshot_preview1"]["proc_exit"] = (code) => {
177177
exitTest(code);
178178
throw new ExitError(code);

Tests/prelude.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ export function setupOptions(options, context) {
44
setupTestGlobals(globalThis);
55
return {
66
...options,
7-
addToCoreImports(importObject, getInstance, getExports) {
8-
options.addToCoreImports?.(importObject);
7+
addToCoreImports(importObject, importsContext) {
8+
const { getInstance, getExports } = importsContext;
9+
options.addToCoreImports?.(importObject, importsContext);
910
importObject["JavaScriptEventLoopTestSupportTests"] = {
1011
"isMainThread": () => context.isMainThread,
1112
}

0 commit comments

Comments
 (0)