@amritk/nish-aarch64-linux 0.39.0 → 0.41.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 (57) hide show
  1. package/INSTALL.md +79 -133
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +112 -33
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +21 -17
  6. package/runtime/nish.h +10 -0
  7. package/runtime/page-frame.d.ts +123 -0
  8. package/runtime/page-frame.mjs +736 -0
  9. package/runtime/page-game.d.ts +132 -0
  10. package/runtime/page-game.mjs +738 -0
  11. package/runtime/runtime-aes.c +85 -8
  12. package/runtime/runtime-bytes.c +88 -4
  13. package/runtime/runtime-net.c +32 -0
  14. package/runtime/runtime-sandbox.c +55 -28
  15. package/runtime/runtime-uring.c +1194 -0
  16. package/runtime/runtime-wasm.c +422 -0
  17. package/runtime/vite.mjs +1 -1
  18. package/scripts/build.sh +22 -6
  19. package/std/README.md +74 -87
  20. package/std/array.ts +36 -3
  21. package/std/collections.ts +51 -0
  22. package/std/crypto/hmac.ts +37 -10
  23. package/std/crypto/p256.ts +1388 -2664
  24. package/std/crypto/pki.ts +116 -4
  25. package/std/crypto/sha256.ts +34 -6
  26. package/std/crypto/x509.ts +7 -0
  27. package/std/host/game.ts +273 -0
  28. package/std/input.ts +56 -0
  29. package/std/json-tokenizer.ts +325 -112
  30. package/std/json-typed.ts +43 -8
  31. package/std/json-value.ts +144 -3
  32. package/std/json.ts +63 -8
  33. package/std/net/fetch.ts +22 -4
  34. package/std/net/http-server.ts +3382 -0
  35. package/std/net/http1-client-codec.ts +6 -2
  36. package/std/net/http1-codec.ts +137 -12
  37. package/std/net/http1-server.ts +83 -29
  38. package/std/net/http2.ts +47 -14
  39. package/std/net/http3-server.ts +29 -3
  40. package/std/net/http3.ts +180 -7
  41. package/std/net/prefork.ts +49 -13
  42. package/std/net/quic-recovery.ts +240 -9
  43. package/std/net/quic-stream.ts +21 -1
  44. package/std/net/quic.ts +98 -4
  45. package/std/net/router.ts +378 -31
  46. package/std/net/serve.ts +873 -65
  47. package/std/net/tls/verify.ts +19 -1
  48. package/std/net/url.ts +83 -35
  49. package/std/net/websocket.ts +74 -1
  50. package/std/net/webtransport.ts +7 -1
  51. package/std/raster.ts +837 -0
  52. package/std/release.ts +593 -0
  53. package/std/synth.ts +143 -0
  54. package/std/temporal.ts +3091 -0
  55. package/std/threads.ts +298 -0
  56. package/std/yaml-json.ts +368 -28
  57. package/std/yaml.ts +122 -27
@@ -111,11 +111,24 @@ void *nish_alloc_struct(uint64_t size) {
111
111
  return nish_arena_grow(size);
112
112
  }
113
113
 
114
+ /* A module with root storage defines both itself (src/emit-root.ts), emptying
115
+ every root first through nish_runtime_root_reset; scripts/build.sh compiles
116
+ it with NISH_WASM_ROOTS, which leaves these two out and takes the root keep
117
+ below in. The wasm profile exports every function, so the root keep is
118
+ compiled into no other module. */
119
+ #ifndef NISH_WASM_ROOTS
114
120
  void nish_reset_arena(void) { nish_arena.off = 0; }
115
121
  void nish_free_arena(void) { nish_arena.off = 0; } /* memory cannot be returned */
122
+ #endif
116
123
  uint64_t nish_arena_used(void) { return nish_arena.off; }
117
124
  uint64_t nish_arena_mark(void) { return nish_arena.buf ? (uint64_t)(uintptr_t)(nish_arena.buf + nish_arena.off) : 0; }
118
125
  void nish_arena_release(uint64_t mark) { nish_arena.off = mark ? mark - (uintptr_t)nish_arena.buf : 0; }
126
+ /* The call-site reclaim's keep (wp6-memory.md §2a). The native runtime moves
127
+ `p` down onto the mark; here it always refuses, the answer runtime.c gives
128
+ whenever it is uncertain: `p` stays where it is and nothing is released, so
129
+ it reads and writes no memory and only reclaims less. String code reached
130
+ from wasm (nish/term) calls it; without it the link fails. */
131
+ void *nish_arena_keep(uint64_t mark, void *p) { (void)mark; return p; }
119
132
 
120
133
  /* ---- Arrays: %struct.nish_array = type { i64, i64, i8* } */
121
134
  typedef struct nish_array { uint64_t len; uint64_t cap; char *data; } nish_array;
