#! @file fault.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains `fault`, which stops a program that has found a mistake of its own, #! saying what, as an index past the end of a view stops it, as a contract that #! fails does, `contracts`, which says whether they are checked, `checking`, #! which says whether the program checks what it does, and the faults the #! containers and allocators of `core` stop with. module core; #! Stops the program at once, as a fault does: the report is "runtime error: " #! and the text given, then the functions that were running, and the status is #! 1, as every fault's. #! #! This is for a mistake the program cannot go on from, as a container asked #! for an element past its end. It needs nothing of the system it runs on, so #! every place a program runs gives it, and it never returns. #! @param why What went wrong, as `Index 5 is out of bounds: the length is 3.` @intrinsic pub def fault(why: strview): void; #! Whether the program's contracts are checked: true unless it was built or #! run with `--contracts off`, which leaves out each `expects`, `asserts`, and #! `ensures`, conditions and all. #! #! It is known while compiling, so `if const contracts()` keeps work that only #! contracts read out of a program that leaves them out. #! @returns Whether they are. @intrinsic pub def contracts(): bool; #! Whether the program checks what it does: true where it runs on the machine, #! but for `run --fast`, and where it was built with `--checks`. #! #! A checked program stops at an index past the end, the wrong field of a union #! read, an integer converted to an enum none of whose members has its value, #! and a `match` finding no arm, each with what went wrong and where, as std's #! containers stop where asked for what they do not hold. One that is not #! checked does none of that, and reads and writes where the program said, as C #! does. Every program stops at null reached through, at a division by zero, #! and where its stack runs out. Contracts are apart from this, and `contracts` #! says whether they are checked. #! #! It is known while compiling, so the checks a container makes itself are #! written `if const checking() { ... }`, and an unchecked program has none of #! their code. #! @returns Whether it does. @intrinsic pub def checking(): bool; # Stops the program where a container of the language's own is asked for an # element past its end, as indexing a view outside its length does, where the # program checks. A container calls it only where its check fails, so that where # the check holds, it costs a compare, and the call is no more than its two # numbers, which keeps what calls it short enough to be emitted where it is # called. def past_end(index: u64, length: u64): void { fault($"Index {index} is out of bounds: the length is {length}."); } # Stops the program where an allocator gives no memory for what is asked of it, # as `raw_allocate` stops it where the system gives none, which every program # does, checked or not. def exhausted(size: u64): void { fault($"The allocator gave no memory for {size} bytes."); }