/* * 777_fmt_handle_run — io fold-2 (#5) commit-2 sentinel. Pins the * fprint family over an [[io.handle]] sink after the fdsink shim * removal: fmt's fprint/fprintln/fprintf now take `io.handle = * (io.file | io.stream)`, so a raw fd (file arm) and a memio vtable * (stream arm) both flow into the same surface and io.write dispatches * per arm. Supersedes the pre-#5 fd_ctx workaround (the fake-stream * vtable around os.write) — drew's placeholder for the then-missing * handle, removed by this commit. * * The file-arm rows open a per-process /tmp output file, call one * fprint-family fn with `fd: io.file` (widens to io.handle), close, * reopen for read, read the bytes back, and assert both (a) the exact * byte content and (b) a unique row-tagged exit constant. The * stream-arm row drives the SAME fprintf over a memio.fixed vtable * (the io.stream arm) and reads back via memio.string — the two-arm * coverage #5 graduated. * * Cstage-only per row (no STAGE_WW, no byte_id). #209 (the wwstage * checker bail `case: not a variant of scrutinee` + `match: variant not * handled (formattable)` on fmt's spread-union match-arms) is now CLOSED * — wwstage compiles fmt. The residual blocker is a SEPARATE pre-existing * cgen cluster surfaced once fmt actually codegens under wwstage: * - #226: io.read's error widening (cgwidentagremap) loses the * io.eof/io.error NAMED tinfo identity under fmt-presence * (flatvariantidxt → -1 → tag collapses to 0); cs!=ww whole-file, so * byte-id fails even though io.read is unused here (it's emitted * regardless). The #10/#218/tinfo-lossy-nominal family. * - #227: spread-union widen + return-ABI, broken in BOTH stages. * 995 self-rebuild dodges all of this because no selfhost main pulls fmt * transitively. Byte-id graduates when #226 (+#227) land. * * row | what it pins * ---------------------+-------------------------------------- * fprintf_file_int | fprintf(fd: io.file, "v={}\n", 42) * | over the FILE arm -> io.write -> os.write, * | plus the {n}-placeholder parser through * | formatfield. * fprintln_file_multi | fprintln(fd: io.file, 7, "hi", true) — * | three positional args, space-separator * | + trailing newline shape over the file arm. * fprint_file_raw | fprint(fd: io.file, "raw") — one str arg, * | no placeholder parser (bare fprint loop). * branched_fprintf | branched callee per #105: two distinct * | format strings runtime-selected from a * | single fprintf call site. * fprintf_stream | fprintf over a memio.fixed io.stream * | (the STREAM arm of io.handle) read back * | via memio.string — the other handle arm. * * IMPORT-ORDER WORKAROUND (sibling task #208, pre-existing): every row * places `import os;` FIRST for the same reason 776 documents (wwstage * checker ordering-sensitivity on os.tryread/trywrite/tryopen's bare * `return r;` over a tagged return type). * * BOOTSTRAP-EMBED CHECK: fmt is NOT embedded in any selfhost * combined.ww (grep confirmed), so the #5 fmt graduation does NOT * require a Makefile regen (#110); 990-997 byte-id gates stay green by * virtue of fmt being test-only on the bootstrap surface. * * GATE POLARITY: must stay GREEN. A red here means lib/fmt regressed, * the file->handle widen miscompiled, io.write's per-arm dispatch broke, * the {n}-placeholder dispatch through formatfield regressed, or the * memio stream arm lost its offset-0 vtable invariant. */ #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[] = { { "fprintf_file_int", "package main;\n" "import os;\n" "import fmt;\n" "import io;\n" "export fn main() i32 = {\n" " let path: str = \"/tmp/wwfmth_777_a\";\n" " let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n" " if (fd < 0) { return 90; };\n" " let wr = fmt.fprintf(fd: io.file, \"v={}\\n\", 42i64);\n" " let nw: size = 0;\n" " match (wr) {\n" " case let n: size => { nw = n; };\n" " case io.error => { return 91; };\n" " };\n" " os.close(fd);\n" " let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n" " if (rd < 0) { return 92; };\n" " let buf: [16]u8;\n" " let n: i64 = os.read(rd, &buf[0], 16u64);\n" " os.close(rd);\n" " if (n != 5i64) { return 93; };\n" " if (nw != 5: size) { return 94; };\n" " if (buf[0] != 118u8 || buf[1] != 61u8 || buf[2] != 52u8 || buf[3] != 50u8 || buf[4] != 10u8) { return 95; };\n" " return 42;\n" "};\n", 42, STAGE_CS | STAGE_WW, 1 }, { "fprintln_file_multi", "package main;\n" "import os;\n" "import fmt;\n" "import io;\n" "export fn main() i32 = {\n" " let path: str = \"/tmp/wwfmth_777_b\";\n" " let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n" " if (fd < 0) { return 90; };\n" " let wr = fmt.fprintln(fd: io.file, 7i64, \"hi\", true);\n" " let nw: size = 0;\n" " match (wr) {\n" " case let n: size => { nw = n; };\n" " case io.error => { return 91; };\n" " };\n" " os.close(fd);\n" " let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n" " if (rd < 0) { return 92; };\n" " let buf: [16]u8;\n" " let n: i64 = os.read(rd, &buf[0], 16u64);\n" " os.close(rd);\n" " if (n != 10i64) { return 93; };\n" " if (nw != 10: size) { return 94; };\n" " if (buf[0] != 55u8 || buf[1] != 32u8 || buf[2] != 104u8 || buf[3] != 105u8 || buf[4] != 32u8 || buf[5] != 116u8 || buf[6] != 114u8 || buf[7] != 117u8 || buf[8] != 101u8 || buf[9] != 10u8) { return 95; };\n" " return 43;\n" "};\n", 43, STAGE_CS | STAGE_WW, 1 }, { "fprint_file_raw", "package main;\n" "import os;\n" "import fmt;\n" "import io;\n" "export fn main() i32 = {\n" " let path: str = \"/tmp/wwfmth_777_c\";\n" " let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n" " if (fd < 0) { return 90; };\n" " let wr = fmt.fprint(fd: io.file, \"raw\");\n" " let nw: size = 0;\n" " match (wr) {\n" " case let n: size => { nw = n; };\n" " case io.error => { return 91; };\n" " };\n" " os.close(fd);\n" " let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n" " if (rd < 0) { return 92; };\n" " let buf: [8]u8;\n" " let n: i64 = os.read(rd, &buf[0], 8u64);\n" " os.close(rd);\n" " if (n != 3i64) { return 93; };\n" " if (nw != 3: size) { return 94; };\n" " if (buf[0] != 114u8 || buf[1] != 97u8 || buf[2] != 119u8) { return 95; };\n" " return 44;\n" "};\n", 44, STAGE_CS | STAGE_WW, 1 }, { "branched_fprintf", "package main;\n" "import os;\n" "import fmt;\n" "import io;\n" "export fn main() i32 = {\n" " let path: str = \"/tmp/wwfmth_777_d\";\n" " let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n" " if (fd < 0) { return 90; };\n" " let sel: i32 = 1;\n" " let f1: str = \"A{}\";\n" " let f2: str = \"B{}\";\n" " let fmtstr: str = f1;\n" " if (sel == 0) { fmtstr = f2; };\n" " let wr = fmt.fprintf(fd: io.file, fmtstr, 9i64);\n" " let nw: size = 0;\n" " match (wr) {\n" " case let n: size => { nw = n; };\n" " case io.error => { return 91; };\n" " };\n" " os.close(fd);\n" " let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n" " if (rd < 0) { return 92; };\n" " let buf: [8]u8;\n" " let n: i64 = os.read(rd, &buf[0], 8u64);\n" " os.close(rd);\n" " if (n != 2i64) { return 93; };\n" " if (nw != 2: size) { return 94; };\n" " if (buf[0] != 65u8 || buf[1] != 57u8) { return 95; };\n" " return 45;\n" "};\n", 45, STAGE_CS | STAGE_WW, 1 }, { "fprintf_stream", "package main;\n" "import os;\n" "import fmt;\n" "import io;\n" "import memio;\n" "export fn main() i32 = {\n" " let buf: [16]u8;\n" " let st: memio.stream = memio.fixed(buf[0:16]);\n" " let s: io.stream = &st.vt;\n" " let wr = fmt.fprintf(s, \"v={}\\n\", 42i64);\n" " let nw: size = 0;\n" " match (wr) {\n" " case let n: size => { nw = n; };\n" " case io.error => { return 91; };\n" " };\n" " if (nw != 5: size) { return 94; };\n" " let view: str = memio.string(&st);\n" " if (view.len != 5) { return 93; };\n" " if (view[0] != 118u8 || view[1] != 61u8 || view[2] != 52u8 || view[3] != 50u8 || view[4] != 10u8) { return 95; };\n" " return 46;\n" "};\n", 46, 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 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", tmpdir, base); unlink(p); /* #93 sep layout: the sep scratch dir + the tmpdir-pinned pkgcache. */ snprintf(p, sizeof p, "rm -rf %s/%s.sepwork %s/pkgc", tmpdir, base, tmpdir); if (system(p)) {} /* best-effort */ rmdir(tmpdir); } static int build_via_driver(const char *driver, const char *tmpdir, const char *cwd, const char *src) { char cmd[2048]; /* #93 sep layout: `--sep -o ` emits the asm to * .sepwork/__root.s; WW_PKGCACHE is pinned under tmpdir so the * shared out/.pkgcache is untouched and the cache dies with the * tmpdir. PRESERVES the `-I %s/lib` import-dir flag. */ snprintf(cmd, sizeof cmd, "cd %s && b='%s'; WW_PKGCACHE=%s/pkgc timeout 180 %s build --sep " "-I %s/lib -o \"${b%%.ww}\" \"$b\" 2>/dev/null", tmpdir, src, tmpdir, driver, cwd); 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/fh_%d_d_%d", getpid(), seq); snprintf(base, sizeof base, "main777"); 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. Unused * while every row is cstage-only (#226/#227; #209 is closed), kept for * the byte-id graduation once those cgen folds land. */ 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/fh_%d_c_%d", getpid(), seq); snprintf(tdw, sizeof tdw, "/tmp/fh_%d_w_%d", getpid(), seq); snprintf(base, sizeof base, "main777"); 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.sepwork/__root.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.sepwork/__root.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, "fmt_handle_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, "fmt_handle_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, "fmt_handle_run[byte-id][%s]: cstage vs wwstage asm differs\n", rows[i].label); fail++; } } } } if (!wwpresent) fprintf(stderr, "fmt_handle_run: skip wwstage (no %s)\n", wdrv); if (fail) { fprintf(stderr, "fmt_handle_run: %d/%d fixtures failed\n", fail, total); return 1; } printf("fmt_handle_run: %d/%d ok\n", total, total); return 0; }