nish 0.35.0 → 0.37.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.
- package/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/json-typed.ts
CHANGED
|
@@ -15,19 +15,101 @@
|
|
|
15
15
|
* jsonEncode(w, p); // out[0 .. w.end) is {"x":..,"y":..}
|
|
16
16
|
* }
|
|
17
17
|
*
|
|
18
|
-
* **What is generated, and from what.** `jsonDecode(d, out)` and
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* of
|
|
30
|
-
*
|
|
18
|
+
* **What is generated, and from what.** `jsonDecode(d, out)` and `jsonEncode(w,
|
|
19
|
+
* value)` are generic, and the bodies written below are not what runs natively:
|
|
20
|
+
* the compiler lowers every call in place, in the module that makes it, to code
|
|
21
|
+
* it writes from the layout of the class the call binds `T` to
|
|
22
|
+
* (`src/json-typed.ts`, `src/emit-json.ts`).
|
|
23
|
+
*
|
|
24
|
+
* That code is straight-line calls into the methods of `JsonDecoder` and the
|
|
25
|
+
* functions here for each field, and into `JsonWriter`'s, so all of the parsing
|
|
26
|
+
* is the tokenizer's and all of the writing is the writer's. A field may be an
|
|
27
|
+
* `i32`, `u32`, `i64`, `u64`, `f64` (or `number`), `boolean` or `string`, a
|
|
28
|
+
* numeric enum (its number), a class decoded the same way, a `JsonNumber`, an
|
|
29
|
+
* array of any of those, a `Map<string, V>` (a JSON object) whose `V` is any of
|
|
30
|
+
* these, or a string, class, array or map `| null`; anything else is refused at
|
|
31
|
+
* the call, naming the field (docs/LANGUAGE.md, "Typed JSON").
|
|
32
|
+
*
|
|
33
|
+
* **Call shapes.** `jsonDecode(d, out)` reads the one JSON object `d` holds
|
|
34
|
+
* into `out`, a declared class, and answers `d.failed`: `JSON_DECODE_OK` (0)
|
|
35
|
+
* or the first error's code. `jsonEncode(w, value)` writes `value` to the
|
|
36
|
+
* writer `w` as one JSON object and answers nothing; the writer's own sticky
|
|
37
|
+
* code reports a failure. The decoder is a `JsonDecoder`, a `JsonTokenizer`
|
|
38
|
+
* over the input and a sticky error, and the encoder is the `JsonWriter` the
|
|
39
|
+
* call is given, so the encoding is written in the writer's modes: a writer
|
|
40
|
+
* set with `setIndent(2)` encodes what `JSON.stringify(v, null, 2)` writes,
|
|
41
|
+
* and a `setStrict(true)` writer fails a NaN or infinite `f64` field with
|
|
42
|
+
* `JSON_WRITE_NON_FINITE`.
|
|
43
|
+
*
|
|
44
|
+
* **What a field may be**, and how each maps:
|
|
45
|
+
*
|
|
46
|
+
* Field type JSON Decoded with Encoded with
|
|
47
|
+
* i32, u32, i64 an integer, no fraction or d.i32(), w.i32, w.u32,
|
|
48
|
+
* exponent, in the type's d.u32(), w.i64
|
|
49
|
+
* range d.i64()
|
|
50
|
+
* u64 digits alone, no sign, d.u64() jsonEncodeU64
|
|
51
|
+
* fraction or exponent, up to
|
|
52
|
+
* 2^64 - 1
|
|
53
|
+
* numeric enum E the number of one of E's d.i32(), then w.i32
|
|
54
|
+
* members a switch
|
|
55
|
+
* f64 (number in f64 mode) any number; past the double d.f64() w.f64
|
|
56
|
+
* range an infinity, which the
|
|
57
|
+
* writer writes as null
|
|
58
|
+
* boolean true, false d.boolean() w.boolean
|
|
59
|
+
* string, string | null a string, and null for the d.string(cur) w.string,
|
|
60
|
+
* second jsonEncodeStringOrNull
|
|
61
|
+
* class C, C | null an object, and null for the C's own C's own
|
|
62
|
+
* second decoding, into encoding
|
|
63
|
+
* the object the
|
|
64
|
+
* field holds
|
|
65
|
+
* T[], T[] | null an array, and null for the d.i32Array(cur) jsonEncodeI32Array
|
|
66
|
+
* second and its kin, or and its kin, or
|
|
67
|
+
* C's or E's per C's or E's per
|
|
68
|
+
* element element
|
|
69
|
+
* Map<string, V>, or | null an object whose keys are the keys in order, entries in the
|
|
70
|
+
* map's, each value a V each value set map's order
|
|
71
|
+
* JsonNumber any number, kept as the text d.number(n) jsonEncodeNumber
|
|
72
|
+
* it is written in
|
|
73
|
+
*
|
|
74
|
+
* A `Map`'s `V` is any type in this list, `| null` or not, a `Map` included;
|
|
75
|
+
* `JsonNumber` may stand alone, be `| null`, be an array's element or be a
|
|
76
|
+
* `Map`'s value. The code written for a nested class, an array's element class,
|
|
77
|
+
* a `| null` field's class and a `Map`'s values is the same code, so it
|
|
78
|
+
* recurses as the data does.
|
|
79
|
+
*
|
|
80
|
+
* Anything else is refused at the call's argument, naming the class and the
|
|
81
|
+
* field (NL2485 for a field type, such as an `f32`, an `i32[][]`, a
|
|
82
|
+
* `Level[][]`, a `Set`, an array of maps, a `Map` not keyed by `string` (an
|
|
83
|
+
* enum key included) or a `Map` whose values cannot be written, and a generic
|
|
84
|
+
* class of the program's own). An enum whose members are strings or a mix is
|
|
85
|
+
* not one the language declares (NL1043), and a `const enum` is refused where
|
|
86
|
+
* it is declared (NL2285).
|
|
87
|
+
*
|
|
88
|
+
* A value that is not a declared class (an interface, a generic class or any
|
|
89
|
+
* other type) is NL2486; decoding into an interface is left for a later
|
|
90
|
+
* version. A call inside a generic function is checked for each class an
|
|
91
|
+
* instantiation binds.
|
|
92
|
+
*
|
|
93
|
+
* **Reading the class.** A class the decoder has to make itself, an array's
|
|
94
|
+
* element past the end, a `| null` field that holds `null` or a map's value,
|
|
95
|
+
* is made with `new C()`, so its constructor takes no parameters (NL2488).
|
|
96
|
+
* Decoding assigns every field, so a `readonly` one is refused (NL2487). A
|
|
97
|
+
* decode stores what it makes into the object it fills, so a call inside a
|
|
98
|
+
* `using a = arena()` block is refused (NL2424); an encoding only reads the
|
|
99
|
+
* object and keeps nothing, so it may run inside one. Two fields holding one
|
|
100
|
+
* object are decoded into it twice, the second winning.
|
|
101
|
+
*
|
|
102
|
+
* **Finding the field.** The code written for a class of three or more fields
|
|
103
|
+
* switches on the key's length in bytes, so a key is compared only with the
|
|
104
|
+
* names of that length. Where three or more names share a length, it switches
|
|
105
|
+
* once more, on the key's byte at the one position where those names differ
|
|
106
|
+
* most, and calls `keyIs` for each name with that byte there, in declaration
|
|
107
|
+
* order; a byte none of them has is an unknown key at once. One or two names
|
|
108
|
+
* are compared in turn with `keyIs`.
|
|
109
|
+
*
|
|
110
|
+
* A key with an escape has no byte to switch on there and is compared with
|
|
111
|
+
* every name of its length, so `"id"` is the field `id`; a field name that is
|
|
112
|
+
* not ASCII is dispatched by its UTF-8 bytes like any other.
|
|
31
113
|
*
|
|
32
114
|
* **The decoding rules.** A JSON object's keys are the class's field names,
|
|
33
115
|
* byte for byte, or the keys its options give them, found by the key's length
|
|
@@ -39,20 +121,101 @@
|
|
|
39
121
|
* }
|
|
40
122
|
*
|
|
41
123
|
* The compiler reads that literal; it is not a field. Every field is required
|
|
42
|
-
* but an optional one, whose missing key sets it to its initializer, a key
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* range, and `f64` takes any number.
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* object
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
124
|
+
* but an optional one, whose missing key sets it to its initializer, a key may
|
|
125
|
+
* appear once, and a key that names no field is an error unless `ignoreUnknown`
|
|
126
|
+
* is set, when its value is stepped over, checked but not kept. A number must
|
|
127
|
+
* fit its field: an integer field takes an integer written without a fraction
|
|
128
|
+
* or an exponent and in the field's range, and `f64` takes any number.
|
|
129
|
+
*
|
|
130
|
+
* `null` is accepted only by a field whose type is `| null`. Fields are written
|
|
131
|
+
* to `out` as they are read, in the input's order: a nested object is decoded
|
|
132
|
+
* into the object the field already holds, an array's elements into the
|
|
133
|
+
* elements it already has, with new ones made past its end and the rest dropped
|
|
134
|
+
* (so a reused object reuses its arrays and objects, and `Arena.used()` stays
|
|
135
|
+
* flat: it is flat over 1,000 decodes of a three-level object), and a string
|
|
136
|
+
* field keeps the string it holds when the input's is equal.
|
|
137
|
+
*
|
|
138
|
+
* A string that changes is a new string, made by one `String.fromBytes`,
|
|
139
|
+
* straight from the input when it has no escape and from its unescaped bytes
|
|
140
|
+
* when it has one. A class the decoder has to make — an array element past the
|
|
141
|
+
* array's end, a `| null` field that holds `null`, or a map's value — is made
|
|
142
|
+
* with `new C()`.
|
|
143
|
+
*
|
|
144
|
+
* A map is emptied once the object opens, then each key is read as a new string
|
|
145
|
+
* and its value as a `V` made fresh for the key (a zero, `""`, the enum's first
|
|
146
|
+
* member, `null`, or a new object, array or map) and `set`, so the entries are
|
|
147
|
+
* the input's, in the input's order. A key the object gave already is
|
|
148
|
+
* `JSON_DECODE_DUPLICATE` before its value is read, however either is escaped.
|
|
149
|
+
* A value whose read fails is not `set`, so a map never holds a value the input
|
|
150
|
+
* did not give.
|
|
151
|
+
*
|
|
152
|
+
* Unlike the rest of a decode, a map's entries are made anew each time, so
|
|
153
|
+
* decoding into a map allocates its keys and values. Every map of one decoding
|
|
154
|
+
* takes at most `maxMapKeys` keys between them (4,096 unless set), the keys of
|
|
155
|
+
* a map inside another map included; the key past it is `JSON_DECODE_MAP_LIMIT`
|
|
156
|
+
* before it is read, and `reset` starts the count again. At `0` or below no map
|
|
157
|
+
* may take a key, and `{}` is the one map value accepted. Encoding writes the
|
|
158
|
+
* entries in the map's own order, which is insertion order.
|
|
159
|
+
*
|
|
160
|
+
* That differs from a JavaScript object in one case: a key that reads as an
|
|
161
|
+
* array index (`"1"`) keeps its place, where `JSON.parse` would move it first.
|
|
162
|
+
*
|
|
163
|
+
* **A class's options** are one `static readonly json` object literal written
|
|
164
|
+
* in the class, which the compiler reads when it writes the class's code. It
|
|
165
|
+
* is not a field: it has no slot, the class's layout is what it would be
|
|
166
|
+
* without it, and the program cannot read it. It is the one `static` member a
|
|
167
|
+
* class may declare. A class without it is decoded exactly as before. Its
|
|
168
|
+
* options are checked where the class is declared, whether or not any call
|
|
169
|
+
* decodes the class. There are three, each a literal and none a default
|
|
170
|
+
* other than being absent:
|
|
171
|
+
*
|
|
172
|
+
* - `rename`, an object mapping a field to its key on the wire, a string
|
|
173
|
+
* literal of any bytes. `{"user_id":7}` is decoded into `userId` and
|
|
174
|
+
* `userId` is encoded as `"user_id"`; the field's own name is then a key
|
|
175
|
+
* no field has. The key is dispatched like any other, escapes and
|
|
176
|
+
* non-ASCII included. Without it a field's key is its name.
|
|
177
|
+
* - `optional`, an array of field names whose key may be missing. A missing
|
|
178
|
+
* one sets the field to its initializer, on a fresh object and on one
|
|
179
|
+
* decoded into again, so a reused object keeps nothing an earlier input
|
|
180
|
+
* gave it, and allocates nothing to do it. An optional field needs an
|
|
181
|
+
* initializer. A key that is given is read like any other: once, and of
|
|
182
|
+
* its field's type. Without it every field is required and a missing key is
|
|
183
|
+
* `JSON_DECODE_MISSING`.
|
|
184
|
+
* - `presence`, described below. Without it nothing is recorded.
|
|
185
|
+
*
|
|
186
|
+
* Each misuse has its own diagnostic:
|
|
187
|
+
*
|
|
188
|
+
* NL2492 `static json` without `readonly` but with a type, `static
|
|
189
|
+
* readonly json` with a type annotation, or on a generic class
|
|
190
|
+
* NL2493 options, `rename`, a key or an `optional` entry not written as
|
|
191
|
+
* the literal it must be (a module constant, an array for the
|
|
192
|
+
* object, a string for `optional`'s array); `presence` or one of
|
|
193
|
+
* its entries not an object literal of string literals
|
|
194
|
+
* NL2494 an option other than `rename`, `optional` and `presence`
|
|
195
|
+
* NL2495 a name that is not one of the class's fields; `presence` naming
|
|
196
|
+
* a field or a presence field the class does not declare
|
|
197
|
+
* NL2496 a rename to a value that is not a string
|
|
198
|
+
* NL2497 two fields with one key (a rename to another field's own name
|
|
199
|
+
* included), or a field renamed or made optional twice
|
|
200
|
+
* NL2498 an optional field with no initializer
|
|
201
|
+
* NL2499 a rename to a key that is not valid UTF-8, a lone surrogate such
|
|
202
|
+
* as `"\ud800"` included, which no input could give
|
|
203
|
+
* NL2592 a presence field that is not a `boolean`, is `readonly`, is the
|
|
204
|
+
* field it records, records two fields, is recorded by a presence
|
|
205
|
+
* field of its own or is renamed or made optional; a field
|
|
206
|
+
* recorded twice
|
|
207
|
+
*
|
|
208
|
+
* A spread or a computed key in the literal is refused as it is anywhere
|
|
209
|
+
* (NL1061, NL1041), a subclass by `extends`, and a second `static readonly
|
|
210
|
+
* json` is a duplicate member. The options are not a value: `User.json` names a
|
|
211
|
+
* class where a value is due, and is refused as any class name there is.
|
|
212
|
+
*
|
|
213
|
+
* Only `static readonly json` may leave out its type: written `static json = {
|
|
214
|
+
* ... }`, or as an instance field, it is an ordinary field to the parser and
|
|
215
|
+
* needs one, and an instance field called `json` with a type is an ordinary
|
|
216
|
+
* field, read and written under its own name. A field or a method called `json`
|
|
217
|
+
* may come before or after the options. Under Node the literal is a static
|
|
218
|
+
* property of the class that nothing reads.
|
|
56
219
|
*
|
|
57
220
|
* **Which keys were given.** An optional field's missing key and a key given
|
|
58
221
|
* the field's initializer leave the same value; the `presence` option tells
|
|
@@ -68,16 +231,18 @@
|
|
|
68
231
|
* hasEmail: boolean = false;
|
|
69
232
|
* }
|
|
70
233
|
*
|
|
71
|
-
* Any field can be recorded, of any kind and optional or not. A presence
|
|
72
|
-
*
|
|
234
|
+
* Any field can be recorded, of any kind and optional or not. A presence field
|
|
235
|
+
* is off the wire: no key reads it, encoding leaves it out, and no option
|
|
73
236
|
* renames it, makes it optional or records it. It is set when the object
|
|
74
|
-
* closes, from the flag the decoder already keeps per field to find a
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
237
|
+
* closes, from the flag the decoder already keeps per field to find a duplicate
|
|
238
|
+
* or a missing key, so recording costs one store per recorded field and
|
|
239
|
+
* allocates nothing, and a class that records nothing compiles to the code it
|
|
240
|
+
* did before.
|
|
241
|
+
*
|
|
242
|
+
* Each object sets its own, so a nested class or an array element answers for
|
|
243
|
+
* its own keys. A decoding that fails may set some of them and not others (when
|
|
244
|
+
* the input is not an object, none), so read them only when `jsonDecode`
|
|
245
|
+
* answered `JSON_DECODE_OK`.
|
|
81
246
|
*
|
|
82
247
|
* **A merge patch (RFC 7396)** is that option's use: a patch class whose
|
|
83
248
|
* every field is optional and recorded, decoded and then applied a field at a
|
|
@@ -93,18 +258,68 @@
|
|
|
93
258
|
* or the tokenizer's own `JSON_TOK_ERR_*` code when the text is not JSON or
|
|
94
259
|
* crosses one of its `JsonLimits` — and `errorAt` to the byte offset of the
|
|
95
260
|
* token it was found at. Every later call does nothing, and `jsonDecode`
|
|
96
|
-
* answers `failed
|
|
97
|
-
* a value of its type in every field, some of them
|
|
98
|
-
* were
|
|
261
|
+
* answers `failed`; `reset` starts over. Nothing an input holds panics. After
|
|
262
|
+
* an error, `out` holds a value of its type in every field, some of them
|
|
263
|
+
* decoded and some as they were: an enum field or element never holds a number
|
|
264
|
+
* that is no member's, because such a number is not stored. The codes:
|
|
265
|
+
*
|
|
266
|
+
* Code Value Condition errorAt
|
|
267
|
+
* JSON_DECODE_OK 0 nothing has failed
|
|
268
|
+
* JSON_DECODE_TYPE -20 a value of the wrong JSON type the value
|
|
269
|
+
* JSON_DECODE_RANGE -21 a number its field cannot hold the number
|
|
270
|
+
* JSON_DECODE_UNKNOWN -22 a key that names no field the key
|
|
271
|
+
* JSON_DECODE_DUPLICATE -23 a key given twice the second
|
|
272
|
+
* JSON_DECODE_MISSING -24 a field the object lacks the object's `}`
|
|
273
|
+
* JSON_DECODE_MAP_LIMIT -25 more than maxMapKeys map keys the first key past it
|
|
274
|
+
* JSON_TOK_ERR_* -1 to -13 text that is not JSON, or past the tokenizer's
|
|
275
|
+
* a JsonLimits limit
|
|
276
|
+
*
|
|
277
|
+
* **Limits.** The recursion is the data's, so the tokenizer's depth limit
|
|
278
|
+
* bounds it: the caller's `JsonLimits.maxDepth`, 128 by default. Each level is
|
|
279
|
+
* one native frame, so a limit raised into the hundreds of thousands can run
|
|
280
|
+
* off the stack, and one kept to a few thousand cannot. The writer refuses a
|
|
281
|
+
* 65th open container (`JSON_WRITER_MAX_DEPTH`), so a value decoded from
|
|
282
|
+
* deeper than 64 levels encodes as `JSON_WRITE_TOO_DEEP`, as does an object
|
|
283
|
+
* that holds itself; the encoding stops at the first failure. A full buffer
|
|
284
|
+
* is the writer's `JSON_WRITE_FULL`. A duplicate of a key `ignoreUnknown`
|
|
285
|
+
* steps over is not detected, because its value is never read. A map is
|
|
286
|
+
* bounded by `maxMapKeys`, and a number's length by `JsonLimits.maxNumberBytes`.
|
|
287
|
+
*
|
|
288
|
+
* **An enum is its number.** A numeric enum is read with `d.i32()`, so a
|
|
289
|
+
* number that is not an `i32` integer is that field's `JSON_DECODE_RANGE`, and
|
|
290
|
+
* an integer that is not the value of one of its members is
|
|
291
|
+
* `JSON_DECODE_TYPE`; a member's name, `"High"`, is a string and a type error
|
|
292
|
+
* too. Two members of one value are one number. It is written as `w.i32` of
|
|
293
|
+
* its value, as `JSON.stringify` writes a TypeScript numeric enum.
|
|
294
|
+
*
|
|
295
|
+
* **A `u64` is exact to 2^64 - 1.** It is read through the tokenizer's
|
|
296
|
+
* `numberU64`, so `18446744073709551615` decodes and `18446744073709551616`,
|
|
297
|
+
* `-1`, `-0` and `1.5` are `JSON_DECODE_RANGE`. It is written as its digits,
|
|
298
|
+
* which `JSON.stringify` cannot do for a number past 2^53.
|
|
299
|
+
*
|
|
300
|
+
* **A `JsonNumber` is a number's text.** A field of it decodes and encodes any
|
|
301
|
+
* number byte for byte: an integer wider than a `u64`, a fraction with more
|
|
302
|
+
* digits than an `f64` holds, `1E+400` and `-0` alike. It is read as a number
|
|
303
|
+
* whole, not as an object, so anything but a number is `JSON_DECODE_TYPE`, and
|
|
304
|
+
* its length is bounded by `JsonLimits.maxNumberBytes` like any number's.
|
|
305
|
+
* Decoding into one reuses its bytes, so `Arena.used()` stays flat over
|
|
306
|
+
* decodes into one object.
|
|
99
307
|
*
|
|
100
308
|
* **Encoding** writes the fields in declaration order, each through the
|
|
101
309
|
* 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.
|
|
310
|
+
* the same object; the writer's own sticky code reports a full buffer. A
|
|
311
|
+
* presence field is left out. The keys come back in the order they were
|
|
312
|
+
* written when the object is decoded from text whose keys are in that order.
|
|
103
313
|
*
|
|
104
314
|
* **Under Node** the types are erased, so there is no layout to generate the
|
|
105
315
|
* code from: `jsonDecode` and `jsonEncode` end the program with a panic
|
|
106
316
|
* 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
|
|
317
|
+
* (docs/LANGUAGE.md, "Typed JSON", class C). Everything else here runs there,
|
|
318
|
+
* so a program may run the decoder's readers and the writer under Node and
|
|
319
|
+
* get the lines the native program gets; the translation of a class is a
|
|
320
|
+
* function per class of the same calls into `JsonDecoder` and `JsonWriter`. A
|
|
321
|
+
* program that imports `std/json-typed.ts` by path rather than as
|
|
322
|
+
* `nish/json-typed` is compiled as written and gets those bodies natively too.
|
|
108
323
|
*
|
|
109
324
|
* Every name here is `json`-prefixed, as `std/json.ts` explains, and every
|
|
110
325
|
* code is an `i32`, so the module is the same under `--number-mode f64`.
|
|
@@ -127,22 +342,35 @@ import {
|
|
|
127
342
|
|
|
128
343
|
/** Nothing has failed. */
|
|
129
344
|
export const JSON_DECODE_OK: i32 = 0
|
|
130
|
-
/**
|
|
345
|
+
/**
|
|
346
|
+
* A value of the wrong JSON type for its field: a string for an `i32`, an
|
|
347
|
+
* array for a class, `null` for a field that is not `| null`, a top-level
|
|
348
|
+
* value that is not an object, or an integer that is no member's value for
|
|
349
|
+
* an enum. `errorAt` is the value.
|
|
350
|
+
*/
|
|
131
351
|
export const JSON_DECODE_TYPE: i32 = -20
|
|
132
|
-
/**
|
|
352
|
+
/**
|
|
353
|
+
* A number its field cannot hold: a fraction or an exponent for an integer
|
|
354
|
+
* field, a value outside its range, or a negative number, `-0` included, for
|
|
355
|
+
* a `u64`. `errorAt` is the number.
|
|
356
|
+
*/
|
|
133
357
|
export const JSON_DECODE_RANGE: i32 = -21
|
|
134
|
-
/** A key that names no field of the class, while `ignoreUnknown` is off. */
|
|
358
|
+
/** A key that names no field of the class, while `ignoreUnknown` is off; `errorAt` is the key. */
|
|
135
359
|
export const JSON_DECODE_UNKNOWN: i32 = -22
|
|
136
|
-
/**
|
|
360
|
+
/**
|
|
361
|
+
* A key the object already gave, a field's or a `Map`'s, however either is
|
|
362
|
+
* escaped; `errorAt` is the second. A key `ignoreUnknown` steps over is not
|
|
363
|
+
* checked, because its value is never read.
|
|
364
|
+
*/
|
|
137
365
|
export const JSON_DECODE_DUPLICATE: i32 = -23
|
|
138
|
-
/** An object that ended without
|
|
366
|
+
/** An object that ended without a required field of the class; `errorAt` is its closing brace. */
|
|
139
367
|
export const JSON_DECODE_MISSING: i32 = -24
|
|
140
368
|
/**
|
|
141
369
|
* More keys, over every `Map` field one decoding fills, than `maxMapKeys`;
|
|
142
370
|
* `errorAt` is the first key past the limit. The global `Map` hashes a
|
|
143
|
-
* string
|
|
144
|
-
* limit bounds what the maps
|
|
145
|
-
* (docs/security/json.md, JSON-13).
|
|
371
|
+
* string under a per-process random key, so a body cannot choose keys that
|
|
372
|
+
* collide, and this limit bounds what all the maps of one decoding hold
|
|
373
|
+
* together (docs/security/json.md, JSON-13).
|
|
146
374
|
*/
|
|
147
375
|
export const JSON_DECODE_MAP_LIMIT: i32 = -25
|
|
148
376
|
|
|
@@ -164,18 +392,27 @@ const JSON_TYPED_ERASED: string =
|
|
|
164
392
|
*
|
|
165
393
|
* A new one holds `0`. Decoding into one reuses its bytes, so an object
|
|
166
394
|
* decoded again with numbers no longer than before allocates nothing for
|
|
167
|
-
* them.
|
|
395
|
+
* them. Its length is bounded by `JsonLimits.maxNumberBytes` like any
|
|
396
|
+
* number's, and anything but a number read into it is `JSON_DECODE_TYPE`.
|
|
397
|
+
* `setText(t)` sets it when `t` is one JSON number and answers whether it was.
|
|
398
|
+
* Its methods run under Node.
|
|
168
399
|
*/
|
|
169
400
|
export class JsonNumber {
|
|
170
401
|
/** The text is `bytes[0, count)`, one JSON number; the bytes past it are room. */
|
|
171
402
|
bytes: u8[]
|
|
403
|
+
/** How many of `bytes` are the number's text, `1` for the `0` a new one holds. */
|
|
172
404
|
count: i32 = 1
|
|
173
405
|
|
|
406
|
+
/** A number that holds `0`. */
|
|
174
407
|
constructor() {
|
|
175
408
|
this.bytes = [48]
|
|
176
409
|
}
|
|
177
410
|
|
|
178
|
-
/**
|
|
411
|
+
/**
|
|
412
|
+
* The number's text.
|
|
413
|
+
*
|
|
414
|
+
* @returns A new string of the text, exactly as it was decoded or set.
|
|
415
|
+
*/
|
|
179
416
|
text(): string {
|
|
180
417
|
return String.fromBytes(this.bytes, 0, this.count)
|
|
181
418
|
}
|
|
@@ -183,8 +420,10 @@ export class JsonNumber {
|
|
|
183
420
|
/**
|
|
184
421
|
* A tokenizer over the text, read to its one token. It may open no
|
|
185
422
|
* container and take a number of any length, so it allocates no stack.
|
|
423
|
+
*
|
|
424
|
+
* @returns A new tokenizer whose current token is the number.
|
|
186
425
|
*/
|
|
187
|
-
reader(): JsonTokenizer {
|
|
426
|
+
private reader(): JsonTokenizer {
|
|
188
427
|
const limits: JsonLimits = new JsonLimits()
|
|
189
428
|
limits.maxDepth = 0
|
|
190
429
|
limits.maxNumberBytes = this.count
|
|
@@ -193,34 +432,57 @@ export class JsonNumber {
|
|
|
193
432
|
return tok
|
|
194
433
|
}
|
|
195
434
|
|
|
196
|
-
/**
|
|
435
|
+
/**
|
|
436
|
+
* Whether the number is an `i64`.
|
|
437
|
+
*
|
|
438
|
+
* @returns `true` when it is written without a fraction or an exponent and its value fits an `i64`.
|
|
439
|
+
*/
|
|
197
440
|
isI64(): boolean {
|
|
198
441
|
return this.reader().isInteger()
|
|
199
442
|
}
|
|
200
443
|
|
|
201
|
-
/**
|
|
444
|
+
/**
|
|
445
|
+
* The number as an `i64`.
|
|
446
|
+
*
|
|
447
|
+
* @returns The value when `isI64()` holds, and `0` otherwise.
|
|
448
|
+
*/
|
|
202
449
|
i64(): i64 {
|
|
203
450
|
return this.reader().numberI64()
|
|
204
451
|
}
|
|
205
452
|
|
|
206
|
-
/**
|
|
453
|
+
/**
|
|
454
|
+
* Whether the number is a `u64`.
|
|
455
|
+
*
|
|
456
|
+
* @returns `true` when it is written in digits alone, with no sign, fraction or exponent, and fits a `u64`.
|
|
457
|
+
*/
|
|
207
458
|
isU64(): boolean {
|
|
208
459
|
return this.reader().isU64()
|
|
209
460
|
}
|
|
210
461
|
|
|
211
|
-
/**
|
|
462
|
+
/**
|
|
463
|
+
* The number as a `u64`.
|
|
464
|
+
*
|
|
465
|
+
* @returns The value when `isU64()` holds, and `0` otherwise.
|
|
466
|
+
*/
|
|
212
467
|
u64(): u64 {
|
|
213
468
|
return this.reader().numberU64()
|
|
214
469
|
}
|
|
215
470
|
|
|
216
|
-
/**
|
|
471
|
+
/**
|
|
472
|
+
* The number as an `f64`.
|
|
473
|
+
*
|
|
474
|
+
* @returns The nearest `f64`, ties to even, as `JSON.parse` reads it; an infinity past the double range.
|
|
475
|
+
*/
|
|
217
476
|
f64(): f64 {
|
|
218
477
|
return this.reader().numberF64()
|
|
219
478
|
}
|
|
220
479
|
|
|
221
480
|
/**
|
|
222
|
-
* Sets the text to `text` when it is one JSON number and nothing else
|
|
223
|
-
*
|
|
481
|
+
* Sets the text to `text` when it is one JSON number and nothing else; any
|
|
482
|
+
* other text leaves the number as it was.
|
|
483
|
+
*
|
|
484
|
+
* @param text The new text: one JSON number, with no blank around it.
|
|
485
|
+
* @returns `true` when `text` was one JSON number and is now the text.
|
|
224
486
|
*/
|
|
225
487
|
setText(text: string): boolean {
|
|
226
488
|
const n: i32 = toI32(text.length)
|
|
@@ -253,7 +515,8 @@ export class JsonNumber {
|
|
|
253
515
|
* strings it decodes, the arrays it grows and the objects it makes.
|
|
254
516
|
*/
|
|
255
517
|
export class JsonDecoder {
|
|
256
|
-
|
|
518
|
+
/** The tokenizer over the input, under the caller's `JsonLimits`. */
|
|
519
|
+
private tok: JsonTokenizer
|
|
257
520
|
/** `JSON_DECODE_OK`, or the first `JSON_DECODE_*` or `JSON_TOK_ERR_*` code. */
|
|
258
521
|
failed: i32 = 0
|
|
259
522
|
/** The byte offset in the input of the token the error was found at; `-1` until one. */
|
|
@@ -273,8 +536,17 @@ export class JsonDecoder {
|
|
|
273
536
|
* the first token of an array element, read to see whether the array had
|
|
274
537
|
* ended, or a value `takeNull` found was not `null`.
|
|
275
538
|
*/
|
|
276
|
-
held: boolean = false
|
|
539
|
+
private held: boolean = false
|
|
277
540
|
|
|
541
|
+
/**
|
|
542
|
+
* A decoder over `src[off, off + len)`, with no error and no map key taken.
|
|
543
|
+
*
|
|
544
|
+
* @param src The bytes that hold the input.
|
|
545
|
+
* @param off Where the input starts in `src`.
|
|
546
|
+
* @param len How many bytes of `src` the input is.
|
|
547
|
+
* @param limits The tokenizer's limits; its `maxDepth`, 128 by default,
|
|
548
|
+
* bounds the recursion, one native frame a level.
|
|
549
|
+
*/
|
|
278
550
|
constructor(src: u8[], off: i32, len: i32, limits: JsonLimits) {
|
|
279
551
|
this.tok = new JsonTokenizer(src, off, len, limits)
|
|
280
552
|
}
|
|
@@ -282,6 +554,10 @@ export class JsonDecoder {
|
|
|
282
554
|
/**
|
|
283
555
|
* Starts over on `src[off, off + len)`, keeping the limits, `ignoreUnknown`
|
|
284
556
|
* and `maxMapKeys`, and clearing any error and the count of map keys taken.
|
|
557
|
+
*
|
|
558
|
+
* @param src The bytes that hold the next input.
|
|
559
|
+
* @param off Where it starts in `src`.
|
|
560
|
+
* @param len How many bytes of `src` it is.
|
|
285
561
|
*/
|
|
286
562
|
reset(src: u8[], off: i32, len: i32): void {
|
|
287
563
|
this.tok.reset(src, off, len)
|
|
@@ -291,21 +567,37 @@ export class JsonDecoder {
|
|
|
291
567
|
this.mapKeys = 0
|
|
292
568
|
}
|
|
293
569
|
|
|
294
|
-
/**
|
|
295
|
-
|
|
570
|
+
/**
|
|
571
|
+
* Records the first error; once one is recorded, this does nothing.
|
|
572
|
+
*
|
|
573
|
+
* @param code The `JSON_DECODE_*` or `JSON_TOK_ERR_*` code.
|
|
574
|
+
* @param at The byte offset of the token it was found at.
|
|
575
|
+
*/
|
|
576
|
+
private fail(code: i32, at: i32): void {
|
|
296
577
|
if (this.failed === JSON_DECODE_OK) {
|
|
297
578
|
this.failed = code
|
|
298
579
|
this.errorAt = at
|
|
299
580
|
}
|
|
300
581
|
}
|
|
301
582
|
|
|
302
|
-
/**
|
|
583
|
+
/**
|
|
584
|
+
* `JSON_DECODE_TYPE` at the current token, unless the read that found it
|
|
585
|
+
* failed already.
|
|
586
|
+
*
|
|
587
|
+
* @internal Called by name by the decoder the compiler writes for
|
|
588
|
+
* `jsonDecode`, src/emit-json.ts.
|
|
589
|
+
*/
|
|
303
590
|
wrongType(): void {
|
|
304
591
|
this.fail(JSON_DECODE_TYPE, this.tok.tokStart)
|
|
305
592
|
}
|
|
306
593
|
|
|
307
|
-
/**
|
|
308
|
-
|
|
594
|
+
/**
|
|
595
|
+
* Takes the next value's first token: the held token, when there is one,
|
|
596
|
+
* and the tokenizer's next otherwise.
|
|
597
|
+
*
|
|
598
|
+
* @returns Its `JSON_TOK_*` kind, or the negative code of the error, which it records.
|
|
599
|
+
*/
|
|
600
|
+
private take(): i32 {
|
|
309
601
|
if (this.failed !== JSON_DECODE_OK) {
|
|
310
602
|
return this.failed
|
|
311
603
|
}
|
|
@@ -320,7 +612,11 @@ export class JsonDecoder {
|
|
|
320
612
|
return k
|
|
321
613
|
}
|
|
322
614
|
|
|
323
|
-
/**
|
|
615
|
+
/**
|
|
616
|
+
* Takes the next value as the opening of an object.
|
|
617
|
+
*
|
|
618
|
+
* @returns `true` when it opens one; `false` with `JSON_DECODE_TYPE` for any other value, and after any error.
|
|
619
|
+
*/
|
|
324
620
|
beginObject(): boolean {
|
|
325
621
|
const k: i32 = this.take()
|
|
326
622
|
if (k === JSON_TOK_OBJECT_BEGIN) {
|
|
@@ -332,7 +628,11 @@ export class JsonDecoder {
|
|
|
332
628
|
return false
|
|
333
629
|
}
|
|
334
630
|
|
|
335
|
-
/**
|
|
631
|
+
/**
|
|
632
|
+
* Reads the next key of the open object, or its end.
|
|
633
|
+
*
|
|
634
|
+
* @returns `true` at a key; `false` at the object's end and after any error.
|
|
635
|
+
*/
|
|
336
636
|
nextField(): boolean {
|
|
337
637
|
if (this.failed !== JSON_DECODE_OK) {
|
|
338
638
|
return false
|
|
@@ -347,15 +647,25 @@ export class JsonDecoder {
|
|
|
347
647
|
return false
|
|
348
648
|
}
|
|
349
649
|
|
|
350
|
-
/**
|
|
650
|
+
/**
|
|
651
|
+
* Whether the current key, its escapes decoded, is `name`.
|
|
652
|
+
*
|
|
653
|
+
* @param name The field's name or its key on the wire.
|
|
654
|
+
* @returns `true` when the key's bytes are `name`'s UTF-8.
|
|
655
|
+
*
|
|
656
|
+
* @internal Called by name by the decoder the compiler writes for
|
|
657
|
+
* `jsonDecode`, src/emit-json.ts.
|
|
658
|
+
*/
|
|
351
659
|
keyIs(name: string): boolean {
|
|
352
660
|
return this.tok.keyEquals(name)
|
|
353
661
|
}
|
|
354
662
|
|
|
355
663
|
/**
|
|
356
664
|
* 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
|
|
665
|
+
* token is not a key or string: what the code written for a class switches on before
|
|
358
666
|
* it compares a key's bytes with a field's name.
|
|
667
|
+
*
|
|
668
|
+
* @returns The decoded length in bytes, or `-1`.
|
|
359
669
|
*/
|
|
360
670
|
keyLength(): i32 {
|
|
361
671
|
return this.tok.stringLength()
|
|
@@ -364,10 +674,13 @@ export class JsonDecoder {
|
|
|
364
674
|
/**
|
|
365
675
|
* Byte `at` of the current key when the key has no escape, so that its
|
|
366
676
|
* 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`
|
|
677
|
+
* an `at` outside it, and when the current token is not a key or string. A `-1`
|
|
368
678
|
* matches no byte of a field's name, and the code written for a class then
|
|
369
679
|
* compares the key with each name of its length through `keyIs`, which
|
|
370
680
|
* decodes the escapes.
|
|
681
|
+
*
|
|
682
|
+
* @param at The byte's index in the key.
|
|
683
|
+
* @returns The byte, `0` to `255`, or `-1`.
|
|
371
684
|
*/
|
|
372
685
|
keyByte(at: i32): i32 {
|
|
373
686
|
const tok: JsonTokenizer = this.tok
|
|
@@ -390,6 +703,8 @@ export class JsonDecoder {
|
|
|
390
703
|
* read, so the keys of a map inside another map count before the outer
|
|
391
704
|
* key's value is whole, and the total never passes the limit.
|
|
392
705
|
* `JSON_DECODE_MAP_LIMIT` at the key when they may not.
|
|
706
|
+
*
|
|
707
|
+
* @returns `true` when the key is counted; `false` with `JSON_DECODE_MAP_LIMIT`.
|
|
393
708
|
*/
|
|
394
709
|
mapRoom(): boolean {
|
|
395
710
|
if (this.mapKeys < this.maxMapKeys) {
|
|
@@ -400,7 +715,11 @@ export class JsonDecoder {
|
|
|
400
715
|
return false
|
|
401
716
|
}
|
|
402
717
|
|
|
403
|
-
/**
|
|
718
|
+
/**
|
|
719
|
+
* The current key, decoded: a `Map` field's key.
|
|
720
|
+
*
|
|
721
|
+
* @returns A new string, or `""` when the current token is not a key or string.
|
|
722
|
+
*/
|
|
404
723
|
key(): string {
|
|
405
724
|
return this.text()
|
|
406
725
|
}
|
|
@@ -408,8 +727,9 @@ export class JsonDecoder {
|
|
|
408
727
|
/**
|
|
409
728
|
* The current key or string as a new string, made by one
|
|
410
729
|
* `String.fromBytes`: straight from the input when it has no escape, and
|
|
411
|
-
* from its unescaped bytes when it has one.
|
|
412
|
-
*
|
|
730
|
+
* from its unescaped bytes when it has one.
|
|
731
|
+
*
|
|
732
|
+
* @returns A new string, or `""` when the current token is neither.
|
|
413
733
|
*/
|
|
414
734
|
text(): string {
|
|
415
735
|
const tok: JsonTokenizer = this.tok
|
|
@@ -430,7 +750,14 @@ export class JsonDecoder {
|
|
|
430
750
|
return String.fromBytes(bytes, 0, n)
|
|
431
751
|
}
|
|
432
752
|
|
|
433
|
-
/**
|
|
753
|
+
/**
|
|
754
|
+
* Handles a key that names no field: with `ignoreUnknown` set, steps over
|
|
755
|
+
* the key's value, checked but not kept, and otherwise records
|
|
756
|
+
* `JSON_DECODE_UNKNOWN` at the key.
|
|
757
|
+
*
|
|
758
|
+
* @internal Called by name by the decoder the compiler writes for
|
|
759
|
+
* `jsonDecode`, src/emit-json.ts.
|
|
760
|
+
*/
|
|
434
761
|
unknownField(): void {
|
|
435
762
|
if (this.failed !== JSON_DECODE_OK) {
|
|
436
763
|
return
|
|
@@ -445,17 +772,37 @@ export class JsonDecoder {
|
|
|
445
772
|
}
|
|
446
773
|
}
|
|
447
774
|
|
|
448
|
-
/**
|
|
775
|
+
/**
|
|
776
|
+
* Records `JSON_DECODE_DUPLICATE` at the current key, which the object
|
|
777
|
+
* already gave.
|
|
778
|
+
*
|
|
779
|
+
* @internal Called by name by the decoder the compiler writes for
|
|
780
|
+
* `jsonDecode`, src/emit-json.ts.
|
|
781
|
+
*/
|
|
449
782
|
duplicateField(): void {
|
|
450
783
|
this.fail(JSON_DECODE_DUPLICATE, this.tok.tokStart)
|
|
451
784
|
}
|
|
452
785
|
|
|
453
|
-
/**
|
|
786
|
+
/**
|
|
787
|
+
* Records `JSON_DECODE_MISSING` at the `}` of the object just closed,
|
|
788
|
+
* which left out a required field.
|
|
789
|
+
*
|
|
790
|
+
* @internal Called by name by the decoder the compiler writes for
|
|
791
|
+
* `jsonDecode`, src/emit-json.ts.
|
|
792
|
+
*/
|
|
454
793
|
missingField(): void {
|
|
455
794
|
this.fail(JSON_DECODE_MISSING, this.tok.tokStart)
|
|
456
795
|
}
|
|
457
796
|
|
|
458
|
-
/**
|
|
797
|
+
/**
|
|
798
|
+
* Ends the decoding after the top-level value: nothing but blanks may
|
|
799
|
+
* follow it, and anything else is `JSON_DECODE_TYPE`.
|
|
800
|
+
*
|
|
801
|
+
* @returns `failed`: `JSON_DECODE_OK`, or the first error's code.
|
|
802
|
+
*
|
|
803
|
+
* @internal Called by name by the decoder the compiler writes for
|
|
804
|
+
* `jsonDecode`, src/emit-json.ts.
|
|
805
|
+
*/
|
|
459
806
|
finish(): i32 {
|
|
460
807
|
if (this.failed === JSON_DECODE_OK) {
|
|
461
808
|
const k: i32 = this.tok.next()
|
|
@@ -468,7 +815,15 @@ export class JsonDecoder {
|
|
|
468
815
|
return this.failed
|
|
469
816
|
}
|
|
470
817
|
|
|
471
|
-
/**
|
|
818
|
+
/**
|
|
819
|
+
* Takes the next value when it is `null`, and holds it for the read that
|
|
820
|
+
* follows when it is not.
|
|
821
|
+
*
|
|
822
|
+
* @returns `true` when it was `null`; `false` otherwise and after any error.
|
|
823
|
+
*
|
|
824
|
+
* @internal Called by name by the decoder the compiler writes for
|
|
825
|
+
* `jsonDecode`, src/emit-json.ts.
|
|
826
|
+
*/
|
|
472
827
|
takeNull(): boolean {
|
|
473
828
|
const k: i32 = this.take()
|
|
474
829
|
if (k === JSON_TOK_NULL) {
|
|
@@ -480,7 +835,11 @@ export class JsonDecoder {
|
|
|
480
835
|
return false
|
|
481
836
|
}
|
|
482
837
|
|
|
483
|
-
/**
|
|
838
|
+
/**
|
|
839
|
+
* Takes the next value as the opening of an array.
|
|
840
|
+
*
|
|
841
|
+
* @returns `true` when it opens one; `false` with `JSON_DECODE_TYPE` for any other value, and after any error.
|
|
842
|
+
*/
|
|
484
843
|
beginArray(): boolean {
|
|
485
844
|
const k: i32 = this.take()
|
|
486
845
|
if (k === JSON_TOK_ARRAY_BEGIN) {
|
|
@@ -492,7 +851,12 @@ export class JsonDecoder {
|
|
|
492
851
|
return false
|
|
493
852
|
}
|
|
494
853
|
|
|
495
|
-
/**
|
|
854
|
+
/**
|
|
855
|
+
* Whether the open array has another element, whose first token is then
|
|
856
|
+
* held for the read that follows.
|
|
857
|
+
*
|
|
858
|
+
* @returns `true` at an element; `false` at the array's end and after any error.
|
|
859
|
+
*/
|
|
496
860
|
nextElement(): boolean {
|
|
497
861
|
if (this.failed !== JSON_DECODE_OK) {
|
|
498
862
|
return false
|
|
@@ -514,8 +878,15 @@ export class JsonDecoder {
|
|
|
514
878
|
* with an error. Every `i64` it answers is the tokenizer's or made with
|
|
515
879
|
* `toI64`, so that under Node, where an `i64` is a BigInt, what a caller
|
|
516
880
|
* computes with it never mixes a BigInt with a number.
|
|
881
|
+
*
|
|
882
|
+
* @param lo The least value accepted when `bounded`.
|
|
883
|
+
* @param hi The greatest value accepted when `bounded`.
|
|
884
|
+
* @param bounded Whether `lo` and `hi` apply.
|
|
885
|
+
* @returns The value; `0` with `JSON_DECODE_TYPE` for a value that is not
|
|
886
|
+
* a number, and with `JSON_DECODE_RANGE` for a fraction, an exponent or
|
|
887
|
+
* a value out of bounds.
|
|
517
888
|
*/
|
|
518
|
-
integer(lo: i64, hi: i64, bounded: boolean): i64 {
|
|
889
|
+
private integer(lo: i64, hi: i64, bounded: boolean): i64 {
|
|
519
890
|
const k: i32 = this.take()
|
|
520
891
|
if (k !== JSON_TOK_NUMBER) {
|
|
521
892
|
if (k >= 0) {
|
|
@@ -535,13 +906,25 @@ export class JsonDecoder {
|
|
|
535
906
|
return v
|
|
536
907
|
}
|
|
537
908
|
|
|
909
|
+
/**
|
|
910
|
+
* The next value as an `i32`: an integer written without a fraction or an
|
|
911
|
+
* exponent, from -2^31 to 2^31 - 1. A numeric enum is read with it too.
|
|
912
|
+
*
|
|
913
|
+
* @returns The value; `0` with `JSON_DECODE_TYPE` or `JSON_DECODE_RANGE`.
|
|
914
|
+
*/
|
|
538
915
|
i32(): i32 {
|
|
539
916
|
const lo: i64 = -2147483648
|
|
540
917
|
const hi: i64 = 2147483647
|
|
541
918
|
return toI32(this.integer(lo, hi, true))
|
|
542
919
|
}
|
|
543
920
|
|
|
544
|
-
/**
|
|
921
|
+
/**
|
|
922
|
+
* The next value as a `u32`: an integer written without a fraction or an
|
|
923
|
+
* exponent, from 0 to 2^32 - 1. The bounds are compared and never computed
|
|
924
|
+
* with, so under Node a number beside a BigInt is fine.
|
|
925
|
+
*
|
|
926
|
+
* @returns The value; `0` with `JSON_DECODE_TYPE` or `JSON_DECODE_RANGE`.
|
|
927
|
+
*/
|
|
545
928
|
u32(): u32 {
|
|
546
929
|
const lo: i64 = 0
|
|
547
930
|
const hi: i64 = 4294967295
|
|
@@ -550,7 +933,13 @@ export class JsonDecoder {
|
|
|
550
933
|
return toU32(toF64(this.integer(lo, hi, true)))
|
|
551
934
|
}
|
|
552
935
|
|
|
553
|
-
/**
|
|
936
|
+
/**
|
|
937
|
+
* The next value as an `i64`: an integer written without a fraction or an
|
|
938
|
+
* exponent. Any integer `isInteger` accepts fits an `i64`, so there is no
|
|
939
|
+
* range to check.
|
|
940
|
+
*
|
|
941
|
+
* @returns The value; `0` with `JSON_DECODE_TYPE` or `JSON_DECODE_RANGE`.
|
|
942
|
+
*/
|
|
554
943
|
i64(): i64 {
|
|
555
944
|
return this.integer(toI64(0), toI64(0), false)
|
|
556
945
|
}
|
|
@@ -559,7 +948,9 @@ export class JsonDecoder {
|
|
|
559
948
|
* The next value as a `u64`: an integer written in digits alone, with no
|
|
560
949
|
* sign, fraction or exponent, up to 2^64 - 1, read through the tokenizer's
|
|
561
950
|
* `numberU64`. A negative number, `-0` included, and one past 2^64 - 1 are
|
|
562
|
-
* `JSON_DECODE_RANGE`, as a fraction is
|
|
951
|
+
* `JSON_DECODE_RANGE`, as a fraction is.
|
|
952
|
+
*
|
|
953
|
+
* @returns The value; `0` with `JSON_DECODE_TYPE` or `JSON_DECODE_RANGE`.
|
|
563
954
|
*/
|
|
564
955
|
u64(): u64 {
|
|
565
956
|
const k: i32 = this.take()
|
|
@@ -579,7 +970,10 @@ export class JsonDecoder {
|
|
|
579
970
|
/**
|
|
580
971
|
* The next value as a `JsonNumber`: its text, copied into `n`'s bytes
|
|
581
972
|
* exactly as the input writes it. Anything but a number is
|
|
582
|
-
* `JSON_DECODE_TYPE`, and `n` keeps the number it held.
|
|
973
|
+
* `JSON_DECODE_TYPE`, and `n` keeps the number it held. Its bytes are
|
|
974
|
+
* reused, and grown only for a longer number.
|
|
975
|
+
*
|
|
976
|
+
* @param n The number to copy the text into.
|
|
583
977
|
*/
|
|
584
978
|
number(n: JsonNumber): void {
|
|
585
979
|
const k: i32 = this.take()
|
|
@@ -608,6 +1002,12 @@ export class JsonDecoder {
|
|
|
608
1002
|
n.count = copied
|
|
609
1003
|
}
|
|
610
1004
|
|
|
1005
|
+
/**
|
|
1006
|
+
* The next value as an `f64`: any number, the nearest double, and past the
|
|
1007
|
+
* double range an infinity.
|
|
1008
|
+
*
|
|
1009
|
+
* @returns The value; `0` with `JSON_DECODE_TYPE` for a value that is not a number.
|
|
1010
|
+
*/
|
|
611
1011
|
f64(): f64 {
|
|
612
1012
|
const k: i32 = this.take()
|
|
613
1013
|
if (k === JSON_TOK_NUMBER) {
|
|
@@ -619,6 +1019,11 @@ export class JsonDecoder {
|
|
|
619
1019
|
return 0
|
|
620
1020
|
}
|
|
621
1021
|
|
|
1022
|
+
/**
|
|
1023
|
+
* The next value as a `boolean`.
|
|
1024
|
+
*
|
|
1025
|
+
* @returns `true` for `true`; `false` for `false`, and with `JSON_DECODE_TYPE` for any other value.
|
|
1026
|
+
*/
|
|
622
1027
|
boolean(): boolean {
|
|
623
1028
|
const k: i32 = this.take()
|
|
624
1029
|
if (k === JSON_TOK_TRUE) {
|
|
@@ -632,8 +1037,10 @@ export class JsonDecoder {
|
|
|
632
1037
|
|
|
633
1038
|
/**
|
|
634
1039
|
* 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
|
|
636
|
-
*
|
|
1040
|
+
* to it, which allocates nothing, and a new string otherwise.
|
|
1041
|
+
*
|
|
1042
|
+
* @param cur The string the field holds now.
|
|
1043
|
+
* @returns The string read; `cur` with `JSON_DECODE_TYPE` for a value that is not a string.
|
|
637
1044
|
*/
|
|
638
1045
|
string(cur: string): string {
|
|
639
1046
|
const k: i32 = this.take()
|
|
@@ -653,6 +1060,18 @@ export class JsonDecoder {
|
|
|
653
1060
|
// array already has, or pushed past its end, and what is left over is
|
|
654
1061
|
// popped, so an array of the same length grows nothing.
|
|
655
1062
|
|
|
1063
|
+
/**
|
|
1064
|
+
* The next value as an array whose every element is an `i32`, read as `i32()` reads one, into `xs`.
|
|
1065
|
+
*
|
|
1066
|
+
* Each element overwrites the slot `xs` already has or is pushed past its
|
|
1067
|
+
* end, and the slots left over are popped, so an array no longer than `xs`
|
|
1068
|
+
* allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
|
|
1069
|
+
* with the tokenizer's code when the input is malformed, and leaves `xs`
|
|
1070
|
+
* untouched; a failure inside the array leaves its contents unspecified.
|
|
1071
|
+
*
|
|
1072
|
+
* @param xs The array the field holds now, whose slots are reused.
|
|
1073
|
+
* @returns `xs`, holding the input's elements.
|
|
1074
|
+
*/
|
|
656
1075
|
i32Array(xs: i32[]): i32[] {
|
|
657
1076
|
if (!this.beginArray()) {
|
|
658
1077
|
return xs
|
|
@@ -673,6 +1092,18 @@ export class JsonDecoder {
|
|
|
673
1092
|
return xs
|
|
674
1093
|
}
|
|
675
1094
|
|
|
1095
|
+
/**
|
|
1096
|
+
* The next value as an array whose every element is a `u32`, read as `u32()` reads one, into `xs`.
|
|
1097
|
+
*
|
|
1098
|
+
* Each element overwrites the slot `xs` already has or is pushed past its
|
|
1099
|
+
* end, and the slots left over are popped, so an array no longer than `xs`
|
|
1100
|
+
* allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
|
|
1101
|
+
* with the tokenizer's code when the input is malformed, and leaves `xs`
|
|
1102
|
+
* untouched; a failure inside the array leaves its contents unspecified.
|
|
1103
|
+
*
|
|
1104
|
+
* @param xs The array the field holds now, whose slots are reused.
|
|
1105
|
+
* @returns `xs`, holding the input's elements.
|
|
1106
|
+
*/
|
|
676
1107
|
u32Array(xs: u32[]): u32[] {
|
|
677
1108
|
if (!this.beginArray()) {
|
|
678
1109
|
return xs
|
|
@@ -693,6 +1124,18 @@ export class JsonDecoder {
|
|
|
693
1124
|
return xs
|
|
694
1125
|
}
|
|
695
1126
|
|
|
1127
|
+
/**
|
|
1128
|
+
* The next value as an array whose every element is an `i64`, read as `i64()` reads one, into `xs`.
|
|
1129
|
+
*
|
|
1130
|
+
* Each element overwrites the slot `xs` already has or is pushed past its
|
|
1131
|
+
* end, and the slots left over are popped, so an array no longer than `xs`
|
|
1132
|
+
* allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
|
|
1133
|
+
* with the tokenizer's code when the input is malformed, and leaves `xs`
|
|
1134
|
+
* untouched; a failure inside the array leaves its contents unspecified.
|
|
1135
|
+
*
|
|
1136
|
+
* @param xs The array the field holds now, whose slots are reused.
|
|
1137
|
+
* @returns `xs`, holding the input's elements.
|
|
1138
|
+
*/
|
|
696
1139
|
i64Array(xs: i64[]): i64[] {
|
|
697
1140
|
if (!this.beginArray()) {
|
|
698
1141
|
return xs
|
|
@@ -713,6 +1156,18 @@ export class JsonDecoder {
|
|
|
713
1156
|
return xs
|
|
714
1157
|
}
|
|
715
1158
|
|
|
1159
|
+
/**
|
|
1160
|
+
* The next value as an array whose every element is a `u64`, read as `u64()` reads one, into `xs`.
|
|
1161
|
+
*
|
|
1162
|
+
* Each element overwrites the slot `xs` already has or is pushed past its
|
|
1163
|
+
* end, and the slots left over are popped, so an array no longer than `xs`
|
|
1164
|
+
* allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
|
|
1165
|
+
* with the tokenizer's code when the input is malformed, and leaves `xs`
|
|
1166
|
+
* untouched; a failure inside the array leaves its contents unspecified.
|
|
1167
|
+
*
|
|
1168
|
+
* @param xs The array the field holds now, whose slots are reused.
|
|
1169
|
+
* @returns `xs`, holding the input's elements.
|
|
1170
|
+
*/
|
|
716
1171
|
u64Array(xs: u64[]): u64[] {
|
|
717
1172
|
if (!this.beginArray()) {
|
|
718
1173
|
return xs
|
|
@@ -733,6 +1188,18 @@ export class JsonDecoder {
|
|
|
733
1188
|
return xs
|
|
734
1189
|
}
|
|
735
1190
|
|
|
1191
|
+
/**
|
|
1192
|
+
* The next value as an array whose every element is an `f64`, read as `f64()` reads one, into `xs`.
|
|
1193
|
+
*
|
|
1194
|
+
* Each element overwrites the slot `xs` already has or is pushed past its
|
|
1195
|
+
* end, and the slots left over are popped, so an array no longer than `xs`
|
|
1196
|
+
* allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
|
|
1197
|
+
* with the tokenizer's code when the input is malformed, and leaves `xs`
|
|
1198
|
+
* untouched; a failure inside the array leaves its contents unspecified.
|
|
1199
|
+
*
|
|
1200
|
+
* @param xs The array the field holds now, whose slots are reused.
|
|
1201
|
+
* @returns `xs`, holding the input's elements.
|
|
1202
|
+
*/
|
|
736
1203
|
f64Array(xs: f64[]): f64[] {
|
|
737
1204
|
if (!this.beginArray()) {
|
|
738
1205
|
return xs
|
|
@@ -753,6 +1220,18 @@ export class JsonDecoder {
|
|
|
753
1220
|
return xs
|
|
754
1221
|
}
|
|
755
1222
|
|
|
1223
|
+
/**
|
|
1224
|
+
* The next value as an array whose every element is a `boolean`, into `xs`.
|
|
1225
|
+
*
|
|
1226
|
+
* Each element overwrites the slot `xs` already has or is pushed past its
|
|
1227
|
+
* end, and the slots left over are popped, so an array no longer than `xs`
|
|
1228
|
+
* allocates nothing. A value that is not an array fails with `JSON_DECODE_TYPE`, or
|
|
1229
|
+
* with the tokenizer's code when the input is malformed, and leaves `xs`
|
|
1230
|
+
* untouched; a failure inside the array leaves its contents unspecified.
|
|
1231
|
+
*
|
|
1232
|
+
* @param xs The array the field holds now, whose slots are reused.
|
|
1233
|
+
* @returns `xs`, holding the input's elements.
|
|
1234
|
+
*/
|
|
756
1235
|
booleanArray(xs: boolean[]): boolean[] {
|
|
757
1236
|
if (!this.beginArray()) {
|
|
758
1237
|
return xs
|
|
@@ -773,6 +1252,19 @@ export class JsonDecoder {
|
|
|
773
1252
|
return xs
|
|
774
1253
|
}
|
|
775
1254
|
|
|
1255
|
+
/**
|
|
1256
|
+
* 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`.
|
|
1257
|
+
*
|
|
1258
|
+
* Each element overwrites the slot `xs` already has or is pushed past its
|
|
1259
|
+
* end, and the slots left over are popped, so an array no longer than `xs`
|
|
1260
|
+
* grows nothing. Only the container is reused: an element whose text
|
|
1261
|
+
* changed is a new string. A value that is not an array fails with `JSON_DECODE_TYPE`, or
|
|
1262
|
+
* with the tokenizer's code when the input is malformed, and leaves `xs`
|
|
1263
|
+
* untouched; a failure inside the array leaves its contents unspecified.
|
|
1264
|
+
*
|
|
1265
|
+
* @param xs The array the field holds now, whose slots are reused.
|
|
1266
|
+
* @returns `xs`, holding the input's elements.
|
|
1267
|
+
*/
|
|
776
1268
|
stringArray(xs: string[]): string[] {
|
|
777
1269
|
if (!this.beginArray()) {
|
|
778
1270
|
return xs
|
|
@@ -798,6 +1290,13 @@ export class JsonDecoder {
|
|
|
798
1290
|
return xs
|
|
799
1291
|
}
|
|
800
1292
|
|
|
1293
|
+
/**
|
|
1294
|
+
* The next value as `null`, or as an array `i32Array` reads, into `xs`
|
|
1295
|
+
* when it is an array and into a new one when it is `null`.
|
|
1296
|
+
*
|
|
1297
|
+
* @param xs The array the field holds now, or `null`.
|
|
1298
|
+
* @returns `null` for `null`, and otherwise the array read.
|
|
1299
|
+
*/
|
|
801
1300
|
i32ArrayOrNull(xs: i32[] | null): i32[] | null {
|
|
802
1301
|
if (this.takeNull()) {
|
|
803
1302
|
return null
|
|
@@ -809,6 +1308,13 @@ export class JsonDecoder {
|
|
|
809
1308
|
return this.i32Array(fresh)
|
|
810
1309
|
}
|
|
811
1310
|
|
|
1311
|
+
/**
|
|
1312
|
+
* The next value as `null`, or as an array `u32Array` reads, into `xs`
|
|
1313
|
+
* when it is an array and into a new one when it is `null`.
|
|
1314
|
+
*
|
|
1315
|
+
* @param xs The array the field holds now, or `null`.
|
|
1316
|
+
* @returns `null` for `null`, and otherwise the array read.
|
|
1317
|
+
*/
|
|
812
1318
|
u32ArrayOrNull(xs: u32[] | null): u32[] | null {
|
|
813
1319
|
if (this.takeNull()) {
|
|
814
1320
|
return null
|
|
@@ -820,6 +1326,13 @@ export class JsonDecoder {
|
|
|
820
1326
|
return this.u32Array(fresh)
|
|
821
1327
|
}
|
|
822
1328
|
|
|
1329
|
+
/**
|
|
1330
|
+
* The next value as `null`, or as an array `i64Array` reads, into `xs`
|
|
1331
|
+
* when it is an array and into a new one when it is `null`.
|
|
1332
|
+
*
|
|
1333
|
+
* @param xs The array the field holds now, or `null`.
|
|
1334
|
+
* @returns `null` for `null`, and otherwise the array read.
|
|
1335
|
+
*/
|
|
823
1336
|
i64ArrayOrNull(xs: i64[] | null): i64[] | null {
|
|
824
1337
|
if (this.takeNull()) {
|
|
825
1338
|
return null
|
|
@@ -831,6 +1344,13 @@ export class JsonDecoder {
|
|
|
831
1344
|
return this.i64Array(fresh)
|
|
832
1345
|
}
|
|
833
1346
|
|
|
1347
|
+
/**
|
|
1348
|
+
* The next value as `null`, or as an array `u64Array` reads, into `xs`
|
|
1349
|
+
* when it is an array and into a new one when it is `null`.
|
|
1350
|
+
*
|
|
1351
|
+
* @param xs The array the field holds now, or `null`.
|
|
1352
|
+
* @returns `null` for `null`, and otherwise the array read.
|
|
1353
|
+
*/
|
|
834
1354
|
u64ArrayOrNull(xs: u64[] | null): u64[] | null {
|
|
835
1355
|
if (this.takeNull()) {
|
|
836
1356
|
return null
|
|
@@ -842,6 +1362,13 @@ export class JsonDecoder {
|
|
|
842
1362
|
return this.u64Array(fresh)
|
|
843
1363
|
}
|
|
844
1364
|
|
|
1365
|
+
/**
|
|
1366
|
+
* The next value as `null`, or as an array `f64Array` reads, into `xs`
|
|
1367
|
+
* when it is an array and into a new one when it is `null`.
|
|
1368
|
+
*
|
|
1369
|
+
* @param xs The array the field holds now, or `null`.
|
|
1370
|
+
* @returns `null` for `null`, and otherwise the array read.
|
|
1371
|
+
*/
|
|
845
1372
|
f64ArrayOrNull(xs: f64[] | null): f64[] | null {
|
|
846
1373
|
if (this.takeNull()) {
|
|
847
1374
|
return null
|
|
@@ -853,6 +1380,13 @@ export class JsonDecoder {
|
|
|
853
1380
|
return this.f64Array(fresh)
|
|
854
1381
|
}
|
|
855
1382
|
|
|
1383
|
+
/**
|
|
1384
|
+
* The next value as `null`, or as an array `booleanArray` reads, into `xs`
|
|
1385
|
+
* when it is an array and into a new one when it is `null`.
|
|
1386
|
+
*
|
|
1387
|
+
* @param xs The array the field holds now, or `null`.
|
|
1388
|
+
* @returns `null` for `null`, and otherwise the array read.
|
|
1389
|
+
*/
|
|
856
1390
|
booleanArrayOrNull(xs: boolean[] | null): boolean[] | null {
|
|
857
1391
|
if (this.takeNull()) {
|
|
858
1392
|
return null
|
|
@@ -864,6 +1398,13 @@ export class JsonDecoder {
|
|
|
864
1398
|
return this.booleanArray(fresh)
|
|
865
1399
|
}
|
|
866
1400
|
|
|
1401
|
+
/**
|
|
1402
|
+
* The next value as `null`, or as an array `stringArray` reads, into `xs`
|
|
1403
|
+
* when it is an array and into a new one when it is `null`.
|
|
1404
|
+
*
|
|
1405
|
+
* @param xs The array the field holds now, or `null`.
|
|
1406
|
+
* @returns `null` for `null`, and otherwise the array read.
|
|
1407
|
+
*/
|
|
867
1408
|
stringArrayOrNull(xs: string[] | null): string[] | null {
|
|
868
1409
|
if (this.takeNull()) {
|
|
869
1410
|
return null
|
|
@@ -877,18 +1418,37 @@ export class JsonDecoder {
|
|
|
877
1418
|
}
|
|
878
1419
|
|
|
879
1420
|
/**
|
|
880
|
-
* Decodes the JSON object `d` holds
|
|
881
|
-
*
|
|
882
|
-
*
|
|
1421
|
+
* Decodes the JSON object `d` holds, the whole input, into `out`. The
|
|
1422
|
+
* compiler writes the code from `T`'s fields at every call, in the module that
|
|
1423
|
+
* makes it; under Node this body ends the program with a panic, because the
|
|
1424
|
+
* types are erased. Called as `jsonDecode(d, out)`, with `T` the class of `out`;
|
|
1425
|
+
* every field of every class it reaches must be a type the module header lists,
|
|
1426
|
+
* and a class the decoder makes must have a constructor with no parameters.
|
|
1427
|
+
* The top-level value must be an object, or the answer is `JSON_DECODE_TYPE`,
|
|
1428
|
+
* and nothing but blanks may follow it. A call inside a `using a = arena()`
|
|
1429
|
+
* block is refused (NL2424).
|
|
1430
|
+
*
|
|
1431
|
+
* @param _d The decoder over the input; its `failed` and `errorAt` say what
|
|
1432
|
+
* went wrong, and `reset` starts it over.
|
|
1433
|
+
* @param _out The object to decode into: a declared class, whose fields,
|
|
1434
|
+
* nested objects and arrays are reused.
|
|
1435
|
+
* @returns `d.failed`: `JSON_DECODE_OK`, or the first error's code, a
|
|
1436
|
+
* `JSON_DECODE_*` or the tokenizer's `JSON_TOK_ERR_*`.
|
|
883
1437
|
*/
|
|
884
1438
|
export const jsonDecode = <T>(_d: JsonDecoder, _out: T): i32 => {
|
|
885
1439
|
panic(JSON_TYPED_ERASED)
|
|
886
1440
|
}
|
|
887
1441
|
|
|
888
1442
|
/**
|
|
889
|
-
* Writes `value` to `w` as one JSON object, its fields in declaration order
|
|
890
|
-
*
|
|
891
|
-
*
|
|
1443
|
+
* Writes `value` to `w` as one JSON object, its fields in declaration order,
|
|
1444
|
+
* the text `JSON.stringify` writes for it, in the writer's modes. The compiler
|
|
1445
|
+
* writes the code from `T`'s fields at every call; under Node this body ends
|
|
1446
|
+
* the program with a panic, because the types are erased. A full buffer is
|
|
1447
|
+
* the writer's `JSON_WRITE_FULL`, and a value deeper than 64 levels, or one
|
|
1448
|
+
* that holds itself, its `JSON_WRITE_TOO_DEEP`.
|
|
1449
|
+
*
|
|
1450
|
+
* @param _w The writer, whose sticky code reports any failure.
|
|
1451
|
+
* @param _value The object to write: a declared class.
|
|
892
1452
|
*/
|
|
893
1453
|
export const jsonEncode = <T>(_w: JsonWriter, _value: T): void => {
|
|
894
1454
|
panic(JSON_TYPED_ERASED)
|
|
@@ -898,6 +1458,15 @@ export const jsonEncode = <T>(_w: JsonWriter, _value: T): void => {
|
|
|
898
1458
|
// because the code the compiler writes calls them from the module that
|
|
899
1459
|
// encodes, which may not be this one.
|
|
900
1460
|
|
|
1461
|
+
/**
|
|
1462
|
+
* Writes a `string | null` field's value: `null`, or the string.
|
|
1463
|
+
*
|
|
1464
|
+
* @param w The writer.
|
|
1465
|
+
* @param s The value.
|
|
1466
|
+
*
|
|
1467
|
+
* @internal Called by name by the code the compiler writes for `jsonEncode`,
|
|
1468
|
+
* src/json-typed.ts `jsonEncodeHelper`.
|
|
1469
|
+
*/
|
|
901
1470
|
export const jsonEncodeStringOrNull = (w: JsonWriter, s: string | null): void => {
|
|
902
1471
|
if (s === null) {
|
|
903
1472
|
w.nullValue()
|
|
@@ -907,10 +1476,13 @@ export const jsonEncodeStringOrNull = (w: JsonWriter, s: string | null): void =>
|
|
|
907
1476
|
}
|
|
908
1477
|
|
|
909
1478
|
/**
|
|
910
|
-
*
|
|
1479
|
+
* Writes a `u64` as its digits: at most 20 of them, written with `raw` from an
|
|
911
1480
|
* array of this call's own that nothing keeps. A value past 2^63 has no
|
|
912
1481
|
* `i64` to go through the writer's `i64` as, so every `u64` takes this one
|
|
913
1482
|
* loop. Every `u64` here is built with `toU64`, a BigInt under Node.
|
|
1483
|
+
*
|
|
1484
|
+
* @param w The writer.
|
|
1485
|
+
* @param v The value.
|
|
914
1486
|
*/
|
|
915
1487
|
export const jsonEncodeU64 = (w: JsonWriter, v: u64): void => {
|
|
916
1488
|
const ten: u64 = toU64(10)
|
|
@@ -929,12 +1501,23 @@ export const jsonEncodeU64 = (w: JsonWriter, v: u64): void => {
|
|
|
929
1501
|
w.raw(digits, first, toI32(digits.length) - first)
|
|
930
1502
|
}
|
|
931
1503
|
|
|
932
|
-
/**
|
|
1504
|
+
/**
|
|
1505
|
+
* Writes a `JsonNumber` as the text it holds, byte for byte.
|
|
1506
|
+
*
|
|
1507
|
+
* @param w The writer.
|
|
1508
|
+
* @param n The number.
|
|
1509
|
+
*/
|
|
933
1510
|
export const jsonEncodeNumber = (w: JsonWriter, n: JsonNumber): void => {
|
|
934
1511
|
const start: i32 = 0
|
|
935
1512
|
w.raw(n.bytes, start, n.count)
|
|
936
1513
|
}
|
|
937
1514
|
|
|
1515
|
+
/**
|
|
1516
|
+
* Writes an array whose every element is an `i32`.
|
|
1517
|
+
*
|
|
1518
|
+
* @param w The writer.
|
|
1519
|
+
* @param xs The elements, in order.
|
|
1520
|
+
*/
|
|
938
1521
|
export const jsonEncodeI32Array = (w: JsonWriter, xs: i32[]): void => {
|
|
939
1522
|
w.beginArray()
|
|
940
1523
|
for (const x of xs) {
|
|
@@ -943,6 +1526,12 @@ export const jsonEncodeI32Array = (w: JsonWriter, xs: i32[]): void => {
|
|
|
943
1526
|
w.endArray()
|
|
944
1527
|
}
|
|
945
1528
|
|
|
1529
|
+
/**
|
|
1530
|
+
* Writes an array whose every element is a `u32`.
|
|
1531
|
+
*
|
|
1532
|
+
* @param w The writer.
|
|
1533
|
+
* @param xs The elements, in order.
|
|
1534
|
+
*/
|
|
946
1535
|
export const jsonEncodeU32Array = (w: JsonWriter, xs: u32[]): void => {
|
|
947
1536
|
w.beginArray()
|
|
948
1537
|
for (const x of xs) {
|
|
@@ -951,6 +1540,12 @@ export const jsonEncodeU32Array = (w: JsonWriter, xs: u32[]): void => {
|
|
|
951
1540
|
w.endArray()
|
|
952
1541
|
}
|
|
953
1542
|
|
|
1543
|
+
/**
|
|
1544
|
+
* Writes an array whose every element is an `i64`.
|
|
1545
|
+
*
|
|
1546
|
+
* @param w The writer.
|
|
1547
|
+
* @param xs The elements, in order.
|
|
1548
|
+
*/
|
|
954
1549
|
export const jsonEncodeI64Array = (w: JsonWriter, xs: i64[]): void => {
|
|
955
1550
|
w.beginArray()
|
|
956
1551
|
for (const x of xs) {
|
|
@@ -959,6 +1554,12 @@ export const jsonEncodeI64Array = (w: JsonWriter, xs: i64[]): void => {
|
|
|
959
1554
|
w.endArray()
|
|
960
1555
|
}
|
|
961
1556
|
|
|
1557
|
+
/**
|
|
1558
|
+
* Writes an array whose every element is a `u64`, each as `jsonEncodeU64` writes it.
|
|
1559
|
+
*
|
|
1560
|
+
* @param w The writer.
|
|
1561
|
+
* @param xs The elements, in order.
|
|
1562
|
+
*/
|
|
962
1563
|
export const jsonEncodeU64Array = (w: JsonWriter, xs: u64[]): void => {
|
|
963
1564
|
w.beginArray()
|
|
964
1565
|
for (const x of xs) {
|
|
@@ -967,6 +1568,12 @@ export const jsonEncodeU64Array = (w: JsonWriter, xs: u64[]): void => {
|
|
|
967
1568
|
w.endArray()
|
|
968
1569
|
}
|
|
969
1570
|
|
|
1571
|
+
/**
|
|
1572
|
+
* Writes an array whose every element is an `f64`, a NaN or an infinity as the writer writes one.
|
|
1573
|
+
*
|
|
1574
|
+
* @param w The writer.
|
|
1575
|
+
* @param xs The elements, in order.
|
|
1576
|
+
*/
|
|
970
1577
|
export const jsonEncodeF64Array = (w: JsonWriter, xs: f64[]): void => {
|
|
971
1578
|
w.beginArray()
|
|
972
1579
|
for (const x of xs) {
|
|
@@ -975,6 +1582,12 @@ export const jsonEncodeF64Array = (w: JsonWriter, xs: f64[]): void => {
|
|
|
975
1582
|
w.endArray()
|
|
976
1583
|
}
|
|
977
1584
|
|
|
1585
|
+
/**
|
|
1586
|
+
* Writes an array whose every element is a `boolean`.
|
|
1587
|
+
*
|
|
1588
|
+
* @param w The writer.
|
|
1589
|
+
* @param xs The elements, in order.
|
|
1590
|
+
*/
|
|
978
1591
|
export const jsonEncodeBooleanArray = (w: JsonWriter, xs: boolean[]): void => {
|
|
979
1592
|
w.beginArray()
|
|
980
1593
|
for (const x of xs) {
|
|
@@ -983,6 +1596,12 @@ export const jsonEncodeBooleanArray = (w: JsonWriter, xs: boolean[]): void => {
|
|
|
983
1596
|
w.endArray()
|
|
984
1597
|
}
|
|
985
1598
|
|
|
1599
|
+
/**
|
|
1600
|
+
* Writes an array whose every element is a string.
|
|
1601
|
+
*
|
|
1602
|
+
* @param w The writer.
|
|
1603
|
+
* @param xs The elements, in order.
|
|
1604
|
+
*/
|
|
986
1605
|
export const jsonEncodeStringArray = (w: JsonWriter, xs: string[]): void => {
|
|
987
1606
|
w.beginArray()
|
|
988
1607
|
for (const x of xs) {
|
|
@@ -991,6 +1610,12 @@ export const jsonEncodeStringArray = (w: JsonWriter, xs: string[]): void => {
|
|
|
991
1610
|
w.endArray()
|
|
992
1611
|
}
|
|
993
1612
|
|
|
1613
|
+
/**
|
|
1614
|
+
* Writes `null`, or the array as `jsonEncodeI32Array` writes it.
|
|
1615
|
+
*
|
|
1616
|
+
* @param w The writer.
|
|
1617
|
+
* @param xs The elements, or `null`.
|
|
1618
|
+
*/
|
|
994
1619
|
export const jsonEncodeI32ArrayOrNull = (w: JsonWriter, xs: i32[] | null): void => {
|
|
995
1620
|
if (xs === null) {
|
|
996
1621
|
w.nullValue()
|
|
@@ -999,6 +1624,12 @@ export const jsonEncodeI32ArrayOrNull = (w: JsonWriter, xs: i32[] | null): void
|
|
|
999
1624
|
}
|
|
1000
1625
|
}
|
|
1001
1626
|
|
|
1627
|
+
/**
|
|
1628
|
+
* Writes `null`, or the array as `jsonEncodeU32Array` writes it.
|
|
1629
|
+
*
|
|
1630
|
+
* @param w The writer.
|
|
1631
|
+
* @param xs The elements, or `null`.
|
|
1632
|
+
*/
|
|
1002
1633
|
export const jsonEncodeU32ArrayOrNull = (w: JsonWriter, xs: u32[] | null): void => {
|
|
1003
1634
|
if (xs === null) {
|
|
1004
1635
|
w.nullValue()
|
|
@@ -1007,6 +1638,12 @@ export const jsonEncodeU32ArrayOrNull = (w: JsonWriter, xs: u32[] | null): void
|
|
|
1007
1638
|
}
|
|
1008
1639
|
}
|
|
1009
1640
|
|
|
1641
|
+
/**
|
|
1642
|
+
* Writes `null`, or the array as `jsonEncodeI64Array` writes it.
|
|
1643
|
+
*
|
|
1644
|
+
* @param w The writer.
|
|
1645
|
+
* @param xs The elements, or `null`.
|
|
1646
|
+
*/
|
|
1010
1647
|
export const jsonEncodeI64ArrayOrNull = (w: JsonWriter, xs: i64[] | null): void => {
|
|
1011
1648
|
if (xs === null) {
|
|
1012
1649
|
w.nullValue()
|
|
@@ -1015,6 +1652,12 @@ export const jsonEncodeI64ArrayOrNull = (w: JsonWriter, xs: i64[] | null): void
|
|
|
1015
1652
|
}
|
|
1016
1653
|
}
|
|
1017
1654
|
|
|
1655
|
+
/**
|
|
1656
|
+
* Writes `null`, or the array as `jsonEncodeU64Array` writes it.
|
|
1657
|
+
*
|
|
1658
|
+
* @param w The writer.
|
|
1659
|
+
* @param xs The elements, or `null`.
|
|
1660
|
+
*/
|
|
1018
1661
|
export const jsonEncodeU64ArrayOrNull = (w: JsonWriter, xs: u64[] | null): void => {
|
|
1019
1662
|
if (xs === null) {
|
|
1020
1663
|
w.nullValue()
|
|
@@ -1023,6 +1666,12 @@ export const jsonEncodeU64ArrayOrNull = (w: JsonWriter, xs: u64[] | null): void
|
|
|
1023
1666
|
}
|
|
1024
1667
|
}
|
|
1025
1668
|
|
|
1669
|
+
/**
|
|
1670
|
+
* Writes `null`, or the array as `jsonEncodeF64Array` writes it.
|
|
1671
|
+
*
|
|
1672
|
+
* @param w The writer.
|
|
1673
|
+
* @param xs The elements, or `null`.
|
|
1674
|
+
*/
|
|
1026
1675
|
export const jsonEncodeF64ArrayOrNull = (w: JsonWriter, xs: f64[] | null): void => {
|
|
1027
1676
|
if (xs === null) {
|
|
1028
1677
|
w.nullValue()
|
|
@@ -1031,6 +1680,12 @@ export const jsonEncodeF64ArrayOrNull = (w: JsonWriter, xs: f64[] | null): void
|
|
|
1031
1680
|
}
|
|
1032
1681
|
}
|
|
1033
1682
|
|
|
1683
|
+
/**
|
|
1684
|
+
* Writes `null`, or the array as `jsonEncodeBooleanArray` writes it.
|
|
1685
|
+
*
|
|
1686
|
+
* @param w The writer.
|
|
1687
|
+
* @param xs The elements, or `null`.
|
|
1688
|
+
*/
|
|
1034
1689
|
export const jsonEncodeBooleanArrayOrNull = (w: JsonWriter, xs: boolean[] | null): void => {
|
|
1035
1690
|
if (xs === null) {
|
|
1036
1691
|
w.nullValue()
|
|
@@ -1039,6 +1694,12 @@ export const jsonEncodeBooleanArrayOrNull = (w: JsonWriter, xs: boolean[] | null
|
|
|
1039
1694
|
}
|
|
1040
1695
|
}
|
|
1041
1696
|
|
|
1697
|
+
/**
|
|
1698
|
+
* Writes `null`, or the array as `jsonEncodeStringArray` writes it.
|
|
1699
|
+
*
|
|
1700
|
+
* @param w The writer.
|
|
1701
|
+
* @param xs The elements, or `null`.
|
|
1702
|
+
*/
|
|
1042
1703
|
export const jsonEncodeStringArrayOrNull = (w: JsonWriter, xs: string[] | null): void => {
|
|
1043
1704
|
if (xs === null) {
|
|
1044
1705
|
w.nullValue()
|