@amritk/nish-x86_64-linux 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 (103) hide show
  1. package/INSTALL.md +92 -71
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +75 -14
  4. package/package.json +1 -1
  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/std/LICENSE-fdlibm +8 -0
  22. package/std/README.md +109 -82
  23. package/std/array.ts +266 -0
  24. package/std/bytes.ts +23 -19
  25. package/std/compress/checksum.ts +1 -1
  26. package/std/crypto/aes.ts +48 -43
  27. package/std/crypto/base64url.ts +7 -5
  28. package/std/crypto/bcrypt-batch.ts +13 -13
  29. package/std/crypto/bcrypt.ts +33 -27
  30. package/std/crypto/chacha20poly1305.ts +16 -14
  31. package/std/crypto/ed25519.ts +19 -16
  32. package/std/crypto/hmac.ts +7 -6
  33. package/std/crypto/jwt.ts +23 -20
  34. package/std/crypto/p256.ts +63 -56
  35. package/std/crypto/p384.ts +23 -20
  36. package/std/crypto/pki.ts +18 -15
  37. package/std/crypto/rsa.ts +23 -19
  38. package/std/crypto/x25519.ts +11 -10
  39. package/std/crypto/x509.ts +69 -62
  40. package/std/host/ring.ts +124 -0
  41. package/std/host/text.ts +86 -0
  42. package/std/json-equal.ts +12 -11
  43. package/std/json-number.ts +12 -12
  44. package/std/json-tokenizer.ts +26 -22
  45. package/std/json-typed.ts +92 -78
  46. package/std/json-value.ts +38 -33
  47. package/std/json.ts +32 -28
  48. package/std/math.ts +743 -41
  49. package/std/net/dns-resolver.ts +46 -6
  50. package/std/net/dns.ts +50 -13
  51. package/std/net/fetch.ts +1744 -295
  52. package/std/net/handler.ts +30 -0
  53. package/std/net/hpack.ts +29 -26
  54. package/std/net/http-compress.ts +42 -28
  55. package/std/net/http-fields.ts +19 -4
  56. package/std/net/http1-client-codec.ts +29 -25
  57. package/std/net/http1-codec.ts +62 -91
  58. package/std/net/http1-server.ts +79 -57
  59. package/std/net/http2-frame.ts +17 -16
  60. package/std/net/http2-tls.ts +17 -15
  61. package/std/net/http2.ts +67 -54
  62. package/std/net/http3-frame.ts +11 -10
  63. package/std/net/http3-server.ts +36 -35
  64. package/std/net/http3.ts +70 -58
  65. package/std/net/postgres-async.ts +1005 -0
  66. package/std/net/postgres.ts +185 -164
  67. package/std/net/prefork.ts +38 -34
  68. package/std/net/qpack.ts +15 -14
  69. package/std/net/quic-conn-ack.ts +1 -0
  70. package/std/net/quic-conn-cid.ts +11 -0
  71. package/std/net/quic-conn-params.ts +41 -14
  72. package/std/net/quic-datagram.ts +7 -0
  73. package/std/net/quic-frame.ts +48 -12
  74. package/std/net/quic-listener.ts +61 -44
  75. package/std/net/quic-packet.ts +71 -52
  76. package/std/net/quic-recovery.ts +59 -21
  77. package/std/net/quic-stream.ts +83 -55
  78. package/std/net/quic.ts +204 -152
  79. package/std/net/serve.ts +763 -0
  80. package/std/net/tls/client.ts +1 -0
  81. package/std/net/tls/codec.ts +81 -12
  82. package/std/net/tls/record-client.ts +30 -18
  83. package/std/net/tls/record-server.ts +74 -46
  84. package/std/net/tls/record.ts +33 -24
  85. package/std/net/tls/ticket.ts +2 -0
  86. package/std/net/tls/verify.ts +8 -7
  87. package/std/net/tls-tcp.ts +58 -50
  88. package/std/net/tls.ts +55 -43
  89. package/std/net/url.ts +12 -9
  90. package/std/net/websocket.ts +23 -19
  91. package/std/net/webtransport.ts +24 -18
  92. package/std/random.ts +8 -6
  93. package/std/regex.ts +70 -59
  94. package/std/sync/ring.ts +24 -21
  95. package/std/sync/shm.ts +9 -7
  96. package/std/term-io.ts +17 -14
  97. package/std/term-probe.ts +11 -10
  98. package/std/term-rate.ts +71 -61
  99. package/std/term.ts +73 -59
  100. package/std/text.ts +242 -15
  101. package/std/threads.ts +19 -15
  102. package/std/yaml-json.ts +17 -15
  103. package/std/yaml-write.ts +21 -14
