@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.
- package/INSTALL.md +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/std/json-typed.ts
CHANGED
|
@@ -25,10 +25,12 @@
|
|
|
25
25
|
* functions here for each field, and into `JsonWriter`'s, so all of the parsing
|
|
26
26
|
* is the tokenizer's and all of the writing is the writer's. A field may be an
|
|
27
27
|
* `i32`, `u32`, `i64`, `u64`, `f64` (or `number`), `boolean` or `string`, a
|
|
28
|
-
* numeric enum (its number), a
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
28
|
+
* numeric enum (its number), a string enum (its member's string), a class
|
|
29
|
+
* decoded the same way, a tagged union of
|
|
30
|
+
* classes, a `JsonNumber`, an array of any of those, a `Map<string, V>` (a
|
|
31
|
+
* JSON object) whose `V` is any of these, or a string, class, union, array or
|
|
32
|
+
* map `| null`; anything else is refused at the call, naming the field
|
|
33
|
+
* (docs/LANGUAGE.md, "Typed JSON").
|
|
32
34
|
*
|
|
33
35
|
* **Call shapes.** `jsonDecode(d, out)` reads the one JSON object `d` holds
|
|
34
36
|
* into `out`, a declared class, and answers `d.failed`: `JSON_DECODE_OK` (0)
|
|
@@ -52,6 +54,10 @@
|
|
|
52
54
|
* 2^64 - 1
|
|
53
55
|
* numeric enum E the number of one of E's d.i32(), then w.i32
|
|
54
56
|
* members a switch
|
|
57
|
+
* string enum E the string of one of E's d.enumString(), w.string of
|
|
58
|
+
* members then its bytes the member's
|
|
59
|
+
* against each string
|
|
60
|
+
* member's
|
|
55
61
|
* f64 (number in f64 mode) any number; past the double d.f64() w.f64
|
|
56
62
|
* range an infinity, which the
|
|
57
63
|
* writer writes as null
|
|
@@ -70,6 +76,11 @@
|
|
|
70
76
|
* map's, each value a V each value set map's order
|
|
71
77
|
* JsonNumber any number, kept as the text d.number(n) jsonEncodeNumber
|
|
72
78
|
* it is written in
|
|
79
|
+
* tagged union U, U | null an object whose tag names a the variant's the variant's
|
|
80
|
+
* variant, and null for the decoding, into encoding, its
|
|
81
|
+
* second the object held tag first
|
|
82
|
+
* when its tag
|
|
83
|
+
* matches
|
|
73
84
|
*
|
|
74
85
|
* A `Map`'s `V` is any type in this list, `| null` or not, a `Map` included;
|
|
75
86
|
* `JsonNumber` may stand alone, be `| null`, be an array's element or be a
|
|
@@ -81,9 +92,9 @@
|
|
|
81
92
|
* field (NL2485 for a field type, such as an `f32`, an `i32[][]`, a
|
|
82
93
|
* `Level[][]`, a `Set`, an array of maps, a `Map` not keyed by `string` (an
|
|
83
94
|
* enum key included) or a `Map` whose values cannot be written, and a generic
|
|
84
|
-
* class of the program's own). An enum
|
|
85
|
-
* not one the language declares (NL1043),
|
|
86
|
-
* it is declared (NL2285).
|
|
95
|
+
* class of the program's own). An enum that mixes string and numeric members
|
|
96
|
+
* is not one the language declares (NL1043), an enum is never `| null`
|
|
97
|
+
* (NL2010) and a `const enum` is refused where it is declared (NL2285).
|
|
87
98
|
*
|
|
88
99
|
* A value that is not a declared class (an interface, a generic class or any
|
|
89
100
|
* other type) is NL2486; decoding into an interface is left for a later
|
|
@@ -182,6 +193,9 @@
|
|
|
182
193
|
* its field's type. Without it every field is required and a missing key is
|
|
183
194
|
* `JSON_DECODE_MISSING`.
|
|
184
195
|
* - `presence`, described below. Without it nothing is recorded.
|
|
196
|
+
* - `tag`, a string literal: what a variant of a tagged union writes as its
|
|
197
|
+
* tag, described below. Without it, or with `tag: ""`, the tag is the enum
|
|
198
|
+
* member's name, or for a string enum the member's string.
|
|
185
199
|
*
|
|
186
200
|
* Each misuse has its own diagnostic:
|
|
187
201
|
*
|
|
@@ -191,7 +205,7 @@
|
|
|
191
205
|
* the literal it must be (a module constant, an array for the
|
|
192
206
|
* object, a string for `optional`'s array); `presence` or one of
|
|
193
207
|
* its entries not an object literal of string literals
|
|
194
|
-
* NL2494 an option other than `rename`, `optional` and `
|
|
208
|
+
* NL2494 an option other than `rename`, `optional`, `presence` and `tag`
|
|
195
209
|
* NL2495 a name that is not one of the class's fields; `presence` naming
|
|
196
210
|
* a field or a presence field the class does not declare
|
|
197
211
|
* NL2496 a rename to a value that is not a string
|
|
@@ -204,9 +218,11 @@
|
|
|
204
218
|
* field it records, records two fields, is recorded by a presence
|
|
205
219
|
* field of its own or is renamed or made optional; a field
|
|
206
220
|
* recorded twice
|
|
221
|
+
* NL2722 a `tag` that is not a string literal
|
|
222
|
+
* NL2724 a `tag` on a class with no field whose type is one enum member
|
|
207
223
|
*
|
|
208
|
-
* A spread
|
|
209
|
-
* (
|
|
224
|
+
* A spread in the literal is refused (NL2711), a computed key as it is
|
|
225
|
+
* anywhere (NL1041), a subclass by `extends`, and a second `static readonly
|
|
210
226
|
* json` is a duplicate member. The options are not a value: `User.json` names a
|
|
211
227
|
* class where a value is due, and is refused as any class name there is.
|
|
212
228
|
*
|
|
@@ -253,6 +269,54 @@
|
|
|
253
269
|
* refused before any of it is applied. `examples/merge-patch.ts` is the
|
|
254
270
|
* recipe in full.
|
|
255
271
|
*
|
|
272
|
+
* **A tagged union.** A union of classes, each with a field whose type is
|
|
273
|
+
* one member of one enum (`readonly kind: Kind.Num = Kind.Num`), is written
|
|
274
|
+
* as one object whose tag picks the variant:
|
|
275
|
+
*
|
|
276
|
+
* enum Kind { Num, Neg }
|
|
277
|
+
* class Num { readonly kind: Kind.Num = Kind.Num; value: f64 = 0 }
|
|
278
|
+
* class Neg {
|
|
279
|
+
* static readonly json = { tag: "negate" };
|
|
280
|
+
* readonly kind: Kind.Neg = Kind.Neg;
|
|
281
|
+
* arg: Expr;
|
|
282
|
+
* constructor() { this.arg = zero() }
|
|
283
|
+
* }
|
|
284
|
+
* type Expr = Num | Neg; // {"kind":"negate","arg":{"kind":"Num","value":2}}
|
|
285
|
+
*
|
|
286
|
+
* let e: Expr = zero();
|
|
287
|
+
* e = jsonDecodeUnion(d, e); // d.failed says whether it worked
|
|
288
|
+
* jsonEncode(w, e);
|
|
289
|
+
*
|
|
290
|
+
* The tag's key is the tag field's name, or its `rename`, and every variant
|
|
291
|
+
* gives the same one (NL2721). Its value is the enum member's name (a string
|
|
292
|
+
* enum's member's string), or the variant's `tag` option, and no two variants
|
|
293
|
+
* write the same one (NL2720). The
|
|
294
|
+
* tag may be any key of the object: when it is the first, the variant is picked
|
|
295
|
+
* at once; when it is not, the keys before it are stepped over, checked but not
|
|
296
|
+
* kept, and the object is read again from its first key once the variant is
|
|
297
|
+
* known. A nested union does the same inside the bytes its parent read ahead
|
|
298
|
+
* over only when it is decoded, so each byte of the input is read again at
|
|
299
|
+
* most once per level of nesting above it: at most `maxDepth` times
|
|
300
|
+
* (docs/security/json.md, JSON-17), counted in `JsonDecoder.unionReread`.
|
|
301
|
+
*
|
|
302
|
+
* `jsonDecode` decodes into the object it is given and cannot change which
|
|
303
|
+
* class that is, so a union is decoded with `jsonDecodeUnion(d, prev)`, which
|
|
304
|
+
* answers the object it decoded into: `prev` when the tag names `prev`'s
|
|
305
|
+
* variant, a new one made by `new V()` when it does not or `prev` is `null`,
|
|
306
|
+
* and `prev` when the decode fails (NL2719 refuses `jsonDecode` of a union;
|
|
307
|
+
* NL2723 refuses `jsonDecodeUnion` of anything else). A union field, a union
|
|
308
|
+
* array's element and a `| null` union field follow the same rule. Encoding
|
|
309
|
+
* writes the tag first, then the variant's fields. A variant decoded alone,
|
|
310
|
+
* by `jsonDecode`, reads its tag like any field and fails with
|
|
311
|
+
* `JSON_DECODE_TAG` when it is not its own. The code is written once per
|
|
312
|
+
* union, so a recursive union is one function that calls itself, and a
|
|
313
|
+
* `Map<string, Expr>` decodes and encodes each value through it.
|
|
314
|
+
*
|
|
315
|
+
* A failed decode answers `prev` at the top only. A union field, element or
|
|
316
|
+
* map value below it whose tag named another variant already holds the new
|
|
317
|
+
* variant when the failure comes, decoded up to it, as any failed `jsonDecode`
|
|
318
|
+
* leaves what it read: check `d.failed` before using the object.
|
|
319
|
+
*
|
|
256
320
|
* **Errors are sticky codes**, as the tokenizer's and the writer's are. The
|
|
257
321
|
* first error sets `failed` to a negative code — a `JSON_DECODE_*` code here,
|
|
258
322
|
* or the tokenizer's own `JSON_TOK_ERR_*` code when the text is not JSON or
|
|
@@ -271,6 +335,8 @@
|
|
|
271
335
|
* JSON_DECODE_DUPLICATE -23 a key given twice the second
|
|
272
336
|
* JSON_DECODE_MISSING -24 a field the object lacks the object's `}`
|
|
273
337
|
* JSON_DECODE_MAP_LIMIT -25 more than maxMapKeys map keys the first key past it
|
|
338
|
+
* JSON_DECODE_TAG -26 a union's tag missing, not a the tag's value, or
|
|
339
|
+
* string, or no variant's the object's `{`
|
|
274
340
|
* JSON_TOK_ERR_* -1 to -13 text that is not JSON, or past the tokenizer's
|
|
275
341
|
* a JsonLimits limit
|
|
276
342
|
*
|
|
@@ -285,13 +351,21 @@
|
|
|
285
351
|
* steps over is not detected, because its value is never read. A map is
|
|
286
352
|
* bounded by `maxMapKeys`, and a number's length by `JsonLimits.maxNumberBytes`.
|
|
287
353
|
*
|
|
288
|
-
* **
|
|
354
|
+
* **A numeric enum is its number.** It is read with `d.i32()`, so a
|
|
289
355
|
* number that is not an `i32` integer is that field's `JSON_DECODE_RANGE`, and
|
|
290
356
|
* an integer that is not the value of one of its members is
|
|
291
357
|
* `JSON_DECODE_TYPE`; a member's name, `"High"`, is a string and a type error
|
|
292
358
|
* too. Two members of one value are one number. It is written as `w.i32` of
|
|
293
359
|
* its value, as `JSON.stringify` writes a TypeScript numeric enum.
|
|
294
360
|
*
|
|
361
|
+
* **A string enum is its member's string.** It is read with `d.enumString()`
|
|
362
|
+
* and the string's bytes are matched against the members' strings by length,
|
|
363
|
+
* then by a byte, then with `keyIs`, so the read allocates nothing; an escaped
|
|
364
|
+
* string matches as its unescaped bytes. A string that is no member's string,
|
|
365
|
+
* and a value that is not a string, are `JSON_DECODE_TYPE`, and the field keeps
|
|
366
|
+
* the member it held. It is written as `w.string` of the member's string, as
|
|
367
|
+
* `JSON.stringify` writes a TypeScript string enum.
|
|
368
|
+
*
|
|
295
369
|
* **A `u64` is exact to 2^64 - 1.** It is read through the tokenizer's
|
|
296
370
|
* `numberU64`, so `18446744073709551615` decodes and `18446744073709551616`,
|
|
297
371
|
* `-1`, `-0` and `1.5` are `JSON_DECODE_RANGE`. It is written as its digits,
|
|
@@ -373,10 +447,17 @@ export const JSON_DECODE_MISSING: i32 = -24
|
|
|
373
447
|
* together (docs/security/json.md, JSON-13).
|
|
374
448
|
*/
|
|
375
449
|
export const JSON_DECODE_MAP_LIMIT: i32 = -25
|
|
450
|
+
/**
|
|
451
|
+
* A tagged union's object whose tag is missing, not a string, or none of the
|
|
452
|
+
* union's variants' wire tags, or a variant's object whose tag is not that
|
|
453
|
+
* variant's own; `errorAt` is the tag's value, or the object's `{` when the
|
|
454
|
+
* tag is missing.
|
|
455
|
+
*/
|
|
456
|
+
export const JSON_DECODE_TAG: i32 = -26
|
|
376
457
|
|
|
377
458
|
/** What a program running under Node is told, where the compiler would have written the code. */
|
|
378
459
|
const JSON_TYPED_ERASED: string =
|
|
379
|
-
"nish/json-typed: jsonDecode and jsonEncode are written by the compiler from the class's fields, and under Node the types are erased"
|
|
460
|
+
"nish/json-typed: jsonDecode, jsonDecodeUnion and jsonEncode are written by the compiler from the class's fields, and under Node the types are erased"
|
|
380
461
|
|
|
381
462
|
/**
|
|
382
463
|
* A JSON number kept as the text it was written in, so that no digit is lost:
|
|
@@ -537,6 +618,20 @@ export class JsonDecoder {
|
|
|
537
618
|
* ended, or a value `takeNull` found was not `null`.
|
|
538
619
|
*/
|
|
539
620
|
private held: boolean = false
|
|
621
|
+
/** The `{` of the union object `unionTag` read ahead in, or `-1` when its tag was the first key. */
|
|
622
|
+
private unionStart: i32 = -1
|
|
623
|
+
/**
|
|
624
|
+
* The bytes this decoding has read twice: for each tagged union whose tag
|
|
625
|
+
* was not its first key, those from its `{` to the end of its tag, which
|
|
626
|
+
* the decoder read ahead over and then reads again as the variant's
|
|
627
|
+
* fields. A byte is read again once for each union object it is inside,
|
|
628
|
+
* so this is at most `maxDepth` times the input's length
|
|
629
|
+
* (docs/security/json.md, JSON-17), and it stops at `i32`'s largest
|
|
630
|
+
* value, 2147483647, which only raised limits reach; `reset` clears it.
|
|
631
|
+
* It counts the spans `enterUnion` reads again, not the keys `unionTag`
|
|
632
|
+
* steps over to find a tag.
|
|
633
|
+
*/
|
|
634
|
+
unionReread: i32 = 0
|
|
540
635
|
|
|
541
636
|
/**
|
|
542
637
|
* A decoder over `src[off, off + len)`, with no error and no map key taken.
|
|
@@ -565,6 +660,8 @@ export class JsonDecoder {
|
|
|
565
660
|
this.errorAt = -1
|
|
566
661
|
this.held = false
|
|
567
662
|
this.mapKeys = 0
|
|
663
|
+
this.unionStart = -1
|
|
664
|
+
this.unionReread = 0
|
|
568
665
|
}
|
|
569
666
|
|
|
570
667
|
/**
|
|
@@ -591,6 +688,27 @@ export class JsonDecoder {
|
|
|
591
688
|
this.fail(JSON_DECODE_TYPE, this.tok.tokStart)
|
|
592
689
|
}
|
|
593
690
|
|
|
691
|
+
/**
|
|
692
|
+
* Takes the next value as a string enum's string, which it leaves as the
|
|
693
|
+
* current token for `keyLength`, `keyByte` and `keyIs` to match against
|
|
694
|
+
* the members' strings.
|
|
695
|
+
*
|
|
696
|
+
* @returns `true` at a string; `false` with `JSON_DECODE_TYPE` for any other value, and after any error.
|
|
697
|
+
*
|
|
698
|
+
* @internal Called by name by the decoder the compiler writes for
|
|
699
|
+
* `jsonDecode`, src/emit-json.ts.
|
|
700
|
+
*/
|
|
701
|
+
enumString(): boolean {
|
|
702
|
+
const k: i32 = this.take()
|
|
703
|
+
if (k === JSON_TOK_STRING) {
|
|
704
|
+
return true
|
|
705
|
+
}
|
|
706
|
+
if (k >= 0) {
|
|
707
|
+
this.wrongType()
|
|
708
|
+
}
|
|
709
|
+
return false
|
|
710
|
+
}
|
|
711
|
+
|
|
594
712
|
/**
|
|
595
713
|
* Takes the next value's first token: the held token, when there is one,
|
|
596
714
|
* and the tokenizer's next otherwise.
|
|
@@ -772,6 +890,111 @@ export class JsonDecoder {
|
|
|
772
890
|
}
|
|
773
891
|
}
|
|
774
892
|
|
|
893
|
+
/**
|
|
894
|
+
* Opens a tagged union's object and reads ahead to its tag, the key `key`,
|
|
895
|
+
* stepping over the values of the keys before it, and leaves the tag's
|
|
896
|
+
* string as the current token for `keyLength`, `keyByte` and `keyIs`.
|
|
897
|
+
* When the tag was not the first key, `enterUnion` goes back to the
|
|
898
|
+
* object's first key. A missing tag, or one that is not a string, is
|
|
899
|
+
* `JSON_DECODE_TAG`; any other value than an object is `JSON_DECODE_TYPE`.
|
|
900
|
+
*
|
|
901
|
+
* @param key The tag's key on the wire.
|
|
902
|
+
* @returns `true` at the tag's string; `false` after any error.
|
|
903
|
+
*
|
|
904
|
+
* @internal Called by name by the decoder the compiler writes for a union,
|
|
905
|
+
* src/emit-json.ts.
|
|
906
|
+
*/
|
|
907
|
+
unionTag(key: string): boolean {
|
|
908
|
+
if (!this.beginObject()) {
|
|
909
|
+
return false
|
|
910
|
+
}
|
|
911
|
+
const tok: JsonTokenizer = this.tok
|
|
912
|
+
const start: i32 = tok.tokStart
|
|
913
|
+
let first: boolean = true
|
|
914
|
+
while (this.nextField()) {
|
|
915
|
+
if (tok.keyEquals(key)) {
|
|
916
|
+
const k: i32 = tok.next()
|
|
917
|
+
if (k < 0) {
|
|
918
|
+
this.fail(k, tok.errorAt)
|
|
919
|
+
return false
|
|
920
|
+
}
|
|
921
|
+
if (k !== JSON_TOK_STRING) {
|
|
922
|
+
this.fail(JSON_DECODE_TAG, tok.tokStart)
|
|
923
|
+
return false
|
|
924
|
+
}
|
|
925
|
+
this.unionStart = first ? -1 : start
|
|
926
|
+
return true
|
|
927
|
+
}
|
|
928
|
+
first = false
|
|
929
|
+
const k: i32 = tok.skipValue()
|
|
930
|
+
if (k < 0) {
|
|
931
|
+
this.fail(k, tok.errorAt)
|
|
932
|
+
return false
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
this.fail(JSON_DECODE_TAG, start)
|
|
936
|
+
return false
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* Records `JSON_DECODE_TAG` at the current string, a tag no variant of the
|
|
941
|
+
* union gives.
|
|
942
|
+
*
|
|
943
|
+
* @internal Called by name by the decoder the compiler writes for a union,
|
|
944
|
+
* src/emit-json.ts.
|
|
945
|
+
*/
|
|
946
|
+
unknownTag(): void {
|
|
947
|
+
this.unionStart = -1
|
|
948
|
+
this.fail(JSON_DECODE_TAG, this.tok.tokStart)
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* Readies the object `unionTag` read the tag of for its variant's fields:
|
|
953
|
+
* when the tag was the first key, the next key is the one after it; when it
|
|
954
|
+
* was not, the object starts over from its first key, and the tag is read
|
|
955
|
+
* again as a field.
|
|
956
|
+
*
|
|
957
|
+
* @returns `true` when the tag is already read; `false` when it is still to come.
|
|
958
|
+
*
|
|
959
|
+
* @internal Called by name by the decoder the compiler writes for a union,
|
|
960
|
+
* src/emit-json.ts.
|
|
961
|
+
*/
|
|
962
|
+
enterUnion(): boolean {
|
|
963
|
+
const start: i32 = this.unionStart
|
|
964
|
+
if (start < 0) {
|
|
965
|
+
return true
|
|
966
|
+
}
|
|
967
|
+
this.unionStart = -1
|
|
968
|
+
// Held at `i32`'s largest value, where raised limits could carry a sum
|
|
969
|
+
// past it: nothing an input holds may panic.
|
|
970
|
+
const span: i32 = this.tok.tokEnd - start
|
|
971
|
+
this.unionReread = span > 2147483647 - this.unionReread ? 2147483647 : this.unionReread + span
|
|
972
|
+
this.tok.rewindObject(start)
|
|
973
|
+
return false
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
/**
|
|
977
|
+
* Reads a variant's tag field: its value must be the string `name`, and
|
|
978
|
+
* anything else is `JSON_DECODE_TAG` at the value.
|
|
979
|
+
*
|
|
980
|
+
* @param name The variant's tag on the wire.
|
|
981
|
+
*
|
|
982
|
+
* @internal Called by name by the decoder the compiler writes for
|
|
983
|
+
* `jsonDecode`, src/emit-json.ts.
|
|
984
|
+
*/
|
|
985
|
+
tagValue(name: string): void {
|
|
986
|
+
const k: i32 = this.take()
|
|
987
|
+
if (k === JSON_TOK_STRING) {
|
|
988
|
+
if (!this.tok.keyEquals(name)) {
|
|
989
|
+
this.fail(JSON_DECODE_TAG, this.tok.tokStart)
|
|
990
|
+
}
|
|
991
|
+
return
|
|
992
|
+
}
|
|
993
|
+
if (k >= 0) {
|
|
994
|
+
this.fail(JSON_DECODE_TAG, this.tok.tokStart)
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
|
|
775
998
|
/**
|
|
776
999
|
* Records `JSON_DECODE_DUPLICATE` at the current key, which the object
|
|
777
1000
|
* already gave.
|
|
@@ -1439,6 +1662,27 @@ export const jsonDecode = <T>(_d: JsonDecoder, _out: T): i32 => {
|
|
|
1439
1662
|
panic(JSON_TYPED_ERASED)
|
|
1440
1663
|
}
|
|
1441
1664
|
|
|
1665
|
+
/**
|
|
1666
|
+
* Decodes the JSON text `d` reads into a tagged union, `T`: the object's tag
|
|
1667
|
+
* picks the variant. When it is `prev`'s, `prev` is decoded into and
|
|
1668
|
+
* returned, its fields, nested objects and arrays reused; otherwise a new
|
|
1669
|
+
* object of that variant is made with its constructor of no parameters and
|
|
1670
|
+
* returned. The tag may be any key of the object; it is cheapest first. The
|
|
1671
|
+
* compiler writes the code from the union's variants at every call, and `T`
|
|
1672
|
+
* is never written at the call; under Node this body ends the program with a
|
|
1673
|
+
* panic, because the types are erased.
|
|
1674
|
+
*
|
|
1675
|
+
* @param _d The decoder over the input; its `failed` and `errorAt` say what
|
|
1676
|
+
* went wrong, and `reset` starts it over.
|
|
1677
|
+
* @param _prev The value the union holds now, reused when the tag names its variant.
|
|
1678
|
+
* @returns The decoded value; on any failure `prev`, perhaps partly
|
|
1679
|
+
* overwritten, with `d.failed` set to the first error's code, such as
|
|
1680
|
+
* `JSON_DECODE_TAG`.
|
|
1681
|
+
*/
|
|
1682
|
+
export const jsonDecodeUnion = <T>(_d: JsonDecoder, _prev: T): T => {
|
|
1683
|
+
panic(JSON_TYPED_ERASED)
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1442
1686
|
/**
|
|
1443
1687
|
* Writes `value` to `w` as one JSON object, its fields in declaration order,
|
|
1444
1688
|
* the text `JSON.stringify` writes for it, in the writer's modes. The compiler
|
package/std/json-value.ts
CHANGED
|
@@ -149,6 +149,7 @@
|
|
|
149
149
|
* the same under `--number-mode f64`, and it runs under the Node prelude, where
|
|
150
150
|
* `tests/run.js` holds every method to the native answers.
|
|
151
151
|
*/
|
|
152
|
+
import { uncheckedSet } from "nish:unsafe"
|
|
152
153
|
import { JSON_WRITE_FULL, JSON_WRITE_OK, JSON_WRITER_MAX_DEPTH, JsonWriter } from "nish/json"
|
|
153
154
|
import {
|
|
154
155
|
JSON_TOK_ARRAY_BEGIN,
|
|
@@ -235,6 +236,9 @@ const JSON_VALUE_DIGIT_0: i32 = 48
|
|
|
235
236
|
const JSON_VALUE_NO_WORD: i32 = 0
|
|
236
237
|
const JSON_VALUE_TRUE_WORD: i32 = 1
|
|
237
238
|
const JSON_VALUE_MINUS: i32 = 45
|
|
239
|
+
/** A line feed and a carriage return, which `storeSpan` refuses. */
|
|
240
|
+
const JSON_VALUE_LF: i32 = 10
|
|
241
|
+
const JSON_VALUE_CR: i32 = 13
|
|
238
242
|
|
|
239
243
|
/** A `JsonDocument`'s source ranges, made on first use; see `JsonDocument.ranges`. */
|
|
240
244
|
class JsonValueRanges {
|
|
@@ -1463,6 +1467,18 @@ export class JsonDocument {
|
|
|
1463
1467
|
if (start < 0 || end < start) {
|
|
1464
1468
|
return JSON_VALUE_ERR_RANGE
|
|
1465
1469
|
}
|
|
1470
|
+
this.rangeAt(x, start, end)
|
|
1471
|
+
return JSON_VALUE_OK
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
/**
|
|
1475
|
+
* `setRange` for index `x`, a value of this document, with
|
|
1476
|
+
* `0 <= start <= end` checked by the caller.
|
|
1477
|
+
*
|
|
1478
|
+
* @internal Read by std/yaml-json.ts, which holds the index of each value
|
|
1479
|
+
* it makes.
|
|
1480
|
+
*/
|
|
1481
|
+
rangeAt(x: i32, start: i32, end: i32): void {
|
|
1466
1482
|
this.ownRanges()
|
|
1467
1483
|
const r: JsonValueRanges = this.rangeArrays()
|
|
1468
1484
|
// Values between the last current one and `x` have no range yet.
|
|
@@ -1475,7 +1491,6 @@ export class JsonDocument {
|
|
|
1475
1491
|
if (x >= this.rangeTop) {
|
|
1476
1492
|
this.rangeTop = x + 1
|
|
1477
1493
|
}
|
|
1478
|
-
return JSON_VALUE_OK
|
|
1479
1494
|
}
|
|
1480
1495
|
|
|
1481
1496
|
/**
|
|
@@ -1497,6 +1512,18 @@ export class JsonDocument {
|
|
|
1497
1512
|
if (start < 0 || end < start) {
|
|
1498
1513
|
return JSON_VALUE_ERR_RANGE
|
|
1499
1514
|
}
|
|
1515
|
+
this.keyRangeAt(s, start, end)
|
|
1516
|
+
return JSON_VALUE_OK
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
/**
|
|
1520
|
+
* `setKeyRange` for slot `s`, a member's as `append` answers it, with
|
|
1521
|
+
* `0 <= start <= end` checked by the caller.
|
|
1522
|
+
*
|
|
1523
|
+
* @internal Read by std/yaml-json.ts, which holds the slot of each member
|
|
1524
|
+
* it adds.
|
|
1525
|
+
*/
|
|
1526
|
+
keyRangeAt(s: i32, start: i32, end: i32): void {
|
|
1500
1527
|
this.ownRanges()
|
|
1501
1528
|
const r: JsonValueRanges = this.rangeArrays()
|
|
1502
1529
|
for (let t: i32 = this.slotRangeTop; t < s; t += 1) {
|
|
@@ -1508,7 +1535,6 @@ export class JsonDocument {
|
|
|
1508
1535
|
if (s >= this.slotRangeTop) {
|
|
1509
1536
|
this.slotRangeTop = s + 1
|
|
1510
1537
|
}
|
|
1511
|
-
return JSON_VALUE_OK
|
|
1512
1538
|
}
|
|
1513
1539
|
|
|
1514
1540
|
/** The slot of member `i` of the object at index `x`, or `-1` when `x` is not an object or has no member `i`. */
|
|
@@ -1724,11 +1750,12 @@ export class JsonDocument {
|
|
|
1724
1750
|
* first. A container whose slots are full moves them to the end of the slot
|
|
1725
1751
|
* arrays with twice the room, unless they are at the end already, where they
|
|
1726
1752
|
* grow in place; the slots it leaves are not reused until `clear`.
|
|
1753
|
+
* Answers the new member's slot.
|
|
1727
1754
|
*
|
|
1728
1755
|
* @internal Read by std/yaml-json.ts, which builds a document from YAML
|
|
1729
1756
|
* through it.
|
|
1730
1757
|
*/
|
|
1731
|
-
append(c: i32, v: i32, keyAt: i32, keyLength: i32):
|
|
1758
|
+
append(c: i32, v: i32, keyAt: i32, keyLength: i32): i32 {
|
|
1732
1759
|
let start: i32 = jsonValueGet(this.first, c)
|
|
1733
1760
|
const n: i32 = jsonValueGet(this.count, c)
|
|
1734
1761
|
const capacity: i32 = jsonValueGet(this.room, c)
|
|
@@ -1749,6 +1776,120 @@ export class JsonDocument {
|
|
|
1749
1776
|
jsonValuePut(this.slotKeyLength, start + n, keyLength)
|
|
1750
1777
|
jsonValuePut(this.count, c, n + 1)
|
|
1751
1778
|
jsonValuePut(this.parent, v, c)
|
|
1779
|
+
return start + n
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
/**
|
|
1783
|
+
* Gives the empty container at index `c`, made last, room for `n`
|
|
1784
|
+
* 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.
|
|
1786
|
+
*
|
|
1787
|
+
* @internal Read by std/yaml-json.ts, which knows each container's size
|
|
1788
|
+
* before it builds the members.
|
|
1789
|
+
*/
|
|
1790
|
+
reserve(c: i32, n: i32): void {
|
|
1791
|
+
if (n <= 0 || jsonValueGet(this.first, c) !== this.slotTop || jsonValueGet(this.room, c) !== 0) {
|
|
1792
|
+
return
|
|
1793
|
+
}
|
|
1794
|
+
this.slotTop += n
|
|
1795
|
+
this.slotValue = jsonValueRoomI32(this.slotValue, this.slotTop)
|
|
1796
|
+
this.slotKeyAt = jsonValueRoomI32(this.slotKeyAt, this.slotTop)
|
|
1797
|
+
this.slotKeyLength = jsonValueRoomI32(this.slotKeyLength, this.slotTop)
|
|
1798
|
+
jsonValuePut(this.room, c, n)
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
/**
|
|
1802
|
+
* Appends the bytes of `text[from, to)` to the end of `bytes`, unless a
|
|
1803
|
+
* line break or the byte `stop` comes first: then nothing is kept.
|
|
1804
|
+
*
|
|
1805
|
+
* @param text - the text the bytes are copied from.
|
|
1806
|
+
* @param from - the offset of the first byte.
|
|
1807
|
+
* @param to - one past the offset of the last byte.
|
|
1808
|
+
* @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.
|
|
1810
|
+
*
|
|
1811
|
+
* @internal Read by std/yaml-json.ts, which stores a scalar with nothing
|
|
1812
|
+
* to decode straight from its source.
|
|
1813
|
+
*/
|
|
1814
|
+
storeSpan(text: string, from: i32, to: i32, stop: i32): i32 {
|
|
1815
|
+
const at: i32 = this.byteTop
|
|
1816
|
+
const n: i32 = to - from
|
|
1817
|
+
if (from < 0 || n < 0 || to > toI32(text.length) || at < 0) {
|
|
1818
|
+
return -1
|
|
1819
|
+
}
|
|
1820
|
+
const end: i32 = at + n
|
|
1821
|
+
const buf: u8[] = jsonValueRoom(this.bytes, end)
|
|
1822
|
+
this.bytes = buf
|
|
1823
|
+
if (end > toI32(buf.length)) {
|
|
1824
|
+
return -1
|
|
1825
|
+
}
|
|
1826
|
+
let k: i32 = at
|
|
1827
|
+
for (let j: i32 = from; j < to; j += 1) {
|
|
1828
|
+
const code: i32 = toI32(text.charCodeAt(j))
|
|
1829
|
+
// Both breaks are below 14, so one compare passes almost every byte.
|
|
1830
|
+
if (code === stop || (code <= JSON_VALUE_CR && (code === JSON_VALUE_LF || code === JSON_VALUE_CR))) {
|
|
1831
|
+
return -1
|
|
1832
|
+
}
|
|
1833
|
+
// `k` runs `at` to `end` as `j` runs `from` to `to`: in `buf`.
|
|
1834
|
+
uncheckedSet(buf, k, toU8(code))
|
|
1835
|
+
k += 1
|
|
1836
|
+
}
|
|
1837
|
+
this.byteTop = end
|
|
1838
|
+
return n
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
/**
|
|
1842
|
+
* Appends the bytes of `text[from, to)` to the end of `bytes`, as
|
|
1843
|
+
* `storeSpan` does with nothing to refuse: for a caller that knows the span
|
|
1844
|
+
* holds no line break.
|
|
1845
|
+
*
|
|
1846
|
+
* @param text - the text the bytes are copied from.
|
|
1847
|
+
* @param from - the offset of the first byte.
|
|
1848
|
+
* @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 `text`.
|
|
1850
|
+
*
|
|
1851
|
+
* @internal Read by std/yaml-json.ts, for a scalar the parser found to be
|
|
1852
|
+
* its own body.
|
|
1853
|
+
*/
|
|
1854
|
+
copySpan(text: string, from: i32, to: i32): i32 {
|
|
1855
|
+
const at: i32 = this.byteTop
|
|
1856
|
+
const n: i32 = to - from
|
|
1857
|
+
if (from < 0 || n < 0 || to > toI32(text.length) || at < 0) {
|
|
1858
|
+
return -1
|
|
1859
|
+
}
|
|
1860
|
+
const buf: u8[] = jsonValueRoom(this.bytes, at + n)
|
|
1861
|
+
this.bytes = buf
|
|
1862
|
+
if (at + n > toI32(buf.length)) {
|
|
1863
|
+
return -1
|
|
1864
|
+
}
|
|
1865
|
+
const end: i32 = at + n
|
|
1866
|
+
let k: i32 = at
|
|
1867
|
+
for (let j: i32 = from; j < to; j += 1) {
|
|
1868
|
+
// `k` runs `at` to `end` as `j` runs `from` to `to`: in `buf`.
|
|
1869
|
+
uncheckedSet(buf, k, toU8(text.charCodeAt(j)))
|
|
1870
|
+
k += 1
|
|
1871
|
+
}
|
|
1872
|
+
this.byteTop = end
|
|
1873
|
+
return n
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
/**
|
|
1877
|
+
* A new string of the `n` bytes stored from `at`, as a handle.
|
|
1878
|
+
*
|
|
1879
|
+
* @internal Read by std/yaml-json.ts, after `storeSpan`.
|
|
1880
|
+
*/
|
|
1881
|
+
newStoredString(at: i32, n: i32): JsonValue {
|
|
1882
|
+
return this.handle(this.newNode(JSON_VALUE_STRING, at, n))
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
/**
|
|
1886
|
+
* A new number of the `n` bytes stored from `at`, as a handle; the caller
|
|
1887
|
+
* has checked they are one RFC 8259 number, which `newNumber` would check.
|
|
1888
|
+
*
|
|
1889
|
+
* @internal Read by std/yaml-json.ts, after `storeSpan`.
|
|
1890
|
+
*/
|
|
1891
|
+
newStoredNumber(at: i32, n: i32): JsonValue {
|
|
1892
|
+
return this.handle(this.newNode(JSON_VALUE_NUMBER, at, n))
|
|
1752
1893
|
}
|
|
1753
1894
|
|
|
1754
1895
|
/** A new value of `kind` holding `s`'s bytes, as a handle. */
|