cli: add bounded std copy/search and checked arithmetic

This commit is contained in:
dev committed 2026-10-06 15:40:57 +03:00
1 parent a2ca293506
commit 6397a317e2
36 files changed
+1533 -7

No files matched your search

+1 -1
View File
@@ -179,7 +179,7 @@ Lowering: structured control flow becomes blocks and terminators. A variable ass
| `s.len` | `slice.len`, or the static length of an array |
| `s[a..b]` | a sub-slice (`slice.sub` with start `a`, length `b - a`); a borrow, so it is bound or passed, never a value |
| `let (left, right) = s.split_at(k);` | `slice.split`: two disjoint borrows of `s`. Besides `atomic.compare_exchange` (§12), the only multi-result binding. There is no general tuple destructuring. |
| `slice.copy(mut dst, ref src)` | `slice.copy`, the only copy form |
| `slice.copy(mut dst, ref src)` | primitive `slice.copy`; insufficient capacity traps. Recoverable byte copy is `std.bytes.copy` (spec 16 §2.4). |
**Writes.**
+29
View File
@@ -20,6 +20,19 @@ The built-in registry contains `std.math` and `std.bytes`. Both are implemented
These are monomorphic functions, not a generic arithmetic facility. Checked/wrapping operators retain their existing semantics (spec 02); importing this module does not alter them.
#### 2.1.1 Recoverable unsigned arithmetic
For `T` equal to `u64` or `usize`, `checked_add_T(a: T, b: T)`, `checked_sub_T(a: T, b: T)` and `checked_mul_T(a: T, b: T)` return `std.math.U64Result` or `std.math.UsizeResult`, respectively:
```aji
enum U64Result { ok(u64), overflow }
enum UsizeResult { ok(usize), overflow }
```
Each operation MUST return `ok(value)` exactly when its mathematical result is representable in T, and `overflow` otherwise. Unsigned subtraction underflow is `overflow`. A successful zero is distinct from overflow. `usize` uses the target pointer width (spec 02); no fixed-width maximum or implicit cast is allowed. These functions MUST NOT trap for any scalar operands, apart from the execution environment's fuel limit, including multiplication by zero. They allocate no heap and declare `effects []`.
The functions use ordinary Copy enums and the existing integer and enum rules, with no new verifier/runtime primitive. Failure is handled by an exhaustive qualified enum match; no must-use rule or intrinsic outcome shorthand is added. Normal `+`, `-`, `*` still trap on overflow; wrapping uses `+%`, `-%`, `*%`. The library MUST NOT add synonymous wrapping wrappers for these operators. Signed arithmetic and other widths are outside this monomorphic increment; general numeric APIs require separate contracts.
### 2.2 `std.bytes`
Inputs are byte slices, not encoded text. No UTF-8 validation or locale interpretation occurs. Read-only parameters are `slice<u8>`.
@@ -53,3 +66,19 @@ These declarations belong to `std.bytes` and are named with that prefix (spec 15
Short slices are recoverable enum results, not traps. A successful zero value is distinct from a short input. These APIs declare `effects []`, allocate no heap, retain no borrow and consume no owner/resource. They have no concurrency or authority requirements beyond the ordinary slice borrow contract. All capacity checks precede all accesses/mutations. Valid slices and values MUST NOT produce bounds, arithmetic, cast or shift traps; interpreter fuel exhaustion remains an environment outcome.
The verifier uses the existing enum payload, call, shared/exclusive borrow, lifetime and bounds rules. There is no new transfer function, runtime primitive or AIR feature. Ordinary enum construction and exhaustive `enum.switch` lower the result handling. Existing `E7002`, `E7004`, `E7005`, `E7006`, `E2101` cover invalid names/matches/calls and aliasing. The behavior is identical in deterministic and hosted profiles. Existing AIR and the existing nine library operations retain their semantics; imported source receives additional declarations pinned to the compiler version.
### 2.4 Bounded copy, substring search and splitting
`std.bytes.copy(dst: slice_mut<u8>, src: slice<u8>) -> std.bytes.WriteResult` MUST return `short_output` when `dst.len < src.len`, leaving every destination byte unchanged and reading no source elements. Otherwise it MUST copy exactly `src.len` bytes into the beginning of `dst`, preserve remaining destination bytes and return `ok`. Empty input succeeds, including with an empty output. Capacity is checked before any copy. Source/destination overlap MUST be rejected by the existing shared/exclusive alias rules (`E2101`), even if the output is short or the slices are empty. This operation is not an overlap-safe move.
`std.bytes.find_slice(data: slice<u8>, needle: slice<u8>) -> std.bytes.FindResult` returns an ordinary Copy enum:
```aji
enum FindResult { found(usize), not_found }
```
It MUST return `found(index)` for the least index at which all needle bytes match, or `not_found` if there is no match. A found index satisfies `index <= data.len` and `needle.len <= data.len - index`. An empty needle MUST return `found(0)` for any data, including empty data, without reading elements. A needle longer than the data MUST return `not_found` without reading elements. Offsets are relative to the passed slice; a found index is an offset, not necessarily an index of a readable element. Search only reads shared slices, retains no borrow and never mutates its input. The reference implementation uses constant auxiliary storage and at most O(data.len × needle.len) byte comparisons; interpreter fuel still bounds execution.
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.