Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use typeof self === object && self.constructor to identifier execution environment #2855

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

@CountBleck CountBleck Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null);
const isNodeOrBun = typeof process != "undefined" && process.versions && (process.versions.node || process.versions.bun);

Also, at some point in the future, I think the readFile method can be removed, because Node.js in recent versions has fetch built-in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, let us wait for the new features in nodejs.

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));
Loading