@amritk/nish-aarch64-linux 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/INSTALL.md +73 -59
- package/bin/nish +0 -0
- package/docs/AI.md +50 -4
- package/package.json +1 -1
- 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/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-value.ts
CHANGED
|
@@ -31,14 +31,15 @@
|
|
|
31
31
|
* largest request seen, parsing allocates nothing.
|
|
32
32
|
*
|
|
33
33
|
* A handle carries the generation of the document's contents it was made in,
|
|
34
|
-
* which moves on at every `parse` and `clear`, so a handle kept from an
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
34
|
+
* which moves on at every `parse` and `clear`, so a handle kept from an earlier
|
|
35
|
+
* request names nothing in the next one, even when the next one has the same
|
|
36
|
+
* shape: it is `JSON_VALUE_NONE` to `kind`, and every reader answers its empty
|
|
37
|
+
* value for it rather than another document's.
|
|
38
|
+
*
|
|
39
|
+
* The generation has 511 values and moves by one at a `clear`, one at a parse
|
|
40
|
+
* and two at a parse that fails, so the check misses only a handle kept across
|
|
41
|
+
* exactly a multiple of 511 of those steps; a program must not keep handles
|
|
42
|
+
* across documents, and this catches the mistake rather than licensing it. A
|
|
42
43
|
* negative handle, or one past the last value, is `JSON_VALUE_NONE` too.
|
|
43
44
|
*
|
|
44
45
|
* **The document's size is bounded.** Each value costs about 46 bytes of the
|
|
@@ -67,25 +68,28 @@
|
|
|
67
68
|
* the range of bytes it was read from: `start(v)` and `end(v)` bound a value,
|
|
68
69
|
* `keyStart(o, i)` and `keyEnd(o, i)` the key of member `i`, its quotes
|
|
69
70
|
* included. They are offsets into the bytes `parseRanged` was given, or
|
|
70
|
-
* `parseTextRanged`'s string, so a line index over that text
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* `
|
|
88
|
-
*
|
|
71
|
+
* `parseTextRanged`'s string, so a line index over that text (`YamlLineIndex`
|
|
72
|
+
* is one) turns them into a line and a column.
|
|
73
|
+
*
|
|
74
|
+
* Under Node, where `parseTextRanged` takes one byte per UTF-16 code unit
|
|
75
|
+
* (below), its offsets count code units, so past the first non-ASCII character
|
|
76
|
+
* they differ from a native build's, which count UTF-8 bytes. Ranges cost about
|
|
77
|
+
* 20 bytes per value more: two offsets for the value, two for its key and about
|
|
78
|
+
* one on the parse's stacks, which grow with the nesting and the members
|
|
79
|
+
* pending. They are entry points of their own rather than a flag `parse` tests,
|
|
80
|
+
* so that `parse` keeps the loop it had before ranges existed.
|
|
81
|
+
*
|
|
82
|
+
* A plain `parse` writes nothing for them: ranges carry the generation they
|
|
83
|
+
* were written in, which `clear` moves anyway, so after a `parse` every reader
|
|
84
|
+
* answers `-1`, and their arrays are made on first use, so a document that
|
|
85
|
+
* never keeps ranges allocates none. A loader that builds a document from
|
|
86
|
+
* another text `clear`s, then gives each value it makes its range with
|
|
87
|
+
* `setRange` and each key with `setKeyRange`; until then a built value or key
|
|
88
|
+
* answers `-1`. `set` over an existing key keeps the member's key range and
|
|
89
|
+
* takes the new value's own.
|
|
90
|
+
*
|
|
91
|
+
* Under `JSON_VALUE_DUPLICATE_LAST` a repeated key's member keeps its first
|
|
92
|
+
* key's range and holds the last value, with that value's.
|
|
89
93
|
*
|
|
90
94
|
* **Objects keep insertion order**, the order the members were written in or
|
|
91
95
|
* added. `get` finds a member by its key with a scan, so it is for a handful of
|
|
@@ -125,16 +129,17 @@
|
|
|
125
129
|
* `set` can never contain itself: adding a value that is already in a
|
|
126
130
|
* container, or a container to itself or to anything inside it, is
|
|
127
131
|
* `JSON_VALUE_ERR_ATTACHED`, and writing always ends.
|
|
128
|
-
* Strings handed to a builder are taken as their bytes, as `JsonWriter` takes
|
|
129
|
-
* them, and a string read back is made from the stored bytes in one
|
|
130
|
-
* allocation (`String.fromBytes`). Under Node a string is UTF-16, and
|
|
131
|
-
* `parseText` and the builders take one byte per code unit, so non-ASCII text
|
|
132
|
-
* is stored differently there than natively, where a string is its UTF-8
|
|
133
|
-
* bytes; `parse` over a `u8[]` reads the same bytes both ways.
|
|
134
132
|
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
133
|
+
* Strings handed to a builder are taken as their bytes, as `JsonWriter` takes
|
|
134
|
+
* them, and a string read back is made from the stored bytes in one allocation
|
|
135
|
+
* (`String.fromBytes`). Under Node a string is UTF-16, and `parseText` and the
|
|
136
|
+
* builders take one byte per code unit, so non-ASCII text is stored differently
|
|
137
|
+
* there than natively, where a string is its UTF-8 bytes; `parse` over a `u8[]`
|
|
138
|
+
* reads the same bytes both ways.
|
|
139
|
+
*
|
|
140
|
+
* **Public is not the whole API.** The compiler ignores `private`, so every
|
|
141
|
+
* member is reachable by a program; `private` and `@internal` mark what is
|
|
142
|
+
* not API, and the guarantees above hold only through the members that are
|
|
138
143
|
* not marked so. `append` with indices of the program's choosing, for
|
|
139
144
|
* example, skips the check that keeps a tree finite.
|
|
140
145
|
*
|
|
@@ -165,12 +170,17 @@ export type JsonValue = i32
|
|
|
165
170
|
|
|
166
171
|
/** What `kind` answers for a handle that names no value. */
|
|
167
172
|
export const JSON_VALUE_NONE: i32 = 0
|
|
173
|
+
/** The kind of `null`. */
|
|
168
174
|
export const JSON_VALUE_NULL: i32 = 1
|
|
175
|
+
/** The kind of `true` and `false`, which `boolean` reads. */
|
|
169
176
|
export const JSON_VALUE_BOOLEAN: i32 = 2
|
|
170
177
|
/** A number, kept as its text: `numberText`, `isI64`, `i64`, `isU64`, `u64` and `f64` read it. */
|
|
171
178
|
export const JSON_VALUE_NUMBER: i32 = 3
|
|
179
|
+
/** A string, decoded: `string` and `stringEquals` read it. */
|
|
172
180
|
export const JSON_VALUE_STRING: i32 = 4
|
|
181
|
+
/** An array: `length` and `at` read its elements, in order. */
|
|
173
182
|
export const JSON_VALUE_ARRAY: i32 = 5
|
|
183
|
+
/** An object: `length`, `keyAt`, `at` and `get` read its members, in order. */
|
|
174
184
|
export const JSON_VALUE_OBJECT: i32 = 6
|
|
175
185
|
|
|
176
186
|
/** What a builder answers when it did what it was asked. */
|
|
@@ -226,18 +236,32 @@ const JSON_VALUE_NO_WORD: i32 = 0
|
|
|
226
236
|
const JSON_VALUE_TRUE_WORD: i32 = 1
|
|
227
237
|
const JSON_VALUE_MINUS: i32 = 45
|
|
228
238
|
|
|
229
|
-
/**
|
|
239
|
+
/** A `JsonDocument`'s source ranges, made on first use; see `JsonDocument.ranges`. */
|
|
230
240
|
class JsonValueRanges {
|
|
231
241
|
/**
|
|
232
242
|
* Per value, its first byte in the parsed bytes and one past its last; per
|
|
233
243
|
* member slot, its key's, quotes included. `-1` where nothing was recorded.
|
|
244
|
+
*
|
|
245
|
+
* @internal Read and written by `JsonDocument` alone; the class is not
|
|
246
|
+
* exported.
|
|
234
247
|
*/
|
|
235
248
|
valueStart: i32[]
|
|
249
|
+
/** @internal Read and written by `JsonDocument` alone; the class is not exported. */
|
|
236
250
|
valueEnd: i32[]
|
|
251
|
+
/** @internal Read and written by `JsonDocument` alone; the class is not exported. */
|
|
237
252
|
slotKeyStart: i32[]
|
|
253
|
+
/** @internal Read and written by `JsonDocument` alone; the class is not exported. */
|
|
238
254
|
slotKeyEnd: i32[]
|
|
239
|
-
/**
|
|
255
|
+
/**
|
|
256
|
+
* For `parseRanged`: where each open container's key and each pending
|
|
257
|
+
* member's key end; `openKeySource` and `pendingSource` hold where they
|
|
258
|
+
* start.
|
|
259
|
+
*
|
|
260
|
+
* @internal Read and written by `JsonDocument` alone; the class is not
|
|
261
|
+
* exported.
|
|
262
|
+
*/
|
|
240
263
|
openKeyEnd: i32[]
|
|
264
|
+
/** @internal Read and written by `JsonDocument` alone; the class is not exported. */
|
|
241
265
|
pendingKeyEnd: i32[]
|
|
242
266
|
|
|
243
267
|
constructor() {
|
|
@@ -253,26 +277,26 @@ class JsonValueRanges {
|
|
|
253
277
|
/**
|
|
254
278
|
* A JSON tree, and the memory every value of it lives in. Make one, then
|
|
255
279
|
* `parse` into it or build with the `new*` methods; the module header has the
|
|
256
|
-
* rules. Its
|
|
257
|
-
* member in Nish is
|
|
280
|
+
* rules. Its `private` and `@internal` members are reachable, as every
|
|
281
|
+
* member in Nish is, because the compiler ignores `private`; use the others.
|
|
258
282
|
*/
|
|
259
283
|
export class JsonDocument {
|
|
260
284
|
/** Per value: its `JSON_VALUE_*` kind. */
|
|
261
|
-
kinds: u8[]
|
|
285
|
+
private kinds: u8[]
|
|
262
286
|
/** Per value: a string's or a number's first byte in `bytes`, a container's first slot, a boolean's `0` or `1`. */
|
|
263
|
-
first: i32[]
|
|
287
|
+
private first: i32[]
|
|
264
288
|
/** Per value: a string's or a number's byte count, or a container's member count. */
|
|
265
|
-
count: i32[]
|
|
289
|
+
private count: i32[]
|
|
266
290
|
/** Per value: how many slots a container has from `first`, its members and room to grow. */
|
|
267
|
-
room: i32[]
|
|
291
|
+
private room: i32[]
|
|
268
292
|
/** Per value: the container it is in, or `-1`. */
|
|
269
|
-
parent: i32[]
|
|
293
|
+
private parent: i32[]
|
|
270
294
|
/** Per member slot: the member's value, and an object member's key as a window of `bytes`. */
|
|
271
|
-
slotValue: i32[]
|
|
272
|
-
slotKeyAt: i32[]
|
|
273
|
-
slotKeyLength: i32[]
|
|
295
|
+
private slotValue: i32[]
|
|
296
|
+
private slotKeyAt: i32[]
|
|
297
|
+
private slotKeyLength: i32[]
|
|
274
298
|
/** The bytes of every string, key and number, decoded, one after the other. */
|
|
275
|
-
bytes: u8[]
|
|
299
|
+
private bytes: u8[]
|
|
276
300
|
/**
|
|
277
301
|
* The source ranges, `null` until the first `parseRanged` or setter, so
|
|
278
302
|
* that a document that never keeps ranges allocates nothing for them.
|
|
@@ -283,8 +307,8 @@ export class JsonDocument {
|
|
|
283
307
|
* `clear` moves anyway, so that a plain `parse` writes nothing for ranges.
|
|
284
308
|
*/
|
|
285
309
|
ranges: JsonValueRanges | null = null
|
|
286
|
-
rangeTop: i32 = 0
|
|
287
|
-
slotRangeTop: i32 = 0
|
|
310
|
+
private rangeTop: i32 = 0
|
|
311
|
+
private slotRangeTop: i32 = 0
|
|
288
312
|
/** The generation the ranges were written in; `0`, never a generation, when none were. */
|
|
289
313
|
rangeGeneration: i32 = 0
|
|
290
314
|
/**
|
|
@@ -295,22 +319,26 @@ export class JsonDocument {
|
|
|
295
319
|
* duplicate check's. All of them keep their length from one parse to the
|
|
296
320
|
* next, which is what lets a parse allocate nothing.
|
|
297
321
|
*/
|
|
298
|
-
openValue: i32[]
|
|
299
|
-
openBase: i32[]
|
|
300
|
-
openKeyAt: i32[]
|
|
301
|
-
openKeyLength: i32[]
|
|
302
|
-
openKeySource: i32[]
|
|
303
|
-
pendingValue: i32[]
|
|
304
|
-
pendingKeyAt: i32[]
|
|
305
|
-
pendingKeyLength: i32[]
|
|
306
|
-
pendingSource: i32[]
|
|
307
|
-
sortA: i32[]
|
|
308
|
-
sortB: i32[]
|
|
322
|
+
private openValue: i32[]
|
|
323
|
+
private openBase: i32[]
|
|
324
|
+
private openKeyAt: i32[]
|
|
325
|
+
private openKeyLength: i32[]
|
|
326
|
+
private openKeySource: i32[]
|
|
327
|
+
private pendingValue: i32[]
|
|
328
|
+
private pendingKeyAt: i32[]
|
|
329
|
+
private pendingKeyLength: i32[]
|
|
330
|
+
private pendingSource: i32[]
|
|
331
|
+
private sortA: i32[]
|
|
332
|
+
private sortB: i32[]
|
|
309
333
|
/** `parseText`'s copy of its string. */
|
|
310
|
-
textBytes: u8[]
|
|
334
|
+
private textBytes: u8[]
|
|
335
|
+
/**
|
|
336
|
+
* @internal The tokenizer every parse reads through. Read by
|
|
337
|
+
* std/yaml-json.ts, which builds a document from YAML through it.
|
|
338
|
+
*/
|
|
311
339
|
tok: JsonTokenizer
|
|
312
340
|
/** A tokenizer over one stored number at a time, for `isI64`, `i64` and `f64`, limited only by `i32`. */
|
|
313
|
-
numbers: JsonTokenizer
|
|
341
|
+
private numbers: JsonTokenizer
|
|
314
342
|
|
|
315
343
|
/** `JSON_VALUE_DUPLICATE_REFUSE` or `JSON_VALUE_DUPLICATE_LAST`; read by `parse`. */
|
|
316
344
|
duplicates: i32 = 0
|
|
@@ -322,16 +350,25 @@ export class JsonDocument {
|
|
|
322
350
|
top: i32 = 0
|
|
323
351
|
/** How many member slots are in use, and how many bytes. */
|
|
324
352
|
slotTop: i32 = 0
|
|
353
|
+
/** How many bytes of the decoded-text store are in use. */
|
|
325
354
|
byteTop: i32 = 0
|
|
326
355
|
/** How many members are pending. */
|
|
327
|
-
pendingTop: i32 = 0
|
|
356
|
+
private pendingTop: i32 = 0
|
|
328
357
|
/** The most values the document holds; more is `JSON_VALUE_ERR_VALUES`. At most `JSON_VALUE_MAX_VALUES`. */
|
|
329
358
|
maxValues: i32 = 0
|
|
330
359
|
/** The generation every handle of the current document carries; see `JSON_VALUE_INDEX_BITS`. */
|
|
331
360
|
generation: i32 = 1
|
|
332
361
|
/** Whether the last `unsigned` read a number that fits a `u64`. */
|
|
333
|
-
unsignedFits: boolean = false
|
|
362
|
+
private unsignedFits: boolean = false
|
|
334
363
|
|
|
364
|
+
/**
|
|
365
|
+
* An empty document whose parses are held to `limits`, with `maxDepth` capped
|
|
366
|
+
* at `JSON_WRITER_MAX_DEPTH` so that every document a parse accepts can be
|
|
367
|
+
* written back. `maxValues` starts at `JSON_VALUE_DEFAULT_MAX_VALUES`.
|
|
368
|
+
*
|
|
369
|
+
* @param limits - the depth and size limits for every parse; the document
|
|
370
|
+
* copies them.
|
|
371
|
+
*/
|
|
335
372
|
constructor(limits: JsonLimits) {
|
|
336
373
|
this.maxValues = JSON_VALUE_DEFAULT_MAX_VALUES
|
|
337
374
|
this.kinds = []
|
|
@@ -398,6 +435,12 @@ export class JsonDocument {
|
|
|
398
435
|
* source ranges; `parseRanged` does.
|
|
399
436
|
*
|
|
400
437
|
* Keep in step with `parseRanged`, the same loop plus the ranges.
|
|
438
|
+
*
|
|
439
|
+
* @param src - the bytes to parse, which the document only reads.
|
|
440
|
+
* @param off - the offset in `src` of the first byte of the text.
|
|
441
|
+
* @param len - how many bytes the text is.
|
|
442
|
+
* @returns the root value's handle, or the negative code that is also in
|
|
443
|
+
* `failed`.
|
|
401
444
|
*/
|
|
402
445
|
parse(src: u8[], off: i32, len: i32): JsonValue {
|
|
403
446
|
this.clear()
|
|
@@ -489,6 +532,12 @@ export class JsonDocument {
|
|
|
489
532
|
* loop inline there and cost the plain loop more instructions. A program
|
|
490
533
|
* that links both can still see the plain loop's code laid out differently
|
|
491
534
|
* (`docs/BENCHMARKS.md`). Keep in step with `parse`.
|
|
535
|
+
*
|
|
536
|
+
* @param src - the bytes to parse, which the document only reads.
|
|
537
|
+
* @param off - the offset in `src` of the first byte of the text.
|
|
538
|
+
* @param len - how many bytes the text is.
|
|
539
|
+
* @returns the root value's handle, or the negative code that is also in
|
|
540
|
+
* `failed`.
|
|
492
541
|
*/
|
|
493
542
|
parseRanged(src: u8[], off: i32, len: i32): JsonValue {
|
|
494
543
|
this.clear()
|
|
@@ -587,7 +636,14 @@ export class JsonDocument {
|
|
|
587
636
|
return this.handle(root)
|
|
588
637
|
}
|
|
589
638
|
|
|
590
|
-
/**
|
|
639
|
+
/**
|
|
640
|
+
* `parse` over the bytes of `s`, copied into an array the document reuses;
|
|
641
|
+
* `errorAt` is a byte offset in `s`. Keep in step with `parseTextRanged`.
|
|
642
|
+
*
|
|
643
|
+
* @param s - the JSON text.
|
|
644
|
+
* @returns the root value's handle, or the negative code that is also in
|
|
645
|
+
* `failed`.
|
|
646
|
+
*/
|
|
591
647
|
parseText(s: string): JsonValue {
|
|
592
648
|
const n: i32 = toI32(s.length)
|
|
593
649
|
const buf: u8[] = jsonValueRoom(this.textBytes, n)
|
|
@@ -598,7 +654,14 @@ export class JsonDocument {
|
|
|
598
654
|
return this.parse(buf, JSON_VALUE_NO_WORD, n)
|
|
599
655
|
}
|
|
600
656
|
|
|
601
|
-
/**
|
|
657
|
+
/**
|
|
658
|
+
* `parseRanged` over the bytes of `s`, as `parseText` is `parse`'s; its
|
|
659
|
+
* offsets are byte offsets in `s`. Keep in step with `parseText`.
|
|
660
|
+
*
|
|
661
|
+
* @param s - the JSON text.
|
|
662
|
+
* @returns the root value's handle, or the negative code that is also in
|
|
663
|
+
* `failed`.
|
|
664
|
+
*/
|
|
602
665
|
parseTextRanged(s: string): JsonValue {
|
|
603
666
|
const n: i32 = toI32(s.length)
|
|
604
667
|
const buf: u8[] = jsonValueRoom(this.textBytes, n)
|
|
@@ -609,8 +672,8 @@ export class JsonDocument {
|
|
|
609
672
|
return this.parseRanged(buf, JSON_VALUE_NO_WORD, n)
|
|
610
673
|
}
|
|
611
674
|
|
|
612
|
-
/**
|
|
613
|
-
parseFailed(code: i32, at: i32): i32 {
|
|
675
|
+
/** Records a failed parse and forgets what it built. */
|
|
676
|
+
private parseFailed(code: i32, at: i32): i32 {
|
|
614
677
|
this.clear()
|
|
615
678
|
this.failed = code
|
|
616
679
|
this.errorAt = at
|
|
@@ -618,12 +681,12 @@ export class JsonDocument {
|
|
|
618
681
|
}
|
|
619
682
|
|
|
620
683
|
/**
|
|
621
|
-
*
|
|
684
|
+
* Places the members pending from `base` into container `c` as one run of
|
|
622
685
|
* slots, checking an object's keys for duplicates first. Answers `0`, or
|
|
623
686
|
* `JSON_VALUE_ERR_DUPLICATE` with `errorAt` set. Keep in step with
|
|
624
687
|
* `closeRanged`.
|
|
625
688
|
*/
|
|
626
|
-
close(c: i32, base: i32): i32 {
|
|
689
|
+
private close(c: i32, base: i32): i32 {
|
|
627
690
|
let n: i32 = this.pendingTop - base
|
|
628
691
|
const object: boolean = this.kindAt(c) === JSON_VALUE_OBJECT
|
|
629
692
|
if (object && n > 1) {
|
|
@@ -655,8 +718,8 @@ export class JsonDocument {
|
|
|
655
718
|
return 0
|
|
656
719
|
}
|
|
657
720
|
|
|
658
|
-
/**
|
|
659
|
-
closeRanged(c: i32, base: i32): i32 {
|
|
721
|
+
/** `close` for `parseRanged`, which also gives each member its key's range. Keep in step with `close`. */
|
|
722
|
+
private closeRanged(c: i32, base: i32): i32 {
|
|
660
723
|
let n: i32 = this.pendingTop - base
|
|
661
724
|
const object: boolean = this.kindAt(c) === JSON_VALUE_OBJECT
|
|
662
725
|
if (object && n > 1) {
|
|
@@ -692,7 +755,7 @@ export class JsonDocument {
|
|
|
692
755
|
}
|
|
693
756
|
|
|
694
757
|
/**
|
|
695
|
-
*
|
|
758
|
+
* The duplicate check over the `n` members pending from `base`. Under
|
|
696
759
|
* `JSON_VALUE_DUPLICATE_LAST`, a repeated key's later members are marked
|
|
697
760
|
* gone (their pending value set to `-1`) and the first one takes the last
|
|
698
761
|
* one's value. Small objects compare every pair; larger ones are sorted by
|
|
@@ -700,7 +763,7 @@ export class JsonDocument {
|
|
|
700
763
|
* and the first of them is its first member. Keep in step with
|
|
701
764
|
* `duplicateKeysRanged`.
|
|
702
765
|
*/
|
|
703
|
-
duplicateKeys(base: i32, n: i32): i32 {
|
|
766
|
+
private duplicateKeys(base: i32, n: i32): i32 {
|
|
704
767
|
let firstRepeat: i32 = -1
|
|
705
768
|
if (n <= JSON_VALUE_PAIRWISE) {
|
|
706
769
|
for (let i: i32 = base + 1; i < base + n; i += 1) {
|
|
@@ -732,11 +795,11 @@ export class JsonDocument {
|
|
|
732
795
|
}
|
|
733
796
|
|
|
734
797
|
/**
|
|
735
|
-
*
|
|
798
|
+
* `duplicateKeys` for `closeRanged`, the same check. A copy, so that
|
|
736
799
|
* `duplicateKeys` keeps the one caller that lets it inline into `parse`.
|
|
737
800
|
* Keep in step with `duplicateKeys`.
|
|
738
801
|
*/
|
|
739
|
-
duplicateKeysRanged(base: i32, n: i32): i32 {
|
|
802
|
+
private duplicateKeysRanged(base: i32, n: i32): i32 {
|
|
740
803
|
let firstRepeat: i32 = -1
|
|
741
804
|
if (n <= JSON_VALUE_PAIRWISE) {
|
|
742
805
|
for (let i: i32 = base + 1; i < base + n; i += 1) {
|
|
@@ -768,11 +831,11 @@ export class JsonDocument {
|
|
|
768
831
|
}
|
|
769
832
|
|
|
770
833
|
/**
|
|
771
|
-
*
|
|
834
|
+
* Pending member `later` repeats the key of the earlier `head`: under
|
|
772
835
|
* `JSON_VALUE_DUPLICATE_LAST` `head` takes its value and it is marked gone.
|
|
773
836
|
* Answers the smallest source offset of a repeating key seen so far.
|
|
774
837
|
*/
|
|
775
|
-
repeat(head: i32, later: i32, firstRepeat: i32): i32 {
|
|
838
|
+
private repeat(head: i32, later: i32, firstRepeat: i32): i32 {
|
|
776
839
|
const source: i32 = jsonValueGet(this.pendingSource, later)
|
|
777
840
|
if (this.duplicates === JSON_VALUE_DUPLICATE_LAST) {
|
|
778
841
|
jsonValuePut(this.pendingValue, head, jsonValueGet(this.pendingValue, later))
|
|
@@ -781,18 +844,18 @@ export class JsonDocument {
|
|
|
781
844
|
return firstRepeat < 0 || source < firstRepeat ? source : firstRepeat
|
|
782
845
|
}
|
|
783
846
|
|
|
784
|
-
/**
|
|
785
|
-
samePendingKey(a: i32, b: i32): boolean {
|
|
847
|
+
/** Whether pending members `a` and `b` have the same key bytes. */
|
|
848
|
+
private samePendingKey(a: i32, b: i32): boolean {
|
|
786
849
|
return this.comparePending(a, b) === 0
|
|
787
850
|
}
|
|
788
851
|
|
|
789
852
|
/**
|
|
790
|
-
*
|
|
853
|
+
* The pending indices `base .. base + n - 1` sorted by key, ties in index
|
|
791
854
|
* order: a bottom-up merge sort between the two sort arrays, which answers
|
|
792
855
|
* whichever of them holds the result. Keep in step with
|
|
793
856
|
* `sortPendingRanged`.
|
|
794
857
|
*/
|
|
795
|
-
sortPending(base: i32, n: i32): i32[] {
|
|
858
|
+
private sortPending(base: i32, n: i32): i32[] {
|
|
796
859
|
let from: i32[] = jsonValueRoomI32(this.sortA, n)
|
|
797
860
|
let to: i32[] = jsonValueRoomI32(this.sortB, n)
|
|
798
861
|
this.sortA = from
|
|
@@ -832,11 +895,11 @@ export class JsonDocument {
|
|
|
832
895
|
}
|
|
833
896
|
|
|
834
897
|
/**
|
|
835
|
-
*
|
|
898
|
+
* `sortPending` for `duplicateKeysRanged`, the same sort. A copy, so
|
|
836
899
|
* that `sortPending` keeps the one caller that lets it inline into `parse`.
|
|
837
900
|
* Keep in step with `sortPending`.
|
|
838
901
|
*/
|
|
839
|
-
sortPendingRanged(base: i32, n: i32): i32[] {
|
|
902
|
+
private sortPendingRanged(base: i32, n: i32): i32[] {
|
|
840
903
|
let from: i32[] = jsonValueRoomI32(this.sortA, n)
|
|
841
904
|
let to: i32[] = jsonValueRoomI32(this.sortB, n)
|
|
842
905
|
this.sortA = from
|
|
@@ -875,8 +938,8 @@ export class JsonDocument {
|
|
|
875
938
|
return from
|
|
876
939
|
}
|
|
877
940
|
|
|
878
|
-
/**
|
|
879
|
-
comparePending(a: i32, b: i32): i32 {
|
|
941
|
+
/** Pending members `a` and `b` ordered by key bytes. */
|
|
942
|
+
private comparePending(a: i32, b: i32): i32 {
|
|
880
943
|
return jsonValueCompareBytes(
|
|
881
944
|
this.bytes,
|
|
882
945
|
jsonValueGet(this.pendingKeyAt, a),
|
|
@@ -886,8 +949,8 @@ export class JsonDocument {
|
|
|
886
949
|
)
|
|
887
950
|
}
|
|
888
951
|
|
|
889
|
-
/**
|
|
890
|
-
storeString(tok: JsonTokenizer): i32 {
|
|
952
|
+
/** The tokenizer's current string or key, decoded onto the end of `bytes`; answers its length. */
|
|
953
|
+
private storeString(tok: JsonTokenizer): i32 {
|
|
891
954
|
const n: i32 = tok.stringLength()
|
|
892
955
|
const at: i32 = this.byteTop
|
|
893
956
|
const buf: u8[] = jsonValueRoom(this.bytes, at + n)
|
|
@@ -897,8 +960,8 @@ export class JsonDocument {
|
|
|
897
960
|
return n
|
|
898
961
|
}
|
|
899
962
|
|
|
900
|
-
/**
|
|
901
|
-
storeSource(tok: JsonTokenizer): i32 {
|
|
963
|
+
/** The tokenizer's current token as written, a number's text, onto the end of `bytes`; answers its length. Keep in step with `storeSourceRanged`. */
|
|
964
|
+
private storeSource(tok: JsonTokenizer): i32 {
|
|
902
965
|
const src: u8[] = tok.src
|
|
903
966
|
const start: i32 = tok.tokStart
|
|
904
967
|
const n: i32 = tok.tokEnd - start
|
|
@@ -915,11 +978,11 @@ export class JsonDocument {
|
|
|
915
978
|
}
|
|
916
979
|
|
|
917
980
|
/**
|
|
918
|
-
*
|
|
981
|
+
* `storeSource` for `parseRanged`, the same copy. A copy, so that
|
|
919
982
|
* `storeSource` keeps the one caller that lets it inline into `parse`.
|
|
920
983
|
* Keep in step with `storeSource`.
|
|
921
984
|
*/
|
|
922
|
-
storeSourceRanged(tok: JsonTokenizer): i32 {
|
|
985
|
+
private storeSourceRanged(tok: JsonTokenizer): i32 {
|
|
923
986
|
const src: u8[] = tok.src
|
|
924
987
|
const start: i32 = tok.tokStart
|
|
925
988
|
const n: i32 = tok.tokEnd - start
|
|
@@ -936,11 +999,11 @@ export class JsonDocument {
|
|
|
936
999
|
}
|
|
937
1000
|
|
|
938
1001
|
/**
|
|
939
|
-
*
|
|
1002
|
+
* A new value of `kind` with its two words, in no container, and
|
|
940
1003
|
* its index; `JSON_VALUE_ERR_VALUES` when the document already holds
|
|
941
1004
|
* `maxValues`.
|
|
942
1005
|
*/
|
|
943
|
-
newNode(kind: i32, first: i32, count: i32): i32 {
|
|
1006
|
+
private newNode(kind: i32, first: i32, count: i32): i32 {
|
|
944
1007
|
const v: i32 = this.top
|
|
945
1008
|
const cap: i32 = this.maxValues < JSON_VALUE_MAX_VALUES ? this.maxValues : JSON_VALUE_MAX_VALUES
|
|
946
1009
|
if (v >= cap) {
|
|
@@ -961,7 +1024,13 @@ export class JsonDocument {
|
|
|
961
1024
|
|
|
962
1025
|
// ---- Handles ----------------------------------------------------------------
|
|
963
1026
|
|
|
964
|
-
/**
|
|
1027
|
+
/**
|
|
1028
|
+
* The index handle `v` names in this document, or `-1` when it
|
|
1029
|
+
* names nothing here.
|
|
1030
|
+
*
|
|
1031
|
+
* @internal Read by std/yaml-json.ts, which builds a document from YAML
|
|
1032
|
+
* through it.
|
|
1033
|
+
*/
|
|
965
1034
|
index(v: JsonValue): i32 {
|
|
966
1035
|
if (v < 0 || v >> JSON_VALUE_INDEX_BITS !== this.generation) {
|
|
967
1036
|
return -1
|
|
@@ -970,20 +1039,26 @@ export class JsonDocument {
|
|
|
970
1039
|
return x < this.top ? x : -1
|
|
971
1040
|
}
|
|
972
1041
|
|
|
973
|
-
/**
|
|
974
|
-
handle(x: i32): JsonValue {
|
|
1042
|
+
/** The handle of index `x` in this document's generation; a negative `x` is answered as it is. */
|
|
1043
|
+
private handle(x: i32): JsonValue {
|
|
975
1044
|
return x < 0 ? x : (this.generation << JSON_VALUE_INDEX_BITS) | x
|
|
976
1045
|
}
|
|
977
1046
|
|
|
978
1047
|
// ---- Reading ----------------------------------------------------------------
|
|
979
1048
|
|
|
980
|
-
/**
|
|
1049
|
+
/**
|
|
1050
|
+
* `v`'s `JSON_VALUE_*` kind, or `JSON_VALUE_NONE` when `v` names no value of
|
|
1051
|
+
* this document's current contents.
|
|
1052
|
+
*
|
|
1053
|
+
* @param v - any handle.
|
|
1054
|
+
* @returns a `JSON_VALUE_*` kind.
|
|
1055
|
+
*/
|
|
981
1056
|
kind(v: JsonValue): i32 {
|
|
982
1057
|
return this.kindAt(this.index(v))
|
|
983
1058
|
}
|
|
984
1059
|
|
|
985
|
-
/**
|
|
986
|
-
kindAt(x: i32): i32 {
|
|
1060
|
+
/** The kind of index `x`, or `JSON_VALUE_NONE`. */
|
|
1061
|
+
private kindAt(x: i32): i32 {
|
|
987
1062
|
const kinds: u8[] = this.kinds
|
|
988
1063
|
if (x < 0 || x >= this.top || x >= toI32(kinds.length)) {
|
|
989
1064
|
return JSON_VALUE_NONE
|
|
@@ -991,38 +1066,65 @@ export class JsonDocument {
|
|
|
991
1066
|
return toI32(kinds[x])
|
|
992
1067
|
}
|
|
993
1068
|
|
|
994
|
-
/**
|
|
1069
|
+
/**
|
|
1070
|
+
* A boolean's value; `false` for any other value.
|
|
1071
|
+
*
|
|
1072
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1073
|
+
* nothing.
|
|
1074
|
+
* @returns true only for a `true` value.
|
|
1075
|
+
*/
|
|
995
1076
|
boolean(v: JsonValue): boolean {
|
|
996
1077
|
const x: i32 = this.index(v)
|
|
997
1078
|
return this.kindAt(x) === JSON_VALUE_BOOLEAN && jsonValueGet(this.first, x) === JSON_VALUE_TRUE_WORD
|
|
998
1079
|
}
|
|
999
1080
|
|
|
1000
|
-
/**
|
|
1081
|
+
/**
|
|
1082
|
+
* How many elements an array has, or members an object has; `-1` for any
|
|
1083
|
+
* other value.
|
|
1084
|
+
*
|
|
1085
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1086
|
+
* nothing.
|
|
1087
|
+
* @returns the element or member count, or `-1`.
|
|
1088
|
+
*/
|
|
1001
1089
|
length(v: JsonValue): i32 {
|
|
1002
1090
|
return this.lengthAt(this.index(v))
|
|
1003
1091
|
}
|
|
1004
1092
|
|
|
1005
|
-
/**
|
|
1006
|
-
lengthAt(x: i32): i32 {
|
|
1093
|
+
/** `length` of index `x`. */
|
|
1094
|
+
private lengthAt(x: i32): i32 {
|
|
1007
1095
|
const k: i32 = this.kindAt(x)
|
|
1008
1096
|
return k === JSON_VALUE_ARRAY || k === JSON_VALUE_OBJECT ? jsonValueGet(this.count, x) : -1
|
|
1009
1097
|
}
|
|
1010
1098
|
|
|
1011
|
-
/**
|
|
1012
|
-
slotAt(x: i32, i: i32): i32 {
|
|
1099
|
+
/** The slot of member or element `i` of the container at index `x`, or `-1`. */
|
|
1100
|
+
private slotAt(x: i32, i: i32): i32 {
|
|
1013
1101
|
if (i < 0 || i >= this.lengthAt(x)) {
|
|
1014
1102
|
return -1
|
|
1015
1103
|
}
|
|
1016
1104
|
return jsonValueGet(this.first, x) + i
|
|
1017
1105
|
}
|
|
1018
1106
|
|
|
1019
|
-
/**
|
|
1107
|
+
/**
|
|
1108
|
+
* Element `i` of an array, or the value of member `i` of an object, in order;
|
|
1109
|
+
* `-1` when there is none.
|
|
1110
|
+
*
|
|
1111
|
+
* @param v - an array or object handle.
|
|
1112
|
+
* @param i - the position, from 0.
|
|
1113
|
+
* @returns the element or member value, or `-1`.
|
|
1114
|
+
*/
|
|
1020
1115
|
at(v: JsonValue, i: i32): JsonValue {
|
|
1021
1116
|
const s: i32 = this.slotAt(this.index(v), i)
|
|
1022
1117
|
return s < 0 ? -1 : this.handle(jsonValueGet(this.slotValue, s))
|
|
1023
1118
|
}
|
|
1024
1119
|
|
|
1025
|
-
/**
|
|
1120
|
+
/**
|
|
1121
|
+
* The key of member `i` of an object, decoded; `""` when `v` is not an object
|
|
1122
|
+
* or has no member `i`.
|
|
1123
|
+
*
|
|
1124
|
+
* @param v - an object handle.
|
|
1125
|
+
* @param i - the member position, from 0.
|
|
1126
|
+
* @returns the key, a new string, or `""`.
|
|
1127
|
+
*/
|
|
1026
1128
|
keyAt(v: JsonValue, i: i32): string {
|
|
1027
1129
|
const x: i32 = this.index(v)
|
|
1028
1130
|
if (this.kindAt(x) !== JSON_VALUE_OBJECT) {
|
|
@@ -1035,7 +1137,14 @@ export class JsonDocument {
|
|
|
1035
1137
|
return this.textOf(jsonValueGet(this.slotKeyAt, s), jsonValueGet(this.slotKeyLength, s))
|
|
1036
1138
|
}
|
|
1037
1139
|
|
|
1038
|
-
/**
|
|
1140
|
+
/**
|
|
1141
|
+
* The value of the member of object `v` whose key is `key`'s bytes; `-1` when
|
|
1142
|
+
* there is none.
|
|
1143
|
+
*
|
|
1144
|
+
* @param v - an object handle.
|
|
1145
|
+
* @param key - the key to look for, compared byte by byte.
|
|
1146
|
+
* @returns the member value, or `-1`.
|
|
1147
|
+
*/
|
|
1039
1148
|
get(v: JsonValue, key: string): JsonValue {
|
|
1040
1149
|
const x: i32 = this.index(v)
|
|
1041
1150
|
if (this.kindAt(x) !== JSON_VALUE_OBJECT) {
|
|
@@ -1045,8 +1154,8 @@ export class JsonDocument {
|
|
|
1045
1154
|
return s < 0 ? -1 : this.handle(jsonValueGet(this.slotValue, s))
|
|
1046
1155
|
}
|
|
1047
1156
|
|
|
1048
|
-
/**
|
|
1049
|
-
findKey(x: i32, key: string): i32 {
|
|
1157
|
+
/** The slot of the member of the object at index `x` whose key is `key`'s bytes, or `-1`. */
|
|
1158
|
+
private findKey(x: i32, key: string): i32 {
|
|
1050
1159
|
const start: i32 = jsonValueGet(this.first, x)
|
|
1051
1160
|
const n: i32 = jsonValueGet(this.count, x)
|
|
1052
1161
|
for (let s: i32 = start; s < start + n; s += 1) {
|
|
@@ -1057,7 +1166,13 @@ export class JsonDocument {
|
|
|
1057
1166
|
return -1
|
|
1058
1167
|
}
|
|
1059
1168
|
|
|
1060
|
-
/**
|
|
1169
|
+
/**
|
|
1170
|
+
* A string's decoded bytes as a string; `""` for any other value.
|
|
1171
|
+
*
|
|
1172
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1173
|
+
* nothing.
|
|
1174
|
+
* @returns the string, a new one each call, or `""`.
|
|
1175
|
+
*/
|
|
1061
1176
|
string(v: JsonValue): string {
|
|
1062
1177
|
const x: i32 = this.index(v)
|
|
1063
1178
|
if (this.kindAt(x) !== JSON_VALUE_STRING) {
|
|
@@ -1066,7 +1181,14 @@ export class JsonDocument {
|
|
|
1066
1181
|
return this.textOf(jsonValueGet(this.first, x), jsonValueGet(this.count, x))
|
|
1067
1182
|
}
|
|
1068
1183
|
|
|
1069
|
-
/**
|
|
1184
|
+
/**
|
|
1185
|
+
* Whether `v` is a string whose decoded bytes are `s`'s. Allocates nothing.
|
|
1186
|
+
*
|
|
1187
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1188
|
+
* nothing.
|
|
1189
|
+
* @param s - the text to compare against.
|
|
1190
|
+
* @returns true when the bytes match exactly.
|
|
1191
|
+
*/
|
|
1070
1192
|
stringEquals(v: JsonValue, s: string): boolean {
|
|
1071
1193
|
const x: i32 = this.index(v)
|
|
1072
1194
|
return (
|
|
@@ -1075,7 +1197,14 @@ export class JsonDocument {
|
|
|
1075
1197
|
)
|
|
1076
1198
|
}
|
|
1077
1199
|
|
|
1078
|
-
/**
|
|
1200
|
+
/**
|
|
1201
|
+
* A number's text, exactly as it was written or built; `""` for any other
|
|
1202
|
+
* value.
|
|
1203
|
+
*
|
|
1204
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1205
|
+
* nothing.
|
|
1206
|
+
* @returns the number text, or `""`.
|
|
1207
|
+
*/
|
|
1079
1208
|
numberText(v: JsonValue): string {
|
|
1080
1209
|
const x: i32 = this.index(v)
|
|
1081
1210
|
if (this.kindAt(x) !== JSON_VALUE_NUMBER) {
|
|
@@ -1088,12 +1217,22 @@ export class JsonDocument {
|
|
|
1088
1217
|
* Whether `v` is a number written without a fraction or an exponent whose
|
|
1089
1218
|
* value fits an `i64`: the spelling, so `1.0` is not one. `isIntegral(v)`
|
|
1090
1219
|
* asks whether the value is whole.
|
|
1220
|
+
*
|
|
1221
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1222
|
+
* nothing.
|
|
1223
|
+
* @returns true when `i64(v)` gives the exact value.
|
|
1091
1224
|
*/
|
|
1092
1225
|
isI64(v: JsonValue): boolean {
|
|
1093
1226
|
return this.readNumber(this.index(v)) && this.numbers.isInteger()
|
|
1094
1227
|
}
|
|
1095
1228
|
|
|
1096
|
-
/**
|
|
1229
|
+
/**
|
|
1230
|
+
* A number's value as an `i64` when `isI64(v)` holds, and `0` otherwise.
|
|
1231
|
+
*
|
|
1232
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1233
|
+
* nothing.
|
|
1234
|
+
* @returns the integer, or `0`.
|
|
1235
|
+
*/
|
|
1097
1236
|
i64(v: JsonValue): i64 {
|
|
1098
1237
|
// `numberI64` is already `0` for a number that is not an `i64`.
|
|
1099
1238
|
return this.readNumber(this.index(v)) ? this.numbers.numberI64() : toI64(0)
|
|
@@ -1102,24 +1241,34 @@ export class JsonDocument {
|
|
|
1102
1241
|
/**
|
|
1103
1242
|
* Whether `v` is a number written without a sign, a fraction or an exponent
|
|
1104
1243
|
* whose value fits a `u64`: the spelling, as `isI64(v)` is.
|
|
1244
|
+
*
|
|
1245
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1246
|
+
* nothing.
|
|
1247
|
+
* @returns true when `u64(v)` gives the exact value.
|
|
1105
1248
|
*/
|
|
1106
1249
|
isU64(v: JsonValue): boolean {
|
|
1107
1250
|
this.unsigned(this.index(v))
|
|
1108
1251
|
return this.unsignedFits
|
|
1109
1252
|
}
|
|
1110
1253
|
|
|
1111
|
-
/**
|
|
1254
|
+
/**
|
|
1255
|
+
* A number's value as a `u64` when `isU64(v)` holds, and `0` otherwise.
|
|
1256
|
+
*
|
|
1257
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1258
|
+
* nothing.
|
|
1259
|
+
* @returns the integer, or `0`.
|
|
1260
|
+
*/
|
|
1112
1261
|
u64(v: JsonValue): u64 {
|
|
1113
1262
|
return this.unsigned(this.index(v))
|
|
1114
1263
|
}
|
|
1115
1264
|
|
|
1116
1265
|
/**
|
|
1117
|
-
*
|
|
1266
|
+
* The digits of the number at index `x` as a `u64`, setting
|
|
1118
1267
|
* `unsignedFits` to whether it is a `u64` at all: no sign, no fraction or
|
|
1119
1268
|
* exponent, and at most 2^64 - 1. Every `u64` comes from `toU64`, which is
|
|
1120
1269
|
* a BigInt under Node, and nothing here wraps, so it means the same there.
|
|
1121
1270
|
*/
|
|
1122
|
-
unsigned(x: i32): u64 {
|
|
1271
|
+
private unsigned(x: i32): u64 {
|
|
1123
1272
|
this.unsignedFits = false
|
|
1124
1273
|
if (!this.readNumber(x) || !this.numbers.integral) {
|
|
1125
1274
|
return toU64(0)
|
|
@@ -1153,12 +1302,23 @@ export class JsonDocument {
|
|
|
1153
1302
|
* `1e2` and `1e400` are, where `isI64(v)` and `isU64(v)` go by spelling and
|
|
1154
1303
|
* by range and answer `false` for all three. `false` for any other kind and
|
|
1155
1304
|
* for a handle from an earlier parse.
|
|
1305
|
+
*
|
|
1306
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1307
|
+
* nothing.
|
|
1308
|
+
* @returns true when the value is a whole number.
|
|
1156
1309
|
*/
|
|
1157
1310
|
isIntegral(v: JsonValue): boolean {
|
|
1158
1311
|
return this.readNumber(this.index(v)) && this.numbers.isIntegral()
|
|
1159
1312
|
}
|
|
1160
1313
|
|
|
1161
|
-
/**
|
|
1314
|
+
/**
|
|
1315
|
+
* A number's nearest `f64`, ties to even, as `JSON.parse` reads it; `0` for
|
|
1316
|
+
* any other value.
|
|
1317
|
+
*
|
|
1318
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1319
|
+
* nothing.
|
|
1320
|
+
* @returns the nearest double, or `0`.
|
|
1321
|
+
*/
|
|
1162
1322
|
f64(v: JsonValue): f64 {
|
|
1163
1323
|
if (!this.readNumber(this.index(v))) {
|
|
1164
1324
|
return 0
|
|
@@ -1166,8 +1326,8 @@ export class JsonDocument {
|
|
|
1166
1326
|
return this.numbers.numberF64()
|
|
1167
1327
|
}
|
|
1168
1328
|
|
|
1169
|
-
/**
|
|
1170
|
-
readNumber(x: i32): boolean {
|
|
1329
|
+
/** Points the number tokenizer at index `x`'s text and reads its one token; `false` when it is not a number. */
|
|
1330
|
+
private readNumber(x: i32): boolean {
|
|
1171
1331
|
if (this.kindAt(x) !== JSON_VALUE_NUMBER) {
|
|
1172
1332
|
return false
|
|
1173
1333
|
}
|
|
@@ -1176,8 +1336,8 @@ export class JsonDocument {
|
|
|
1176
1336
|
return numbers.next() === JSON_TOK_NUMBER
|
|
1177
1337
|
}
|
|
1178
1338
|
|
|
1179
|
-
/**
|
|
1180
|
-
textOf(at: i32, n: i32): string {
|
|
1339
|
+
/** `bytes[at, at + n)` as a string, in one allocation. */
|
|
1340
|
+
private textOf(at: i32, n: i32): string {
|
|
1181
1341
|
const bytes: u8[] = this.bytes
|
|
1182
1342
|
if (at < 0 || n <= 0 || at > toI32(bytes.length) - n) {
|
|
1183
1343
|
return ""
|
|
@@ -1185,8 +1345,8 @@ export class JsonDocument {
|
|
|
1185
1345
|
return String.fromBytes(bytes, at, at + n)
|
|
1186
1346
|
}
|
|
1187
1347
|
|
|
1188
|
-
/**
|
|
1189
|
-
bytesEqual(at: i32, n: i32, s: string): boolean {
|
|
1348
|
+
/** Whether `bytes[at, at + n)` are exactly `s`'s bytes. */
|
|
1349
|
+
private bytesEqual(at: i32, n: i32, s: string): boolean {
|
|
1190
1350
|
const bytes: u8[] = this.bytes
|
|
1191
1351
|
if (toI32(s.length) !== n || at < 0 || at > toI32(bytes.length) - n) {
|
|
1192
1352
|
return false
|
|
@@ -1204,42 +1364,68 @@ export class JsonDocument {
|
|
|
1204
1364
|
// that is not there, for a value or key a builder made and no setter gave a
|
|
1205
1365
|
// range, and for every value and key after a `parse` or `parseText`.
|
|
1206
1366
|
|
|
1207
|
-
/**
|
|
1367
|
+
/**
|
|
1368
|
+
* The offset of `v`'s first byte in the bytes the document parsed.
|
|
1369
|
+
*
|
|
1370
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1371
|
+
* nothing.
|
|
1372
|
+
* @returns the byte offset, or `-1`.
|
|
1373
|
+
*/
|
|
1208
1374
|
start(v: JsonValue): i32 {
|
|
1209
1375
|
const x: i32 = this.index(v)
|
|
1210
1376
|
return this.hasValueRange(x) ? jsonValueGet(this.rangeArrays().valueStart, x) : -1
|
|
1211
1377
|
}
|
|
1212
1378
|
|
|
1213
|
-
/**
|
|
1379
|
+
/**
|
|
1380
|
+
* One past the offset of `v`'s last byte: `v` is `src[start(v), end(v))`.
|
|
1381
|
+
*
|
|
1382
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1383
|
+
* nothing.
|
|
1384
|
+
* @returns the byte offset, or `-1`.
|
|
1385
|
+
*/
|
|
1214
1386
|
end(v: JsonValue): i32 {
|
|
1215
1387
|
const x: i32 = this.index(v)
|
|
1216
1388
|
return this.hasValueRange(x) ? jsonValueGet(this.rangeArrays().valueEnd, x) : -1
|
|
1217
1389
|
}
|
|
1218
1390
|
|
|
1219
|
-
/**
|
|
1391
|
+
/**
|
|
1392
|
+
* The offset of the first byte of the key of member `i` of object `o`, its
|
|
1393
|
+
* opening quote.
|
|
1394
|
+
*
|
|
1395
|
+
* @param o - an object handle.
|
|
1396
|
+
* @param i - the member position, from 0.
|
|
1397
|
+
* @returns the byte offset, or `-1`.
|
|
1398
|
+
*/
|
|
1220
1399
|
keyStart(o: JsonValue, i: i32): i32 {
|
|
1221
1400
|
const s: i32 = this.keySlot(this.index(o), i)
|
|
1222
1401
|
return this.hasKeyRange(s) ? jsonValueGet(this.rangeArrays().slotKeyStart, s) : -1
|
|
1223
1402
|
}
|
|
1224
1403
|
|
|
1225
|
-
/**
|
|
1404
|
+
/**
|
|
1405
|
+
* One past the offset of the last byte of the key of member `i` of object
|
|
1406
|
+
* `o`, its closing quote.
|
|
1407
|
+
*
|
|
1408
|
+
* @param o - an object handle.
|
|
1409
|
+
* @param i - the member position, from 0.
|
|
1410
|
+
* @returns the byte offset, or `-1`.
|
|
1411
|
+
*/
|
|
1226
1412
|
keyEnd(o: JsonValue, i: i32): i32 {
|
|
1227
1413
|
const s: i32 = this.keySlot(this.index(o), i)
|
|
1228
1414
|
return this.hasKeyRange(s) ? jsonValueGet(this.rangeArrays().slotKeyEnd, s) : -1
|
|
1229
1415
|
}
|
|
1230
1416
|
|
|
1231
|
-
/**
|
|
1232
|
-
hasValueRange(x: i32): boolean {
|
|
1417
|
+
/** Whether value `x` has a current range: below `rangeTop`, and written in this generation. */
|
|
1418
|
+
private hasValueRange(x: i32): boolean {
|
|
1233
1419
|
return x >= 0 && x < this.rangeTop && this.rangeGeneration === this.generation
|
|
1234
1420
|
}
|
|
1235
1421
|
|
|
1236
|
-
/**
|
|
1237
|
-
hasKeyRange(s: i32): boolean {
|
|
1422
|
+
/** Whether slot `s` has a current key range: below `slotRangeTop`, and written in this generation. */
|
|
1423
|
+
private hasKeyRange(s: i32): boolean {
|
|
1238
1424
|
return s >= 0 && s < this.slotRangeTop && this.rangeGeneration === this.generation
|
|
1239
1425
|
}
|
|
1240
1426
|
|
|
1241
|
-
/**
|
|
1242
|
-
rangeArrays(): JsonValueRanges {
|
|
1427
|
+
/** The range arrays, made the first time a parse or setter writes a range. */
|
|
1428
|
+
private rangeArrays(): JsonValueRanges {
|
|
1243
1429
|
const had: JsonValueRanges | null = this.ranges
|
|
1244
1430
|
if (had !== null) {
|
|
1245
1431
|
return had
|
|
@@ -1249,8 +1435,8 @@ export class JsonDocument {
|
|
|
1249
1435
|
return made
|
|
1250
1436
|
}
|
|
1251
1437
|
|
|
1252
|
-
/**
|
|
1253
|
-
ownRanges(): void {
|
|
1438
|
+
/** Before a setter writes a range, forgets an earlier generation's, so that none of them reads as this one's. */
|
|
1439
|
+
private ownRanges(): void {
|
|
1254
1440
|
if (this.rangeGeneration !== this.generation) {
|
|
1255
1441
|
this.rangeTop = 0
|
|
1256
1442
|
this.slotRangeTop = 0
|
|
@@ -1263,6 +1449,11 @@ export class JsonDocument {
|
|
|
1263
1449
|
* from another text with the `new*` methods and keeps where each value
|
|
1264
1450
|
* came from. `JSON_VALUE_OK`, `JSON_VALUE_ERR_HANDLE`, or
|
|
1265
1451
|
* `JSON_VALUE_ERR_RANGE` when `0 <= start <= end` does not hold.
|
|
1452
|
+
*
|
|
1453
|
+
* @param v - the value to give the range to.
|
|
1454
|
+
* @param start - the offset of its first byte in the other text.
|
|
1455
|
+
* @param end - one past the offset of its last byte.
|
|
1456
|
+
* @returns `JSON_VALUE_OK` or the error code.
|
|
1266
1457
|
*/
|
|
1267
1458
|
setRange(v: JsonValue, start: i32, end: i32): i32 {
|
|
1268
1459
|
const x: i32 = this.index(v)
|
|
@@ -1291,6 +1482,12 @@ export class JsonDocument {
|
|
|
1291
1482
|
* Gives the key of member `i` of object `o` the range `[start, end)`, as
|
|
1292
1483
|
* `setRange` does a value's. `JSON_VALUE_ERR_HANDLE` when `o` is not an
|
|
1293
1484
|
* object or has no member `i`.
|
|
1485
|
+
*
|
|
1486
|
+
* @param o - an object handle.
|
|
1487
|
+
* @param i - the member position, from 0.
|
|
1488
|
+
* @param start - the offset of the key's first byte in the other text.
|
|
1489
|
+
* @param end - one past the offset of its last byte.
|
|
1490
|
+
* @returns `JSON_VALUE_OK` or the error code.
|
|
1294
1491
|
*/
|
|
1295
1492
|
setKeyRange(o: JsonValue, i: i32, start: i32, end: i32): i32 {
|
|
1296
1493
|
const s: i32 = this.keySlot(this.index(o), i)
|
|
@@ -1314,8 +1511,8 @@ export class JsonDocument {
|
|
|
1314
1511
|
return JSON_VALUE_OK
|
|
1315
1512
|
}
|
|
1316
1513
|
|
|
1317
|
-
/**
|
|
1318
|
-
keySlot(x: i32, i: i32): i32 {
|
|
1514
|
+
/** The slot of member `i` of the object at index `x`, or `-1` when `x` is not an object or has no member `i`. */
|
|
1515
|
+
private keySlot(x: i32, i: i32): i32 {
|
|
1319
1516
|
return this.kindAt(x) === JSON_VALUE_OBJECT ? this.slotAt(x, i) : -1
|
|
1320
1517
|
}
|
|
1321
1518
|
|
|
@@ -1323,27 +1520,58 @@ export class JsonDocument {
|
|
|
1323
1520
|
// Each `new*` answers a handle, or `JSON_VALUE_ERR_VALUES` when the document
|
|
1324
1521
|
// already holds `maxValues`.
|
|
1325
1522
|
|
|
1523
|
+
/**
|
|
1524
|
+
* A new `null`.
|
|
1525
|
+
*
|
|
1526
|
+
* @returns the new value's handle, in no container yet, or
|
|
1527
|
+
* `JSON_VALUE_ERR_VALUES` when the document already holds `maxValues`.
|
|
1528
|
+
*/
|
|
1326
1529
|
newNull(): JsonValue {
|
|
1327
1530
|
return this.handle(this.newNode(JSON_VALUE_NULL, JSON_VALUE_NO_WORD, JSON_VALUE_NO_WORD))
|
|
1328
1531
|
}
|
|
1329
1532
|
|
|
1533
|
+
/**
|
|
1534
|
+
* A new boolean.
|
|
1535
|
+
*
|
|
1536
|
+
* @param b - the value it holds.
|
|
1537
|
+
* @returns the new value's handle, in no container yet, or
|
|
1538
|
+
* `JSON_VALUE_ERR_VALUES` when the document already holds `maxValues`.
|
|
1539
|
+
*/
|
|
1330
1540
|
newBoolean(b: boolean): JsonValue {
|
|
1331
1541
|
return this.handle(
|
|
1332
1542
|
this.newNode(JSON_VALUE_BOOLEAN, b ? JSON_VALUE_TRUE_WORD : JSON_VALUE_NO_WORD, JSON_VALUE_NO_WORD)
|
|
1333
1543
|
)
|
|
1334
1544
|
}
|
|
1335
1545
|
|
|
1336
|
-
/**
|
|
1546
|
+
/**
|
|
1547
|
+
* A string holding `s`'s bytes.
|
|
1548
|
+
*
|
|
1549
|
+
* @param s - the text, copied into the document.
|
|
1550
|
+
* @returns the new value's handle, in no container yet, or
|
|
1551
|
+
* `JSON_VALUE_ERR_VALUES` when the document already holds `maxValues`.
|
|
1552
|
+
*/
|
|
1337
1553
|
newString(s: string): JsonValue {
|
|
1338
1554
|
return this.newStored(JSON_VALUE_STRING, s)
|
|
1339
1555
|
}
|
|
1340
1556
|
|
|
1341
|
-
/**
|
|
1557
|
+
/**
|
|
1558
|
+
* A number holding `x`'s digits.
|
|
1559
|
+
*
|
|
1560
|
+
* @param x - the value, written in decimal.
|
|
1561
|
+
* @returns the new value's handle, in no container yet, or
|
|
1562
|
+
* `JSON_VALUE_ERR_VALUES` when the document already holds `maxValues`.
|
|
1563
|
+
*/
|
|
1342
1564
|
newI64(x: i64): JsonValue {
|
|
1343
1565
|
return this.newStored(JSON_VALUE_NUMBER, `${x}`)
|
|
1344
1566
|
}
|
|
1345
1567
|
|
|
1346
|
-
/**
|
|
1568
|
+
/**
|
|
1569
|
+
* A number holding `x`'s digits.
|
|
1570
|
+
*
|
|
1571
|
+
* @param x - the value, written in decimal.
|
|
1572
|
+
* @returns the new value's handle, in no container yet, or
|
|
1573
|
+
* `JSON_VALUE_ERR_VALUES` when the document already holds `maxValues`.
|
|
1574
|
+
*/
|
|
1347
1575
|
newU64(x: u64): JsonValue {
|
|
1348
1576
|
return this.newStored(JSON_VALUE_NUMBER, `${x}`)
|
|
1349
1577
|
}
|
|
@@ -1353,6 +1581,11 @@ export class JsonDocument {
|
|
|
1353
1581
|
* read back as `x`, with `-0` as `0`: what `JsonWriter.f64` writes.
|
|
1354
1582
|
* `JSON_VALUE_ERR_NUMBER` for NaN and both infinities, which JSON cannot
|
|
1355
1583
|
* spell.
|
|
1584
|
+
*
|
|
1585
|
+
* @param x - the value; it must be finite.
|
|
1586
|
+
* @returns the new value's handle, in no container yet, or
|
|
1587
|
+
* `JSON_VALUE_ERR_VALUES` when the document already holds `maxValues`.
|
|
1588
|
+
* `JSON_VALUE_ERR_NUMBER` for a value that is not finite.
|
|
1356
1589
|
*/
|
|
1357
1590
|
newF64(x: f64): JsonValue {
|
|
1358
1591
|
// `x - x` is `0` for every finite `x`, and NaN for NaN and both infinities.
|
|
@@ -1366,6 +1599,11 @@ export class JsonDocument {
|
|
|
1366
1599
|
* A number holding `text` exactly, which must be one JSON number and
|
|
1367
1600
|
* nothing else (RFC 8259 §6); `JSON_VALUE_ERR_NUMBER` otherwise. This is how
|
|
1368
1601
|
* a program writes a number no integer or float type holds.
|
|
1602
|
+
*
|
|
1603
|
+
* @param text - the number text, such as `"1e400"`.
|
|
1604
|
+
* @returns the new value's handle, in no container yet, or
|
|
1605
|
+
* `JSON_VALUE_ERR_VALUES` when the document already holds `maxValues`.
|
|
1606
|
+
* `JSON_VALUE_ERR_NUMBER` for text that is not one number.
|
|
1369
1607
|
*/
|
|
1370
1608
|
newNumber(text: string): JsonValue {
|
|
1371
1609
|
const at: i32 = this.byteTop
|
|
@@ -1382,15 +1620,34 @@ export class JsonDocument {
|
|
|
1382
1620
|
return this.handle(this.newNode(JSON_VALUE_NUMBER, at, n))
|
|
1383
1621
|
}
|
|
1384
1622
|
|
|
1623
|
+
/**
|
|
1624
|
+
* A new empty array, which `push` fills.
|
|
1625
|
+
*
|
|
1626
|
+
* @returns the new value's handle, in no container yet, or
|
|
1627
|
+
* `JSON_VALUE_ERR_VALUES` when the document already holds `maxValues`.
|
|
1628
|
+
*/
|
|
1385
1629
|
newArray(): JsonValue {
|
|
1386
1630
|
return this.handle(this.newNode(JSON_VALUE_ARRAY, this.slotTop, JSON_VALUE_NO_WORD))
|
|
1387
1631
|
}
|
|
1388
1632
|
|
|
1633
|
+
/**
|
|
1634
|
+
* A new empty object, which `set` fills.
|
|
1635
|
+
*
|
|
1636
|
+
* @returns the new value's handle, in no container yet, or
|
|
1637
|
+
* `JSON_VALUE_ERR_VALUES` when the document already holds `maxValues`.
|
|
1638
|
+
*/
|
|
1389
1639
|
newObject(): JsonValue {
|
|
1390
1640
|
return this.handle(this.newNode(JSON_VALUE_OBJECT, this.slotTop, JSON_VALUE_NO_WORD))
|
|
1391
1641
|
}
|
|
1392
1642
|
|
|
1393
|
-
/**
|
|
1643
|
+
/**
|
|
1644
|
+
* Appends `v` to array `a`. `JSON_VALUE_OK`, or `JSON_VALUE_ERR_HANDLE` or
|
|
1645
|
+
* `JSON_VALUE_ERR_ATTACHED`.
|
|
1646
|
+
*
|
|
1647
|
+
* @param a - the array to append to.
|
|
1648
|
+
* @param v - the value to append, which must be in no container.
|
|
1649
|
+
* @returns `JSON_VALUE_OK` or the error code.
|
|
1650
|
+
*/
|
|
1394
1651
|
push(a: JsonValue, v: JsonValue): i32 {
|
|
1395
1652
|
const ax: i32 = this.index(a)
|
|
1396
1653
|
const vx: i32 = this.index(v)
|
|
@@ -1411,6 +1668,11 @@ export class JsonDocument {
|
|
|
1411
1668
|
* property of a JavaScript object does. The value it replaces leaves the
|
|
1412
1669
|
* object and is in no container afterwards. `JSON_VALUE_OK`, or
|
|
1413
1670
|
* `JSON_VALUE_ERR_HANDLE` or `JSON_VALUE_ERR_ATTACHED`.
|
|
1671
|
+
*
|
|
1672
|
+
* @param o - the object to change.
|
|
1673
|
+
* @param key - the member key.
|
|
1674
|
+
* @param v - the value, which must be in no container.
|
|
1675
|
+
* @returns `JSON_VALUE_OK` or the error code.
|
|
1414
1676
|
*/
|
|
1415
1677
|
set(o: JsonValue, key: string, v: JsonValue): i32 {
|
|
1416
1678
|
const ox: i32 = this.index(o)
|
|
@@ -1434,12 +1696,12 @@ export class JsonDocument {
|
|
|
1434
1696
|
}
|
|
1435
1697
|
|
|
1436
1698
|
/**
|
|
1437
|
-
*
|
|
1699
|
+
* Whether the value at index `v` may go into the container at
|
|
1438
1700
|
* index `c`: it is a value, it is in no container, and it is not `c` or a
|
|
1439
1701
|
* container `c` is inside. Through `push` and `set`, that is what keeps
|
|
1440
1702
|
* every tree finite.
|
|
1441
1703
|
*/
|
|
1442
|
-
adoptable(c: i32, v: i32): i32 {
|
|
1704
|
+
private adoptable(c: i32, v: i32): i32 {
|
|
1443
1705
|
if (this.kindAt(v) === JSON_VALUE_NONE) {
|
|
1444
1706
|
return JSON_VALUE_ERR_HANDLE
|
|
1445
1707
|
}
|
|
@@ -1457,11 +1719,14 @@ export class JsonDocument {
|
|
|
1457
1719
|
}
|
|
1458
1720
|
|
|
1459
1721
|
/**
|
|
1460
|
-
*
|
|
1722
|
+
* Adds index `v`, with the key `bytes[keyAt, keyAt + keyLength)`,
|
|
1461
1723
|
* after the last member of index `c`, unchecked: `push` and `set` check
|
|
1462
1724
|
* first. A container whose slots are full moves them to the end of the slot
|
|
1463
1725
|
* arrays with twice the room, unless they are at the end already, where they
|
|
1464
1726
|
* grow in place; the slots it leaves are not reused until `clear`.
|
|
1727
|
+
*
|
|
1728
|
+
* @internal Read by std/yaml-json.ts, which builds a document from YAML
|
|
1729
|
+
* through it.
|
|
1465
1730
|
*/
|
|
1466
1731
|
append(c: i32, v: i32, keyAt: i32, keyLength: i32): void {
|
|
1467
1732
|
let start: i32 = jsonValueGet(this.first, c)
|
|
@@ -1486,19 +1751,19 @@ export class JsonDocument {
|
|
|
1486
1751
|
jsonValuePut(this.parent, v, c)
|
|
1487
1752
|
}
|
|
1488
1753
|
|
|
1489
|
-
/**
|
|
1490
|
-
newStored(kind: i32, s: string): JsonValue {
|
|
1754
|
+
/** A new value of `kind` holding `s`'s bytes, as a handle. */
|
|
1755
|
+
private newStored(kind: i32, s: string): JsonValue {
|
|
1491
1756
|
return this.handle(this.newNode(kind, this.byteTop, this.storeText(s)))
|
|
1492
1757
|
}
|
|
1493
1758
|
|
|
1494
1759
|
/**
|
|
1495
|
-
*
|
|
1760
|
+
* Copies the `n` slots from `start` to the end of the slot arrays,
|
|
1496
1761
|
* key ranges with them, and answers where they now start. Key ranges move
|
|
1497
1762
|
* only when the slots are below `slotRangeTop` and of this generation, the
|
|
1498
1763
|
* test that comes first: a document that never keeps ranges, or a builder's
|
|
1499
1764
|
* own slots, skip the copy without leaving `append`'s loop.
|
|
1500
1765
|
*/
|
|
1501
|
-
moveSlots(start: i32, n: i32): i32 {
|
|
1766
|
+
private moveSlots(start: i32, n: i32): i32 {
|
|
1502
1767
|
const moved: i32 = this.slotTop
|
|
1503
1768
|
for (let i: i32 = 0; i < n; i += 1) {
|
|
1504
1769
|
jsonValuePut(this.slotValue, moved + i, jsonValueGet(this.slotValue, start + i))
|
|
@@ -1512,13 +1777,13 @@ export class JsonDocument {
|
|
|
1512
1777
|
}
|
|
1513
1778
|
|
|
1514
1779
|
/**
|
|
1515
|
-
*
|
|
1780
|
+
* `append` moving the `n` slots from `from`, some of them below
|
|
1516
1781
|
* `slotRangeTop`, to `to`, the end of the slots: their key ranges move with
|
|
1517
1782
|
* them, and the slots between keep none. `moveSlots` has checked the
|
|
1518
1783
|
* generation, and a slot below `slotRangeTop` means the arrays were made;
|
|
1519
1784
|
* reading them here, not through `rangeArrays`, keeps `append` small.
|
|
1520
1785
|
*/
|
|
1521
|
-
moveKeyRanges(from: i32, to: i32, n: i32): void {
|
|
1786
|
+
private moveKeyRanges(from: i32, to: i32, n: i32): void {
|
|
1522
1787
|
const r: JsonValueRanges | null = this.ranges
|
|
1523
1788
|
const top: i32 = this.slotRangeTop
|
|
1524
1789
|
if (r !== null) {
|
|
@@ -1535,7 +1800,15 @@ export class JsonDocument {
|
|
|
1535
1800
|
}
|
|
1536
1801
|
}
|
|
1537
1802
|
|
|
1538
|
-
/**
|
|
1803
|
+
/**
|
|
1804
|
+
* Appends `s`'s bytes to the end of `bytes`.
|
|
1805
|
+
*
|
|
1806
|
+
* @param s - the text whose bytes are stored.
|
|
1807
|
+
* @returns how many bytes were stored.
|
|
1808
|
+
*
|
|
1809
|
+
* @internal Read by std/yaml-json.ts, which builds a document from YAML
|
|
1810
|
+
* through it.
|
|
1811
|
+
*/
|
|
1539
1812
|
storeText(s: string): i32 {
|
|
1540
1813
|
const n: i32 = toI32(s.length)
|
|
1541
1814
|
const at: i32 = this.byteTop
|
|
@@ -1558,13 +1831,17 @@ export class JsonDocument {
|
|
|
1558
1831
|
* nothing writes nothing and fails nothing. A parsed tree is never deeper
|
|
1559
1832
|
* than the writer nests; a built one deeper than `JSON_WRITER_MAX_DEPTH`
|
|
1560
1833
|
* ends at `JSON_WRITE_TOO_DEEP`, where the recursion stops.
|
|
1834
|
+
*
|
|
1835
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1836
|
+
* nothing.
|
|
1837
|
+
* @param w - the writer, where a value is due.
|
|
1561
1838
|
*/
|
|
1562
1839
|
write(v: JsonValue, w: JsonWriter): void {
|
|
1563
1840
|
this.writeAt(this.index(v), w)
|
|
1564
1841
|
}
|
|
1565
1842
|
|
|
1566
|
-
/**
|
|
1567
|
-
writeAt(x: i32, w: JsonWriter): void {
|
|
1843
|
+
/** `write` of index `x`. */
|
|
1844
|
+
private writeAt(x: i32, w: JsonWriter): void {
|
|
1568
1845
|
const k: i32 = this.kindAt(x)
|
|
1569
1846
|
if (k === JSON_VALUE_NONE || w.failed !== JSON_WRITE_OK) {
|
|
1570
1847
|
return
|
|
@@ -1606,6 +1883,10 @@ export class JsonDocument {
|
|
|
1606
1883
|
* fits, then made into one string. `""` when `v` names nothing or the
|
|
1607
1884
|
* writer refuses it, which only a built tree deeper than
|
|
1608
1885
|
* `JSON_WRITER_MAX_DEPTH` makes it do; `write` gives the code.
|
|
1886
|
+
*
|
|
1887
|
+
* @param v - a handle from this document; one from an earlier parse names
|
|
1888
|
+
* nothing.
|
|
1889
|
+
* @returns the minified text, or `""`.
|
|
1609
1890
|
*/
|
|
1610
1891
|
stringify(v: JsonValue): string {
|
|
1611
1892
|
// Every byte stored and a few of punctuation per value: one pass fits most
|