Skip to content

Commit

Permalink
fix: detect execution environment when loading wasm module (#2855)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrCai0907 authored Jun 21, 2024
1 parent be49610 commit 78963c5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/bindings/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,9 @@ 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)); }
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;
Expand Down
5 changes: 3 additions & 2 deletions tests/compiler/bindings/esm.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,9 @@ 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)); }
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));
5 changes: 3 additions & 2 deletions tests/compiler/bindings/esm.release.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,9 @@ 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)); }
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));
5 changes: 3 additions & 2 deletions tests/compiler/bindings/noExportRuntime.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ 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)); }
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));
5 changes: 3 additions & 2 deletions tests/compiler/bindings/noExportRuntime.release.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ 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)); }
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));

0 comments on commit 78963c5

Please sign in to comment.