/* * 779_log_vstream_run — project #94 fold-e5 sentinel. Pins the * additive lib/log/vstream.ww Option C parallel API: module-static * stderrsink_ctx_g with `vt: io.vtable` as the first field for the * intrusive vstream cast, the 10 _v variants of OLD log.ww's surface * (new_v / lprintln_v / println_v / lprintfln_v / printfln_v / lfatal_v * / fatal_v / lfatalf_v / fatalf_v / setlogger_v), a vlogger vtable + * vstdlogger over io.vstream sink, and lazy ensureinit_v wiring the * stderr default + silent_v / default_v / global_v *vlogger globals. * Coexists with the pre-vtable log.logger / log.stdlogger / log.new / * log.println / etc. surface in log.ww (fold-eFinal, task #50, retires * the latter and drops the `_v` suffix wholesale to match Hare's bare * names). * * Each row imports os + log + memio + io. log itself imports fmt, * so the formattable / field types flow through transitively. * * Cstage-only per row (no STAGE_WW, no byte_id) — pre-existing * wwstage bug #209 (sibling of #190): the wwstage checker bails on * match-arm-over-formattable when fmt is imported transitively, which * lib/log does for the vlogger vtable signatures + the fmt.vfprint / * vfprintf dispatch in stdprintln_v / stdprintfln_v. The bug bites the * OLD log surface identically — `log.println("hi")` from a probe * trips the same trace. Existing 970 logtest runs the @test fixture * under cstage `ww run` only, so the OLD surface is fine via cstage. * 995 self-rebuild dodges it because no selfhost cmd imports log * transitively. Fix-of-the-bug = one-class checker repair, out-of- * scope for the additive fold-e5; closes the wwstage half here when * it lands. Byte-id graduates with #209 close. * * row | what it pins * --------------------------+-------------------------------------- * println_v_default_stderr | log.println_v through the [[default_v]] * | global, routed to fd 2 via the * | module-static stderrsink_ctx_g. Asserts * | ensureinit_v wires + dispatches without * | crashing; stderr capture is left to the * | shell runner. Pins the full chain: * | println_v → lprintln_v(global_v) → * | (*vlogger).println_v → stdprintln_v → * | fmt.vfprint + io.st_write("\n"). * printfln_v_default_stderr | Same chain as row 1 but through the * | format-string slot (printfln_v → * | lprintfln_v(global_v) → stdprintfln_v * | → fmt.vfprintf + io.st_write("\n")). * | Pins the {n}-placeholder dispatch * | through vformatfield reaches the * | default sink. * lprintln_v_custom_sink | memio.fixed_vstream-backed vstdlogger; * | log.new_v wires the vtable + sink, * | log.lprintln_v dispatches "hi" + 7i64 * | through stdprintln_v. Asserts the * | exact bytes ("hi 7\n") land in the * | caller's buffer (fixed_vstream borrows * | the slice, so the bytes are visible * | back through buf[]). * branched_lprintln_v | branched callee per #105: two distinct * | vstdlogger sinks runtime-selected via * | *vlogger pointer. lprintln_v dispatches * | through the picked sink only; the * | other stays empty. Catches a constant- * | fold mistake in the fn-ptr dispatch * | (mirror of 778's branched_bufio_vstream). * * SIBLINGS (filed inline, NOT fixed here — fold-e5 is purely * additive over fold-e1/e2/e3/e4's frozen io.* + memio.* + fmt.* + * bufio.* surface plus the minimal `export` bump on fmt.vfprint / * fmt.vfprintf): * * - eFinal (#50): atomic flip + delete OLD log surface + drop the * `_v` suffix wholesale. * * - #206 (bare &fn → (*alias|void)): 2 cast sites at ensureinit_v * in lib/log/vstream.ww; drop out wholesale on close. * * - #173 (TRY-on-tagged-return both-stages broken): stderrwrite_v * constructs nomem and widens to io.error explicitly rather than * using `os.trywrite(...)?`; same shape memio.vstream.ww + * fmt.vstream.ww + bufio.vstream.ww adopt. * * - #209 (wwstage formattable match-arm bail): every row is * STAGE_CS-only; byte-id deferred until #209 lands. * * BOOTSTRAP-EMBED CHECK: log is NOT embedded in any selfhost * combined.ww (grep `package log\|import log` returned empty pre- * impl). Adding lib/log/vstream.ww + the two `export` bumps on * lib/fmt/vstream.ww does NOT require a Makefile regen (#110); only * lib/log/ + lib/fmt/ test paths see the new module surface. 990-997 * byte-id gates stay green by virtue of log being test-only and the * fmt vstream.ww changes being non-embedded. * * GATE POLARITY: must stay GREEN. A red here means vstream.ww * regressed, the module-static stderrsink_ctx_g zero-init or chained * vt-field assigns regressed, the intrusive vstream→*stderrsink_ctx * cast miscomputed offsets, ensureinit_v's one-shot guard broke, or * the fn-ptr dispatch through vlogger.println_v / printfln_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[] = { { "println_v_default_stderr", "package main;\n" "import os;\n" "import log;\n" "export fn main() i32 = {\n" " log.println_v(\"hello\");\n" " return 42;\n" "};\n", 42, STAGE_CS, 0 }, { "printfln_v_default_stderr", "package main;\n" "import os;\n" "import log;\n" "export fn main() i32 = {\n" " log.printfln_v(\"v={}\", 7i64);\n" " return 43;\n" "};\n", 43, STAGE_CS, 0 }, { "lprintln_v_custom_sink", "package main;\n" "import os;\n" "import log;\n" "import memio;\n" "import io;\n" "export fn main() i32 = {\n" " let buf: [16]u8;\n" " let r = memio.fixed_vstream(buf[0:16]);\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 sl: log.vstdlogger;\n" " log.new_v(&sl, vs);\n" " log.lprintln_v(&sl.logger, \"hi\", 7i64);\n" " if (buf[0] != 104u8) { return 91; };\n" " if (buf[1] != 105u8) { return 92; };\n" " if (buf[2] != 32u8) { return 93; };\n" " if (buf[3] != 55u8) { return 94; };\n" " if (buf[4] != 10u8) { return 95; };\n" " return 44;\n" "};\n", 44, STAGE_CS, 0 }, { "branched_lprintln_v", "package main;\n" "import os;\n" "import log;\n" "import memio;\n" "import io;\n" "export fn main() i32 = {\n" " let bufA: [16]u8;\n" " let bufB: [16]u8;\n" " let rA = memio.fixed_vstream(bufA[0:16]);\n" " let vsA: io.vstream = nil: *io.vtable;\n" " match (rA) {\n" " case let v: io.vstream => { vsA = v; };\n" " case nomem => { return 50; };\n" " };\n" " let rB = memio.fixed_vstream(bufB[0:16]);\n" " let vsB: io.vstream = nil: *io.vtable;\n" " match (rB) {\n" " case let v: io.vstream => { vsB = v; };\n" " case nomem => { return 51; };\n" " };\n" " let slA: log.vstdlogger;\n" " let slB: log.vstdlogger;\n" " log.new_v(&slA, vsA);\n" " log.new_v(&slB, vsB);\n" " let sel: i32 = 1;\n" " let chosen: *log.vlogger = &slA.logger;\n" " if (sel == 0) { chosen = &slB.logger; };\n" " log.lprintln_v(chosen, \"pick\");\n" " if (bufA[0] != 112u8) { return 91; };\n" " if (bufA[1] != 105u8) { return 92; };\n" " if (bufA[2] != 99u8) { return 93; };\n" " if (bufA[3] != 107u8) { return 94; };\n" " if (bufA[4] != 10u8) { return 95; };\n" " if (bufB[0] != 0u8) { return 96; };\n" " return 45;\n" "};\n", 45, STAGE_CS, 0 }, }; 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 778'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/lvs_%d_d_%d", getpid(), seq); snprintf(base, sizeof base, "main779"); 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); /* Suppress stderr for rows 1/2 — the default-sink probes * write through fd 2; don't pollute the test runner's * stderr. */ char runcmd[1024]; snprintf(runcmd, sizeof runcmd, "%s 2>/dev/null", outbin); rc = runwait(runcmd); } 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 778's. Unused * for fold-e5 — every row is STAGE_CS-only per #209 — but kept * scaffolded for when #209 closes and byte-id graduates. */ 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/lvs_%d_c_%d", getpid(), seq); snprintf(tdw, sizeof tdw, "/tmp/lvs_%d_w_%d", getpid(), seq); snprintf(base, sizeof base, "main779"); 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, "log_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, "log_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, "log_vstream_run[byte-id][%s]: cstage vs wwstage asm differs\n", rows[i].label); fail++; } } } } if (!wwpresent) fprintf(stderr, "log_vstream_run: skip wwstage (no %s)\n", wdrv); if (fail) { fprintf(stderr, "log_vstream_run: %d/%d fixtures failed\n", fail, total); return 1; } printf("log_vstream_run: %d/%d ok\n", total, total); return 0; }