cli: add safe UTF-8 and supplied-buffer hex/base64 std modules
This commit is contained in:
42 files changed
+2807
-14
No files matched your search
@@ -478,7 +478,7 @@ Lowering MUST append the complete type and function declarations of the imported
|
||||
|
||||
Imports grant no authority, perform no runtime initialization and allocate no program heap memory. Merely importing a module does not add effects to the caller's contract; calling an effectful function would require its declared effects under the usual call rule.
|
||||
|
||||
Canonical AIR MUST be independent of import order, working directory and execution profile for a fixed compiler and program. Source diagnostics and traps inside a standard function MUST identify its stable repository-relative source path (`std/math.aji` or `std/bytes.aji`) and its original line/column, rather than the program's path. `aji lower --annotate` MUST include that path on annotations for imported instructions. AIR locations remain available as in §10.
|
||||
Canonical AIR MUST be independent of import order, working directory and execution profile for a fixed compiler and program. Source diagnostics and traps inside a standard function MUST identify its stable repository-relative source path (spec 16 §2) and its original line/column, rather than the program's path. `aji lower --annotate` MUST include that path on annotations for imported instructions. AIR locations remain available as in §10.
|
||||
|
||||
Standard enum types are named by their full path, for example `std.bytes.ReadU32`. In type annotations, constructors and match patterns, that path MUST resolve only to a type from an explicitly imported standard module. An unknown module/type or missing import is `E7002`. Importing a module MUST NOT introduce short type names. Standard declarations with the same short names as user types remain distinct.
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ APIs SHOULD offer one canonical operation for a common need, small documentation
|
||||
|
||||
## 2. Bootstrap module registry
|
||||
|
||||
The built-in registry contains `std.math` and `std.bytes`. Both are implemented in safe `.aji` with `effects []`. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. All operations are deterministic in every profile, have no concurrency effects and allocate no heap memory. Ordinary interpreter fuel limits apply.
|
||||
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.math` and `std.utf8`. They are implemented in safe `.aji` with `effects []`; their stable source paths are `std/bytes.aji`, `std/encoding/base64.aji`, `std/encoding/hex.aji`, `std/math.aji` and `std/utf8.aji`, respectively. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. All operations are deterministic in every profile, have no concurrency effects and allocate no heap memory. Ordinary interpreter fuel limits apply.
|
||||
|
||||
### 2.1 `std.math`
|
||||
|
||||
@@ -82,3 +82,44 @@ It MUST return `found(index)` for the least index at which all needle bytes matc
|
||||
Splitting at the first byte-string separator uses `find_slice`, then the existing explicit sub-slice or `split_at` forms (spec 15 §6). On `found(index)`, the left part is `data[0..index]`, the separator `data[index..index + separator.len]` and the right part `data[index + separator.len..data.len]`. On `not_found` the caller chooses how to handle an absent separator. An empty separator yields an empty left part and the whole data as the right part; repeated splitting with an empty separator needs a caller-defined progress rule. There is no standard split iterator, returned aggregate of borrows or hidden cursor. Single-byte splitting MAY use the existing `find` length sentinel, checked before slicing.
|
||||
|
||||
Both functions declare `effects []`, allocate no heap, retain no borrow and consume no owner/resource. Valid slices MUST NOT produce bounds or integer traps; fuel exhaustion remains an environment outcome. The invariant is existing shared-read/exclusive-write access within the supplied slices, with no partial mutation on a short-output result. Existing call, alias, lifetime, initialization, capacity/bounds and enum transfer functions apply, with no new privilege, diagnostic code, AIR feature or runtime rule. Deterministic and hosted profiles behave identically. Existing APIs and AIR format 0 retain their contracts.
|
||||
|
||||
### 2.5 UTF-8
|
||||
|
||||
`std.utf8` interprets UTF-8 as the shortest encoding of Unicode scalar values 0..0x10FFFF excluding 0xD800..0xDFFF, following RFC 3629 §3/§4. Unassigned scalar values, noncharacters, NUL and U+FEFF are valid. No normalization, replacement decoding, locale handling or BOM removal occurs.
|
||||
|
||||
```aji
|
||||
enum NextResult { ok(u32, usize), end, invalid_input }
|
||||
enum ValidateResult { ok, invalid_input(usize) }
|
||||
```
|
||||
|
||||
- `next(src: slice<u8>) -> std.utf8.NextResult` MUST return `end` exactly when src is empty. Otherwise it MUST decode only the first scalar: `ok(value, byte_count)` with 1..4 consumed bytes, or `invalid_input` if the first sequence is malformed or truncated. Trailing bytes after a successful first sequence MUST NOT affect that result. Iteration uses an explicit caller offset and a sub-slice, without retained cursor state or borrows.
|
||||
- `validate(src: slice<u8>) -> std.utf8.ValidateResult` MUST return `ok` for a wholly valid sequence, including empty input. Otherwise it MUST return `invalid_input(offset)` at the beginning of the first malformed or truncated sequence, relative to src, after the maximal valid prefix. The offset identifies the sequence's leading byte, not necessarily the bad continuation byte.
|
||||
|
||||
ASCII bytes 00..7F are single-byte scalars. Multi-byte leading bytes are C2..DF, E0..EF and F0..F4 with two, three or four total bytes, respectively. Continuations MUST be 80..BF; the second byte MUST be A0..BF after E0, 80..9F after ED, 90..BF after F0 and 80..8F after F4. These conditions reject overlong forms, surrogates and out-of-range scalars. Invalid input is a result, never replacement text or a trap. Validation and explicit iteration use O(src.len) work and constant auxiliary storage.
|
||||
|
||||
### 2.6 Supplied-buffer encodings
|
||||
|
||||
Both `std.encoding.hex` and `std.encoding.base64` export these ordinary Copy enums, distinct between modules:
|
||||
|
||||
```aji
|
||||
enum EncodeResult { ok(usize), short_output }
|
||||
enum DecodeResult { ok(usize), invalid_input, short_output }
|
||||
```
|
||||
|
||||
Each module provides `encode(dst: slice_mut<u8>, src: slice<u8>) -> EncodeResult` and `decode(dst: slice_mut<u8>, src: slice<u8>) -> DecodeResult`, with fully qualified result types. `ok(count)` MUST report the number of output bytes. Successful operations MUST write exactly the output prefix and leave remaining destination bytes unchanged; no terminator is appended. Empty input MUST succeed with `ok(0)` for any capacity, including zero.
|
||||
|
||||
Encoding MUST check full output capacity before writing. If the mathematical encoded length exceeds either usize or output capacity, it MUST return `short_output`, leaving the entire destination unchanged; guards MUST avoid intermediate overflow. Decoding MUST validate the entire input before any destination write. Malformed input MUST return `invalid_input`, even with insufficient output capacity. Valid input with insufficient capacity MUST return `short_output`. Both error results MUST leave the entire destination unchanged. Ordinary shared/exclusive alias rules (`E2101`) apply even on failure paths; overlapping input/output is not allowed. Guarantees about unchanged output apply to returned errors, not interpreter fuel exhaustion mid-operation.
|
||||
|
||||
#### 2.6.1 Hex
|
||||
|
||||
`std.encoding.hex.encode` MUST produce two lowercase ASCII hexadecimal digits per byte, most-significant nibble first, with mathematical length 2 × src.len. `decode` MUST accept exactly even-length sequences of ASCII digits 0..9, a..f or A..F, interpreting each pair as one byte. Mixed case is accepted. Odd lengths, separators, prefixes, whitespace and other bytes are `invalid_input`. There is no padding.
|
||||
|
||||
#### 2.6.2 Base64
|
||||
|
||||
`std.encoding.base64` uses RFC 4648 §4's standard alphabet `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`, with `=` padding, no line breaks and zero unused bits. Encoded length is 4 × ceil(src.len / 3). Implementations MUST check that this length fits without overflowing an intermediate such as an unconditional `src.len + 2`.
|
||||
|
||||
Decoding MUST accept only canonical standard Base64: input length divisible by four; alphabet symbols in every unpadded position; zero, one or two padding symbols only at the end; and zero unused low bits (four low bits in the second symbol for `==`, two low bits in the third for `=`). Padding is required for incomplete final groups, and omitted for complete groups. Whitespace, URL-safe `-`/`_`, extra/misplaced padding, incomplete groups and nonzero unused bits are `invalid_input`. Empty input is canonical. Invalid-input precedence and unchanged-output guarantees are those of §2.6.
|
||||
|
||||
### 2.7 Text/encoding safety and compatibility
|
||||
|
||||
Sections 2.5/2.6 use safe source, ordinary calls, integer operations, slices, local fixed-size tables and enums. They declare `effects []`, allocate no heap, retain no borrow and consume no owner/resource. Work is O(input length) with constant auxiliary storage. The preserved invariant is shared-read/exclusive-write access within supplied initialized slices, no escaping borrow and no mutation on returned encoding errors. Existing verifier call/alias/lifetime/bounds/initialization/enum rules and deterministic interpreter behavior apply without a privilege, new transfer function, diagnostic, AIR feature or runtime primitive. Valid slices MUST NOT produce bounds, arithmetic, cast or shift traps; malformed bytes and capacity failure are results. Deterministic and hosted profiles behave identically. Existing APIs, source syntax and AIR format 0 retain their contracts.
|
||||
Reference in new issue
Block a user