@amritk/nish-x86_64-linux 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.
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +47 -5
- package/runtime/nish.mjs +4 -0
- package/std/README.md +3 -3
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1275 -217
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/json-value.ts
CHANGED
|
@@ -194,7 +194,11 @@ export const JSON_VALUE_ERR_HANDLE: i32 = -31
|
|
|
194
194
|
export const JSON_VALUE_ERR_ATTACHED: i32 = -32
|
|
195
195
|
/** Text that is not one JSON number, for `newNumber`, or an `f64` that is NaN or an infinity, for `newF64`. */
|
|
196
196
|
export const JSON_VALUE_ERR_NUMBER: i32 = -33
|
|
197
|
-
/**
|
|
197
|
+
/**
|
|
198
|
+
* One value more than `JsonDocument.maxValues`, by a parse (with `errorAt` at
|
|
199
|
+
* its first byte) or a builder; or, from a builder, text that would take the
|
|
200
|
+
* document's stored bytes past 2^31 - 1.
|
|
201
|
+
*/
|
|
198
202
|
export const JSON_VALUE_ERR_VALUES: i32 = -34
|
|
199
203
|
/** `setRange` or `setKeyRange` with a range that is not `0 <= start <= end`. */
|
|
200
204
|
export const JSON_VALUE_ERR_RANGE: i32 = -35
|
|
@@ -231,6 +235,8 @@ const JSON_VALUE_PAIRWISE: i32 = 8
|
|
|
231
235
|
const JSON_VALUE_FIRST_SLOTS: i32 = 4
|
|
232
236
|
/** The largest an `i32` count may be, which every limit of the number reader is set to. */
|
|
233
237
|
const JSON_VALUE_I32_MAX: i32 = 2147483647
|
|
238
|
+
/** The most slots `reserve` makes room up to, 2^30: `append` keeps its sums under 2^31 with it. */
|
|
239
|
+
const JSON_VALUE_RESERVE_LIMIT: i32 = 1073741824
|
|
234
240
|
const JSON_VALUE_DIGIT_0: i32 = 48
|
|
235
241
|
/** The words of a value with nothing to point at, and a `true` boolean's first word; spelled `i32` for `--number-mode f64`. */
|
|
236
242
|
const JSON_VALUE_NO_WORD: i32 = 0
|
|
@@ -413,13 +419,14 @@ export class JsonDocument {
|
|
|
413
419
|
|
|
414
420
|
/** Forgets every value, keeping the memory for the next parse or build. Every handle is invalid afterwards. */
|
|
415
421
|
clear(): void {
|
|
416
|
-
|
|
422
|
+
const g: i32 = this.generation
|
|
423
|
+
if (g >= JSON_VALUE_LAST_GENERATION) {
|
|
417
424
|
// Every return to a generation already used passes here, so ranges
|
|
418
425
|
// stamped with one are forgotten before it comes round again.
|
|
419
426
|
this.generation = 1
|
|
420
427
|
this.rangeGeneration = 0
|
|
421
428
|
} else {
|
|
422
|
-
this.generation
|
|
429
|
+
this.generation = g + 1
|
|
423
430
|
}
|
|
424
431
|
this.top = 0
|
|
425
432
|
this.slotTop = 0
|
|
@@ -476,9 +483,11 @@ export class JsonDocument {
|
|
|
476
483
|
jsonValuePut(this.openKeyAt, depth, keyAt)
|
|
477
484
|
jsonValuePut(this.openKeyLength, depth, keyLength)
|
|
478
485
|
jsonValuePut(this.openKeySource, depth, keySource)
|
|
479
|
-
depth
|
|
486
|
+
// The tokenizer counts the containers open after each token, one more than `depth` here.
|
|
487
|
+
depth = tok.depth
|
|
480
488
|
} else if (k === JSON_TOK_OBJECT_END || k === JSON_TOK_ARRAY_END) {
|
|
481
|
-
|
|
489
|
+
// One fewer than before: the closer's own container is the one at `depth`.
|
|
490
|
+
depth = tok.depth
|
|
482
491
|
const c: i32 = jsonValueGet(this.openValue, depth)
|
|
483
492
|
const code: i32 = this.close(c, jsonValueGet(this.openBase, depth))
|
|
484
493
|
if (code < 0) {
|
|
@@ -511,6 +520,11 @@ export class JsonDocument {
|
|
|
511
520
|
root = v
|
|
512
521
|
} else {
|
|
513
522
|
const p: i32 = this.pendingTop
|
|
523
|
+
// Every pending member is a value, and `newNode` holds the values
|
|
524
|
+
// under the cap, so this refusal is never reached.
|
|
525
|
+
if (p >= JSON_VALUE_MAX_VALUES) {
|
|
526
|
+
return this.parseFailed(JSON_VALUE_ERR_VALUES, tok.tokStart)
|
|
527
|
+
}
|
|
514
528
|
jsonValuePut(this.pendingValue, p, v)
|
|
515
529
|
jsonValuePut(this.pendingKeyAt, p, keyAt)
|
|
516
530
|
jsonValuePut(this.pendingKeyLength, p, keyLength)
|
|
@@ -580,9 +594,11 @@ export class JsonDocument {
|
|
|
580
594
|
jsonValuePut(r.valueStart, c, tok.tokStart)
|
|
581
595
|
jsonValuePut(r.valueEnd, c, -1)
|
|
582
596
|
jsonValuePut(r.openKeyEnd, depth, keyEnd)
|
|
583
|
-
depth
|
|
597
|
+
// The tokenizer counts the containers open after each token, one more than `depth` here.
|
|
598
|
+
depth = tok.depth
|
|
584
599
|
} else if (k === JSON_TOK_OBJECT_END || k === JSON_TOK_ARRAY_END) {
|
|
585
|
-
|
|
600
|
+
// One fewer than before: the closer's own container is the one at `depth`.
|
|
601
|
+
depth = tok.depth
|
|
586
602
|
const c: i32 = jsonValueGet(this.openValue, depth)
|
|
587
603
|
const code: i32 = this.closeRanged(c, jsonValueGet(this.openBase, depth))
|
|
588
604
|
if (code < 0) {
|
|
@@ -621,6 +637,11 @@ export class JsonDocument {
|
|
|
621
637
|
root = v
|
|
622
638
|
} else {
|
|
623
639
|
const p: i32 = this.pendingTop
|
|
640
|
+
// Every pending member is a value, and `newNode` holds the values
|
|
641
|
+
// under the cap, so this refusal is never reached.
|
|
642
|
+
if (p >= JSON_VALUE_MAX_VALUES) {
|
|
643
|
+
return this.parseFailed(JSON_VALUE_ERR_VALUES, tok.tokStart)
|
|
644
|
+
}
|
|
624
645
|
jsonValuePut(this.pendingValue, p, v)
|
|
625
646
|
jsonValuePut(this.pendingKeyAt, p, keyAt)
|
|
626
647
|
jsonValuePut(this.pendingKeyLength, p, keyLength)
|
|
@@ -691,75 +712,83 @@ export class JsonDocument {
|
|
|
691
712
|
* `closeRanged`.
|
|
692
713
|
*/
|
|
693
714
|
private close(c: i32, base: i32): i32 {
|
|
694
|
-
|
|
715
|
+
const end: i32 = this.pendingTop
|
|
716
|
+
// `base` is the pending top when the container opened, and members are
|
|
717
|
+
// only added since, so the count cannot wrap.
|
|
718
|
+
const n: i32 = toI32(toU32(end) - toU32(base))
|
|
695
719
|
const object: boolean = this.kindAt(c) === JSON_VALUE_OBJECT
|
|
696
720
|
if (object && n > 1) {
|
|
697
|
-
const code: i32 = this.duplicateKeys(base, n)
|
|
721
|
+
const code: i32 = this.duplicateKeys(base, end, n)
|
|
698
722
|
if (code < 0) {
|
|
699
723
|
return code
|
|
700
724
|
}
|
|
701
725
|
}
|
|
702
726
|
const start: i32 = this.slotTop
|
|
703
|
-
|
|
704
|
-
|
|
727
|
+
// A parse places each value but the root in one slot, and `newNode` holds
|
|
728
|
+
// the values under the cap, so `s` cannot wrap.
|
|
729
|
+
let s: u32 = toU32(start)
|
|
730
|
+
for (let p: i32 = base; p < end; p += 1) {
|
|
705
731
|
const v: i32 = jsonValueGet(this.pendingValue, p)
|
|
706
732
|
if (v < 0) {
|
|
707
733
|
continue
|
|
708
734
|
}
|
|
709
|
-
|
|
710
|
-
jsonValuePut(this.
|
|
711
|
-
jsonValuePut(this.
|
|
712
|
-
jsonValuePut(this.slotKeyLength, s, jsonValueGet(this.pendingKeyLength, p))
|
|
735
|
+
jsonValuePut(this.slotValue, toI32(s), v)
|
|
736
|
+
jsonValuePut(this.slotKeyAt, toI32(s), jsonValueGet(this.pendingKeyAt, p))
|
|
737
|
+
jsonValuePut(this.slotKeyLength, toI32(s), jsonValueGet(this.pendingKeyLength, p))
|
|
713
738
|
jsonValuePut(this.parent, v, c)
|
|
714
|
-
|
|
739
|
+
s += 1
|
|
715
740
|
}
|
|
716
|
-
|
|
717
|
-
this.slotTop =
|
|
741
|
+
const kept: i32 = toI32(s - toU32(start))
|
|
742
|
+
this.slotTop = toI32(s)
|
|
718
743
|
jsonValuePut(this.first, c, start)
|
|
719
|
-
jsonValuePut(this.count, c,
|
|
720
|
-
jsonValuePut(this.room, c,
|
|
744
|
+
jsonValuePut(this.count, c, kept)
|
|
745
|
+
jsonValuePut(this.room, c, kept)
|
|
721
746
|
this.pendingTop = base
|
|
722
747
|
return 0
|
|
723
748
|
}
|
|
724
749
|
|
|
725
750
|
/** `close` for `parseRanged`, which also gives each member its key's range. Keep in step with `close`. */
|
|
726
751
|
private closeRanged(c: i32, base: i32): i32 {
|
|
727
|
-
|
|
752
|
+
const end: i32 = this.pendingTop
|
|
753
|
+
// `base` is the pending top when the container opened, and members are
|
|
754
|
+
// only added since, so the count cannot wrap.
|
|
755
|
+
const n: i32 = toI32(toU32(end) - toU32(base))
|
|
728
756
|
const object: boolean = this.kindAt(c) === JSON_VALUE_OBJECT
|
|
729
757
|
if (object && n > 1) {
|
|
730
|
-
const code: i32 = this.duplicateKeysRanged(base, n)
|
|
758
|
+
const code: i32 = this.duplicateKeysRanged(base, end, n)
|
|
731
759
|
if (code < 0) {
|
|
732
760
|
return code
|
|
733
761
|
}
|
|
734
762
|
}
|
|
735
763
|
const r: JsonValueRanges = this.rangeArrays()
|
|
736
764
|
const start: i32 = this.slotTop
|
|
737
|
-
|
|
738
|
-
|
|
765
|
+
// A parse places each value but the root in one slot, and `newNode` holds
|
|
766
|
+
// the values under the cap, so `s` cannot wrap.
|
|
767
|
+
let s: u32 = toU32(start)
|
|
768
|
+
for (let p: i32 = base; p < end; p += 1) {
|
|
739
769
|
const v: i32 = jsonValueGet(this.pendingValue, p)
|
|
740
770
|
if (v < 0) {
|
|
741
771
|
continue
|
|
742
772
|
}
|
|
743
|
-
|
|
744
|
-
jsonValuePut(this.
|
|
745
|
-
jsonValuePut(this.
|
|
746
|
-
jsonValuePut(
|
|
747
|
-
jsonValuePut(r.
|
|
748
|
-
jsonValuePut(r.slotKeyEnd, s, jsonValueGet(r.pendingKeyEnd, p))
|
|
773
|
+
jsonValuePut(this.slotValue, toI32(s), v)
|
|
774
|
+
jsonValuePut(this.slotKeyAt, toI32(s), jsonValueGet(this.pendingKeyAt, p))
|
|
775
|
+
jsonValuePut(this.slotKeyLength, toI32(s), jsonValueGet(this.pendingKeyLength, p))
|
|
776
|
+
jsonValuePut(r.slotKeyStart, toI32(s), jsonValueGet(this.pendingSource, p))
|
|
777
|
+
jsonValuePut(r.slotKeyEnd, toI32(s), jsonValueGet(r.pendingKeyEnd, p))
|
|
749
778
|
jsonValuePut(this.parent, v, c)
|
|
750
|
-
|
|
779
|
+
s += 1
|
|
751
780
|
}
|
|
752
|
-
|
|
753
|
-
this.slotTop =
|
|
781
|
+
const kept: i32 = toI32(s - toU32(start))
|
|
782
|
+
this.slotTop = toI32(s)
|
|
754
783
|
jsonValuePut(this.first, c, start)
|
|
755
|
-
jsonValuePut(this.count, c,
|
|
756
|
-
jsonValuePut(this.room, c,
|
|
784
|
+
jsonValuePut(this.count, c, kept)
|
|
785
|
+
jsonValuePut(this.room, c, kept)
|
|
757
786
|
this.pendingTop = base
|
|
758
787
|
return 0
|
|
759
788
|
}
|
|
760
789
|
|
|
761
790
|
/**
|
|
762
|
-
* The duplicate check over the `n` members pending from `base`. Under
|
|
791
|
+
* The duplicate check over the `n` members pending from `base` to `end`. Under
|
|
763
792
|
* `JSON_VALUE_DUPLICATE_LAST`, a repeated key's later members are marked
|
|
764
793
|
* gone (their pending value set to `-1`) and the first one takes the last
|
|
765
794
|
* one's value. Small objects compare every pair; larger ones are sorted by
|
|
@@ -767,10 +796,11 @@ export class JsonDocument {
|
|
|
767
796
|
* and the first of them is its first member. Keep in step with
|
|
768
797
|
* `duplicateKeysRanged`.
|
|
769
798
|
*/
|
|
770
|
-
private duplicateKeys(base: i32, n: i32): i32 {
|
|
799
|
+
private duplicateKeys(base: i32, end: i32, n: i32): i32 {
|
|
771
800
|
let firstRepeat: i32 = -1
|
|
772
801
|
if (n <= JSON_VALUE_PAIRWISE) {
|
|
773
|
-
|
|
802
|
+
// From `base` itself, whose inner loop is empty, so that no sum is needed.
|
|
803
|
+
for (let i: i32 = base; i < end; i += 1) {
|
|
774
804
|
for (let j: i32 = base; j < i; j += 1) {
|
|
775
805
|
if (jsonValueGet(this.pendingValue, j) >= 0 && this.samePendingKey(i, j)) {
|
|
776
806
|
firstRepeat = this.repeat(j, i, firstRepeat)
|
|
@@ -779,7 +809,7 @@ export class JsonDocument {
|
|
|
779
809
|
}
|
|
780
810
|
}
|
|
781
811
|
} else {
|
|
782
|
-
const order: i32[] = this.sortPending(base, n)
|
|
812
|
+
const order: i32[] = this.sortPending(base, end, n)
|
|
783
813
|
let i: i32 = 0
|
|
784
814
|
while (i < n) {
|
|
785
815
|
const head: i32 = jsonValueGet(order, i)
|
|
@@ -803,10 +833,11 @@ export class JsonDocument {
|
|
|
803
833
|
* `duplicateKeys` keeps the one caller that lets it inline into `parse`.
|
|
804
834
|
* Keep in step with `duplicateKeys`.
|
|
805
835
|
*/
|
|
806
|
-
private duplicateKeysRanged(base: i32, n: i32): i32 {
|
|
836
|
+
private duplicateKeysRanged(base: i32, end: i32, n: i32): i32 {
|
|
807
837
|
let firstRepeat: i32 = -1
|
|
808
838
|
if (n <= JSON_VALUE_PAIRWISE) {
|
|
809
|
-
|
|
839
|
+
// From `base` itself, whose inner loop is empty, so that no sum is needed.
|
|
840
|
+
for (let i: i32 = base; i < end; i += 1) {
|
|
810
841
|
for (let j: i32 = base; j < i; j += 1) {
|
|
811
842
|
if (jsonValueGet(this.pendingValue, j) >= 0 && this.samePendingKey(i, j)) {
|
|
812
843
|
firstRepeat = this.repeat(j, i, firstRepeat)
|
|
@@ -815,7 +846,7 @@ export class JsonDocument {
|
|
|
815
846
|
}
|
|
816
847
|
}
|
|
817
848
|
} else {
|
|
818
|
-
const order: i32[] = this.sortPendingRanged(base, n)
|
|
849
|
+
const order: i32[] = this.sortPendingRanged(base, end, n)
|
|
819
850
|
let i: i32 = 0
|
|
820
851
|
while (i < n) {
|
|
821
852
|
const head: i32 = jsonValueGet(order, i)
|
|
@@ -854,35 +885,47 @@ export class JsonDocument {
|
|
|
854
885
|
}
|
|
855
886
|
|
|
856
887
|
/**
|
|
857
|
-
* The pending indices `base ..
|
|
888
|
+
* The `n` pending indices `base .. end - 1` sorted by key, ties in index
|
|
858
889
|
* order: a bottom-up merge sort between the two sort arrays, which answers
|
|
859
890
|
* whichever of them holds the result. Keep in step with
|
|
860
891
|
* `sortPendingRanged`.
|
|
861
892
|
*/
|
|
862
|
-
private sortPending(base: i32, n: i32): i32[] {
|
|
893
|
+
private sortPending(base: i32, end: i32, n: i32): i32[] {
|
|
863
894
|
let from: i32[] = jsonValueRoomI32(this.sortA, n)
|
|
864
895
|
let to: i32[] = jsonValueRoomI32(this.sortB, n)
|
|
865
896
|
this.sortA = from
|
|
866
897
|
this.sortB = to
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
898
|
+
let i: i32 = 0
|
|
899
|
+
for (let p: i32 = base; p < end && i < toI32(from.length); p += 1) {
|
|
900
|
+
from[i] = p
|
|
901
|
+
i += 1
|
|
902
|
+
}
|
|
903
|
+
// `lo`, `mid` and `width` all lie in `0..n` with `n` an `i32`, so each
|
|
904
|
+
// sum and double below is under 2^32 and cannot wrap in `u32`; past `n`
|
|
905
|
+
// it is clamped to `n`, as the merge needs.
|
|
870
906
|
let width: i32 = 1
|
|
871
907
|
while (width < n) {
|
|
872
908
|
let lo: i32 = 0
|
|
873
909
|
while (lo < n) {
|
|
874
|
-
const
|
|
875
|
-
const
|
|
910
|
+
const midWide: u32 = toU32(lo) + toU32(width)
|
|
911
|
+
const mid: i32 = midWide < toU32(n) ? toI32(midWide) : n
|
|
912
|
+
const hiWide: u32 = toU32(mid) + toU32(width)
|
|
913
|
+
const hi: i32 = hiWide < toU32(n) ? toI32(hiWide) : n
|
|
876
914
|
let a: i32 = lo
|
|
877
915
|
let b: i32 = mid
|
|
878
916
|
let out: i32 = lo
|
|
879
917
|
while (out < hi) {
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
918
|
+
// `a < mid` whenever `b >= hi`, since `out < hi`, so testing it first changes nothing.
|
|
919
|
+
if (
|
|
920
|
+
a < mid &&
|
|
921
|
+
(b >= hi || this.comparePending(jsonValueGet(from, a), jsonValueGet(from, b)) <= 0)
|
|
922
|
+
) {
|
|
883
923
|
jsonValuePut(to, out, jsonValueGet(from, a))
|
|
884
924
|
a += 1
|
|
885
|
-
} else {
|
|
925
|
+
} else if (b < hi) {
|
|
926
|
+
// Always true here: `out - lo` members are taken, `(a - lo) +
|
|
927
|
+
// (b - mid)` of them, and `out < hi`, so a run whose left half is
|
|
928
|
+
// spent has a right member left.
|
|
886
929
|
jsonValuePut(to, out, jsonValueGet(from, b))
|
|
887
930
|
b += 1
|
|
888
931
|
}
|
|
@@ -893,7 +936,8 @@ export class JsonDocument {
|
|
|
893
936
|
const swap: i32[] = from
|
|
894
937
|
from = to
|
|
895
938
|
to = swap
|
|
896
|
-
|
|
939
|
+
const doubled: u32 = toU32(width) * 2
|
|
940
|
+
width = doubled < toU32(n) ? toI32(doubled) : n
|
|
897
941
|
}
|
|
898
942
|
return from
|
|
899
943
|
}
|
|
@@ -903,30 +947,42 @@ export class JsonDocument {
|
|
|
903
947
|
* that `sortPending` keeps the one caller that lets it inline into `parse`.
|
|
904
948
|
* Keep in step with `sortPending`.
|
|
905
949
|
*/
|
|
906
|
-
private sortPendingRanged(base: i32, n: i32): i32[] {
|
|
950
|
+
private sortPendingRanged(base: i32, end: i32, n: i32): i32[] {
|
|
907
951
|
let from: i32[] = jsonValueRoomI32(this.sortA, n)
|
|
908
952
|
let to: i32[] = jsonValueRoomI32(this.sortB, n)
|
|
909
953
|
this.sortA = from
|
|
910
954
|
this.sortB = to
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
955
|
+
let i: i32 = 0
|
|
956
|
+
for (let p: i32 = base; p < end && i < toI32(from.length); p += 1) {
|
|
957
|
+
from[i] = p
|
|
958
|
+
i += 1
|
|
959
|
+
}
|
|
960
|
+
// `lo`, `mid` and `width` all lie in `0..n` with `n` an `i32`, so each
|
|
961
|
+
// sum and double below is under 2^32 and cannot wrap in `u32`; past `n`
|
|
962
|
+
// it is clamped to `n`, as the merge needs.
|
|
914
963
|
let width: i32 = 1
|
|
915
964
|
while (width < n) {
|
|
916
965
|
let lo: i32 = 0
|
|
917
966
|
while (lo < n) {
|
|
918
|
-
const
|
|
919
|
-
const
|
|
967
|
+
const midWide: u32 = toU32(lo) + toU32(width)
|
|
968
|
+
const mid: i32 = midWide < toU32(n) ? toI32(midWide) : n
|
|
969
|
+
const hiWide: u32 = toU32(mid) + toU32(width)
|
|
970
|
+
const hi: i32 = hiWide < toU32(n) ? toI32(hiWide) : n
|
|
920
971
|
let a: i32 = lo
|
|
921
972
|
let b: i32 = mid
|
|
922
973
|
let out: i32 = lo
|
|
923
974
|
while (out < hi) {
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
975
|
+
// `a < mid` whenever `b >= hi`, since `out < hi`, so testing it first changes nothing.
|
|
976
|
+
if (
|
|
977
|
+
a < mid &&
|
|
978
|
+
(b >= hi || this.comparePending(jsonValueGet(from, a), jsonValueGet(from, b)) <= 0)
|
|
979
|
+
) {
|
|
927
980
|
jsonValuePut(to, out, jsonValueGet(from, a))
|
|
928
981
|
a += 1
|
|
929
|
-
} else {
|
|
982
|
+
} else if (b < hi) {
|
|
983
|
+
// Always true here: `out - lo` members are taken, `(a - lo) +
|
|
984
|
+
// (b - mid)` of them, and `out < hi`, so a run whose left half is
|
|
985
|
+
// spent has a right member left.
|
|
930
986
|
jsonValuePut(to, out, jsonValueGet(from, b))
|
|
931
987
|
b += 1
|
|
932
988
|
}
|
|
@@ -937,7 +993,8 @@ export class JsonDocument {
|
|
|
937
993
|
const swap: i32[] = from
|
|
938
994
|
from = to
|
|
939
995
|
to = swap
|
|
940
|
-
|
|
996
|
+
const doubled: u32 = toU32(width) * 2
|
|
997
|
+
width = doubled < toU32(n) ? toI32(doubled) : n
|
|
941
998
|
}
|
|
942
999
|
return from
|
|
943
1000
|
}
|
|
@@ -953,14 +1010,20 @@ export class JsonDocument {
|
|
|
953
1010
|
)
|
|
954
1011
|
}
|
|
955
1012
|
|
|
956
|
-
/**
|
|
1013
|
+
/**
|
|
1014
|
+
* The tokenizer's current string or key, decoded onto the end of `bytes`;
|
|
1015
|
+
* answers its length. A parse decodes no more bytes than it reads, so the
|
|
1016
|
+
* store's end stays under the text's `i32` length and the `u32` sum cannot
|
|
1017
|
+
* wrap; `storeSource` leans on the same bound.
|
|
1018
|
+
*/
|
|
957
1019
|
private storeString(tok: JsonTokenizer): i32 {
|
|
958
1020
|
const n: i32 = tok.stringLength()
|
|
959
1021
|
const at: i32 = this.byteTop
|
|
960
|
-
const
|
|
1022
|
+
const end: i32 = toI32(toU32(at) + toU32(n))
|
|
1023
|
+
const buf: u8[] = jsonValueRoom(this.bytes, end)
|
|
961
1024
|
this.bytes = buf
|
|
962
1025
|
tok.stringInto(buf, at)
|
|
963
|
-
this.byteTop =
|
|
1026
|
+
this.byteTop = end
|
|
964
1027
|
return n
|
|
965
1028
|
}
|
|
966
1029
|
|
|
@@ -968,16 +1031,24 @@ export class JsonDocument {
|
|
|
968
1031
|
private storeSource(tok: JsonTokenizer): i32 {
|
|
969
1032
|
const src: u8[] = tok.src
|
|
970
1033
|
const start: i32 = tok.tokStart
|
|
971
|
-
const
|
|
1034
|
+
const stop: i32 = tok.tokEnd
|
|
1035
|
+
// The token lies in the text, `start <= stop`, so neither its length nor
|
|
1036
|
+
// the store's end can wrap (`storeString` has the bound).
|
|
1037
|
+
const n: i32 = toI32(toU32(stop) - toU32(start))
|
|
972
1038
|
const at: i32 = this.byteTop
|
|
973
|
-
const
|
|
1039
|
+
const end: i32 = toI32(toU32(at) + toU32(n))
|
|
1040
|
+
const buf: u8[] = jsonValueRoom(this.bytes, end)
|
|
974
1041
|
this.bytes = buf
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
1042
|
+
let o: i32 = at
|
|
1043
|
+
for (
|
|
1044
|
+
let i: i32 = start;
|
|
1045
|
+
i < stop && i >= 0 && i < toI32(src.length) && o >= 0 && o < toI32(buf.length);
|
|
1046
|
+
i += 1
|
|
1047
|
+
) {
|
|
1048
|
+
buf[o] = src[i]
|
|
1049
|
+
o += 1
|
|
979
1050
|
}
|
|
980
|
-
this.byteTop =
|
|
1051
|
+
this.byteTop = end
|
|
981
1052
|
return n
|
|
982
1053
|
}
|
|
983
1054
|
|
|
@@ -989,16 +1060,24 @@ export class JsonDocument {
|
|
|
989
1060
|
private storeSourceRanged(tok: JsonTokenizer): i32 {
|
|
990
1061
|
const src: u8[] = tok.src
|
|
991
1062
|
const start: i32 = tok.tokStart
|
|
992
|
-
const
|
|
1063
|
+
const stop: i32 = tok.tokEnd
|
|
1064
|
+
// The token lies in the text, `start <= stop`, so neither its length nor
|
|
1065
|
+
// the store's end can wrap (`storeString` has the bound).
|
|
1066
|
+
const n: i32 = toI32(toU32(stop) - toU32(start))
|
|
993
1067
|
const at: i32 = this.byteTop
|
|
994
|
-
const
|
|
1068
|
+
const end: i32 = toI32(toU32(at) + toU32(n))
|
|
1069
|
+
const buf: u8[] = jsonValueRoom(this.bytes, end)
|
|
995
1070
|
this.bytes = buf
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1071
|
+
let o: i32 = at
|
|
1072
|
+
for (
|
|
1073
|
+
let i: i32 = start;
|
|
1074
|
+
i < stop && i >= 0 && i < toI32(src.length) && o >= 0 && o < toI32(buf.length);
|
|
1075
|
+
i += 1
|
|
1076
|
+
) {
|
|
1077
|
+
buf[o] = src[i]
|
|
1078
|
+
o += 1
|
|
1000
1079
|
}
|
|
1001
|
-
this.byteTop =
|
|
1080
|
+
this.byteTop = end
|
|
1002
1081
|
return n
|
|
1003
1082
|
}
|
|
1004
1083
|
|
|
@@ -1013,7 +1092,7 @@ export class JsonDocument {
|
|
|
1013
1092
|
if (v >= cap) {
|
|
1014
1093
|
return JSON_VALUE_ERR_VALUES
|
|
1015
1094
|
}
|
|
1016
|
-
if (v < toI32(this.kinds.length)) {
|
|
1095
|
+
if (v >= 0 && v < toI32(this.kinds.length)) {
|
|
1017
1096
|
this.kinds[v] = toU8(kind)
|
|
1018
1097
|
} else {
|
|
1019
1098
|
this.kinds.push(toU8(kind))
|
|
@@ -1105,7 +1184,9 @@ export class JsonDocument {
|
|
|
1105
1184
|
if (i < 0 || i >= this.lengthAt(x)) {
|
|
1106
1185
|
return -1
|
|
1107
1186
|
}
|
|
1108
|
-
|
|
1187
|
+
// A container's run of slots lies inside the slot arrays, whose length is
|
|
1188
|
+
// an `i32`, so the slot of a member it has cannot wrap.
|
|
1189
|
+
return toI32(toU32(jsonValueGet(this.first, x)) + toU32(i))
|
|
1109
1190
|
}
|
|
1110
1191
|
|
|
1111
1192
|
/**
|
|
@@ -1161,8 +1242,9 @@ export class JsonDocument {
|
|
|
1161
1242
|
/** The slot of the member of the object at index `x` whose key is `key`'s bytes, or `-1`. */
|
|
1162
1243
|
private findKey(x: i32, key: string): i32 {
|
|
1163
1244
|
const start: i32 = jsonValueGet(this.first, x)
|
|
1164
|
-
|
|
1165
|
-
|
|
1245
|
+
// The object's run of slots lies inside the slot arrays, as `slotAt` says.
|
|
1246
|
+
const end: i32 = toI32(toU32(start) + toU32(jsonValueGet(this.count, x)))
|
|
1247
|
+
for (let s: i32 = start; s < end; s += 1) {
|
|
1166
1248
|
if (this.bytesEqual(jsonValueGet(this.slotKeyAt, s), jsonValueGet(this.slotKeyLength, s), key)) {
|
|
1167
1249
|
return s
|
|
1168
1250
|
}
|
|
@@ -1279,7 +1361,8 @@ export class JsonDocument {
|
|
|
1279
1361
|
}
|
|
1280
1362
|
const bytes: u8[] = this.bytes
|
|
1281
1363
|
const start: i32 = jsonValueGet(this.first, x)
|
|
1282
|
-
|
|
1364
|
+
// A number's text lies inside `bytes`, whose length is an `i32`, so its end cannot wrap.
|
|
1365
|
+
const end: i32 = toI32(toU32(start) + toU32(jsonValueGet(this.count, x)))
|
|
1283
1366
|
if (start < 0 || start >= toI32(bytes.length) || toI32(bytes[start]) === JSON_VALUE_MINUS) {
|
|
1284
1367
|
return toU64(0)
|
|
1285
1368
|
}
|
|
@@ -1343,22 +1426,28 @@ export class JsonDocument {
|
|
|
1343
1426
|
/** `bytes[at, at + n)` as a string, in one allocation. */
|
|
1344
1427
|
private textOf(at: i32, n: i32): string {
|
|
1345
1428
|
const bytes: u8[] = this.bytes
|
|
1346
|
-
|
|
1429
|
+
// With both below 2^31 the `u32` sum is exact; one past 2^31 - 1 turns
|
|
1430
|
+
// negative, below `at`, and like any end past `bytes` names no text.
|
|
1431
|
+
const end: i32 = toI32(toU32(at) + toU32(n))
|
|
1432
|
+
if (at < 0 || n <= 0 || end < at || end > toI32(bytes.length)) {
|
|
1347
1433
|
return ""
|
|
1348
1434
|
}
|
|
1349
|
-
return String.fromBytes(bytes, at,
|
|
1435
|
+
return String.fromBytes(bytes, at, end)
|
|
1350
1436
|
}
|
|
1351
1437
|
|
|
1352
|
-
/** Whether `bytes[at, at +
|
|
1353
|
-
private bytesEqual(at: i32,
|
|
1438
|
+
/** Whether `bytes[at, at + length)` are exactly `s`'s bytes. */
|
|
1439
|
+
private bytesEqual(at: i32, length: i32, s: string): boolean {
|
|
1354
1440
|
const bytes: u8[] = this.bytes
|
|
1355
|
-
|
|
1441
|
+
const n: i32 = toI32(s.length)
|
|
1442
|
+
if (length !== n || at < 0 || at > toI32(bytes.length) - n) {
|
|
1356
1443
|
return false
|
|
1357
1444
|
}
|
|
1358
|
-
|
|
1359
|
-
|
|
1445
|
+
let b: i32 = at
|
|
1446
|
+
for (let i: i32 = 0; i < n && b < toI32(bytes.length); i += 1) {
|
|
1447
|
+
if (toI32(bytes[b]) !== toI32(s.charCodeAt(i))) {
|
|
1360
1448
|
return false
|
|
1361
1449
|
}
|
|
1450
|
+
b += 1
|
|
1362
1451
|
}
|
|
1363
1452
|
return true
|
|
1364
1453
|
}
|
|
@@ -1488,7 +1577,9 @@ export class JsonDocument {
|
|
|
1488
1577
|
}
|
|
1489
1578
|
jsonValuePut(r.valueStart, x, start)
|
|
1490
1579
|
jsonValuePut(r.valueEnd, x, end)
|
|
1491
|
-
|
|
1580
|
+
// `x` is a value, so below `top`, and the second test never fails.
|
|
1581
|
+
const top: i32 = this.top
|
|
1582
|
+
if (x >= this.rangeTop && x < top) {
|
|
1492
1583
|
this.rangeTop = x + 1
|
|
1493
1584
|
}
|
|
1494
1585
|
}
|
|
@@ -1530,9 +1621,11 @@ export class JsonDocument {
|
|
|
1530
1621
|
jsonValuePut(r.slotKeyStart, t, -1)
|
|
1531
1622
|
jsonValuePut(r.slotKeyEnd, t, -1)
|
|
1532
1623
|
}
|
|
1533
|
-
jsonValuePut(r.slotKeyStart, s, start)
|
|
1534
|
-
jsonValuePut(r.slotKeyEnd, s, end)
|
|
1535
|
-
|
|
1624
|
+
jsonValuePut(r.slotKeyStart, toI32(s), start)
|
|
1625
|
+
jsonValuePut(r.slotKeyEnd, toI32(s), end)
|
|
1626
|
+
// `s` is a member's slot, so below `slotTop`, and the second test never fails.
|
|
1627
|
+
const top: i32 = this.slotTop
|
|
1628
|
+
if (s >= this.slotRangeTop && s < top) {
|
|
1536
1629
|
this.slotRangeTop = s + 1
|
|
1537
1630
|
}
|
|
1538
1631
|
}
|
|
@@ -1634,11 +1727,15 @@ export class JsonDocument {
|
|
|
1634
1727
|
newNumber(text: string): JsonValue {
|
|
1635
1728
|
const at: i32 = this.byteTop
|
|
1636
1729
|
const n: i32 = this.storeText(text)
|
|
1730
|
+
if (n < 0) {
|
|
1731
|
+
return JSON_VALUE_ERR_VALUES
|
|
1732
|
+
}
|
|
1637
1733
|
const numbers: JsonTokenizer = this.numbers
|
|
1638
1734
|
numbers.reset(this.bytes, at, n)
|
|
1639
|
-
// The tokenizer allows blanks around a value, and a number here is the
|
|
1735
|
+
// The tokenizer allows blanks around a value, and a number here is the
|
|
1736
|
+
// text alone, which ends where the store now does.
|
|
1640
1737
|
const isNumber: boolean =
|
|
1641
|
-
numbers.next() === JSON_TOK_NUMBER && numbers.tokStart === at && numbers.tokEnd ===
|
|
1738
|
+
numbers.next() === JSON_TOK_NUMBER && numbers.tokStart === at && numbers.tokEnd === this.byteTop
|
|
1642
1739
|
if (!isNumber || numbers.next() !== JSON_TOK_END) {
|
|
1643
1740
|
this.byteTop = at
|
|
1644
1741
|
return JSON_VALUE_ERR_NUMBER
|
|
@@ -1693,7 +1790,8 @@ export class JsonDocument {
|
|
|
1693
1790
|
* when `o` has the key, and appended last when it does not, as assigning a
|
|
1694
1791
|
* property of a JavaScript object does. The value it replaces leaves the
|
|
1695
1792
|
* object and is in no container afterwards. `JSON_VALUE_OK`, or
|
|
1696
|
-
* `JSON_VALUE_ERR_HANDLE` or `JSON_VALUE_ERR_ATTACHED
|
|
1793
|
+
* `JSON_VALUE_ERR_HANDLE` or `JSON_VALUE_ERR_ATTACHED`, or
|
|
1794
|
+
* `JSON_VALUE_ERR_VALUES` for a new key the stored bytes have no room for.
|
|
1697
1795
|
*
|
|
1698
1796
|
* @param o - the object to change.
|
|
1699
1797
|
* @param key - the member key.
|
|
@@ -1717,7 +1815,12 @@ export class JsonDocument {
|
|
|
1717
1815
|
jsonValuePut(this.parent, vx, ox)
|
|
1718
1816
|
return JSON_VALUE_OK
|
|
1719
1817
|
}
|
|
1720
|
-
|
|
1818
|
+
const keyAt: i32 = this.byteTop
|
|
1819
|
+
const keyLength: i32 = this.storeText(key)
|
|
1820
|
+
if (keyLength < 0) {
|
|
1821
|
+
return JSON_VALUE_ERR_VALUES
|
|
1822
|
+
}
|
|
1823
|
+
this.append(ox, vx, keyAt, keyLength)
|
|
1721
1824
|
return JSON_VALUE_OK
|
|
1722
1825
|
}
|
|
1723
1826
|
|
|
@@ -1756,42 +1859,57 @@ export class JsonDocument {
|
|
|
1756
1859
|
* through it.
|
|
1757
1860
|
*/
|
|
1758
1861
|
append(c: i32, v: i32, keyAt: i32, keyLength: i32): i32 {
|
|
1862
|
+
// The sums here are `u32`, and none reaches 2^31, because the slots stay
|
|
1863
|
+
// under 2^30 + 5 * 2^22: `reserve` holds its rooms under 2^30, a parse
|
|
1864
|
+
// puts each value in at most one slot, and growth by doubling takes at
|
|
1865
|
+
// most 4 slots per member appended. Each append takes a value in no
|
|
1866
|
+
// container, and `set` frees one only by taking another, so appends are
|
|
1867
|
+
// at most the 2^22 values a document can hold.
|
|
1759
1868
|
let start: i32 = jsonValueGet(this.first, c)
|
|
1760
1869
|
const n: i32 = jsonValueGet(this.count, c)
|
|
1761
1870
|
const capacity: i32 = jsonValueGet(this.room, c)
|
|
1762
1871
|
if (n === capacity) {
|
|
1763
|
-
const grown: i32 =
|
|
1764
|
-
|
|
1872
|
+
const grown: i32 =
|
|
1873
|
+
capacity < JSON_VALUE_FIRST_SLOTS ? JSON_VALUE_FIRST_SLOTS : toI32(toU32(capacity) * 2)
|
|
1874
|
+
if (toI32(toU32(start) + toU32(capacity)) !== this.slotTop) {
|
|
1765
1875
|
start = this.moveSlots(start, n)
|
|
1766
1876
|
jsonValuePut(this.first, c, start)
|
|
1767
1877
|
}
|
|
1768
|
-
this.slotTop = start + grown
|
|
1878
|
+
this.slotTop = toI32(toU32(start) + toU32(grown))
|
|
1769
1879
|
this.slotValue = jsonValueRoomI32(this.slotValue, this.slotTop)
|
|
1770
1880
|
this.slotKeyAt = jsonValueRoomI32(this.slotKeyAt, this.slotTop)
|
|
1771
1881
|
this.slotKeyLength = jsonValueRoomI32(this.slotKeyLength, this.slotTop)
|
|
1772
1882
|
jsonValuePut(this.room, c, grown)
|
|
1773
1883
|
}
|
|
1774
|
-
|
|
1775
|
-
jsonValuePut(this.
|
|
1776
|
-
jsonValuePut(this.
|
|
1777
|
-
jsonValuePut(this.
|
|
1884
|
+
const s: i32 = toI32(toU32(start) + toU32(n))
|
|
1885
|
+
jsonValuePut(this.slotValue, s, v)
|
|
1886
|
+
jsonValuePut(this.slotKeyAt, s, keyAt)
|
|
1887
|
+
jsonValuePut(this.slotKeyLength, s, keyLength)
|
|
1888
|
+
jsonValuePut(this.count, c, toI32(toU32(n) + 1))
|
|
1778
1889
|
jsonValuePut(this.parent, v, c)
|
|
1779
|
-
return
|
|
1890
|
+
return s
|
|
1780
1891
|
}
|
|
1781
1892
|
|
|
1782
1893
|
/**
|
|
1783
1894
|
* Gives the empty container at index `c`, made last, room for `n`
|
|
1784
1895
|
* members at the end of the slot arrays, so that `n` appends fill it
|
|
1785
|
-
* without moving it. Nothing for a container that is not empty and last
|
|
1896
|
+
* without moving it. Nothing for a container that is not empty and last,
|
|
1897
|
+
* or when the slots would pass 2^30, which bounds what `append` sums.
|
|
1786
1898
|
*
|
|
1787
1899
|
* @internal Read by std/yaml-json.ts, which knows each container's size
|
|
1788
1900
|
* before it builds the members.
|
|
1789
1901
|
*/
|
|
1790
1902
|
reserve(c: i32, n: i32): void {
|
|
1791
|
-
|
|
1903
|
+
const top: i32 = this.slotTop
|
|
1904
|
+
if (n <= 0 || top < 0 || jsonValueGet(this.first, c) !== top || jsonValueGet(this.room, c) !== 0) {
|
|
1792
1905
|
return
|
|
1793
1906
|
}
|
|
1794
|
-
|
|
1907
|
+
// Both below 2^31, so the `u32` sum is exact.
|
|
1908
|
+
const reserved: u32 = toU32(top) + toU32(n)
|
|
1909
|
+
if (reserved > toU32(JSON_VALUE_RESERVE_LIMIT)) {
|
|
1910
|
+
return
|
|
1911
|
+
}
|
|
1912
|
+
this.slotTop = toI32(reserved)
|
|
1795
1913
|
this.slotValue = jsonValueRoomI32(this.slotValue, this.slotTop)
|
|
1796
1914
|
this.slotKeyAt = jsonValueRoomI32(this.slotKeyAt, this.slotTop)
|
|
1797
1915
|
this.slotKeyLength = jsonValueRoomI32(this.slotKeyLength, this.slotTop)
|
|
@@ -1806,25 +1924,32 @@ export class JsonDocument {
|
|
|
1806
1924
|
* @param from - the offset of the first byte.
|
|
1807
1925
|
* @param to - one past the offset of the last byte.
|
|
1808
1926
|
* @param stop - a byte that refuses the copy, or `-1` for none.
|
|
1809
|
-
* @returns how many bytes were stored, or `-1` when the copy was refused
|
|
1927
|
+
* @returns how many bytes were stored, or `-1` when the copy was refused,
|
|
1928
|
+
* or would take the stored bytes past 2^31 - 1.
|
|
1810
1929
|
*
|
|
1811
1930
|
* @internal Read by std/yaml-json.ts, which stores a scalar with nothing
|
|
1812
1931
|
* to decode straight from its source.
|
|
1813
1932
|
*/
|
|
1814
1933
|
storeSpan(text: string, from: i32, to: i32, stop: i32): i32 {
|
|
1815
1934
|
const at: i32 = this.byteTop
|
|
1816
|
-
|
|
1817
|
-
|
|
1935
|
+
if (from < 0 || to < from || to > toI32(text.length) || at < 0) {
|
|
1936
|
+
return -1
|
|
1937
|
+
}
|
|
1938
|
+
// `from <= to`, so the `u32` difference is exact.
|
|
1939
|
+
const n: i32 = toI32(toU32(to) - toU32(from))
|
|
1940
|
+
// Both below 2^31, so the `u32` sum is exact; past 2^31 - 1 it turns
|
|
1941
|
+
// negative, below `at`, and the store is full.
|
|
1942
|
+
const end: i32 = toI32(toU32(at) + toU32(n))
|
|
1943
|
+
if (end < at) {
|
|
1818
1944
|
return -1
|
|
1819
1945
|
}
|
|
1820
|
-
const end: i32 = at + n
|
|
1821
1946
|
const buf: u8[] = jsonValueRoom(this.bytes, end)
|
|
1822
1947
|
this.bytes = buf
|
|
1823
1948
|
if (end > toI32(buf.length)) {
|
|
1824
1949
|
return -1
|
|
1825
1950
|
}
|
|
1826
1951
|
let k: i32 = at
|
|
1827
|
-
for (let j: i32 = from; j < to; j += 1) {
|
|
1952
|
+
for (let j: i32 = from; j < to && k < end; j += 1) {
|
|
1828
1953
|
const code: i32 = toI32(text.charCodeAt(j))
|
|
1829
1954
|
// Both breaks are below 14, so one compare passes almost every byte.
|
|
1830
1955
|
if (code === stop || (code <= JSON_VALUE_CR && (code === JSON_VALUE_LF || code === JSON_VALUE_CR))) {
|
|
@@ -1846,25 +1971,31 @@ export class JsonDocument {
|
|
|
1846
1971
|
* @param text - the text the bytes are copied from.
|
|
1847
1972
|
* @param from - the offset of the first byte.
|
|
1848
1973
|
* @param to - one past the offset of the last byte.
|
|
1849
|
-
* @returns how many bytes were stored, or `-1` when the span is not in
|
|
1974
|
+
* @returns how many bytes were stored, or `-1` when the span is not in
|
|
1975
|
+
* `text` or would take the stored bytes past 2^31 - 1.
|
|
1850
1976
|
*
|
|
1851
1977
|
* @internal Read by std/yaml-json.ts, for a scalar the parser found to be
|
|
1852
1978
|
* its own body.
|
|
1853
1979
|
*/
|
|
1854
1980
|
copySpan(text: string, from: i32, to: i32): i32 {
|
|
1855
1981
|
const at: i32 = this.byteTop
|
|
1856
|
-
|
|
1857
|
-
|
|
1982
|
+
if (from < 0 || to < from || to > toI32(text.length) || at < 0) {
|
|
1983
|
+
return -1
|
|
1984
|
+
}
|
|
1985
|
+
// As in `storeSpan`: `from <= to` keeps the difference exact, and an
|
|
1986
|
+
// end below `at` is past 2^31 - 1.
|
|
1987
|
+
const n: i32 = toI32(toU32(to) - toU32(from))
|
|
1988
|
+
const end: i32 = toI32(toU32(at) + toU32(n))
|
|
1989
|
+
if (end < at) {
|
|
1858
1990
|
return -1
|
|
1859
1991
|
}
|
|
1860
|
-
const buf: u8[] = jsonValueRoom(this.bytes,
|
|
1992
|
+
const buf: u8[] = jsonValueRoom(this.bytes, end)
|
|
1861
1993
|
this.bytes = buf
|
|
1862
|
-
if (
|
|
1994
|
+
if (end > toI32(buf.length)) {
|
|
1863
1995
|
return -1
|
|
1864
1996
|
}
|
|
1865
|
-
const end: i32 = at + n
|
|
1866
1997
|
let k: i32 = at
|
|
1867
|
-
for (let j: i32 = from; j < to; j += 1) {
|
|
1998
|
+
for (let j: i32 = from; j < to && k < end; j += 1) {
|
|
1868
1999
|
// `k` runs `at` to `end` as `j` runs `from` to `to`: in `buf`.
|
|
1869
2000
|
uncheckedSet(buf, k, toU8(text.charCodeAt(j)))
|
|
1870
2001
|
k += 1
|
|
@@ -1894,7 +2025,12 @@ export class JsonDocument {
|
|
|
1894
2025
|
|
|
1895
2026
|
/** A new value of `kind` holding `s`'s bytes, as a handle. */
|
|
1896
2027
|
private newStored(kind: i32, s: string): JsonValue {
|
|
1897
|
-
|
|
2028
|
+
const at: i32 = this.byteTop
|
|
2029
|
+
const n: i32 = this.storeText(s)
|
|
2030
|
+
if (n < 0) {
|
|
2031
|
+
return JSON_VALUE_ERR_VALUES
|
|
2032
|
+
}
|
|
2033
|
+
return this.handle(this.newNode(kind, at, n))
|
|
1898
2034
|
}
|
|
1899
2035
|
|
|
1900
2036
|
/**
|
|
@@ -1907,9 +2043,12 @@ export class JsonDocument {
|
|
|
1907
2043
|
private moveSlots(start: i32, n: i32): i32 {
|
|
1908
2044
|
const moved: i32 = this.slotTop
|
|
1909
2045
|
for (let i: i32 = 0; i < n; i += 1) {
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
2046
|
+
// The slots stay under 2^31, as `append` says, so neither sum wraps.
|
|
2047
|
+
const from: i32 = toI32(toU32(start) + toU32(i))
|
|
2048
|
+
const to: i32 = toI32(toU32(moved) + toU32(i))
|
|
2049
|
+
jsonValuePut(this.slotValue, to, jsonValueGet(this.slotValue, from))
|
|
2050
|
+
jsonValuePut(this.slotKeyAt, to, jsonValueGet(this.slotKeyAt, from))
|
|
2051
|
+
jsonValuePut(this.slotKeyLength, to, jsonValueGet(this.slotKeyLength, from))
|
|
1913
2052
|
}
|
|
1914
2053
|
if (start < this.slotRangeTop && this.rangeGeneration === this.generation) {
|
|
1915
2054
|
this.moveKeyRanges(start, moved, n)
|
|
@@ -1932,12 +2071,15 @@ export class JsonDocument {
|
|
|
1932
2071
|
jsonValuePut(r.slotKeyStart, t, -1)
|
|
1933
2072
|
jsonValuePut(r.slotKeyEnd, t, -1)
|
|
1934
2073
|
}
|
|
2074
|
+
// The slots stay under 2^31, as `append` says, so no sum here wraps.
|
|
1935
2075
|
for (let i: i32 = 0; i < n; i += 1) {
|
|
1936
|
-
const
|
|
1937
|
-
|
|
1938
|
-
|
|
2076
|
+
const a: i32 = toI32(toU32(from) + toU32(i))
|
|
2077
|
+
const b: i32 = toI32(toU32(to) + toU32(i))
|
|
2078
|
+
const has: boolean = a < top
|
|
2079
|
+
jsonValuePut(r.slotKeyStart, b, has ? jsonValueGet(r.slotKeyStart, a) : -1)
|
|
2080
|
+
jsonValuePut(r.slotKeyEnd, b, has ? jsonValueGet(r.slotKeyEnd, a) : -1)
|
|
1939
2081
|
}
|
|
1940
|
-
this.slotRangeTop = to + n
|
|
2082
|
+
this.slotRangeTop = toI32(toU32(to) + toU32(n))
|
|
1941
2083
|
}
|
|
1942
2084
|
}
|
|
1943
2085
|
|
|
@@ -1945,7 +2087,8 @@ export class JsonDocument {
|
|
|
1945
2087
|
* Appends `s`'s bytes to the end of `bytes`.
|
|
1946
2088
|
*
|
|
1947
2089
|
* @param s - the text whose bytes are stored.
|
|
1948
|
-
* @returns how many bytes were stored
|
|
2090
|
+
* @returns how many bytes were stored, or `-1`, storing none, when they
|
|
2091
|
+
* would take the stored bytes past 2^31 - 1.
|
|
1949
2092
|
*
|
|
1950
2093
|
* @internal Read by std/yaml-json.ts, which builds a document from YAML
|
|
1951
2094
|
* through it.
|
|
@@ -1953,14 +2096,20 @@ export class JsonDocument {
|
|
|
1953
2096
|
storeText(s: string): i32 {
|
|
1954
2097
|
const n: i32 = toI32(s.length)
|
|
1955
2098
|
const at: i32 = this.byteTop
|
|
1956
|
-
|
|
2099
|
+
// Both below 2^31, so the `u32` sum is exact; past 2^31 - 1 it turns
|
|
2100
|
+
// negative, below `at`, and the store is full.
|
|
2101
|
+
const end: i32 = toI32(toU32(at) + toU32(n))
|
|
2102
|
+
if (end < at) {
|
|
2103
|
+
return -1
|
|
2104
|
+
}
|
|
2105
|
+
const buf: u8[] = jsonValueRoom(this.bytes, end)
|
|
1957
2106
|
this.bytes = buf
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
2107
|
+
let k: i32 = at
|
|
2108
|
+
for (let i: i32 = 0; i < n && k >= 0 && k < toI32(buf.length); i += 1) {
|
|
2109
|
+
buf[k] = toU8(s.charCodeAt(i))
|
|
2110
|
+
k += 1
|
|
1962
2111
|
}
|
|
1963
|
-
this.byteTop =
|
|
2112
|
+
this.byteTop = end
|
|
1964
2113
|
return n
|
|
1965
2114
|
}
|
|
1966
2115
|
|
|
@@ -1999,7 +2148,8 @@ export class JsonDocument {
|
|
|
1999
2148
|
// A container: its members are the slots from `first`, read directly.
|
|
2000
2149
|
const object: boolean = k === JSON_VALUE_OBJECT
|
|
2001
2150
|
const start: i32 = jsonValueGet(this.first, x)
|
|
2002
|
-
|
|
2151
|
+
// The run lies inside the slot arrays, as `slotAt` says.
|
|
2152
|
+
const end: i32 = toI32(toU32(start) + toU32(jsonValueGet(this.count, x)))
|
|
2003
2153
|
if (object) {
|
|
2004
2154
|
w.beginObject()
|
|
2005
2155
|
} else {
|
|
@@ -2032,13 +2182,17 @@ export class JsonDocument {
|
|
|
2032
2182
|
stringify(v: JsonValue): string {
|
|
2033
2183
|
// Every byte stored and a few of punctuation per value: one pass fits most
|
|
2034
2184
|
// documents, and a text with many escapes doubles from there.
|
|
2035
|
-
|
|
2185
|
+
// In `u32`, which cannot wrap: under 2^31 bytes and 8 * 2^22 values. The
|
|
2186
|
+
// longest array caps it.
|
|
2187
|
+
const guess: u32 = toU32(this.byteTop) + 8 * toU32(this.top) + 64
|
|
2188
|
+
let size: u32 = guess < toU32(JSON_VALUE_I32_MAX) ? guess : toU32(JSON_VALUE_I32_MAX)
|
|
2036
2189
|
while (true) {
|
|
2037
2190
|
const buf: u8[] = new Array<u8>(size)
|
|
2038
2191
|
const w: JsonWriter = new JsonWriter(buf, 0)
|
|
2039
2192
|
this.write(v, w)
|
|
2040
|
-
if (w.failed !== JSON_WRITE_FULL || size > JSON_VALUE_I32_MAX / 2) {
|
|
2041
|
-
|
|
2193
|
+
if (w.failed !== JSON_WRITE_FULL || size > toU32(JSON_VALUE_I32_MAX / 2)) {
|
|
2194
|
+
const end: i32 = w.end
|
|
2195
|
+
return w.complete() && end >= 0 && end <= toI32(buf.length) ? String.fromBytes(buf, 0, end) : ""
|
|
2042
2196
|
}
|
|
2043
2197
|
size = size * 2
|
|
2044
2198
|
}
|
|
@@ -2061,14 +2215,21 @@ const jsonValuePut = (a: i32[], i: i32, x: i32): void => {
|
|
|
2061
2215
|
* `a` when it holds at least `n` elements, and otherwise a copy of it at
|
|
2062
2216
|
* least twice as long, for the caller to keep in `a`'s place. Doubling is
|
|
2063
2217
|
* what keeps a run of appends linear, and the old array is left in the arena.
|
|
2218
|
+
* The copy is built by `push`, so no length is computed for `new Array`.
|
|
2064
2219
|
*/
|
|
2065
2220
|
const jsonValueRoom = (a: u8[], n: i32): u8[] => {
|
|
2066
2221
|
const have: i32 = toI32(a.length)
|
|
2067
2222
|
if (n <= have) {
|
|
2068
2223
|
return a
|
|
2069
2224
|
}
|
|
2070
|
-
const
|
|
2071
|
-
grown
|
|
2225
|
+
const target: u32 = jsonValueGrownLength(have, n)
|
|
2226
|
+
const grown: u8[] = []
|
|
2227
|
+
for (let i: i32 = 0; i < toI32(a.length); i += 1) {
|
|
2228
|
+
grown.push(a[i])
|
|
2229
|
+
}
|
|
2230
|
+
while (toU32(grown.length) < target) {
|
|
2231
|
+
grown.push(0)
|
|
2232
|
+
}
|
|
2072
2233
|
return grown
|
|
2073
2234
|
}
|
|
2074
2235
|
|
|
@@ -2078,20 +2239,48 @@ const jsonValueRoomI32 = (a: i32[], n: i32): i32[] => {
|
|
|
2078
2239
|
if (n <= have) {
|
|
2079
2240
|
return a
|
|
2080
2241
|
}
|
|
2081
|
-
const
|
|
2082
|
-
grown
|
|
2242
|
+
const target: u32 = jsonValueGrownLength(have, n)
|
|
2243
|
+
const grown: i32[] = []
|
|
2244
|
+
for (let i: i32 = 0; i < toI32(a.length); i += 1) {
|
|
2245
|
+
grown.push(a[i])
|
|
2246
|
+
}
|
|
2247
|
+
while (toU32(grown.length) < target) {
|
|
2248
|
+
grown.push(0)
|
|
2249
|
+
}
|
|
2083
2250
|
return grown
|
|
2084
2251
|
}
|
|
2085
2252
|
|
|
2253
|
+
/**
|
|
2254
|
+
* The length an array of `have` elements grows to, for `n > have`: `n` or
|
|
2255
|
+
* twice `have`, whichever is more, but no more than the longest array. In
|
|
2256
|
+
* `u32`, where doubling an `i32` length cannot wrap.
|
|
2257
|
+
*/
|
|
2258
|
+
const jsonValueGrownLength = (have: i32, n: i32): u32 => {
|
|
2259
|
+
const doubled: u32 = toU32(have) * 2
|
|
2260
|
+
if (doubled <= toU32(n)) {
|
|
2261
|
+
return toU32(n)
|
|
2262
|
+
}
|
|
2263
|
+
return doubled < toU32(JSON_VALUE_I32_MAX) ? doubled : toU32(JSON_VALUE_I32_MAX)
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2086
2266
|
/** `bytes[a, a + an)` and `bytes[b, b + bn)` compared byte by byte, a shorter prefix first: negative, `0` or positive. */
|
|
2087
2267
|
const jsonValueCompareBytes = (bytes: u8[], a: i32, an: i32, b: i32, bn: i32): i32 => {
|
|
2088
2268
|
const n: i32 = an < bn ? an : bn
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2269
|
+
const len: i32 = toI32(bytes.length)
|
|
2270
|
+
// Keys are stored in `bytes`, so the loop ends on `n`, never on a bound.
|
|
2271
|
+
let p: i32 = a
|
|
2272
|
+
let q: i32 = b
|
|
2273
|
+
for (let i: i32 = 0; i < n && p >= 0 && p < len && q >= 0 && q < len; i += 1) {
|
|
2274
|
+
const x: i32 = toI32(bytes[p])
|
|
2275
|
+
const y: i32 = toI32(bytes[q])
|
|
2092
2276
|
if (x !== y) {
|
|
2093
2277
|
return x - y
|
|
2094
2278
|
}
|
|
2279
|
+
p += 1
|
|
2280
|
+
q += 1
|
|
2281
|
+
}
|
|
2282
|
+
if (an === bn) {
|
|
2283
|
+
return 0
|
|
2095
2284
|
}
|
|
2096
|
-
return an -
|
|
2285
|
+
return an < bn ? -1 : 1
|
|
2097
2286
|
}
|