Skip to main content

Module stdlib

Module stdlib 

Source
Expand description

the native-Rust standard library.

Qala’s standard library is fifteen functions implemented directly in Rust, not in Qala. codegen assigns each one a reserved fn-id at or above STDLIB_FN_BASE (40000); the VM’s CALL handler routes any such fn-id here through dispatch. user functions get dense ids 0..N into Program.chunks; the two id spaces never overlap.

the functions, in the order their fn-ids are assigned (the order MUST match codegen.rs’s STDLIB_TABLE – entry i is fn-id STDLIB_FN_BASE + i):

fn-idnameeffectwhat it does
40000printiorender the argument, append to the console
40001printlniorender the argument, append a console line
40002sqrtpuref64 square root
40003abspureabsolute value of an i64 or an f64
40004assertpanica false argument is a runtime error
40005lenpureelement count of an array, or char count of a string
40006pushallocappend a value to a heap array
40007popallocremove the last element, return Option<T>
40008type_ofpurethe runtime type name as a string
40009openioopen a mock file handle
40010closeiomark a file handle closed
40011mappure*apply a callback to each array element
40012filterpure*keep the elements a callback accepts
40013reducepure*fold an array with a callback
40014read_allioread a file handle’s content (FileHandle.read_all)

(map / filter / reduce inherit the callback’s effect; Phase 3’s effect inference already propagated that into the caller, so the native code here does not re-check effects.)

the effect column is consistent with the typechecker’s stdlib_signatures table – same fifteen functions, same effects. the typechecker owns the signature; this module owns the runtime behavior.

file I/O is a mock. open does NOT touch a real filesystem: the VM compiles to WASM and runs in a browser sandbox where there is no filesystem to reach. open builds a HeapObject::FileHandle with the requested path and an empty mock content string; close flips its closed flag; read_all returns the (empty) content as Ok. this is a deliberate v1 limitation – it makes the effect system and defer close(f) demonstrable in the playground without a real file API. print / println likewise write to the VM’s console buffer, not real stdout.

every function is total against untrusted bytecode: the fn-id and the argument values come from a CALL instruction the VM does not trust, so a wrong-arity or wrong-type call is a QalaError::Runtime, never a panic. dispatch rejects an unknown fn-id the same way.

Functions§

dispatch
dispatch a stdlib CALL to its native implementation.