lib: collapse the parallel vstream scaffold onto the single Hare io surface (#94 fold-eFinal)
The Option-C parallel _v vstream API was scaffolding to bring the io stack up alongside the old surface; carrying both permanently is a rule-9 divergence from ref/hare, which has exactly one io surface. Collapse onto that surface (stream = *vtable, ref/hare/io/stream.ha) and rename the _v symbols to their Hare names (io vstream->stream, fmt vfprint->fprint, bufio/memio/log surfaces, log.new). Deletes the 4 lib/*/vstream.ww scaffold files; regenerates w6c/wwdump combined.ww. cstage and wwstage stay byte-identical and combined_ww_fresh holds; all 220 tests pass.
This commit is contained in:
240
lib/log/log.ww
240
lib/log/log.ww
@@ -1,11 +1,12 @@
|
||||
// log — process logger over a [[io.stream]] sink. Subset of Hare's
|
||||
// lib/log (ref/hare/log/{logger,funcs,global,silent}.ha).
|
||||
// log — process logger over an [[io.stream]] (= `*io.vtable`) sink.
|
||||
// Subset of Hare's lib/log (ref/hare/log/{logger,funcs,global,silent}.ha).
|
||||
// Project #94 fold-eFinal.
|
||||
//
|
||||
// Surface today:
|
||||
// Surface:
|
||||
//
|
||||
// log.logger — vtable with `println` + `printfln` slots
|
||||
// log.stdlogger — first-field embed of logger + a `*io.stream` sink
|
||||
// log.new (sl: *stdlogger, sink: *io.stream) void
|
||||
// log.stdlogger — first-field embed of logger + an io.stream sink
|
||||
// log.new (sink: io.stream) stdlogger
|
||||
// log.silent *logger — a logger that discards every record
|
||||
// log.default *logger — a stdlogger writing to stderr
|
||||
// log.global *logger — the dispatch target for [[println]] / [[fatal]]
|
||||
@@ -19,57 +20,43 @@
|
||||
// log.lfatalf (log: *logger, format: str, fields: fmt.field...) never
|
||||
// log.setlogger (log: *logger) void
|
||||
//
|
||||
// VALUE-RETURN (Hare ref/hare/log/logger.ha:20): [[new]] builds a
|
||||
// stdlogger in a local and `return r;` (proven field-by-field sret).
|
||||
// stdlogger is 24B (two fn-ptrs + sink), returned via the SysV memory
|
||||
// class; the caller owns the returned logger (stack ownership, no-GC)
|
||||
// and passes `&sl.logger` to the dispatch fns.
|
||||
//
|
||||
// Divergences from Hare:
|
||||
//
|
||||
// • Hare returns the stdlogger from `new` by value; ww cgen can't
|
||||
// return wide structs, so [[new]] is out-parameter shaped. Same
|
||||
// workaround as memio.fixed / bufio.init.
|
||||
// • silent / default / _default initialised via a lazy [[ensureinit]]
|
||||
// (lib/temp's `rnginit==0` pattern) rather than module-scope struct-
|
||||
// literal lets: cstage `emit_lets` only constexprs primitive /
|
||||
// str-literal / array-literal lets. The public *logger globals are
|
||||
// zero initially and become non-nil after the first lib/log call;
|
||||
// callers that read globals directly must call some lib/log fn first
|
||||
// so init runs. Graduates if static-init lands.
|
||||
//
|
||||
// • Hare initialises silent / default / _default via module-scope
|
||||
// struct literal lets. cstage `emit_lets` (cmd/w6c/cgen.c:6176)
|
||||
// only constexprs primitive / str-literal / array-literal lets —
|
||||
// struct- and pointer-literal init aren't reachable. ww drops to
|
||||
// a lazy [[ensureinit]] (lib/temp's `rnginit==0` pattern) called
|
||||
// from every exported fn. The public *logger globals are zero
|
||||
// initially and become non-nil after the first lib/log call;
|
||||
// callers that read globals directly (rather than going through
|
||||
// [[println]] / [[lprintln]] / [[setlogger]] / etc.) must call
|
||||
// some lib/log fn first so init runs.
|
||||
// • Param name divergence: the format-string parameter is `format`
|
||||
// (Hare's is `fmt`). Permanent — #19 refuses any let/param shadowing
|
||||
// an imported module name; `use fmt; fn x(fmt: T)` is structurally
|
||||
// ambiguous under ww's `.`-for-both rule.
|
||||
//
|
||||
// • Param name divergence from Hare: the format-string parameter is
|
||||
// `format` (Hare's is `fmt`). Permanent — #19 refuses any
|
||||
// let/param that shadows an imported module name, regardless of
|
||||
// body usage. ww chose `.` for both module-access and field-
|
||||
// access, so `use fmt; fn x(fmt: T)` is structurally ambiguous;
|
||||
// the resolver refuses it at decl.
|
||||
// Sink today is an [[io.stream]] only — lib/io has no fd-backed handle
|
||||
// yet (Hare's `io::handle = file | int`, io fold-2 #5). The default
|
||||
// logger writes to stderr via a private fd_ctx whose write callback
|
||||
// forwards to [[os.write]] on fd 2.
|
||||
//
|
||||
// Sink today is [[io.stream]] only — lib/io has no fd-backed stream
|
||||
// yet (Hare's `io::handle = file | int` collapses to one variant).
|
||||
// The default logger writes to stderr via a private io.stream whose
|
||||
// write callback forwards to [[os.write]] on fd 2.
|
||||
// Cast workaround per #206-payoff (ken: KEEP the explicit casts; they
|
||||
// are cgen-neutral and sidestep the #214 over-acceptance surface). The
|
||||
// `(&fn_name): *io.<role>` cast at the stderrsink_ctx_g vt wire-up; the
|
||||
// #206 cast-drop is gated on #214. stderrwrite constructs nomem and
|
||||
// widens to io.error explicitly rather than `os.trywrite(...)?` for the
|
||||
// no-handle interim.
|
||||
//
|
||||
// Compiler note: wwstage cgen's variadic dispatch is name-based
|
||||
// (`fnparamslookup(callee.str)`), which would conflate the
|
||||
// `logger.println` vtable field with the top-level [[println]] fn
|
||||
// if both stages compiled this module. cstage uses type-driven
|
||||
// dispatch via the call-node's lhs type and handles fn-pointer
|
||||
// variadic calls correctly. Today lib/log is consumed only by tests
|
||||
// routed through `ww run` (cstage), so this is a latent issue
|
||||
// covered by #11 (wwstage checkfile pass).
|
||||
//
|
||||
// let buf: [128]u8;
|
||||
// let mem: memio.state;
|
||||
// let s: io.stream;
|
||||
// memio.fixed(&mem, &s, buf[0:128]);
|
||||
//
|
||||
// let sl: log.stdlogger;
|
||||
// log.new(&sl, &s);
|
||||
// let sl: log.stdlogger = log.new(&s.vt);
|
||||
// log.lprintln(&sl.logger, "hello", 42i64);
|
||||
// // mem now holds "hello 42\n"
|
||||
//
|
||||
// log.setlogger(&sl.logger);
|
||||
// log.println("through global");
|
||||
//
|
||||
// log.setlogger(log.silent);
|
||||
// log.println("dropped");
|
||||
|
||||
@@ -87,53 +74,55 @@ export type logger = struct {
|
||||
printfln: fn(l: *logger, format: str, fields: fmt.field...) void,
|
||||
};
|
||||
|
||||
// stdlogger — concrete logger forwarding to a `*io.stream` sink.
|
||||
// First-field embed: `&sl.logger` yields a `*logger` (same shape as
|
||||
// bufio.stream over its embedded io.stream vtable). The vtable
|
||||
// callback recovers the outer stdlogger by casting the dispatch arg
|
||||
// back to `*stdlogger`.
|
||||
// stdlogger — concrete logger forwarding to an io.stream sink.
|
||||
// First-field embed: `&sl.logger` yields a `*logger` (intrusive
|
||||
// outer→inner cast); the vtable callback recovers the outer via
|
||||
// `*stdlogger` cast. Mirrors ref/hare/log/logger.ha:18.
|
||||
export type stdlogger = struct {
|
||||
logger: logger,
|
||||
sink: *io.stream,
|
||||
sink: io.stream,
|
||||
};
|
||||
|
||||
// stderrsink — private io.stream wired through fd 2 via [[os.write]].
|
||||
// Used as [[default]]'s sink. Independent of lib/io's future fd-backed
|
||||
// stream — when that lands, this collapses to a thin wrapper and
|
||||
// graduates in one go.
|
||||
let stderrsink: io.stream;
|
||||
// stderrsink_ctx — module-static state for the default logger's stderr
|
||||
// sink. vt FIRST field for the intrusive io.stream→*stderrsink_ctx cast
|
||||
// in stderrwrite. Mirrors fmt.fd_ctx; only scalars/ptrs beyond vt.
|
||||
type stderrsink_ctx = struct {
|
||||
vt: io.vtable,
|
||||
fd: i32,
|
||||
};
|
||||
|
||||
// _silent — backing storage for the [[silent]] global.
|
||||
let _silent: logger;
|
||||
// Zero-init at link time per #129 A.2/A.3 SSoT; ensureinit wires
|
||||
// vt.reader / vt.writer + fd lazily on first dispatch.
|
||||
let stderrsink_ctx_g: stderrsink_ctx;
|
||||
|
||||
// _default — backing storage for the [[default]] global. Its sink
|
||||
// points at [[stderrsink]] after [[ensureinit]] runs.
|
||||
// _silent / _default — backing storage for the silent / default
|
||||
// *logger globals. Zero-init at link time; ensureinit fills the
|
||||
// println / printfln slots.
|
||||
let _silent: logger;
|
||||
let _default: stdlogger;
|
||||
|
||||
// silent / default / global — Hare-style *logger globals. Zero-init
|
||||
// at link time; lazily wired by [[ensureinit]] on the first call to
|
||||
// any exported fn. See the file header for the divergence rationale.
|
||||
// silent / default / global — Hare-style *logger globals. Zero-init at
|
||||
// link time; lazily wired by [[ensureinit]] on the first call to any
|
||||
// exported fn. See the file header for the divergence rationale.
|
||||
export let silent: *logger;
|
||||
export let default: *logger;
|
||||
export let global: *logger;
|
||||
|
||||
// initdone — guards [[ensureinit]] so the one-shot wiring runs once.
|
||||
// Mirrors lib/temp's `rnginit` pattern.
|
||||
let initdone: i32 = 0;
|
||||
|
||||
fn ensureinit() void = {
|
||||
if (initdone != 0) { return; };
|
||||
stderrsink.ctx = nil;
|
||||
stderrsink.read = stderrread;
|
||||
stderrsink.write = stderrwrite;
|
||||
stderrsink.close = stderrclose;
|
||||
stderrsink_ctx_g.fd = 2;
|
||||
stderrsink_ctx_g.vt.reader = (&stderrread): *io.reader;
|
||||
stderrsink_ctx_g.vt.writer = (&stderrwrite): *io.writer;
|
||||
|
||||
_silent.println = silentprintln;
|
||||
_silent.printfln = silentprintfln;
|
||||
|
||||
_default.logger.println = stdprintln;
|
||||
_default.logger.printfln = stdprintfln;
|
||||
_default.sink = &stderrsink;
|
||||
_default.sink = &stderrsink_ctx_g.vt;
|
||||
|
||||
silent = &_silent;
|
||||
default = &_default.logger;
|
||||
@@ -141,120 +130,127 @@ fn ensureinit() void = {
|
||||
initdone = 1;
|
||||
};
|
||||
|
||||
// stderrread / stderrwrite / stderrclose — io.stream callbacks for
|
||||
// the private stderr sink. Read is a no-op returning io.eof; close
|
||||
// is a no-op (the process owns fd 2). Write forwards to write(2)
|
||||
// and translates a negative-errno into io.closed.
|
||||
fn stderrread(s: *io.stream, buf: []u8) (i32 | io.eof | io.closed) = {
|
||||
// stderrread / stderrwrite — vtable callbacks for the private stderr
|
||||
// sink. Read is eof-only — lib/log writes; never reads. -errno collapses
|
||||
// to a nomem-widened io.error.
|
||||
fn stderrread(s: io.stream, buf: []u8) (size | io.eof | io.error) = {
|
||||
let e: io.eof;
|
||||
return e;
|
||||
};
|
||||
|
||||
fn stderrwrite(s: *io.stream, buf: []u8) (i32 | io.closed) = {
|
||||
let r: i64 = os.write(2, buf.ptr, buf.len: u64);
|
||||
fn stderrwrite(s: io.stream, buf: []u8) (size | io.error) = {
|
||||
let c: *stderrsink_ctx = s: *stderrsink_ctx;
|
||||
let r: i64 = os.write(c.fd, buf.ptr, buf.len: u64);
|
||||
if (r < 0) {
|
||||
let e: io.closed;
|
||||
let nm: nomem;
|
||||
let e: io.error = nm;
|
||||
return e;
|
||||
};
|
||||
return r: i32;
|
||||
return r: size;
|
||||
};
|
||||
|
||||
fn stderrclose(s: *io.stream) (void | io.closed) = { return; };
|
||||
|
||||
// stdprintln — vtable callback for stdlogger. Forwards to
|
||||
// [[fmt.fprintln]] on the sink; the (i32 | io.closed) result is
|
||||
// dropped — lib/log's surface is `void` (matches Hare).
|
||||
// stdprintln / stdprintfln — vtable callbacks for stdlogger. Forwards
|
||||
// to fmt.fprint / fprintf on the sink + appends the trailing newline
|
||||
// via io.write. Hare's fmt.fprintln emits the newline inside one
|
||||
// wrapper (ref/hare/fmt/wrappers.ha:60-68); we inline that split at the
|
||||
// log site because fmt.fprint is the bare (no-newline) primitive. The
|
||||
// (size | io.error) results are dropped — lib/log's surface is `void`.
|
||||
fn stdprintln(l: *logger, args: fmt.formattable...) void = {
|
||||
let sl: *stdlogger = l: *stdlogger;
|
||||
fmt.fprintln(sl.sink, args...);
|
||||
fmt.fprint(sl.sink, args...);
|
||||
let nl: [1]u8;
|
||||
nl[0] = 10u8;
|
||||
let v: []u8 = nl[0:1];
|
||||
io.write(sl.sink, v);
|
||||
};
|
||||
|
||||
// stdprintfln — vtable callback for stdlogger's format-string slot.
|
||||
// Mirrors ref/hare/log/logger.ha:38 log_printfln. `format` (not
|
||||
// Hare's `fmt`) per the param-name divergence noted in the header.
|
||||
fn stdprintfln(l: *logger, format: str, fields: fmt.field...) void = {
|
||||
let sl: *stdlogger = l: *stdlogger;
|
||||
fmt.fprintfln(sl.sink, format, fields...);
|
||||
fmt.fprintf(sl.sink, format, fields...);
|
||||
let nl: [1]u8;
|
||||
nl[0] = 10u8;
|
||||
let v: []u8 = nl[0:1];
|
||||
io.write(sl.sink, v);
|
||||
};
|
||||
|
||||
// silentprintln — vtable callback for the silent logger. Discards
|
||||
// every record without dispatching through fmt; keeps silent truly
|
||||
// silent if fmt ever gets stateful.
|
||||
// silentprintln / silentprintfln — vtable callbacks for the silent
|
||||
// logger. Discard every record without dispatching through fmt; keeps
|
||||
// silent truly silent if fmt ever gets stateful. Mirror
|
||||
// ref/hare/log/silent.ha:15.
|
||||
fn silentprintln(l: *logger, args: fmt.formattable...) void = { };
|
||||
|
||||
// silentprintfln — format-string sibling of [[silentprintln]].
|
||||
// Mirrors ref/hare/log/silent.ha:15.
|
||||
fn silentprintfln(l: *logger, format: str, fields: fmt.field...) void = { };
|
||||
|
||||
// new — wire `sl` as a stdlogger over `sink`. Hare returns by value
|
||||
// (ref/hare/log/logger.ha:20); ww cgen can't return wide structs,
|
||||
// so we take an out-parameter pointer (same shape as memio.fixed,
|
||||
// bufio.init).
|
||||
export fn new(sl: *stdlogger, sink: *io.stream) void = {
|
||||
// ---- public API ----------------------------------------------------------
|
||||
|
||||
// new — build a stdlogger over `sink`, returned BY VALUE. Mirrors
|
||||
// ref/hare/log/logger.ha:20.
|
||||
export fn new(sink: io.stream) stdlogger = {
|
||||
ensureinit();
|
||||
sl.logger.println = stdprintln;
|
||||
sl.logger.printfln = stdprintfln;
|
||||
sl.sink = sink;
|
||||
let r: stdlogger;
|
||||
r.logger.println = stdprintln;
|
||||
r.logger.printfln = stdprintfln;
|
||||
r.sink = sink;
|
||||
return r;
|
||||
};
|
||||
|
||||
// lprintln — dispatch `args` through `log`. Hare's lib/log/funcs.ha
|
||||
// counterpart at line 8.
|
||||
// lprintln — dispatch `args` through `log`. Mirrors ref/hare/log/funcs.ha:8.
|
||||
export fn lprintln(log: *logger, args: fmt.formattable...) void = {
|
||||
ensureinit();
|
||||
log.println(log, args...);
|
||||
};
|
||||
|
||||
// println — dispatch through the [[global]] logger.
|
||||
// println — dispatch through the [[global]] logger. Mirrors
|
||||
// ref/hare/log/funcs.ha:18.
|
||||
export fn println(args: fmt.formattable...) void = {
|
||||
ensureinit();
|
||||
lprintln(global, args...);
|
||||
};
|
||||
|
||||
// lfatal — lprintln to `log` then exit(255). `never` return marks
|
||||
// the bottom type so flow-control checks treat callers as terminated.
|
||||
// Hare's lib/log/funcs.ha counterpart at line 28.
|
||||
// lfatal — lprintln to `log` then exit(255). `never` return marks the
|
||||
// bottom type so flow-control checks treat callers as terminated.
|
||||
// Mirrors ref/hare/log/funcs.ha:28.
|
||||
export fn lfatal(log: *logger, args: fmt.formattable...) never = {
|
||||
lprintln(log, args...);
|
||||
os.exit(255);
|
||||
};
|
||||
|
||||
// fatal — lprintln to [[global]] then exit(255). Hare's lib/log/
|
||||
// funcs.ha counterpart at line 45.
|
||||
// fatal — lprintln to [[global]] then exit(255). Mirrors
|
||||
// ref/hare/log/funcs.ha:45.
|
||||
export fn fatal(args: fmt.formattable...) never = {
|
||||
println(args...);
|
||||
os.exit(255);
|
||||
};
|
||||
|
||||
// setlogger — install `log` as the [[global]] logger. Hare's
|
||||
// lib/log/global.ha counterpart at line 20.
|
||||
// setlogger — install `log` as the [[global]] logger. Mirrors
|
||||
// ref/hare/log/global.ha:20.
|
||||
export fn setlogger(log: *logger) void = {
|
||||
ensureinit();
|
||||
global = log;
|
||||
};
|
||||
|
||||
// lprintfln — dispatch a format-string record through `log`. Hare's
|
||||
// lib/log/funcs.ha counterpart at line 13.
|
||||
// lprintfln — dispatch a format-string record through `log`. Mirrors
|
||||
// ref/hare/log/funcs.ha:13.
|
||||
export fn lprintfln(log: *logger, format: str, fields: fmt.field...) void = {
|
||||
ensureinit();
|
||||
log.printfln(log, format, fields...);
|
||||
};
|
||||
|
||||
// printfln — dispatch through the [[global]] logger. Hare's
|
||||
// lib/log/funcs.ha counterpart at line 23.
|
||||
// printfln — dispatch through the [[global]] logger. Mirrors
|
||||
// ref/hare/log/funcs.ha:23.
|
||||
export fn printfln(format: str, fields: fmt.field...) void = {
|
||||
ensureinit();
|
||||
lprintfln(global, format, fields...);
|
||||
};
|
||||
|
||||
// lfatalf — lprintfln to `log` then exit(255). Hare's
|
||||
// lib/log/funcs.ha counterpart at line 35.
|
||||
// lfatalf — lprintfln to `log` then exit(255). Mirrors
|
||||
// ref/hare/log/funcs.ha:35.
|
||||
export fn lfatalf(log: *logger, format: str, fields: fmt.field...) never = {
|
||||
lprintfln(log, format, fields...);
|
||||
os.exit(255);
|
||||
};
|
||||
|
||||
// fatalf — lprintfln to [[global]] then exit(255). Hare's
|
||||
// lib/log/funcs.ha counterpart at line 52.
|
||||
// fatalf — lprintfln to [[global]] then exit(255). Mirrors
|
||||
// ref/hare/log/funcs.ha:52.
|
||||
export fn fatalf(format: str, fields: fmt.field...) never = {
|
||||
printfln(format, fields...);
|
||||
os.exit(255);
|
||||
|
||||
Reference in New Issue
Block a user