lib/fmt: port V-side fprintln/fprintfln/bsprintf/asprintf (#94 fold-e7)
V had vfprint / vfprintf but no compositions over them, so callers needing the newline / printf-newline / bounded-buffer / heap-grow shapes still routed through the OLD io.stream-shaped fprintln / fprintfln / bsprintf / asprintf. Port the four compositions into vstream.ww as the v* twins: vfprintln + vfprintfln chain a "\n" vputbytes after the underlying primitive; vbsprintf threads a caller buffer through memio.fixed_vstream and returns the prefix view; vasprintf grows through memio.dynamic_vstream and shrink-copies to a tight allocation before io.st_close. Bundles the two memio enablers (fixed_string / dynamic_string in lib/memio/vstream.ww) that vbsprintf / vasprintf depend on directly, per drew-approved exception to one-class-one-commit (feedback_refactor_routing_same_class_drops applies — helpers are direct prereqs, not unrelated churn; the bus-routing site lives in v* fmt code, not in memio). They mirror OLD memio.string (memio.ww: 102) over the per-flavour *fixed_ctx / *dynamic_ctx intrusive cast, same shape as the read/write callback split at memio/vstream.ww:144. Mirror sites: vfprintln fmt.ww:240 fprintln ref/hare/fmt/wrappers.ha:48 vfprintfln fmt.ww:740 fprintfln ref/hare/fmt/wrappers.ha:69 vbsprintf fmt.ww:839 bsprintf ref/hare/fmt/wrappers.ha:42 vasprintf fmt.ww:873 asprintf ref/hare/fmt/wrappers.ha:29 Divergence vs Hare on vbsprintf: Hare returns `(const str | nomem)`; ww collapses to `(str | io.error)` so the underlying vfprintf io.error arm stays uniform. The fixed_vstream nomem widens into io.error explicitly (no `memio.fixed_vstream(buf)?`) because #173 (TRY-on- tagged-return both-stages broken) is still open — same shape memio/ vstream.ww adopted at line 87-99 for fixed_vstream itself. vasprintf keeps OLD's bare `str` return (no nomem variant on public surface). ken cs==ww mechanical: additive only, both stages compile identically. fmt is NOT embedded in any selfhost main.combined.ww (grep verified pre-impl: zero `^package fmt;` hits in selfhost/cmd/*/main.combined. ww). memio.vstream.ww IS embedded in w6c + wwdump combined.ww (lib/ ww/cgen.ww uses memio.dynamic for buffer growth); the two memio helpers regen-and-commit via ww build per #110 SSoT. test/wcc/781_fmt_vstream_compositions_run.c (cstage-only per #209): 4 rows pin all four V wrappers — fdprintln_v_run_basic (newline shape), fdprintfln_v_run_fmt ({n}-placeholder + newline), bsprintf_v_basic (fixed buffer + returned view + caller bytes), asprintf_v_basic (owned heap str + os.free roundtrip). Mirror of 777/780 cstage carve- out (#209 wwstage formattable match-arm bail). Byte-id graduates with #209 close. 214 total tests green (was 213).
This commit is contained in:
324
test/wcc/781_fmt_vstream_compositions_run.c
Normal file
324
test/wcc/781_fmt_vstream_compositions_run.c
Normal file
@@ -0,0 +1,324 @@
|
||||
/*
|
||||
* 781_fmt_vstream_compositions_run — project #94 fold-e7 sentinel.
|
||||
* Pins the V-side composition wrappers in lib/fmt/vstream.ww —
|
||||
* vfprintln / vfprintfln / vbsprintf / vasprintf — plus the two
|
||||
* memio enablers fold-e7 bundles alongside (memio.fixed_string /
|
||||
* memio.dynamic_string in lib/memio/vstream.ww). Coexists with the
|
||||
* OLD fmt.fprintln / fprintfln / bsprintf / asprintf surface in
|
||||
* fmt.ww:240,740,839,873 (fmt.ww UNCHANGED this fold; eFinal
|
||||
* task #50 collapses both surfaces and renames the `v` suffix off).
|
||||
*
|
||||
* The two memio helpers are direct enablers (vbsprintf reads its
|
||||
* written prefix via fixed_string; vasprintf reads its grown buffer
|
||||
* via dynamic_string), drew-approved per
|
||||
* feedback_refactor_routing_same_class_drops (prereqs, not churn).
|
||||
*
|
||||
* Each row imports fmt + os + io, drives one of the four V wrappers,
|
||||
* and asserts the exact bytes / size emitted. Per-row unique exit
|
||||
* constant lets a failure pinpoint the offending case.
|
||||
*
|
||||
* row | what it pins
|
||||
* --------------------------+--------------------------------------
|
||||
* fdprintln_v_run_basic | vfprintln through an fd_ctx vstream
|
||||
* | (via fdprintln_v's wired vstream sink
|
||||
* | dispatching vfprint then vputbytes
|
||||
* | "\n"). One i64 arg + str arg + bool
|
||||
* | arg; asserts space-separator plus
|
||||
* | trailing newline byte content.
|
||||
* fdprintfln_v_run_fmt | vfprintfln through fd_ctx vstream
|
||||
* | with a {n}-placeholder format string.
|
||||
* | Asserts placeholder substitution +
|
||||
* | trailing newline shape (mirror of
|
||||
* | fmt.fprintfln, fmt.ww:740).
|
||||
* bsprintf_v_basic | vbsprintf into a caller-supplied
|
||||
* | [16]u8 buffer through
|
||||
* | memio.fixed_vstream;
|
||||
* | memio.fixed_string returns the prefix
|
||||
* | view. Asserts both the bytes in the
|
||||
* | caller's buffer AND the returned str
|
||||
* | view (ptr == buf + 0, len == 5).
|
||||
* asprintf_v_basic | vasprintf into a heap-grown str
|
||||
* | through memio.dynamic_vstream;
|
||||
* | memio.dynamic_string + shrink-copy +
|
||||
* | io.st_close. Asserts the owned str
|
||||
* | bytes, then frees via os.free.
|
||||
*
|
||||
* Cstage-only per row (no STAGE_WW, no byte_id) — pre-existing
|
||||
* wwstage bug #209 (sibling of #190): the wwstage checker bails
|
||||
* `case: not a variant of scrutinee (X)` + `match: variant not
|
||||
* handled (formattable)` on fmt.formattable match-arms whenever a
|
||||
* downstream probe `import fmt;`s the package. Identical carve-out
|
||||
* to 777_fmt_vstream_run + 780_fmt_vstream_mods_run (cited there at
|
||||
* line 18-33 / 18-23). Byte-id graduates with #209 close.
|
||||
*
|
||||
* 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).
|
||||
*
|
||||
* SIBLINGS (filed inline, NOT fixed here — fold-e7 is purely
|
||||
* additive over fold-e1/e2/e3/e4/e5/e6's frozen io.* + memio.* +
|
||||
* fmt.* + bufio.* + log.* surface):
|
||||
*
|
||||
* - #209 (wwstage formattable match-arm bail): blocks STAGE_WW
|
||||
* here, same as 777/780. Cstage-only until close.
|
||||
* - #173 (TRY-on-tagged-return both-stages broken): vbsprintf
|
||||
* widens nomem into io.error inline rather than `memio.
|
||||
* fixed_vstream(buf)?` for the same reason memio.vstream.ww
|
||||
* does at line 87-99.
|
||||
* - eFinal (#50): collapses V + OLD surfaces; the `v` names
|
||||
* rename off and the OLD fprintln / fprintfln / bsprintf /
|
||||
* asprintf delete.
|
||||
*
|
||||
* BOOTSTRAP-EMBED CHECK: fmt is NOT embedded in any selfhost
|
||||
* combined.ww (grep verified pre-impl). memio.vstream.ww IS embedded
|
||||
* in w6c + wwdump combined.ww — the fold-e7 memio helpers ride along
|
||||
* via the regen committed in this same commit (per #110 SSoT). 990/
|
||||
* 994/995 byte-id gates + combined_ww_fresh stay green by virtue of
|
||||
* the regen.
|
||||
*
|
||||
* GATE POLARITY: must stay GREEN. A red here means vfprintln /
|
||||
* vfprintfln dropped the newline byte, vbsprintf returned a stale
|
||||
* memio.fixed_string view (intrusive cast broke), vasprintf failed
|
||||
* to shrink-copy / close cleanly, or the underlying vfprint /
|
||||
* vfprintf primitives regressed.
|
||||
*/
|
||||
#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[] = {
|
||||
{ "fdprintln_v_run_basic",
|
||||
"package main;\n"
|
||||
"import os;\n"
|
||||
"import fmt;\n"
|
||||
"import io;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let path: str = \"/tmp/wwfmtv_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.fdprintln_v(fd, 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, 0 },
|
||||
{ "fdprintfln_v_run_fmt",
|
||||
"package main;\n"
|
||||
"import os;\n"
|
||||
"import fmt;\n"
|
||||
"import io;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let path: str = \"/tmp/wwfmtv_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.fdprintfln_v(fd, \"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, 0 },
|
||||
{ "bsprintf_v_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.vbsprintf(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, 0 },
|
||||
{ "asprintf_v_basic",
|
||||
"package main;\n"
|
||||
"import os;\n"
|
||||
"import fmt;\n"
|
||||
"import io;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let view: str = fmt.vasprintf(\"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, 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 (task #15), so each row's build leaves <src>.{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/fvc_%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_vstream_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_vstream_compositions_run[ww][%s]: exit=%d want=%d\n",
|
||||
rows[i].label, got, rows[i].want_exit);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!wwpresent)
|
||||
fprintf(stderr, "fmt_vstream_compositions_run: skip wwstage (no %s)\n", wdrv);
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "fmt_vstream_compositions_run: %d/%d fixtures failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("fmt_vstream_compositions_run: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user