@amritk/nish-x86_64-linux 0.35.0 → 0.36.0

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 (119) hide show
  1. package/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
package/std/json-typed.ts CHANGED
@@ -29,6 +29,82 @@
29
29
  * of these, or a string, class, array or map `| null`; anything else is
30
30
  * refused at the call, naming the field (docs/LANGUAGE.md, "Typed JSON").
31
31
  *
32
+ * **Call shapes.** `jsonDecode(d, out)` reads the one JSON object `d` holds
33
+ * into `out`, a declared class, and answers `d.failed`: `JSON_DECODE_OK` (0)
34
+ * or the first error's code. `jsonEncode(w, value)` writes `value` to the
35
+ * writer `w` as one JSON object and answers nothing; the writer's own sticky
36
+ * code reports a failure. The decoder is a `JsonDecoder`, a `JsonTokenizer`
37
+ * over the input and a sticky error, and the encoder is the `JsonWriter` the
38
+ * call is given, so the encoding is written in the writer's modes: a writer
39
+ * set with `setIndent(2)` encodes what `JSON.stringify(v, null, 2)` writes,
40
+ * and a `setStrict(true)` writer fails a NaN or infinite `f64` field with
41
+ * `JSON_WRITE_NON_FINITE`.
42
+ *
43
+ * **What a field may be**, and how each maps:
44
+ *
45
+ * Field type JSON Decoded with Encoded with
46
+ * i32, u32, i64 an integer, no fraction or d.i32(), w.i32, w.u32,
47
+ * exponent, in the type's d.u32(), w.i64
48
+ * range d.i64()
49
+ * u64 digits alone, no sign, d.u64() jsonEncodeU64
50
+ * fraction or exponent, up to
51
+ * 2^64 - 1
52
+ * numeric enum E the number of one of E's d.i32(), then w.i32
53
+ * members a switch
54
+ * f64 (number in f64 mode) any number; past the double d.f64() w.f64
55
+ * range an infinity, which the
56
+ * writer writes as null
57
+ * boolean true, false d.boolean() w.boolean
58
+ * string, string | null a string, and null for the d.string(cur) w.string,
59
+ * second jsonEncodeStringOrNull
60
+ * class C, C | null an object, and null for the C's own C's own
61
+ * second decoding, into encoding
62
+ * the object the
63
+ * field holds
64
+ * T[], T[] | null an array, and null for the d.i32Array(cur) jsonEncodeI32Array
65
+ * second and its kin, or and its kin, or
66
+ * C's or E's per C's or E's per
67
+ * element element
68
+ * Map<string, V>, or | null an object whose keys are the keys in order, entries in the
69
+ * map's, each value a V each value set map's order
70
+ * JsonNumber any number, kept as the text d.number(n) jsonEncodeNumber
71
+ * it is written in
72
+ *
73
+ * A `Map`'s `V` is any type in this list, `| null` or not, a `Map` included;
74
+ * `JsonNumber` may stand alone, be `| null`, be an array's element or be a
75
+ * `Map`'s value. The code written for a nested class, an array's element
76
+ * class, a `| null` field's class and a `Map`'s values is the same code, so it
77
+ * recurses as the data does. Anything else is refused at the call's argument,
78
+ * naming the class and the field (NL2485 for a field type, such as an `f32`,
79
+ * an `i32[][]`, a `Level[][]`, a `Set`, an array of maps, a `Map` not keyed by
80
+ * `string` (an enum key included) or a `Map` whose values cannot be written,
81
+ * and a generic class of the program's own). An enum whose members are strings
82
+ * or a mix is not one the language declares (NL1043), and a `const enum` is
83
+ * refused where it is declared (NL2285). A value that is not a declared class
84
+ * (an interface, a generic class or any other type) is NL2486; decoding into an
85
+ * interface is left for a later version. A call inside a generic function is
86
+ * checked for each class an instantiation binds.
87
+ *
88
+ * **Reading the class.** A class the decoder has to make itself, an array's
89
+ * element past the end, a `| null` field that holds `null` or a map's value,
90
+ * is made with `new C()`, so its constructor takes no parameters (NL2488).
91
+ * Decoding assigns every field, so a `readonly` one is refused (NL2487). A
92
+ * decode stores what it makes into the object it fills, so a call inside a
93
+ * `using a = arena()` block is refused (NL2424); an encoding only reads the
94
+ * object and keeps nothing, so it may run inside one. Two fields holding one
95
+ * object are decoded into it twice, the second winning.
96
+ *
97
+ * **Finding the field.** The code written for a class of three or more fields
98
+ * switches on the key's length in bytes, so a key is compared only with the
99
+ * names of that length. Where three or more names share a length, it switches
100
+ * once more, on the key's byte at the one position where those names differ
101
+ * most, and calls `keyIs` for each name with that byte there, in declaration
102
+ * order; a byte none of them has is an unknown key at once. One or two names
103
+ * are compared in turn with `keyIs`. A key with an escape has no byte to switch
104
+ * on there and is compared with every name of its length, so `"id"` is
105
+ * the field `id`; a field name that is not ASCII is dispatched by its UTF-8
106
+ * bytes like any other.
107
+ *
32
108
  * **The decoding rules.** A JSON object's keys are the class's field names,
33
109
  * byte for byte, or the keys its options give them, found by the key's length
34
110
  * and then, among three or more names of that length, by one of its bytes:
@@ -41,18 +117,93 @@
41
117
  * The compiler reads that literal; it is not a field. Every field is required
42
118
  * but an optional one, whose missing key sets it to its initializer, a key
43
119
  * may appear once, and a key that names no field is an error unless
44
- * `ignoreUnknown` is set, when its value is stepped over. A number must fit its field: an integer field takes
120
+ * `ignoreUnknown` is set, when its value is stepped over, checked but not
121
+ * kept. A number must fit its field: an integer field takes
45
122
  * an integer written without a fraction or an exponent and in the field's
46
123
  * range, and `f64` takes any number. `null` is accepted only by a field whose
47
124
  * type is `| null`. Fields are written to `out` as they are read, in the
48
125
  * input's order: a nested object is decoded into the object the field already
49
- * holds, an array's elements into the elements it already has (so a reused
50
- * object reuses its arrays and objects, and `Arena.used()` stays flat), and a
51
- * string field keeps the string it holds when the input's is equal.
126
+ * holds, an array's elements into the elements it already has, with new ones
127
+ * made past its end and the rest dropped (so a reused
128
+ * object reuses its arrays and objects, and `Arena.used()` stays flat: it is
129
+ * flat over 1,000 decodes of a three-level object), and a
130
+ * string field keeps the string it holds when the input's is equal. A string
131
+ * that changes is a new string, made by one `String.fromBytes`, straight from
132
+ * the input when it has no escape and from its unescaped bytes when it has one.
52
133
  * A class the decoder has to make — an array element past the array's end, a
53
134
  * `| null` field that holds `null`, or a map's value — is made with `new C()`.
