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-id | name | effect | what it does |
|---|---|---|---|
| 40000 | print | io | render the argument, append to the console |
| 40001 | println | io | render the argument, append a console line |
| 40002 | sqrt | pure | f64 square root |
| 40003 | abs | pure | absolute value of an i64 or an f64 |
| 40004 | assert | panic | a false argument is a runtime error |
| 40005 | len | pure | element count of an array, or char count of a string |
| 40006 | push | alloc | append a value to a heap array |
| 40007 | pop | alloc | remove the last element, return Option<T> |
| 40008 | type_of | pure | the runtime type name as a string |
| 40009 | open | io | open a mock file handle |
| 40010 | close | io | mark a file handle closed |
| 40011 | map | pure* | apply a callback to each array element |
| 40012 | filter | pure* | keep the elements a callback accepts |
| 40013 | reduce | pure* | fold an array with a callback |
| 40014 | read_all | io | read 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
CALLto its native implementation.