lib: collapse the manual rt_ensure append workarounds onto the fixed builtin (#34 follow-up)

shlex.appendstr, getopt.appendoption, bytes.appendslice and
strings.appendstr existed only because the append builtin stored the
first 8 bytes of the element; each carried its own @symbol("rt_ensure")
bind and a grow-then-store-through-*T body, with comments promising to
"collapse in one go when the append builtin is fixed". The previous
commit fixed the builtin; this removes all four helpers and their
rt_ensure binds and spells every call site as plain append().

Bonus correctness: getopt's appendoption passed a hardcoded membsz of
24, stale since the str 24B redesign made option {rune, str} 32B — the
manual growth under-allocated past 6 options while &opts.ptr[i] strode
32 (latent OOB). The builtin derives membsz from the type table
(probe: MOVQ $32, SI), closing that drift by construction.
This commit is contained in:
2026-06-04 02:22:25 +09:00
parent faade48513
commit 70fa9e2264
9 changed files with 66 additions and 354 deletions

View File

@@ -88,7 +88,7 @@ export fn dup(s: str) str = {
// so the only nomem propagation point is the initial slice alloc.
// With no inner failure path, the rollback is structurally a no-op
// and is omitted; it returns once dup graduates to `(str | nomem)`
// (#46). The pre-allocated slice has `cap == s.len`, so appendstr's
// (#46). The pre-allocated slice has `cap == s.len`, so append's
// rt_ensure call never reaches the grow branch.
//
// Empty input bypasses the alloc: rt_malloc(0) is an mmap of 0 bytes
@@ -107,7 +107,7 @@ export fn dupall(s: []str) ([]str | nomem) = {
let newsl: []str = alloc([], s.len)?;
let i: i32 = 0;
for (i < s.len) {
appendstr(&newsl, dup(s[i]));
append(newsl, dup(s[i]));
i += 1;
};
return newsl;
@@ -733,24 +733,6 @@ export fn rcut(in: str, delim: str) (str, str) = {
return (frombytes(a), frombytes(b));
};
// rt_ensure is the runtime slice-growth helper invoked by the
// `append(s, v)` builtin. Direct bind for the same reason as
// lib/shlex.shlex (appendstr, 16B): the builtin's expansion stores
// only 8B of the new element, losing the `.len` half of a `str`.
@symbol("rt_ensure") fn rtensure(s: *void, membsz: u64) void;
// appendstr — grow `*slice` by one and store `item` (16B). Mirror of
// lib/shlex.shlex appendstr. Collapses when the append builtin learns
// to store the full element width.
fn appendstr(slice: *[]str, item: str) void = {
let newlen: i32 = slice.len + 1;
slice.len = newlen;
rtensure(slice: *void, size(str): u64);
let dst: *str = &slice.ptr[newlen - 1];
dst.ptr = item.ptr;
dst.len = item.len;
};
// splitn — split `in` on any byte in `delim`, returning up to `n`
// tokens via forward iteration. The trailing slot (when more than
// `n - 1` tokens exist) holds the unconsumed remainder. Strings
@@ -773,7 +755,7 @@ export fn splitn(in: str, delim: str, n: i32) []str = {
let i: i32 = 0;
for (i < n - 1) {
match (next_token(&tok)) {
case let s: str => { appendstr(&toks, s); };
case let s: str => { append(toks, s); };
case bytes.done => { return toks; };
};
i += 1;
@@ -782,7 +764,7 @@ export fn splitn(in: str, delim: str, n: i32) []str = {
case bytes.done => void;
case let pk: str => {
let r: str = remaining_tokens(&tok);
appendstr(&toks, r);
append(toks, r);
};
};
return toks;
@@ -807,7 +789,7 @@ export fn rsplitn(in: str, delim: str, n: i32) []str = {
let i: i32 = 0;
for (i < n - 1) {
match (next_token(&tok)) {
case let s: str => { appendstr(&toks, s); };
case let s: str => { append(toks, s); };
case bytes.done => { return toks; };
};
i += 1;
@@ -816,7 +798,7 @@ export fn rsplitn(in: str, delim: str, n: i32) []str = {
case bytes.done => void;
case let pk: str => {
let r: str = remaining_tokens(&tok);
appendstr(&toks, r);
append(toks, r);
};
};