@@ -147,6 +160,415 @@ void nish_array_grow(nish_array *a, uint64_t elem_size) {
147
160
  a->cap = cap;
148
161
  }
149
162
 
163
+ #ifdef NISH_WASM_ROOTS
164
+ /* ---- Root storage (docs/wp55-persistent-root.md §4): keep what the roots
165
+ * reach between export calls, release the rest.
166
+ *
167
+ * The loader of a program with root storage calls `nish_root_keep(mark)`
168
+ * (written by the compiler, src/emit-root.ts) where it would release the
169
+ * call's arena, and that calls `nish_runtime_root_keep` with every root table.
170
+ * The **kept region** is [floor, off): `floor` is the lowest mark a keep has
171
+ * seen, and nothing below it ever moves. While the region is at most
172
+ * `2 * live + 64 KiB` nothing is released. Past that, the graph is traced
173
+ * from the root slots, the blocks it reaches inside the region are slid down
174
+ * to `floor` in address order, every pointer to them is rewritten, and `off`
175
+ * is set past the last; `live` is what was kept. Everything the trace needs
176
+ * is scratch above `off`, which the next allocation reuses.
177
+ *
178
+ * A pointer outside the region (a string constant, memory below `floor`) is
179
+ * followed, so what it reaches is traced too, but it is never moved. The
180
+ * blocks are the compiler's: a string is `8 + len + 1` bytes, an object
181
+ * `sizeof` its struct, an array a 24-byte header and `cap` slots of data,
182
+ * each rounded to 8 as every allocation is. Overlapping and adjacent blocks
183
+ * merge into one extent, so a block reached twice, or through a pointer into
184
+ * its middle, moves once and keeps its shape. Each extent moves down by a
185
+ * multiple of 8, so nothing loses its alignment.
186
+ *
187
+ * The page can write every byte the trace reads (docs/security/wasm-host.md,
188
+ * WH-10), so the trace believes no pointer and no length it did not check.
189
+ * An object, an array's header and its data lie in the arena below `off`;
190
+ * a string lies there or in the static data, where the compiler puts its
191
+ * constants. An array's `len` is at most its `cap`, and `cap` slots fit
192
+ * before `off`; a string's bytes fit before the end of its area. Anything
193
+ * else panics (NISH_DIE, kind panic, so the page hears why) before the trace
194
+ * reads it, and nothing has moved: the rewrites come after the trace. The
195
+ * scratch lies above `off`, so no pointer the trace follows can reach it. */
196
+ #define NISH_TRACE_STRUCT 0
197
+ #define NISH_TRACE_STRING 1
198
+ #define NISH_TRACE_ARRAY 2
199
+ #define NISH_TRACE_UNION 3
200
+
201
+ /* `%nish.trace = type { i32, i32, i32, i32*, i8** }` (src/emit-root.ts). */
202
+ typedef struct nish_trace {
203
+ int32_t kind;
204
+ int32_t size;
205
+ int32_t count;
206
+ const int32_t *at;
207
+ const struct nish_trace *const *to;
208
+ } nish_trace;
209
+
210
+ /* `%nish.root = type { i8*, i32, i8* }`: `count` slots, each `desc` or null. */
211
+ typedef struct nish_root {
212
+ char **slots;
213
+ int32_t count;
214
+ const nish_trace *desc;
215
+ } nish_root;
216
+
217
+ /* The two layouts above are what src/emit-root.ts writes as wasm32 IR: three
218
+ * `i32`s and two 4-byte pointers, and a pointer, an `i32` and a pointer. A
219
+ * target with another pointer size would read every descriptor at the wrong
220
+ * offsets, so the build refuses it here rather than tracing garbage. */
221
+ _Static_assert(sizeof(void *) == 4, "root descriptors are emitted for wasm32's 4-byte pointers");
222
+ _Static_assert(sizeof(nish_trace) == 20, "%nish.trace layout is ABI: src/emit-root.ts");
223
+ _Static_assert(sizeof(nish_root) == 12, "%nish.root layout is ABI: src/emit-root.ts");
224
+
225
+ static const nish_root *const *nish_roots;
226
+ static int32_t nish_root_count;
227
+ static uint64_t nish_root_floor = (uint64_t)-1;
228
+ static uint64_t nish_root_live;
229
+
230
+ static void nish_roots_clear(void) {
231
+ for (int32_t r = 0; r < nish_root_count; r++) {
232
+ const nish_root *root = nish_roots[r];
233
+ for (int32_t i = 0; i < root->count; i++) root->slots[i] = 0;
234
+ }
235
+ nish_root_floor = (uint64_t)-1;
236
+ nish_root_live = 0;
237
+ }
238
+
239
+ /* Scratch: a bump allocator from the old `off` up, growing memory but never
240
+ `off`, so the call's own blocks below it are untouched. */
241
+ static uintptr_t nish_scratch_top;
242
+
243
+ static void *nish_scratch(uint64_t size) {
244
+ size = (size + 7) & ~(uint64_t)7;
245
+ uint64_t at = nish_scratch_top - (uintptr_t)nish_arena.buf;
246
+ uint64_t need = at + size;
247
+ if (need < size || need > (uintptr_t)-1 - (uintptr_t)nish_arena.buf) NISH_DIE(NISH_PANIC_MEMORY, -1, 0, 0, 0, 0);
248
+ if (need > nish_arena.cap) {
249
+ uint64_t pages = (need - nish_arena.cap + NISH_PAGE - 1) / NISH_PAGE;
250
+ if (__builtin_wasm_memory_grow(0, (uintptr_t)pages) == (uintptr_t)-1) NISH_DIE(NISH_PANIC_MEMORY, -1, 0, 0, 0, 0);
251
+ nish_arena.cap += pages * NISH_PAGE;
252
+ }
253
+ void *p = (void *)nish_scratch_top;
254
+ nish_scratch_top += (uintptr_t)size;
255
+ return p;
256
+ }
257
+
258
+ /* Chunks of scratch the trace is done with: the copy a vector or the seen set
259
+ grew out of, and, once the trace ends, the work list and the seen set. A
260
+ request takes the smallest chunk it fits and leaves the rest of it free;
261
+ only what none fits bumps `nish_scratch_top`. Without this the old copies
262
+ alone added the live slot and block lists over again. */
263
+ #define NISH_SCRATCH_FREE 64
264
+ static struct { uintptr_t at; uint64_t size; } nish_scratch_free[NISH_SCRATCH_FREE];
265
+ static uint32_t nish_scratch_nfree;
266
+
267
+ static void nish_scratch_give(void *p, uint64_t size) {
268
+ size &= ~(uint64_t)7;
269
+ if (!p || !size || nish_scratch_nfree == NISH_SCRATCH_FREE) return;
270
+ nish_scratch_free[nish_scratch_nfree].at = (uintptr_t)p;
271
+ nish_scratch_free[nish_scratch_nfree].size = size;
272
+ nish_scratch_nfree++;
273
+ }
274
+
275
+ static void *nish_scratch_take(uint64_t size) {
276
+ size = (size + 7) & ~(uint64_t)7;
277
+ uint32_t best = NISH_SCRATCH_FREE;
278
+ for (uint32_t i = 0; i < nish_scratch_nfree; i++)
279
+ if (nish_scratch_free[i].size >= size && (best == NISH_SCRATCH_FREE || nish_scratch_free[i].size < nish_scratch_free[best].size))
280
+ best = i;
281
+ if (best == NISH_SCRATCH_FREE) return nish_scratch(size);
282
+ void *p = (void *)nish_scratch_free[best].at;
283
+ nish_scratch_free[best].at += (uintptr_t)size;
284
+ nish_scratch_free[best].size -= size;
285
+ if (!nish_scratch_free[best].size) nish_scratch_free[best] = nish_scratch_free[--nish_scratch_nfree];
286
+ return p;
287
+ }
288
+
289
+ /* A growable vector of 32-bit words in scratch; a grown vector gives its old
290
+ copy back to the free chunks. */
291
+ typedef struct { uint32_t *at; uint32_t len; uint32_t cap; } nish_words;
292
+
293
+ static void nish_words_push(nish_words *v, uint32_t a, uint32_t b) {
294
+ if (v->len + 2 > v->cap) {
295
+ uint32_t cap = v->cap ? v->cap * 2 : 256;
296
+ uint32_t *at = (uint32_t *)nish_scratch_take((uint64_t)cap * 4);
297
+ if (v->len) __builtin_memcpy(at, v->at, (uintptr_t)v->len * 4);
298
+ nish_scratch_give(v->at, (uint64_t)v->cap * 4);
299
+ v->at = at;
300
+ v->cap = cap;
301
+ }
302
+ v->at[v->len] = a;
303
+ v->at[v->len + 1] = b;
304
+ v->len += 2;
305
+ }
306
+
307
+ /* The trace's state: the region, what it reached and what is left to visit. */
308
+ static struct {
309
+ uintptr_t lo, hi;
310
+ nish_words blocks; /* start, end: a block in the region */
311
+ nish_words slots; /* address, 0: a slot holding a pointer into the region */
312
+ nish_words work; /* object, descriptor: left to visit */
313
+ uint32_t *seen; /* open addressing on (object, descriptor) pairs */
314
+ uint32_t seen_cap, seen_len;
315
+ } nish_tr;
316
+
317
+ static uint32_t nish_seen_hash(uint32_t p, uint32_t d) {
318
+ uint32_t h = (p ^ (d * 0x9e3779b1u)) * 0x85ebca6bu;
319
+ return h ^ (h >> 15);
320
+ }
321
+
322
+ /* Whether (p, d) was new, adding it; the set doubles at half full. */
323
+ static int nish_seen_add(uint32_t p, uint32_t d) {
324
+ if (nish_tr.seen_len * 2 >= nish_tr.seen_cap) {
325
+ uint32_t *old = nish_tr.seen;
326
+ uint32_t old_cap = nish_tr.seen_cap;
327
+ nish_tr.seen_cap = old_cap ? old_cap * 2 : 1024;
328
+ nish_tr.seen = (uint32_t *)nish_scratch_take((uint64_t)nish_tr.seen_cap * 8);
329
+ __builtin_memset(nish_tr.seen, 0, (uintptr_t)nish_tr.seen_cap * 8);
330
+ nish_tr.seen_len = 0;
331
+ for (uint32_t i = 0; i < old_cap; i++)
332
+ if (old[2 * i]) nish_seen_add(old[2 * i], old[2 * i + 1]);
333
+ nish_scratch_give(old, (uint64_t)old_cap * 8);
334
+ }
335
+ uint32_t mask = nish_tr.seen_cap - 1;
336
+ for (uint32_t i = nish_seen_hash(p, d) & mask;; i = (i + 1) & mask) {
337
+ if (!nish_tr.seen[2 * i]) {
338
+ nish_tr.seen[2 * i] = p;
339
+ nish_tr.seen[2 * i + 1] = d;
340
+ nish_tr.seen_len++;
341
+ return 1;
342
+ }
343
+ if (nish_tr.seen[2 * i] == p && nish_tr.seen[2 * i + 1] == d) return 0;
344
+ }
345
+ }
346
+
347
+ static int nish_in_region(uintptr_t p) { return p >= nish_tr.lo && p < nish_tr.hi; }
348
+
349
+ /* The static data wasm-ld lays out: string constants live here. */
350
+ extern unsigned char __global_base;
351
+ extern unsigned char __data_end;
352
+
353
+ #define NISH_FORGED_POINTER "root storage: a pointer in the kept graph names memory outside the arena"
354
+ #define NISH_FORGED_LENGTH "root storage: a length in the kept graph runs past its block"
355
+ #define NISH_FORGED(msg) NISH_DIE(NISH_PANIC_MESSAGE, -1, (msg), (uint32_t)(sizeof(msg) - 1), 0, 0)
356
+
357
+ /* The end of the area `p` may lie in and `size` bytes from it fit before:
358
+ the arena [buf, off), or for a string the static data too; 0 for neither.
359
+ The checks subtract, so no forged size wraps a sum. */
360
+ static uintptr_t nish_area_end(uintptr_t p, uint64_t size, int string) {
361
+ uintptr_t end = 0;
362
+ if (p >= (uintptr_t)nish_arena.buf && p <= nish_tr.hi) end = nish_tr.hi;
363
+ else if (string && p >= (uintptr_t)&__global_base && p <= (uintptr_t)&__data_end) end = (uintptr_t)&__data_end;
364
+ return end && size <= (uint64_t)(end - p) ? end : 0;
365
+ }
366
+
367
+ static void nish_block(uintptr_t p, uint64_t size) {
368
+ if (!size || !nish_in_region(p)) return;
369
+ uint64_t end = (uint64_t)p + ((size + 7) & ~(uint64_t)7);
370
+ if (end > nish_tr.hi) end = nish_tr.hi; /* a block is never past `off` */
371
+ nish_words_push(&nish_tr.blocks, (uint32_t)p, (uint32_t)end);
372
+ }
373
+
374
+ /* The pointer slot `s`, which holds an object of descriptor `d` or null. */
375
+ static void nish_slot(char **s, const nish_trace *d) {
376
+ uintptr_t p = (uintptr_t)*s;
377
+ if (!p) return;
378
+ /* The fixed part of the block: a string's length word, an array's header,
379
+ a union's tag (its arm is checked once the tag is read). */
380
+ uint64_t head = d->kind == NISH_TRACE_STRING ? 8 : d->kind == NISH_TRACE_ARRAY ? sizeof(nish_array)
381
+ : d->kind == NISH_TRACE_UNION ? 4 : (uint64_t)d->size;
382
+ if (!nish_area_end(p, head, d->kind == NISH_TRACE_STRING)) NISH_FORGED(NISH_FORGED_POINTER);
383
+ if (nish_in_region(p)) nish_words_push(&nish_tr.slots, (uint32_t)(uintptr_t)s, 0);
384
+ if (nish_seen_add((uint32_t)p, (uint32_t)(uintptr_t)d)) nish_words_push(&nish_tr.work, (uint32_t)p, (uint32_t)(uintptr_t)d);
385
+ }
386
+
387
+ static void nish_fields(char *p, const nish_trace *d) {
388
+ for (int32_t i = 0; i < d->count; i++) nish_slot((char **)(p + d->at[i]), d->to[i]);
389
+ }
390
+
391
+ static void nish_visit(char *p, const nish_trace *d) {
392
+ switch (d->kind) {
393
+ case NISH_TRACE_STRING: {
394
+ uint64_t len;
395
+ __builtin_memcpy(&len, p, 8);
396
+ uintptr_t end = nish_area_end((uintptr_t)p, 0, 1);
397
+ if (len > (uint64_t)(end - (uintptr_t)p) - 8) NISH_FORGED(NISH_FORGED_LENGTH);
398
+ nish_block((uintptr_t)p, 8 + len + 1);
399
+ break;
400
+ }
401
+ case NISH_TRACE_STRUCT:
402
+ nish_block((uintptr_t)p, (uint64_t)d->size);
403
+ nish_fields(p, d);
404
+ break;
405
+ case NISH_TRACE_UNION: {
406
+ int32_t tag;
407
+ __builtin_memcpy(&tag, p, 4);
408
+ for (int32_t i = 0; i < d->count; i++) {
409
+ if (d->at[i] == tag) {
410
+ if (!nish_area_end((uintptr_t)p, (uint64_t)d->to[i]->size, 0)) NISH_FORGED(NISH_FORGED_LENGTH);
411
+ nish_block((uintptr_t)p, (uint64_t)d->to[i]->size);
412
+ nish_fields(p, d->to[i]);
413
+ break;
414
+ }
415
+ }
416
+ break;
417
+ }
418
+ case NISH_TRACE_ARRAY: {
419
+ nish_array *a = (nish_array *)p;
420
+ nish_block((uintptr_t)p, sizeof *a);
421
+ /* `len <= cap` and `cap * size <= off - data`, by division, so a forged
422
+ `cap` cannot wrap the product and a forged `len` walks no slot the
423
+ data does not hold. */
424
+ if (a->len > a->cap) NISH_FORGED(NISH_FORGED_LENGTH);
425
+ if (!a->data) {
426
+ if (a->cap) NISH_FORGED(NISH_FORGED_POINTER);
427
+ break;
428
+ }
429
+ if (!nish_area_end((uintptr_t)a->data, 0, 0)) NISH_FORGED(NISH_FORGED_POINTER);
430
+ if (d->size > 0 && a->cap > (uint64_t)(nish_tr.hi - (uintptr_t)a->data) / (uint64_t)d->size) NISH_FORGED(NISH_FORGED_LENGTH);
431
+ if (nish_in_region((uintptr_t)a->data)) nish_words_push(&nish_tr.slots, (uint32_t)(uintptr_t)&a->data, 0);
432
+ nish_block((uintptr_t)a->data, a->cap * (uint64_t)d->size);
433
+ if (d->count == 1) {
434
+ for (uint64_t i = 0; i < a->len; i++) nish_slot((char **)(a->data + i * (uint64_t)d->size), d->to[0]);
435
+ } else if (d->count == 2) {
436
+ for (uint64_t i = 0; i < a->len; i++) nish_fields(a->data + i * (uint64_t)d->size, d->to[0]);
437
+ }
438
+ break;
439
+ }
440
+ }
441
+ }
442
+
443
+ /* Heapsort of `n` pairs by their first word: no recursion, no scratch. */
444
+ static void nish_sift(uint32_t *v, uint32_t at, uint32_t n) {
445
+ for (;;) {
446
+ uint32_t big = at, l = 2 * at + 1, r = l + 1;
447
+ if (l < n && v[2 * l] > v[2 * big]) big = l;
448
+ if (r < n && v[2 * r] > v[2 * big]) big = r;
449
+ if (big == at) return;
450
+ uint32_t a = v[2 * at], b = v[2 * at + 1];
451
+ v[2 * at] = v[2 * big];
452
+ v[2 * at + 1] = v[2 * big + 1];
453
+ v[2 * big] = a;
454
+ v[2 * big + 1] = b;
455
+ at = big;
456
+ }
457
+ }
458
+
459
+ static void nish_sort_pairs(uint32_t *v, uint32_t n) {
460
+ for (uint32_t i = n / 2; i-- > 0;) nish_sift(v, i, n);
461
+ while (n > 1) {
462
+ n--;
463
+ uint32_t a = v[0], b = v[1];
464
+ v[0] = v[2 * n];
465
+ v[1] = v[2 * n + 1];
466
+ v[2 * n] = a;
467
+ v[2 * n + 1] = b;
468
+ nish_sift(v, 0, n);
469
+ }
470
+ }
471
+
472
+ /* Where the region address `p` lands: inside an extent, at its new place;
473
+ in a gap, at the end of the extent before it, which is where the gap's
474
+ bytes went (a pointer past a block's end, as an empty array's data can be). */
475
+ static uint32_t nish_moved(const uint32_t *ext, uint32_t n, uint32_t p) {
476
+ uint32_t lo = 0, hi = n; /* the first extent starting after p */
477
+ while (lo < hi) {
478
+ uint32_t mid = (lo + hi) / 2;
479
+ if (ext[3 * mid] <= p) lo = mid + 1;
480
+ else hi = mid;
481
+ }
482
+ if (lo == 0) return (uint32_t)nish_tr.lo;
483
+ const uint32_t *e = ext + 3 * (lo - 1);
484
+ return p < e[1] ? e[2] + (p - e[0]) : e[2] + (e[1] - e[0]);
485
+ }
486
+
487
+ static void nish_root_compact(void) {
488
+ nish_tr.lo = (uintptr_t)nish_arena.buf + (uintptr_t)nish_root_floor;
489
+ nish_tr.hi = (uintptr_t)nish_arena.buf + (uintptr_t)nish_arena.off;
490
+ nish_scratch_top = (nish_tr.hi + 7) & ~(uintptr_t)7;
491
+ nish_tr.blocks.len = nish_tr.blocks.cap = 0;
492
+ nish_tr.slots.len = nish_tr.slots.cap = 0;
493
+ nish_tr.work.len = nish_tr.work.cap = 0;
494
+ nish_tr.seen = 0;
495
+ nish_tr.seen_cap = nish_tr.seen_len = 0;
496
+ nish_scratch_nfree = 0;
497
+ for (int32_t r = 0; r < nish_root_count; r++) {
498
+ const nish_root *root = nish_roots[r];
499
+ for (int32_t i = 0; i < root->count; i++) nish_slot(&root->slots[i], root->desc);
500
+ }
501
+ while (nish_tr.work.len) {
502
+ nish_tr.work.len -= 2;
503
+ nish_visit((char *)(uintptr_t)nish_tr.work.at[nish_tr.work.len],
504
+ (const nish_trace *)(uintptr_t)nish_tr.work.at[nish_tr.work.len + 1]);
505
+ }
506
+ /* Blocks into extents (start, end, new start). An extent takes three words
507
+ where a block took two, so the extents go in a chunk of their own, which
508
+ may be the work list or the seen set the trace no longer needs. */
509
+ uint32_t nb = nish_tr.blocks.len / 2;
510
+ nish_sort_pairs(nish_tr.blocks.at, nb);
511
+ nish_scratch_give(nish_tr.work.at, (uint64_t)nish_tr.work.cap * 4);
512
+ nish_scratch_give(nish_tr.seen, (uint64_t)nish_tr.seen_cap * 8);
513
+ uint32_t *ext = (uint32_t *)nish_scratch_take((uint64_t)nb * 12 + 12);
514
+ uint32_t ne = 0, to = (uint32_t)nish_tr.lo;
515
+ for (uint32_t i = 0; i < nb; i++) {
516
+ uint32_t start = nish_tr.blocks.at[2 * i], end = nish_tr.blocks.at[2 * i + 1];
517
+ if (ne && start <= ext[3 * (ne - 1) + 1]) {
518
+ uint32_t *e = ext + 3 * (ne - 1);
519
+ if (end > e[1]) {
520
+ to += end - e[1];
521
+ e[1] = end;
522
+ }
523
+ continue;
524
+ }
525
+ ext[3 * ne] = start;
526
+ ext[3 * ne + 1] = end;
527
+ ext[3 * ne + 2] = to;
528
+ to += end - start;
529
+ ne++;
530
+ }
531
+ /* Every slot once: rewriting one twice would move its pointer twice. */
532
+ uint32_t ns = nish_tr.slots.len / 2;
533
+ nish_sort_pairs(nish_tr.slots.at, ns);
534
+ for (uint32_t i = 0; i < ns; i++) {
535
+ uint32_t s = nish_tr.slots.at[2 * i];
536
+ if (i && s == nish_tr.slots.at[2 * (i - 1)]) continue;
537
+ char **slot = (char **)(uintptr_t)s;
538
+ *slot = (char *)(uintptr_t)nish_moved(ext, ne, (uint32_t)(uintptr_t)*slot);
539
+ }
540
+ /* Down, in address order: each extent's new place is at or below its old
541
+ one and above every earlier extent's, so a forward move is safe. */
542
+ for (uint32_t i = 0; i < ne; i++) {
543
+ uint32_t *e = ext + 3 * i;
544
+ if (e[2] != e[0]) __builtin_memmove((char *)(uintptr_t)e[2], (char *)(uintptr_t)e[0], (uintptr_t)(e[1] - e[0]));
545
+ }
546
+ nish_root_live = (uint64_t)(to - (uint32_t)nish_tr.lo);
547
+ nish_arena.off = (uint64_t)((uintptr_t)to - (uintptr_t)nish_arena.buf);
548
+ }
549
+
550
+ /* Root storage's nish_reset_arena and nish_free_arena (WP55 §4.2): a root
551
+ left pointing at a released block would be traced into whatever lands
552
+ there, so every root is emptied before the arena. */
553
+ void nish_runtime_root_reset(const nish_root *const *roots, int32_t count) {
554
+ nish_roots = roots;
555
+ nish_root_count = count;
556
+ nish_roots_clear();
557
+ nish_arena.off = 0;
558
+ }
559
+
560
+ /* After an export call that began at `mark`: keep what `roots` reach. */
561
+ void nish_runtime_root_keep(uint64_t mark, const nish_root *const *roots, int32_t count) {
562
+ nish_roots = roots;
563
+ nish_root_count = count;
564
+ if (!nish_arena.buf) return;
565
+ uint64_t at = mark ? mark - (uintptr_t)nish_arena.buf : 0;
566
+ if (at < nish_root_floor) nish_root_floor = at;
567
+ if (nish_arena.off - nish_root_floor <= 2 * nish_root_live + NISH_PAGE) return;
568
+ nish_root_compact();
569
+ }
570
+ #endif /* NISH_WASM_ROOTS */
571
+
150
572
  /* ---- Panics: `unreachable`, which the host sees as a RuntimeError; under
151
573
  * wasm-host the page hears of each first (NISH_DIE above). */
