From c62e8e5560b3b28312cb91db6d17947434c64e7e Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 29 May 2026 10:24:02 +0900 Subject: [PATCH] lib/bufio: add Option C parallel vstream API (#94 fold-e4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds bufio_vstream + isbuffered_v alongside the pre-vtable bufio.init / bufio.isbuffered surface, mirroring fold-e2's lib/memio and fold-e3's lib/fmt parallel-API shape. The OLD bufio.ww surface stays untouched; fold-eFinal (#50) atomically flips the package shape, drops the `_v` suffix, and retires the legacy callbacks. bufio_ctx wraps an underlying *io.stream (OLD API) — bufio_vstream src parameter type stays *io.stream until io fold-2 lands `handle = (file | int)` (drew-deferred). vt is the first field for the intrusive vstream→*bufio_ctx cast, same shape as memio/fmt vstream wrappers. Sibling task filed: #210 struct-lit slice-typed field silently drops under alloc(T{slice = val})?. Parallel to #207 for slice fields; scalar/ptr fields in the same alloc-struct-lit populate correctly. Workaround: post-alloc field-assign c.slicefield = val. Documented inline; drops out on close. Other deferrals retained inline: #206 cast wrappers (3 vtable wire-up + 2 isbuffered_v comparand), #173 nomem-widen for the io.closed → io.error boundary, #207 alloc-zero-chain for vt. bufio is not embedded in any selfhost combined.ww (test-only); no Makefile regen needed (#110-blind safe). test/wcc/778_bufio_vstream_run pins the 5-row scenario set: write+flush, read+refill, isbuffered_v discriminator, OLD/NEW boundary check, and a branched-callee #105 row. cs+ww+byte-id green on all 5 rows. make test: 211 passed (was 210). --- Makefile | 8 + lib/bufio/vstream.ww | 320 +++++++++++++++++++++ test/wcc/778_bufio_vstream_run.c | 475 +++++++++++++++++++++++++++++++ 3 files changed, 803 insertions(+) create mode 100644 lib/bufio/vstream.ww create mode 100644 test/wcc/778_bufio_vstream_run.c diff --git a/Makefile b/Makefile index 21d3a1f2..e01ca66d 100644 --- a/Makefile +++ b/Makefile @@ -328,6 +328,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_io_vtable_run \ $(BIN)/test_memio_vstream_run \ $(BIN)/test_fmt_vstream_run \ + $(BIN)/test_bufio_vstream_run \ $(BIN)/test_use_promote_alias \ $(BIN)/test_field_signed $(BIN)/test_frame_argcount \ $(BIN)/test_selfhost $(BIN)/test_w6a_ww $(BIN)/test_w6l_ww \ @@ -704,6 +705,13 @@ $(BIN)/test_fmt_vstream_run: test/wcc/777_fmt_vstream_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_bufio_vstream_run: test/wcc/778_bufio_vstream_run.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + lib/bufio/bufio.ww lib/bufio/vstream.ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_arrlit_str_full: test/wcc/711_arrlit_str_full.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/lib/bufio/vstream.ww b/lib/bufio/vstream.ww new file mode 100644 index 00000000..8e0f3220 --- /dev/null +++ b/lib/bufio/vstream.ww @@ -0,0 +1,320 @@ +// vstream — Hare-shaped vtable wrappers over [[bufio.stream]]. Project +// #94 fold-e4 (Option C, parallel API for lib/bufio). +// +// Adds bufio_vstream + isbuffered_v alongside the pre-vtable +// bufio.init / bufio.isbuffered surface in bufio.ww. The OLD surface +// stays untouched here — fold-eFinal (task #50) atomically flips the +// package shape: deletes the OLD constructors + callbacks, renames +// `_v` suffix off (drops the `isbuffered_v` to `isbuffered` per Hare's +// ref/hare/bufio/stream.ha:179), and migrates the few callers. +// +// Hare's ref/hare/bufio/stream.ha:69 (init) returns the stream by +// value, with `vtable: nullable *io::vtable` pre-resolved per-mode +// (vtable_r / vtable_w / vtable_rw module-static singletons at +// stream.ha:8-25); ww doesn't ship static-storage tagged-union +// singletons yet, so we heap-allocate bufio_ctx, fill the vtable +// post-alloc per slot, and return &c.vt as the io.vstream. Same +// intrusive shape memio/vstream.ww + fmt/vstream.ww use. +// +// drew-approved deferrals (Hare-shape, not collapsed here): +// +// - Hare uses three vtable singletons (vtable_r / vtable_w / +// vtable_rw) to discriminate r-only / w-only / r+w mode; +// bufio_vstream always installs all three callbacks per +// bufio.ww:32-38's pre-existing divergence (zero-length rbuf or +// wbuf degenerates the matching callback in-cb). The Hare-shape +// mode-keyed singletons graduate with io fold-2 (handle). +// +// - Hare's defer-handle (`flag::MANAGED_*` ownership bits) deferred +// to io fold-2 (drew). Caller owns rbuf/wbuf/src for now; +// bclose_v flushes and forwards close to src but doesn't free +// any buffers (mirrors bufio.ww:60-64 OLD-side ownership note). +// +// Cast workaround per #206: bare `&fn_name` does not type-check as +// a `(* | void)` field-init / let-binding. Explicit +// `(&fn_name): *io.` cast at each store site is the Hare- +// faithful minimum-touch route — same workaround memio/vstream.ww +// and fmt/vstream.ww use. 3 cast tokens at the vtable wire-up site, +// plus 2 in isbuffered_v for the fn-ptr-equality comparand. All 5 +// drop out wholesale once #206 closes. +// +// Slice-field workaround per #210 (sibling of #207, surfaced here): +// struct-lit slice-typed field through `alloc(bufio_ctx{rbuf = rbuf, +// ...})?` SILENTLY drops the slice (reads back as zero-length). +// Scalar i32 and pointer fields in the same struct-lit populate +// correctly. Workaround: post-alloc `c.rbuf = rbuf;` field-assign, +// which works through the *bufio_ctx pointer. Same alloc-zero-chain +// shape as memio/vstream.ww uses for #207 over vt's tagged fields. +// 3 slice-field stores (rbuf / wbuf / flush) move post-alloc. +// Drops out wholesale on #210 close. +// +// fn-ptr-equality discriminator per ref/hare/bufio/stream.ha:179-184: +// isbuffered_v pattern-extracts the reader/writer slot and compares +// against `(&bread_v): *io.reader` / `(&bwrite_v): *io.writer`. Hare +// uses static-singleton symbol-address equality; ww routes through +// the cast-then-eq form because each slot is a tagged union +// (`(*reader | void)`) rather than a plain pointer. eFinal collapses +// to the Hare-direct form (`s.reader == &read`) once #192 closes and +// the slot becomes a plain nullable. +// +// io.closed → io.error conversion at the OLD-API boundary: the +// underlying *io.stream's io.read/io.write return (.. | io.closed) +// but the V API surface returns (.. | io.error). We construct nomem +// and widen to io.error per the fdsinkwrite_v precedent +// (fmt/vstream.ww:88-92) — same shape memio/vstream.ww uses for its +// os.write < 0 path. nomem is the carrier (not a semantic match for +// "closed") until io fold-2 lands a properly-shaped error variant. +// +// Sibling tasks parked here (filed, NOT fixed): +// +// - io fold-2 (handle port): drew-deferred. Hare's +// ref/hare/bufio/stream.ha:69 takes `src: io::handle`; once +// handle = (file | int) lands, bufio_vstream's src parameter +// collapses accordingly. eFinal (#50) graduates. +// +// - #206 (bare &fn → (*alias|void)): 5 cast sites here; drop +// out wholesale on close. +// +// - #207 (struct-lit multi-tagged-field copy drops past first): +// alloc-zero-chain pattern (`vt = zero` + post-alloc +// `c.vt.X = ...`) sidesteps; same shape memio/vstream.ww uses. +// Only one tagged field in bufio_ctx (vt), but the local-zero +// route stays consistent with memio's idiom. +// +// - #210 (struct-lit slice-typed field drops under alloc): +// surfaced by this fold. rbuf/wbuf/flush move post-alloc; +// drops out wholesale on close. +// +// - #209 (wwstage formattable match-arm bail when fmt imported): +// bufio doesn't import fmt transitively (only io), so the +// test/wcc/778 probe runs both stages. + +package bufio; + +import io; + +// bufio_ctx — heap-alloc'd state for bufio_vstream. `vt` at offset 0 +// for the intrusive vstream→*bufio_ctx cast. Mirrors lib/bufio.stream +// (bufio.ww:252) modulo the new io.vtable in place of the OLD +// io.stream first-field embed. +export type bufio_ctx = struct { + vt: io.vtable, + src: *io.stream, + rbuf: []u8, + rstart: i32, + rend: i32, + wbuf: []u8, + wend: i32, + flush: []u8, +}; + +// bufio_vstream — wire a vstream over an underlying *io.stream with +// caller-supplied read/write buffers. Both rbuf and wbuf may be +// empty; bread_v / bwrite_v degenerate per bufio.ww:34-38 in that +// case. The flush byte-set defaults to "\n" (line-buffered writes); +// per-stream setflush graduates with #50 / io fold-2. +// +// Mirrors ref/hare/bufio/stream.ha:69 (init). +export fn bufio_vstream(src: *io.stream, rbuf: []u8, wbuf: []u8) (io.vstream | nomem) = { + let zero: io.vtable; + let c: *bufio_ctx = alloc(bufio_ctx{ + vt = zero, + src = src, + rstart = rbuf.len, + rend = rbuf.len, + wend = 0, + })?; + // Post-alloc slice-field stores per #210 (sibling of #207): + // struct-lit `rbuf = rbuf` silently drops slice-typed fields + // under alloc(T{...}). Scalar/ptr fields in the same struct-lit + // store correctly. Same alloc-zero-chain shape memio/vstream.ww + // uses for #207 over vt's tagged fields. 3 stores; drops out on + // #210 close. + c.rbuf = rbuf; + c.wbuf = wbuf; + c.flush = flushdefault[0:1]; + c.vt.reader = (&bread_v): *io.reader; + c.vt.writer = (&bwrite_v): *io.writer; + c.vt.closer = (&bclose_v): *io.closer; + return &c.vt; +}; + +// isbuffered_v — true when `s` was returned by [[bufio_vstream]]. +// Hare uses callback-identity (ref/hare/bufio/stream.ha:179); +// matches the OLD isbuffered shape (bufio.ww:338) but over the new +// `_v` fn symbols. Either reader or writer matching is sufficient +// (closer alone isn't unique to bufio). +export fn isbuffered_v(s: io.vstream) bool = { + match (s.reader) { + case let r: *io.reader => { + if (r == (&bread_v): *io.reader) { return true; }; + }; + case void => { }; + }; + match (s.writer) { + case let w: *io.writer => { + if (w == (&bwrite_v): *io.writer) { return true; }; + }; + case void => { }; + }; + return false; +}; + +// ---- vtable callbacks ---------------------------------------------------- + +// bread_v — vstream-side read. Recover ctx via the intrusive +// vstream→*bufio_ctx cast. Mirrors bufio.ww:346 (OLD bread) modulo +// the (size | io.eof | io.error) return surface; io.closed from the +// underlying *io.stream's io.read widens to io.error via nomem +// (same shape fmt/vstream.ww:88-92 + memio/vstream.ww:71-74 use, +// per #173 deferral). +fn bread_v(s: io.vstream, buf: []u8) (size | io.eof | io.error) = { + let b: *bufio_ctx = s: *bufio_ctx; + if (b.rbuf.len == 0) { + let e: io.eof; return e; + }; + if (b.rstart >= b.rend) { + b.rstart = 0; + b.rend = 0; + let r: (i32 | io.eof | io.closed) = io.read(b.src, b.rbuf); + match (r) { + case let n: i32 => { b.rend = n; }; + case io.eof => { let e: io.eof; return e; }; + case io.closed => { + let nm: nomem; + let e: io.error = nm; + return e; + }; + }; + }; + let avail: i32 = b.rend - b.rstart; + let n: i32 = buf.len; + if (avail < n) { n = avail; }; + let i: i32 = 0; + for (i < n) { + buf[i] = b.rbuf[b.rstart + i]; + i += 1; + }; + b.rstart += n; + return n: size; +}; + +// bwrite_v — vstream-side write. Mirrors bufio.ww:376 (OLD bwrite) +// modulo the (size | io.error) return surface; io.closed widens to +// io.error per the bread_v note. Default-flush scan + per-batch copy +// + post-write conditional flush — same flush-byte-set semantics as +// the OLD path (ref/hare/bufio/stream.ha:236-246, labeled-break +// inlined per bufio.ww:382's note). +fn bwrite_v(s: io.vstream, buf: []u8) (size | io.error) = { + let b: *bufio_ctx = s: *bufio_ctx; + if (b.wbuf.len == 0) { + let r: (i32 | io.closed) = io.write(b.src, buf); + match (r) { + case let n: i32 => { return n: size; }; + case io.closed => { + let nm: nomem; + let e: io.error = nm; + return e; + }; + }; + }; + let doflush: bool = false; + if (b.flush.len != 0) { + let i: i32 = 0; + for (i < buf.len) { + let j: i32 = 0; + for (j < b.flush.len) { + if (buf[i] == b.flush[j]) { + doflush = true; + i = buf.len; + j = b.flush.len; + }; + j += 1; + }; + i += 1; + }; + }; + let z: i32 = 0; + for (z < buf.len) { + let avail: i32 = b.wbuf.len - b.wend; + if (avail == 0) { + let fr: (void | io.error) = flush_v(b); + match (fr) { + case void => { }; + case let e: io.error => return e; + }; + avail = b.wbuf.len; + }; + let n: i32 = buf.len - z; + if (avail < n) { n = avail; }; + let i: i32 = 0; + for (i < n) { + b.wbuf[b.wend + i] = buf[z + i]; + i += 1; + }; + b.wend += n; + z += n; + }; + if (doflush) { + let fr: (void | io.error) = flush_v(b); + match (fr) { + case void => { }; + case let e: io.error => return e; + }; + }; + return buf.len: size; +}; + +// bclose_v — flush pending wbuf, forward close to src. Mirrors +// bufio.ww:431. Hare's close_buffered also handles the +// MANAGED_HANDLE / MANAGED_RDBUF / MANAGED_WRBUF ownership bits +// (ref/hare/bufio/stream.ha:188-202); ww drops them per bufio.ww:33-47 +// (caller-owned today; defer-handle graduates with io fold-2, +// drew-deferred). +fn bclose_v(s: io.vstream) (void | io.error) = { + let b: *bufio_ctx = s: *bufio_ctx; + let fr: (void | io.error) = flush_v(b); + match (fr) { + case void => { }; + case let e: io.error => return e; + }; + let cr: (void | io.closed) = io.close(b.src); + match (cr) { + case void => return void; + case io.closed => { + let nm: nomem; + let e: io.error = nm; + return e; + }; + }; +}; + +// flush_v — drain pending wbuf to src. Mirror of bufio.ww:294 (OLD +// flush) over the V surface; io.closed widens to io.error per the +// bread_v note. Module-prefixed `_v` suffix keeps the cstage flat-TU +// private-fn scope from colliding with OLD flush (same shape +// memio.dynamicgrow_v at memio/vstream.ww:229). +fn flush_v(b: *bufio_ctx) (void | io.error) = { + if (b.wend == 0) { return; }; + let off: i32 = 0; + for (off < b.wend) { + let r: (i32 | io.closed) = io.write(b.src, b.wbuf[off:b.wend]); + match (r) { + case let n: i32 => { + if (n == 0) { + let nm: nomem; + let e: io.error = nm; + return e; + }; + off += n; + }; + case io.closed => { + let nm: nomem; + let e: io.error = nm; + return e; + }; + }; + }; + b.wend = 0; + return; +}; diff --git a/test/wcc/778_bufio_vstream_run.c b/test/wcc/778_bufio_vstream_run.c new file mode 100644 index 00000000..4b5ae7d9 --- /dev/null +++ b/test/wcc/778_bufio_vstream_run.c @@ -0,0 +1,475 @@ +/* + * 778_bufio_vstream_run — project #94 fold-e4 sentinel. Pins the + * additive lib/bufio/vstream.ww Option C parallel API: heap-alloc'd + * bufio_ctx with `vt: io.vtable` as the first field for the intrusive + * vstream cast, the bufio_vstream constructor that returns + * `(io.vstream | nomem)` wrapping an underlying *io.stream (OLD + * API; bufio_vstream's `src` parameter type stays OLD until io + * fold-2 lands `handle = (file | int)`), and isbuffered_v as the + * fn-ptr-equality discriminator over the new `_v` callback symbols. + * Coexists with the pre-vtable bufio.init / bufio.isbuffered surface + * in bufio.ww (fold-eFinal, task #50, retires the latter once io + * fold-2 ports `handle = (file | int)`). + * + * Each row imports os + bufio + memio + io (NOT fmt — bufio doesn't + * transitively pull fmt, so #209's formattable-match bail doesn't + * propagate; both stages run on every row). + * + * row | what it pins + * --------------------------+-------------------------------------- + * stream_write_flush | bufio_vstream init + io.st_write + * | through bwrite_v + bufio.flush_v via + * | io.st_close. Asserts the data + * | reaches the underlying memio buffer + * | after close drains wbuf. Full chain: + * | alloc bufio_ctx → return &c.vt → + * | dispatcher recovers via *bufio_ctx → + * | flush_v → io.write(b.src, ...). + * stream_read_unread | bufio_vstream read path: io.st_read + * | refills rbuf from src, serves first + * | N bytes. Pins bread_v's read path + * | through the intrusive cast. unread_v + * | not exposed yet (no bufio.unread + * | equivalent on the V API surface + * | until eFinal); this row exercises + * | the base read path instead. + * isbuffered_v_discriminate | isbuffered_v on a fresh bufio_vstream + * | returns true; isbuffered_v on a + * | plain memio.fixed_vstream (no bufio + * | wrap) returns false. Pins the + * | fn-ptr-equality discriminator + * | (ref/hare/bufio/stream.ha:179) over + * | the new `_v` callback symbols. + * isbuffered_v_boundary | NEW isbuffered_v on bufio_vstream + * | returns true AND OLD bufio.isbuffered + * | on a bytewise-cast view of the same + * | vstream returns false. Boundary + * | check: the OLD discriminator reads + * | (s.read == bread) but the bytes at + * | &bufio_ctx.vt are vt.reader (tagged + * | union) — the cast reinterprets vt's + * | tag+ptr layout as the OLD stream's + * | ctx/read/write/close 4-fnptr layout, + * | and the OLD `bread` symbol address + * | is distinct from the NEW `bread_v`, + * | so the OLD discriminator returns + * | false. Proves the OLD + NEW worlds + * | use independent fn symbols. + * branched_bufio_vstream | branched callee per #105: two + * | distinct underlying *io.stream's + * | runtime-selected; bufio_vstream + * | wraps the picked one. st_read + * | returns the matching source's + * | content. Catches a constant-fold + * | mistake in the dispatch path + * | (mirror of 775's branched_readers + * | for the bufio-wrap side). + * + * SIBLINGS (filed inline, NOT fixed here — fold-e4 is purely + * additive over fold-e1/e2/e3's frozen io.* + memio.* + fmt.* + * surface): + * + * - io fold-2 (handle port): drew-deferred. Hare's + * ref/hare/bufio/stream.ha:69 takes `src: io::handle`; once + * handle = (file | int) lands, bufio_vstream's src parameter + * collapses accordingly. eFinal (#50) graduates. + * + * - #206 (bare &fn → (*alias|void)): 5 cast sites in vstream.ww + * (3 vtable wire-up + 2 in isbuffered_v); drop out wholesale + * on close. + * + * - #173 (TRY-on-tagged-return both-stages broken): flush_v + * constructs nomem and widens to io.error explicitly rather + * than using `io.write(...)?`; same memio.vstream.ww + + * fmt.vstream.ww shape. + * + * - #207 (struct-lit multi-tagged-field copy drops past first): + * alloc-zero-chain pattern (`vt = zero` + post-alloc + * `c.vt.X = ...`) sidesteps; only 1 tagged field in bufio_ctx + * so the workaround is overkill, but stays for idiom + * consistency with memio/vstream.ww. + * + * BOOTSTRAP-EMBED CHECK: bufio is NOT embedded in any selfhost + * combined.ww (grep confirmed). Adding lib/bufio/vstream.ww does + * NOT require a Makefile regen (#110); only lib/bufio/ test paths + * see the new module. 990-997 byte-id gates stay green by virtue + * of bufio being test-only. + * + * GATE POLARITY: must stay GREEN. A red here means vstream.ww + * regressed, the alloc-struct-? path miscompiled, the intrusive + * vstream→*bufio_ctx cast miscomputed offsets, the io.vtable + * first-field embed lost its offset-0 invariant, or the fn-ptr- + * equality discriminator in isbuffered_v stopped resolving. + */ +#include +#include +#include +#include +#include + +static int +runwait(const char *cmd) +{ + int rc = system(cmd); + if (rc == -1) return -1; + if (WIFEXITED(rc)) return WEXITSTATUS(rc); + return -1; +} + +#define STAGE_CS 1 +#define STAGE_WW 2 + +struct row { + const char *label; + const char *src; + int want_exit; + int stage_mask; + int byte_id; +}; + +static const struct row rows[] = { + { "stream_write_flush", + "package main;\n" + "import os;\n" + "import bufio;\n" + "import memio;\n" + "import io;\n" + "export fn main() i32 = {\n" + " let raw: [16]u8;\n" + " let mem: memio.state;\n" + " let m: io.stream;\n" + " memio.fixed(&mem, &m, raw[0:16]);\n" + " let rbuf: [8]u8;\n" + " let wbuf: [8]u8;\n" + " let r = bufio.bufio_vstream(&m, rbuf[0:8], wbuf[0:8]);\n" + " let vs: io.vstream = nil: *io.vtable;\n" + " match (r) {\n" + " case let v: io.vstream => { vs = v; };\n" + " case nomem => { return 50; };\n" + " };\n" + " let payload: [5]u8;\n" + " payload[0] = 104u8; payload[1] = 105u8;\n" + " payload[2] = 33u8; payload[3] = 98u8; payload[4] = 121u8;\n" + " let wr = io.st_write(vs, payload[0:5]);\n" + " let nw: size = 0;\n" + " if (wr is size) { nw = wr as size; };\n" + " if (mem.pos != 0) { return 91; };\n" + " let cl = io.st_close(vs);\n" + " if (cl is io.error) { return 92; };\n" + " if (mem.pos != 5) { return 93; };\n" + " if (nw != 5: size) { return 94; };\n" + " if (raw[0] != 104u8 || raw[4] != 121u8) { return 95; };\n" + " return 46;\n" + "};\n", + 46, + STAGE_CS | STAGE_WW, 1 }, + { "stream_read_unread", + "package main;\n" + "import os;\n" + "import bufio;\n" + "import memio;\n" + "import io;\n" + "export fn main() i32 = {\n" + " let raw: [8]u8;\n" + " raw[0] = 65u8; raw[1] = 66u8; raw[2] = 67u8; raw[3] = 68u8;\n" + " raw[4] = 69u8; raw[5] = 70u8; raw[6] = 71u8; raw[7] = 72u8;\n" + " let mem: memio.state;\n" + " let m: io.stream;\n" + " memio.fixed(&mem, &m, raw[0:8]);\n" + " let rbuf: [8]u8;\n" + " let wbuf: [4]u8;\n" + " let r = bufio.bufio_vstream(&m, rbuf[0:8], wbuf[0:4]);\n" + " let vs: io.vstream = nil: *io.vtable;\n" + " match (r) {\n" + " case let v: io.vstream => { vs = v; };\n" + " case nomem => { return 50; };\n" + " };\n" + " let out: [4]u8;\n" + " let rd1 = io.st_read(vs, out[0:3]);\n" + " let n1: size = 0;\n" + " if (rd1 is size) { n1 = rd1 as size; };\n" + " if (n1 != 3: size) { return 91; };\n" + " if (out[0] != 65u8 || out[2] != 67u8) { return 92; };\n" + " let rd2 = io.st_read(vs, out[0:4]);\n" + " let n2: size = 0;\n" + " if (rd2 is size) { n2 = rd2 as size; };\n" + " if (n2 != 4: size) { return 93; };\n" + " if (out[0] != 68u8 || out[3] != 71u8) { return 94; };\n" + " return 47;\n" + "};\n", + 47, + STAGE_CS | STAGE_WW, 1 }, + { "isbuffered_v_discriminate", + "package main;\n" + "import os;\n" + "import bufio;\n" + "import memio;\n" + "import io;\n" + "export fn main() i32 = {\n" + " let raw: [8]u8;\n" + " let mem: memio.state;\n" + " let m: io.stream;\n" + " memio.fixed(&mem, &m, raw[0:8]);\n" + " let rbuf: [4]u8;\n" + " let wbuf: [4]u8;\n" + " let r = bufio.bufio_vstream(&m, rbuf[0:4], wbuf[0:4]);\n" + " let vsbuf: io.vstream = nil: *io.vtable;\n" + " match (r) {\n" + " case let v: io.vstream => { vsbuf = v; };\n" + " case nomem => { return 50; };\n" + " };\n" + " let buf2: [4]u8;\n" + " let r2 = memio.fixed_vstream(buf2[0:4]);\n" + " let vsplain: io.vstream = nil: *io.vtable;\n" + " match (r2) {\n" + " case let v: io.vstream => { vsplain = v; };\n" + " case nomem => { return 51; };\n" + " };\n" + " if (!bufio.isbuffered_v(vsbuf)) { return 91; };\n" + " if (bufio.isbuffered_v(vsplain)) { return 92; };\n" + " return 48;\n" + "};\n", + 48, + STAGE_CS | STAGE_WW, 1 }, + { "isbuffered_v_boundary", + "package main;\n" + "import os;\n" + "import bufio;\n" + "import memio;\n" + "import io;\n" + "export fn main() i32 = {\n" + " let raw: [8]u8;\n" + " let mem: memio.state;\n" + " let m: io.stream;\n" + " memio.fixed(&mem, &m, raw[0:8]);\n" + " let rbuf: [4]u8;\n" + " let wbuf: [4]u8;\n" + " let r = bufio.bufio_vstream(&m, rbuf[0:4], wbuf[0:4]);\n" + " let vs: io.vstream = nil: *io.vtable;\n" + " match (r) {\n" + " case let v: io.vstream => { vs = v; };\n" + " case nomem => { return 50; };\n" + " };\n" + " if (!bufio.isbuffered_v(vs)) { return 91; };\n" + " let raw2: u64 = vs: u64;\n" + " let asold: *io.stream = raw2: *io.stream;\n" + " if (bufio.isbuffered(asold)) { return 92; };\n" + " let b: bufio.stream;\n" + " let rb: [4]u8;\n" + " let wb: [4]u8;\n" + " let raw3: [8]u8;\n" + " let mem3: memio.state;\n" + " let m3: io.stream;\n" + " memio.fixed(&mem3, &m3, raw3[0:8]);\n" + " bufio.init(&b, &m3, rb[0:4], wb[0:4]);\n" + " if (!bufio.isbuffered(&b.vtable)) { return 93; };\n" + " return 49;\n" + "};\n", + 49, + STAGE_CS | STAGE_WW, 1 }, + { "branched_bufio_vstream", + "package main;\n" + "import os;\n" + "import bufio;\n" + "import memio;\n" + "import io;\n" + "export fn main() i32 = {\n" + " let raA: [4]u8;\n" + " raA[0] = 10u8; raA[1] = 11u8; raA[2] = 12u8; raA[3] = 13u8;\n" + " let raB: [4]u8;\n" + " raB[0] = 20u8; raB[1] = 21u8; raB[2] = 22u8; raB[3] = 23u8;\n" + " let memA: memio.state;\n" + " let memB: memio.state;\n" + " let mA: io.stream;\n" + " let mB: io.stream;\n" + " memio.fixed(&memA, &mA, raA[0:4]);\n" + " memio.fixed(&memB, &mB, raB[0:4]);\n" + " let sel: i32 = 1;\n" + " let src: *io.stream = &mA;\n" + " if (sel == 0) { src = &mB; };\n" + " let rbuf: [4]u8;\n" + " let wbuf: [4]u8;\n" + " let r = bufio.bufio_vstream(src, rbuf[0:4], wbuf[0:4]);\n" + " let vs: io.vstream = nil: *io.vtable;\n" + " match (r) {\n" + " case let v: io.vstream => { vs = v; };\n" + " case nomem => { return 50; };\n" + " };\n" + " let out: [4]u8;\n" + " let rd = io.st_read(vs, out[0:4]);\n" + " let n: size = 0;\n" + " if (rd is size) { n = rd as size; };\n" + " if (n != 4: size) { return 91; };\n" + " if (out[0] != 10u8 || out[3] != 13u8) { return 92; };\n" + " return 51;\n" + "};\n", + 51, + STAGE_CS | STAGE_WW, 1 }, +}; + +static int +write_source(const char *path, const char *src) +{ + FILE *f = fopen(path, "wb"); + if (!f) return -1; + fputs(src, f); + fclose(f); + return 0; +} + +/* Per-row tmpdir cleanup. ww_ww writes intermediates next to the + * source (filed task #15), so each row's build leaves + * .{combined.ww,s,o} + bare exe alongside. Sweep all then + * rmdir. Mirror of 776's/777's cleanup_tmp. */ +static void +cleanup_tmp(const char *tmpdir, const char *base) +{ + char p[640]; + snprintf(p, sizeof p, "%s/%s.ww", tmpdir, base); unlink(p); + snprintf(p, sizeof p, "%s/%s.s", tmpdir, base); unlink(p); + snprintf(p, sizeof p, "%s/%s.o", tmpdir, base); unlink(p); + snprintf(p, sizeof p, "%s/%s.combined.ww", tmpdir, base); unlink(p); + snprintf(p, sizeof p, "%s/%s", tmpdir, base); unlink(p); + rmdir(tmpdir); +} + +static int +build_via_driver(const char *driver, const char *tmpdir, const char *cwd, + const char *src) +{ + char cmd[2048]; + snprintf(cmd, sizeof cmd, + "cd %s && timeout 180 %s build -I %s/lib %s 2>/dev/null", + tmpdir, driver, cwd, src); + return runwait(cmd); +} + +static int +run_row(const char *driver, const char *cwd, const struct row *r, int seq) +{ + char tmpdir[256], src[512], base[64], outbin[768]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/bvs_%d_d_%d", getpid(), seq); + snprintf(base, sizeof base, "main778"); + snprintf(src, sizeof src, "%s/%s.ww", tmpdir, base); + mkdir(tmpdir, 0755); + if (write_source(src, r->src) != 0) { cleanup_tmp(tmpdir, base); return -1; } + int rc; + int br = build_via_driver(driver, tmpdir, cwd, src); + if (br == 0) { + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + rc = runwait(outbin); + } else { + rc = -1; + } + cleanup_tmp(tmpdir, base); + return rc; +} + +/* asm_byte_identical — diff cstage vs wwstage .s. Parallel trees so + * ww_ww writing intermediates next to the source doesn't clobber the + * cstage .s (CLAUDE.md rule 14 phase split). Mirror of 776's/777's. */ +static int +asm_byte_identical(const char *cdrv, const char *wdrv, const char *cwd, + const struct row *r, int seq) +{ + char src[512], tdc[256], tdw[256], base[64], cs[512], ws[512]; + snprintf(tdc, sizeof tdc, "/tmp/bvs_%d_c_%d", getpid(), seq); + snprintf(tdw, sizeof tdw, "/tmp/bvs_%d_w_%d", getpid(), seq); + snprintf(base, sizeof base, "main778"); + mkdir(tdc, 0755); + mkdir(tdw, 0755); + snprintf(src, sizeof src, "%s/%s.ww", tdc, base); + if (write_source(src, r->src) != 0) { cleanup_tmp(tdc, base); cleanup_tmp(tdw, base); return -1; } + int rc = -1; + if (build_via_driver(cdrv, tdc, cwd, src) != 0) goto out; + snprintf(cs, sizeof cs, "%s/%s.s", tdc, base); + + snprintf(src, sizeof src, "%s/%s.ww", tdw, base); + if (write_source(src, r->src) != 0) goto out; + if (build_via_driver(wdrv, tdw, cwd, src) != 0) goto out; + snprintf(ws, sizeof ws, "%s/%s.s", tdw, base); + + FILE *fc = fopen(cs, "rb"); + FILE *fw = fopen(ws, "rb"); + if (fc && fw) { + rc = 0; + for (;;) { + int a = fgetc(fc); + int b = fgetc(fw); + if (a != b) { rc = -1; break; } + if (a == EOF) break; + } + } + if (fc) fclose(fc); + if (fw) fclose(fw); +out: + cleanup_tmp(tdc, base); + cleanup_tmp(tdw, base); + return rc; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char cwd[256]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + char absbin[512]; + if (bin[0] != '/') { + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char cdrv[640], wdrv[640]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + int wwpresent = (access(wdrv, X_OK) == 0); + int seq = 0; + + for (int i = 0; i < n; i++) { + if (rows[i].stage_mask & STAGE_CS) { + total++; + int got = run_row(cdrv, cwd, &rows[i], seq++); + if (got != rows[i].want_exit) { + fprintf(stderr, + "bufio_vstream_run[cs][%s]: exit=%d want=%d\n", + rows[i].label, got, rows[i].want_exit); + fail++; + } + } + if (wwpresent && (rows[i].stage_mask & STAGE_WW)) { + total++; + int got = run_row(wdrv, cwd, &rows[i], seq++); + if (got != rows[i].want_exit) { + fprintf(stderr, + "bufio_vstream_run[ww][%s]: exit=%d want=%d\n", + rows[i].label, got, rows[i].want_exit); + fail++; + } + if (rows[i].byte_id) { + total++; + if (asm_byte_identical(cdrv, wdrv, cwd, &rows[i], seq++) != 0) { + fprintf(stderr, + "bufio_vstream_run[byte-id][%s]: cstage vs wwstage asm differs\n", + rows[i].label); + fail++; + } + } + } + } + + if (!wwpresent) + fprintf(stderr, "bufio_vstream_run: skip wwstage (no %s)\n", wdrv); + + if (fail) { + fprintf(stderr, "bufio_vstream_run: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("bufio_vstream_run: %d/%d ok\n", total, total); + return 0; +}