lib/memio+shlex+getopt: use os.alloc / os.free; drop @symbol bindings

Migrate the three modules that still carried private
@symbol("rt_alloc") / @symbol("rt_free") bindings onto the public
lib/os.alloc / lib/os.free surface that landed in 87c0883.

memio: 1 alloc (grow) + 2 free (grow's old-buffer drop, dynamicclose).
shlex: 1 alloc (dupstr) + 2 free (freepartial: element strs + slice
header). getopt: 1 alloc (tryparse) + 2 free (tryparse + finish).
ABI identity holds — same rt syms, same shapes, just routed through
the public surface.

rt_ensure stays inline in shlex + getopt; the slice-growth helper
isn't part of os and has no stdlib facade. Comments explain why.

Header rationale comments updated: dropped the now-stale
"lib/io ↔ lib/os C-symbol collision" framing on shlex's inlined
dupstr (that was a pre-#9 concern); reworded shlex's OOM trailer to
match lib/os.ww's documented contract (poisonous pointer, not nil,
fault on deref); fixed memio's dynamicfrom doc to reference
[[os.free]] instead of the retired rt_free name.

980_memio_run / 973_shlex_run / 982_getopt_run all green; bootstrap
byte-identical.
This commit is contained in:
2026-05-16 02:03:18 +09:00
parent 87c088359d
commit db2b05bbe5
3 changed files with 27 additions and 44 deletions

View File

@@ -91,18 +91,15 @@
// };
// defer getopt.finish(&cmd);
use os;
use strings;
// Direct rt_free / rt_ensure bindings rather than `use os;` — os
// exports read/write/close, which collide with io.read/write/close
// in callers that mix both (task #7). Same pattern as lib/memio.
//
// rt_ensure is the runtime slice-growth helper invoked by the
// `append(s, v)` builtin. We bind it directly because the builtin's
// expansion stores only 8 bytes of the new element (cgen emits a
// single MOVQ), losing the `value: str` half of an [[option]].
// [[appendoption]] grows manually and stores both fields via *option.
@symbol("rt_free") fn rtfree(p: *void, n: u64) void;
// No public stdlib facade exposes rt_ensure, hence the direct @symbol.
@symbol("rt_ensure") fn rtensure(s: *void, membsz: u64) void;
// helpkind — which slot of [[help]] is meaningful. Hare's getopt
@@ -296,7 +293,7 @@ export fn tryparse(out: *command, argv: []str, help: []help) (void | error) = {
match (look) {
case void => {
if (opts.cap > 0) {
rtfree(opts.ptr: *void,
os.free(opts.ptr: *void,
(opts.cap: u64) * 24u64);
};
let e: error;
@@ -318,7 +315,7 @@ export fn tryparse(out: *command, argv: []str, help: []help) (void | error) = {
} else {
if (i + 1 >= argv.len) {
if (opts.cap > 0) {
rtfree(opts.ptr: *void,
os.free(opts.ptr: *void,
(opts.cap: u64) * 24u64);
};
let e: error;
@@ -355,7 +352,7 @@ export fn tryparse(out: *command, argv: []str, help: []help) (void | error) = {
export fn finish(cmd: *command) void = {
if (cmd.optscap > 0) {
// option layout: rune (4) + pad (4) + str (16) = 24B.
rtfree(cmd.optsptr: *void, (cmd.optscap: u64) * 24u64);
os.free(cmd.optsptr: *void, (cmd.optscap: u64) * 24u64);
};
cmd.optsptr = nil: *option;
cmd.optslen = 0;