152
574
  #ifdef NISH_WASM_HOST
package/runtime/vite.mjs CHANGED
@@ -118,7 +118,7 @@ export default function nish(options = {}) {
118
118
  );
119
119
  throw new Error(
120
120
  `nish/vite: the compiler ${compiler} (${version}) wrote a loader with no \`loadSync\` export: ` +
121
- "it predates `loadSync` (WP50 stage 1), which the generated module calls. " +
121
+ "it predates `loadSync` (nish 0.36.0), which the generated module calls. " +
122
122
  "Use a newer one: update the nish package, or pass its path as `nish({ compiler })`.",
123
123
  );
124
124
  }
package/scripts/build.sh CHANGED
@@ -3,7 +3,7 @@
3
3
  #
4
4
  # scripts/build.sh <module.ll> [more .ll/.c files...] -o <out> [--profile debug|speed|size|wasm|wasm-host|wasi|napi]
5
5
  #
6
- # The C runtime is seventeen translation units and is named as one: an input
6
+ # The C runtime is eighteen translation units and is named as one: an input
7
7
  # <dir>/runtime.c also compiles <dir>/runtime-text.c, the strings and the digits
8
8
  # of numbers, <dir>/runtime-os.c, the half that wraps the
9
9
  # system calls (files, directories, subprocesses, the environment, the clock),