package/runtime/nish.h CHANGED
@@ -4,7 +4,8 @@
4
4
  * runtime next to compiled Nish modules. Everything here is a contract shared
5
5
  * with `src/runtime.ts` (the IR side) and the implementation, which is
6
6
  * several translation units: `runtime/runtime.c` holds the core every program
7
- * touches — the arena, strings, arrays, number formatting, the panics —
7
+ * touches — the arena, arrays, the panics — `runtime/runtime-text.c` holds
8
+ * strings and number formatting (freestanding, so a wasm module links it too),
8
9
  * `runtime/runtime-os.c` holds everything that wraps a system call: the file
9
10
  * functions, the directory and subprocess calls, `nish_getenv`, the monotonic
10
11
  * clock, `nish_platform` / `nish_arch` — and runtime-parallel.c, runtime-host.c,
@@ -51,7 +52,8 @@ extern "C" {
51
52
  * ELF refuses to link that against a non-TLS definition. `scripts/build.sh
52
53
  * --threads` passes the macro to every input, which is how `nish --threads
53
54
  * --link` keeps the two halves in step. The same definition is in
54
- * runtime/runtime.c and runtime/runtime-wasm.c. */
55
+ * runtime/runtime.c and runtime/runtime-wasm.c, and runtime/runtime-text.c
56
+ * repeats it for its inlined allocation fast path. */
55
57
  #ifdef NISH_THREADS
56
58
  #define NISH_TLS _Thread_local
57
59
  #else
@@ -94,7 +96,8 @@ static inline __attribute__((unused)) const char *nish_cpath(const nish_str *s)
94
96
  * `off` and `cap` are `uint64_t` and not `size_t` because that `i64` is what
95
97
  * the inlined fast path bumps and compares on every target: under wasm32 a
96
98
  * `size_t` pair would put them at bytes 4 and 8 while compiled code reads 8
97
- * and 16. runtime.c and runtime-wasm.c static-assert these offsets. */
99
+ * and 16. runtime.c and runtime-wasm.c static-assert these offsets, and
100
+ * runtime-text.c, which keeps its own copy of the struct, asserts all four. */
98
101
  struct nish_arena {
99
102
  char *buf;
100
103
  uint64_t off;
@@ -146,12 +149,56 @@ uint64_t nish_arena_used(void);
146
149
  * already entered panics. `nish_region_leave` must name the region entered
147
150
  * last on this thread, puts back the arena its enter displaced and records
148
151
  * `root`. `nish_region_free` frees every chunk of a region that is not
149
- * entered and makes its handle stale. The wasm profile has none of these
150
- * (scripts/build.sh refuses IR that names them); wasi has. */
152
+ * entered and makes its handle stale. `nish_region_adopt` instead moves the
153
+ * chunks of a region that is not entered onto the thread's current arena, so
154
+ * they live on until that arena releases below them, and makes its handle
155
+ * stale: an `awaitAll` lane's region, once the lane has answered. The wasm
156
+ * profile has none of these (scripts/build.sh refuses IR that names them);
157
+ * wasi has. */
151
158
  uint64_t nish_region_new(void);
152
159
  void *nish_region_enter(uint64_t r);
153
160
  void nish_region_leave(uint64_t r, void *root);
154
161
  void nish_region_free(uint64_t r);
162
+ void nish_region_adopt(uint64_t r);
163
+
164
+ /* ---- The async executor (runtime-async.c) --------------------------------
165
+ * Linked only into a program whose IR names `@nish_async_`
166
+ * (docs/wp49-async-server.md, "Stage 5, part 1"). `nish_async_spawn` answers
167
+ * a node for a task of `kind` whose frame is `bytes` long, the frame 32 bytes
168
+ * in, with a region of its own; `nish_async_enter` makes a node the running
169
+ * task and enters its region, answering the task it displaced, which
170
+ * `nish_async_leave` restores; `nish_async_done` frees a finished task's
171
+ * region and reuses its node. `nish_async_wait_fd` (`dir` 0 readable, 1
172
+ * writable) and `nish_async_sleep` answer a token `nish_async_poll` reads: -1
173
+ * pending, 1 ready, 0 failed. `nish_async_next` answers the next task to
174
+ * resume, blocking in the kernel until one is ready, or null when nothing is
175
+ * ready and nothing waits. `nish_async_stat` answers 0: nodes allocated, 1:
176
+ * completions dropped because their task had finished, 2: spawned tasks not yet
177
+ * finished.
178
+ *
179
+ * Cancellation is cooperative. A task's handle (`nish_async_task_id`) is its
180
+ * slot and generation, and `main`'s is 0. `nish_async_cancel` flags a task and
181
+ * fires the waits it is parked on, which then read 0, as does every later wait
182
+ * it makes; it answers 1 when it flagged a live task other than `main`.
183
+ * `nish_async_cancelled` answers 1 inside a flagged task, and
184
+ * `nish_async_cancel_after` arms a timer that cancels the task in `ms`.
185
+ * `nish_async_wait_fd_within` is `nish_async_wait_fd` that reads 0 when `ms`
186
+ * pass first. */
187
+ void *nish_async_spawn(int32_t kind, uint64_t bytes);
188
+ void *nish_async_enter(void *node);
189
+ void nish_async_leave(void *node, void *prev);
190
+ void nish_async_done(void *node);
191
+ uint64_t nish_async_wait_fd(int32_t fd, int32_t dir);
192
+ uint64_t nish_async_sleep(int32_t ms);
193
+ int32_t nish_async_poll(uint64_t token);
194
+ void *nish_async_next(void);
195
+ void *nish_async_copy_str(const void *s);
196
+ int32_t nish_async_stat(int32_t which);
197
+ uint64_t nish_async_wait_fd_within(int32_t fd, int32_t dir, int32_t ms);
198
+ int64_t nish_async_task_id(void);
199
+ int32_t nish_async_cancel(int64_t handle);
200
+ int32_t nish_async_cancelled(void);
201
+ int32_t nish_async_cancel_after(int64_t handle, int32_t ms);
155
202
 
156
203
  /* ---- String operations ------------------------------------------------- */
157
204
  /* Copy `len` bytes into a new arena string (`bytes` need not be terminated). */
@@ -223,7 +270,7 @@ nish_str *nish_str_from_i32(int32_t v);
223
270
  nish_str *nish_str_from_f64(double v);
224
271
  /* The bytes `nish_str_from_f64` would hold, written into `out` with no NUL;
225
272
  returns the length. 32 bytes is room to spare: the longest spelling,
226
- -0.0000012345678901234567, is 25 (runtime.c says why, and runtime-test.c
273
+ -0.0000012345678901234567, is 25 (runtime-text.c says why, and runtime-test.c
227
274
  pins it). For a caller that writes numbers into its own buffer. */
228
275
  #define NISH_F64_MAX 32
229
276
  int nish_f64_format(double v, char out[NISH_F64_MAX]);
@@ -0,0 +1,53 @@
1
+ /**
2
+ * `nish/page`: the page-side helpers for a wasm module built with
3
+ * `--profile wasm-host` (docs/guide/using-nish/interop.md).
4
+ */
5
+
6
+ /**
7
+ * The objects a host module hands the program as `HostHandle<"kind">` values.
8
+ * Slot 0 is "none" and is never handed out; `get` and `take` throw a
9
+ * RangeError on a handle that names no object.
10
+ */
11
+ export declare class HostTable<T> {
12
+ /** Stores `value` and answers its handle, never 0, typed as the handle the caller returns. */
13
+ add<H extends number = number>(value: T): H;
14
+ /** The object `handle` names. */
15
+ get(handle: number): T;
16
+ /** Removes the object `handle` names and answers it. */
17
+ take(handle: number): T;
18
+ /** How many objects the table holds. */
19
+ readonly size: number;
20
+ }
21
+
22
+ /** A `layout` entry the loader answers for module storage. */
23
+ export interface RingField {
24
+ readonly at: number;
25
+ readonly length: number;
26
+ }
27
+
28
+ /** The page's half of a `nish/host/ring` ring. */
29
+ export interface Ring {
30
+ /** The number of slots, the storage's length less two. */
31
+ readonly capacity: number;
32
+ /** The words put and not yet taken. */
33
+ size(): number;
34
+ /** The words `push` would still take. */
35
+ free(): number;
36
+ /** Puts `word` (an i32) at the tail; false, and nothing changed, when full. */
37
+ push(word: number): boolean;
38
+ /** Takes the word at the head; 0, and nothing changed, when empty. */
39
+ shift(): number;
40
+ /** Empties the ring. */
41
+ clear(): void;
42
+ }
43
+
44
+ /** The ring held in module storage `field` of the loaded module's `memory`. */
45
+ export declare function ringOver(memory: { readonly buffer: ArrayBuffer }, field: RingField): Ring;
46
+
47
+ /** A panic as the loader reports it. */
48
+ export interface PagePanic {
49
+ readonly message: string;
50
+ }
51
+
52
+ /** The default `onPanic`: `console.error("nish: " + panic.message)`. */
53
+ export declare function reportPanic(panic: PagePanic): void;
@@ -0,0 +1,146 @@
1
+ // nish/page: the page-side helpers for a wasm module built with
2
+ // `--profile wasm-host` (docs/wp50-wasm-host.md §11): the typed handle table a
3
+ // host module keeps its objects in, the page's half of `nish/host/ring`, and
4
+ // the default `onPanic` reporter. Plain ECMAScript with no imports, so a
5
+ // browser loads it as it is.
6
+
7
+ /** What a slot holds when no object is in it: the free list is never searched. */
8
+ const EMPTY = Symbol("empty");
9
+
10
+ /**
11
+ * The objects a host module hands the program as `HostHandle<"kind">`
12
+ * values: `add` answers a handle, `get` reads it and `take` removes it. Slot 0
13
+ * is "none" and is never handed out, and a handle that names no object (0, a
14
+ * taken slot, a number no `add` answered) throws a RangeError rather than
15
+ * answering `undefined`: thrown inside an import, that ends the module, which
16
+ * is what a program holding a forged handle has earned. A taken slot is handed
17
+ * out again by a later `add`, so a stale handle the program kept names the
18
+ * newer object; it can reach only what the table holds.
19
+ */
20
+ export class HostTable {
21
+ #slots = [EMPTY];
22
+ #free = [];
23
+
24
+ /** Stores `value` and answers its handle, never 0. */
25
+ add(value) {
26
+ const reused = this.#free.pop();
27
+ if (reused !== undefined) {
28
+ this.#slots[reused] = value;
29
+ return reused;
30
+ }
31
+ this.#slots.push(value);
32
+ return this.#slots.length - 1;
33
+ }
34
+
35
+ /** The object `handle` names. */
36
+ get(handle) {
37
+ return this.#slots[this.#check(handle)];
38
+ }
39
+
40
+ /** Removes the object `handle` names and answers it; the handle names nothing after. */
41
+ take(handle) {
42
+ const at = this.#check(handle);
43
+ const value = this.#slots[at];
44
+ this.#slots[at] = EMPTY;
45
+ this.#free.push(at);
46
+ return value;
47
+ }
48
+
49
+ /** How many objects the table holds. */
50
+ get size() {
51
+ return this.#slots.length - 1 - this.#free.length;
52
+ }
53
+
54
+ #check(handle) {
55
+ if (
56
+ !Number.isInteger(handle) ||
57
+ handle <= 0 ||
58
+ handle >= this.#slots.length ||
59
+ this.#slots[handle] === EMPTY
60
+ ) {
61
+ throw new RangeError(`nish: no host object has handle ${handle}`);
62
+ }
63
+ return handle;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * The page's half of a `nish/host/ring` ring held in module storage: `memory`
69
+ * is the loaded module's `memory` and `field` its `layout` entry, an
70
+ * `Int32Array` of two counts and the slots. The words are read through one
71
+ * `Int32Array` view, made again only when a growing memory replaced its
72
+ * buffer. The arithmetic is the module's (`std/host/ring.ts`): the head and
73
+ * the tail are counts modulo twice the capacity, reduced before every use, so
74
+ * counts the program wrote out of protocol are never read out of bounds.
75
+ */
76
+ export const ringOver = (memory, field) => {
77
+ const at = field.at;
78
+ const length = field.length;
79
+ if (!Number.isInteger(at) || at < 0 || at % 4 !== 0 || !Number.isInteger(length) || length < 0) {
80
+ throw new RangeError("nish: a ring is an Int32Array's layout entry: { at, length }");
81
+ }
82
+ const capacity = length > 2 ? length - 2 : 0;
83
+ const twice = capacity * 2;
84
+ let view = null;
85
+ const words = () => {
86
+ if (view === null || view.buffer !== memory.buffer) {
87
+ view = new Int32Array(memory.buffer, at, length);
88
+ }
89
+ return view;
90
+ };
91
+ const count = (c) => {
92
+ const r = c % twice;
93
+ return r < 0 ? r + twice : r;
94
+ };
95
+ const size = () => {
96
+ if (capacity === 0) {
97
+ return 0;
98
+ }
99
+ const w = words();
100
+ const n = count(count(w[1]) - count(w[0]));
101
+ return n > capacity ? capacity : n;
102
+ };
103
+ return {
104
+ capacity,
105
+ size,
106
+ free: () => capacity - size(),
107
+ /** Puts `word` (an i32) at the tail; false, and nothing changed, when the ring is full. */
108
+ push(word) {
109
+ if (capacity === 0 || size() >= capacity) {
110
+ return false;
111
+ }
112
+ const w = words();
113
+ const tail = count(w[1]);
114
+ w[2 + (tail % capacity)] = word;
115
+ w[1] = count(tail + 1);
116
+ return true;
117
+ },
118
+ /** Takes the word at the head; 0, and nothing changed, when the ring is empty. */
119
+ shift() {
120
+ if (capacity === 0 || size() === 0) {
121
+ return 0;
122
+ }
123
+ const w = words();
124
+ const head = count(w[0]);
125
+ const word = w[2 + (head % capacity)];
126
+ w[0] = count(head + 1);
127
+ return word;
128
+ },
129
+ /** Empties the ring: the head catches up with the tail. */
130
+ clear() {
131
+ if (length >= 2) {
132
+ const w = words();
133
+ w[0] = w[1];
134
+ }
135
+ },
136
+ };
137
+ };
138
+
139
+ /**
140
+ * The reporter a loader calls when no `onPanic` was given: the panic's message
141
+ * on the console, as a native program prints it on stderr. The loader throws
142
+ * `NishPanic` after it either way.
143
+ */
144
+ export const reportPanic = (panic) => {
145
+ console.error("nish: " + panic.message);
146
+ };