diff options
Diffstat (limited to 'ext/io/12_io.js')
-rw-r--r-- | ext/io/12_io.js | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/ext/io/12_io.js b/ext/io/12_io.js index c43117cef..094868371 100644 --- a/ext/io/12_io.js +++ b/ext/io/12_io.js @@ -9,6 +9,7 @@ import { op_set_raw } from "ext:core/ops"; const { Uint8Array, ArrayPrototypePush, + Symbol, TypedArrayPrototypeSubarray, TypedArrayPrototypeSet, TypedArrayPrototypeGetByteLength, @@ -181,9 +182,14 @@ const STDIN_RID = 0; const STDOUT_RID = 1; const STDERR_RID = 2; +const REF = Symbol("REF"); +const UNREF = Symbol("UNREF"); + class Stdin { #rid = STDIN_RID; + #ref = true; #readable; + #opPromise; constructor() { } @@ -197,8 +203,14 @@ class Stdin { return this.#rid; } - read(p) { - return read(this.#rid, p); + async read(p) { + if (p.length === 0) return 0; + this.#opPromise = core.read(this.#rid, p); + if (!this.#ref) { + core.unrefOpPromise(this.#opPromise); + } + const nread = await this.#opPromise; + return nread === 0 ? null : nread; } readSync(p) { @@ -224,6 +236,20 @@ class Stdin { isTerminal() { return core.isTerminal(this.#rid); } + + [REF]() { + this.#ref = true; + if (this.#opPromise) { + core.refOpPromise(this.#opPromise); + } + } + + [UNREF]() { + this.#ref = false; + if (this.#opPromise) { + core.unrefOpPromise(this.#opPromise); + } + } } class Stdout { @@ -318,6 +344,7 @@ export { readAll, readAllSync, readSync, + REF, SeekMode, Stderr, stderr, @@ -327,6 +354,7 @@ export { Stdout, stdout, STDOUT_RID, + UNREF, write, writeSync, }; |