From 5deb2b88f91d8f08d846e45de92eba6e3f1f2515 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 20 Jun 2024 11:35:27 +0800 Subject: [PATCH 1/2] fix: use `typeof self === object && self.constructor` to identifier execution environment Use a better way to identifier exection environemt becasue `try` and `catch` will cause misleaded error message --- src/bindings/js.ts | 4 ++-- tests/compiler/bindings/esm.debug.js | 4 ++-- tests/compiler/bindings/esm.release.js | 4 ++-- tests/compiler/bindings/noExportRuntime.debug.js | 4 ++-- tests/compiler/bindings/noExportRuntime.release.js | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/bindings/js.ts b/src/bindings/js.ts index a86f77dec4..2b4ddda757 100644 --- a/src/bindings/js.ts +++ b/src/bindings/js.ts @@ -976,8 +976,8 @@ export class JSBuilder extends ExportsWalker { } sb.push(`} = await (async url => instantiate( await (async () => { - try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } + else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } })(), { `); let needsMaybeDefault = false; diff --git a/tests/compiler/bindings/esm.debug.js b/tests/compiler/bindings/esm.debug.js index d2d89456b8..33638760a7 100644 --- a/tests/compiler/bindings/esm.debug.js +++ b/tests/compiler/bindings/esm.debug.js @@ -553,8 +553,8 @@ export const { fn, } = await (async url => instantiate( await (async () => { - try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } + else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } })(), { } ))(new URL("esm.debug.wasm", import.meta.url)); diff --git a/tests/compiler/bindings/esm.release.js b/tests/compiler/bindings/esm.release.js index 30c529402d..44f3d1fe58 100644 --- a/tests/compiler/bindings/esm.release.js +++ b/tests/compiler/bindings/esm.release.js @@ -553,8 +553,8 @@ export const { fn, } = await (async url => instantiate( await (async () => { - try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } + else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } })(), { } ))(new URL("esm.release.wasm", import.meta.url)); diff --git a/tests/compiler/bindings/noExportRuntime.debug.js b/tests/compiler/bindings/noExportRuntime.debug.js index 9db98d97b4..5b911085c0 100644 --- a/tests/compiler/bindings/noExportRuntime.debug.js +++ b/tests/compiler/bindings/noExportRuntime.debug.js @@ -162,8 +162,8 @@ export const { takesFunction, } = await (async url => instantiate( await (async () => { - try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } + else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } })(), { } ))(new URL("noExportRuntime.debug.wasm", import.meta.url)); diff --git a/tests/compiler/bindings/noExportRuntime.release.js b/tests/compiler/bindings/noExportRuntime.release.js index de99664237..b24a1960e7 100644 --- a/tests/compiler/bindings/noExportRuntime.release.js +++ b/tests/compiler/bindings/noExportRuntime.release.js @@ -162,8 +162,8 @@ export const { takesFunction, } = await (async url => instantiate( await (async () => { - try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } + else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } })(), { } ))(new URL("noExportRuntime.release.wasm", import.meta.url)); From ea621ce36c39225d261ac47607b4e9d87be307ee Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 21 Jun 2024 00:19:07 +0800 Subject: [PATCH 2/2] support bun --- src/bindings/js.ts | 5 +++-- tests/compiler/bindings/esm.debug.js | 5 +++-- tests/compiler/bindings/esm.release.js | 5 +++-- tests/compiler/bindings/noExportRuntime.debug.js | 5 +++-- tests/compiler/bindings/noExportRuntime.release.js | 5 +++-- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/bindings/js.ts b/src/bindings/js.ts index 2b4ddda757..8876118b11 100644 --- a/src/bindings/js.ts +++ b/src/bindings/js.ts @@ -976,8 +976,9 @@ export class JSBuilder extends ExportsWalker { } sb.push(`} = await (async url => instantiate( await (async () => { - if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null); + if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } })(), { `); let needsMaybeDefault = false; diff --git a/tests/compiler/bindings/esm.debug.js b/tests/compiler/bindings/esm.debug.js index 33638760a7..8ebfcb8c8f 100644 --- a/tests/compiler/bindings/esm.debug.js +++ b/tests/compiler/bindings/esm.debug.js @@ -553,8 +553,9 @@ export const { fn, } = await (async url => instantiate( await (async () => { - if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null); + if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } })(), { } ))(new URL("esm.debug.wasm", import.meta.url)); diff --git a/tests/compiler/bindings/esm.release.js b/tests/compiler/bindings/esm.release.js index 44f3d1fe58..7f0ac87f85 100644 --- a/tests/compiler/bindings/esm.release.js +++ b/tests/compiler/bindings/esm.release.js @@ -553,8 +553,9 @@ export const { fn, } = await (async url => instantiate( await (async () => { - if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null); + if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } })(), { } ))(new URL("esm.release.wasm", import.meta.url)); diff --git a/tests/compiler/bindings/noExportRuntime.debug.js b/tests/compiler/bindings/noExportRuntime.debug.js index 5b911085c0..2521ab12b1 100644 --- a/tests/compiler/bindings/noExportRuntime.debug.js +++ b/tests/compiler/bindings/noExportRuntime.debug.js @@ -162,8 +162,9 @@ export const { takesFunction, } = await (async url => instantiate( await (async () => { - if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null); + if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } })(), { } ))(new URL("noExportRuntime.debug.wasm", import.meta.url)); diff --git a/tests/compiler/bindings/noExportRuntime.release.js b/tests/compiler/bindings/noExportRuntime.release.js index b24a1960e7..26ab44837a 100644 --- a/tests/compiler/bindings/noExportRuntime.release.js +++ b/tests/compiler/bindings/noExportRuntime.release.js @@ -162,8 +162,9 @@ export const { takesFunction, } = await (async url => instantiate( await (async () => { - if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } - else { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null); + if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } + else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } })(), { } ))(new URL("noExportRuntime.release.wasm", import.meta.url));