rt: move slice append helpers from lib/slices/ into libwwrt.a

This commit is contained in:
2026-05-11 16:56:46 +09:00
parent 8ffe6dbee6
commit 5408160d49
5 changed files with 23 additions and 34 deletions

49
rt/append.ww Normal file
View File

@@ -0,0 +1,49 @@
// rt/append.ww — slice runtime helpers, archived into libwwrt.a.
//
// The compiler lowers `append(s, v)` to a CALL to one of these by
// element size (1 byte → appendu8, else → appendi64). User code
// never `use`s this — the symbol comes in via the runtime archive,
// like rt_alloc and rt_streq.
@symbol("rt_alloc") fn alloc(n: u64) *void;
@symbol("rt_free") fn free(p: *void, n: u64) void;
export fn appendu8(s: *[]u8, v: u8) void = {
if (s.len >= s.cap) {
let nc: i32 = s.cap * 2;
if (nc < 8) { nc = 8; };
let np: *u8 = alloc(nc: u64): *u8;
let i: i32 = 0;
for (i < s.len) {
np[i] = s.ptr[i];
i += 1;
};
if (s.cap > 0) {
free(s.ptr: *void, s.cap: u64);
};
s.ptr = np;
s.cap = nc;
};
s.ptr[s.len] = v;
s.len += 1;
};
export fn appendi64(s: *[]i64, v: i64) void = {
if (s.len >= s.cap) {
let nc: i32 = s.cap * 2;
if (nc < 8) { nc = 8; };
let np: *i64 = alloc((nc * 8): u64): *i64;
let i: i32 = 0;
for (i < s.len) {
np[i] = s.ptr[i];
i += 1;
};
if (s.cap > 0) {
free(s.ptr: *void, (s.cap * 8): u64);
};
s.ptr = np;
s.cap = nc;
};
s.ptr[s.len] = v;
s.len += 1;
};