@amritk/nish-x86_64-linux 0.38.0 → 0.40.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 (86) hide show
  1. package/INSTALL.md +74 -129
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +455 -65
  4. package/package.json +1 -1
  5. package/runtime/core/host.d.ts +11 -0
  6. package/runtime/core/host.mjs +35 -0
  7. package/runtime/nish.d.ts +320 -33
  8. package/runtime/nish.h +84 -10
  9. package/runtime/nish.mjs +69 -6
  10. package/runtime/node.mjs +117 -0
  11. package/runtime/page-frame.d.ts +123 -0
  12. package/runtime/page-frame.mjs +736 -0
  13. package/runtime/page-game.d.ts +132 -0
  14. package/runtime/page-game.mjs +738 -0
  15. package/runtime/runtime-aes.c +97 -12
  16. package/runtime/runtime-async.c +43 -10
  17. package/runtime/runtime-bytes.c +88 -4
  18. package/runtime/runtime-checksum.c +207 -15
  19. package/runtime/runtime-net.c +64 -3
  20. package/runtime/runtime-os.c +174 -27
  21. package/runtime/runtime-region.c +94 -10
  22. package/runtime/runtime-sandbox.c +404 -17
  23. package/runtime/runtime-sha256.c +11 -4
  24. package/runtime/runtime-wasm.c +437 -0
  25. package/runtime/runtime.c +5 -0
  26. package/runtime/vite.mjs +1 -1
  27. package/scripts/build.sh +42 -8
  28. package/std/README.md +78 -91
  29. package/std/array.ts +36 -3
  30. package/std/collections.ts +51 -0
  31. package/std/compress/checksum.ts +13 -0
  32. package/std/compress/deflate.ts +0 -2
  33. package/std/crypto/hmac.ts +37 -10
  34. package/std/crypto/p256.ts +1629 -2732
  35. package/std/crypto/p384.ts +3 -1
  36. package/std/crypto/pki.ts +151 -13
  37. package/std/crypto/sha256.ts +34 -6
  38. package/std/crypto/x25519.ts +153 -64
  39. package/std/crypto/x509.ts +120 -10
  40. package/std/host/game.ts +273 -0
  41. package/std/input.ts +56 -0
  42. package/std/json-tokenizer.ts +344 -111
  43. package/std/json-typed.ts +256 -12
  44. package/std/json-value.ts +144 -3
  45. package/std/json.ts +63 -8
  46. package/std/net/fetch.ts +22 -4
  47. package/std/net/handler.ts +130 -16
  48. package/std/net/http-server.ts +3382 -0
  49. package/std/net/http1-client-codec.ts +6 -2
  50. package/std/net/http1-codec.ts +636 -162
  51. package/std/net/http1-server.ts +569 -42
  52. package/std/net/http2-cleartext.ts +526 -0
  53. package/std/net/http2-tls.ts +48 -0
  54. package/std/net/http2.ts +131 -14
  55. package/std/net/http3-server.ts +29 -3
  56. package/std/net/http3.ts +180 -7
  57. package/std/net/postgres-async.ts +1304 -86
  58. package/std/net/postgres-carrier.ts +343 -10
  59. package/std/net/postgres.ts +122 -7
  60. package/std/net/prefork.ts +195 -11
  61. package/std/net/quic-datagram.ts +30 -0
  62. package/std/net/quic-listener.ts +38 -12
  63. package/std/net/quic-packet.ts +19 -6
  64. package/std/net/quic-recovery.ts +373 -27
  65. package/std/net/quic-stream.ts +195 -37
  66. package/std/net/quic.ts +516 -22
  67. package/std/net/router.ts +407 -38
  68. package/std/net/serve.ts +1022 -110
  69. package/std/net/tls/codec.ts +110 -32
  70. package/std/net/tls/record-server.ts +37 -7
  71. package/std/net/tls/verify.ts +19 -1
  72. package/std/net/tls-tcp.ts +56 -1
  73. package/std/net/tls.ts +196 -50
  74. package/std/net/url.ts +83 -35
  75. package/std/net/websocket.ts +129 -19
  76. package/std/net/webtransport.ts +185 -1
  77. package/std/raster.ts +837 -0
  78. package/std/regex.ts +69 -47
  79. package/std/region.ts +62 -11
  80. package/std/release.ts +593 -0
  81. package/std/secret.ts +26 -59
  82. package/std/synth.ts +143 -0
  83. package/std/text.ts +233 -0
  84. package/std/threads.ts +298 -0
  85. package/std/yaml-json.ts +368 -28
  86. package/std/yaml.ts +122 -27
@@ -52,11 +52,13 @@
52
52
  * the decoded bytes of the current key or string into the caller's array, and
53
53
  * `keyEquals` compares them with a string without writing them anywhere.
54
54
  *