54
- * A map is emptied and filled in the input's order, and every map of one
55
- * decoding takes at most `maxMapKeys` keys between them.
135
+ * A map is emptied once the object opens, then each key is read as a new
136
+ * string and its value as a `V` made fresh for the key (a zero, `""`, the
137
+ * enum's first member, `null`, or a new object, array or map) and `set`, so
138
+ * the entries are the input's, in the input's order. A key the object gave
139
+ * already is `JSON_DECODE_DUPLICATE` before its value is read, however either
140
+ * is escaped. A value whose read fails is not `set`, so a map never holds a
141
+ * value the input did not give. Unlike the rest of a decode, a map's entries
142
+ * are made anew each time, so decoding into a map allocates its keys and
143
+ * values. Every map of one decoding takes at most `maxMapKeys` keys between
144
+ * them (4,096 unless set), the keys of a map inside another map included; the
145
+ * key past it is `JSON_DECODE_MAP_LIMIT` before it is read, and `reset`
146
+ * starts the count again. At `0` or below no map may take a key, and `{}` is
147
+ * the one map value accepted. Encoding writes the entries in the map's own
148
+ * order, which is insertion order. That differs from a JavaScript object in
149
+ * one case: a key that reads as an array index (`"1"`) keeps its place, where
150
+ * `JSON.parse` would move it first.
151
+ *
152
+ * **A class's options** are one `static readonly json` object literal written
153
+ * in the class, which the compiler reads when it writes the class's code. It
154
+ * is not a field: it has no slot, the class's layout is what it would be
155
+ * without it, and the program cannot read it. It is the one `static` member a
156
+ * class may declare. A class without it is decoded exactly as before. Its
157
+ * options are checked where the class is declared, whether or not any call
158
+ * decodes the class. There are three, each a literal and none a default
159
+ * other than being absent:
160
+ *
161
+ * - `rename`, an object mapping a field to its key on the wire, a string
162
+ * literal of any bytes. `{"user_id":7}` is decoded into `userId` and
163
+ * `userId` is encoded as `"user_id"`; the field's own name is then a key
164
+ * no field has. The key is dispatched like any other, escapes and
165
+ * non-ASCII included. Without it a field's key is its name.
166
+ * - `optional`, an array of field names whose key may be missing. A missing
167
+ * one sets the field to its initializer, on a fresh object and on one
168
+ * decoded into again, so a reused object keeps nothing an earlier input
169
+ * gave it, and allocates nothing to do it. An optional field needs an
170
+ * initializer. A key that is given is read like any other: once, and of
171
+ * its field's type. Without it every field is required and a missing key is
172
+ * `JSON_DECODE_MISSING`.
173
+ * - `presence`, described below. Without it nothing is recorded.
174
+ *
175
+ * Each misuse has its own diagnostic:
176
+ *
177
+ * NL2492 `static json` without `readonly` but with a type, `static
178
+ * readonly json` with a type annotation, or on a generic class
179
+ * NL2493 options, `rename`, a key or an `optional` entry not written as
180
+ * the literal it must be (a module constant, an array for the
181
+ * object, a string for `optional`'s array); `presence` or one of
182
+ * its entries not an object literal of string literals
183
+ * NL2494 an option other than `rename`, `optional` and `presence`
184
+ * NL2495 a name that is not one of the class's fields; `presence` naming
185
+ * a field or a presence field the class does not declare
186
+ * NL2496 a rename to a value that is not a string
187
+ * NL2497 two fields with one key (a rename to another field's own name
188
+ * included), or a field renamed or made optional twice
189
+ * NL2498 an optional field with no initializer
190
+ * NL2499 a rename to a key that is not valid UTF-8, a lone surrogate such
191
+ * as `"\ud800"` included, which no input could give
192
+ * NL2592 a presence field that is not a `boolean`, is `readonly`, is the
193
+ * field it records, records two fields, is recorded by a presence
194
+ * field of its own or is renamed or made optional; a field
195
+ * recorded twice
196
+ *
197
+ * A spread or a computed key in the literal is refused as it is anywhere
198
+ * (NL1061, NL1041), a subclass by `extends`, and a second `static readonly
199
+ * json` is a duplicate member. The options are not a value: `User.json` names
200
+ * a class where a value is due, and is refused as any class name there is.
201
+ * Only `static readonly json` may leave out its type: written `static json =
202
+ * { ... }`, or as an instance field, it is an ordinary field to the parser and
203
+ * needs one, and an instance field called `json` with a type is an ordinary
204
+ * field, read and written under its own name. A field or a method called
205
+ * `json` may come before or after the options. Under Node the literal is a
206
+ * static property of the class that nothing reads.
56
207
  *
57
208
  * **Which keys were given.** An optional field's missing key and a key given
58
209
  * the field's initializer leave the same value; the `presence` option tells
@@ -93,18 +244,68 @@
93
244
  * or the tokenizer's own `JSON_TOK_ERR_*` code when the text is not JSON or
94
245
  * crosses one of its `JsonLimits` — and `errorAt` to the byte offset of the
95
246
  * token it was found at. Every later call does nothing, and `jsonDecode`
96
- * answers `failed`. Nothing an input holds panics. After an error, `out` holds
97
- * a value of its type in every field, some of them decoded and some as they
98
- * were.
247
+ * answers `failed`; `reset` starts over. Nothing an input holds panics. After
248
+ * an error, `out` holds a value of its type in every field, some of them
249
+ * decoded and some as they were: an enum field or element never holds a number
250
+ * that is no member's, because such a number is not stored. The codes:
251
+ *
252
+ * Code Value Condition errorAt
253
+ * JSON_DECODE_OK 0 nothing has failed
254
+ * JSON_DECODE_TYPE -20 a value of the wrong JSON type the value
255
+ * JSON_DECODE_RANGE -21 a number its field cannot hold the number
256
+ * JSON_DECODE_UNKNOWN -22 a key that names no field the key
257
+ * JSON_DECODE_DUPLICATE -23 a key given twice the second
258
+ * JSON_DECODE_MISSING -24 a field the object lacks the object's `}`
259
+ * JSON_DECODE_MAP_LIMIT -25 more than maxMapKeys map keys the first key past it
260
+ * JSON_TOK_ERR_* -1 to -13 text that is not JSON, or past the tokenizer's
261
+ * a JsonLimits limit
262
+ *
263
+ * **Limits.** The recursion is the data's, so the tokenizer's depth limit
264
+ * bounds it: the caller's `JsonLimits.maxDepth`, 128 by default. Each level is
265
+ * one native frame, so a limit raised into the hundreds of thousands can run
266
+ * off the stack, and one kept to a few thousand cannot. The writer refuses a
267
+ * 65th open container (`JSON_WRITER_MAX_DEPTH`), so a value decoded from
268
+ * deeper than 64 levels encodes as `JSON_WRITE_TOO_DEEP`, as does an object
269
+ * that holds itself; the encoding stops at the first failure. A full buffer
270
+ * is the writer's `JSON_WRITE_FULL`. A duplicate of a key `ignoreUnknown`
271
+ * steps over is not detected, because its value is never read. A map is
272
+ * bounded by `maxMapKeys`, and a number's length by `JsonLimits.maxNumberBytes`.
273
+ *
274
+ * **An enum is its number.** A numeric enum is read with `d.i32()`, so a
275
+ * number that is not an `i32` integer is that field's `JSON_DECODE_RANGE`, and
276
+ * an integer that is not the value of one of its members is
277
+ * `JSON_DECODE_TYPE`; a member's name, `"High"`, is a string and a type error
278
+ * too. Two members of one value are one number. It is written as `w.i32` of
279
+ * its value, as `JSON.stringify` writes a TypeScript numeric enum.
280
+ *
281
+ * **A `u64` is exact to 2^64 - 1.** It is read through the tokenizer's
282
+ * `numberU64`, so `18446744073709551615` decodes and `18446744073709551616`,
283
+ * `-1`, `-0` and `1.5` are `JSON_DECODE_RANGE`. It is written as its digits,
284
+ * which `JSON.stringify` cannot do for a number past 2^53.
285
+ *
286
+ * **A `JsonNumber` is a number's text.** A field of it decodes and encodes any
287
+ * number byte for byte: an integer wider than a `u64`, a fraction with more
288
+ * digits than an `f64` holds, `1E+400` and `-0` alike. It is read as a number
289
+ * whole, not as an object, so anything but a number is `JSON_DECODE_TYPE`, and
290
+ * its length is bounded by `JsonLimits.maxNumberBytes` like any number's.
291
+ * Decoding into one reuses its bytes, so `Arena.used()` stays flat over
292
+ * decodes into one object.
99
293
  *
100
294
  * **Encoding** writes the fields in declaration order, each through the
101
295
  * writer method of its type, so the text is what `JSON.stringify` writes for
102
- * the same object; the writer's own sticky code reports a full buffer.
296
+ * the same object; the writer's own sticky code reports a full buffer. A
297
+ * presence field is left out. The keys come back in the order they were
298
+ * written when the object is decoded from text whose keys are in that order.
103
299
  *
104
300
  * **Under Node** the types are erased, so there is no layout to generate the
105
301
  * code from: `jsonDecode` and `jsonEncode` end the program with a panic
106
302
  * naming this, rather than guessing a type from whatever value a field holds
107
- * (docs/LANGUAGE.md, "Typed JSON", class C). Everything else here runs there.
303
+ * (docs/LANGUAGE.md, "Typed JSON", class C). Everything else here runs there,
304
+ * so a program may run the decoder's readers and the writer under Node and
305
+ * get the lines the native program gets; the translation of a class is a
306
+ * function per class of the same calls into `JsonDecoder` and `JsonWriter`. A
307
+ * program that imports `std/json-typed.ts` by path rather than as
308
+ * `nish/json-typed` is compiled as written and gets those bodies natively too.
108
309
  *
109
310
  * Every name here is `json`-prefixed, as `std/json.ts` explains, and every
110
311
  * code is an `i32`, so the module is the same under `--number-mode f64`.
@@ -127,22 +328,35 @@ import {
127
328
 
128
329
  /** Nothing has failed. */
129
330
  export const JSON_DECODE_OK: i32 = 0
130
- /** A value of the wrong JSON type for its field: a string for an `i32`, an array for a class, `null` for a field that is not `| null`. */
331
+ /**
332
+ * A value of the wrong JSON type for its field: a string for an `i32`, an
333
+ * array for a class, `null` for a field that is not `| null`, a top-level
334
+ * value that is not an object, or an integer that is no member's value for
335
+ * an enum. `errorAt` is the value.
336
+ */
131
337
  export const JSON_DECODE_TYPE: i32 = -20
132
- /** A number its field cannot hold: a fraction or an exponent for an integer field, or a value outside its range. */
338
+ /**
339
+ * A number its field cannot hold: a fraction or an exponent for an integer
340
+ * field, a value outside its range, or a negative number, `-0` included, for
341
+ * a `u64`. `errorAt` is the number.
342
+ */
133
343
  export const JSON_DECODE_RANGE: i32 = -21
134
- /** A key that names no field of the class, while `ignoreUnknown` is off. */
344
+ /** A key that names no field of the class, while `ignoreUnknown` is off; `errorAt` is the key. */
135
345
  export const JSON_DECODE_UNKNOWN: i32 = -22
136
- /** A key that names a field the object already gave. */
346
+ /**
347
+ * A key the object already gave, a field's or a `Map`'s, however either is
348
+ * escaped; `errorAt` is the second. A key `ignoreUnknown` steps over is not
349
+ * checked, because its value is never read.
350
+ */
137
351
  export const JSON_DECODE_DUPLICATE: i32 = -23
138
- /** An object that ended without one of the class's fields; `errorAt` is its closing brace. */
352
+ /** An object that ended without a required field of the class; `errorAt` is its closing brace. */
139
353
  export const JSON_DECODE_MISSING: i32 = -24
140
354
  /**
141
355
  * More keys, over every `Map` field one decoding fills, than `maxMapKeys`;
142
356
  * `errorAt` is the first key past the limit. The global `Map` hashes a
143
- * string with no seed, so a body can choose keys that all collide, and the
144
- * limit bounds what the maps' probes cost over the whole decoding
145
- * (docs/security/json.md, JSON-13).
357
+ * string under a per-process random key, so a body cannot choose keys that
358
+ * collide, and this limit bounds what all the maps of one decoding hold
359
+ * together (docs/security/json.md, JSON-13).
146
360
  */
147
361
  export const JSON_DECODE_MAP_LIMIT: i32 = -25
148
362
 
@@ -164,18 +378,27 @@ const JSON_TYPED_ERASED: string =
164
378
  *
165
379
  * A new one holds `0`. Decoding into one reuses its bytes, so an object
166
380
  * decoded again with numbers no longer than before allocates nothing for
167
- * them.
381
+ * them. Its length is bounded by `JsonLimits.maxNumberBytes` like any
382
+ * number's, and anything but a number read into it is `JSON_DECODE_TYPE`.
383
+ * `setText(t)` sets it when `t` is one JSON number and answers whether it was.
384
+ * Its methods run under Node.
168
385
  */
169
386
  export class JsonNumber {
170
387
  /** The text is `bytes[0, count)`, one JSON number; the bytes past it are room. */
171
388
  bytes: u8[]
389
+ /** How many of `bytes` are the number's text, `1` for the `0` a new one holds. */
172
390
  count: i32 = 1
173
391
 
392
+ /** A number that holds `0`. */
174
393
  constructor() {
175
394
  this.bytes = [48]
176
395
  }
177
396
 
178
- /** The number's text, exactly as it was written or set. */
397
+ /**
398
+ * The number's text.
399
+ *
400
+ * @returns A new string of the text, exactly as it was decoded or set.
401
+ */
179
402
  text(): string {
180
403
  return String.fromBytes(this.bytes, 0, this.count)
181
404
  }
@@ -183,8 +406,10 @@ export class JsonNumber {
183
406
  /**
184
407
  * A tokenizer over the text, read to its one token. It may open no
185
408
  * container and take a number of any length, so it allocates no stack.
409
+ *
410
+ * @returns A new tokenizer whose current token is the number.
186
411
  */
187
- reader(): JsonTokenizer {
412
+ private reader(): JsonTokenizer {
188
413
  const limits: JsonLimits = new JsonLimits()
189
414
  limits.maxDepth = 0
190
415
  limits.maxNumberBytes = this.count
@@ -193,34 +418,57 @@ export class JsonNumber {
193
418
  return tok
194
419
  }
195
420
 
196
- /** Whether the number is written without a fraction or an exponent and its value fits an `i64`. */
421
+ /**
422
+ * Whether the number is an `i64`.
423
+ *
424
+ * @returns `true` when it is written without a fraction or an exponent and its value fits an `i64`.
425
+ */
197
426
  isI64(): boolean {
198
427
  return this.reader().isInteger()
199
428
  }
200
429
 
201
- /** The value as an `i64` when `isI64()` holds, and `0` otherwise. */
430
+ /**
431
+ * The number as an `i64`.
432
+ *
433
+ * @returns The value when `isI64()` holds, and `0` otherwise.
434
+ */
202
435
  i64(): i64 {
203
436
  return this.reader().numberI64()
204
437
  }
205
438
 
206
- /** Whether the number is written in digits alone, with no sign, fraction or exponent, and fits a `u64`. */
439
+ /**
440
+ * Whether the number is a `u64`.
441
+ *
442
+ * @returns `true` when it is written in digits alone, with no sign, fraction or exponent, and fits a `u64`.
443
+ */
207
444
  isU64(): boolean {
208
445
  return this.reader().isU64()
209
446
  }
210
447
 
211
- /** The value as a `u64` when `isU64()` holds, and `0` otherwise. */
448
+ /**
449
+ * The number as a `u64`.
450
+ *
451
+ * @returns The value when `isU64()` holds, and `0` otherwise.
452
+ */
212
453
  u64(): u64 {
213
454
  return this.reader().numberU64()
214
455
  }
215
456
 
216
- /** The nearest `f64`, ties to even, as `JSON.parse` reads it. */
457
+ /**
458
+ * The number as an `f64`.
459
+ *
460
+ * @returns The nearest `f64`, ties to even, as `JSON.parse` reads it; an infinity past the double range.
461
+ */
217
462
  f64(): f64 {
218
463
  return this.reader().numberF64()
219
464
  }
220
465
 
221
466
  /**
222
- * Sets the text to `text` when it is one JSON number and nothing else, and
223
- * answers whether it was; any other text leaves the number as it was.
467
+ * Sets the text to `text` when it is one JSON number and nothing else; any
468
+ * other text leaves the number as it was.
469
+ *
470
+ * @param text The new text: one JSON number, with no blank around it.
471
+ * @returns `true` when `text` was one JSON number and is now the text.
224
472
  */
225
473
  setText(text: string): boolean {
226
474
  const n: i32 = toI32(text.length)
@@ -253,7 +501,8 @@ export class JsonNumber {
253
501
  * strings it decodes, the arrays it grows and the objects it makes.
254
502
  */
255
503
  export class JsonDecoder {
256
- tok: JsonTokenizer
504
+ /** The tokenizer over the input, under the caller's `JsonLimits`. */
505
+ private tok: JsonTokenizer
257
506
  /** `JSON_DECODE_OK`, or the first `JSON_DECODE_*` or `JSON_TOK_ERR_*` code. */
258
507
  failed: i32 = 0
259
508
  /** The byte offset in the input of the token the error was found at; `-1` until one. */
@@ -273,8 +522,17 @@ export class JsonDecoder {
273
522
  * the first token of an array element, read to see whether the array had
274
523
  * ended, or a value `takeNull` found was not `null`.
275
524
  */
276
- held: boolean = false
525
+ private held: boolean = false
277
526
 
527
+ /**
528
+ * A decoder over `src[off, off + len)`, with no error and no map key taken.
529
+ *
530
+ * @param src The bytes that hold the input.
531
+ * @param off Where the input starts in `src`.
532
+ * @param len How many bytes of `src` the input is.
533
+ * @param limits The tokenizer's limits; its `maxDepth`, 128 by default,
534
+ * bounds the recursion, one native frame a level.
535
+ */
278
536
  constructor(src: u8[], off: i32, len: i32, limits: JsonLimits) {
279
537
  this.tok = new JsonTokenizer(src, off, len, limits)
280
538
  }
@@ -282,6 +540,10 @@ export class JsonDecoder {
282
540
  /**
283
541
  * Starts over on `src[off, off + len)`, keeping the limits, `ignoreUnknown`
284
542
  * and `maxMapKeys`, and clearing any error and the count of map keys taken.
543
+ *
544
+ * @param src The bytes that hold the next input.
545
+ * @param off Where it starts in `src`.
546
+ * @param len How many bytes of `src` it is.
285
547
  */
286
548
  reset(src: u8[], off: i32, len: i32): void {
287
549
  this.tok.reset(src, off, len)
@@ -291,21 +553,37 @@ export class JsonDecoder {
291
553
  this.mapKeys = 0
292
554
  }
293
555
 
294
- /** Records the first error. */
295
- fail(code: i32, at: i32): void {
556
+ /**
557
+ * Records the first error; once one is recorded, this does nothing.
558
+ *
559
+ * @param code The `JSON_DECODE_*` or `JSON_TOK_ERR_*` code.
560
+ * @param at The byte offset of the token it was found at.
561
+ */
562
+ private fail(code: i32, at: i32): void {
296
563
  if (this.failed === JSON_DECODE_OK) {
297
564
  this.failed = code
298
565
  this.errorAt = at
299
566
  }
300
567
  }
301
568
 
302
- /** `JSON_DECODE_TYPE` at the current token, unless the read that found it failed already. */
569
+ /**
570
+ * `JSON_DECODE_TYPE` at the current token, unless the read that found it
571
+ * failed already.
572
+ *
573
+ * @internal Called by name by the decoder the compiler writes for
574
+ * `jsonDecode`, src/emit-json.ts.
575
+ */
303
576
  wrongType(): void {
304
577
  this.fail(JSON_DECODE_TYPE, this.tok.tokStart)
305
578
  }
306
579
 
307
- /** The kind of the next value's first token, or a negative code; the held token first, when there is one. */
308
- take(): i32 {
580
+ /**
581
+ * Takes the next value's first token: the held token, when there is one,
582
+ * and the tokenizer's next otherwise.
583
+ *
584
+ * @returns Its `JSON_TOK_*` kind, or the negative code of the error, which it records.
585
+ */
586
+ private take(): i32 {
309
587
  if (this.failed !== JSON_DECODE_OK) {
310
588
  return this.failed
311
589
  }
@@ -320,7 +598,11 @@ export class JsonDecoder {
320
598
  return k
321
599
  }
322
600
 
323
- /** The next value opens an object; anything else is `JSON_DECODE_TYPE`. */
601
+ /**
602
+ * Takes the next value as the opening of an object.
603
+ *
604
+ * @returns `true` when it opens one; `false` with `JSON_DECODE_TYPE` for any other value, and after any error.
605
+ */
324
606
  beginObject(): boolean {
325
607
  const k: i32 = this.take()
326
608
  if (k === JSON_TOK_OBJECT_BEGIN) {
@@ -332,7 +614,11 @@ export class JsonDecoder {
332
614
  return false
333
615
  }
334
616
 
335
- /** Reads the next key of the open object, or its end; `false` at the end and after any error. */
617
+ /**
618
+ * Reads the next key of the open object, or its end.
619
+ *
620
+ * @returns `true` at a key; `false` at the object's end and after any error.
621
+ */
336
622
  nextField(): boolean {
337
623
  if (this.failed !== JSON_DECODE_OK) {
338
624
  return false
@@ -347,15 +633,25 @@ export class JsonDecoder {
347
633
  return false
348
634
  }
349
635
 
350
- /** Whether the current key is `name`. */
636
+ /**
637
+ * Whether the current key, its escapes decoded, is `name`.
638
+ *
639
+ * @param name The field's name or its key on the wire.
640
+ * @returns `true` when the key's bytes are `name`'s UTF-8.
641
+ *
642
+ * @internal Called by name by the decoder the compiler writes for
643
+ * `jsonDecode`, src/emit-json.ts.
644
+ */
351
645
  keyIs(name: string): boolean {
352
646
  return this.tok.keyEquals(name)
353
647
  }
354
648
 
355
649
  /**
356
650
  * The current key's length in bytes once decoded, or `-1` when the current
357
- * token is not a key: what the code written for a class switches on before
651
+ * token is not a key or string: what the code written for a class switches on before
358
652
  * it compares a key's bytes with a field's name.
653
+ *
654
+ * @returns The decoded length in bytes, or `-1`.
359
655
  */
360
656
  keyLength(): i32 {
361
657
  return this.tok.stringLength()
@@ -364,10 +660,13 @@ export class JsonDecoder {
364
660
  /**
365
661
  * Byte `at` of the current key when the key has no escape, so that its
366
662
  * bytes are the input's, and `-1` otherwise: for a key with an escape, for
367
- * an `at` outside it, and when the current token is not a key. A `-1`
663
+ * an `at` outside it, and when the current token is not a key or string. A `-1`
368
664
  * matches no byte of a field's name, and the code written for a class then
369
665
  * compares the key with each name of its length through `keyIs`, which
370
666
  * decodes the escapes.
667
+ *
668
+ * @param at The byte's index in the key.
669
+ * @returns The byte, `0` to `255`, or `-1`.
371
670
  */
372
671
  keyByte(at: i32): i32 {
373
672
  const tok: JsonTokenizer = this.tok
@@ -390,6 +689,8 @@ export class JsonDecoder {
390
689
  * read, so the keys of a map inside another map count before the outer
391
690
  * key's value is whole, and the total never passes the limit.
392
691
  * `JSON_DECODE_MAP_LIMIT` at the key when they may not.
692
+ *
693
+ * @returns `true` when the key is counted; `false` with `JSON_DECODE_MAP_LIMIT`.
393
694
  */
394
695
  mapRoom(): boolean {
395
696
  if (this.mapKeys < this.maxMapKeys) {
@@ -400,7 +701,11 @@ export class JsonDecoder {
400
701
  return false
401
702
  }
402
703
 
403
- /** The current key, decoded, as a new string: a `Map` field's key. `""` when the current token is not a key. */
704
+ /**
705
+ * The current key, decoded: a `Map` field's key.
706
+ *
707
+ * @returns A new string, or `""` when the current token is not a key or string.
708
+ */
404
709
  key(): string {
405
710
  return this.text()
406
711
  }
@@ -408,8 +713,9 @@ export class JsonDecoder {
408
713
  /**
409
714
  * The current key or string as a new string, made by one
410
715
  * `String.fromBytes`: straight from the input when it has no escape, and
411
- * from its unescaped bytes when it has one. `""` when the current token is
412
- * neither.
716
+ * from its unescaped bytes when it has one.
717
+ *
718
+ * @returns A new string, or `""` when the current token is neither.
413
719
  */
414
720
  text(): string {
415
721
  const tok: JsonTokenizer = this.tok
@@ -430,7 +736,14 @@ export class JsonDecoder {
430
736
  return String.fromBytes(bytes, 0, n)
431
737
  }
432
738
 
433
- /** The current key names no field: stepped over with its value, or `JSON_DECODE_UNKNOWN`. */
739
+ /**
740
+ * Handles a key that names no field: with `ignoreUnknown` set, steps over
741
+ * the key's value, checked but not kept, and otherwise records
742
+ * `JSON_DECODE_UNKNOWN` at the key.
743
+ *
744
+ * @internal Called by name by the decoder the compiler writes for
745
+ * `jsonDecode`, src/emit-json.ts.
746
+ */
434
747
  unknownField(): void {
435
748
  if (this.failed !== JSON_DECODE_OK) {
436
749
  return
@@ -445,17 +758,37 @@ export class JsonDecoder {
445
758
  }
446
759
  }
447
760
 
448
- /** The current key names a field the object already gave. */
761
+ /**
762
+ * Records `JSON_DECODE_DUPLICATE` at the current key, which the object
763
+ * already gave.
764
+ *
765
+ * @internal Called by name by the decoder the compiler writes for
766
+ * `jsonDecode`, src/emit-json.ts.
767
+ */
449
768
  duplicateField(): void {
450
769
  this.fail(JSON_DECODE_DUPLICATE, this.tok.tokStart)
451
770
  }
452
771
 
453
- /** The object just closed without one of the fields. */
772
+ /**
773
+ * Records `JSON_DECODE_MISSING` at the `}` of the object just closed,
774
+ * which left out a required field.
775
+ *
776
+ * @internal Called by name by the decoder the compiler writes for
777
+ * `jsonDecode`, src/emit-json.ts.
778
+ */
454
779
  missingField(): void {
455
780
  this.fail(JSON_DECODE_MISSING, this.tok.tokStart)
456
781
  }
457
782
 
458
- /** After the top-level value: nothing but blanks may follow. Answers `failed`. */
783
+ /**
784
+ * Ends the decoding after the top-level value: nothing but blanks may
785
+ * follow it, and anything else is `JSON_DECODE_TYPE`.
786
+ *
787
+ * @returns `failed`: `JSON_DECODE_OK`, or the first error's code.
788
+ *
789
+ * @internal Called by name by the decoder the compiler writes for
790
+ * `jsonDecode`, src/emit-json.ts.
791
+ */
459
792
  finish(): i32 {
460
793
  if (this.failed === JSON_DECODE_OK) {
461
794
  const k: i32 = this.tok.next()
@@ -468,7 +801,15 @@ export class JsonDecoder {
468
801
  return this.failed
469
802
  }
470
803
 
471
- /** Whether the next value is `null`, read if so; otherwise it is held for the read that follows. */
804
+ /**
805
+ * Takes the next value when it is `null`, and holds it for the read that
806
+ * follows when it is not.
807
+ *
808
+ * @returns `true` when it was `null`; `false` otherwise and after any error.
809
+ *
810
+ * @internal Called by name by the decoder the compiler writes for
811
+ * `jsonDecode`, src/emit-json.ts.
812
+ */
472
813
  takeNull(): boolean {
473
814
  const k: i32 = this.take()
474
815
  if (k === JSON_TOK_NULL) {
@@ -480,7 +821,11 @@ export class JsonDecoder {
480
821
  return false
481
822
  }
482
823
 
483
- /** The next value opens an array; anything else is `JSON_DECODE_TYPE`. */
824
+ /**
825
+ * Takes the next value as the opening of an array.
826
+ *
827
+ * @returns `true` when it opens one; `false` with `JSON_DECODE_TYPE` for any other value, and after any error.
828
+ */
484
829
  beginArray(): boolean {
485
830
  const k: i32 = this.take()
486
831
  if (k === JSON_TOK_ARRAY_BEGIN) {
@@ -492,7 +837,12 @@ export class JsonDecoder {
492
837
  return false
493
838
  }
494
839
 
495
- /** Whether the open array has another element, whose first token is then held; `false` at its end and after any error. */
840
+ /**
841
+ * Whether the open array has another element, whose first token is then
842
+ * held for the read that follows.
843
+ *
844
+ * @returns `true` at an element; `false` at the array's end and after any error.
845
+ */
496
846
  nextElement(): boolean {
497
847
  if (this.failed !== JSON_DECODE_OK) {
498
848
  return false
@@ -514,8 +864,15 @@ export class JsonDecoder {
514
864
  * with an error. Every `i64` it answers is the tokenizer's or made with
515
865
  * `toI64`, so that under Node, where an `i64` is a BigInt, what a caller
516
866
  * computes with it never mixes a BigInt with a number.
867
+ *
868
+ * @param lo The least value accepted when `bounded`.
869
+ * @param hi The greatest value accepted when `bounded`.
870
+ * @param bounded Whether `lo` and `hi` apply.
871
+ * @returns The value; `0` with `JSON_DECODE_TYPE` for a value that is not
872
+ * a number, and with `JSON_DECODE_RANGE` for a fraction, an exponent or
873
+ * a value out of bounds.
517
874
  */
518
- integer(lo: i64, hi: i64, bounded: boolean): i64 {
875
+ private integer(lo: i64, hi: i64, bounded: boolean): i64 {
519
876
  const k: i32 = this.take()
520
877
  if (k !== JSON_TOK_NUMBER) {
521
878
  if (k >= 0) {
@@ -535,13 +892,25 @@ export class JsonDecoder {
535
892
  return v
536
893
  }
537
894
 
895
+ /**
896
+ * The next value as an `i32`: an integer written without a fraction or an
897
+ * exponent, from -2^31 to 2^31 - 1. A numeric enum is read with it too.
898
+ *
899
+ * @returns The value; `0` with `JSON_DECODE_TYPE` or `JSON_DECODE_RANGE`.
900
+ */
538
901
  i32(): i32 {
539
902
  const lo: i64 = -2147483648
540
903
  const hi: i64 = 2147483647
541
904
  return toI32(this.integer(lo, hi, true))
542
905
  }
543
906
 
544
- /** The bounds are compared and never computed with, so under Node a number beside a BigInt is fine. */
907
+ /**
908
+ * The next value as a `u32`: an integer written without a fraction or an
909
+ * exponent, from 0 to 2^32 - 1. The bounds are compared and never computed
910
+ * with, so under Node a number beside a BigInt is fine.
911
+ *
912
+ * @returns The value; `0` with `JSON_DECODE_TYPE` or `JSON_DECODE_RANGE`.
913
+ */
545
914
  u32(): u32 {
546
915
  const lo: i64 = 0
547
916
  const hi: i64 = 4294967295
@@ -550,7 +919,13 @@ export class JsonDecoder {
550
919
  return toU32(toF64(this.integer(lo, hi, true)))
551
920
  }
552
921
 
553
- /** Any integer `isInteger` accepts fits an `i64`, so there is no range to check. */
922
+ /**
923
+ * The next value as an `i64`: an integer written without a fraction or an
924
+ * exponent. Any integer `isInteger` accepts fits an `i64`, so there is no
925
+ * range to check.
926
+ *
927
+ * @returns The value; `0` with `JSON_DECODE_TYPE` or `JSON_DECODE_RANGE`.
928
+ */
554
929
  i64(): i64 {
555
930
  return this.integer(toI64(0), toI64(0), false)
556
931
  }
@@ -559,7 +934,9 @@ export class JsonDecoder {
559
934
  * The next value as a `u64`: an integer written in digits alone, with no
560
935
  * sign, fraction or exponent, up to 2^64 - 1, read through the tokenizer's
561
936
  * `numberU64`. A negative number, `-0` included, and one past 2^64 - 1 are
562
- * `JSON_DECODE_RANGE`, as a fraction is; `0` with an error.
937
+ * `JSON_DECODE_RANGE`, as a fraction is.
938
+ *
939
+ * @returns The value; `0` with `JSON_DECODE_TYPE` or `JSON_DECODE_RANGE`.
563
940
  */
564
941
  u64(): u64 {
565
942
  const k: i32 = this.take()
@@ -579,7 +956,10 @@ export class JsonDecoder {
579
956
  /**
580
957
  * The next value as a `JsonNumber`: its text, copied into `n`'s bytes
581
958
  * exactly as the input writes it. Anything but a number is
582
- * `JSON_DECODE_TYPE`, and `n` keeps the number it held.
959
+ * `JSON_DECODE_TYPE`, and `n` keeps the number it held. Its bytes are
960
+ * reused, and grown only for a longer number.
961
+ *
962
+ * @param n The number to copy the text into.
583
963
  */
584
964
  number(n: JsonNumber): void {
585
965
  const k: i32 = this.take()
@@ -608,6 +988,12 @@ export class JsonDecoder {
608
988
  n.count = copied
609
989
  }
610
990
 
991
+ /**
992
+ * The next value as an `f64`: any number, the nearest double, and past the
993
+ * double range an infinity.
994
+ *
995
+ * @returns The value; `0` with `JSON_DECODE_TYPE` for a value that is not a number.
996
+ */
611
997
  f64(): f64 {
612
998
  const k: i32 = this.take()
613
999
  if (k === JSON_TOK_NUMBER) {
@@ -619,6 +1005,11 @@ export class JsonDecoder {
619
1005
  return 0
620
1006
  }
621
1007
 
1008
+ /**
1009
+ * The next value as a `boolean`.
1010
+ *
1011
+ * @returns `true` for `true`; `false` for `false`, and with `JSON_DECODE_TYPE` for any other value.
1012
+ */
622
1013
  boolean(): boolean {
623
1014
  const k: i32 = this.take()
624
1015
  if (k === JSON_TOK_TRUE) {
@@ -632,8 +1023,10 @@ export class JsonDecoder {
632
1023
 
633
1024
  /**
634
1025
  * The next value as a string: `cur` itself when the input's string is equal
635
- * to it, which allocates nothing, and a new string otherwise; `cur` with an
636
- * error.
1026
+ * to it, which allocates nothing, and a new string otherwise.
1027
+ *
1028
+ * @param cur The string the field holds now.
1029
+ * @returns The string read; `cur` with `JSON_DECODE_TYPE` for a value that is not a string.
637
1030
  */
638
1031
  string(cur: string): string {
639
1032
  const k: i32 = this.take()
@@ -653,6 +1046,18 @@ export class JsonDecoder {
653
1046
  // array already has, or pushed past its end, and what is left over is
654
1047
  // popped, so an array of the same length grows nothing.
655
1048
 
1049
+ /**
1050
+ * The next value as an array whose every element is an `i32`, read as `i32()` reads one, into `xs`.
1051
+ *
1052
+ * Each element overwrites the slot `xs` already has or is pushed past its
1053
+ * end, and the slots left over are popped, so an array no longer than `xs`
1054
+ * allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
1055
+ * with the tokenizer's code when the input is malformed, and leaves `xs`
1056
+ * untouched; a failure inside the array leaves its contents unspecified.
1057
+ *
1058
+ * @param xs The array the field holds now, whose slots are reused.
1059
+ * @returns `xs`, holding the input's elements.
1060
+ */
656
1061
  i32Array(xs: i32[]): i32[] {
657
1062
  if (!this.beginArray()) {
658
1063
  return xs
@@ -673,6 +1078,18 @@ export class JsonDecoder {
673
1078
  return xs
674
1079
  }
675
1080
 
1081
+ /**
1082
+ * The next value as an array whose every element is a `u32`, read as `u32()` reads one, into `xs`.
1083
+ *
1084
+ * Each element overwrites the slot `xs` already has or is pushed past its
1085
+ * end, and the slots left over are popped, so an array no longer than `xs`
1086
+ * allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
1087
+ * with the tokenizer's code when the input is malformed, and leaves `xs`
1088
+ * untouched; a failure inside the array leaves its contents unspecified.
1089
+ *
1090
+ * @param xs The array the field holds now, whose slots are reused.
1091
+ * @returns `xs`, holding the input's elements.
1092
+ */
676
1093
  u32Array(xs: u32[]): u32[] {
677
1094
  if (!this.beginArray()) {
678
1095
  return xs
@@ -693,6 +1110,18 @@ export class JsonDecoder {
693
1110
  return xs
694
1111
  }
695
1112
 
1113
+ /**
1114
+ * The next value as an array whose every element is an `i64`, read as `i64()` reads one, into `xs`.
1115
+ *
1116
+ * Each element overwrites the slot `xs` already has or is pushed past its
1117
+ * end, and the slots left over are popped, so an array no longer than `xs`
1118
+ * allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
1119
+ * with the tokenizer's code when the input is malformed, and leaves `xs`
1120
+ * untouched; a failure inside the array leaves its contents unspecified.
1121
+ *
1122
+ * @param xs The array the field holds now, whose slots are reused.
1123
+ * @returns `xs`, holding the input's elements.
1124
+ */
696
1125
  i64Array(xs: i64[]): i64[] {
697
1126
  if (!this.beginArray()) {
698
1127
  return xs
@@ -713,6 +1142,18 @@ export class JsonDecoder {
713
1142
  return xs
714
1143
  }
715
1144
 
1145
+ /**
1146
+ * The next value as an array whose every element is a `u64`, read as `u64()` reads one, into `xs`.
1147
+ *
1148
+ * Each element overwrites the slot `xs` already has or is pushed past its
1149
+ * end, and the slots left over are popped, so an array no longer than `xs`
1150
+ * allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
1151
+ * with the tokenizer's code when the input is malformed, and leaves `xs`
1152
+ * untouched; a failure inside the array leaves its contents unspecified.
1153
+ *
1154
+ * @param xs The array the field holds now, whose slots are reused.
1155
+ * @returns `xs`, holding the input's elements.
1156
+ */
716
1157
  u64Array(xs: u64[]): u64[] {
717
1158
  if (!this.beginArray()) {
718
1159
  return xs
@@ -733,6 +1174,18 @@ export class JsonDecoder {
733
1174
  return xs
734
1175
  }
735
1176
 
1177
+ /**
1178
+ * The next value as an array whose every element is an `f64`, read as `f64()` reads one, into `xs`.
1179
+ *
1180
+ * Each element overwrites the slot `xs` already has or is pushed past its
1181
+ * end, and the slots left over are popped, so an array no longer than `xs`
1182
+ * allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
1183
+ * with the tokenizer's code when the input is malformed, and leaves `xs`
1184
+ * untouched; a failure inside the array leaves its contents unspecified.
1185
+ *
1186
+ * @param xs The array the field holds now, whose slots are reused.
1187
+ * @returns `xs`, holding the input's elements.
1188
+ */
736
1189
  f64Array(xs: f64[]): f64[] {
737
1190
  if (!this.beginArray()) {
738
1191
  return xs
@@ -753,6 +1206,18 @@ export class JsonDecoder {
753
1206
  return xs
754
1207
  }
755
1208
 
1209
+ /**
1210
+ * The next value as an array whose every element is a `boolean`, into `xs`.
1211
+ *
1212
+ * Each element overwrites the slot `xs` already has or is pushed past its
1213
+ * end, and the slots left over are popped, so an array no longer than `xs`
1214
+ * allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
1215
+ * with the tokenizer's code when the input is malformed, and leaves `xs`
1216
+ * untouched; a failure inside the array leaves its contents unspecified.
1217
+ *
1218
+ * @param xs The array the field holds now, whose slots are reused.
1219
+ * @returns `xs`, holding the input's elements.
1220
+ */
756
1221
  booleanArray(xs: boolean[]): boolean[] {
757
1222
  if (!this.beginArray()) {
758
1223
  return xs
@@ -773,6 +1238,19 @@ export class JsonDecoder {
773
1238
  return xs
774
1239
  }
775
1240
 
1241
+ /**
1242
+ * The next value as an array whose every element is a string, read as `string(cur)` reads one, each against the string its slot holds, into `xs`.
1243
+ *
1244
+ * Each element overwrites the slot `xs` already has or is pushed past its
1245
+ * end, and the slots left over are popped, so an array no longer than `xs`
1246
+ * grows nothing. Only the container is reused: an element whose text
1247
+ * changed is a new string. A value that is not an array fails with `JSON_DECODE_TYPE`, or
1248
+ * with the tokenizer's code when the input is malformed, and leaves `xs`
1249
+ * untouched; a failure inside the array leaves its contents unspecified.
1250
+ *
1251
+ * @param xs The array the field holds now, whose slots are reused.
1252
+ * @returns `xs`, holding the input's elements.
1253
+ */
776
1254
  stringArray(xs: string[]): string[] {
777
1255
  if (!this.beginArray()) {
778
1256
  return xs
@@ -798,6 +1276,13 @@ export class JsonDecoder {
798
1276
  return xs
799
1277
  }
800
1278
 
1279
+ /**
1280
+ * The next value as `null`, or as an array `i32Array` reads, into `xs`
1281
+ * when it is an array and into a new one when it is `null`.
1282
+ *
1283
+ * @param xs The array the field holds now, or `null`.
1284
+ * @returns `null` for `null`, and otherwise the array read.
1285
+ */
801
1286
  i32ArrayOrNull(xs: i32[] | null): i32[] | null {
802
1287
  if (this.takeNull()) {
803
1288
  return null
@@ -809,6 +1294,13 @@ export class JsonDecoder {
809
1294
  return this.i32Array(fresh)
810
1295
  }
811
1296
 
1297
+ /**
1298
+ * The next value as `null`, or as an array `u32Array` reads, into `xs`
1299
+ * when it is an array and into a new one when it is `null`.
1300
+ *
1301
+ * @param xs The array the field holds now, or `null`.
1302
+ * @returns `null` for `null`, and otherwise the array read.
1303
+ */
812
1304
  u32ArrayOrNull(xs: u32[] | null): u32[] | null {
813
1305
  if (this.takeNull()) {
814
1306
  return null
@@ -820,6 +1312,13 @@ export class JsonDecoder {
820
1312
  return this.u32Array(fresh)
821
1313
  }
822
1314
 
1315
+ /**
1316
+ * The next value as `null`, or as an array `i64Array` reads, into `xs`
1317
+ * when it is an array and into a new one when it is `null`.
1318
+ *
1319
+ * @param xs The array the field holds now, or `null`.
1320
+ * @returns `null` for `null`, and otherwise the array read.
1321
+ */
823
1322
  i64ArrayOrNull(xs: i64[] | null): i64[] | null {
824
1323
  if (this.takeNull()) {
825
1324
  return null
@@ -831,6 +1330,13 @@ export class JsonDecoder {
831
1330
  return this.i64Array(fresh)
832
1331
  }
833
1332
 
1333
+ /**
1334
+ * The next value as `null`, or as an array `u64Array` reads, into `xs`
1335
+ * when it is an array and into a new one when it is `null`.
1336
+ *
1337
+ * @param xs The array the field holds now, or `null`.
1338
+ * @returns `null` for `null`, and otherwise the array read.
1339
+ */
834
1340
  u64ArrayOrNull(xs: u64[] | null): u64[] | null {
835
1341
  if (this.takeNull()) {
836
1342
  return null
@@ -842,6 +1348,13 @@ export class JsonDecoder {
842
1348
  return this.u64Array(fresh)
843
1349
  }
844
1350
 
1351
+ /**
1352
+ * The next value as `null`, or as an array `f64Array` reads, into `xs`
1353
+ * when it is an array and into a new one when it is `null`.
1354
+ *
1355
+ * @param xs The array the field holds now, or `null`.
1356
+ * @returns `null` for `null`, and otherwise the array read.
1357
+ */
845
1358
  f64ArrayOrNull(xs: f64[] | null): f64[] | null {
846
1359
  if (this.takeNull()) {
847
1360
  return null
@@ -853,6 +1366,13 @@ export class JsonDecoder {
853
1366
  return this.f64Array(fresh)
854
1367
  }
855
1368
 
1369
+ /**
1370
+ * The next value as `null`, or as an array `booleanArray` reads, into `xs`
1371
+ * when it is an array and into a new one when it is `null`.
1372
+ *
1373
+ * @param xs The array the field holds now, or `null`.
1374
+ * @returns `null` for `null`, and otherwise the array read.
1375
+ */
856
1376
  booleanArrayOrNull(xs: boolean[] | null): boolean[] | null {
857
1377
  if (this.takeNull()) {
858
1378
  return null
@@ -864,6 +1384,13 @@ export class JsonDecoder {
864
1384
  return this.booleanArray(fresh)
865
1385
  }
866
1386
 
1387
+ /**
1388
+ * The next value as `null`, or as an array `stringArray` reads, into `xs`
1389
+ * when it is an array and into a new one when it is `null`.
1390
+ *
1391
+ * @param xs The array the field holds now, or `null`.
1392
+ * @returns `null` for `null`, and otherwise the array read.
1393
+ */
867
1394
  stringArrayOrNull(xs: string[] | null): string[] | null {
868
1395
  if (this.takeNull()) {
869
1396
  return null
@@ -877,18 +1404,37 @@ export class JsonDecoder {
877
1404
  }
878
1405
 
879
1406
  /**
880
- * Decodes the JSON object `d` holds into `out`, the whole input, and answers
881
- * `d.failed`: `0`, or the first error's code. The compiler writes the code
882
- * from `T`'s fields at every call; this body is what Node runs.
1407
+ * Decodes the JSON object `d` holds, the whole input, into `out`. The
1408
+ * compiler writes the code from `T`'s fields at every call, in the module that
1409
+ * makes it; under Node this body ends the program with a panic, because the
1410
+ * types are erased. Called as `jsonDecode(d, out)`, with `T` the class of `out`;
1411
+ * every field of every class it reaches must be a type the module header lists,
1412
+ * and a class the decoder makes must have a constructor with no parameters.
1413
+ * The top-level value must be an object, or the answer is `JSON_DECODE_TYPE`,
1414
+ * and nothing but blanks may follow it. A call inside a `using a = arena()`
1415
+ * block is refused (NL2424).
1416
+ *
1417
+ * @param _d The decoder over the input; its `failed` and `errorAt` say what
1418
+ * went wrong, and `reset` starts it over.
1419
+ * @param _out The object to decode into: a declared class, whose fields,
1420
+ * nested objects and arrays are reused.
1421
+ * @returns `d.failed`: `JSON_DECODE_OK`, or the first error's code, a
1422
+ * `JSON_DECODE_*` or the tokenizer's `JSON_TOK_ERR_*`.
883
1423
  */
884
1424
  export const jsonDecode = <T>(_d: JsonDecoder, _out: T): i32 => {
885
1425
  panic(JSON_TYPED_ERASED)
886
1426
  }
887
1427
 
888
1428
  /**
889
- * Writes `value` to `w` as one JSON object, its fields in declaration order.
890
- * The compiler writes the code from `T`'s fields at every call; this body is
891
- * what Node runs.
1429
+ * Writes `value` to `w` as one JSON object, its fields in declaration order,
1430
+ * the text `JSON.stringify` writes for it, in the writer's modes. The compiler
1431
+ * writes the code from `T`'s fields at every call; under Node this body ends
1432
+ * the program with a panic, because the types are erased. A full buffer is
1433
+ * the writer's `JSON_WRITE_FULL`, and a value deeper than 64 levels, or one
1434
+ * that holds itself, its `JSON_WRITE_TOO_DEEP`.
1435
+ *
1436
+ * @param _w The writer, whose sticky code reports any failure.
1437
+ * @param _value The object to write: a declared class.
892
1438
  */
893
1439
  export const jsonEncode = <T>(_w: JsonWriter, _value: T): void => {
894
1440
  panic(JSON_TYPED_ERASED)
@@ -898,6 +1444,15 @@ export const jsonEncode = <T>(_w: JsonWriter, _value: T): void => {
898
1444
  // because the code the compiler writes calls them from the module that
899
1445
  // encodes, which may not be this one.
900
1446
 
1447
+ /**
1448
+ * Writes a `string | null` field's value: `null`, or the string.
1449
+ *
1450
+ * @param w The writer.
1451
+ * @param s The value.
1452
+ *
1453
+ * @internal Called by name by the code the compiler writes for `jsonEncode`,
1454
+ * src/json-typed.ts `jsonEncodeHelper`.
1455
+ */
901
1456
  export const jsonEncodeStringOrNull = (w: JsonWriter, s: string | null): void => {
902
1457
  if (s === null) {
903
1458
  w.nullValue()
@@ -907,10 +1462,13 @@ export const jsonEncodeStringOrNull = (w: JsonWriter, s: string | null): void =>
907
1462
  }
908
1463
 
909
1464
  /**
910
- * A `u64`, as its digits: at most 20 of them, written with `raw` from an
1465
+ * Writes a `u64` as its digits: at most 20 of them, written with `raw` from an
911
1466
  * array of this call's own that nothing keeps. A value past 2^63 has no
912
1467
  * `i64` to go through the writer's `i64` as, so every `u64` takes this one
913
1468
  * loop. Every `u64` here is built with `toU64`, a BigInt under Node.
1469
+ *
1470
+ * @param w The writer.
1471
+ * @param v The value.
914
1472
  */
915
1473
  export const jsonEncodeU64 = (w: JsonWriter, v: u64): void => {
916
1474
  const ten: u64 = toU64(10)
@@ -929,12 +1487,23 @@ export const jsonEncodeU64 = (w: JsonWriter, v: u64): void => {
929
1487
  w.raw(digits, first, toI32(digits.length) - first)
930
1488
  }
931
1489
 
932
- /** A `JsonNumber`, as the text it holds, byte for byte. */
1490
+ /**
1491
+ * Writes a `JsonNumber` as the text it holds, byte for byte.
1492
+ *
1493
+ * @param w The writer.
1494
+ * @param n The number.
1495
+ */
933
1496
  export const jsonEncodeNumber = (w: JsonWriter, n: JsonNumber): void => {
934
1497
  const start: i32 = 0
935
1498
  w.raw(n.bytes, start, n.count)
936
1499
  }
937
1500
 
1501
+ /**
1502
+ * Writes an array whose every element is an `i32`.
1503
+ *
1504
+ * @param w The writer.
1505
+ * @param xs The elements, in order.
1506
+ */
938
1507
  export const jsonEncodeI32Array = (w: JsonWriter, xs: i32[]): void => {
939
1508
  w.beginArray()
940
1509
  for (const x of xs) {
@@ -943,6 +1512,12 @@ export const jsonEncodeI32Array = (w: JsonWriter, xs: i32[]): void => {
943
1512
  w.endArray()
944
1513
  }
945
1514
 
1515
+ /**
1516
+ * Writes an array whose every element is a `u32`.
1517
+ *
1518
+ * @param w The writer.
1519
+ * @param xs The elements, in order.
1520
+ */
946
1521
  export const jsonEncodeU32Array = (w: JsonWriter, xs: u32[]): void => {
947
1522
  w.beginArray()
948
1523
  for (const x of xs) {
@@ -951,6 +1526,12 @@ export const jsonEncodeU32Array = (w: JsonWriter, xs: u32[]): void => {
951
1526
  w.endArray()
952
1527
  }
953
1528
 
1529
+ /**
1530
+ * Writes an array whose every element is an `i64`.
1531
+ *
1532
+ * @param w The writer.
1533
+ * @param xs The elements, in order.
1534
+ */
954
1535
  export const jsonEncodeI64Array = (w: JsonWriter, xs: i64[]): void => {
955
1536
  w.beginArray()
956
1537
  for (const x of xs) {
@@ -959,6 +1540,12 @@ export const jsonEncodeI64Array = (w: JsonWriter, xs: i64[]): void => {
959
1540
  w.endArray()
960
1541
  }
961
1542
 
1543
+ /**
1544
+ * Writes an array whose every element is a `u64`, each as `jsonEncodeU64` writes it.
1545
+ *
1546
+ * @param w The writer.
1547
+ * @param xs The elements, in order.
1548
+ */
962
1549
  export const jsonEncodeU64Array = (w: JsonWriter, xs: u64[]): void => {
963
1550
  w.beginArray()
964
1551
  for (const x of xs) {
@@ -967,6 +1554,12 @@ export const jsonEncodeU64Array = (w: JsonWriter, xs: u64[]): void => {
967
1554
  w.endArray()
968
1555
  }
969
1556
 
1557
+ /**
1558
+ * Writes an array whose every element is an `f64`, a NaN or an infinity as the writer writes one.
1559
+ *
1560
+ * @param w The writer.
1561
+ * @param xs The elements, in order.
1562
+ */
970
1563
  export const jsonEncodeF64Array = (w: JsonWriter, xs: f64[]): void => {
971
1564
  w.beginArray()
972
1565
  for (const x of xs) {
@@ -975,6 +1568,12 @@ export const jsonEncodeF64Array = (w: JsonWriter, xs: f64[]): void => {
975
1568
  w.endArray()
976
1569
  }
977
1570
 
1571
+ /**
1572
+ * Writes an array whose every element is a `boolean`.
1573
+ *
1574
+ * @param w The writer.
1575
+ * @param xs The elements, in order.
1576
+ */
978
1577
  export const jsonEncodeBooleanArray = (w: JsonWriter, xs: boolean[]): void => {
979
1578
  w.beginArray()
980
1579
  for (const x of xs) {
@@ -983,6 +1582,12 @@ export const jsonEncodeBooleanArray = (w: JsonWriter, xs: boolean[]): void => {
983
1582
  w.endArray()
984
1583
  }
985
1584
 
1585
+ /**
1586
+ * Writes an array whose every element is a string.
1587
+ *
1588
+ * @param w The writer.
1589
+ * @param xs The elements, in order.
1590
+ */
986
1591
  export const jsonEncodeStringArray = (w: JsonWriter, xs: string[]): void => {
987
1592
  w.beginArray()
988
1593
  for (const x of xs) {
@@ -991,6 +1596,12 @@ export const jsonEncodeStringArray = (w: JsonWriter, xs: string[]): void => {
991
1596
  w.endArray()
992
1597
  }
993
1598
 
1599
+ /**
1600
+ * Writes `null`, or the array as `jsonEncodeI32Array` writes it.
1601
+ *
1602
+ * @param w The writer.
1603
+ * @param xs The elements, or `null`.
1604
+ */
994
1605
  export const jsonEncodeI32ArrayOrNull = (w: JsonWriter, xs: i32[] | null): void => {
995
1606
  if (xs === null) {
996
1607
  w.nullValue()
@@ -999,6 +1610,12 @@ export const jsonEncodeI32ArrayOrNull = (w: JsonWriter, xs: i32[] | null): void
999
1610
  }
1000
1611
  }
1001
1612
 
1613
+ /**
1614
+ * Writes `null`, or the array as `jsonEncodeU32Array` writes it.
1615
+ *
1616
+ * @param w The writer.
1617
+ * @param xs The elements, or `null`.
1618
+ */
1002
1619
  export const jsonEncodeU32ArrayOrNull = (w: JsonWriter, xs: u32[] | null): void => {
1003
1620
  if (xs === null) {
1004
1621
  w.nullValue()
@@ -1007,6 +1624,12 @@ export const jsonEncodeU32ArrayOrNull = (w: JsonWriter, xs: u32[] | null): void
1007
1624
  }
1008
1625
  }
1009
1626
 
1627
+ /**
1628
+ * Writes `null`, or the array as `jsonEncodeI64Array` writes it.
1629
+ *
1630
+ * @param w The writer.
1631
+ * @param xs The elements, or `null`.
1632
+ */
1010
1633
  export const jsonEncodeI64ArrayOrNull = (w: JsonWriter, xs: i64[] | null): void => {
1011
1634
  if (xs === null) {
1012
1635
  w.nullValue()
@@ -1015,6 +1638,12 @@ export const jsonEncodeI64ArrayOrNull = (w: JsonWriter, xs: i64[] | null): void
1015
1638
  }
1016
1639
  }
1017
1640
 
1641
+ /**
1642
+ * Writes `null`, or the array as `jsonEncodeU64Array` writes it.
1643
+ *
1644
+ * @param w The writer.
1645
+ * @param xs The elements, or `null`.
1646
+ */
1018
1647
  export const jsonEncodeU64ArrayOrNull = (w: JsonWriter, xs: u64[] | null): void => {
1019
1648
  if (xs === null) {
1020
1649
  w.nullValue()
@@ -1023,6 +1652,12 @@ export const jsonEncodeU64ArrayOrNull = (w: JsonWriter, xs: u64[] | null): void
1023
1652
  }
1024
1653
  }
1025
1654
 
1655
+ /**
1656
+ * Writes `null`, or the array as `jsonEncodeF64Array` writes it.
1657
+ *
1658
+ * @param w The writer.
1659
+ * @param xs The elements, or `null`.
1660
+ */
1026
1661
  export const jsonEncodeF64ArrayOrNull = (w: JsonWriter, xs: f64[] | null): void => {
1027
1662
  if (xs === null) {
1028
1663
  w.nullValue()
@@ -1031,6 +1666,12 @@ export const jsonEncodeF64ArrayOrNull = (w: JsonWriter, xs: f64[] | null): void
1031
1666
  }
1032
1667
  }
1033
1668
 
1669
+ /**
1670
+ * Writes `null`, or the array as `jsonEncodeBooleanArray` writes it.
1671
+ *
1672
+ * @param w The writer.
1673
+ * @param xs The elements, or `null`.
1674
+ */
1034
1675
  export const jsonEncodeBooleanArrayOrNull = (w: JsonWriter, xs: boolean[] | null): void => {
1035
1676
  if (xs === null) {
1036
1677
  w.nullValue()
@@ -1039,6 +1680,12 @@ export const jsonEncodeBooleanArrayOrNull = (w: JsonWriter, xs: boolean[] | null
1039
1680
  }
1040
1681
  }
1041
1682
 
1683
+ /**
1684
+ * Writes `null`, or the array as `jsonEncodeStringArray` writes it.
1685
+ *
1686
+ * @param w The writer.
1687
+ * @param xs The elements, or `null`.
1688
+ */
1042
1689
  export const jsonEncodeStringArrayOrNull = (w: JsonWriter, xs: string[] | null): void => {
1043
1690
  if (xs === null) {
1044
1691
  w.nullValue()