@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
package/std/yaml-json.ts CHANGED
@@ -99,6 +99,7 @@ import { JSON_TOK_ERR_TOTAL_LIMIT, JsonLimits } from "nish/json-tokenizer"
99
99
  import {
100
100
  JSON_VALUE_DUPLICATE_LAST,
101
101
  JSON_VALUE_ERR_DUPLICATE,
102
+ JSON_VALUE_ERR_RANGE,
102
103
  JSON_VALUE_ERR_VALUES,
103
104
  JSON_VALUE_MAX_VALUES,
104
105
  JsonDocument,
@@ -107,13 +108,17 @@ import {
107
108
  import {
108
109
  YAML_ALIAS,
109
110
  YAML_BOOL,
111
+ YAML_DOUBLE_QUOTED,
110
112
  YAML_FLOAT,
111
113
  YAML_INT,
112
114
  YAML_MAP,
113
115
  YAML_NULL,
116
+ YAML_PAIR,
114
117
  YAML_PLAIN,
115
118
  YAML_SCALAR,
116
119
  YAML_SEQ,
120
+ YAML_SINGLE_QUOTED,
121
+ YAML_VERBATIM,
117
122
  YamlStream,
118
123
  keyText,
119
124
  parseYaml,
@@ -160,6 +165,15 @@ const YAML_JSON_DASH: i32 = 45
160
165
  const YAML_JSON_DOT: i32 = 46
161
166
  const YAML_JSON_ZERO: i32 = 48
162
167
  const YAML_JSON_LT: i32 = 60
168
+ /** The bytes a core-schema null can start with: `~`, `n` and `N`. */
169
+ const YAML_JSON_TILDE: i32 = 126
170
+ const YAML_JSON_LOWER_N: i32 = 110
171
+ const YAML_JSON_UPPER_N: i32 = 78
172
+ /** The bytes that end a quoted scalar's run of text with nothing to decode: `'` and `\`. */
173
+ const YAML_JSON_SQUOTE: i32 = 39
174
+ const YAML_JSON_BACKSLASH: i32 = 92
175
+ /** The key words of a member with no key, and both words of an array's member. */
176
+ const YAML_JSON_NO_WORD: i32 = 0
163
177
 
164
178
  /**
165
179
  * A load failure's name, for a message.
@@ -196,6 +210,8 @@ export class YamlJsonLoader {
196
210
  /** The nodes the last count set a size on, the first `countedTop` of them: all a count clears. */
197
211
  private counted: i32[]
198
212
  private countedTop: i32 = 0
213
+ /** `quickCount`'s sizes, by node; each pass writes every one it reads. */
214
+ private quickSizes: i32[]
199
215
  /** The count's own stack of nodes. */
200
216
  private pending: i32[]
201
217
  /** The most values the document may hold; a size saturates one past it. */
@@ -229,6 +245,7 @@ export class YamlJsonLoader {
229
245
  this.stream = parseYaml("")
230
246
  this.doc = new JsonDocument(new JsonLimits())
231
247
  this.sizes = []
248
+ this.quickSizes = []
232
249
  this.counted = []
233
250
  this.pending = []
234
251
  this.entryKeys = []
@@ -273,7 +290,7 @@ export class YamlJsonLoader {
273
290
  return this.nullAt(at)
274
291
  }
275
292
  const room: i32 = doc.maxValues < JSON_VALUE_MAX_VALUES ? doc.maxValues : JSON_VALUE_MAX_VALUES
276
- if (this.count(root, room) > room) {
293
+ if (this.quickCount(this.firstNode(d), root, room) > room && this.count(root, room) > room) {
277
294
  if (this.bigAlias >= 0) {
278
295
  return this.fail(YAML_JSON_ERR_EXPANSION, stream.start(this.bigAlias), stream.end(this.bigAlias))
279
296
  }
@@ -298,8 +315,12 @@ export class YamlJsonLoader {
298
315
  if (v < 0) {
299
316
  return this.fail(v, start, end)
300
317
  }
301
- const code: i32 = this.doc.setRange(v, start, end)
302
- return code < 0 ? this.fail(code, start, end) : v
318
+ if (start < 0 || end < start) {
319
+ return this.fail(JSON_VALUE_ERR_RANGE, start, end)
320
+ }
321
+ // `v` was just made, so its index is current: what `setRange` would check.
322
+ this.doc.rangeAt(this.doc.index(v), start, end)
323
+ return v
303
324
  }
304
325
 
305
326
  /** A null with the empty range at `at`. */
@@ -363,6 +384,89 @@ export class YamlJsonLoader {
363
384
  return sizes[root]
364
385
  }
365
386
 
387
+ /** The first node of document `d`: the one after the last earlier document's root. */
388
+ private firstNode(d: i32): i32 {
389
+ for (let e: i32 = d - 1; e >= 0; e -= 1) {
390
+ const root: i32 = this.stream.documentRoot(e)
391
+ if (root >= 0) {
392
+ return root + 1
393
+ }
394
+ }
395
+ return 0
396
+ }
397
+
398
+ /**
399
+ * What `count(root, cap)` answers, found in one pass over nodes `lo` to
400
+ * `root` when that is enough: the parser numbers a node after its parts and
401
+ * an alias after its anchor, so each part's size is known when its node is
402
+ * reached. A part outside that span (a cycle, or a node of another
403
+ * document) counts as too many, so an answer over `cap` sends `load` to
404
+ * `count`, which also finds the alias to blame. Sizes are kept apart from
405
+ * `count`'s, and every one read was written by this pass, so none is cleared.
406
+ */
407
+ private quickCount(lo: i32, root: i32, cap: i32): i32 {
408
+ const stream: YamlStream = this.stream
409
+ const kinds: u8[] = stream.kinds
410
+ const over: i32 = cap + 1
411
+ if (lo < 0 || root < lo || root >= toI32(kinds.length) || root >= stream.top) {
412
+ return over
413
+ }
414
+ const sizes: i32[] = this.quickSizes
415
+ while (toI32(sizes.length) <= root) {
416
+ sizes.push(0)
417
+ }
418
+ this.cap = cap
419
+ const firsts: i32[] = stream.firsts
420
+ const counts: i32[] = stream.counts
421
+ const kids: i32[] = stream.kids
422
+ const top: i32 = stream.top
423
+ const merging: boolean = stream.merge
424
+ for (let n: i32 = lo; n <= root && n < toI32(kinds.length); n += 1) {
425
+ const kind: i32 = toI32(kinds[n])
426
+ let total: i32 = 1
427
+ if (kind === YAML_ALIAS) {
428
+ total = this.quickPart(yamlJsonAt(firsts, n), lo, n)
429
+ } else if (kind === YAML_SEQ || kind === YAML_MAP) {
430
+ const from: i32 = yamlJsonAt(firsts, n)
431
+ const parts: i32 = yamlJsonAt(counts, n)
432
+ for (let i: i32 = 0; i < parts; i += 1) {
433
+ const child: i32 = yamlJsonAt(kids, from + i)
434
+ if (kind === YAML_SEQ) {
435
+ total = this.plus(total, this.quickPart(child, lo, n))
436
+ } else {
437
+ // As `sizeOf` takes a merge's members in, less the merge itself;
438
+ // read straight, as `stream.value` and `stream.key` read a pair.
439
+ const pair: boolean =
440
+ child >= 0 && child < top && child < toI32(kinds.length) && toI32(kinds[child]) === YAML_PAIR
441
+ const value: i32 = pair ? yamlJsonAt(counts, child) : -1
442
+ const merged: boolean = pair && merging && this.mergeShaped(yamlJsonAt(firsts, child))
443
+ const part: i32 = this.quickPart(value, lo, n)
444
+ let less: i32 = merged ? 1 : 0
445
+ if (merged && stream.kind(stream.resolved(value)) === YAML_SEQ) {
446
+ less += stream.count(stream.resolved(value))
447
+ }
448
+ total = part > cap ? over : this.plus(total, part - less)
449
+ }
450
+ }
451
+ }
452
+ if (n < toI32(sizes.length)) {
453
+ sizes[n] = total
454
+ }
455
+ }
456
+ return sizes[root]
457
+ }
458
+
459
+ /** Part `p`'s size in `quickCount` at node `n`: `1` for a missing node, too many for one outside `[lo, n)`. */
460
+ private quickPart(p: i32, lo: i32, n: i32): i32 {
461
+ if (p < 0) {
462
+ return 1
463
+ }
464
+ if (p < lo || p >= n || p >= toI32(this.quickSizes.length)) {
465
+ return this.cap + 1
466
+ }
467
+ return this.quickSizes[p]
468
+ }
469
+
366
470
  /** Pushes node `n` onto the count's stack unless it is counted; answers the new top. */
367
471
  private pushNode(top: i32, n: i32): i32 {
368
472
  if (n < 0 || this.sizes[n] !== 0) {
@@ -456,10 +560,13 @@ export class YamlJsonLoader {
456
560
  /** Builds node `n`, nested `depth` collections deep, with `n`'s range on the value it answers. */
457
561
  private build(n: i32, depth: i32): JsonValue {
458
562
  const stream: YamlStream = this.stream
459
- const at: i32 = stream.resolved(n)
460
- const kind: i32 = stream.kind(at)
563
+ // Read straight, as `kind` reads it, for the node every value starts from.
564
+ const kinds: u8[] = stream.kinds
565
+ const nodeKind: i32 = n >= 0 && n < stream.top && n < toI32(kinds.length) ? toI32(kinds[n]) : -1
566
+ const at: i32 = nodeKind === YAML_ALIAS ? yamlJsonAt(stream.firsts, n) : n
567
+ const kind: i32 = at === n ? nodeKind : stream.kind(at)
461
568
  let v: JsonValue = 0
462
- const outer: boolean = this.copying < 0 && stream.kind(n) === YAML_ALIAS
569
+ const outer: boolean = this.copying < 0 && nodeKind === YAML_ALIAS
463
570
  if (outer) {
464
571
  this.copying = n
465
572
  }
@@ -498,6 +605,13 @@ export class YamlJsonLoader {
498
605
  private buildScalar(at: i32): JsonValue {
499
606
  const stream: YamlStream = this.stream
500
607
  const doc: JsonDocument = this.doc
608
+ // A quoted or block scalar with no properties is a string, as `type`
609
+ // would resolve it, with no tag to read; anything else asks `type`.
610
+ const style: i32 = stream.style(at)
611
+ if (style !== YAML_PLAIN && yamlJsonAt(stream.props, at) < 0) {
612
+ const quotedAt: i32 = doc.byteTop
613
+ return doc.newStoredString(quotedAt, this.storeScalar(at, style))
614
+ }
501
615
  const type: i32 = stream.type(at)
502
616
  if (type === YAML_NULL) {
503
617
  return doc.newNull()
@@ -506,6 +620,11 @@ export class YamlJsonLoader {
506
620
  return doc.newBoolean(stream.boolValue(at))
507
621
  }
508
622
  if (type === YAML_INT || type === YAML_FLOAT) {
623
+ const numberAt: i32 = doc.byteTop
624
+ const stored: i32 = this.storeJsonNumber(at)
625
+ if (stored >= 0) {
626
+ return doc.newStoredNumber(numberAt, stored)
627
+ }
509
628
  const text: string = yamlJsonNumber(stream.text(at))
510
629
  if (text === "") {
511
630
  return this.fail(YAML_JSON_ERR_NOT_FINITE, stream.start(at), stream.end(at))
@@ -515,7 +634,123 @@ export class YamlJsonLoader {
515
634
  }
516
635
  return doc.newNumber(text)
517
636
  }
518
- return doc.newString(stream.text(at))
637
+ const textAt: i32 = doc.byteTop
638
+ return doc.newStoredString(textAt, this.storeScalar(at, style))
639
+ }
640
+
641
+ /**
642
+ * Stores scalar `at`'s decoded text in the document and answers its
643
+ * length. A flow scalar with no break, and no `'` or `\\` when quoted, is
644
+ * its own body, copied straight from the source in the scan that finds so.
645
+ */
646
+ private storeScalar(at: i32, style: i32): i32 {
647
+ const stream: YamlStream = this.stream
648
+ if (style === YAML_PLAIN || style === YAML_SINGLE_QUOTED || style === YAML_DOUBLE_QUOTED) {
649
+ const firsts: i32[] = stream.firsts
650
+ const counts: i32[] = stream.counts
651
+ if (at >= 0 && at < toI32(firsts.length) && at < toI32(counts.length)) {
652
+ let stop: i32 = -1
653
+ if (style === YAML_SINGLE_QUOTED) {
654
+ stop = YAML_JSON_SQUOTE
655
+ } else if (style === YAML_DOUBLE_QUOTED) {
656
+ stop = YAML_JSON_BACKSLASH
657
+ }
658
+ const n: i32 =
659
+ yamlJsonAt(stream.extras, at) === YAML_VERBATIM
660
+ ? this.doc.copySpan(stream.source, firsts[at], counts[at])
661
+ : this.doc.storeSpan(stream.source, firsts[at], counts[at], stop)
662
+ if (n >= 0) {
663
+ return n
664
+ }
665
+ }
666
+ }
667
+ return this.doc.storeText(stream.text(at))
668
+ }
669
+
670
+ /**
671
+ * Stores number scalar `at` straight from its source when its text is
672
+ * already the JSON spelling `yamlJsonNumber` would give it, and answers its
673
+ * length; `-1`, with nothing stored, otherwise. That spelling is RFC 8259's
674
+ * number grammar, less `-0`, which `yamlJsonNumber` writes as `0`.
675
+ */
676
+ private storeJsonNumber(at: i32): i32 {
677
+ const stream: YamlStream = this.stream
678
+ if (stream.style(at) !== YAML_PLAIN) {
679
+ return -1
680
+ }
681
+ const source: string = stream.source
682
+ const from: i32 = yamlJsonAt(stream.firsts, at)
683
+ const to: i32 = yamlJsonAt(stream.counts, at)
684
+ if (from < 0 || to <= from || to > toI32(source.length)) {
685
+ return -1
686
+ }
687
+ let i: i32 = from
688
+ if (toI32(source.charCodeAt(i)) === YAML_JSON_DASH) {
689
+ i += 1
690
+ }
691
+ // A sign alone: the byte past it is not this scalar's.
692
+ if (i >= to) {
693
+ return -1
694
+ }
695
+ const lead: i32 = yamlJsonByte(source, i)
696
+ if (lead === YAML_JSON_ZERO) {
697
+ i += 1
698
+ if (i === to && i - from === 2) {
699
+ return -1
700
+ }
701
+ } else if (yamlJsonIsDigit(lead)) {
702
+ i = yamlJsonDigits(source, i, to)
703
+ } else {
704
+ return -1
705
+ }
706
+ if (i < to && toI32(source.charCodeAt(i)) === YAML_JSON_DOT) {
707
+ const fraction: i32 = yamlJsonDigits(source, i + 1, to)
708
+ if (fraction === i + 1) {
709
+ return -1
710
+ }
711
+ i = fraction
712
+ }
713
+ if (i < to && (toI32(source.charCodeAt(i)) | 32) === 101) {
714
+ i += 1
715
+ const sign: i32 = yamlJsonByte(source, i)
716
+ if (i < to && (sign === YAML_JSON_PLUS || sign === YAML_JSON_DASH)) {
717
+ i += 1
718
+ }
719
+ const exponent: i32 = yamlJsonDigits(source, i, to)
720
+ if (exponent === i) {
721
+ return -1
722
+ }
723
+ i = exponent
724
+ }
725
+ if (i !== to) {
726
+ return -1
727
+ }
728
+ // Digits, a dot, `e` and signs only: no break to look for.
729
+ return this.doc.copySpan(source, from, to)
730
+ }
731
+
732
+ /** Stores key node `key`'s text, as `keyText` spells it, in the document and answers its length. */
733
+ private storeKey(key: i32): i32 {
734
+ const stream: YamlStream = this.stream
735
+ if (stream.kind(key) === YAML_SCALAR) {
736
+ const style: i32 = stream.style(key)
737
+ if (yamlJsonAt(stream.props, key) < 0) {
738
+ // Untagged, only a plain key that is empty or starts `~`, `n` or `N` can be a null.
739
+ const start: i32 = stream.start(key)
740
+ const c: i32 = yamlJsonByte(stream.source, start)
741
+ if (
742
+ style !== YAML_PLAIN ||
743
+ (stream.end(key) > start &&
744
+ c !== YAML_JSON_TILDE &&
745
+ c !== YAML_JSON_LOWER_N &&
746
+ c !== YAML_JSON_UPPER_N)
747
+ ) {
748
+ return this.storeScalar(key, style)
749
+ }
750
+ }
751
+ return stream.type(key) === YAML_NULL ? 0 : this.storeScalar(key, style)
752
+ }
753
+ return this.doc.storeText(keyText(stream, key))
519
754
  }
520
755
 
521
756
  /** A sequence's array, each entry built in turn. */
@@ -526,27 +761,50 @@ export class YamlJsonLoader {
526
761
  if (a < 0) {
527
762
  return this.fail(a, stream.start(at), stream.end(at))
528
763
  }
529
- for (let i: i32 = 0; i < stream.count(at); i += 1) {
530
- const c: i32 = stream.child(at, i)
764
+ const ax: i32 = doc.index(a)
765
+ const n: i32 = stream.count(at)
766
+ doc.reserve(ax, n)
767
+ // `at` is a seq, so its items are `kids[first, first + n)`, as `child` reads them.
768
+ const kids: i32[] = stream.kids
769
+ const first: i32 = yamlJsonAt(stream.firsts, at)
770
+ for (let i: i32 = 0; i < n; i += 1) {
771
+ const c: i32 = yamlJsonAt(kids, first + i)
531
772
  const v: JsonValue = c < 0 ? this.nullAt(stream.end(at)) : this.build(c, depth)
532
773
  if (v < 0) {
533
774
  return v
534
775
  }
535
- const code: i32 = doc.push(a, v)
536
- if (code < 0) {
537
- return this.fail(code, stream.start(at), stream.end(at))
538
- }
776
+ // `v` was just made, so it is in no container: what `push` would check.
777
+ doc.append(ax, doc.index(v), YAML_JSON_NO_WORD, YAML_JSON_NO_WORD)
539
778
  }
540
779
  return a
541
780
  }
542
781
 
543
782
  /**
544
- * A map's object. Its members are gathered first, own keys then
545
- * merged ones, with repeats settled; then each value is built and added.
783
+ * A map's object. A map with no merge key, in a stream whose parser
784
+ * reported every repeated key, is its own pairs in order (`buildOwnMap`).
785
+ * Otherwise its members are gathered first, own keys then merged ones, with
786
+ * repeats settled; then each value is built and added.
546
787
  */
547
788
  private buildMap(at: i32, depth: i32): JsonValue {
548
789
  const stream: YamlStream = this.stream
549
790
  const doc: JsonDocument = this.doc
791
+ const pairs: i32 = stream.count(at)
792
+ let own: boolean = stream.uniqueKeys
793
+ let merges: boolean = false
794
+ const kids: i32[] = stream.kids
795
+ const first: i32 = yamlJsonAt(stream.firsts, at)
796
+ for (let i: i32 = 0; i < pairs; i += 1) {
797
+ const key: i32 = stream.key(yamlJsonAt(kids, first + i))
798
+ if (key < 0) {
799
+ own = false
800
+ } else if (this.mergeShaped(key)) {
801
+ own = false
802
+ merges = merges || stream.merge
803
+ }
804
+ }
805
+ if (own) {
806
+ return this.buildOwnMap(at, pairs, depth)
807
+ }
550
808
  const base: i32 = this.entryTop
551
809
  // Clearing costs the table's capacity, which a large map leaves behind.
552
810
  if (this.seen.size > 0) {
@@ -558,11 +816,13 @@ export class YamlJsonLoader {
558
816
  return this.failed
559
817
  }
560
818
  }
561
- const outer: i32 = 0
562
- this.newMergeStamp()
563
- this.markMerged(at)
564
- if (this.mergeInto(base, at, outer) < 0) {
565
- return this.failed
819
+ if (merges) {
820
+ const outer: i32 = 0
821
+ this.newMergeStamp()
822
+ this.markMerged(at)
823
+ if (this.mergeInto(base, at, outer) < 0) {
824
+ return this.failed
825
+ }
566
826
  }
567
827
  const top: i32 = this.entryTop
568
828
  const o: JsonValue = doc.newObject()
@@ -570,6 +830,7 @@ export class YamlJsonLoader {
570
830
  return this.fail(o, stream.start(at), stream.end(at))
571
831
  }
572
832
  const ox: i32 = doc.index(o)
833
+ doc.reserve(ox, top - base)
573
834
  for (let e: i32 = base; e < top; e += 1) {
574
835
  const pair: i32 = this.entryPairs[e]
575
836
  const valueNode: i32 = this.entryValues[e]
@@ -579,22 +840,73 @@ export class YamlJsonLoader {
579
840
  }
580
841
  const key: string = this.entryKeys[e]
581
842
  const keyAt: i32 = doc.byteTop
582
- doc.append(ox, doc.index(v), keyAt, doc.storeText(key))
583
843
  const keyNode: i32 = stream.key(pair)
844
+ // `key` is `keyText(keyNode)`, which `storeKey` spells from the node without the copy.
845
+ const keyLength: i32 = keyNode < 0 ? doc.storeText(key) : this.storeKey(keyNode)
846
+ const slot: i32 = doc.append(ox, doc.index(v), keyAt, keyLength)
584
847
  if (doc.byteTop > this.byteCap) {
585
848
  return this.tooMuchText(keyNode < 0 ? pair : keyNode)
586
849
  }
587
850
  const keyStart: i32 = keyNode < 0 ? stream.start(pair) : stream.start(keyNode)
588
851
  const keyEnd: i32 = keyNode < 0 ? keyStart : stream.end(keyNode)
589
- const code: i32 = doc.setKeyRange(o, e - base, keyStart, keyEnd)
590
- if (code < 0) {
591
- return this.fail(code, keyStart, keyEnd)
852
+ if (this.keyRange(slot, keyStart, keyEnd) < 0) {
853
+ return this.failed
592
854
  }
593
855
  }
594
856
  this.entryTop = base
595
857
  return o
596
858
  }
597
859
 
860
+ /**
861
+ * Map `at`'s object when its `pairs` pairs are all its members: no
862
+ * merge key, and no two keys alike, which the parser has reported under
863
+ * `uniqueKeys` (a document with an error never loads). Each key is
864
+ * stored from its node as `keyText` spells it, after its value is built.
865
+ */
866
+ private buildOwnMap(at: i32, pairs: i32, depth: i32): JsonValue {
867
+ const stream: YamlStream = this.stream
868
+ const doc: JsonDocument = this.doc
869
+ const o: JsonValue = doc.newObject()
870
+ if (o < 0) {
871
+ return this.fail(o, stream.start(at), stream.end(at))
872
+ }
873
+ const ox: i32 = doc.index(o)
874
+ doc.reserve(ox, pairs)
875
+ // `buildMap` found every pair's key, so each child is a pair, whose key
876
+ // and value are its `firsts` and `counts`, as `key` and `value` read them.
877
+ const kids: i32[] = stream.kids
878
+ const firsts: i32[] = stream.firsts
879
+ const counts: i32[] = stream.counts
880
+ const first: i32 = yamlJsonAt(firsts, at)
881
+ for (let i: i32 = 0; i < pairs; i += 1) {
882
+ const pair: i32 = yamlJsonAt(kids, first + i)
883
+ const valueNode: i32 = yamlJsonAt(counts, pair)
884
+ const v: JsonValue = valueNode < 0 ? this.nullAt(stream.end(pair)) : this.build(valueNode, depth)
885
+ if (v < 0) {
886
+ return v
887
+ }
888
+ const keyNode: i32 = yamlJsonAt(firsts, pair)
889
+ const keyAt: i32 = doc.byteTop
890
+ const slot: i32 = doc.append(ox, doc.index(v), keyAt, this.storeKey(keyNode))
891
+ if (doc.byteTop > this.byteCap) {
892
+ return this.tooMuchText(keyNode)
893
+ }
894
+ if (this.keyRange(slot, stream.start(keyNode), stream.end(keyNode)) < 0) {
895
+ return this.failed
896
+ }
897
+ }
898
+ return o
899
+ }
900
+
901
+ /** Gives member slot `slot` the key range `[start, end)`; a negative answer is the failure, recorded. */
902
+ private keyRange(slot: i32, start: i32, end: i32): i32 {
903
+ if (start < 0 || end < start) {
904
+ return this.fail(JSON_VALUE_ERR_RANGE, start, end)
905
+ }
906
+ this.doc.keyRangeAt(slot, start, end)
907
+ return 0
908
+ }
909
+
598
910
  /**
599
911
  * Adds the merge keys' maps of map `at` to the entries from
600
912
  * `base`, each key only if it is not there yet: every map a merge key
@@ -749,19 +1061,35 @@ export class YamlJsonLoader {
749
1061
 
750
1062
  /**
751
1063
  * Whether `key` is a merge key, as the stream reads one: a plain
752
- * `<<`, untagged or `!!merge`. Keep in step with `yamlIsMergeKey` in
753
- * `std/yaml.ts`, which is not exported.
1064
+ * `<<`, untagged or `!!merge`, while the stream merges. Keep in step with
1065
+ * `yamlIsMergeKey` in `std/yaml.ts`, which is not exported.
754
1066
  */
755
1067
  private isMergeKey(key: i32): boolean {
1068
+ return this.stream.merge && this.mergeShaped(key)
1069
+ }
1070
+
1071
+ /**
1072
+ * Whether `key` is shaped as a merge key, whatever `stream.merge` now
1073
+ * says: the parser checked repeats with the flag as it was then, so a map
1074
+ * holding one is not a map of unique keys.
1075
+ */
1076
+ private mergeShaped(key: i32): boolean {
756
1077
  const stream: YamlStream = this.stream
757
- if (!stream.merge || key < 0 || stream.kind(key) !== YAML_SCALAR || stream.style(key) !== YAML_PLAIN) {
1078
+ if (key < 0) {
758
1079
  return false
759
1080
  }
1081
+ // Almost every key is turned away by its length or its first byte; the
1082
+ // length is read first, so an empty key at the end of the text is not
1083
+ // read past.
760
1084
  const start: i32 = stream.start(key)
761
1085
  if (
1086
+ start < 0 ||
762
1087
  stream.end(key) !== start + 2 ||
1088
+ start + 2 > toI32(stream.source.length) ||
763
1089
  toI32(stream.source.charCodeAt(start)) !== YAML_JSON_LT ||
764
- toI32(stream.source.charCodeAt(start + 1)) !== YAML_JSON_LT
1090
+ toI32(stream.source.charCodeAt(start + 1)) !== YAML_JSON_LT ||
1091
+ stream.kind(key) !== YAML_SCALAR ||
1092
+ stream.style(key) !== YAML_PLAIN
765
1093
  ) {
766
1094
  return false
767
1095
  }
@@ -770,6 +1098,9 @@ export class YamlJsonLoader {
770
1098
  }
771
1099
  }
772
1100
 
1101
+ /** `a[i]`, or `-1` outside it, as `YamlStream`'s accessors read their arrays. */
1102
+ const yamlJsonAt = (a: i32[], i: i32): i32 => (i >= 0 && i < toI32(a.length) ? a[i] : -1)
1103
+
773
1104
  // ---- Numbers ---------------------------------------------------------------------
774
1105
 
775
1106
  /**
@@ -846,6 +1177,15 @@ const yamlJsonByte = (text: string, i: i32): i32 =>
846
1177
 
847
1178
  const yamlJsonIsDigit = (c: i32): boolean => c >= YAML_JSON_ZERO && c <= 57
848
1179
 
1180
+ /** The end of the run of decimal digits in `text[from, to)` that starts at `from`. */
1181
+ const yamlJsonDigits = (text: string, from: i32, to: i32): i32 => {
1182
+ let i: i32 = from
1183
+ while (i < to && yamlJsonIsDigit(yamlJsonByte(text, i))) {
1184
+ i += 1
1185
+ }
1186
+ return i
1187
+ }
1188
+
849
1189
  /** A hex digit's value; the text is the stream's, already checked to be digits of its base. */
850
1190
  const yamlJsonHexValue = (c: i32): i32 => {
851
1191
  if (c >= YAML_JSON_ZERO && c <= 57) {