lib: io.handle union + seeker + handle-typed read/write/close/seek dispatch (#5)
Ports ref/hare/io handle.ha: a handle is (file | *stream); ww stream is already *vtable (#94 collapse) so the payload is (file | stream). file=i32 (Hare int is 32-bit, ww int is 8B word -- width-faithful, USER-ruled). read/write/close/seek/tell match on the handle: file-arm to os.read/write/close/lseek (os plays Hare sys role), stream-arm to the unchanged st_* vtable bodies; seeker is the 4th vtable slot (copier deferred). On a file-arm syscall error the stub returns errors.unsupported with a #199b marker -- faithful errno to io.error needs io.error to spread ...errors.error (#204/#199b-blocked); only the error value is lossy until then, the type stays faithful. Regenerates w6c/wwdump combined.ww.
This commit is contained in:
@@ -14250,23 +14250,31 @@ export type underread = !i32;
|
||||
|
||||
package io;
|
||||
|
||||
// Q2 layering (ratified): the file-arm of the handle dispatchers routes
|
||||
// to os.{read,write,close,lseek}; ww `os` plays Hare's `sys` role, so
|
||||
// `lib/io import os` is the correct direction (os is the import floor).
|
||||
import os;
|
||||
|
||||
// ref/hare/io/stream.ha:36-42. Nullable spelling per #192 (ww parser
|
||||
// rejects `nullable *T`); each slot is `(*T | void)`.
|
||||
// rejects `nullable *T`); each slot is `(*T | void)`. The `seeker` slot
|
||||
// (4th) lands with io fold-2 (#5); `copier` stays deferred.
|
||||
export type vtable = struct {
|
||||
reader: (*reader | void),
|
||||
writer: (*writer | void),
|
||||
closer: (*closer | void),
|
||||
seeker: (*seeker | void),
|
||||
};
|
||||
|
||||
// ref/hare/io/stream.ha:33.
|
||||
export type stream = *vtable;
|
||||
|
||||
// ref/hare/io/stream.ha:44-50. The void-arm `return e;` chains two
|
||||
// ref/hare/io/stream.ha:44-51. Private stream-arm body behind the
|
||||
// handle-typed [[read]] (#5). The void-arm `return e;` chains two
|
||||
// direct widens: `errors.unsupported → error` via #199 α, then
|
||||
// `error → (size | eof | error)` via #205. The call-arm spells the
|
||||
// fn-ptr call explicitly per #193 (ww requires `(*r)(args)` rather
|
||||
// than implicit `r(args)`).
|
||||
export fn read(s: stream, buf: []u8) (size | eof | error) = {
|
||||
fn st_read(s: stream, buf: []u8) (size | eof | error) = {
|
||||
match (s.reader) {
|
||||
case void => {
|
||||
let u: errors.unsupported;
|
||||
@@ -14277,8 +14285,8 @@ export fn read(s: stream, buf: []u8) (size | eof | error) = {
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/stream.ha:53-59.
|
||||
export fn write(s: stream, buf: []u8) (size | error) = {
|
||||
// ref/hare/io/stream.ha:53-60.
|
||||
fn st_write(s: stream, buf: []u8) (size | error) = {
|
||||
match (s.writer) {
|
||||
case void => {
|
||||
let u: errors.unsupported;
|
||||
@@ -14293,13 +14301,108 @@ export fn write(s: stream, buf: []u8) (size | error) = {
|
||||
// `c(s)?;` then falls through to the void arm; ww collapses to a
|
||||
// direct return for the void-arm fall-through shape. The surface
|
||||
// (void | error) matches.
|
||||
export fn close(s: stream) (void | error) = {
|
||||
fn st_close(s: stream) (void | error) = {
|
||||
match (s.closer) {
|
||||
case void => return void;
|
||||
case let c: *closer => return (*c)(s);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/stream.ha:70-77. Clone of st_read's slot-dispatch shape
|
||||
// over the new `seeker` vtable slot.
|
||||
fn st_seek(s: stream, off: off, w: whence) (off | error) = {
|
||||
match (s.seeker) {
|
||||
case void => {
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
case let sk: *seeker => return (*sk)(s, off, w);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:16-23. The handle-typed public read: the
|
||||
// file-arm routes to os.read (Q2 layering), the stream-arm delegates to
|
||||
// the unchanged st_read vtable dispatch above.
|
||||
export fn read(h: handle, buf: []u8) (size | eof | error) = {
|
||||
match (h) {
|
||||
case let fd: file => {
|
||||
let r: i64 = os.read(fd, buf.ptr, buf.len: u64);
|
||||
if (r < 0) {
|
||||
// #199b: faithful errno→io.error needs the ...errors.error spread; see lib/io/types.ww:40
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
if (r == 0) {
|
||||
let z: eof;
|
||||
return z;
|
||||
};
|
||||
return r: size;
|
||||
};
|
||||
case let s: stream => return st_read(s, buf);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:27-36.
|
||||
export fn write(h: handle, buf: []u8) (size | error) = {
|
||||
match (h) {
|
||||
case let fd: file => {
|
||||
let r: i64 = os.write(fd, buf.ptr, buf.len: u64);
|
||||
if (r < 0) {
|
||||
// #199b: faithful errno→io.error needs the ...errors.error spread; see lib/io/types.ww:40
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
return r: size;
|
||||
};
|
||||
case let s: stream => return st_write(s, buf);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:44-51. Hare propagates each arm's error via `?`
|
||||
// then falls through; ww returns directly (matching st_close's shape).
|
||||
export fn close(h: handle) (void | error) = {
|
||||
match (h) {
|
||||
case let fd: file => {
|
||||
let r: i32 = os.close(fd);
|
||||
if (r < 0) {
|
||||
// #199b: faithful errno→io.error needs the ...errors.error spread; see lib/io/types.ww:40
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
return void;
|
||||
};
|
||||
case let s: stream => return st_close(s);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:55-62. The io.whence → os.whence cast bridges
|
||||
// the two enum copies (same SET/CUR/END values); os.lseek owns the
|
||||
// syscall.
|
||||
export fn seek(h: handle, off: off, w: whence) (off | error) = {
|
||||
match (h) {
|
||||
case let fd: file => {
|
||||
let r: i64 = os.lseek(fd, off, w: os.whence);
|
||||
if (r < 0) {
|
||||
// #199b: faithful errno→io.error needs the ...errors.error spread; see lib/io/types.ww:40
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
return r: off;
|
||||
};
|
||||
case let s: stream => return st_seek(s, off, w);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:65-67.
|
||||
export fn tell(h: handle) (off | error) = {
|
||||
return seek(h, 0, whence.CUR);
|
||||
};
|
||||
|
||||
// errors — domain-agnostic error types. Mirrors ref/hare/errors/.
|
||||
//
|
||||
// Named-void tagged-union variants, so `(T | errors.invalid | ...)`
|
||||
@@ -14475,6 +14578,21 @@ export type whence = enum i32 {
|
||||
END = 2,
|
||||
};
|
||||
|
||||
// ref/hare/io/+linux/platform_file.ha:13. Hare's `file = int`; on
|
||||
// linux/x86_64 Hare's `int` is 32-bit, but ww's `int` is an 8B machine
|
||||
// word, so a literal `int` here would be width-wrong for a fd. i32 is
|
||||
// width-faithful to Hare's real fd width (USER-ruled 2026-05-31).
|
||||
export type file = i32;
|
||||
|
||||
// ref/hare/io/arch+x86_64.ha:6. ABI-compatible with POSIX off_t.
|
||||
export type off = i64;
|
||||
|
||||
// ref/hare/io/handle.ha:12. Hare spells it `(file | *stream)`; ww's
|
||||
// `stream` is already `*vtable` (the #94 collapse absorbed Hare's
|
||||
// `*stream` indirection), so the faithful ww payload is `(file |
|
||||
// stream)`, NOT a literal `*stream` which would be a double-pointer.
|
||||
export type handle = (file | stream);
|
||||
|
||||
// ref/hare/io/types.ha:11 spreads `...errors::error` into the union;
|
||||
// ww enumerates the tags explicitly (ken's #204-block — spread of an
|
||||
// errors-side tagged into this union triggers a wrapper-vs-flatten
|
||||
@@ -14497,6 +14615,11 @@ export type writer = fn(s: stream, buf: []u8) (size | error);
|
||||
// ref/hare/io/types.ha:55.
|
||||
export type closer = fn(s: stream) (void | error);
|
||||
|
||||
// ref/hare/io/types.ha:76. Hare's `fn(s: *stream, off: off, w: whence)`;
|
||||
// ww's `stream` is already `*vtable` (the #94 collapse), so the param is
|
||||
// `stream` directly, mirroring the reader/writer/closer aliases above.
|
||||
export type seeker = fn(s: stream, off: off, w: whence) (off | error);
|
||||
|
||||
// memio — in-memory io stream. Project #94 fold-eFinal.
|
||||
//
|
||||
// Hare's memio:: surface, drop underscores. Two flavours behind a
|
||||
|
||||
@@ -14250,23 +14250,31 @@ export type underread = !i32;
|
||||
|
||||
package io;
|
||||
|
||||
// Q2 layering (ratified): the file-arm of the handle dispatchers routes
|
||||
// to os.{read,write,close,lseek}; ww `os` plays Hare's `sys` role, so
|
||||
// `lib/io import os` is the correct direction (os is the import floor).
|
||||
import os;
|
||||
|
||||
// ref/hare/io/stream.ha:36-42. Nullable spelling per #192 (ww parser
|
||||
// rejects `nullable *T`); each slot is `(*T | void)`.
|
||||
// rejects `nullable *T`); each slot is `(*T | void)`. The `seeker` slot
|
||||
// (4th) lands with io fold-2 (#5); `copier` stays deferred.
|
||||
export type vtable = struct {
|
||||
reader: (*reader | void),
|
||||
writer: (*writer | void),
|
||||
closer: (*closer | void),
|
||||
seeker: (*seeker | void),
|
||||
};
|
||||
|
||||
// ref/hare/io/stream.ha:33.
|
||||
export type stream = *vtable;
|
||||
|
||||
// ref/hare/io/stream.ha:44-50. The void-arm `return e;` chains two
|
||||
// ref/hare/io/stream.ha:44-51. Private stream-arm body behind the
|
||||
// handle-typed [[read]] (#5). The void-arm `return e;` chains two
|
||||
// direct widens: `errors.unsupported → error` via #199 α, then
|
||||
// `error → (size | eof | error)` via #205. The call-arm spells the
|
||||
// fn-ptr call explicitly per #193 (ww requires `(*r)(args)` rather
|
||||
// than implicit `r(args)`).
|
||||
export fn read(s: stream, buf: []u8) (size | eof | error) = {
|
||||
fn st_read(s: stream, buf: []u8) (size | eof | error) = {
|
||||
match (s.reader) {
|
||||
case void => {
|
||||
let u: errors.unsupported;
|
||||
@@ -14277,8 +14285,8 @@ export fn read(s: stream, buf: []u8) (size | eof | error) = {
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/stream.ha:53-59.
|
||||
export fn write(s: stream, buf: []u8) (size | error) = {
|
||||
// ref/hare/io/stream.ha:53-60.
|
||||
fn st_write(s: stream, buf: []u8) (size | error) = {
|
||||
match (s.writer) {
|
||||
case void => {
|
||||
let u: errors.unsupported;
|
||||
@@ -14293,13 +14301,108 @@ export fn write(s: stream, buf: []u8) (size | error) = {
|
||||
// `c(s)?;` then falls through to the void arm; ww collapses to a
|
||||
// direct return for the void-arm fall-through shape. The surface
|
||||
// (void | error) matches.
|
||||
export fn close(s: stream) (void | error) = {
|
||||
fn st_close(s: stream) (void | error) = {
|
||||
match (s.closer) {
|
||||
case void => return void;
|
||||
case let c: *closer => return (*c)(s);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/stream.ha:70-77. Clone of st_read's slot-dispatch shape
|
||||
// over the new `seeker` vtable slot.
|
||||
fn st_seek(s: stream, off: off, w: whence) (off | error) = {
|
||||
match (s.seeker) {
|
||||
case void => {
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
case let sk: *seeker => return (*sk)(s, off, w);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:16-23. The handle-typed public read: the
|
||||
// file-arm routes to os.read (Q2 layering), the stream-arm delegates to
|
||||
// the unchanged st_read vtable dispatch above.
|
||||
export fn read(h: handle, buf: []u8) (size | eof | error) = {
|
||||
match (h) {
|
||||
case let fd: file => {
|
||||
let r: i64 = os.read(fd, buf.ptr, buf.len: u64);
|
||||
if (r < 0) {
|
||||
// #199b: faithful errno→io.error needs the ...errors.error spread; see lib/io/types.ww:40
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
if (r == 0) {
|
||||
let z: eof;
|
||||
return z;
|
||||
};
|
||||
return r: size;
|
||||
};
|
||||
case let s: stream => return st_read(s, buf);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:27-36.
|
||||
export fn write(h: handle, buf: []u8) (size | error) = {
|
||||
match (h) {
|
||||
case let fd: file => {
|
||||
let r: i64 = os.write(fd, buf.ptr, buf.len: u64);
|
||||
if (r < 0) {
|
||||
// #199b: faithful errno→io.error needs the ...errors.error spread; see lib/io/types.ww:40
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
return r: size;
|
||||
};
|
||||
case let s: stream => return st_write(s, buf);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:44-51. Hare propagates each arm's error via `?`
|
||||
// then falls through; ww returns directly (matching st_close's shape).
|
||||
export fn close(h: handle) (void | error) = {
|
||||
match (h) {
|
||||
case let fd: file => {
|
||||
let r: i32 = os.close(fd);
|
||||
if (r < 0) {
|
||||
// #199b: faithful errno→io.error needs the ...errors.error spread; see lib/io/types.ww:40
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
return void;
|
||||
};
|
||||
case let s: stream => return st_close(s);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:55-62. The io.whence → os.whence cast bridges
|
||||
// the two enum copies (same SET/CUR/END values); os.lseek owns the
|
||||
// syscall.
|
||||
export fn seek(h: handle, off: off, w: whence) (off | error) = {
|
||||
match (h) {
|
||||
case let fd: file => {
|
||||
let r: i64 = os.lseek(fd, off, w: os.whence);
|
||||
if (r < 0) {
|
||||
// #199b: faithful errno→io.error needs the ...errors.error spread; see lib/io/types.ww:40
|
||||
let u: errors.unsupported;
|
||||
let e: error = u;
|
||||
return e;
|
||||
};
|
||||
return r: off;
|
||||
};
|
||||
case let s: stream => return st_seek(s, off, w);
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/io/handle.ha:65-67.
|
||||
export fn tell(h: handle) (off | error) = {
|
||||
return seek(h, 0, whence.CUR);
|
||||
};
|
||||
|
||||
// errors — domain-agnostic error types. Mirrors ref/hare/errors/.
|
||||
//
|
||||
// Named-void tagged-union variants, so `(T | errors.invalid | ...)`
|
||||
@@ -14475,6 +14578,21 @@ export type whence = enum i32 {
|
||||
END = 2,
|
||||
};
|
||||
|
||||
// ref/hare/io/+linux/platform_file.ha:13. Hare's `file = int`; on
|
||||
// linux/x86_64 Hare's `int` is 32-bit, but ww's `int` is an 8B machine
|
||||
// word, so a literal `int` here would be width-wrong for a fd. i32 is
|
||||
// width-faithful to Hare's real fd width (USER-ruled 2026-05-31).
|
||||
export type file = i32;
|
||||
|
||||
// ref/hare/io/arch+x86_64.ha:6. ABI-compatible with POSIX off_t.
|
||||
export type off = i64;
|
||||
|
||||
// ref/hare/io/handle.ha:12. Hare spells it `(file | *stream)`; ww's
|
||||
// `stream` is already `*vtable` (the #94 collapse absorbed Hare's
|
||||
// `*stream` indirection), so the faithful ww payload is `(file |
|
||||
// stream)`, NOT a literal `*stream` which would be a double-pointer.
|
||||
export type handle = (file | stream);
|
||||
|
||||
// ref/hare/io/types.ha:11 spreads `...errors::error` into the union;
|
||||
// ww enumerates the tags explicitly (ken's #204-block — spread of an
|
||||
// errors-side tagged into this union triggers a wrapper-vs-flatten
|
||||
@@ -14497,6 +14615,11 @@ export type writer = fn(s: stream, buf: []u8) (size | error);
|
||||
// ref/hare/io/types.ha:55.
|
||||
export type closer = fn(s: stream) (void | error);
|
||||
|
||||
// ref/hare/io/types.ha:76. Hare's `fn(s: *stream, off: off, w: whence)`;
|
||||
// ww's `stream` is already `*vtable` (the #94 collapse), so the param is
|
||||
// `stream` directly, mirroring the reader/writer/closer aliases above.
|
||||
export type seeker = fn(s: stream, off: off, w: whence) (off | error);
|
||||
|
||||
// memio — in-memory io stream. Project #94 fold-eFinal.
|
||||
//
|
||||
// Hare's memio:: surface, drop underscores. Two flavours behind a
|
||||
|
||||
Reference in New Issue
Block a user