diff --git a/src/bindings/js.ts b/src/bindings/js.ts index 8876118b11..456b2f79ac 100644 --- a/src/bindings/js.ts +++ b/src/bindings/js.ts @@ -554,18 +554,10 @@ export class JSBuilder extends ExportsWalker { continue; } let resetPos = sb.length; - sb.push(": Object.assign(Object.create("); - if (moduleName == "env") { - sb.push("globalThis"); - } else { - sb.push("__module"); - sb.push(moduleId.toString()); - } - sb.push("), "); - if (moduleName == "env") { - sb.push("imports.env || {}, "); - } - sb.push("{\n"); + + // Use Object.setPrototypeOf to avoid issues with read-only properties + // on module objects created by bundlers (issue #2659) + sb.push(": Object.setPrototypeOf({\n"); ++this.indentLevel; let numInstrumented = 0; for (let _keys2 = Map_keys(module), j = 0, l = _keys2.length; j < l; ++j) { @@ -598,7 +590,15 @@ export class JSBuilder extends ExportsWalker { sb.push(",\n"); } else { indent(sb, this.indentLevel); - sb.push("}),\n"); + sb.push("}, "); + if (moduleName == "env") { + // TODO: If necessary, use "Object.setPrototypeOf(Object.assign({}, imports.env || {}), globalThis)" + sb.push("Object.assign(Object.create(globalThis), imports.env || {})"); + } else { + sb.push("__module"); + sb.push(moduleId.toString()); + } + sb.push("),\n"); } } --this.indentLevel; diff --git a/tests/compiler/bindings/esm.debug.js b/tests/compiler/bindings/esm.debug.js index 8ebfcb8c8f..e1a514ba47 100644 --- a/tests/compiler/bindings/esm.debug.js +++ b/tests/compiler/bindings/esm.debug.js @@ -1,6 +1,6 @@ async function instantiate(module, imports = {}) { const adaptedImports = { - env: Object.assign(Object.create(globalThis), imports.env || {}, { + env: Object.setPrototypeOf({ trace(message, n, a0, a1, a2, a3, a4) { // ~lib/builtins/trace(~lib/string/String, i32?, f64?, f64?, f64?, f64?, f64?) => void message = __liftString(message >>> 0); @@ -44,7 +44,7 @@ async function instantiate(module, imports = {}) { throw Error(`${message} in ${fileName}:${lineNumber}:${columnNumber}`); })(); }, - }), + }, Object.assign(Object.create(globalThis), imports.env || {})), }; const { exports } = await WebAssembly.instantiate(module, adaptedImports); const memory = exports.memory || imports.env.memory; diff --git a/tests/compiler/bindings/esm.release.js b/tests/compiler/bindings/esm.release.js index 7f0ac87f85..4f91c23e94 100644 --- a/tests/compiler/bindings/esm.release.js +++ b/tests/compiler/bindings/esm.release.js @@ -1,6 +1,6 @@ async function instantiate(module, imports = {}) { const adaptedImports = { - env: Object.assign(Object.create(globalThis), imports.env || {}, { + env: Object.setPrototypeOf({ trace(message, n, a0, a1, a2, a3, a4) { // ~lib/builtins/trace(~lib/string/String, i32?, f64?, f64?, f64?, f64?, f64?) => void message = __liftString(message >>> 0); @@ -44,7 +44,7 @@ async function instantiate(module, imports = {}) { throw Error(`${message} in ${fileName}:${lineNumber}:${columnNumber}`); })(); }, - }), + }, Object.assign(Object.create(globalThis), imports.env || {})), }; const { exports } = await WebAssembly.instantiate(module, adaptedImports); const memory = exports.memory || imports.env.memory; diff --git a/tests/compiler/bindings/noExportRuntime.debug.js b/tests/compiler/bindings/noExportRuntime.debug.js index 2521ab12b1..bdf08ae5ff 100644 --- a/tests/compiler/bindings/noExportRuntime.debug.js +++ b/tests/compiler/bindings/noExportRuntime.debug.js @@ -1,6 +1,6 @@ async function instantiate(module, imports = {}) { const adaptedImports = { - env: Object.assign(Object.create(globalThis), imports.env || {}, { + env: Object.setPrototypeOf({ abort(message, fileName, lineNumber, columnNumber) { // ~lib/builtins/abort(~lib/string/String | null?, ~lib/string/String | null?, u32?, u32?) => void message = __liftString(message >>> 0); @@ -12,7 +12,7 @@ async function instantiate(module, imports = {}) { throw Error(`${message} in ${fileName}:${lineNumber}:${columnNumber}`); })(); }, - }), + }, Object.assign(Object.create(globalThis), imports.env || {})), }; const { exports } = await WebAssembly.instantiate(module, adaptedImports); const memory = exports.memory || imports.env.memory; diff --git a/tests/compiler/bindings/noExportRuntime.release.js b/tests/compiler/bindings/noExportRuntime.release.js index 26ab44837a..24a1b9596b 100644 --- a/tests/compiler/bindings/noExportRuntime.release.js +++ b/tests/compiler/bindings/noExportRuntime.release.js @@ -1,6 +1,6 @@ async function instantiate(module, imports = {}) { const adaptedImports = { - env: Object.assign(Object.create(globalThis), imports.env || {}, { + env: Object.setPrototypeOf({ abort(message, fileName, lineNumber, columnNumber) { // ~lib/builtins/abort(~lib/string/String | null?, ~lib/string/String | null?, u32?, u32?) => void message = __liftString(message >>> 0); @@ -12,7 +12,7 @@ async function instantiate(module, imports = {}) { throw Error(`${message} in ${fileName}:${lineNumber}:${columnNumber}`); })(); }, - }), + }, Object.assign(Object.create(globalThis), imports.env || {})), }; const { exports } = await WebAssembly.instantiate(module, adaptedImports); const memory = exports.memory || imports.env.memory; diff --git a/tests/compiler/bindings/raw.debug.js b/tests/compiler/bindings/raw.debug.js index cc59b6a796..564035a225 100644 --- a/tests/compiler/bindings/raw.debug.js +++ b/tests/compiler/bindings/raw.debug.js @@ -1,6 +1,6 @@ export async function instantiate(module, imports = {}) { const adaptedImports = { - env: Object.assign(Object.create(globalThis), imports.env || {}, { + env: Object.setPrototypeOf({ trace(message, n, a0, a1, a2, a3, a4) { // ~lib/builtins/trace(~lib/string/String, i32?, f64?, f64?, f64?, f64?, f64?) => void message = __liftString(message >>> 0); @@ -44,7 +44,7 @@ export async function instantiate(module, imports = {}) { throw Error(`${message} in ${fileName}:${lineNumber}:${columnNumber}`); })(); }, - }), + }, Object.assign(Object.create(globalThis), imports.env || {})), }; const { exports } = await WebAssembly.instantiate(module, adaptedImports); const memory = exports.memory || imports.env.memory; diff --git a/tests/compiler/bindings/raw.release.js b/tests/compiler/bindings/raw.release.js index cc59b6a796..564035a225 100644 --- a/tests/compiler/bindings/raw.release.js +++ b/tests/compiler/bindings/raw.release.js @@ -1,6 +1,6 @@ export async function instantiate(module, imports = {}) { const adaptedImports = { - env: Object.assign(Object.create(globalThis), imports.env || {}, { + env: Object.setPrototypeOf({ trace(message, n, a0, a1, a2, a3, a4) { // ~lib/builtins/trace(~lib/string/String, i32?, f64?, f64?, f64?, f64?, f64?) => void message = __liftString(message >>> 0); @@ -44,7 +44,7 @@ export async function instantiate(module, imports = {}) { throw Error(`${message} in ${fileName}:${lineNumber}:${columnNumber}`); })(); }, - }), + }, Object.assign(Object.create(globalThis), imports.env || {})), }; const { exports } = await WebAssembly.instantiate(module, adaptedImports); const memory = exports.memory || imports.env.memory;