lib: rename stdlib surface to Hare names; add endian/math
Sweeping rename so the lib/ surface mirrors Hare's stdlib spellings. - ascii: rune-taking predicates; ishex -> isxdigit - bufio: rinit -> init; take1/takeline -> readbyte/readline - bytes: indexsub -> index - encoding/utf8: runelen -> runesz - errors: eEOF/eShortRead/... -> eof/underread/... - fmt: errln -> errorln; println/fprintln return i64 - os: readfull/writefull -> readall/writeall; unlink -> remove - path: isabs -> abs; drop lastindex (now strings.rbyteindex) - strconv: u64toa/i64toa -> u64tos/i64tos; parse64/parseu64 -> stoi64/stou64 - strings: drop len/isempty; equal -> compare; indexbyte -> byteindex; +rbyteindex - types: drop numeric helpers (moved to math) - new lib/endian (htonu16/ntohu16), lib/math (absi32/absi64) - net: drop htons (use endian.htonu16) Callers in selfhost/, lib/ww/, cmd/w6c/cgen.c, and test/wcc/700_e2e.c updated to match.
This commit is contained in:
@@ -120,9 +120,11 @@ export fn filesize(fd: i32) i64 = {
|
||||
return end;
|
||||
};
|
||||
|
||||
// readfull — keep reading until `n` bytes have arrived or the fd
|
||||
// readall — keep reading until `n` bytes have arrived or the fd
|
||||
// closes early. Returns bytes read (0..=n) or -1 on read error.
|
||||
export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
|
||||
// Hare name (io::readall); the buffer is caller-supplied, matching
|
||||
// the Plan 9 subset convention.
|
||||
export fn readall(fd: i32, buf: *u8, n: u64) i64 = {
|
||||
let got: u64 = 0u64;
|
||||
for (got < n) {
|
||||
let r: i64 = read(fd, buf + got, n - got);
|
||||
@@ -133,9 +135,10 @@ export fn readfull(fd: i32, buf: *u8, n: u64) i64 = {
|
||||
return got: i64;
|
||||
};
|
||||
|
||||
// writefull — keep writing until `n` bytes have been accepted or the
|
||||
// fd refuses progress. Returns bytes written or -1.
|
||||
export fn writefull(fd: i32, buf: *u8, n: u64) i64 = {
|
||||
// writeall — keep writing until `n` bytes have been accepted or the
|
||||
// fd refuses progress. Returns bytes written or -1. Hare name
|
||||
// (io::writeall).
|
||||
export fn writeall(fd: i32, buf: *u8, n: u64) i64 = {
|
||||
let sent: u64 = 0u64;
|
||||
for (sent < n) {
|
||||
let r: i64 = write(fd, buf + sent, n - sent);
|
||||
@@ -154,8 +157,8 @@ export fn access(path: *u8, mode: i32) i32 = {
|
||||
return syscall2(SYS_ACCESS, path: i64, mode: i64): i32;
|
||||
};
|
||||
|
||||
// unlink(2).
|
||||
export fn unlink(path: *u8) i32 = {
|
||||
// remove — unlink(2). Hare name; the underlying syscall is unlink(2).
|
||||
export fn remove(path: *u8) i32 = {
|
||||
return syscall1(SYS_UNLINK, path: i64): i32;
|
||||
};
|
||||
|
||||
@@ -599,14 +602,14 @@ fn locateimport(a: *arena, dirs: *u8, name: *u8, namelen: u64) *u8 = {
|
||||
|
||||
// ---- file slurp -------------------------------------------------------
|
||||
|
||||
fn readall(pathcs: *u8) (*u8, u64) = {
|
||||
fn slurp(pathcs: *u8) (*u8, u64) = {
|
||||
let fd: i32 = os.open(pathcs, os.O_RDONLY, 0i32);
|
||||
if (fd < 0) { return nil, 0u64; };
|
||||
let n: i64 = os.filesize(fd);
|
||||
if (n < 0i64) { os.close(fd); return nil, 0u64; };
|
||||
let nu: u64 = n: u64;
|
||||
let buf: *u8 = os.alloc(nu + 1u64): *u8;
|
||||
let got: i64 = os.readfull(fd, buf, nu);
|
||||
let got: i64 = os.readall(fd, buf, nu);
|
||||
os.close(fd);
|
||||
if (got != n) { return nil, 0u64; };
|
||||
buf[nu] = 0u8;
|
||||
@@ -707,7 +710,7 @@ fn expand(c: *expctx, pathcs: *u8) void = {
|
||||
|
||||
let bufp: *u8;
|
||||
let blen: u64;
|
||||
bufp, blen = readall(pathcs);
|
||||
bufp, blen = slurp(pathcs);
|
||||
if (bufp == nil) {
|
||||
os.write(2, "ww: cannot read source\n".ptr, 23u64);
|
||||
return;
|
||||
@@ -743,12 +746,12 @@ fn expand(c: *expctx, pathcs: *u8) void = {
|
||||
let mn: u64;
|
||||
mp, mn = modulename(pathcs, plen);
|
||||
if (mn > 0u64) {
|
||||
os.writefull(c.out, "// MODULE: ".ptr, 11u64);
|
||||
os.writefull(c.out, mp, mn);
|
||||
os.writefull(c.out, "\n".ptr, 1u64);
|
||||
os.writeall(c.out, "// MODULE: ".ptr, 11u64);
|
||||
os.writeall(c.out, mp, mn);
|
||||
os.writeall(c.out, "\n".ptr, 1u64);
|
||||
};
|
||||
os.writefull(c.out, bufp, blen);
|
||||
os.writefull(c.out, "\n".ptr, 1u64);
|
||||
os.writeall(c.out, bufp, blen);
|
||||
os.writeall(c.out, "\n".ptr, 1u64);
|
||||
};
|
||||
|
||||
// ---- Build pipeline ---------------------------------------------------
|
||||
@@ -1342,7 +1345,7 @@ fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
lf.libs = libs;
|
||||
lf.nlibs = nlibs;
|
||||
if (buildone(selfdir, resolved, tmp, incs, &lf) != 0) {
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
return 1;
|
||||
};
|
||||
|
||||
@@ -1359,7 +1362,7 @@ fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
};
|
||||
execargv[nextra + 1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
return rc;
|
||||
};
|
||||
|
||||
@@ -1374,14 +1377,14 @@ fn runsingletest(selfdir: *u8, src: *u8) i32 = {
|
||||
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
makeruntmp(tmp);
|
||||
if (buildone(selfdir, src, tmp, "\0".ptr, nil) != 0) {
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
return 1;
|
||||
};
|
||||
let execargv: **u8 = os.alloc(16u64): **u8;
|
||||
execargv[0] = tmp;
|
||||
execargv[1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
return rc;
|
||||
};
|
||||
|
||||
@@ -1446,7 +1449,7 @@ fn rundirtests(selfdir: *u8, dir: *u8) i32 = {
|
||||
os.write(2, "\n".ptr, 1u64);
|
||||
};
|
||||
};
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
};
|
||||
off += reclen;
|
||||
};
|
||||
|
||||
@@ -280,14 +280,14 @@ fn locateimport(a: *arena, dirs: *u8, name: *u8, namelen: u64) *u8 = {
|
||||
|
||||
// ---- file slurp -------------------------------------------------------
|
||||
|
||||
fn readall(pathcs: *u8) (*u8, u64) = {
|
||||
fn slurp(pathcs: *u8) (*u8, u64) = {
|
||||
let fd: i32 = os.open(pathcs, os.O_RDONLY, 0i32);
|
||||
if (fd < 0) { return nil, 0u64; };
|
||||
let n: i64 = os.filesize(fd);
|
||||
if (n < 0i64) { os.close(fd); return nil, 0u64; };
|
||||
let nu: u64 = n: u64;
|
||||
let buf: *u8 = os.alloc(nu + 1u64): *u8;
|
||||
let got: i64 = os.readfull(fd, buf, nu);
|
||||
let got: i64 = os.readall(fd, buf, nu);
|
||||
os.close(fd);
|
||||
if (got != n) { return nil, 0u64; };
|
||||
buf[nu] = 0u8;
|
||||
@@ -388,7 +388,7 @@ fn expand(c: *expctx, pathcs: *u8) void = {
|
||||
|
||||
let bufp: *u8;
|
||||
let blen: u64;
|
||||
bufp, blen = readall(pathcs);
|
||||
bufp, blen = slurp(pathcs);
|
||||
if (bufp == nil) {
|
||||
os.write(2, "ww: cannot read source\n".ptr, 23u64);
|
||||
return;
|
||||
@@ -424,12 +424,12 @@ fn expand(c: *expctx, pathcs: *u8) void = {
|
||||
let mn: u64;
|
||||
mp, mn = modulename(pathcs, plen);
|
||||
if (mn > 0u64) {
|
||||
os.writefull(c.out, "// MODULE: ".ptr, 11u64);
|
||||
os.writefull(c.out, mp, mn);
|
||||
os.writefull(c.out, "\n".ptr, 1u64);
|
||||
os.writeall(c.out, "// MODULE: ".ptr, 11u64);
|
||||
os.writeall(c.out, mp, mn);
|
||||
os.writeall(c.out, "\n".ptr, 1u64);
|
||||
};
|
||||
os.writefull(c.out, bufp, blen);
|
||||
os.writefull(c.out, "\n".ptr, 1u64);
|
||||
os.writeall(c.out, bufp, blen);
|
||||
os.writeall(c.out, "\n".ptr, 1u64);
|
||||
};
|
||||
|
||||
// ---- Build pipeline ---------------------------------------------------
|
||||
@@ -1023,7 +1023,7 @@ fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
lf.libs = libs;
|
||||
lf.nlibs = nlibs;
|
||||
if (buildone(selfdir, resolved, tmp, incs, &lf) != 0) {
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
return 1;
|
||||
};
|
||||
|
||||
@@ -1040,7 +1040,7 @@ fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
};
|
||||
execargv[nextra + 1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
return rc;
|
||||
};
|
||||
|
||||
@@ -1055,14 +1055,14 @@ fn runsingletest(selfdir: *u8, src: *u8) i32 = {
|
||||
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
makeruntmp(tmp);
|
||||
if (buildone(selfdir, src, tmp, "\0".ptr, nil) != 0) {
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
return 1;
|
||||
};
|
||||
let execargv: **u8 = os.alloc(16u64): **u8;
|
||||
execargv[0] = tmp;
|
||||
execargv[1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
return rc;
|
||||
};
|
||||
|
||||
@@ -1127,7 +1127,7 @@ fn rundirtests(selfdir: *u8, dir: *u8) i32 = {
|
||||
os.write(2, "\n".ptr, 1u64);
|
||||
};
|
||||
};
|
||||
os.unlink(tmp);
|
||||
os.remove(tmp);
|
||||
};
|
||||
off += reclen;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user