Skip to content

Commit f8cbaa8

Browse files
Fix inheritance of polyfilled WebAssembly.Module
The polyfilled WebAssembly.Module instance was not inheriting from the polyfilled constructor's prototype when it's created by WebAssembly.compile or WebAssembly.compileStreaming.
1 parent 4e100e4 commit f8cbaa8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

polyfill.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,19 @@ export function polyfill(WebAssembly) {
6060
module[polyfilledImportsSymbol] = parseImports(sourceBytes);
6161
}
6262

63+
// Modify the module's prototype chain to make the following inheritance
64+
// test pass:
65+
// * `module instanceof WebAssembly.Module === true`
66+
// * `module instanceof newWebAssembly.Module === true`
67+
const prependInheritance = (module) => {
68+
Object.setPrototypeOf(module, newModule.prototype);
69+
}
70+
6371
// Hook the Module constructor to store the source bytes.
6472
const newModule = newWebAssembly.Module = function (bytes) {
6573
const module = new WebAssembly.Module(bytes);
6674
assignImports(module, bytes);
67-
Object.setPrototypeOf(module, newModule.prototype);
75+
prependInheritance(module);
6876
return module;
6977
}
7078
Object.setPrototypeOf(newModule.prototype, WebAssembly.Module.prototype);
@@ -73,6 +81,7 @@ export function polyfill(WebAssembly) {
7381
newWebAssembly.compile = async (source) => {
7482
const module = await WebAssembly.compile(source);
7583
assignImports(module, source);
84+
prependInheritance(module);
7685
return module;
7786
};
7887

@@ -83,6 +92,7 @@ export function polyfill(WebAssembly) {
8392
const clone = response.clone();
8493
const module = await WebAssembly.compileStreaming(response);
8594
assignImports(module, new Uint8Array(await clone.arrayBuffer()));
95+
prependInheritance(module);
8696
return module;
8797
};
8898
}

test/check.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ async function checkPolyfill() {
146146
} else {
147147
module = getModule.fn();
148148
}
149+
150+
if (module instanceof WebAssembly.Module === false) {
151+
process.stdout.write("\x1b[31mF\x1b[0m\n");
152+
console.error(`Expected module to be instance of the original WebAssembly.Module, but got something wrong by ${getModule.name}`);
153+
return false;
154+
}
155+
if (module instanceof polyfilledWebAssembly.Module === false) {
156+
process.stdout.write("\x1b[31mF\x1b[0m\n");
157+
console.error(`Expected module to be instance of the polyfilled WebAssembly.Module, but got something wrong by ${getModule.name}`);
158+
return false;
159+
}
160+
149161
imports = polyfilledWebAssembly.Module.imports(module);
150162
} catch (e) {
151163
process.stdout.write("\x1b[31mF\x1b[0m\n");

0 commit comments

Comments
 (0)