nish 0.36.0 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -7
- package/docs/AI.md +75 -14
- package/docs/INSTALL.md +92 -71
- package/package.json +12 -7
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/nish.d.ts +1045 -512
- package/runtime/nish.h +53 -6
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +740 -0
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-region.c +85 -7
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +65 -9
- package/runtime/runtime.c +50 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +231 -31
- package/scripts/fuzz.sh +59 -8
- package/scripts/gen-pow10-table.py +1 -1
- package/scripts/postinstall.mjs +8 -7
- package/scripts/size-report.sh +2 -2
- package/scripts/undocumented.txt +14 -0
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +109 -82
- package/std/array.ts +266 -0
- package/std/bytes.ts +23 -19
- package/std/compress/checksum.ts +1 -1
- package/std/crypto/aes.ts +48 -43
- package/std/crypto/base64url.ts +7 -5
- package/std/crypto/bcrypt-batch.ts +13 -13
- package/std/crypto/bcrypt.ts +33 -27
- package/std/crypto/chacha20poly1305.ts +16 -14
- package/std/crypto/ed25519.ts +19 -16
- package/std/crypto/hmac.ts +7 -6
- package/std/crypto/jwt.ts +23 -20
- package/std/crypto/p256.ts +63 -56
- package/std/crypto/p384.ts +23 -20
- package/std/crypto/pki.ts +18 -15
- package/std/crypto/rsa.ts +23 -19
- package/std/crypto/x25519.ts +11 -10
- package/std/crypto/x509.ts +69 -62
- package/std/host/ring.ts +124 -0
- package/std/host/text.ts +86 -0
- package/std/json-equal.ts +12 -11
- package/std/json-number.ts +12 -12
- package/std/json-tokenizer.ts +26 -22
- package/std/json-typed.ts +92 -78
- package/std/json-value.ts +38 -33
- package/std/json.ts +32 -28
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +1744 -295
- package/std/net/handler.ts +30 -0
- package/std/net/hpack.ts +29 -26
- package/std/net/http-compress.ts +42 -28
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-client-codec.ts +29 -25
- package/std/net/http1-codec.ts +62 -91
- package/std/net/http1-server.ts +79 -57
- package/std/net/http2-frame.ts +17 -16
- package/std/net/http2-tls.ts +17 -15
- package/std/net/http2.ts +67 -54
- package/std/net/http3-frame.ts +11 -10
- package/std/net/http3-server.ts +36 -35
- package/std/net/http3.ts +70 -58
- package/std/net/postgres-async.ts +1005 -0
- package/std/net/postgres.ts +185 -164
- package/std/net/prefork.ts +38 -34
- package/std/net/qpack.ts +15 -14
- package/std/net/quic-conn-ack.ts +1 -0
- package/std/net/quic-conn-cid.ts +11 -0
- package/std/net/quic-conn-params.ts +41 -14
- package/std/net/quic-datagram.ts +7 -0
- package/std/net/quic-frame.ts +48 -12
- package/std/net/quic-listener.ts +61 -44
- package/std/net/quic-packet.ts +71 -52
- package/std/net/quic-recovery.ts +59 -21
- package/std/net/quic-stream.ts +83 -55
- package/std/net/quic.ts +204 -152
- package/std/net/serve.ts +763 -0
- package/std/net/tls/client.ts +1 -0
- package/std/net/tls/codec.ts +81 -12
- package/std/net/tls/record-client.ts +30 -18
- package/std/net/tls/record-server.ts +74 -46
- package/std/net/tls/record.ts +33 -24
- package/std/net/tls/ticket.ts +2 -0
- package/std/net/tls/verify.ts +8 -7
- package/std/net/tls-tcp.ts +58 -50
- package/std/net/tls.ts +55 -43
- package/std/net/url.ts +12 -9
- package/std/net/websocket.ts +23 -19
- package/std/net/webtransport.ts +24 -18
- package/std/random.ts +8 -6
- package/std/regex.ts +70 -59
- package/std/sync/ring.ts +24 -21
- package/std/sync/shm.ts +9 -7
- package/std/term-io.ts +17 -14
- package/std/term-probe.ts +11 -10
- package/std/term-rate.ts +71 -61
- package/std/term.ts +73 -59
- package/std/text.ts +242 -15
- package/std/threads.ts +19 -15
- package/std/yaml-json.ts +17 -15
- package/std/yaml-write.ts +21 -14
package/runtime/runtime-wasm.c
CHANGED
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
* (`%struct.nish_arena = type { i8*, i64, i64, i8* }`), not size_t: wasm32
|
|
10
10
|
* pointers are 4 bytes, and the inlined fast path in every compiled function
|
|
11
11
|
* bumps the 64-bit `off` field directly. Panics are `unreachable` (a JS
|
|
12
|
-
* RuntimeError); there is no stderr to write a message to
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* RuntimeError); there is no stderr to write a message to, so under
|
|
13
|
+
* wasm-host the page is told first (NISH_DIE). Strings are
|
|
14
|
+
* runtime-text.c's, which build.sh links beside this file when the IR builds
|
|
15
|
+
* one; I/O needs a WASI runtime and is not provided. */
|
|
15
16
|
#include <stddef.h>
|
|
16
17
|
#include <stdint.h>
|
|
17
18
|
|
|
@@ -37,6 +38,35 @@ _Static_assert(offsetof(struct nish_arena, off) == 8, "the inlined allocator bum
|
|
|
37
38
|
_Static_assert(offsetof(struct nish_arena, cap) == 16, "the inlined allocator reads field 2");
|
|
38
39
|
_Static_assert(offsetof(struct nish_arena, chunks) == 24, "arena layout is ABI");
|
|
39
40
|
|
|
41
|
+
/* WP50 stage 3: a module for a page (`scripts/build.sh --profile wasm-host`,
|
|
42
|
+
* which compiles this file with -DNISH_WASM_HOST) reports each panic to the
|
|
43
|
+
* page before it traps, through the import `nish.panic`. `kind` is one of the
|
|
44
|
+
* NISH_PANIC_* below; `site` numbers the call in `<out>.sites.json`, or is -1
|
|
45
|
+
* for a panic the compiler did not tag (one this runtime raises itself);
|
|
46
|
+
* `msg`/`len` are a message's bytes in linear memory (kind panic only) and
|
|
47
|
+
* `a`, `b` its numbers: an index and a length, a slice's start and end (its
|
|
48
|
+
* length in `len`), an arithmetic operator, an exit code. The page's loader
|
|
49
|
+
* formats the message, marks the module dead and throws, so the import does
|
|
50
|
+
* not return; the trap after it is for a page that returns anyway. Without
|
|
51
|
+
* NISH_WASM_HOST (`--profile wasm`) each panic is the bare trap it was. */
|
|
52
|
+
#define NISH_PANIC_MESSAGE 0
|
|
53
|
+
#define NISH_PANIC_INDEX 1
|
|
54
|
+
#define NISH_PANIC_SLICE 2
|
|
55
|
+
#define NISH_PANIC_ARITH 3
|
|
56
|
+
#define NISH_PANIC_MEMORY 4
|
|
57
|
+
#define NISH_PANIC_EXIT 5
|
|
58
|
+
#ifdef NISH_WASM_HOST
|
|
59
|
+
__attribute__((import_module("nish"), import_name("panic"))) void nish_host_panic(
|
|
60
|
+
int32_t kind, int32_t site, const void *msg, uint32_t len, double a, double b);
|
|
61
|
+
#define NISH_DIE(kind, site, msg, len, a, b) \
|
|
62
|
+
do { \
|
|
63
|
+
nish_host_panic((kind), (site), (msg), (len), (a), (b)); \
|
|
64
|
+
__builtin_trap(); \
|
|
65
|
+
} while (0)
|
|
66
|
+
#else
|
|
67
|
+
#define NISH_DIE(kind, site, msg, len, a, b) __builtin_trap()
|
|
68
|
+
#endif
|
|
69
|
+
|
|
40
70
|
extern unsigned char __heap_base;
|
|
41
71
|
#define NISH_PAGE ((uint64_t)65536)
|
|
42
72
|
|
|
@@ -55,10 +85,12 @@ void *nish_arena_grow(uint64_t size) {
|
|
|
55
85
|
nish_arena.cap = (uint64_t)__builtin_wasm_memory_size(0) * NISH_PAGE - base;
|
|
56
86
|
}
|
|
57
87
|
uint64_t need = nish_arena.off + size;
|
|
58
|
-
|
|
88
|
+
/* Under wasm-host both are "out of memory": the address space, or a
|
|
89
|
+
`--memory` the module was built with, is spent. */
|
|
90
|
+
if (need < size || need > (uintptr_t)-1 - (uintptr_t)nish_arena.buf) NISH_DIE(NISH_PANIC_MEMORY, -1, 0, 0, 0, 0);
|
|
59
91
|
if (need > nish_arena.cap) {
|
|
60
92
|
uint64_t pages = (need - nish_arena.cap + NISH_PAGE - 1) / NISH_PAGE;
|
|
61
|
-
if (__builtin_wasm_memory_grow(0, (uintptr_t)pages) == (uintptr_t)-1)
|
|
93
|
+
if (__builtin_wasm_memory_grow(0, (uintptr_t)pages) == (uintptr_t)-1) NISH_DIE(NISH_PANIC_MEMORY, -1, 0, 0, 0, 0);
|
|
62
94
|
nish_arena.cap += pages * NISH_PAGE;
|
|
63
95
|
}
|
|
64
96
|
void *p = nish_arena.buf + nish_arena.off;
|
|
@@ -69,7 +101,7 @@ void *nish_arena_grow(uint64_t size) {
|
|
|
69
101
|
/* `size <= cap - off` rather than `off + size <= cap`, which a size near 2^64
|
|
70
102
|
wraps into a yes; a size that rounding wraps to 0 is trapped the same way. */
|
|
71
103
|
void *nish_alloc_struct(uint64_t size) {
|
|
72
|
-
if (size > (uint64_t)-8)
|
|
104
|
+
if (size > (uint64_t)-8) NISH_DIE(NISH_PANIC_MEMORY, -1, 0, 0, 0, 0);
|
|
73
105
|
size = (size + 7) & ~(uint64_t)7;
|
|
74
106
|
if (nish_arena.buf && size <= nish_arena.cap - nish_arena.off) {
|
|
75
107
|
void *p = nish_arena.buf + nish_arena.off;
|
|
@@ -96,7 +128,7 @@ typedef struct nish_array { uint64_t len; uint64_t cap; char *data; } nish_array
|
|
|
96
128
|
nish_array *nish_alloc_array(uint64_t elem_size, uint64_t len) {
|
|
97
129
|
/* With `len` in range, `len * elem_size` cannot wrap for any element
|
|
98
130
|
smaller than 8 GiB, and a product past 4 GiB traps in the grow. */
|
|
99
|
-
if (len > NISH_ARRAY_MAX)
|
|
131
|
+
if (len > NISH_ARRAY_MAX) NISH_DIE(NISH_PANIC_MEMORY, -1, 0, 0, 0, 0);
|
|
100
132
|
nish_array *a = (nish_array *)nish_alloc_struct(sizeof *a);
|
|
101
133
|
a->len = a->cap = len;
|
|
102
134
|
a->data = (char *)nish_alloc_struct(len * elem_size);
|
|
@@ -106,7 +138,7 @@ nish_array *nish_alloc_array(uint64_t elem_size, uint64_t len) {
|
|
|
106
138
|
void nish_array_grow(nish_array *a, uint64_t elem_size) {
|
|
107
139
|
uint64_t cap = a->cap ? a->cap * 2 : 4;
|
|
108
140
|
if (cap > NISH_ARRAY_MAX) {
|
|
109
|
-
if (a->len >= NISH_ARRAY_MAX)
|
|
141
|
+
if (a->len >= NISH_ARRAY_MAX) NISH_DIE(NISH_PANIC_MEMORY, -1, 0, 0, 0, 0);
|
|
110
142
|
cap = NISH_ARRAY_MAX;
|
|
111
143
|
}
|
|
112
144
|
char *data = (char *)nish_alloc_struct(cap * elem_size);
|
|
@@ -115,7 +147,30 @@ void nish_array_grow(nish_array *a, uint64_t elem_size) {
|
|
|
115
147
|
a->cap = cap;
|
|
116
148
|
}
|
|
117
149
|
|
|
118
|
-
/* ---- Panics: `unreachable`, which the host sees as a RuntimeError
|
|
150
|
+
/* ---- Panics: `unreachable`, which the host sees as a RuntimeError; under
|
|
151
|
+
* wasm-host the page hears of each first (NISH_DIE above). */
|
|
152
|
+
#ifdef NISH_WASM_HOST
|
|
153
|
+
/* The `_at` forms take the site number build.sh rewrote a tagged call to. */
|
|
154
|
+
void nish_panic_index_at(int32_t site, uint64_t idx, uint64_t len) {
|
|
155
|
+
NISH_DIE(NISH_PANIC_INDEX, site, 0, 0, (double)idx, (double)len);
|
|
156
|
+
}
|
|
157
|
+
void nish_panic_slice_at(int32_t site, int64_t s, int64_t e, int64_t len) {
|
|
158
|
+
NISH_DIE(NISH_PANIC_SLICE, site, 0, (uint32_t)len, (double)s, (double)e);
|
|
159
|
+
}
|
|
160
|
+
void nish_panic_overflow_at(int32_t site, uint32_t op) { NISH_DIE(NISH_PANIC_ARITH, site, 0, 0, (double)op, 0); }
|
|
161
|
+
void nish_panic_div_at(int32_t site, _Bool by_zero) { nish_panic_overflow_at(site, 4 + by_zero); }
|
|
162
|
+
/* A string is `{ i64 len, bytes[len] }` (runtime.c's nish_str). */
|
|
163
|
+
void nish_panic_at(int32_t site, const void *s) {
|
|
164
|
+
const uint64_t *len = (const uint64_t *)s;
|
|
165
|
+
NISH_DIE(NISH_PANIC_MESSAGE, site, (const char *)s + 8, (uint32_t)*len, 0, 0);
|
|
166
|
+
}
|
|
167
|
+
void nish_panic_index(uint64_t idx, uint64_t len) { nish_panic_index_at(-1, idx, len); }
|
|
168
|
+
void nish_panic_slice(int64_t s, int64_t e, int64_t len) { nish_panic_slice_at(-1, s, e, len); }
|
|
169
|
+
void nish_panic_div(_Bool by_zero) { nish_panic_div_at(-1, by_zero); }
|
|
170
|
+
void nish_panic_overflow(uint32_t op) { nish_panic_overflow_at(-1, op); }
|
|
171
|
+
void nish_exit(int32_t code) { NISH_DIE(NISH_PANIC_EXIT, -1, 0, 0, (double)code, 0); }
|
|
172
|
+
void nish_panic(const void *s) { nish_panic_at(-1, s); }
|
|
173
|
+
#else
|
|
119
174
|
void nish_panic_index(uint64_t idx, uint64_t len) { (void)idx; (void)len; __builtin_trap(); }
|
|
120
175
|
void nish_panic_slice(int64_t s, int64_t e, int64_t len) { (void)s; (void)e; (void)len; __builtin_trap(); }
|
|
121
176
|
void nish_panic_div(_Bool by_zero) { (void)by_zero; __builtin_trap(); }
|
|
@@ -126,6 +181,7 @@ void nish_exit(int32_t code) { (void)code; __builtin_trap(); }
|
|
|
126
181
|
* parameter checks it on entry (WP31 §9), so this is what lets such a module
|
|
127
182
|
* link; the loader throws a RangeError before the call reaches it. */
|
|
128
183
|
void nish_panic(const void *s) { (void)s; __builtin_trap(); }
|
|
184
|
+
#endif
|
|
129
185
|
/* Output has nowhere to go either. */
|
|
130
186
|
void nish_write(const void *s, int32_t fd, _Bool newline) { (void)s; (void)fd; (void)newline; }
|
|
131
187
|
|