@@ -11,6 +11,7 @@
11
11
  # threads, <dir>/runtime-cross.c, the copies that carry a string or an array
12
12
  # from one thread's arena to another's, <dir>/runtime-host.c, the wall clock, entropy, file times and
13
13
  # signals, <dir>/runtime-net.c, the sockets of `nish:net`,
14
+ # <dir>/runtime-uring.c, the io_uring rings that will hold them past RLIMIT_NOFILE,
14
15
  # <dir>/runtime-simd.c, the byte searches that read a vector at a time and
15
16
  # the SSE4.1 rounding of `Math.floor` and its kin,
16
17
  # <dir>/runtime-aes.c, AES-GCM on the CPU's AES instructions,
@@ -127,7 +128,7 @@
127
128
  #
128
129
  # The runtime compiled once (the debug, speed and size profiles only; `nish
129
130
  # --link` and `nish run` use these through their runtime cache, src/compile.ts):
130
- # --runtime-objects <dir> compile the runtime.c input, its sixteen halves and runtime-async.c,
131
+ # --runtime-objects <dir> compile the runtime.c input, its seventeen halves and runtime-async.c,
131
132
  # one $CC -c each and all at once, into
132
133
  # <dir>/<unit>.o with the profile's compile flags,
133
134
  # and link nothing. speed and size store LTO bitcode,
