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.
Files changed (108) hide show
  1. package/README.md +8 -7
  2. package/docs/AI.md +75 -14
  3. package/docs/INSTALL.md +92 -71
  4. package/package.json +12 -7
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/core/arrays.d.ts +4 -0
  8. package/runtime/core/arrays.mjs +33 -0
  9. package/runtime/nish.d.ts +1045 -512
  10. package/runtime/nish.h +53 -6
  11. package/runtime/page.d.ts +53 -0
  12. package/runtime/page.mjs +146 -0
  13. package/runtime/runtime-async.c +740 -0
  14. package/runtime/runtime-cross.c +2 -1
  15. package/runtime/runtime-region.c +85 -7
  16. package/runtime/runtime-text.c +1068 -0
  17. package/runtime/runtime-wasm.c +65 -9
  18. package/runtime/runtime.c +50 -961
  19. package/runtime/vite.mjs +48 -11
  20. package/scripts/build.sh +231 -31
  21. package/scripts/fuzz.sh +59 -8
  22. package/scripts/gen-pow10-table.py +1 -1
  23. package/scripts/postinstall.mjs +8 -7
  24. package/scripts/size-report.sh +2 -2
  25. package/scripts/undocumented.txt +14 -0
  26. package/std/LICENSE-fdlibm +8 -0
  27. package/std/README.md +109 -82
  28. package/std/array.ts +266 -0
  29. package/std/bytes.ts +23 -19
  30. package/std/compress/checksum.ts +1 -1
  31. package/std/crypto/aes.ts +48 -43
  32. package/std/crypto/base64url.ts +7 -5
  33. package/std/crypto/bcrypt-batch.ts +13 -13
  34. package/std/crypto/bcrypt.ts +33 -27
  35. package/std/crypto/chacha20poly1305.ts +16 -14
  36. package/std/crypto/ed25519.ts +19 -16
  37. package/std/crypto/hmac.ts +7 -6
  38. package/std/crypto/jwt.ts +23 -20
  39. package/std/crypto/p256.ts +63 -56
  40. package/std/crypto/p384.ts +23 -20
  41. package/std/crypto/pki.ts +18 -15
  42. package/std/crypto/rsa.ts +23 -19
  43. package/std/crypto/x25519.ts +11 -10
  44. package/std/crypto/x509.ts +69 -62
  45. package/std/host/ring.ts +124 -0
  46. package/std/host/text.ts +86 -0
  47. package/std/json-equal.ts +12 -11
  48. package/std/json-number.ts +12 -12
  49. package/std/json-tokenizer.ts +26 -22
  50. package/std/json-typed.ts +92 -78
  51. package/std/json-value.ts +38 -33
  52. package/std/json.ts +32 -28
  53. package/std/math.ts +743 -41
  54. package/std/net/dns-resolver.ts +46 -6
  55. package/std/net/dns.ts +50 -13
  56. package/std/net/fetch.ts +1744 -295
  57. package/std/net/handler.ts +30 -0
  58. package/std/net/hpack.ts +29 -26
  59. package/std/net/http-compress.ts +42 -28
  60. package/std/net/http-fields.ts +19 -4
  61. package/std/net/http1-client-codec.ts +29 -25
  62. package/std/net/http1-codec.ts +62 -91
  63. package/std/net/http1-server.ts +79 -57
  64. package/std/net/http2-frame.ts +17 -16
  65. package/std/net/http2-tls.ts +17 -15
  66. package/std/net/http2.ts +67 -54
  67. package/std/net/http3-frame.ts +11 -10
  68. package/std/net/http3-server.ts +36 -35
  69. package/std/net/http3.ts +70 -58
  70. package/std/net/postgres-async.ts +1005 -0
  71. package/std/net/postgres.ts +185 -164
  72. package/std/net/prefork.ts +38 -34
  73. package/std/net/qpack.ts +15 -14
  74. package/std/net/quic-conn-ack.ts +1 -0
  75. package/std/net/quic-conn-cid.ts +11 -0
  76. package/std/net/quic-conn-params.ts +41 -14
  77. package/std/net/quic-datagram.ts +7 -0
  78. package/std/net/quic-frame.ts +48 -12
  79. package/std/net/quic-listener.ts +61 -44
  80. package/std/net/quic-packet.ts +71 -52
  81. package/std/net/quic-recovery.ts +59 -21
  82. package/std/net/quic-stream.ts +83 -55
  83. package/std/net/quic.ts +204 -152
  84. package/std/net/serve.ts +763 -0
  85. package/std/net/tls/client.ts +1 -0
  86. package/std/net/tls/codec.ts +81 -12
  87. package/std/net/tls/record-client.ts +30 -18
  88. package/std/net/tls/record-server.ts +74 -46
  89. package/std/net/tls/record.ts +33 -24
  90. package/std/net/tls/ticket.ts +2 -0
  91. package/std/net/tls/verify.ts +8 -7
  92. package/std/net/tls-tcp.ts +58 -50
  93. package/std/net/tls.ts +55 -43
  94. package/std/net/url.ts +12 -9
  95. package/std/net/websocket.ts +23 -19
  96. package/std/net/webtransport.ts +24 -18
  97. package/std/random.ts +8 -6
  98. package/std/regex.ts +70 -59
  99. package/std/sync/ring.ts +24 -21
  100. package/std/sync/shm.ts +9 -7
  101. package/std/term-io.ts +17 -14
  102. package/std/term-probe.ts +11 -10
  103. package/std/term-rate.ts +71 -61
  104. package/std/term.ts +73 -59
  105. package/std/text.ts +242 -15
  106. package/std/threads.ts +19 -15
  107. package/std/yaml-json.ts +17 -15
  108. package/std/yaml-write.ts +21 -14
@@ -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. Strings and I/O
13
- * need a WASI runtime and are not provided here, so `--emit-dts` lists
14
- * string functions as not exported. */
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
- if (need < size || need > (uintptr_t)-1 - (uintptr_t)nish_arena.buf) __builtin_trap();
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) __builtin_trap();
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) __builtin_trap();
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) __builtin_trap();
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) __builtin_trap();
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