nish 0.42.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 (125) hide show
  1. package/README.md +11 -5
  2. package/docs/AI.md +287 -129
  3. package/package.json +9 -9
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/skills/writing-nish/SKILL.md +12 -4
  21. package/std/README.md +6 -6
  22. package/std/array.ts +10 -2
  23. package/std/bytes.ts +239 -26
  24. package/std/cli-args.ts +1072 -606
  25. package/std/cli-flags.ts +180 -98
  26. package/std/cli.ts +21 -13
  27. package/std/collections.ts +245 -127
  28. package/std/compress/checksum.ts +32 -37
  29. package/std/compress/deflate.ts +1102 -542
  30. package/std/compress/gzip.ts +290 -145
  31. package/std/compress/zlib.ts +57 -21
  32. package/std/crypto/aes.ts +153 -75
  33. package/std/crypto/base64url.ts +163 -39
  34. package/std/crypto/bcrypt-batch.ts +164 -90
  35. package/std/crypto/bcrypt.ts +185 -107
  36. package/std/crypto/chacha20poly1305.ts +84 -68
  37. package/std/crypto/ct.ts +9 -7
  38. package/std/crypto/ed25519.ts +64 -27
  39. package/std/crypto/hkdf.ts +105 -63
  40. package/std/crypto/hmac.ts +95 -89
  41. package/std/crypto/jwt.ts +248 -126
  42. package/std/crypto/p256.ts +758 -444
  43. package/std/crypto/p384.ts +20 -13
  44. package/std/crypto/pki-roots.ts +95 -38
  45. package/std/crypto/pki.ts +164 -60
  46. package/std/crypto/rsa.ts +266 -223
  47. package/std/crypto/scram.ts +141 -0
  48. package/std/crypto/sha1.ts +157 -57
  49. package/std/crypto/sha256.ts +502 -422
  50. package/std/crypto/sha512.ts +204 -135
  51. package/std/crypto/x509-random.ts +9 -3
  52. package/std/crypto/x509.ts +331 -171
  53. package/std/host/ring.ts +1 -1
  54. package/std/host/text.ts +1 -1
  55. package/std/json-equal.ts +35 -19
  56. package/std/json-format.ts +215 -108
  57. package/std/json-number.ts +126 -72
  58. package/std/json-pointer.ts +42 -13
  59. package/std/json-tokenizer.ts +498 -307
  60. package/std/json-typed.ts +115 -36
  61. package/std/json-value.ts +341 -152
  62. package/std/json.ts +300 -158
  63. package/std/net/buffer-pool.ts +17 -7
  64. package/std/net/dns-config.ts +427 -236
  65. package/std/net/dns-message.ts +114 -47
  66. package/std/net/dns-resolver.ts +736 -237
  67. package/std/net/dns.ts +205 -52
  68. package/std/net/fetch.ts +320 -184
  69. package/std/net/grpc.ts +948 -0
  70. package/std/net/handler.ts +1276 -218
  71. package/std/net/hpack.ts +395 -180
  72. package/std/net/http-compress.ts +502 -65
  73. package/std/net/http-fields.ts +85 -49
  74. package/std/net/http-server.ts +1764 -603
  75. package/std/net/http1-client-codec.ts +372 -161
  76. package/std/net/http1-codec.ts +665 -331
  77. package/std/net/http1-server.ts +881 -269
  78. package/std/net/http2-cleartext.ts +165 -95
  79. package/std/net/http2-frame.ts +232 -89
  80. package/std/net/http2-tls.ts +42 -12
  81. package/std/net/http2.ts +327 -101
  82. package/std/net/http3-frame.ts +68 -33
  83. package/std/net/http3-server.ts +672 -206
  84. package/std/net/http3.ts +669 -191
  85. package/std/net/oidc.ts +63 -24
  86. package/std/net/outbound.ts +207 -142
  87. package/std/net/postgres-async.ts +1223 -470
  88. package/std/net/postgres-carrier.ts +575 -372
  89. package/std/net/postgres.ts +1521 -632
  90. package/std/net/prefork.ts +254 -98
  91. package/std/net/protobuf.ts +1017 -0
  92. package/std/net/qpack.ts +143 -92
  93. package/std/net/quic-conn-ack.ts +104 -22
  94. package/std/net/quic-conn-cid.ts +66 -26
  95. package/std/net/quic-conn-params.ts +13 -7
  96. package/std/net/quic-datagram.ts +90 -27
  97. package/std/net/quic-frame.ts +324 -121
  98. package/std/net/quic-listener.ts +206 -91
  99. package/std/net/quic-packet.ts +289 -163
  100. package/std/net/quic-recovery.ts +279 -128
  101. package/std/net/quic-stream.ts +466 -167
  102. package/std/net/quic.ts +584 -258
  103. package/std/net/router.ts +450 -99
  104. package/std/net/serve.ts +661 -240
  105. package/std/net/static.ts +1458 -0
  106. package/std/net/tls/client.ts +140 -45
  107. package/std/net/tls/codec.ts +510 -260
  108. package/std/net/tls/record-client.ts +268 -76
  109. package/std/net/tls/record-server.ts +236 -79
  110. package/std/net/tls/record.ts +173 -47
  111. package/std/net/tls/schedule.ts +58 -39
  112. package/std/net/tls/ticket.ts +295 -103
  113. package/std/net/tls/verify.ts +1 -1
  114. package/std/net/tls-tcp.ts +218 -75
  115. package/std/net/tls.ts +260 -152
  116. package/std/net/upstream.ts +283 -86
  117. package/std/net/url.ts +277 -102
  118. package/std/net/websocket.ts +532 -209
  119. package/std/net/webtransport.ts +755 -436
  120. package/std/regex.ts +1659 -829
  121. package/std/sqlite.ts +325 -0
  122. package/std/sync/ring.ts +320 -156
  123. package/std/sync/shm.ts +53 -6
  124. package/std/text.ts +285 -135
  125. package/std/threads.ts +22 -2
