-
Notifications
You must be signed in to change notification settings - Fork 12
fix: mark threads as experimental #49
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,25 +6,34 @@ import { closeSync } from 'node:fs'; | |
|
||
async function createDevNullFDs() { | ||
const [stdin, stdout] = await Promise.all([open(devNull, 'r'), open(devNull, 'w')]); | ||
|
||
let needsClose = true; | ||
const fr = new globalThis.FinalizationRegistry((held: number) => { | ||
try { | ||
closeSync(held); | ||
if (needsClose) closeSync(held); | ||
} catch { | ||
// The fd may already be closed. | ||
} | ||
}); | ||
fr.register(stdin, stdin.fd); | ||
fr.register(stdout, stdout.fd); | ||
|
||
return [stdin.fd, stdout.fd, stdout.fd]; | ||
return { | ||
async close() { | ||
needsClose = false; | ||
await Promise.all([stdin.close(), stdout.close()]).catch(() => {}); | ||
}, | ||
fds: [stdin.fd, stdout.fd, stdout.fd], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deno, unlike Node, runs its "leaked file descriptor" check before the |
||
}; | ||
} | ||
|
||
export async function loadWasi( | ||
allowedPaths: { [from: string]: string }, | ||
enableWasiOutput: boolean, | ||
): Promise<InternalWasi> { | ||
const [stdin, stdout, stderr] = enableWasiOutput ? [0, 1, 2] : await createDevNullFDs(); | ||
const { | ||
close, | ||
fds: [stdin, stdout, stderr], | ||
} = enableWasiOutput ? { async close() {}, fds: [0, 1, 2] } : await createDevNullFDs(); | ||
const context = new Context({ | ||
preopens: allowedPaths, | ||
exitOnReturn: false, | ||
|
@@ -38,6 +47,10 @@ export async function loadWasi( | |
return context.exports; | ||
}, | ||
|
||
async close() { | ||
await close(); | ||
}, | ||
|
||
async initialize(instance: WebAssembly.Instance) { | ||
const memory = instance.exports.memory as WebAssembly.Memory; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,25 +6,44 @@ import { closeSync } from 'node:fs'; | |
|
||
async function createDevNullFDs() { | ||
const [stdin, stdout] = await Promise.all([open(devNull, 'r'), open(devNull, 'w')]); | ||
let needsClose = true; | ||
// TODO: make this check always run when bun fixes [1], so `fs.promises.open()` returns a `FileHandle` as expected. | ||
// [1]: https://github.com/oven-sh/bun/issues/5918 | ||
let close = async () => { | ||
closeSync(stdin as any); | ||
closeSync(stdout as any); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... And Bun returns the wrong type from |
||
if (typeof stdin !== 'number') { | ||
const fr = new globalThis.FinalizationRegistry((held: number) => { | ||
try { | ||
if (needsClose) closeSync(held); | ||
} catch { | ||
// The fd may already be closed. | ||
} | ||
}); | ||
|
||
const fr = new globalThis.FinalizationRegistry((held: number) => { | ||
try { | ||
closeSync(held); | ||
} catch { | ||
// The fd may already be closed. | ||
} | ||
}); | ||
fr.register(stdin, stdin.fd); | ||
fr.register(stdout, stdout.fd); | ||
fr.register(stdin, stdin.fd); | ||
fr.register(stdout, stdout.fd); | ||
close = async () => { | ||
needsClose = false; | ||
await Promise.all([stdin.close(), stdout.close()]).catch(() => {}); | ||
}; | ||
} | ||
|
||
return [stdin.fd, stdout.fd, stdout.fd]; | ||
return { | ||
close, | ||
fds: [stdin.fd, stdout.fd, stdout.fd], | ||
}; | ||
} | ||
|
||
export async function loadWasi( | ||
allowedPaths: { [from: string]: string }, | ||
enableWasiOutput: boolean, | ||
): Promise<InternalWasi> { | ||
const [stdin, stdout, stderr] = enableWasiOutput ? [0, 1, 2] : await createDevNullFDs(); | ||
const { | ||
close, | ||
fds: [stdin, stdout, stderr], | ||
} = enableWasiOutput ? { async close() {}, fds: [0, 1, 2] } : await createDevNullFDs(); | ||
|
||
const context = new WASI({ | ||
version: 'preview1', | ||
|
@@ -39,6 +58,10 @@ export async function loadWasi( | |
return context.wasiImport; | ||
}, | ||
|
||
async close() { | ||
await close(); | ||
}, | ||
|
||
async initialize(instance: WebAssembly.Instance) { | ||
const memory = instance.exports.memory as WebAssembly.Memory; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(It's a blink-and-you'll-miss-it diff – this is what actually changes the default threading behavior of the plugin.)