char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
f20b64f
main
1import { 2ObjectFreeze , 3TypedArrayPrototypeGetByteLength as length , 4TypedArrayPrototypeGetTag as tag , 5TypeError , 6Uint8Array , 7Uint8ArrayPrototypeSet as setBytes , 8} from "ext:core/primordials" ; 9import { op_stdio_read_sync , op_stdio_write_sync } from "ext:core/ops" ; 10import { array } from "ext:web/idl.js" ; 11import { result } from "ext:file/errors.js" ; 12 13const empty = new Uint8Array (); 14 15export const stdin = ObjectFreeze ({ 16read ( buffer :Uint8Array < ArrayBufferLike >) :number | null { 17if ( tag ( buffer ) !== "Uint8Array" ) { 18throw new TypeError ( "Stdin reads require a Uint8Array" ); 19} 20setBytes ( buffer , empty ); 21const chunk = result ( op_stdio_read_sync ( length ( buffer ))); 22setBytes ( buffer , empty ); 23if ( chunk === null ) return null ; 24setBytes ( buffer , chunk ); 25return length ( chunk ); 26}, 27}); 28 29function write ( stderr :boolean , bytes :Uint8Array < ArrayBufferLike >) :number { 30if ( tag ( bytes ) !== "Uint8Array" ) { 31throw new TypeError ( "Stdio writes require a Uint8Array" ); 32} 33const args = array () as [ boolean , Uint8Array < ArrayBuffer >]; 34args [ 0 ] = stderr ; 35args [ 1 ] = new Uint8Array ( bytes ); 36return result ( op_stdio_write_sync ( args )); 37} 38 39export const stdout = ObjectFreeze ({ 40write ( bytes :Uint8Array < ArrayBufferLike >) { 41return write ( false , bytes ); 42}, 43}); 44 45export const stderr = ObjectFreeze ({ 46write ( bytes :Uint8Array < ArrayBufferLike >) { 47return write ( true , bytes ); 48}, 49});