@@ -272,9 +273,9 @@ if [ -n "$sqlite_to" ] || [ -n "$sqlite_from" ]; then
272
273
  fi
273
274
  [ -n "$out" ] || [ -n "$objects_to" ] || [ -n "$sqlite_to" ] || { echo "error: -o <out> is required" >&2; exit 2; }
274
275
 
275
- # The runtime is seventeen translation units, and a caller names one: whoever passes
276
+ # The runtime is eighteen translation units, and a caller names one: whoever passes
276
277
  # <dir>/runtime.c gets <dir>/runtime-text.c, <dir>/runtime-os.c, <dir>/runtime-parallel.c,
277
- # <dir>/runtime-cross.c, <dir>/runtime-host.c, <dir>/runtime-net.c, <dir>/runtime-simd.c,
278
+ # <dir>/runtime-cross.c, <dir>/runtime-host.c, <dir>/runtime-net.c, <dir>/runtime-uring.c, <dir>/runtime-simd.c,
278
279
  # <dir>/runtime-aes.c, <dir>/runtime-sha256.c, <dir>/runtime-chacha20.c, <dir>/runtime-checksum.c, <dir>/runtime-term.c,
279
280
  # <dir>/runtime-bytes.c, <dir>/runtime-sandbox.c, <dir>/runtime-shm.c and <dir>/runtime-region.c compiled beside it, on every profile,
