lib: complete the parallel vstream surface to Hare value-return shape (#94 fold-eFinal prep)

The #94 Option-C vstream surface was left incomplete and structurally
divergent from Hare: constructors heap-allocated and returned (X | nomem)
or used out-params instead of Hare's by-value stack ownership; memio lacked
reset/buffer/borrowedread; bufio's scanner was never ported to the vtable.
This is the additive half of the eFinal collapse — OLD surface stays fully
live; the destructive FLIP (delete OLD + drop _v + repoint) is the next
commit.

Reshape all constructors to VALUE-RETURN (field-by-field sret; the heap +
nomem was an unnecessary crutch — wide slice-bearing struct return-by-value
is byte-id-proven, cf 925_sret_struct_return_run). memio fixed/dynamic/
dynamicfrom, bufio init, log new now return the struct by value; the nomem
is gone with the alloc that forced it.

memio: unify the per-flavour ctx structs onto one `stream` (vt at offset 0);
collapse fixed_string + dynamic_string into a single string() over the common
header (bare-str return is the ratified rule-9 frombytes carve-out, cited at
the site per ref/hare/memio/stream.ha:81); port reset/buffer/borrowedread as
single fns over the header.

bufio: collapse the EXISTING scanner subset (newscannerbuf/scanbyte/scanbytes/
scanline/finish + setflush/flush/unread/isbuffered) onto the vtable, with src
now io.vstream so reads go through io.st_read. The Hare scanner functions ww
never implemented (scanrune/scanstring-arbitrary-delim/readtok/readline/
auto-grow newscanner) are out of scope and deferred to #217 — eFinal is a
collapse, not a feature expansion.

Keep the explicit (&fn): *io.T casts on vtable-slot stores (cgen-neutral;
avoids the #214 (X|void) over-acceptance surface; dropping the casts is a
deferred #206 payoff gated on #214).

Self-gate: 776 (memio) 18/18 and 778 (bufio) 27/27, every row carrying a
cs.s == ww.s byte-id check — bufio/fmt/log are not compiler-embedded, so
these rows are their only byte-id coverage. 779/781 stay STAGE_CS-only
pending #209. Regen w6c+wwdump combined.ww (io+memio are the embedded
modules).
This commit is contained in:
2026-05-29 21:14:39 +09:00
parent f9f83faca7
commit 10cb835f99
10 changed files with 1068 additions and 860 deletions

View File

@@ -1,13 +1,17 @@
/*
* 776_memio_vstream_run — project #94 fold-e2 sentinel. Pins the
* additive lib/memio/vstream.ww Option C parallel API: heap-alloc'd
* ctx structs (fixed_ctx / dynamic_ctx) with `vt: io.vtable` as the
* first field for the intrusive cast, three constructors
* (fixed_vstream / dynamic_vstream / dynamicfrom_vstream) that
* return `(io.vstream | nomem)`, and the matching read/write/close
* callbacks that recover the outer ctx via a vstream→*ctx pointer
* cast. Coexists with the pre-vtable memio.fixed/dynamic/dynamicfrom
* surface in memio.ww (fold-eFinal, task #50, retires the latter).
* 776_memio_vstream_run — project #94 fold-eFinal(PREP) sentinel. Pins
* the additive lib/memio/vstream.ww parallel API: a single unified
* `stream` struct with `vt: io.vtable` as the first field for the
* intrusive cast, three constructors (fixed_vstream / dynamic_vstream /
* dynamicfrom_vstream) that return the `stream` BY VALUE (sret —
* field-by-field build then `return r;`, ref/hare/memio/stream.ha:46,
* 58,64), the unified read_v / fixedwrite_v / dynamicwrite_v /
* dynamicclose_v callbacks that recover the outer stream via a
* vstream→*stream pointer cast, and the collapsed single accessors
* (string_v / buffer_v / reset_v / borrowedread_v) over the common
* header. Coexists with the pre-vtable memio.fixed/dynamic/dynamicfrom
* + `state` surface in memio.ww (fold-eFinal FLIP retires the latter
* and drops the `_vstream`/`_v` suffix wholesale).
*
* Each row imports memio + io, calls one or more constructors, drives
* the io.st_read/st_write/st_close dispatchers, and asserts an exit
@@ -19,9 +23,10 @@
* | over an 8-byte source. Asserts the
* | return is `size` and equal to 5, and
* | that out[0]/out[4] mirror the buffer.
* | Pins the full intrusive shape (alloc
* | fixed_ctx → return &c.vt; dispatcher
* | recovers via s: *fixed_ctx).
* | Pins the full intrusive shape (build
* | stream field-by-field → return r (sret);
* | caller takes &st.vt; dispatcher recovers
* | via s: *stream).
* dynamic_write_grow | dynamic_vstream + io.st_write of 3
* | bytes. Asserts the write reports 3
* | and that the dynamicgrow_v path
@@ -67,11 +72,10 @@
* fold-eFinal can graduate once #173 closes.
*
* - #195 (cgassign N_DOT TK_ASSIGN on aliased-ptr receiver):
* would bite if vstream.ww used chained `c.vt.reader = …`
* stores through the alloc'd ctx pointer. We sidestep via a
* local `let vt: io.vtable;` + field-assigns + struct-lit
* `alloc(fixed_ctx{vt=vt, …})`. Mirrors memio.ww:39's flat-
* slice-fields workaround. eFinal can collapse once #195 closes.
* not bitten — the value-return constructor builds the stream in
* a local `let r: stream;` and field-assigns each slot (including
* r.vt.reader) through a plain pointer-to-local, not an aliased-ptr
* receiver. The flat ptr/len/cap fields (memio.ww:39) stay flat.
*
* GATE POLARITY: must stay GREEN. A red here means vstream.ww
* regressed, the alloc-struct-? path miscompiled, the intrusive
@@ -114,12 +118,8 @@ static const struct row rows[] = {
" let buf: [8]u8;\n"
" buf[0] = 65u8; buf[1] = 66u8; buf[2] = 67u8; buf[3] = 68u8;\n"
" buf[4] = 69u8; buf[5] = 70u8; buf[6] = 71u8; buf[7] = 72u8;\n"
" let r = memio.fixed_vstream(buf[0:8]);\n"
" let s: io.vstream = nil: *io.vtable;\n"
" match (r) {\n"
" case let v: io.vstream => { s = v; };\n"
" case nomem => { return 50; };\n"
" };\n"
" let st: memio.stream = memio.fixed_vstream(buf[0:8]);\n"
" let s: io.vstream = &st.vt;\n"
" let out: [5]u8;\n"
" let n: size = 0;\n"
" let rd = io.st_read(s, out[0:5]);\n"
@@ -135,12 +135,8 @@ static const struct row rows[] = {
"import io;\n"
"import os;\n"
"export fn main() i32 = {\n"
" let r = memio.dynamic_vstream();\n"
" let s: io.vstream = nil: *io.vtable;\n"
" match (r) {\n"
" case let v: io.vstream => { s = v; };\n"
" case nomem => { return 50; };\n"
" };\n"
" let st: memio.stream = memio.dynamic_vstream();\n"
" let s: io.vstream = &st.vt;\n"
" let src: [3]u8;\n"
" src[0] = 88u8; src[1] = 89u8; src[2] = 90u8;\n"
" let wr = io.st_write(s, src[0:3]);\n"
@@ -160,12 +156,8 @@ static const struct row rows[] = {
"export fn main() i32 = {\n"
" let seed: [4]u8;\n"
" seed[0] = 1u8; seed[1] = 2u8; seed[2] = 3u8; seed[3] = 4u8;\n"
" let r = memio.dynamicfrom_vstream(seed[0:4]);\n"
" let s: io.vstream = nil: *io.vtable;\n"
" match (r) {\n"
" case let v: io.vstream => { s = v; };\n"
" case nomem => { return 50; };\n"
" };\n"
" let st: memio.stream = memio.dynamicfrom_vstream(seed[0:4]);\n"
" let s: io.vstream = &st.vt;\n"
" let out: [4]u8;\n"
" let rd = io.st_read(s, out[0:4]);\n"
" let rn: size = 0;\n"
@@ -190,18 +182,10 @@ static const struct row rows[] = {
" a[0] = 10u8; a[1] = 11u8; a[2] = 12u8;\n"
" let b: [5]u8;\n"
" b[0] = 20u8; b[1] = 21u8; b[2] = 22u8; b[3] = 23u8; b[4] = 24u8;\n"
" let ra = memio.fixed_vstream(a[0:3]);\n"
" let rb = memio.fixed_vstream(b[0:5]);\n"
" let sa: io.vstream = nil: *io.vtable;\n"
" let sb: io.vstream = nil: *io.vtable;\n"
" match (ra) {\n"
" case let v: io.vstream => { sa = v; };\n"
" case nomem => { return 50; };\n"
" };\n"
" match (rb) {\n"
" case let v: io.vstream => { sb = v; };\n"
" case nomem => { return 51; };\n"
" };\n"
" let sta: memio.stream = memio.fixed_vstream(a[0:3]);\n"
" let stb: memio.stream = memio.fixed_vstream(b[0:5]);\n"
" let sa: io.vstream = &sta.vt;\n"
" let sb: io.vstream = &stb.vt;\n"
" let outa: [3]u8;\n"
" let outb: [5]u8;\n"
" let rda = io.st_read(sa, outa[0:3]);\n"
@@ -215,6 +199,65 @@ static const struct row rows[] = {
"};\n",
45,
STAGE_CS | STAGE_WW, 1 },
/* accessors_string_buffer_reset — fold-eFinal PREP: the collapsed
* single accessors over the common `stream` header (string_v /
* buffer_v / reset_v, ref/hare/memio/stream.ha:81,74,87). Writes 3
* bytes through the value-returned fixed stream, then asserts
* string_v (str view), buffer_v ([]u8 view), and reset_v (rewind to
* 0) all read the flat header correctly. Pins the value-return
* field integrity (st.vt + st.ptr/len/cap/pos survive sret) AND the
* accessor collapse. cs.s == ww.s byte-id. */
{ "accessors_string_buffer_reset",
"package main;\n"
"import memio;\n"
"import io;\n"
"import os;\n"
"export fn main() i32 = {\n"
" let buf: [16]u8;\n"
" let st: memio.stream = memio.fixed_vstream(buf[0:16]);\n"
" let s: io.vstream = &st.vt;\n"
" let payload: [3]u8;\n"
" payload[0] = 65u8; payload[1] = 66u8; payload[2] = 67u8;\n"
" let wr = io.st_write(s, payload[0:3]);\n"
" if (!(wr is size)) { return 90; };\n"
" let v: str = memio.string_v(&st);\n"
" if (v.len != 3 || v[0] != 65u8 || v[2] != 67u8) { return 91; };\n"
" let bv: []u8 = memio.buffer_v(&st);\n"
" if (bv.len != 3 || bv[1] != 66u8) { return 92; };\n"
" memio.reset_v(&st);\n"
" if (memio.string_v(&st).len != 0) { return 93; };\n"
" return 46;\n"
"};\n",
46,
STAGE_CS | STAGE_WW, 1 },
/* borrowedread_view_eof — the collapsed borrowedread_v
* (ref/hare/memio/stream.ha:94): returns an amt-byte borrowed view
* advancing the cursor, eof when fewer remain. Reads 3 of 4 seeded
* bytes (view len 3), then a 2-byte borrowedread over the remaining
* 1 byte returns io.eof. cs.s == ww.s byte-id. */
{ "borrowedread_view_eof",
"package main;\n"
"import memio;\n"
"import io;\n"
"import os;\n"
"export fn main() i32 = {\n"
" let seed: [4]u8;\n"
" seed[0] = 1u8; seed[1] = 2u8; seed[2] = 3u8; seed[3] = 4u8;\n"
" let st: memio.stream = memio.dynamicfrom_vstream(seed[0:4]);\n"
" let br = memio.borrowedread_v(&st, 3);\n"
" match (br) {\n"
" case let b: []u8 => { if (b.len != 3 || b[0] != 1u8 || b[2] != 3u8) { return 91; }; };\n"
" case io.eof => { return 92; };\n"
" };\n"
" let br2 = memio.borrowedread_v(&st, 2);\n"
" match (br2) {\n"
" case let b: []u8 => { return 93; };\n"
" case io.eof => { return 47; };\n"
" };\n"
" return 99;\n"
"};\n",
47,
STAGE_CS | STAGE_WW, 1 },
};
static int