@@ -75,6 +75,15 @@ const JSON_NUMBER_LOWER_E: i32 = 101
75
75
  /** The byte at `i` of `s`, or `-1` past its end. */
76
76
  const jsonNumberByte = (s: string, i: i32): i32 => (i < toI32(s.length) ? toI32(s.charCodeAt(i)) : -1)
77
77
 
78
+ /** A digit's value, with room for the low four bits of any byte. */
79
+ type JsonNumberDigit = integer<0, 15>
80
+
81
+ /** What one digit's subtraction takes from the next one up. */
82
+ type JsonNumberBorrow = integer<0, 1>
83
+
84
+ /** A byte of the text, as `charCodeAt` answers it. */
85
+ type JsonNumberByte = integer<0, 65535>
86
+
78
87
  const jsonNumberIsDigit = (c: i32): boolean => c >= JSON_NUMBER_DIGIT_0 && c <= JSON_NUMBER_DIGIT_9
79
88
 
80
89
  /**
@@ -106,9 +115,8 @@ class JsonNumberParts {
106
115
  /** Where the integer part's digits start, and how many there are. */
107
116
  private intStart: i32
108
117
  private intLen: i32
109
- /** Where the fraction's digits start, after the point, and how many there are. */
118
+ /** Where the fraction's digits start, after the point. */
110
119
  private fracStart: i32
111
- private fracLen: i32
112
120
  /** The first significant digit's place among the integer and fraction digits run together. */
113
121
  private first: i32
114
122
  /**
@@ -135,6 +143,14 @@ class JsonNumberParts {
135
143
  * class is not exported.
136
144
  */
137
145
  last: i64
146
+ /**
147
+ * `last` plus 2^62, which no exponent's size reaches, so that two of them
148
+ * subtract in `u64` without a sign to carry.
149
+ *
150
+ * @internal Read by the `jsonNumber*` free functions of this module; the
151
+ * class is not exported.
152
+ */
153
+ lastBiased: u64
138
154
 