280
281
  # wasi included: runtime-simd.c and runtime-bytes.c take their scalar paths there
@@ -283,7 +284,7 @@ fi
283
284
  # stubs that nothing calls, because the compiler emits no call to their kernels for wasm32, and
284
285
  # runtime-term.c reads stdin and answers "not a terminal", and runtime-sandbox.c
285
286
  # compiles to two functions that refuse to run, which no wasm build calls, and
286
- # runtime-shm.c answers -95 for every region. They were one file until the
287
+ # runtime-shm.c answers -95 for every region, and runtime-uring.c compiles to nothing. They were one file until the
287
288
  # operating-system half was split out for its own size budget, and the parallel
288
289
  # and text halves followed for the same reason (each file's header comment says why), and
289
290
  # a link line is where those splits
@@ -293,7 +294,7 @@ fi
293
294
  # here keeps every one of those correct, and keeps "the runtime" one thing to
294
295
  # name from the outside. A caller that names one itself is left alone, because
295
296
  # naming one object twice is a duplicate-symbol error.
296
- halves="runtime-text.c runtime-os.c runtime-parallel.c runtime-cross.c runtime-host.c runtime-net.c runtime-simd.c runtime-aes.c runtime-sha256.c runtime-chacha20.c runtime-checksum.c runtime-term.c runtime-bytes.c runtime-sandbox.c runtime-shm.c runtime-region.c"
297
+ halves="runtime-text.c runtime-os.c runtime-parallel.c runtime-cross.c runtime-host.c runtime-net.c runtime-uring.c runtime-simd.c runtime-aes.c runtime-sha256.c runtime-chacha20.c runtime-checksum.c runtime-term.c runtime-bytes.c runtime-sandbox.c runtime-shm.c runtime-region.c"
297
298
  for i in ${inputs[@]+"${inputs[@]}"}; do