55
- * **Numbers are checked against the grammar and parsed on request.**
56
- * `numberF64` is correctly rounded, ties to even, in one of three ways. A
57
- * number of at most 19 significant digits is read into a `u64`. When that is at
58
- * most 2^53 and the decimal exponent is within ±22, the answer is one exact
59
- * multiply or divide of two exact doubles (Clinger's fast path).
55
+ * **Numbers are read in the pass that checks them against the grammar.**
56
+ * The scan adds the digits into a `u64` as it steps over them, eight at a time
57
+ * where eight digits fit before the window's end, and keeps that mantissa, the
58
+ * decimal exponent and the digit count for `numberF64`, which is correctly
59
+ * rounded, ties to even, in one of three ways. When the mantissa of at most 19
60
+ * digits is at most 2^53 and the decimal exponent is within ±22, the answer is
61
+ * one exact multiply or divide of two exact doubles (Clinger's fast path).
60
62
  *
61
63
  * Any other number of at most 19 digits is the Eisel–Lemire algorithm as
62
64
  * fast_float computes it: one or two 64×64-bit products with a truncated power
@@ -96,8 +98,9 @@
96
98
  * (`std/README.md`). Every offset is a byte offset, and every constant is an
97
99
  * `i32`, so the module is the same under `--number-mode f64`
98
100
  * (`docs/wp26-stdlib.md` §4). Written from RFC 8259 and RFC 3629, except
99
- * `numberF64`'s Eisel–Lemire path and its table of powers of five, which are
100
- * adapted from fast_float under the notice above `jsonTokW64`.
101
+ * `numberF64`'s Eisel–Lemire path, its table of powers of five and the scan's
102
+ * eight-digit reads, which are adapted from fast_float under the notice above
103
+ * `jsonTokW64`.
101
104
  */
102
105
 
103
106
  import { copyBytes, skipBlank, skipJsonText } from "nish/bytes"
@@ -202,6 +205,8 @@ const JSON_TOK_WANT_KEY_OR_CLOSE: i32 = 2
202
205
  const JSON_TOK_WANT_COLON: i32 = 3
203
206
  const JSON_TOK_WANT_COMMA_OR_CLOSE: i32 = 4
204
207
  const JSON_TOK_WANT_NOTHING: i32 = 5
208
+ /** After an error: `fail` sets it, so `next` answers the sticky error from its switch. */
209
+ const JSON_TOK_WANT_FAILED: i32 = 6
205
210
 
206
211
  /** What the depth stack holds for an open container. */
207
212
  const JSON_TOK_IN_OBJECT: i32 = 1
@@ -330,6 +335,30 @@ const jsonTokIsBlank = (b: i32): boolean =>
330
335
 
331
336
  const jsonTokIsDigit = (b: i32): boolean => b >= JSON_TOK_DIGIT_0 && b <= JSON_TOK_DIGIT_9
332
337
 
338
+ /** `numberDigits` for a number whose exponent ran past `JSON_TOK_EXPONENT_CAP`: more than any 19. */
339
+ const JSON_TOK_NUMBER_WALK: i32 = 2147483647
340
+
341
+ /** The most digits an integral number's `numberMantissa` holds that are sure to fit an `i64`. */
342
+ const JSON_TOK_EXACT_DIGITS: i32 = 18
343
+
344
+ /**
345
+ * The eight bytes at `i`, the first in the low byte. The caller has checked
346
+ * `i + 8 <= src.length`; the test here repeats it so each index is proved.
347
+ */
348
+ const jsonTokLoad8 = (src: u8[], i: i32): u64 => {
349
+ if (i < 0 || i >= JSON_TOK_END_CEILING || i + 8 > toI32(src.length)) {
350
+ return toU64(0)
351
+ }
352
+ return (
353
+ toU64(src[i]) |
354
+ (toU64(src[i + 1]) << toU64(8)) |
355
+ ((toU64(src[i + 2]) << toU64(16)) | (toU64(src[i + 3]) << toU64(24))) |
356
+ ((toU64(src[i + 4]) << toU64(32)) |
357
+ (toU64(src[i + 5]) << toU64(40)) |
358
+ ((toU64(src[i + 6]) << toU64(48)) | (toU64(src[i + 7]) << toU64(56))))
359
+ )
360
+ }
361
+
333
362
  /**
334
363
  * The first index at or after `i` that is not a blank, or `end`. The run is
335
364
  * read straight out of `src`, bounded by both `end` and the array's length,
@@ -356,21 +385,6 @@ const jsonTokSkipBlank = (src: u8[], i: i32, end: i32): i32 => {
356
385
  return skipBlank(src, i + 2, to)
357
386
  }
358
387
 
359
- /** The first index at or after `i` that is not a digit, or `end`, read as `jsonTokSkipBlank` reads its run. */
360
- const jsonTokSkipDigits = (src: u8[], i: i32, end: i32): i32 => {
361
- if (i < 0) {
362
- return i
363
- }
364
- // A number's digits are short runs, which a call into `skipDigits`'s
365
- // kernel costs more than it saves (WP38 S6a), so they stay a loop here.
366
- const n: i32 = toI32(src.length)
367
- let j: i32 = i
368
- while (j < end && j < n && jsonTokIsDigit(toI32(src[j]))) {
369
- j += 1
370
- }
371
- return j
372
- }
373
-
374
388
  /**
375
389
  * The first index at or after `i` that is not a plain string byte, or
376
390
  * `limit`, read as `jsonTokSkipBlank` reads its run. A plain byte is one a
@@ -559,6 +573,12 @@ export class JsonTokenizer {
559
573
  private integerValue: i64
560
574
  /** The integral number `unsignedScan` read last. */
561
575
  private unsignedValue: u64
576
+ /**
577
+ * The current number's digits, the integer part's and then the fraction's,
578
+ * read as one integer, as `number()` scanned them. It has wrapped when
579
+ * `numberDigits` is past 19, and is not read then.
580
+ */
581
+ private numberMantissa: u64
562
582
  /** One byte per open container, `JSON_TOK_IN_OBJECT` or `JSON_TOK_IN_ARRAY`, `maxDepth` long. */
563
583
  private stack: u8[]
564
584
  /**
@@ -572,6 +592,12 @@ export class JsonTokenizer {
572
592
  private pos: i32
573
593
  private maxDepth: i32
574
594
  private maxStringBytes: i32
595
+ /**
596
+ * The first opening quote whose string could pass `maxStringBytes` before
597
+ * `end`: `end - 1 - maxStringBytes`, set with `end`, so `string` finds its
598
+ * limit with one comparison.
599
+ */
600
+ private stringFrom: i32
575
601
  private maxNumberBytes: i32
576
602
  /**
577
603
  * @internal Read by std/yaml-json.ts, which caps the text a YAML load stores
@@ -580,6 +606,12 @@ export class JsonTokenizer {
580
606
  maxTotalBytes: i32
581
607
  /** Containers open after the current token. */
582
608
  depth: i32
609
+ /**
610
+ * The innermost open container, `JSON_TOK_IN_OBJECT` or `JSON_TOK_IN_ARRAY`,
611
+ * or `0` at the top level: the stack's last byte, kept here by `open` and
612
+ * `close` so a comma and a closer read no stack.
613
+ */
614
+ private inside: i32
583
615
  /** What `next()` expects at `pos`. */
584
616
  private want: i32
585
617
  /** The current token's kind, the last `next()` answered; `JSON_TOK_END` before the first. */
@@ -598,11 +630,20 @@ export class JsonTokenizer {
598
630
  errorAt: i32
599
631
  /** The current string's or key's length once decoded; shorter than its raw length exactly when it holds an escape. */
600
632
  private stringBytes: i32
633
+ /** The current number's exponent less the count of its fraction's digits, so its value is `numberMantissa × 10^numberScale`. */
634
+ private numberScale: i32
635
+ /**
636
+ * How many digits `numberMantissa` holds, leading zeros included, or
637
+ * `JSON_TOK_NUMBER_WALK` when the exponent ran past `JSON_TOK_EXPONENT_CAP`.
638
+ */
639
+ private numberDigits: i32
601
640
  /**
602
641
  * Whether the current number is written with neither a fraction nor an
603
642
  * exponent: the spelling, so `1.0` is not. `isIntegral()` reads the value.
604
643
  */
605
644
  integral: boolean
645
+ /** Whether the current number starts with `-`. */
646
+ private numberNegative: boolean
606
647
 
607
648
  /**
608
649
  * A tokenizer over `src[off, off + len)` held to `limits`. A negative limit
@@ -624,8 +665,10 @@ export class JsonTokenizer {
624
665
  this.src = src
625
666
  this.off = 0
626
667
  this.end = 0
668
+ this.stringFrom = -1 - this.maxStringBytes
627
669
  this.pos = 0
628
670
  this.depth = 0
671
+ this.inside = 0
629
672
  this.want = JSON_TOK_WANT_VALUE
630
673
  this.kind = JSON_TOK_END
631
674
  this.tokStart = 0
@@ -636,6 +679,10 @@ export class JsonTokenizer {
636
679
  this.integral = false
637
680
  this.integerValue = toI64(0)
638
681
  this.unsignedValue = toU64(0)
682
+ this.numberMantissa = toU64(0)
683
+ this.numberScale = 0
684
+ this.numberDigits = 0
685
+ this.numberNegative = false
639
686
  this.reset(src, off, len)
640
687
  }
641
688
 
@@ -651,6 +698,7 @@ export class JsonTokenizer {
651
698
  reset(src: u8[], off: i32, len: i32): void {
652
699
  this.src = src
653
700
  this.depth = 0
701
+ this.inside = 0
654
702
  this.want = JSON_TOK_WANT_VALUE
655
703
  this.kind = JSON_TOK_END
656
704
  this.tokStart = off
@@ -663,12 +711,15 @@ export class JsonTokenizer {
663
711
  if (off < 0 || len < 0 || off > toI32(src.length) - len || off + len > JSON_TOK_END_CEILING) {
664
712
  this.off = 0
665
713
  this.end = 0
714
+ this.stringFrom = -1 - this.maxStringBytes
666
715
  this.pos = 0
667
716
  this.fail(JSON_TOK_ERR_WINDOW, off)
668
717
  return
669
718
  }
670
719
  this.off = off
671
720
  this.end = off + len
721
+ // Fits: `end` and `maxStringBytes` are both at least 0.
722
+ this.stringFrom = off + len - 1 - this.maxStringBytes
672
723
  this.pos = off
673
724
  if (len > this.maxTotalBytes) {
674
725
  this.fail(JSON_TOK_ERR_TOTAL_LIMIT, off + this.maxTotalBytes)
@@ -680,6 +731,7 @@ export class JsonTokenizer {
680
731
  if (this.failed === 0) {
681
732
  this.failed = code
682
733
  this.errorAt = at
734
+ this.want = JSON_TOK_WANT_FAILED
683
735
  }
684
736
  return this.failed
685
737
  }
@@ -693,65 +745,84 @@ export class JsonTokenizer {
693
745
  * repeats.
694
746
  */
695
747
  next(): i32 {
696
- if (this.failed !== 0) {
697
- return this.failed
698
- }
699
748
  const src: u8[] = this.src
700
749
  const end: i32 = this.end
701
- const at: i32 = jsonTokSkipBlank(src, this.pos, end)
702
- const b: i32 = jsonTokByteAt(src, at, end)
703
- switch (this.want) {
704
- case JSON_TOK_WANT_NOTHING:
705
- if (b < 0) {
706
- this.pos = at
707
- this.kind = JSON_TOK_END
708
- return JSON_TOK_END
750
+ // `jsonTokSkipBlank` and then `jsonTokByteAt`, fused: both bounds are
751
+ // folded into `to` once, and the byte the skip stops at is the one read.
752
+ const n: i32 = toI32(src.length)
753
+ const to: i32 = end < n ? end : n
754
+ let at: i32 = this.pos
755
+ let b: i32 = -1
756
+ if (at >= 0 && at < to) {
757
+ b = toI32(src[at])
758
+ if (jsonTokIsBlank(b)) {
759
+ at = jsonTokSkipBlank(src, at + 1, to)
760
+ b = at >= 0 && at < to ? toI32(src[at]) : -1
761
+ }
762
+ }
763
+ const want: i32 = this.want
764
+ // The byte is switched on first, so a quote, colon, comma or closer, most
765
+ // of a document's tokens, takes one switch and one test of `want`.
766
+ // Anything else, and any of these where `want` refuses it, goes on to the
767
+ // switch on `want` below, which answers it as it always has.
768
+ switch (b) {
769
+ case JSON_TOK_QUOTE:
770
+ if (want === JSON_TOK_WANT_KEY_OR_CLOSE) {
771
+ return this.string(at, JSON_TOK_KEY)
709
772
  }
710
- return this.fail(JSON_TOK_ERR_TRAILING, at)
711
- case JSON_TOK_WANT_COLON:
712
- if (b === JSON_TOK_COLON) {
713
- return this.value(jsonTokSkipBlank(src, at + 1, end))
773
+ if (want === JSON_TOK_WANT_VALUE || want === JSON_TOK_WANT_VALUE_OR_CLOSE) {
774
+ return this.string(at, JSON_TOK_STRING)
714
775
  }
715
- return this.fail(b < 0 ? JSON_TOK_ERR_TRUNCATED : JSON_TOK_ERR_SYNTAX, at)
716
- case JSON_TOK_WANT_KEY_OR_CLOSE:
717
- if (b === JSON_TOK_CLOSE_BRACE) {
718
- return this.close(at, JSON_TOK_IN_OBJECT)
719
- }
720
- return this.key(at)
721
- case JSON_TOK_WANT_VALUE_OR_CLOSE:
722
- if (b === JSON_TOK_CLOSE_BRACKET) {
723
- return this.close(at, JSON_TOK_IN_ARRAY)
776
+ break
777
+ case JSON_TOK_COLON:
778
+ if (want === JSON_TOK_WANT_COLON) {
779
+ return this.value(jsonTokSkipBlank(src, at + 1, end))
724
780
  }
725
- return this.value(at)
726
- case JSON_TOK_WANT_COMMA_OR_CLOSE:
727
- if (b === JSON_TOK_COMMA) {
781
+ break
782
+ case JSON_TOK_COMMA:
783
+ if (want === JSON_TOK_WANT_COMMA_OR_CLOSE) {
728
784
  const after: i32 = jsonTokSkipBlank(src, at + 1, end)
729
- if (this.top() === JSON_TOK_IN_OBJECT) {
785
+ if (this.inside === JSON_TOK_IN_OBJECT) {
730
786
  return this.key(after)
731
787
  }
732
788
  return this.value(after)
733
789
  }
734
- if (b === JSON_TOK_CLOSE_BRACE) {
790
+ break
791
+ case JSON_TOK_CLOSE_BRACE:
792
+ if (want === JSON_TOK_WANT_COMMA_OR_CLOSE || want === JSON_TOK_WANT_KEY_OR_CLOSE) {
735
793
  return this.close(at, JSON_TOK_IN_OBJECT)
736
794
  }
737
- if (b === JSON_TOK_CLOSE_BRACKET) {
795
+ break
796
+ case JSON_TOK_CLOSE_BRACKET:
797
+ if (want === JSON_TOK_WANT_COMMA_OR_CLOSE || want === JSON_TOK_WANT_VALUE_OR_CLOSE) {
738
798
  return this.close(at, JSON_TOK_IN_ARRAY)
739
799
  }
800
+ break
801
+ default:
802
+ break
803
+ }
804
+ switch (want) {
805
+ case JSON_TOK_WANT_FAILED:
806
+ return this.failed
807
+ case JSON_TOK_WANT_NOTHING:
808
+ if (b < 0) {
809
+ this.pos = at
810
+ this.kind = JSON_TOK_END
811
+ return JSON_TOK_END
812
+ }
813
+ return this.fail(JSON_TOK_ERR_TRAILING, at)
814
+ // A byte the switch above took is not met again here: each case
815
+ // below answers only what its `want` refuses or reads on to.
816
+ case JSON_TOK_WANT_COLON:
817
+ case JSON_TOK_WANT_COMMA_OR_CLOSE:
740
818
  return this.fail(b < 0 ? JSON_TOK_ERR_TRUNCATED : JSON_TOK_ERR_SYNTAX, at)
819
+ case JSON_TOK_WANT_KEY_OR_CLOSE:
820
+ return this.key(at)
741
821
  default:
742
822
  return this.value(at)
743
823
  }
744
824
  }
745
825
 
746
- /** The kind of the innermost open container, or `0` at the top level. */
747
- private top(): i32 {
748
- const d: i32 = this.depth
749
- if (d <= 0 || d > toI32(this.stack.length)) {
750
- return 0
751
- }
752
- return toI32(this.stack[d - 1])
753
- }
754
-
755
826
  /** Records the token `src[start, stop)` of kind `k` and what may follow it. */
756
827
  private emit(k: i32, start: i32, stop: i32): i32 {
757
828
  this.kind = k
@@ -772,10 +843,14 @@ export class JsonTokenizer {
772
843
 
773
844
  /** A closer at `at`, which must match the innermost container, `inside`. */
774
845
  private close(at: i32, inside: i32): i32 {
775
- if (this.top() !== inside) {
846
+ if (this.inside !== inside) {
776
847
  return this.fail(JSON_TOK_ERR_SYNTAX, at)
777
848
  }
778
- this.depth -= 1
849
+ // The container closed was the stack's last byte, so the one now
850
+ // innermost is the byte before it, or none.
851
+ const d: i32 = this.depth - 1
852
+ this.depth = d
853
+ this.inside = d > 0 && d <= toI32(this.stack.length) ? toI32(this.stack[d - 1]) : 0
779
854
  return this.emit(inside === JSON_TOK_IN_OBJECT ? JSON_TOK_OBJECT_END : JSON_TOK_ARRAY_END, at, at + 1)
780
855
  }
781
856
 
@@ -787,6 +862,7 @@ export class JsonTokenizer {
787
862
  }
788
863
  this.stack[d] = toU8(inside)
789
864
  this.depth = d + 1
865
+ this.inside = inside
790
866
  return this.emit(inside === JSON_TOK_IN_OBJECT ? JSON_TOK_OBJECT_BEGIN : JSON_TOK_ARRAY_BEGIN, at, at + 1)
791
867
  }
792
868
 
@@ -857,45 +933,104 @@ export class JsonTokenizer {
857
933
  const src: u8[] = this.src
858
934
  const end: i32 = this.maxNumberBytes >= this.end - at ? this.end : at + this.maxNumberBytes + 1
859
935
  let i: i32 = at
860
- if (jsonTokByteAt(src, i, end) === JSON_TOK_MINUS) {
936
+ const negative: boolean = jsonTokByteAt(src, i, end) === JSON_TOK_MINUS
937
+ if (negative) {
861
938
  i += 1
862
939
  }
940
+ const digitsAt: i32 = i
863
941
  const first: i32 = jsonTokByteAt(src, i, end)
864
- if (first === JSON_TOK_DIGIT_0) {
865
- i += 1
866
- } else if (first >= JSON_TOK_DIGIT_1 && first <= JSON_TOK_DIGIT_9) {
867
- i = jsonTokSkipDigits(src, i + 1, end)
868
- } else {
942
+ if (first < JSON_TOK_DIGIT_0 || first > JSON_TOK_DIGIT_9) {
869
943
  return this.numberRefused(first < 0 ? JSON_TOK_ERR_TRUNCATED : JSON_TOK_ERR_NUMBER, at, i)
870
944
  }
871
- let integral = true
872
- if (jsonTokByteAt(src, i, end) === JSON_TOK_DOT) {
873
- integral = false
945
+ // The integer part's digits and then the fraction's are folded into one
946
+ // `u64` as they are passed, eight at a time while eight bytes remain. It
947
+ // wraps on a long run, which needs no count: the digits are counted from
948
+ // the indices once the runs end, and `numberF64` reads the mantissa only
949
+ // when there are 19 or fewer. A leading `0` is its whole integer part.
950
+ const n: i32 = toI32(src.length)
951
+ const to: i32 = end < n ? end : n
952
+ let mantissa: u64 = toU64(0)
953
+ let fractionAt: i32 = -1
954
+ let run: boolean = first !== JSON_TOK_DIGIT_0
955
+ if (!run) {
956
+ i += 1
957
+ }
958
+ while (true) {
959
+ if (run) {
960
+ // A run shorter than eight digits, most of a document's, ends
961
+ // before its eighth byte: that one byte is read first, and only a
962
+ // run that reaches it pays for the eight-byte read and check.
963
+ while (
964
+ i >= 0 &&
965
+ i < JSON_TOK_END_CEILING &&
966
+ i + 8 <= to &&
967
+ i + 8 <= n &&
968
+ jsonTokIsDigit(toI32(src[i + 7]))
969
+ ) {
970
+ const word: u64 = jsonTokLoad8(src, i)
971
+ if (!jsonTokEightDigits(word)) {
972
+ break
973
+ }
974
+ mantissa = mantissa * toU64(100000000) + jsonTokParseEight(word)
975
+ i += 8
976
+ }
977
+ while (i >= 0 && i < to && i < n) {
978
+ const digit: i32 = toI32(src[i]) - JSON_TOK_DIGIT_0
979
+ if (digit < 0 || digit > 9) {
980
+ break
981
+ }
982
+ mantissa = mantissa * toU64(10) + toU64(digit)
983
+ i += 1
984
+ }
985
+ }
986
+ if (fractionAt >= 0 || jsonTokByteAt(src, i, end) !== JSON_TOK_DOT) {
987
+ break
988
+ }
874
989
  i += 1
875
990
  const digit: i32 = jsonTokByteAt(src, i, end)
876
991
  if (!jsonTokIsDigit(digit)) {
877
992
  return this.numberRefused(digit < 0 ? JSON_TOK_ERR_TRUNCATED : JSON_TOK_ERR_NUMBER, at, i)
878
993
  }
879
- i = jsonTokSkipDigits(src, i + 1, end)
994
+ fractionAt = i
995
+ run = true
880
996
  }
997
+ let integral: boolean = fractionAt < 0
998
+ let digits: i32 = integral ? i - digitsAt : i - digitsAt - 1
999
+ let scale: i32 = integral ? 0 : fractionAt - i
881
1000
  const e: i32 = jsonTokByteAt(src, i, end)
882
1001
  if (e === JSON_TOK_LOWER_E || e === JSON_TOK_UPPER_E) {
883
1002
  integral = false
884
1003
  i += 1
885
1004
  const sign: i32 = jsonTokByteAt(src, i, end)
886
- if (sign === JSON_TOK_PLUS || sign === JSON_TOK_MINUS) {
1005
+ const minus: boolean = sign === JSON_TOK_MINUS
1006
+ if (minus || sign === JSON_TOK_PLUS) {
887
1007
  i += 1
888
1008
  }
889
1009
  const digit: i32 = jsonTokByteAt(src, i, end)
890
1010
  if (!jsonTokIsDigit(digit)) {
891
1011
  return this.numberRefused(digit < 0 ? JSON_TOK_ERR_TRUNCATED : JSON_TOK_ERR_NUMBER, at, i)
892
1012
  }
893
- i = jsonTokSkipDigits(src, i + 1, end)
1013
+ // Accumulated until it reaches the cap; a digit after that sends
1014
+ // `numberF64` to `parseFloat`, which reads any exponent.
1015
+ let exponent: i32 = 0
1016
+ while (i >= 0 && i < end && i < n && jsonTokIsDigit(toI32(src[i]))) {
1017
+ if (exponent < JSON_TOK_EXPONENT_CAP) {
1018
+ exponent = exponent * JSON_TOK_TEN + toI32(src[i]) - JSON_TOK_DIGIT_0
1019
+ } else {
1020
+ digits = JSON_TOK_NUMBER_WALK
1021
+ }
1022
+ i += 1
1023
+ }
1024
+ scale += minus ? -exponent : exponent
894
1025
  }
895
1026
  if (i - at > this.maxNumberBytes) {
896
1027
  return this.fail(JSON_TOK_ERR_NUMBER_LIMIT, at + this.maxNumberBytes)
897
1028
  }
898
1029
  this.integral = integral
1030
+ this.numberMantissa = mantissa
1031
+ this.numberScale = scale
1032
+ this.numberDigits = digits
1033
+ this.numberNegative = negative
899
1034
  return this.emit(JSON_TOK_NUMBER, at, i)
900
1035
  }
901
1036
 
@@ -917,9 +1052,9 @@ export class JsonTokenizer {
917
1052
  const src: u8[] = this.src
918
1053
  const end: i32 = this.end
919
1054
  // Where the string would pass its limit, or `end` when it cannot pass it
920
- // before the window ends, which also keeps the sum from overflowing.
921
- const room: i32 = end - at - 1
922
- const limit: i32 = this.maxStringBytes >= room ? end : at + 1 + this.maxStringBytes
1055
+ // before the window ends, which also keeps the sum from overflowing:
1056
+ // below `stringFrom`, `at + 1 + maxStringBytes` is less than `end`.
1057
+ const limit: i32 = at >= this.stringFrom ? end : at + 1 + this.maxStringBytes
923
1058
  let i: i32 = at + 1
924
1059
  let decoded: i32 = 0
925
1060
  while (true) {
@@ -1048,6 +1183,12 @@ export class JsonTokenizer {
1048
1183
  // No escape (see `keyEquals`): the bytes are copied as they are, as one
1049
1184
  // `copyBytes` of as many as both arrays hold, which the caller's test
1050
1185
  // above has made all of them.
1186
+ // The source window still in `src` is the case met; the clamps below
1187
+ // are for one shrunk since it was scanned.
1188
+ if (i >= 0 && o >= 0 && end <= toI32(src.length)) {
1189
+ copyBytes(out, o, src, i, n)
1190
+ return n
1191
+ }
1051
1192
  if (i >= 0 && o >= 0) {
1052
1193
  const srcLength: i32 = toI32(src.length)
1053
1194
  const outLength: i32 = toI32(out.length)
@@ -1067,6 +1208,28 @@ export class JsonTokenizer {
1067
1208
  }
1068
1209
  while (i < end) {
1069
1210
  const b: i32 = jsonTokByteAt(src, i, end)
1211
+ // The plain run before the next escape is copied whole. In a string
1212
+ // `string` passed, only a backslash stops the run before `end`; any
1213
+ // other byte it stops at is copied alone below, as before. A backslash
1214
+ // here, as in a run of escapes, skips the scan.
1215
+ const stop: i32 = b === JSON_TOK_BACKSLASH ? i : jsonTokSkipPlain(src, i, end)
1216
+ if (stop > i) {
1217
+ const count: i32 = stop - i
1218
+ if (o >= 0) {
1219
+ // The entry guard sized `out` for the decoded length the scan
1220
+ // measured, but the source can change after the scan: a backslash
1221
+ // turned plain lets a run outgrow that length, so the copy is cut
1222
+ // to the room left.
1223
+ const room: i32 = toI32(out.length) - o
1224
+ const c: i32 = room < count ? room : count
1225
+ if (c > 0) {
1226
+ copyBytes(out, o, src, i, c)
1227
+ }
1228
+ }
1229
+ o += count
1230
+ i = stop
1231
+ continue
1232
+ }
1070
1233
  if (b !== JSON_TOK_BACKSLASH) {
1071
1234
  if (o >= 0 && o < toI32(out.length)) {
1072
1235
  out[o] = toU8(b)
@@ -1194,6 +1357,11 @@ export class JsonTokenizer {
1194
1357
  * the bound for a positive number is one less.
1195
1358
  */
1196
1359
  private integerScan(): boolean {
1360
+ if (this.numberDigits <= JSON_TOK_EXACT_DIGITS) {
1361
+ // Below 10^18, so `number()`'s mantissa is the magnitude and fits.
1362
+ this.integerValue = -toI64(this.numberMantissa)
1363
+ return true
1364
+ }
1197
1365
  const src: u8[] = this.src
1198
1366
  const end: i32 = this.tokEnd
1199
1367
  const negative: boolean = jsonTokByteAt(src, this.tokStart, end) === JSON_TOK_MINUS
@@ -1378,6 +1546,46 @@ export class JsonTokenizer {
1378
1546
  if (this.kind !== JSON_TOK_NUMBER) {
1379
1547
  return 0
1380
1548
  }
1549
+ if (this.numberDigits > JSON_TOK_FAST_DIGITS) {
1550
+ return this.numberWalk()
1551
+ }
1552
+ const mantissa: u64 = this.numberMantissa
1553
+ const scale: i32 = this.numberScale
1554
+ let value: f64 = 0
1555
+ if (mantissa !== toU64(0)) {
1556
+ if (
1557
+ mantissa <= toU64(1) << toU64(53) &&
1558
+ scale >= -JSON_TOK_FAST_EXPONENT &&
1559
+ scale <= JSON_TOK_FAST_EXPONENT
1560
+ ) {
1561
+ // Clinger: both operands are exact doubles, so one rounding. An
1562
+ // integer of up to 15 digits is below 2^53 and needs no power at all.
1563
+ // `numberWalk` repeats this decision; a shared helper cost 0.5% Ir
1564
+ // on metrics, so the two copies must change together.
1565
+ if (scale === 0) {
1566
+ value = toF64(mantissa)
1567
+ } else {
1568
+ const power: f64 = jsonTokPowerOfTen(scale < 0 ? -scale : scale)
1569
+ value = scale < 0 ? toF64(mantissa) / power : toF64(mantissa) * power
1570
+ }
1571
+ } else {
1572
+ value = jsonTokEiselLemire(mantissa, scale)
1573
+ }
1574
+ }
1575
+ return this.numberNegative ? -value : value
1576
+ }
1577
+
1578
+ /**
1579
+ * `numberF64` for a number `number()` could not read into 19 digits: it
1580
+ * folds the digits again, skipping leading zeros, so `0.000…0001` of many
1581
+ * zeros still takes the fast path. `numberScale` already holds the dot and
1582
+ * the exponent, and dropping leading zeros does not move it. More than 19
1583
+ * significant digits, or an exponent past the cap, go to `numberSlow`.
1584
+ */
1585
+ private numberWalk(): f64 {
1586
+ if (this.numberDigits === JSON_TOK_NUMBER_WALK) {
1587
+ return this.numberSlow()
1588
+ }
1381
1589
  const src: u8[] = this.src
1382
1590
  const start: i32 = this.tokStart
1383
1591
  const end: i32 = this.tokEnd
@@ -1388,8 +1596,6 @@ export class JsonTokenizer {
1388
1596
  const ten: u64 = toU64(10)
1389
1597
  let mantissa: u64 = toU64(0)
1390
1598
  let digits: i32 = 0
1391
- let scale: i32 = 0
1392
- let afterDot = false
1393
1599
  let i: i32 = negative ? start + 1 : start
1394
1600
  if (i < 0 || end > toI32(src.length)) {
1395
1601
  return this.numberSlow()
@@ -1398,49 +1604,26 @@ export class JsonTokenizer {
1398
1604
  // `numberSlow` in the loop would otherwise end what a hoisted copy proves.
1399
1605
  while (i < end && i < toI32(src.length)) {
1400
1606
  const b: i32 = toI32(src[i])
1401
- if (b === JSON_TOK_DOT) {
1402
- afterDot = true
1403
- } else if (jsonTokIsDigit(b)) {
1607
+ if (jsonTokIsDigit(b)) {
1404
1608
  if (digits > 0 || b !== JSON_TOK_DIGIT_0) {
1405
1609
  digits += 1
1406
- if (digits <= JSON_TOK_FAST_DIGITS) {
1407
- mantissa = mantissa * ten + toU64(b - JSON_TOK_DIGIT_0)
1610
+ if (digits > JSON_TOK_FAST_DIGITS) {
1611
+ return this.numberSlow()
1408
1612
  }
1613
+ mantissa = mantissa * ten + toU64(b - JSON_TOK_DIGIT_0)
1409
1614
  }
1410
- if (afterDot) {
1411
- scale -= 1
1412
- }
1413
- if (digits > JSON_TOK_FAST_DIGITS) {
1414
- return this.numberSlow()
1415
- }
1416
- } else {
1615
+ } else if (b !== JSON_TOK_DOT) {
1417
1616
  break
1418
1617
  }
1419
1618
  i += 1
1420
1619
  }
1421
- if (i < end) {
1422
- // The exponent: `i` is at the `e` or `E`.
1423
- i += 1
1424
- const sign: i32 = jsonTokByteAt(src, i, end)
1425
- const minus: boolean = sign === JSON_TOK_MINUS
1426
- if (minus || sign === JSON_TOK_PLUS) {
1427
- i += 1
1428
- }
1429
- let exponent: i32 = 0
1430
- while (i < end && i < toI32(src.length) && exponent < JSON_TOK_EXPONENT_CAP) {
1431
- exponent = exponent * JSON_TOK_TEN + toI32(src[i]) - JSON_TOK_DIGIT_0
1432
- i += 1
1433
- }
1434
- if (i < end) {
1435
- return this.numberSlow()
1436
- }
1437
- scale += minus ? -exponent : exponent
1438
- }
1620
+ const scale: i32 = this.numberScale
1439
1621
  let value: f64 = 0
1440
1622
  if (digits > 0) {
1441
1623
  const exact: boolean = mantissa <= toU64(1) << toU64(53)
1442
1624
  if (exact && scale >= -JSON_TOK_FAST_EXPONENT && scale <= JSON_TOK_FAST_EXPONENT) {
1443
- // Clinger: both operands are exact doubles, so one rounding.
1625
+ // Clinger: both operands are exact doubles, so one rounding. The
1626
+ // same decision as `numberF64`'s; change the two together.
1444
1627
  const power: f64 = jsonTokPowerOfTen(scale < 0 ? -scale : scale)
1445
1628
  value = scale < 0 ? toF64(mantissa) / power : toF64(mantissa) * power
1446
1629
  } else {
@@ -1472,6 +1655,29 @@ export class JsonTokenizer {
1472
1655
  return parseFloat(String.fromBytes(src, start, end))
1473
1656
  }
1474
1657
 
1658
+ /**
1659
+ * Goes back to just after the `{` at `start`, the opener of the innermost
1660
+ * open object, so that the next `next()` answers that object's first key
1661
+ * again. The decoder of a tagged union reads ahead to the tag and then
1662
+ * decodes the object from its start; the bytes between are read twice.
1663
+ * Call it only while that object is still the innermost one open, and
1664
+ * before any error: the depth stack is as it was when the `{` was read.
1665
+ *
1666
+ * @param start - the byte offset of the object's `{`, its `tokStart`.
1667
+ *
1668
+ * @internal Called by `JsonDecoder.enterUnion`, std/json-typed.ts.
1669
+ */
1670
+ rewindObject(start: i32): void {
1671
+ this.kind = JSON_TOK_OBJECT_BEGIN
1672
+ this.tokStart = start
1673
+ this.tokEnd = start + 1
1674
+ this.pos = start + 1
1675
+ // An error stays sticky: `next` answers it while `want` says so.
1676
+ if (this.failed === 0) {
1677
+ this.want = JSON_TOK_WANT_KEY_OR_CLOSE
1678
+ }
1679
+ }
1680
+
1475
1681
  /**
1476
1682
  * Steps over the value the current token starts, so that the next `next()`
1477
1683
  * answers what follows it. After a key, that is the key's value; after an
@@ -1569,11 +1775,38 @@ const jsonTokPowerOfTen = (n: i32): f64 => {
1569
1775
  * half of a product is built from four 32-bit limb products, none of which
1570
1776
  * overflows, and the carry into it is found by comparison rather than by the
1571
1777
  * sum wrapping.
1778
+ *
1779
+ * `jsonTokEightDigits` and `jsonTokParseEight` are upstream's
1780
+ * `is_made_of_eight_digits_fast` and `parse_eight_digits_unrolled`, from
1781
+ * include/fast_float/ascii_number.h at the same commit. They do rely on a
1782
+ * wrap, which a `u64` keeps under Node too (docs/LANGUAGE.md, "Unsigned
1783
+ * integers").
1572
1784
  */
1573
1785
 
1574
1786
  /** A `u64` from its two 32-bit halves. */
1575
1787
  const jsonTokW64 = (hi: u32, lo: u32): u64 => (toU64(hi) << toU64(32)) | toU64(lo)
1576
1788
 
1789
+ /** Whether all eight bytes of `word` are ASCII digits: fast_float's `is_made_of_eight_digits_fast`. */
1790
+ const jsonTokEightDigits = (word: u64): boolean => {
1791
+ const high: u64 = jsonTokW64(0xf0f0f0f0, 0xf0f0f0f0)
1792
+ const six: u64 = jsonTokW64(0x06060606, 0x06060606)
1793
+ return ((word & high) | (((word + six) & high) >> toU64(4))) === jsonTokW64(0x33333333, 0x33333333)
1794
+ }
1795
+
1796
+ /**
1797
+ * The eight digits of `word`, the first the most significant, as an integer:
1798
+ * fast_float's `parse_eight_digits_unrolled`. Pairs, then fours, then the
1799
+ * eight are combined by three multiplies, and every product wraps as fast_float's does.
1800
+ */
1801
+ const jsonTokParseEight = (word: u64): u64 => {
1802
+ const mask: u64 = jsonTokW64(0x000000ff, 0x000000ff)
1803
+ const v: u64 = word - jsonTokW64(0x30303030, 0x30303030)
1804
+ const pairs: u64 = v * toU64(10) + (v >> toU64(8))
1805
+ const low: u64 = (pairs & mask) * jsonTokW64(0x000f4240, 0x00000064)
1806
+ const high: u64 = ((pairs >> toU64(16)) & mask) * jsonTokW64(0x00002710, 0x00000001)
1807
+ return ((low + high) >> toU64(32)) & jsonTokW64(0, 0xffffffff)
1808
+ }
1809
+
1577
1810
  /**
1578
1811
  * The high 64 bits of the 128-bit product `a × b`. Each limb product is at
1579
1812
  * most (2^32 - 1)^2, so `cross` is at most 2^64 - 1 and the answer at most