139
155
  constructor(text: string) {
140
156
  this.text = text
@@ -144,28 +160,39 @@ class JsonNumberParts {
144
160
  this.intStart = 0
145
161
  this.intLen = 0
146
162
  this.fracStart = 0
147
- this.fracLen = 0
148
163
  this.first = 0
149
164
  this.count = 0
150
165
  this.lead = toI64(0)
151
166
  this.last = toI64(0)
167
+ this.lastBiased = toU64(0)
152
168
  this.code = this.read()
153
169
  }
154
170
 
155
171
  /**
156
- * The k-th significant digit, `0 <= k < count`, as `0` to `9`.
172
+ * The k-th significant digit, `0 <= k < count`, as `0` to `9`: the low four
173
+ * bits of an ASCII digit are its value.
157
174
  *
158
175
  * @internal Read by the `jsonNumber*` free functions of this module; the
159
176
  * class is not exported.
160
177
  */
161
- digit(k: i32): i32 {
162
- return this.placeDigit(this.first + k)
178
+ digit(k: i32): JsonNumberDigit {
179
+ // Places run over digits `read` found in the text, so the sum fits.
180
+ const value: i32 = this.placeByte(toI32(toI64(this.first) + toI64(k))) & 15
181
+ return value
163
182
  }
164
183
 
165
- /** The digit at place `p` of the integer and fraction digits run together. */
166
- private placeDigit(p: i32): i32 {
167
- const at: i32 = p < this.intLen ? this.intStart + p : this.fracStart + (p - this.intLen)
168
- return toI32(this.text.charCodeAt(at)) - JSON_NUMBER_DIGIT_0
184
+ /** The byte of the digit at place `p` of the integer and fraction digits run together. */
185
+ private placeByte(p: i32): JsonNumberByte {
186
+ const s: string = this.text
187
+ const intLen: i32 = this.intLen
188
+ // Both positions are inside the text `read` measured them in, so they fit.
189
+ const at: i32 =
190
+ p < intLen
191
+ ? toI32(toI64(this.intStart) + toI64(p))
192
+ : toI32(toI64(this.fracStart) + toI64(p) - toI64(intLen))
193
+ // Every place `read` counted is a digit of the text, so a place outside
194
+ // it is never asked for; the `0` only keeps the read in bounds.
195
+ return at >= 0 && at < toI32(s.length) ? toI32(s.charCodeAt(at)) : 48
169
196
  }
170
197
 
171
198
  /** Reads the text against RFC 8259's number grammar; answers `0` or a code. */
@@ -175,83 +202,99 @@ class JsonNumberParts {
175
202
  let i: i32 = 0
176
203
  if (jsonNumberByte(s, i) === JSON_NUMBER_MINUS) {
177
204
  this.negative = true
178
- i += 1
205
+ i = 1
179
206
  }
180
- this.intStart = i
207
+ const intStart: i32 = i
208
+ this.intStart = intStart
181
209
  const lead: i32 = jsonNumberByte(s, i)
182
- if (!jsonNumberIsDigit(lead)) {
210
+ if (i >= n || !jsonNumberIsDigit(lead)) {
183
211
  return JSON_NUMBER_ERR_SYNTAX
184
212
  }
185
213
  i += 1
186
214
  if (lead !== JSON_NUMBER_DIGIT_0) {
187
- while (jsonNumberIsDigit(jsonNumberByte(s, i))) {
215
+ while (i < n && jsonNumberIsDigit(toI32(s.charCodeAt(i)))) {
188
216
  i += 1
189
217
  }
190
218
  }
191
- this.intLen = i - this.intStart
192
- if (jsonNumberByte(s, i) === JSON_NUMBER_DOT) {
219
+ const intLen: i32 = i - intStart
220
+ this.intLen = intLen
221
+ let fracLen: i32 = 0
222
+ if (i < n && toI32(s.charCodeAt(i)) === JSON_NUMBER_DOT) {
193
223
  i += 1
194
- this.fracStart = i
195
- while (jsonNumberIsDigit(jsonNumberByte(s, i))) {
224
+ const fracStart: i32 = i
225
+ this.fracStart = fracStart
226
+ while (i < n && jsonNumberIsDigit(toI32(s.charCodeAt(i)))) {
196
227
  i += 1
197
228
  }
198
- this.fracLen = i - this.fracStart
199
- if (this.fracLen === 0) {
229
+ fracLen = i - fracStart
230
+ if (fracLen === 0) {
200
231
  return JSON_NUMBER_ERR_SYNTAX
201
232
  }
202
233
  }
203
- let exponent: i64 = toI64(0)
234
+ // The exponent's size and sign apart, so that no step can overflow: the
235
+ // size stops growing past the bound.
236
+ let size: u64 = toU64(0)
237
+ let minus: boolean = false
204
238
  // An exponent past the bound is refused only once the digits are known
205
239
  // not all to be zero: `0e9999999999` is zero, as any other zero is.
206
240
  let big: boolean = false
207
241
  const e: i32 = jsonNumberByte(s, i)
208
- if (e === JSON_NUMBER_LOWER_E || e === JSON_NUMBER_UPPER_E) {
242
+ if (i < n && (e === JSON_NUMBER_LOWER_E || e === JSON_NUMBER_UPPER_E)) {
209
243
  i += 1
210
- let minus: boolean = false
211
244
  const sign: i32 = jsonNumberByte(s, i)
212
- if (sign === JSON_NUMBER_PLUS || sign === JSON_NUMBER_MINUS) {
245
+ if (i < n && (sign === JSON_NUMBER_PLUS || sign === JSON_NUMBER_MINUS)) {
213
246
  minus = sign === JSON_NUMBER_MINUS
214
247
  i += 1
215
248
  }
216
249
  if (!jsonNumberIsDigit(jsonNumberByte(s, i))) {
217
250
  return JSON_NUMBER_ERR_SYNTAX
218
251
  }
219
- while (jsonNumberIsDigit(jsonNumberByte(s, i))) {
252
+ while (i < n) {
253
+ const c: i32 = toI32(s.charCodeAt(i))
254
+ if (!jsonNumberIsDigit(c)) {
255
+ break
256
+ }
220
257
  // Adding stops past the bound, so a thousand-digit exponent neither
221
258
  // overflows nor is mistaken for a small one.
222
259
  if (!big) {
223
- exponent = exponent * toI64(10) + toI64(jsonNumberByte(s, i) - JSON_NUMBER_DIGIT_0)
224
- big = exponent > JSON_NUMBER_MAX_EXPONENT
260
+ size = size * toU64(10) + toU64(c - JSON_NUMBER_DIGIT_0)
261
+ big = size > toU64(JSON_NUMBER_MAX_EXPONENT)
225
262
  }
226
263
  i += 1
227
264
  }
228
- if (minus) {
229
- exponent = -exponent
230
- }
231
265
  }
232
266
  if (i !== n) {
233
267
  return JSON_NUMBER_ERR_SYNTAX
234
268
  }
235
- const places: i32 = this.intLen + this.fracLen
269
+ // Both counts are of digits in the text, so the sum fits.
270
+ const places: i32 = toI32(toI64(intLen) + toI64(fracLen))
236
271
  let first: i32 = 0
237
- while (first < places && this.placeDigit(first) === 0) {
272
+ while (first < places && this.placeByte(first) === JSON_NUMBER_DIGIT_0) {
238
273
  first += 1
239
274
  }
240
- if (first === places) {
275
+ if (first >= places || places <= 0) {
241
276
  return 0
242
277
  }
243
278
  if (big) {
244
279
  return JSON_NUMBER_ERR_EXPONENT
245
280
  }
246
281
  let last: i32 = places - 1
247
- while (this.placeDigit(last) === 0) {
282
+ // The digit at `first` is not a zero, so the walk stops there at the latest.
283
+ while (last > 0 && this.placeByte(last) === JSON_NUMBER_DIGIT_0) {
248
284
  last -= 1
249
285
  }
250
286
  this.zero = false
251
287
  this.first = first
252
- this.count = last - first + 1
253
- this.lead = exponent + toI64(this.intLen - 1 - first)
254
- this.last = exponent + toI64(this.intLen - 1 - last)
288
+ // Both are places, so the count fits.
289
+ this.count = toI32(toI64(last) - toI64(first) + toI64(1))
290
+ // A digit's exponent is the exponent plus `intLen - 1 - place`. The parts
291
+ // that add and the parts that take away are summed apart, each below
292
+ // 2^51, so the one subtraction is exact in `u64` and its result an `i64`.
293
+ const up: u64 = toU64(intLen) + (minus ? toU64(0) : size)
294
+ const down: u64 = minus ? size + toU64(1) : toU64(1)
295
+ this.lead = toI64(up - (down + toU64(first)))
296
+ this.last = toI64(up - (down + toU64(last)))
297
+ this.lastBiased = up + (toU64(1) << toU64(62)) - (down + toU64(last))
255
298
  return 0
256
299
  }
257
300
  }
@@ -309,8 +352,7 @@ export const jsonNumberCompare = (a: string, b: string): i32 => {
309
352
  if (sx === 0) {
310
353
  return 0
311
354
  }
312
- const size: i32 = jsonNumberCompareSize(x, y)
313
- return sx > 0 ? size : -size
355
+ return sx > 0 ? jsonNumberCompareSize(x, y) : jsonNumberCompareSize(y, x)
314
356
  }
315
357
 
316
358
  /**
@@ -338,7 +380,8 @@ export const jsonNumberCanonical = (text: string): string => {
338
380
  bytes.push(toU8(JSON_NUMBER_MINUS))
339
381
  }
340
382
  for (let k: i32 = 0; k < p.count; k++) {
341
- bytes.push(toU8(p.digit(k) + JSON_NUMBER_DIGIT_0))
383
+ const digit: JsonNumberDigit = p.digit(k)
384
+ bytes.push(toU8(digit + JSON_NUMBER_DIGIT_0))
342
385
  }
343
386
  const digits: string = String.fromBytes(bytes, 0, toI32(bytes.length))
344
387
  return `${digits}e${p.last}`
@@ -366,40 +409,42 @@ const jsonNumberAtLeast = (rem: u8[], d: JsonNumberParts): boolean => {
366
409
  * has; each step shifts a digit in and takes the divisor away at most nine
367
410
  * times.
368
411
  */
369
- const jsonNumberDividesLong = (v: JsonNumberParts, d: JsonNumberParts, total: i64): boolean => {
412
+ const jsonNumberDividesLong = (v: JsonNumberParts, d: JsonNumberParts, total: i32): boolean => {
370
413
  const n: i32 = d.count
371
- const rem: u8[] = []
372
- for (let k: i32 = 0; k <= n; k++) {
414
+ const rem: u8[] = [0]
415
+ for (let k: i32 = 0; k < n; k++) {
373
416
  rem.push(0)
374
417
  }
375
- for (let step: i64 = toI64(0); step < total; step++) {
418
+ const width: i32 = toI32(rem.length)
419
+ // `rem` was just given `n + 1` bytes, so this never answers: it is what
420
+ // lets every read of the top byte below go unchecked.
421
+ if (toI32(rem.length) < 1) {
422
+ return false
423
+ }
424
+ for (let step: i32 = 0; step < total; step++) {
425
+ let next: i32 = 0
426
+ if (step < v.count) {
427
+ next = v.digit(step)
428
+ }
376
429
  // The remainder is below the divisor, so its top byte is zero and the
377
430
  // shift loses nothing.
378
- for (let k: i32 = 0; k + 1 < toI32(rem.length); k++) {
379
- rem[k] = rem[k + 1]
380
- }
381
- let next: i32 = 0
382
- if (step < toI64(v.count)) {
383
- next = v.digit(toI32(step))
431
+ for (let k: i32 = 0; k < width; k++) {
432
+ rem[k] = k + 1 < width ? rem[k + 1] : toU8(next)
384
433
  }
385
- rem[toI32(rem.length) - 1] = toU8(next)
386
434
  // Below ten divisors now: a non-zero top byte is n + 1 digits against n,
387
435
  // so certainly at least the divisor; otherwise the low n digits decide.
388
436
  while (rem[0] > 0 || jsonNumberAtLeast(rem, d)) {
389
- let borrow: i32 = 0
390
- for (let k: i32 = toI32(rem.length) - 1; k >= 1; k--) {
391
- let x: i32 = toI32(rem[k]) - d.digit(k - 1) - borrow
392
- borrow = 0
393
- if (x < 0) {
394
- x += 10
395
- borrow = 1
396
- }
397
- rem[k] = toU8(x)
437
+ let borrow: JsonNumberBorrow = 0
438
+ for (let k: i32 = width - 1; k >= 1; k--) {
439
+ const y: JsonNumberDigit = d.digit(k - 1)
440
+ const x: i32 = toI32(rem[k]) - y - borrow
441
+ borrow = x < 0 ? 1 : 0
442
+ rem[k] = toU8(x < 0 ? x + 10 : x)
398
443
  }
399
444
  rem[0] = toU8(toI32(rem[0]) - borrow)
400
445
  }
401
446
  }
402
- for (let k: i32 = 0; k < toI32(rem.length); k++) {
447
+ for (let k: i32 = 0; k < width; k++) {
403
448
  if (rem[k] !== 0) {
404
449
  return false
405
450
  }
@@ -411,14 +456,19 @@ const jsonNumberDividesLong = (v: JsonNumberParts, d: JsonNumberParts, total: i6
411
456
  * The same for a divisor of at most 18 digits, whose remainder fits a `u64`
412
457
  * with a digit shifted in: one multiply and one remainder per step.
413
458
  */
414
- const jsonNumberDividesShort = (v: JsonNumberParts, d: JsonNumberParts, total: i64): boolean => {
459
+ const jsonNumberDividesShort = (v: JsonNumberParts, d: JsonNumberParts, total: i32): boolean => {
415
460
  let divisor: u64 = toU64(0)
416
461
  for (let k: i32 = 0; k < d.count; k++) {
417
462
  divisor = divisor * toU64(10) + toU64(d.digit(k))
418
463
  }
464
+ // The divisor's first digit is not a zero, so this never answers: it is
465
+ // what lets the remainders below go unchecked.
466
+ if (divisor === toU64(0)) {
467
+ return false
468
+ }
419
469
  let rem: u64 = toU64(0)
420
- for (let step: i64 = toI64(0); step < total; step++) {
421
- const digit: u64 = step < toI64(v.count) ? toU64(v.digit(toI32(step))) : toU64(0)
470
+ for (let step: i32 = 0; step < total; step++) {
471
+ const digit: u64 = step < v.count ? toU64(v.digit(step)) : toU64(0)
422
472
  rem = (rem * toU64(10) + digit) % divisor
423
473
  }
424
474
  return rem === toU64(0)
@@ -460,19 +510,23 @@ export const jsonNumberMultipleOf = (value: string, divisor: string): i32 => {
460
510
  // value = V * 10^v.last and divisor = D * 10^d.last, neither V nor D ending
461
511
  // in a zero. A value whose last digit is finer than the divisor's would need
462
512
  // D * 10^k to divide V for some k above zero, and then ten would divide V.
463
- const gap: i64 = v.last - d.last
464
- if (gap < toI64(0)) {
513
+ if (v.lastBiased < d.lastBiased) {
465
514
  return 0
466
515
  }
467
- const total: i64 = toI64(v.count) + gap
516
+ // Both exponents are below 2^52 either way, so neither sum wraps.
517
+ const total: u64 = toU64(v.count) + (v.lastBiased - d.lastBiased)
468
518
  const short: boolean = d.count <= JSON_NUMBER_SMALL_DIVISOR
469
- // A module constant is a plain number under Node, where an `i64` is a
470
- // BigInt: comparing the two is allowed, dividing them is not.
471
- if (total > (short ? JSON_NUMBER_MAX_STEPS : toI64(JSON_NUMBER_MAX_STEPS) / toI64(d.count))) {
519
+ // A long divisor's steps each walk its digits: `total` past the bound over
520
+ // `d.count` is `total * d.count` past the bound, and with `total` within the
521
+ // bound that product is far below 2^64.
522
+ const most: u64 = toU64(JSON_NUMBER_MAX_STEPS)
523
+ if (total > most || (!short && total * toU64(d.count) > most)) {
472
524
  return JSON_NUMBER_ERR_GAP
473
525
  }
526
+ // Within the bound, 2^20, so the count fits.
527
+ const steps: i32 = toI32(total)
474
528
  if (short) {
475
- return jsonNumberDividesShort(v, d, total) ? 1 : 0
529
+ return jsonNumberDividesShort(v, d, steps) ? 1 : 0
476
530
  }
477
- return jsonNumberDividesLong(v, d, total) ? 1 : 0
531
+ return jsonNumberDividesLong(v, d, steps) ? 1 : 0
478
532
  }
@@ -91,13 +91,22 @@ export const isJsonPointer = (s: string): boolean => jsonPointerWellFormed(s, 0)
91
91
  * `0`, or past a digit run led by `1`-`9`. `-1` when no digit is there.
92
92
  */
93
93
  const jsonPointerIntegerEnd = (s: string, from: i32): i32 => {
94
+ const n: i32 = toI32(s.length)
95
+ // Past either end there is no byte, so no digit: the same `-1` the digit test below gives.
96
+ if (from < 0 || from >= n) {
97
+ return -1
98
+ }
94
99
  const first: i32 = jsonPointerByte(s, from)
95
100
  if (first < JSON_POINTER_DIGIT_0 || first > JSON_POINTER_DIGIT_9) {
96
101
  return -1
97
102
  }
98
103
  let i: i32 = from + 1
99
104
  if (first !== JSON_POINTER_DIGIT_0) {
100
- while (jsonPointerByte(s, i) >= JSON_POINTER_DIGIT_0 && jsonPointerByte(s, i) <= JSON_POINTER_DIGIT_9) {
105
+ while (
106
+ i < n &&
107
+ jsonPointerByte(s, i) >= JSON_POINTER_DIGIT_0 &&
108
+ jsonPointerByte(s, i) <= JSON_POINTER_DIGIT_9
109
+ ) {
101
110
  i += 1
102
111
  }
103
112
  }
@@ -114,13 +123,18 @@ const jsonPointerIntegerEnd = (s: string, from: i32): i32 => {
114
123
  * @returns true when `s` is a relative JSON Pointer.
115
124
  */
116
125
  export const isRelativeJsonPointer = (s: string): boolean => {
126
+ const n: i32 = toI32(s.length)
117
127
  let i: i32 = jsonPointerIntegerEnd(s, 0)
118
128
  if (i < 0) {
119
129
  return false
120
130
  }
131
+ // The integer ends at the end of `s` or before a byte that is no `#`, `+` or `-`: the rest is the pointer.
132
+ if (i >= n) {
133
+ return jsonPointerWellFormed(s, i)
134
+ }
121
135
  const next: i32 = jsonPointerByte(s, i)
122
136
  if (next === JSON_POINTER_HASH) {
123
- return i + 1 === toI32(s.length)
137
+ return i + 1 === n
124
138
  }
125
139
  if (next === JSON_POINTER_PLUS || next === JSON_POINTER_MINUS) {
126
140
  i = jsonPointerIntegerEnd(s, i + 1)
@@ -131,6 +145,13 @@ export const isRelativeJsonPointer = (s: string): boolean => {
131
145
  return jsonPointerWellFormed(s, i)
132
146
  }
133
147
 
148
+ /**
149
+ * `s[from, to)`. Every caller's range lies inside `s`, so `substring`'s clamp
150
+ * never moves it; it runs once per piece copied, beside that piece's
151
+ * allocation, where `slice` would add a panic path no input reaches.
152
+ */
153
+ const jsonPointerPiece = (s: string, from: i32, to: i32): string => s.substring(from, to)
154
+
134
155
  /**
135
156
  * The token of well-formed pointer `p` that starts at `from` and ends before
136
157
  * `end`, with `~1` and `~0` decoded. The runs between escapes are copied
@@ -141,16 +162,18 @@ const jsonPointerToken = (p: string, from: i32, end: i32): string => {
141
162
  let run: i32 = from
142
163
  let i: i32 = from
143
164
  while (i < end) {
144
- if (jsonPointerByte(p, i) === JSON_POINTER_TILDE) {
145
- parts.push(p.slice(run, i))
146
- parts.push(jsonPointerByte(p, i + 1) === JSON_POINTER_DIGIT_1 ? "/" : "~")
147
- i += 2
165
+ const next: i32 = i + 1
166
+ // Well-formed, every `~` has its digit before `end`; a `~` with none would be copied as itself.
167
+ if (next < end && jsonPointerByte(p, i) === JSON_POINTER_TILDE) {
168
+ parts.push(jsonPointerPiece(p, run, i))
169
+ parts.push(jsonPointerByte(p, next) === JSON_POINTER_DIGIT_1 ? "/" : "~")
170
+ i = next + 1
148
171
  run = i
149
172
  } else {
150
- i += 1
173
+ i = next
151
174
  }
152
175
  }
153
- parts.push(p.slice(run, end))
176
+ parts.push(jsonPointerPiece(p, run, end))
154
177
  return parts.join("")
155
178
  }
156
179
 
@@ -198,12 +221,12 @@ export const jsonPointerEscape = (token: string): string => {
198
221
  for (let i: i32 = 0; i < toI32(token.length); i++) {
199
222
  const c: i32 = jsonPointerByte(token, i)
200
223
  if (c === JSON_POINTER_TILDE || c === JSON_POINTER_SLASH) {
201
- parts.push(token.slice(run, i))
224
+ parts.push(jsonPointerPiece(token, run, i))
202
225
  parts.push(c === JSON_POINTER_TILDE ? "~0" : "~1")
203
226
  run = i + 1
204
227
  }
205
228
  }
206
- parts.push(token.slice(run))
229
+ parts.push(jsonPointerPiece(token, run, toI32(token.length)))
207
230
  return parts.join("")
208
231
  }
209
232
 
@@ -214,7 +237,7 @@ export const jsonPointerEscape = (token: string): string => {
214
237
  * of any length is read without overflow.
215
238
  */
216
239
  const jsonPointerIndex = (p: string, from: i32, end: i32, length: i32): i32 => {
217
- if (from === end || (end - from > 1 && jsonPointerByte(p, from) === JSON_POINTER_DIGIT_0)) {
240
+ if (from === end) {
218
241
  return -1
219
242
  }
220
243
  let value: i32 = 0
@@ -223,10 +246,16 @@ const jsonPointerIndex = (p: string, from: i32, end: i32, length: i32): i32 => {
223
246
  if (c < JSON_POINTER_DIGIT_0 || c > JSON_POINTER_DIGIT_9) {
224
247
  return -1
225
248
  }
226
- value = value * 10 + (c - JSON_POINTER_DIGIT_0)
227
- if (value >= length) {
249
+ // Past the first digit, a value still 0 means the token led with a zero.
250
+ if (i > from && value === 0) {
251
+ return -1
252
+ }
253
+ // Widened, ten times anything below `length` plus a digit cannot wrap; below `length` it narrows back whole.
254
+ const next: i64 = toI64(value) * 10 + (toI64(c) - toI64(JSON_POINTER_DIGIT_0))
255
+ if (next >= toI64(length)) {
228
256
  return -1
229
257
  }
258
+ value = toI32(next)
230
259
  }
231
260
  return value
232
261
  }