/* * 781_fmt_compositions_run — io fold-2 (#5) commit-2 sentinel. Pins the * composition wrappers in lib/fmt/fmt.ww — fprintln / fprintfln / * bsprintf / asprintf — plus the memio accessor (memio.string in * lib/memio/memio.ww) they read back through. #5 graduated the * fprint-family sink io.stream -> io.handle; the file-arm rows drive * fprintln / fprintfln over `fd: io.file`, the stream-arm rows over a * memio.fixed io.stream — both arms of io.handle. * * memio.string is the direct enabler: bsprintf reads its written prefix * via memio.string; asprintf reads its grown buffer via the same * accessor. * * row | what it pins * --------------------+-------------------------------------- * fprintln_file_basic | fprintln(fd: io.file, 1, "hi", true) * | over the file arm — space-separator plus * | trailing newline byte content. * fprintfln_file_fmt | fprintfln(fd: io.file, "x={}", 7) over the * | file arm — placeholder substitution + * | trailing newline. * fprintln_basic | fmt.fprintln dispatched over a memio.fixed * | io.stream (stream arm) + read back via * | memio.string. Pins the composition symbol. * fprintfln_basic | fmt.fprintfln over memio.fixed + memio.string, * | same stream-arm rationale. * bsprintf_basic | bsprintf into a caller-supplied [16]u8 * | buffer through memio.fixed; memio.string * | returns the prefix view. Asserts both the * | caller-buffer bytes AND the returned str view. * asprintf_basic | asprintf into a heap-grown str through * | memio.dynamic; memio.string + shrink-copy + * | io.close. Asserts the owned str bytes, then * | frees via os.free. * * 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 the pre-existing cgen * cluster #226 (io.read error-remap nominal-identity) + #227 (spread-union * widen/return-ABI). Identical carve-out to 777_fmt_handle_run + * 780_fmt_mods_run. Byte-id graduates when #226 (+#227) land. * * IMPORT-ORDER WORKAROUND (sibling task #208, pre-existing): every row * places `import os;` FIRST for the same reason 776/777/780 document * (wwstage checker ordering-sensitivity on os.tryread / trywrite / * tryopen's bare `return r;` over a tagged return type). * * DEFERRALS / RELATED (NOT addressed by these rows): * * - #226 (io.read error-remap nominal-identity) + #227 (spread-union * widen/return-ABI): block STAGE_WW here, same as 777/780. #209 (the * checker bail) is closed; these cgen folds are the residual. * Cstage-only until they land. * - #173 (TRY-on-tagged-return both-stages broken): bsprintf widens * nomem into io.error inline rather than `memio.fixed(buf)?` for * the same reason memio.ww does. * * BOOTSTRAP-EMBED CHECK: fmt is NOT embedded in any selfhost * combined.ww (grep verified). memio IS embedded in w6c + wwdump * combined.ww but is unchanged by #5, so no regen rides along here. * 990/994/995 byte-id gates stay green. * * GATE POLARITY: must stay GREEN. A red here means fprintln / * fprintfln dropped the newline byte, bsprintf returned a stale * memio.string view (intrusive cast broke), asprintf failed to * shrink-copy / close cleanly, or the underlying fprint / fprintf * primitives regressed over the io.handle sink. */ #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[] = { { "fprintln_file_basic", "package main;\n" "import os;\n" "import fmt;\n" "import io;\n" "export fn main() i32 = {\n" " let path: str = \"/tmp/wwfmtc_781_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.fprintln(fd: io.file, 1i64, \"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] != 49u8) { return 95; };\n" " if (buf[1] != 32u8) { return 96; };\n" " if (buf[2] != 104u8 || buf[3] != 105u8) { return 97; };\n" " if (buf[4] != 32u8) { return 98; };\n" " if (buf[9] != 10u8) { return 99; };\n" " return 60;\n" "};\n", 60, STAGE_CS | STAGE_WW, 1 }, { "fprintfln_file_fmt", "package main;\n" "import os;\n" "import fmt;\n" "import io;\n" "export fn main() i32 = {\n" " let path: str = \"/tmp/wwfmtc_781_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.fprintfln(fd: io.file, \"x={}\", 7i64);\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 != 4i64) { return 93; };\n" " if (nw != 4: size) { return 94; };\n" " if (buf[0] != 120u8 || buf[1] != 61u8 || buf[2] != 55u8 || buf[3] != 10u8) { return 95; };\n" " return 61;\n" "};\n", 61, STAGE_CS | STAGE_WW, 1 }, { "fprintln_basic", "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 vs: io.stream = &st.vt;\n" " let wr = fmt.fprintln(vs, 1i64, \"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" " if (nw != 10: size) { return 92; };\n" " let view: str = memio.string(&st);\n" " if (view.len != 10) { return 93; };\n" " if (view[0] != 49u8) { return 94; };\n" " if (view[1] != 32u8) { return 95; };\n" " if (view[2] != 104u8 || view[3] != 105u8) { return 96; };\n" " if (view[9] != 10u8) { return 97; };\n" " return 64;\n" "};\n", 64, STAGE_CS | STAGE_WW, 1 }, { "fprintfln_basic", "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 vs: io.stream = &st.vt;\n" " let wr = fmt.fprintfln(vs, \"x={}\", 7i64);\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 != 4: size) { return 92; };\n" " let view: str = memio.string(&st);\n" " if (view.len != 4) { return 93; };\n" " if (view[0] != 120u8 || view[1] != 61u8) { return 94; };\n" " if (view[2] != 55u8 || view[3] != 10u8) { return 95; };\n" " return 65;\n" "};\n", 65, STAGE_CS | STAGE_WW, 1 }, { "bsprintf_basic", "package main;\n" "import os;\n" "import fmt;\n" "import io;\n" "export fn main() i32 = {\n" " let buf: [16]u8;\n" " let r = fmt.bsprintf(buf[0:16], \"v={}\", 42i64);\n" " let view: str = \"\";\n" " match (r) {\n" " case let s: str => { view = s; };\n" " case io.error => { return 90; };\n" " };\n" " if (view.len != 4) { return 91; };\n" " if (view[0] != 118u8 || view[1] != 61u8) { return 92; };\n" " if (view[2] != 52u8 || view[3] != 50u8) { return 93; };\n" " if (buf[0] != 118u8 || buf[3] != 50u8) { return 94; };\n" " return 62;\n" "};\n", 62, STAGE_CS | STAGE_WW, 1 }, { "asprintf_basic", "package main;\n" "import os;\n" "import fmt;\n" "import io;\n" "export fn main() i32 = {\n" " let view: str = fmt.asprintf(\"hello={}\", 9i64);\n" " if (view.len != 7) { return 90; };\n" " if (view[0] != 104u8 || view[1] != 101u8 || view[2] != 108u8) { return 91; };\n" " if (view[3] != 108u8 || view[4] != 111u8) { return 92; };\n" " if (view[5] != 61u8 || view[6] != 57u8) { return 93; };\n" " os.free(view.ptr: *void, view.len: u64);\n" " return 63;\n" "};\n", 63, 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 (task #15), so each row's build leaves .{combined.ww, * s,o} + bare exe alongside. Mirror of 777/780's. */ 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/fhc_%d_d_%d", getpid(), seq); snprintf(base, sizeof base, "main781"); 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; } 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_compositions_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_compositions_run[ww][%s]: exit=%d want=%d\n", rows[i].label, got, rows[i].want_exit); fail++; } } } if (!wwpresent) fprintf(stderr, "fmt_compositions_run: skip wwstage (no %s)\n", wdrv); if (fail) { fprintf(stderr, "fmt_compositions_run: %d/%d fixtures failed\n", fail, total); return 1; } printf("fmt_compositions_run: %d/%d ok\n", total, total); return 0; }