import { ObjectFreeze, TypedArrayPrototypeGetByteLength as length, TypedArrayPrototypeGetTag as tag, TypeError, Uint8Array, Uint8ArrayPrototypeSet as setBytes, } from "ext:core/primordials"; import { op_stdio_read_sync, op_stdio_write_sync } from "ext:core/ops"; import { array } from "ext:web/idl.js"; import { result } from "ext:file/errors.js"; const empty = new Uint8Array(); export const stdin = ObjectFreeze({ read(buffer: Uint8Array): number | null { if (tag(buffer) !== "Uint8Array") { throw new TypeError("Stdin reads require a Uint8Array"); } setBytes(buffer, empty); const chunk = result(op_stdio_read_sync(length(buffer))); setBytes(buffer, empty); if (chunk === null) return null; setBytes(buffer, chunk); return length(chunk); }, }); function write(stderr: boolean, bytes: Uint8Array): number { if (tag(bytes) !== "Uint8Array") { throw new TypeError("Stdio writes require a Uint8Array"); } const args = array() as [boolean, Uint8Array]; args[0] = stderr; args[1] = new Uint8Array(bytes); return result(op_stdio_write_sync(args)); } export const stdout = ObjectFreeze({ write(bytes: Uint8Array) { return write(false, bytes); }, }); export const stderr = ObjectFreeze({ write(bytes: Uint8Array) { return write(true, bytes); }, });