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).
604 lines
22 KiB
C
604 lines
22 KiB
C
/*
|
|
* 778_bufio_vstream_run — project #94 fold-eFinal(PREP) sentinel. Pins
|
|
* the lib/bufio/vstream.ww parallel API — a COLLAPSE of the OLD bufio
|
|
* surface onto the io.vtable, NOT a port of Hare's missing scanner
|
|
* features (those are #217, OUT of eFinal). Covers:
|
|
* - buffered stream: `bufio_ctx` (vt@offset0 for the intrusive cast),
|
|
* `bufio_vstream` (returns the ctx BY VALUE; sret field-by-field)
|
|
* over an underlying **io.vstream** src — every underlying read/
|
|
* write/close goes through io.st_read/io.st_write/io.st_close (the
|
|
* vtable dispatchers), io.error forwards directly (no nomem-widen).
|
|
* Plus setflush_v / flush_v (public + internal drain) / unread_v /
|
|
* isbuffered_v (fn-ptr-equality discriminator).
|
|
* - scanner: `scanner_v` (value-return newscannerbuf_v over an
|
|
* io.vstream src) + scanbyte_v / scanbytes_v (single-byte delim) /
|
|
* scanline_v / finish_v.
|
|
* Coexists with the pre-vtable bufio.init / scanner / isbuffered surface
|
|
* in bufio.ww (fold-eFinal FLIP retires the latter + drops the `_v`
|
|
* suffix: bufio_ctx→stream, bufio_vstream→init, scanner_v→scanner,
|
|
* scanbytes_v→scanbytes, newscannerbuf_v→newscannerbuf, …).
|
|
*
|
|
* 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, cs.s==ww.s — bufio is NOT
|
|
* compiler-embedded, so these byte-id rows are its ONLY byte-id cover).
|
|
*
|
|
* 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:
|
|
* | bufio_ctx built field-by-field →
|
|
* | return r (sret) → caller takes &b.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) and
|
|
* #210 (slice-field struct-lit drop): not reachable from the
|
|
* value-return form — bufio_ctx is built field-by-field in a
|
|
* local then sret-returned, no `alloc(T{...})` struct-lit.
|
|
*
|
|
* 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 <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/wait.h>
|
|
|
|
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 mst: memio.stream = memio.fixed_vstream(raw[0:16]);\n"
|
|
" let msrc: io.vstream = &mst.vt;\n"
|
|
" let rbuf: [8]u8;\n"
|
|
" let wbuf: [8]u8;\n"
|
|
" let b: bufio.bufio_ctx = bufio.bufio_vstream(msrc, rbuf[0:8], wbuf[0:8]);\n"
|
|
" let vs: io.vstream = &b.vt;\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 (mst.pos != 0) { return 91; };\n"
|
|
" let cl = io.st_close(vs);\n"
|
|
" if (cl is io.error) { return 92; };\n"
|
|
" if (mst.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 mst: memio.stream = memio.fixed_vstream(raw[0:8]);\n"
|
|
" let msrc: io.vstream = &mst.vt;\n"
|
|
" let rbuf: [8]u8;\n"
|
|
" let wbuf: [4]u8;\n"
|
|
" let b: bufio.bufio_ctx = bufio.bufio_vstream(msrc, rbuf[0:8], wbuf[0:4]);\n"
|
|
" let vs: io.vstream = &b.vt;\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 mst: memio.stream = memio.fixed_vstream(raw[0:8]);\n"
|
|
" let msrc: io.vstream = &mst.vt;\n"
|
|
" let rbuf: [4]u8;\n"
|
|
" let wbuf: [4]u8;\n"
|
|
" let b: bufio.bufio_ctx = bufio.bufio_vstream(msrc, rbuf[0:4], wbuf[0:4]);\n"
|
|
" let vsbuf: io.vstream = &b.vt;\n"
|
|
" let buf2: [4]u8;\n"
|
|
" let st2: memio.stream = memio.fixed_vstream(buf2[0:4]);\n"
|
|
" let vsplain: io.vstream = &st2.vt;\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 mst: memio.stream = memio.fixed_vstream(raw[0:8]);\n"
|
|
" let msrc: io.vstream = &mst.vt;\n"
|
|
" let rbuf: [4]u8;\n"
|
|
" let wbuf: [4]u8;\n"
|
|
" let bc: bufio.bufio_ctx = bufio.bufio_vstream(msrc, rbuf[0:4], wbuf[0:4]);\n"
|
|
" let vs: io.vstream = &bc.vt;\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 mstA: memio.stream = memio.fixed_vstream(raA[0:4]);\n"
|
|
" let mstB: memio.stream = memio.fixed_vstream(raB[0:4]);\n"
|
|
" let sel: i32 = 1;\n"
|
|
" let src: io.vstream = &mstA.vt;\n"
|
|
" if (sel == 0) { src = &mstB.vt; };\n"
|
|
" let rbuf: [4]u8;\n"
|
|
" let wbuf: [4]u8;\n"
|
|
" let bc: bufio.bufio_ctx = bufio.bufio_vstream(src, rbuf[0:4], wbuf[0:4]);\n"
|
|
" let vs: io.vstream = &bc.vt;\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 },
|
|
/* scanner_lines — fold-eFinal PREP: the value-return scanner over an
|
|
* io.vstream src. newscannerbuf_v (Hare newscanner_buf) → scanline_v
|
|
* twice ("ab", "c") → trailing "def" with no newline is EOF_DISCARD
|
|
* → io.eof → finish_v. Pins the scanner reads through io.st_read
|
|
* (vtable dispatch) on the underlying memio stream. cs.s==ww.s. */
|
|
{ "scanner_lines",
|
|
"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]=97u8; raw[1]=98u8; raw[2]=10u8;\n"
|
|
" raw[3]=99u8; raw[4]=10u8;\n"
|
|
" raw[5]=100u8; raw[6]=101u8; raw[7]=102u8;\n"
|
|
" let mst: memio.stream = memio.dynamicfrom_vstream(raw[0:8]);\n"
|
|
" let vs: io.vstream = &mst.vt;\n"
|
|
" let win: [16]u8;\n"
|
|
" let sc: bufio.scanner_v = bufio.newscannerbuf_v(vs, win[0:16]);\n"
|
|
" let ok: i32 = 0;\n"
|
|
" match (bufio.scanline_v(&sc)) {\n"
|
|
" case let s: str => { if (s.len == 2 && s[0] == 97u8 && s[1] == 98u8) { ok += 1; }; };\n"
|
|
" case io.eof => { return 81; };\n"
|
|
" case let e: io.error => { return 82; };\n"
|
|
" case bufio.overflow => { return 83; };\n"
|
|
" };\n"
|
|
" match (bufio.scanline_v(&sc)) {\n"
|
|
" case let s: str => { if (s.len == 1 && s[0] == 99u8) { ok += 1; }; };\n"
|
|
" case io.eof => { return 84; };\n"
|
|
" case let e: io.error => { return 85; };\n"
|
|
" case bufio.overflow => { return 86; };\n"
|
|
" };\n"
|
|
" match (bufio.scanline_v(&sc)) {\n"
|
|
" case let s: str => { return 87; };\n"
|
|
" case io.eof => { ok += 1; };\n"
|
|
" case let e: io.error => { return 88; };\n"
|
|
" case bufio.overflow => { return 89; };\n"
|
|
" };\n"
|
|
" bufio.finish_v(&sc);\n"
|
|
" if (ok != 3) { return 90; };\n"
|
|
" return 52;\n"
|
|
"};\n",
|
|
52,
|
|
STAGE_CS | STAGE_WW, 1 },
|
|
/* scanner_byte_bytes — scanbyte_v pops 'A'; scanbytes_v(',') then
|
|
* tokenizes "B" and "CD" from "AB,CD,". Single-byte delim (the OLD
|
|
* scantok behaviour; multibyte is #217). cs.s==ww.s. */
|
|
{ "scanner_byte_bytes",
|
|
"package main;\n"
|
|
"import os;\n"
|
|
"import bufio;\n"
|
|
"import memio;\n"
|
|
"import io;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let raw: [6]u8;\n"
|
|
" raw[0]=65u8; raw[1]=66u8; raw[2]=44u8;\n"
|
|
" raw[3]=67u8; raw[4]=68u8; raw[5]=44u8;\n"
|
|
" let mst: memio.stream = memio.dynamicfrom_vstream(raw[0:6]);\n"
|
|
" let vs: io.vstream = &mst.vt;\n"
|
|
" let win: [8]u8;\n"
|
|
" let sc: bufio.scanner_v = bufio.newscannerbuf_v(vs, win[0:8]);\n"
|
|
" let fb: u8 = 0u8;\n"
|
|
" match (bufio.scanbyte_v(&sc)) {\n"
|
|
" case let b: u8 => { fb = b; };\n"
|
|
" case io.eof => { return 81; };\n"
|
|
" case let e: io.error => { return 82; };\n"
|
|
" };\n"
|
|
" if (fb != 65u8) { return 83; };\n"
|
|
" match (bufio.scanbytes_v(&sc, 44u8)) {\n"
|
|
" case let bs: []u8 => { if (bs.len != 1 || bs[0] != 66u8) { return 84; }; };\n"
|
|
" case io.eof => { return 85; };\n"
|
|
" case let e: io.error => { return 86; };\n"
|
|
" case bufio.overflow => { return 87; };\n"
|
|
" };\n"
|
|
" match (bufio.scanbytes_v(&sc, 44u8)) {\n"
|
|
" case let bs: []u8 => { if (bs.len != 2 || bs[0] != 67u8 || bs[1] != 68u8) { return 88; }; };\n"
|
|
" case io.eof => { return 89; };\n"
|
|
" case let e: io.error => { return 90; };\n"
|
|
" case bufio.overflow => { return 91; };\n"
|
|
" };\n"
|
|
" return 53;\n"
|
|
"};\n",
|
|
53,
|
|
STAGE_CS | STAGE_WW, 1 },
|
|
/* stream_setflush — setflush_v swaps the auto-flush byte-set to ';';
|
|
* writing "ab;" auto-flushes (contains ';') so the sink advances to
|
|
* 3 with no explicit flush. Pins setflush_v + the flush-scan path.
|
|
* cs.s==ww.s. */
|
|
{ "stream_setflush",
|
|
"package main;\n"
|
|
"import os;\n"
|
|
"import bufio;\n"
|
|
"import memio;\n"
|
|
"import io;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let sinkbuf: [16]u8;\n"
|
|
" let sink: memio.stream = memio.fixed_vstream(sinkbuf[0:16]);\n"
|
|
" let sinkvs: io.vstream = &sink.vt;\n"
|
|
" let rb: [4]u8;\n"
|
|
" let wb: [8]u8;\n"
|
|
" let b: bufio.bufio_ctx = bufio.bufio_vstream(sinkvs, rb[0:4], wb[0:8]);\n"
|
|
" let bvs: io.vstream = &b.vt;\n"
|
|
" let semi: [1]u8;\n"
|
|
" semi[0] = 59u8;\n"
|
|
" bufio.setflush_v(&b, semi[0:1]);\n"
|
|
" let payload: [3]u8;\n"
|
|
" payload[0]=97u8; payload[1]=98u8; payload[2]=59u8;\n"
|
|
" let wr = io.st_write(bvs, payload[0:3]);\n"
|
|
" if (!(wr is size)) { return 81; };\n"
|
|
" if (sink.pos != 3) { return 82; };\n"
|
|
" if (sinkbuf[0] != 97u8 || sinkbuf[2] != 59u8) { return 83; };\n"
|
|
" return 54;\n"
|
|
"};\n",
|
|
54,
|
|
STAGE_CS | STAGE_WW, 1 },
|
|
/* stream_unread — read "XYZ" then unread_v("XY") pushes the two
|
|
* bytes back in front of the read buffer; the next read returns them
|
|
* first. Pins unread_v's in-place shift through *bufio_ctx.
|
|
* cs.s==ww.s. */
|
|
{ "stream_unread",
|
|
"package main;\n"
|
|
"import os;\n"
|
|
"import bufio;\n"
|
|
"import memio;\n"
|
|
"import io;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let raw: [4]u8;\n"
|
|
" raw[0]=88u8; raw[1]=89u8; raw[2]=90u8;\n"
|
|
" let mst: memio.stream = memio.dynamicfrom_vstream(raw[0:3]);\n"
|
|
" let vs: io.vstream = &mst.vt;\n"
|
|
" let rbuf: [4]u8;\n"
|
|
" let wbuf: [4]u8;\n"
|
|
" let b: bufio.bufio_ctx = bufio.bufio_vstream(vs, rbuf[0:4], wbuf[0:4]);\n"
|
|
" let bvs: io.vstream = &b.vt;\n"
|
|
" let out: [4]u8;\n"
|
|
" let rd = io.st_read(bvs, out[0:3]);\n"
|
|
" let n: size = 0;\n"
|
|
" if (rd is size) { n = rd as size; };\n"
|
|
" if (n != 3: size || out[0] != 88u8 || out[2] != 90u8) { return 81; };\n"
|
|
" let push: [2]u8;\n"
|
|
" push[0]=88u8; push[1]=89u8;\n"
|
|
" bufio.unread_v(&b, push[0:2]);\n"
|
|
" let out2: [2]u8;\n"
|
|
" let rd2 = io.st_read(bvs, out2[0:2]);\n"
|
|
" let n2: size = 0;\n"
|
|
" if (rd2 is size) { n2 = rd2 as size; };\n"
|
|
" if (n2 != 2: size || out2[0] != 88u8 || out2[1] != 89u8) { return 82; };\n"
|
|
" return 55;\n"
|
|
"};\n",
|
|
55,
|
|
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
|
|
* <src>.{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;
|
|
}
|