298
299
  case "$i" in
299
300
  */runtime.c|runtime.c)
@@ -889,12 +890,27 @@ case "$profile" in
889
890
  esac
890
891
  done
891
892
  fi
893
+ # Root storage (WP55): a module with root storage defines its own
894
+ # `nish_reset_arena`, which empties every root through the runtime's
895
+ # nish_runtime_root_reset. Its IR links runtime-wasm.c, compiled with
896
+ # NISH_WASM_ROOTS, which takes in the root keep and leaves the arena resets
897
+ # to the IR. The definition is the trigger: a bare `declare` of the keep
898
+ # (every `--runtime-decls` module has one) or a string spelling a name is
899
+ # not. Without it the module links the bytes it always did: the wasm
900
+ # profile exports every function it compiles.
901
+ roots_wanted=0
902
+ for i in ${inputs[@]+"${inputs[@]}"}; do
903
+ case "$i" in
904
+ *.ll) if grep -qE '^define void @nish_reset_arena\(' "$i"; then roots_wanted=1; round_wanted=1; fi ;;
905
+ esac
906
+ done
892
907
  if [ "$round_wanted" = 1 ] && [ "$wasm_named" = 0 ]; then
893
908
  inputs+=("$(cd "$(dirname "$0")/../runtime" && pwd)/runtime-wasm.c")
894
909
  fi
895
910
  # -mbulk-memory lowers llvm.memset/memcpy (`new Array<T>(n)`, `push` growth) to the
896
911
  # memory.fill/memory.copy instructions instead of libc calls the freestanding link lacks.
897
912
  wasm_compile=(-Wno-override-module --target=wasm32-unknown-unknown -Oz -nostdlib -mbulk-memory -ffp-contract=off)
913
+ if [ "$roots_wanted" = 1 ]; then wasm_compile+=(-DNISH_WASM_ROOTS); fi
898
914
  if [ "$profile" = wasm ]; then
899
915
  "$CC" "${wasm_compile[@]}" \
900
916
  ${tls[@]+"${tls[@]}"} \