@amritk/nish-x86_64-darwin 0.44.0 → 0.45.1

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 (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
package/bin/nish CHANGED
Binary file
package/docs/AI.md CHANGED
@@ -130,13 +130,11 @@ are one node per line, which is to say diffable.
130
130
 
131
131
  ## A whole program
132
132
 
133
- This program divides by a value it does not test, so it builds with
134
- `--allow-panics` ([Proving it cannot panic](#proving-it-cannot-panic)); a
135
- guard that the divisor is not zero would let it build under the default. Other
136
- examples below that do integer arithmetic nothing bounds build the same way.
133
+ This program builds under the default: its `%` sits behind the loop's
134
+ `y > 0` test ([Proving it cannot panic](#proving-it-cannot-panic)). Examples
135
+ below that do integer arithmetic nothing bounds build with `--allow-panics`.
137
136
 
138
- <!-- allow-panics: divides by a value it does not test -->
139
- <!-- nish:ok --allow-panics -->
137
+ <!-- nish:ok -->
140
138
  ```ts
141
139
  const gcd = (a: i32, b: i32): i32 => {
142
140
  let x = a;
@@ -2568,6 +2566,10 @@ barrier, so neither ever becomes a branch; select and compare on a secret with
2568
2566
  these, never with `if`, `?:`, `===` or `table[secret]`, which they cannot fix.
2569
2567
  `secureZero(bytes)` sets every byte of a `u8[]` to zero in stores the optimiser
2570
2568
  may not drop, as a statement: how a key in a plain array is cleared once used.
2569
+ `mulHigh(a, b)` is the high 64 bits of the 128-bit product of two `u64`s
2570
+ (with `a * b` for the low half, the whole product), one multiply on x86-64 and
2571
+ AArch64 and four 32 × 32 products on wasm32, neither branching on an operand;
2572
+ `mulHighIsNative()` is `true` where it is one instruction.
2571
2573
 
2572
2574
  **Secrets.** Hold a key as a `Secret<T>` from `nish:secret` (`T` an integer
2573
2575
  array or a record of integer fields). It is opaque: no printing, interpolating,
@@ -2904,8 +2906,7 @@ was there) and `clear()`, and `keys()` / `values()` as the iterable of a
2904
2906
  and `s.forEach((x) => ...)` compile to that `for...of` walk with `f` inlined,
2905
2907
  and answer `void`. `entries` is refused by name.
2906
2908
 
2907
- <!-- allow-panics: calls `std/collections.ts` code that keeps panic sites no proof removes yet -->
2908
- <!-- nish:ok-body --allow-panics -->
2909
+ <!-- nish:ok-body -->
2909
2910
  ```ts
2910
2911
  const seen = new Set<string>();
2911
2912
  const counts: Map<string, i32> = new Map();
@@ -2932,7 +2933,7 @@ console.log(`${seen.size} ${counts.size} ${seen.has("b")}`);
2932
2933
  three ways: a default with `??`, a `const` tested with `!== undefined` (or
2933
2934
  `=== undefined` and an early `return`), or a bare test. Each is one probe.
2934
2935
 
2935
- <!-- allow-panics: does integer arithmetic nothing bounds; calls `std/collections.ts` code that keeps panic sites no proof removes yet -->
2936
+ <!-- allow-panics: does integer arithmetic nothing bounds -->
2936
2937
  <!-- nish:ok-body --allow-panics -->
2937
2938
  ```ts
2938
2939
  const counts = new Map<string, i32>();
@@ -2966,7 +2967,7 @@ may `set`, `delete` or `clear` the table inside the walk and get exactly what
2966
2967
  JavaScript gives (a new key is visited later in the same walk; a key deleted
2967
2968
  before it is reached is not).
2968
2969
 
2969
- <!-- allow-panics: does integer arithmetic nothing bounds; calls `std/collections.ts` code that keeps panic sites no proof removes yet -->
2970
+ <!-- allow-panics: does integer arithmetic nothing bounds -->
2970
2971
  <!-- nish:ok-body --allow-panics -->
2971
2972
  ```ts
2972
2973
  const ages = new Map<string, i32>();
@@ -3007,7 +3008,7 @@ string `+`) or assign. Anything else still compiles, as one probe per call.
3007
3008
  inserting it — and `reserve(m, n)`, which sizes the table for `n` entries; both
3008
3009
  are one module with the program and run unchanged under Node.
3009
3010
 
3010
- <!-- allow-panics: does integer arithmetic nothing bounds; calls `std/collections.ts` code that keeps panic sites no proof removes yet -->
3011
+ <!-- allow-panics: does integer arithmetic nothing bounds -->
3011
3012
  <!-- nish:ok --allow-panics -->
3012
3013
  ```ts
3013
3014
  import { getOrInsert, reserve } from "nish/map";
@@ -3072,7 +3073,7 @@ tag under one key (NL2721) as a different string (NL2720). A `tag` on a class
3072
3073
  no union holds does nothing, and a JSON call on that class refuses its
3073
3074
  enum-member field.
3074
3075
 
3075
- <!-- allow-panics: builds an array whose length it does not bound; indexes an array without a guard the checker reads; calls `std/json.ts` code that keeps panic sites no proof removes yet -->
3076
+ <!-- allow-panics: builds an array whose length it does not bound; indexes an array without a guard the checker reads; calls `std/json-typed.ts` code that keeps panic sites no proof removes yet -->
3076
3077
  <!-- nish:ok --allow-panics -->
3077
3078
  ```ts
3078
3079
  import { JsonWriter } from "nish/json";
@@ -3114,7 +3115,7 @@ export const main = (): i32 => {
3114
3115
  };
3115
3116
  ```
3116
3117
 
3117
- <!-- allow-panics: indexes an array without a guard the checker reads; calls `std/json.ts` code that keeps panic sites no proof removes yet -->
3118
+ <!-- allow-panics: indexes an array without a guard the checker reads; calls `std/json-typed.ts` code that keeps panic sites no proof removes yet -->
3118
3119
  <!-- nish:ok --allow-panics -->
3119
3120
  ```ts
3120
3121
  import { JsonWriter } from "nish/json";
@@ -3325,8 +3326,8 @@ you need to walk them, since there is no iteration yet.
3325
3326
  **Find the next byte of a few** — `indexOfAny(text, bytes, from)` from
3326
3327
  `nish/text` answers the first index at or after `from` whose byte is one of
3327
3328
  `bytes`, or -1: the scan for the next `"` or `\\` in a JSON string, the next
3328
- delimiter in a line. `bytes` holds 1 to 16 ASCII bytes and anything else
3329
- panics; `from` is clamped to `[0, text.length]`. Natively it reads 16 or 32
3329
+ delimiter in a line. `bytes` is any set, and an ASCII one answers the same
3330
+ natively and under Node; `from` is clamped to `[0, text.length]`. Natively it reads 16 or 32
3330
3331
  bytes at a time, so prefer it to a `charCodeAt` loop over a long text — and
3331
3332
  never call it on secret data, because its time says where the match is.
3332
3333
  (No block: a program importing `nish/text` writes two `.ll` files;
@@ -3338,7 +3339,8 @@ methods on a number and no optional parameters, so `x.toFixed(n)` is
3338
3339
  both from `nish/text` and both answering what JavaScript does. `toFixed`
3339
3340
  rounds the exact binary value (`toFixed(1.005, 2)` is `"1.00"`) and panics
3340
3341
  outside 0 to 100 digits; `splitLimit` keeps the first `limit` pieces, a
3341
- negative limit keeps all of them, and an empty separator panics.
3342
+ negative limit keeps all of them, and an empty separator splits into code
3343
+ points.
3342
3344
  (No block, as for `nish/text`; `tests/link/std_text_to_fixed` and
3343
3345
  `tests/link/std_text_split_limit` are its programs.)
3344
3346
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/nish-x86_64-darwin",
3
- "version": "0.44.0",
3
+ "version": "0.45.1",
4
4
  "description": "The nish native compiler for macOS on x86_64",
5
5
  "license": "MIT",
6
6
  "os": [
@@ -250,7 +250,9 @@ export function convert(x, from, to) {
250
250
  // The hot paths of u64 arithmetic written over i64 (`toU64(a) * toU64(b)`)
251
251
  // stay in a function small enough for V8 to inline at the call: the two
252
252
  // 64-bit kinds reinterpret each other's bits, and a zext of an unsigned
253
- // 32-bit-or-narrower value to u64 keeps the value as it is.
253
+ // 32-bit-or-narrower value to u64 keeps the value as it is. Keep it this
254
+ // small: answering the integer `number` cases here, inline or through a
255
+ // helper, made ct_asm_x25519 under --emit-ts 1.1x to 1.8x slower.
254
256
  if (typeof x === "bigint") {
255
257
  if (to === "u64") return BigInt.asUintN(64, x);
256
258
  if (to === "i64") return BigInt.asIntN(64, x);
@@ -11,31 +11,65 @@ import { panicIndex, panicSlice, toIndex } from "./checks.mjs";
11
11
  const ENCODER = new TextEncoder();
12
12
  const DECODER = new TextDecoder("utf-8", { ignoreBOM: true });
13
13
 
14
- /** The string `bytesOf` encoded last, and its bytes. */
15
- let lastString = "";
16
- let lastBytes = new Uint8Array(0);
14
+ /** A UTF-16 unit that is not ASCII, so its UTF-8 bytes are not its units. */
15
+ const NON_ASCII = /[^\x00-\x7f]/;
17
16
 
18
17
  /**
19
- * The UTF-8 bytes of `s`, which is what an Nish string holds. The last string
20
- * encoded is kept with its bytes, so a loop of `s.charCodeAt(i)` over one
21
- * string encodes it once rather than once per byte. Nothing here writes to
18
+ * How many strings `bytesOf` keeps. A loop alternates between strings: std's
19
+ * `indexOfAnyFrom` and `yamlByte` read a byte of the document, then of a
20
+ * needle or a word, per step, so a cache of one encodes the document on every
21
+ * read. Eight covers the most std's loops keep live at once.
22
+ */
23
+ const SLOTS = 8;
24
+
25
+ /**
26
+ * The strings `bytesOf` saw last, most recent first, and their bytes: `null`
27
+ * for an ASCII string, whose units are its bytes and which is never encoded.
28
+ * A hit moves its entry to the front and a miss drops the last one, so a
29
+ * string stays while it is read and leaves when it is not. A lookup compares
30
+ * at most `SLOTS` strings with `===`, which answers at once for the same
31
+ * string or a different length and otherwise stops at the first unit that
32
+ * differs.
33
+ */
34
+ const keys = new Array(SLOTS).fill("");
35
+ const values = new Array(SLOTS).fill(null);
36
+ let used = 0;
37
+
38
+ /**
39
+ * The UTF-8 bytes of `s`, which is what an Nish string holds, or `null` when
40
+ * `s` is ASCII and its UTF-16 units are those bytes. Nothing here writes to
22
41
  * the bytes it is handed.
23
42
  */
24
43
  function bytesOf(s) {
25
- if (s !== lastString) {
26
- lastBytes = ENCODER.encode(s);
27
- lastString = s;
44
+ if (keys[0] === s) return values[0];
45
+ let at = 1;
46
+ while (at < used && keys[at] !== s) at++;
47
+ let bytes;
48
+ if (at < used) {
49
+ bytes = values[at];
50
+ } else {
51
+ bytes = NON_ASCII.test(s) ? ENCODER.encode(s) : null;
52
+ if (used < SLOTS) used++;
53
+ at = used - 1;
54
+ }
55
+ for (; at > 0; at--) {
56
+ keys[at] = keys[at - 1];
57
+ values[at] = values[at - 1];
28
58
  }
29
- return lastBytes;
59
+ keys[0] = s;
60
+ values[0] = bytes;
61
+ return bytes;
30
62
  }
31
63
 
32
64
  /**
33
65
  * `s.length`: UTF-8 byte length for strings (arrays keep their own `.length`).
34
- * The engine's encoder outruns a count in JavaScript, and the loop condition
35
- * `i < s.length` then leaves the bytes for the `s.charCodeAt(i)` it guards.
66
+ * The loop condition `i < s.length` then leaves the bytes for the
67
+ * `s.charCodeAt(i)` it guards.
36
68
  */
37
69
  export function strLen(x) {
38
- return typeof x === "string" ? bytesOf(x).length : x.length;
70
+ if (typeof x !== "string") return x.length;
71
+ const bytes = bytesOf(x);
72
+ return bytes === null ? x.length : bytes.length;
39
73
  }
40
74
 
41
75
  /**
@@ -52,6 +86,10 @@ export function strLenOpt(x) {
52
86
  export function charCodeAt(s, i) {
53
87
  const bytes = bytesOf(s);
54
88
  const k = toIndex(i);
89
+ if (bytes === null) {
90
+ if (!(k >= 0 && k < s.length)) panicIndex(k, s.length);
91
+ return s.charCodeAt(k);
92
+ }
55
93
  if (!(k >= 0 && k < bytes.length)) panicIndex(k, bytes.length);
56
94
  return bytes[k];
57
95
  }
@@ -59,9 +97,13 @@ export function charCodeAt(s, i) {
59
97
  /** `s.substring(a, b)`: JavaScript's clamp and swap, over byte offsets. */
60
98
  export function substring(s, a, b) {
61
99
  const bytes = bytesOf(s);
62
- const clamp = (v) => Math.max(0, Math.min(toIndex(v), bytes.length));
100
+ const len = bytes === null ? s.length : bytes.length;
101
+ const clamp = (v) => Math.max(0, Math.min(toIndex(v), len));
63
102
  const from = clamp(a);
64
- const to = b === undefined ? bytes.length : clamp(b);
103
+ const to = b === undefined ? len : clamp(b);
104
+ // A NaN index cuts nothing, as native's does; JavaScript's would read it as 0.
105
+ if (from !== from || to !== to) return "";
106
+ if (bytes === null) return s.substring(from, to);
65
107
  return DECODER.decode(bytes.subarray(Math.min(from, to), Math.max(from, to)));
66
108
  }
67
109
 
@@ -73,17 +115,22 @@ export function substring(s, a, b) {
73
115
  */
74
116
  export function slice(s, a, b) {
75
117
  const bytes = bytesOf(s);
118
+ const len = bytes === null ? s.length : bytes.length;
76
119
  const from = toIndex(a);
77
- const to = b === undefined ? bytes.length : toIndex(b);
78
- if (!(from >= 0 && from <= to && to <= bytes.length)) panicSlice(from, to, bytes.length);
79
- return DECODER.decode(bytes.subarray(from, to));
120
+ const to = b === undefined ? len : toIndex(b);
121
+ if (!(from >= 0 && from <= to && to <= len)) panicSlice(from, to, len);
122
+ return bytes === null ? s.slice(from, to) : DECODER.decode(bytes.subarray(from, to));
80
123
  }
81
124
 
82
- /** Whether `needle` sits in `bytes` at byte offset `at`. */
83
- function bytesAt(bytes, at, needle) {
84
- if (!(at >= 0 && at + needle.length <= bytes.length)) return false;
85
- for (let j = 0; j < needle.length; j++) {
86
- if (bytes[at + j] !== needle[j]) return false;
125
+ /**
126
+ * Whether `sub` sits in `bytes` at byte offset `at`; `needle` is `sub`'s
127
+ * bytes, or `null` when `sub` is ASCII and its units are compared instead.
128
+ */
129
+ function bytesAt(bytes, at, sub, needle) {
130
+ const n = needle === null ? sub.length : needle.length;
131
+ if (!(at >= 0 && at + n <= bytes.length)) return false;
132
+ for (let j = 0; j < n; j++) {
133
+ if (bytes[at + j] !== (needle === null ? sub.charCodeAt(j) : needle[j])) return false;
87
134
  }
88
135
  return true;
89
136
  }
@@ -92,35 +139,40 @@ function bytesAt(bytes, at, needle) {
92
139
  * `s.indexOf(sub, from)`: the first *byte* offset at or after `from`, or -1.
93
140
  * `from` is converted as JavaScript's `ToIntegerOrInfinity` converts it (NaN
94
141
  * is 0) and clamped into `[0, len]`, so an empty `sub` is found at `from`.
95
- * The engine's `indexOf` over the bytes finds each candidate for `sub`'s
96
- * first byte, and only those are compared in full.
142
+ * In an ASCII `s` the engine's own `indexOf` answers: offsets are bytes there,
143
+ * and a `sub` that is not ASCII is in it nowhere, as none of its bytes are.
144
+ * Otherwise the engine's `indexOf` over the bytes finds each candidate for
145
+ * `sub`'s first byte, and only those are compared in full.
97
146
  */
98
147
  export function indexOf(s, sub, from) {
99
148
  const haystack = bytesOf(s);
149
+ const len = haystack === null ? s.length : haystack.length;
100
150
  const at = from === undefined ? 0 : toIndex(from);
101
- const start = Number.isNaN(at) ? 0 : Math.max(0, Math.min(at, haystack.length));
151
+ const start = Number.isNaN(at) ? 0 : Math.max(0, Math.min(at, len));
102
152
  if (sub.length === 0) return start;
103
- const needle = ENCODER.encode(sub);
104
- const last = haystack.length - needle.length;
105
- for (let i = haystack.indexOf(needle[0], start); i >= 0 && i <= last; i = haystack.indexOf(needle[0], i + 1)) {
106
- if (bytesAt(haystack, i, needle)) return i;
153
+ if (haystack === null) return s.indexOf(sub, start);
154
+ const needle = bytesOf(sub);
155
+ const first = needle === null ? sub.charCodeAt(0) : needle[0];
156
+ const last = len - (needle === null ? sub.length : needle.length);
157
+ for (let i = haystack.indexOf(first, start); i >= 0 && i <= last; i = haystack.indexOf(first, i + 1)) {
158
+ if (bytesAt(haystack, i, sub, needle)) return i;
107
159
  }
108
160
  return -1;
109
161
  }
110
162
 
111
- /** `nish_str_at`: whether `sub`'s bytes sit at byte offset `at`. */
112
- function occursAt(s, at, sub) {
113
- return bytesAt(bytesOf(s), at, ENCODER.encode(sub));
114
- }
115
-
163
+ /** `s.startsWith(sub)`: whether `sub`'s bytes begin `s`'s; an ASCII `s` asks the engine. */
116
164
  export function startsWith(s, sub) {
117
- return occursAt(s, 0, sub);
165
+ const bytes = bytesOf(s);
166
+ if (bytes === null) return s.startsWith(sub);
167
+ return bytesAt(bytes, 0, sub, bytesOf(sub));
118
168
  }
119
169
 
170
+ /** `s.endsWith(sub)`: whether `sub`'s bytes end `s`'s; an ASCII `s` asks the engine. */
120
171
  export function endsWith(s, sub) {
121
172
  const bytes = bytesOf(s);
122
- const needle = ENCODER.encode(sub);
123
- return bytesAt(bytes, bytes.length - needle.length, needle);
173
+ if (bytes === null) return s.endsWith(sub);
174
+ const needle = bytesOf(sub);
175
+ return bytesAt(bytes, bytes.length - (needle === null ? sub.length : needle.length), sub, needle);
124
176
  }
125
177
 
126
178
  /**
package/runtime/core.d.ts CHANGED
@@ -147,3 +147,11 @@ export declare function asI32x4(v: SimdVector): i32x4;
147
147
  export declare function asU32x4(v: SimdVector): u32x4;
148
148
  export declare function asF32x4(v: SimdVector): f32x4;
149
149
  export declare function asF64x2(v: SimdVector): f64x2;
150
+
151
+ // `mulHigh` and `mulHighIsNative`: builtins, written out under these names.
152
+ import type { u64 } from "./core/numbers.js";
153
+
154
+ /** The high 64 bits of the 128-bit product of `a` and `b` (WP41): `(a * b) >> 64n`. */
155
+ export declare function mulHigh(a: u64, b: u64): u64;
156
+ /** `false` under Node, where `mulHigh` is a BigInt multiply rather than one instruction. */
157
+ export declare function mulHighIsNative(): boolean;
package/runtime/core.mjs CHANGED
@@ -89,6 +89,22 @@ const simdLoad = (Lanes) => (xs, i) => {
89
89
  export const Compact = Object.freeze({});
90
90
  export const Wide = Object.freeze({});
91
91
 
92
+ // `mulHigh` / `mulHighIsNative` (WP41). A `u64` is a BigInt here, so the high
93
+ // half is the exact product shifted down. A `number` operand is the mix the
94
+ // native checker refuses (NL2823), and throws rather than answering from a
95
+ // double. `mulHighIsNative` is `false`, so std keeps its narrow-limb code under
96
+ // Node, where `mulHigh` is a BigInt multiply and no faster than four products.
97
+ export function mulHigh(a, b) {
98
+ if (typeof a !== "bigint" || typeof b !== "bigint") {
99
+ throw new TypeError("mulHigh: takes u64 (BigInt) operands");
100
+ }
101
+ return (BigInt.asUintN(64, a) * BigInt.asUintN(64, b)) >> 64n;
102
+ }
103
+
104
+ export function mulHighIsNative() {
105
+ return false;
106
+ }
107
+
92
108
  export const u8x16 = simdMake(Uint8Array);
93
109
  export const u16x8 = simdMake(Uint16Array);
94
110
  export const i32x4 = simdMake(Int32Array);
package/runtime/nish.d.ts CHANGED
@@ -628,6 +628,36 @@ declare function ctSelect<T extends u32 | u64>(mask: T, a: T, b: T): T;
628
628
  * @returns a mask for `ctSelect`.
629
629
  */
630
630
  declare function ctEq<T extends u32 | u64>(a: T, b: T): T;
631
+ /**
632
+ * The high 64 bits of the 128-bit product of `a` and `b`, the half a `u64`
633
+ * `a * b` drops (WP41). Both operands are `u64`. Any other type is refused as
634
+ * `` `mulHigh` takes u64 operands (convert with toU64), got u32 `` (NL2823):
635
+ * `u32`, `i64`, `number` and a ranged integer included. A bare numeric
636
+ * literal is read as a `u64`, so `mulHigh(x, 3)` works, and one that does not
637
+ * fit is the literal's own refusal. The call takes exactly two arguments, and
638
+ * like every function it is not a value.
639
+ *
640
+ * On x86-64 and AArch64 it is one multiply (`mul`, `mulx` or `umulh`) and
641
+ * calls nothing. wasm32 has no wide multiply, so there it is four 32×32
642
+ * products and their carries, inline. Neither lowering branches on its
643
+ * operands. Under Node it is `(a * b) >> 64n` on BigInts, with the same
644
+ * answer. Capability: none.
645
+ *
646
+ * @param a one factor.
647
+ * @param b the other factor.
648
+ * @returns the top 64 bits of `a * b` computed in 128 bits.
649
+ */
650
+ declare function mulHigh(a: u64, b: u64): u64;
651
+ /**
652
+ * Whether `mulHigh` is one instruction on the build's target (WP41): `true` on
653
+ * x86-64 and AArch64, and `false` on wasm32 and under Node. It is a constant of
654
+ * the build, so a branch on it costs nothing and the path not taken is
655
+ * dropped. Code with a wide-limb algorithm and a narrow-limb one keeps both and
656
+ * picks with it. It takes no arguments. Capability: none.
657
+ *
658
+ * @returns `true` where the 64×64→128 product is native.
659
+ */
660
+ declare function mulHighIsNative(): boolean;
631
661
  /**
632
662
  * Every byte of `bytes` set to zero, in stores neither `-O2` nor `-flto` may
633
663
  * drop as dead (#385): how a key, a secret scalar or another secret is cleared
@@ -5328,6 +5358,18 @@ declare module "nish:async" {
5328
5358
  * (or removed); `false` for a task that has finished or is `main`.
5329
5359
  */
5330
5360
  export function cancelAfter(task: i64, ms: i32): boolean;
5361
+ /**
5362
+ * Read `view`, an object or array its spawner owns, in place, without
5363
+ * copying it into the task's region. Inside a spawned task the view, and
5364
+ * what is built from it, is lent only while the task runs: the task may
5365
+ * read it, write numbers into it and copy out of it, but may not store a
5366
+ * pointer into it, grow it, or keep it, its parts or what holds it past its
5367
+ * end (NL2822). Outside a task, and under `--emit-ts`, it is the identity.
5368
+ *
5369
+ * @param view The spawner's object or array the task reads.
5370
+ * @returns `view` itself.
5371
+ */
5372
+ export function lend<T>(view: T): T;
5331
5373
  }
5332
5374
 
5333
5375
  // ---- Arena (docs/LANGUAGE.md -> Arena) ---------------------------------------
@@ -6227,9 +6269,9 @@ interface StringConstructor {
6227
6269
  * reached, visits a key set during the walk later in the same walk, and keeps a
6228
6270
  * re-set key in its place, as JavaScript's does.
6229
6271
  *
6230
- * At most 2^24 − 1 entries (one fewer than Node's 2^24); one more panics with
6231
- * `Map maximum size exceeded`, after first compacting away dead entries (not
6232
- * while a loop walks the table). String keys are hashed with SipHash-1-3 under
6272
+ * No entry cap of its own: past 2^29 entries the bucket table would outgrow
6273
+ * the largest array, and the program stops as out of memory, as any
6274
+ * allocation that size does (Node's `Map` throws at 2^24). String keys are hashed with SipHash-1-3 under
6233
6275
  * a per-process random key drawn from the system's entropy source before
6234
6276
  * `main`, and an integer, `boolean` or enum key under a per-process random
6235
6277
  * multiply-add-shift; if the entropy source fails the program panics before
@@ -6301,8 +6343,8 @@ interface Map<K, V> {
6301
6343
  * `new Set<T>()` takes no arguments ("`new Set<string>` takes no arguments in
6302
6344
  * this version"). `keys()` and `values()` are one walk, as JavaScript's are,
6303
6345
  * admitted only as the iterable of a `for...of`, which also takes the set
6304
- * itself: `for (const x of s)`. At most 2^24 − 1 members; one more panics with
6305
- * `Set maximum size exceeded`. Hashing, the entropy source and the walk's
6346
+ * itself: `for (const x of s)`. Its size is bounded as a `Map`'s is, by
6347
+ * memory alone. Hashing, the entropy source and the walk's
6306
6348
  * rules are `Map`'s. `add` and `has` are one probe each, and `if (!s.has(x)) {
6307
6349
  * s.add(x); ... }` with the `add` first in the branch asks about the member
6308
6350
  * once.
package/runtime/nish.mjs CHANGED
@@ -251,6 +251,10 @@ provide("f32ToBits", shim.f32ToBits);
251
251
  // agree with a native run; the timing does not, and is not claimed here.
252
252
  provide("ctSelect", shim.ctSelect);
253
253
  provide("ctEq", shim.ctEq);
254
+ // `mulHigh` (WP41): exact on BigInts. `mulHighIsNative` is `false`, so std
255
+ // takes its narrow-limb path here.
256
+ provide("mulHigh", shim.mulHigh);
257
+ provide("mulHighIsNative", shim.mulHighIsNative);
254
258
  // `secureZero` (#385): the bytes are zero afterwards here too, though what a
255
259
  // collector may have copied before is out of reach.
256
260
  provide("secureZero", shim.secureZero);
package/std/README.md CHANGED
@@ -13,7 +13,7 @@ a call loads without an import (`collections.ts`, `array.ts`, `sort.ts`,
13
13
  | Module | What it is |
14
14
  | --- | --- |
15
15
  | [`testing.ts`](https://github.com/amritk/nish/blob/main/std/testing.ts) | a test runner: a `Suite` a program drives with straight-line assertions, printing the `PASS` / `FAIL` / `SKIP` lines the repository's own harness prints, and answering the exit code |
16
- | [`text.ts`](https://github.com/amritk/nish/blob/main/std/text.ts) | the string operations a program would otherwise write inline: `split` and `splitLimit` (JavaScript's `split(sep, limit)`), `toFixed` (JavaScript's `x.toFixed(digits)`, rounded on the exact binary value), `splitLines`, `splitWhitespace`, `trim` and its halves, `contains`, `replaceAll`, `firstDifference` over two arrays of lines, `codePointLength`, the number of Unicode code points (what JSON Schema's `minLength` counts, where `.length` counts UTF-8 bytes natively and UTF-16 units under Node; it answers the same on both by asking which it is reading), and `indexOfAny(text, bytes, from)`, the first index at or after `from` whose byte is one of 1 to 16 ASCII `bytes`. Its walker is the meaning and what runs under Node; natively the compiler replaces the one call to it with the runtime kernel `nish_str_index_of_any`, which reads 16 or 32 bytes at a time and is variable-time ([`docs/LANGUAGE.md`](https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#byte-search-indexofany-in-nishtext)). `tests/link/std_text_index_of_any*` are its programs |
16
+ | [`text.ts`](https://github.com/amritk/nish/blob/main/std/text.ts) | the string operations a program would otherwise write inline: `split` and `splitLimit` (JavaScript's `split(sep, limit)`), `toFixed` (JavaScript's `x.toFixed(digits)`, rounded on the exact binary value), `splitLines`, `splitWhitespace`, `trim` and its halves, `contains`, `replaceAll`, `firstDifference` over two arrays of lines, `codePointLength`, the number of Unicode code points (what JSON Schema's `minLength` counts, where `.length` counts UTF-8 bytes natively and UTF-16 units under Node; it answers the same on both by asking which it is reading), and `indexOfAny(text, bytes, from)`, the first index at or after `from` whose byte is one of `bytes`. Its walker is the meaning and what runs under Node; natively the compiler replaces the one call to it with the runtime kernel `nish_str_index_of_any`, which reads 16 or 32 bytes at a time and is variable-time ([`docs/LANGUAGE.md`](https://github.com/amritk/nish/blob/main/docs/LANGUAGE.md#byte-search-indexofany-in-nishtext)). `tests/link/std_text_index_of_any*` are its programs |
17
17
  | [`math.ts`](https://github.com/amritk/nish/blob/main/std/math.ts) | `Math.tan`, `Math.atan2` and `Math.hypot` on `f64`, ported from fdlibm (`LICENSE-fdlibm`) so that every target, wasm included, answers alike without libm. A program never imports it: a call to one of the three loads it, and the compiler copies the functions the call reaches into the calling module as `internal` ones. JavaScript's answer for every NaN, signed zero and infinity, and within one ulp elsewhere (0.83, 0.84 and 0.93 ulp measured against mpmath). `tests/cases/math_std_edges` pins the special values |
18
18
  | [`sort.ts`](https://github.com/amritk/nish/blob/main/std/sort.ts) | `xs.sort(cmp)` on any array: `sortWith`, a stable merge sort over runs of 32 sorted by insertion — one scratch array released before it returns, a pair of runs already in order copied without merging, no bounds check left in it — so its order is element for element Node's `Array.prototype.sort`. Like `math.ts` it is one the compiler knows about: a program never imports it, a `.sort(cmp)` call loads it, the checker rewrites the call to `sortWith`, and each comparator's instantiation — the comparator called directly, no function pointer — is copied into the module that sorts. Under Node the call stays the platform's own. `tests/cases/arr_sort_cmp*` are its programs, a 10,000-element sort with ties among them |
19
19
  | [`array.ts`](https://github.com/amritk/nish/blob/main/std/array.ts) | the array methods that take a callback, and `slice`: `xs.map(f)`, `filter`, `some`, `every`, `findIndex` and `slice`, each a plain loop compiled once per callback, with no function pointer. A program never imports it: a call to one of those methods on an array loads it, and the compiler routes `xs.map(f)` to `map` or `mapIndexed` by how many parameters `f` takes. Each walks the array in order up to the length it had when the call began, as `Array.prototype` does, and `some`, `every` and `findIndex` stop at the element that decides the answer; a callback that shrinks the array ends the walk at the new length, where JavaScript goes on. Under Node the methods are the platform's own (`tests/cases/arr_map`, `arr_filter` and their kin) |
@@ -116,7 +116,7 @@ is listed in [`SECURITY.md`](https://github.com/amritk/nish/blob/main/SECURITY.m
116
116
  | [`crypto/base64url.ts`](https://github.com/amritk/nish/blob/main/std/crypto/base64url.ts) | `base64urlEncode(data)` and `base64urlDecode(text)`, unpadded; `base64StdPadEncode(data)` and `base64StdPadDecode(text)`, the padded standard alphabet that SCRAM and PEM use, with the buffer forms `base64StdPadEncodeInto(data, start, length, out, at)` and `base64StdPadDecodedLength(text, start, length)` that allocate nothing, constant-time in both directions. These are not `nish/base64`, which encodes only, is not constant-time and is for pixels; the names differ because std modules share the program's one namespace. Decoding is strict, so every byte string has one spelling. `base64urlDecode` answers `null` for any `=`, a character outside the alphabet, a length of 1 mod 4 or nonzero unused low bits; `base64StdPadDecode` requires the padding, answering `null` unless the text is whole four-character groups with `=` only as the last one or two characters, every other character in the alphabet and the unused bits zero. Both refuse a text longer than 2^31 − 1 bytes, and `base64urlEncode` panics on such an array | RFC 4648 §4, §5, §10 (`tests/link/std_base64_padded`) |
117
117
  | [`crypto/sha1.ts`](https://github.com/amritk/nish/blob/main/std/crypto/sha1.ts) | `sha1(data)`, a fresh 20-byte digest, `Sha1`, whose `digestInto(data, start, length, out, at)` hashes a slice into a caller's buffer and allocates nothing after it is built, and `SHA1_SIZE` and `SHA1_BLOCK`. **Not for security**: SHA-1 is broken for collisions, and it is here only because RFC 6455 §4.2.2's `Sec-WebSocket-Accept` is defined on it (`nish/net/websocket` is its importer). One-shot only, not streaming; it panics on an array longer than 2^31 − 1 bytes. It came after the #363 audit and has no record in `docs/security/` | FIPS 180-4 §6.1 |
118
118
  | [`crypto/x25519.ts`](https://github.com/amritk/nish/blob/main/std/crypto/x25519.ts) | `x25519(scalar, u)`, the shared secret as a `Secret<u8[]>`, and `x25519Base(scalar)`, the public key, each from a `Secret<u8[]>` scalar, on ten 25.5-bit limbs in `i64`. Either answers `null` unless its arguments are `X25519_SIZE` (32) bytes; the scalar is clamped on a copy, which is wiped | RFC 7748 §5.2, §6.1, Wycheproof `x25519_test` |
119
- | [`crypto/ed25519.ts`](https://github.com/amritk/nish/blob/main/std/crypto/ed25519.ts) | `ed25519PublicKey(seed)`, the 32-byte public key, `ed25519Sign(seed, msg)`, the 64-byte signature `R \|\| S`, each from a `Secret<u8[]>` seed of `ED25519_SEED_SIZE` (32) bytes or `null`, and `ed25519Verify(pub, msg, sig)`. Signing is deterministic and constant time; the seed's hash, the nonce and every intermediate the scalar reaches are wiped. Verify is cofactorless, refuses an S of L or more and a key that does not decode, and is variable time, since its inputs are public. Sign and verify panic on a message longer than 2^31 − 1 bytes. It reuses `crypto/x25519.ts`'s field arithmetic | RFC 8032 §5.1, §7.1 |
119
+ | [`crypto/ed25519.ts`](https://github.com/amritk/nish/blob/main/std/crypto/ed25519.ts) | `ed25519PublicKey(seed)`, the 32-byte public key, `ed25519Sign(seed, msg)`, the 64-byte signature `R \|\| S`, each from a `Secret<u8[]>` seed of `ED25519_SEED_SIZE` (32) bytes or `null`, and `ed25519Verify(pub, msg, sig)`. Signing is deterministic and constant time; the seed's hash, the nonce and every intermediate the scalar reaches are wiped. Verify is cofactorless, refuses an S of L or more and a key that does not decode, and is variable time, since its inputs are public. A message longer than 2^31 − 1 bytes makes sign answer `null` and verify answer `false`. It reuses `crypto/x25519.ts`'s field arithmetic | RFC 8032 §5.1, §7.1 |
120
120
  | [`crypto/chacha20poly1305.ts`](https://github.com/amritk/nish/blob/main/std/crypto/chacha20poly1305.ts) | `chacha20Poly1305Seal(key, nonce, aad, plaintext)`, which answers the ciphertext followed by the 16-byte tag, and `chacha20Poly1305Open`, which checks the whole tag before it decrypts anything and answers `null` for a message that does not authenticate. Beneath them `chacha20Block`, `chacha20`, `chacha20QuarterRound`, `poly1305` and `poly1305KeyGen`, and `chacha20HeaderMask`, QUIC's 5-byte header-protection mask. Every input of the wrong length answers `null`. What a call derives from the key — the ChaCha20 state, the keystream, the Poly1305 key, `r` and accumulator, and on open the tag it compared — is wiped with `wipe`'s volatile stores before the call returns, the refusal path included. Natively, on x86-64 with AVX2 or on AArch64, the compiler sends the keystream of a message of 256 bytes or more to the runtime's kernel instead (`runtime/runtime-chacha20.c`), and `NISH_CHACHA20=soft` pins the Nish code | RFC 8439 §2, RFC 9001 §5.4.4, Wycheproof `chacha20_poly1305` (all 325 cases) |
121
121
  | [`crypto/aes.ts`](https://github.com/amritk/nish/blob/main/std/crypto/aes.ts) | AES-128 and AES-256, bitsliced four blocks at a time with the Boyar–Peralta S-box circuit, so no table is read; natively, on a CPU with AES-NI and PCLMULQDQ or the ARMv8 AES and PMULL instructions, the compiler sends the key schedule, the block, GCTR and GHASH to the runtime's kernels instead (`runtime/runtime-aes.c`), and `NISH_AES=soft` pins the bitsliced code: `aesKey(key)` expands a 16- or 32-byte key into an `AesKey` (or `aesKeyInto(key, out)` into one the caller holds, allocating nothing that outlives the call), then `aesEncryptBlock`, `aesGcmSeal` / `aesGcmOpen` (tag last, checked in full before anything is decrypted; any non-empty IV) and `aesHeaderMask`. AES-192 is out of scope, and a wrong length answers `null`, as does an `AesKey` not made by `aesKey`; one whose H is zero opens nothing. An `AesKey` holds its round keys in `roundKeys` and H in `h`, two `u64` words (high, low); `aesKeyWipe(key)` zeroes both with `wipe`'s volatile stores, the kernels' powers of H after the schedule included, and is how a holder drops a key. The bitsliced schedule's, block's and GCTR's working copies, the tag's E(K, J0) block, the tag open compared and J0 itself are wiped the same way before they are released | FIPS 197, SP 800-38D, RFC 9001 §5.4.3, Wycheproof `aes_gcm` (all 316 cases; the 103 AES-192 ones check that the key is refused) |
122
122
  | [`crypto/p256.ts`](https://github.com/amritk/nish/blob/main/std/crypto/p256.ts) | ECDSA over P-256 with RFC 6979's deterministic nonces: `p256PublicKey(priv)` (65-byte uncompressed SEC1) from a `Secret<u8[]>` private key, `p256Sign` / `p256Verify` over a digest and `p256SignSha256` / `p256VerifySha256` over a message, and `p256SignSha256Into(scratch, priv, msg, off, len, out, at)`, the same signature written into the caller's window in a `P256SignScratch` made once, keeping nothing in the arena; a signature is `r` and `s`, 32 big-endian bytes each, not DER (`crypto/x509.ts`'s `x509DerSignatureRS` converts one). A malformed key, a point off the curve or an `r` or `s` out of range answers `null` or `false`, never a panic. ECDSA is malleable, and `p256Verify` accepts a high `s`: when `(r, s)` verifies, so does `(r, n − s)` | RFC 6979 A.2.5, Wycheproof `ecdsa_secp256r1_sha256`, `_sha256_p1363`, `_sha512` and `_sha512_p1363` |
@@ -212,7 +212,7 @@ libdeflate: `bench/gzip/`; against Go, Rust, Java, Node, Bun and Python:
212
212
  | Module | What it is | Reproduces |
213
213
  | --- | --- | --- |
214
214
  | [`compress/deflate.ts`](https://github.com/amritk/nish/blob/main/std/compress/deflate.ts) | `Deflater(level)` and `Inflater`, raw DEFLATE. Levels 0 (stored), 1 to 3 (greedy on a four-byte hash, `DEFLATE_FAST` = 1) and 4 to 9 (lazy, `DEFLATE_DEFAULT` = 6); each block is the smallest of dynamic, fixed and stored by its exact size. `compress(src, off, len, out, at)` answers the end or `DEFLATE_NO_ROOM` (-1); `deflateBound(n)` / `compressBound(n)` are the most any level writes. `decompress(src, off, len, out, at, limit)` answers the end or a `DEFLATE_ERR_*` code (truncated, block type, stored length, Huffman, symbol, distance, output limit, trailing) and never panics on a stream; `inflate` is the resumable step under it (`INFLATE_DONE`, `INFLATE_NEED_INPUT`, `INFLATE_NEED_OUTPUT`). `FixedDeflater` is the small-body encoder: one fixed-Huffman block, a 16 KB window, nothing kept between calls, two to four times faster than `DEFLATE_FAST` at about 1.35 times its size; `fixedDeflateBound(n)` is the most it writes | RFC 1951: every level against Node's zlib both ways (`tests/link/std_compress_node`), round trips of empty to 1 MB inputs, a 1,500-input fuzz, every prefix and random corruption refused (`tests/link/std_compress`) |
215
- | [`compress/gzip.ts`](https://github.com/amritk/nish/blob/main/std/compress/gzip.ts) | `GzipCompressor(level)` / `GzipDecompressor` one-shot (the decompressor reads concatenated members, refuses trailing bytes, checks the header CRC, CRC-32 and length: `GZIP_ERR_HEADER`, `GZIP_ERR_CRC`, `GZIP_ERR_SIZE`), `gzipCompress` / `gzipDecompress`; `GzipFixedCompressor`, the same framing round `FixedDeflater` (`gzipFixedBound(n)`); `GzipWriter` (`write`, `flush`, `finish`, `bound`) and `GzipReader` (`feed` or `inputBuffer` / `commit`, `endInput`, `read` answering bytes, `GZIP_NEED_INPUT`, `GZIP_END` or an error, with a total `limit`) for bodies that arrive in pieces | RFC 1952: Node's gzip at every level, strategy and window, through both readers; the writer's output through Node's gunzip (`std_compress_node`, `std_compress`) |
215
+ | [`compress/gzip.ts`](https://github.com/amritk/nish/blob/main/std/compress/gzip.ts) | `GzipCompressor(level)` / `GzipDecompressor` one-shot (the decompressor reads concatenated members, refuses trailing bytes, checks the header CRC, CRC-32 and length: `GZIP_ERR_HEADER`, `GZIP_ERR_CRC`, `GZIP_ERR_SIZE`), `gzipCompress` / `gzipDecompress`; `GzipFixedCompressor`, the same framing round `FixedDeflater` (`gzipFixedBound(n)`); `GzipWriter` (`write`, `flush`, `finish`, `bound`, `start`; `GZIP_ERR_FINISHED` after `finish`) and `GzipReader` (`feed` or `inputBuffer` / `commit`, `endInput`, `read` answering bytes, `GZIP_NEED_INPUT`, `GZIP_END` or an error, with a total `limit`) for bodies that arrive in pieces | RFC 1952: Node's gzip at every level, strategy and window, through both readers; the writer's output through Node's gunzip (`std_compress_node`, `std_compress`) |
216
216
  | [`compress/zlib.ts`](https://github.com/amritk/nish/blob/main/std/compress/zlib.ts) | `ZlibCompressor(level)` / `ZlibDecompressor`, `zlibCompress` / `zlibDecompress`: the two-byte header and Adler-32 trailer (`ZLIB_ERR_HEADER`, `ZLIB_ERR_ADLER`, `ZLIB_ERR_DICTIONARY` for `FDICT`) | RFC 1950, against Node's `deflateSync` / `inflateSync` (`std_compress_node`) |
217
217
  | [`compress/checksum.ts`](https://github.com/amritk/nish/blob/main/std/compress/checksum.ts) | `Crc32` (slicing-by-8 tables built once; `update(crc, buf, off, len)`), `crc32` for a one-off, `adler32`, `adler32Update`. Natively the compiler sends `update`'s walk to `runtime/runtime-checksum.c` where the CPU has PCLMULQDQ or the ARMv8 CRC32 instructions, chosen once per process; `NISH_CRC32=soft` pins the tables | The check values of "123456789", pieces against the whole, Adler-32's deferred reduction against its definition (`std_compress`); every kernel against a bit-at-a-time reference (`tests/simd/crc32.c`); both paths on the same programs (`tests/link/std_compress_crc_paths`, `std_compress_crc_diff`) |
218
218
 
package/std/array.ts CHANGED
@@ -291,9 +291,17 @@ export const slice = <T>(xs: readonly T[], start: number, end: number): T[] => {
291
291
  export const sliceFrom = <T>(xs: readonly T[], start: number): T[] => slice(xs, start, xs.length)
292
292
 
293
293
  /**
294
- * A copy of the whole of `xs`: what `xs.slice()` compiles to.
294
+ * A copy of the whole of `xs`: what `xs.slice()` compiles to. It copies
295
+ * directly rather than through `slice`, whose range check can panic and
296
+ * which a whole copy never needs, so it cannot panic but out of memory.
295
297
  *
296
298
  * @param xs the array copied; it is not changed.
297
299
  * @returns a new array with the same elements.
298
300
  */
299
- export const sliceAll = <T>(xs: readonly T[]): T[] => slice(xs, 0, xs.length)
301
+ export const sliceAll = <T>(xs: readonly T[]): T[] => {
302
+ const out: T[] = []
303
+ for (let i: i32 = 0; i >= 0 && i < toI32(xs.length); i++) {
304
+ out.push(xs[i])
305
+ }
306
+ return out
307
+ }