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
@@ -77,7 +77,8 @@ static void *nish_cross_block(uint64_t size) {
77
77
  * value. */
78
78
  static int nish_since(uint64_t mark, uintptr_t p) {
79
79
  for (const nish_xchunk *c = (const nish_xchunk *)nish_arena.chunks; c; c = c->next) {
80
- uintptr_t base = (uintptr_t)(c + 1), end = base + c->cap;
80
+ /* Bit 0 of `cap` marks a carved chunk (runtime.c's NISH_CHUNK_CARVED). */
81
+ uintptr_t base = (uintptr_t)(c + 1), end = base + (c->cap & ~(uint64_t)1);
81
82
  int marked = mark >= base && mark <= end;
82
83
  if (p >= (marked ? (uintptr_t)mark : base) && p < end) return 1;
83
84
  if (marked) return 0;
@@ -55,8 +55,15 @@
55
55
  *
56
56
  * Freeing a region frees its chunks through runtime.c's `nish_free_arena`,
57
57
  * with the region's arena swapped in for the call, so an `-DNISH_ASAN` build
58
- * forgets the freed blocks as it does for the thread's arena. */
58
+ * forgets the freed blocks as it does for the thread's arena.
59
+ *
60
+ * **Adopting** a region gives its chunks to the current arena instead of
61
+ * freeing them, then retires the handle as `free` does: `awaitAll` runs each
62
+ * call that waits in a region of its own, and adopts it when the call
63
+ * finishes, so the answer stays readable and is freed with the caller's
64
+ * memory (docs/security/async.md, ASYNC-14). */
59
65
 
66
+ #include <stddef.h>
60
67
  #include <stdint.h>
61
68
  #include <stdlib.h>
62
69
 
@@ -73,6 +80,20 @@ extern NISH_TLS uint64_t nish_region_chunk;
73
80
  #define NISH_REGION_GEN_END ((uint64_t)1 << 40)
74
81
  #define NISH_REGION_PAGE 1024
75
82
 
83
+ /* runtime.c's `nish_chunk`, which `nish_region_adopt` links through and
84
+ carves: the same three facts runtime.c asserts of it. Bit 0 of `cap` marks a
85
+ carved chunk, as runtime.c's NISH_CHUNK_CARVED. */
86
+ typedef struct nish_chunk_link { struct nish_chunk_link *next; uint64_t cap; } nish_chunk_link;
87
+ _Static_assert(sizeof(nish_chunk_link) == 16, "runtime.c's nish_chunk: { next, cap }, the bytes after it");
88
+ _Static_assert(offsetof(nish_chunk_link, next) == 0, "runtime.c's nish_chunk: next first");
89
+ _Static_assert(offsetof(nish_chunk_link, cap) == 8, "runtime.c's nish_chunk: cap second");
90
+ #define NISH_CHUNK_CARVED 1
91
+
92
+ /* The least a caller's chunk must have left for `nish_region_adopt` to carve
93
+ it into a chunk of its own: below it the bytes are given up, so an adopt
94
+ leaves at most this much behind. */
95
+ #define NISH_ADOPT_CARVE 256
96
+
76
97
  typedef struct nish_region_slot {
77
98
  struct nish_arena arena; /* the region's arena, or, while entered, the one it displaced */
78
99
  uint64_t gen; /* the live handle's generation; NISH_REGION_GEN_END once retired */
@@ -223,12 +244,10 @@ void nish_region_leave(uint64_t r, void *root) {
223
244
  s->root = root;
224
245
  }
225
246
 
226
- void nish_region_free(uint64_t r) {
227
- nish_region_slot *s = nish_region_slot_of(r);
228
- if (s->entered) nish_region_die(NISH_REGION_FREE_ENTERED);
229
- nish_region_swap(s);
230
- nish_free_arena();
231
- nish_region_swap(s);
247
+ /* Give the slot back: its generation moves on, so every copy of `r` is
248
+ stale, and the slot joins the free list unless that generation is the last.
249
+ The slot's arena is already empty. */
250
+ static void nish_region_retire(nish_region_slot *s, uint64_t r) {
232
251
  s->root = 0;
233
252
  uint64_t index = r & (NISH_REGION_SLOTS - 1);
234
253
  nish_region_lock();
@@ -241,3 +260,62 @@ void nish_region_free(uint64_t r) {
241
260
  }
242
261
  nish_region_unlock();
243
262
  }
263
+
264
+ void nish_region_free(uint64_t r) {
265
+ nish_region_slot *s = nish_region_slot_of(r);
266
+ if (s->entered) nish_region_die(NISH_REGION_FREE_ENTERED);
267
+ nish_region_swap(s);
268
+ nish_free_arena();
269
+ nish_region_swap(s);
270
+ nish_region_retire(s, r);
271
+ }
272
+
273
+ /* An `awaitAll` lane's region, once the lane has finished: its chunks go on
274
+ top of the arena in `nish_arena`, newest first as there, so what the lane
275
+ answered lives on in the arena that awaited it and is given back when that
276
+ arena next releases below them. What the caller's current chunk had left is
277
+ then carved into a chunk of its own, on top of the region's, and bumping
278
+ goes on there: the caller's chunk is not buried under the region's, which
279
+ would leave its rest unused and make the caller's next allocation grow a
280
+ fresh 64 KiB chunk -- a chunk per adopt in a loop that never releases
281
+ (docs/security/async.md, ASYNC-15). The carved chunk has no malloc of its
282
+ own, and bit 0 of its `cap` says so, so `nish_free_until` passes it by; it
283
+ always sits above the chunk it was carved from, so whatever frees that one
284
+ has dropped it first. Less than NISH_ADOPT_CARVE left is given up. The
285
+ next-chunk size (`nish_region_chunk`) is not touched: it is the caller's,
286
+ which a region only swaps while entered. A newest chunk the lane released
287
+ back to empty is freed, not moved, so a lane's temporaries do not outlive
288
+ it. A region that never allocated moves nothing. Like `free`, it panics on
289
+ a region that is entered. */
290
+ void nish_region_adopt(uint64_t r) {
291
+ nish_region_slot *s = nish_region_slot_of(r);
292
+ if (s->entered) nish_region_die(NISH_REGION_FREE_ENTERED);
293
+ nish_chunk_link *head = s->arena.chunks;
294
+ if (head && s->arena.off == 0) {
295
+ /* The lane rewound its newest chunk to empty: a release to the mark it
296
+ took on entry, 0 in an empty region, keeps that chunk for reuse
297
+ (`nish_reset_arena`), but nothing will bump it again once adopted. */
298
+ nish_chunk_link *next = head->next;
299
+ if (!(head->cap & NISH_CHUNK_CARVED)) free(head);
300
+ head = next;
301
+ uint64_t cap = head ? head->cap & ~(uint64_t)NISH_CHUNK_CARVED : 0;
302
+ s->arena = head ? (struct nish_arena){ (char *)(head + 1), cap, cap, (void *)head } : (struct nish_arena){ 0 };
303
+ }
304
+ if (head) {
305
+ nish_chunk_link *last = head;
306
+ while (last->next) last = last->next;
307
+ struct nish_arena caller = nish_arena;
308
+ uint64_t left = caller.buf ? caller.cap - caller.off : 0;
309
+ last->next = caller.chunks;
310
+ nish_arena = s->arena;
311
+ if (left >= NISH_ADOPT_CARVE) {
312
+ nish_chunk_link *c = (nish_chunk_link *)(caller.buf + caller.off);
313
+ uint64_t cap = left - sizeof *c;
314
+ c->next = nish_arena.chunks;
315
+ c->cap = cap | NISH_CHUNK_CARVED;
316
+ nish_arena = (struct nish_arena){ (char *)(c + 1), 0, cap, (void *)c };
317
+ }
318
+ }
319
+ s->arena = (struct nish_arena){ 0 };
320
+ nish_region_retire(s, r);
321
+ }