@amritk/nish-x86_64-linux 0.44.0 → 0.45.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +47 -5
- package/runtime/nish.mjs +4 -0
- package/std/README.md +3 -3
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1275 -217
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/json-tokenizer.ts
CHANGED
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
* `jsonTokW64`.
|
|
104
104
|
*/
|
|
105
105
|
|
|
106
|
-
import {
|
|
106
|
+
import { skipBlank, skipJsonText, tryCopyBytes } from "nish/bytes"
|
|
107
107
|
|
|
108
108
|
/** The top-level value is complete and only blanks follow it. Answered again on every later call. */
|
|
109
109
|
export const JSON_TOK_END: i32 = 0
|
|
@@ -273,53 +273,77 @@ const JSON_TOK_FOUR_BYTE_FLOOR: i32 = 65536
|
|
|
273
273
|
const JSON_TOK_HIGH_SURROGATE: i32 = 55296
|
|
274
274
|
const JSON_TOK_LOW_SURROGATE: i32 = 56320
|
|
275
275
|
const JSON_TOK_SURROGATE_END: i32 = 57344
|
|
276
|
-
const JSON_TOK_HEX_BASE: i32 = 16
|
|
277
276
|
const JSON_TOK_TEN: i32 = 10
|
|
278
277
|
|
|
279
278
|
/** The most significant digits a `u64` holds whatever they are, 10^19 - 1 being below 2^64. Past them, `parseFloat` reads the text. */
|
|
280
279
|
const JSON_TOK_FAST_DIGITS: i32 = 19
|
|
281
280
|
/** Clinger's fast path: 10^22 is the largest exact power of ten. */
|
|
282
281
|
const JSON_TOK_FAST_EXPONENT: i32 = 22
|
|
283
|
-
/** The decimal exponents `jsonTokPowerOfFive` has an entry for. Past them, a number of at most 19 digits is zero or infinite. */
|
|
284
|
-
const JSON_TOK_SMALLEST_POWER: i32 = -342
|
|
285
|
-
const JSON_TOK_LARGEST_POWER: i32 = 308
|
|
286
282
|
/** Where an exponent's digits stop being added up: past it, `parseFloat` reads the text. */
|
|
287
283
|
const JSON_TOK_EXPONENT_CAP: i32 = 100000
|
|
288
|
-
/** 2^31 - 1 is 214748364 tens and 7: one more digit on a larger value, or a digit past 7 on that one, leaves `i32`. */
|
|
289
|
-
const JSON_TOK_I32_TENS: i32 = 214748364
|
|
290
|
-
const JSON_TOK_I32_LAST_DIGIT: i32 = 7
|
|
291
|
-
|
|
292
284
|
/**
|
|
293
285
|
* Whether the decimal digits `src[i, end)` add up to more than `bound`, which
|
|
294
|
-
* is at least `0`. The sum stops as soon as it passes
|
|
295
|
-
*
|
|
296
|
-
* of any length is compared exactly
|
|
297
|
-
* a native build.
|
|
286
|
+
* is at least `0`. The sum is kept in an `i64` and stops as soon as it passes
|
|
287
|
+
* `bound`: it is then at most ten times an `i32` plus one byte, so nothing
|
|
288
|
+
* overflows and an exponent of any length is compared exactly, under Node as
|
|
289
|
+
* in a native build.
|
|
298
290
|
*/
|
|
299
291
|
const jsonTokDigitsExceed = (src: u8[], i: i32, end: i32, bound: i32): boolean => {
|
|
300
292
|
let value: i32 = 0
|
|
301
293
|
let at: i32 = i
|
|
302
294
|
while (at < end && at < toI32(src.length)) {
|
|
303
|
-
const
|
|
304
|
-
if (
|
|
305
|
-
return true
|
|
306
|
-
}
|
|
307
|
-
value = value * JSON_TOK_TEN + digit
|
|
308
|
-
if (value > bound) {
|
|
295
|
+
const next: i64 = toI64(value) * toI64(JSON_TOK_TEN) + toI64(toI32(src[at]) - JSON_TOK_DIGIT_0)
|
|
296
|
+
if (next > toI64(bound)) {
|
|
309
297
|
return true
|
|
310
298
|
}
|
|
299
|
+
// At most `bound`, so it fits.
|
|
300
|
+
value = toI32(next)
|
|
311
301
|
at += 1
|
|
312
302
|
}
|
|
313
303
|
return false
|
|
314
304
|
}
|
|
315
305
|
|
|
316
306
|
/**
|
|
317
|
-
*
|
|
318
|
-
* position it has checked against the end,
|
|
319
|
-
*
|
|
320
|
-
* it takes an array of 2 GiB, so no test does.
|
|
307
|
+
* Where a window may end: at most 64 bytes short of 2^31 - 1. The scan reads
|
|
308
|
+
* up to twelve bytes past a position it has checked against the end, and the
|
|
309
|
+
* range is what proves each such offset fits; a window that ends any later is
|
|
310
|
+
* refused. Reaching it takes an array of 2 GiB, so no test does.
|
|
311
|
+
*/
|
|
312
|
+
type JsonTokEnd = integer<0, 2147483583>
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* The `i64` of magnitude `m`, at most 2^63 when `negative` and below it
|
|
316
|
+
* otherwise. `-m` is `~(m - 1)`, which holds -2^63 where `-m` in an `i64`
|
|
317
|
+
* could not; a zero, `-0` included, is `0`.
|
|
318
|
+
*/
|
|
319
|
+
const jsonTokSigned = (m: u64, negative: boolean): i64 => {
|
|
320
|
+
if (!negative) {
|
|
321
|
+
return toI64(m)
|
|
322
|
+
}
|
|
323
|
+
if (m === toU64(0)) {
|
|
324
|
+
return toI64(0)
|
|
325
|
+
}
|
|
326
|
+
return ~toI64(m - toU64(1))
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* A zeroed depth stack `depth` bytes long, `depth` at least `0`. A depth that
|
|
331
|
+
* fits a `u16`, every real limit, is one allocation whose length needs no
|
|
332
|
+
* check; a larger one is grown a byte at a time, which only a limit past
|
|
333
|
+
* 65535 open containers pays for.
|
|
321
334
|
*/
|
|
322
|
-
const
|
|
335
|
+
const jsonTokDepthStack = (depth: i32): u8[] => {
|
|
336
|
+
if (depth >= 0 && depth <= 65535) {
|
|
337
|
+
return new Array<u8>(toU16(depth))
|
|
338
|
+
}
|
|
339
|
+
const stack: u8[] = []
|
|
340
|
+
let k: i32 = 0
|
|
341
|
+
while (k < depth) {
|
|
342
|
+
stack.push(toU8(0))
|
|
343
|
+
k += 1
|
|
344
|
+
}
|
|
345
|
+
return stack
|
|
346
|
+
}
|
|
323
347
|
|
|
324
348
|
/** The byte at `i`, or `-1` when `i` is outside `[0, end)` or outside `src`. */
|
|
325
349
|
const jsonTokByteAt = (src: u8[], i: i32, end: i32): i32 => {
|
|
@@ -346,7 +370,7 @@ const JSON_TOK_EXACT_DIGITS: i32 = 18
|
|
|
346
370
|
* `i + 8 <= src.length`; the test here repeats it so each index is proved.
|
|
347
371
|
*/
|
|
348
372
|
const jsonTokLoad8 = (src: u8[], i: i32): u64 => {
|
|
349
|
-
if (i < 0 || i
|
|
373
|
+
if (i < 0 || i > toI32(src.length) - 8) {
|
|
350
374
|
return toU64(0)
|
|
351
375
|
}
|
|
352
376
|
return (
|
|
@@ -365,12 +389,10 @@ const jsonTokLoad8 = (src: u8[], i: i32): u64 => {
|
|
|
365
389
|
* so it stops wherever `jsonTokByteAt` would have answered `-1`.
|
|
366
390
|
*/
|
|
367
391
|
const jsonTokSkipBlank = (src: u8[], i: i32, end: i32): i32 => {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
}
|
|
392
|
+
// `end` and the length are each compared, rather than the smaller of the
|
|
393
|
+
// two once, so every read below is proved inside `src`.
|
|
371
394
|
const n: i32 = toI32(src.length)
|
|
372
|
-
|
|
373
|
-
if (i >= to) {
|
|
395
|
+
if (i < 0 || i >= end || i >= n) {
|
|
374
396
|
return i
|
|
375
397
|
}
|
|
376
398
|
// Most runs between tokens are no blank or one, so those two bytes are
|
|
@@ -379,10 +401,10 @@ const jsonTokSkipBlank = (src: u8[], i: i32, end: i32): i32 => {
|
|
|
379
401
|
if (!jsonTokIsBlank(toI32(src[i]))) {
|
|
380
402
|
return i
|
|
381
403
|
}
|
|
382
|
-
if (i + 1 >=
|
|
404
|
+
if (i + 1 >= end || i + 1 >= n || !jsonTokIsBlank(toI32(src[i + 1]))) {
|
|
383
405
|
return i + 1
|
|
384
406
|
}
|
|
385
|
-
return skipBlank(src, i + 2,
|
|
407
|
+
return skipBlank(src, i + 2, end < n ? end : n)
|
|
386
408
|
}
|
|
387
409
|
|
|
388
410
|
/**
|
|
@@ -407,7 +429,8 @@ const jsonTokSkipPlain = (src: u8[], i: i32, limit: i32): i32 => {
|
|
|
407
429
|
}
|
|
408
430
|
|
|
409
431
|
/** The value of one hex digit, or `-1`. */
|
|
410
|
-
const jsonTokHexDigit = (
|
|
432
|
+
const jsonTokHexDigit = (byte: u8): i32 => {
|
|
433
|
+
const b: i32 = toI32(byte)
|
|
411
434
|
if (jsonTokIsDigit(b)) {
|
|
412
435
|
return b - JSON_TOK_DIGIT_0
|
|
413
436
|
}
|
|
@@ -426,18 +449,22 @@ const jsonTokHexDigit = (b: i32): i32 => {
|
|
|
426
449
|
* hex digit.
|
|
427
450
|
*/
|
|
428
451
|
const jsonTokHex4 = (src: u8[], at: i32, end: i32): i32 => {
|
|
452
|
+
const n: i32 = toI32(src.length)
|
|
429
453
|
let value: i32 = 0
|
|
454
|
+
let i: i32 = at
|
|
430
455
|
let k: i32 = 0
|
|
431
456
|
while (k < 4) {
|
|
432
|
-
|
|
433
|
-
if (b < 0) {
|
|
457
|
+
if (i < 0 || i >= end || i >= n) {
|
|
434
458
|
return JSON_TOK_ERR_TRUNCATED
|
|
435
459
|
}
|
|
436
|
-
const digit: i32 = jsonTokHexDigit(
|
|
460
|
+
const digit: i32 = jsonTokHexDigit(src[i])
|
|
437
461
|
if (digit < 0) {
|
|
438
462
|
return JSON_TOK_ERR_ESCAPE
|
|
439
463
|
}
|
|
440
|
-
value
|
|
464
|
+
// `value` is below 2^12 before the shift and `digit` below 16, so this is
|
|
465
|
+
// `value * 16 + digit`, written so no step can overflow.
|
|
466
|
+
value = (value << 4) | digit
|
|
467
|
+
i += 1
|
|
441
468
|
k += 1
|
|
442
469
|
}
|
|
443
470
|
return value
|
|
@@ -447,7 +474,7 @@ const jsonTokHex4 = (src: u8[], at: i32, end: i32): i32 => {
|
|
|
447
474
|
const jsonTokInRange = (b: i32, low: i32, high: i32): boolean => b >= low && b <= high
|
|
448
475
|
|
|
449
476
|
/**
|
|
450
|
-
*
|
|
477
|
+
* Where the UTF-8 sequence whose lead byte, at `at`, is not ASCII ends, or
|
|
451
478
|
* `JSON_TOK_ERR_UTF8`. RFC 3629 §4's table, row by row: the second byte's
|
|
452
479
|
* range depends on the lead, which is what refuses an overlong form (`C0`,
|
|
453
480
|
* `C1`, `E0 80`–`E0 9F`, `F0 80`–`F0 8F`), a surrogate (`ED A0`–`ED BF`) and a
|
|
@@ -455,9 +482,15 @@ const jsonTokInRange = (b: i32, low: i32, high: i32): boolean => b >= low && b <
|
|
|
455
482
|
* well formed as far as the window goes and is cut short by its end is
|
|
456
483
|
* `JSON_TOK_ERR_TRUNCATED`, as any other value the window cuts short is.
|
|
457
484
|
*/
|
|
458
|
-
const
|
|
459
|
-
|
|
460
|
-
|
|
485
|
+
const jsonTokUtf8End = (src: u8[], at: i32, end: i32): i32 => {
|
|
486
|
+
// Each byte is read only once the window and the array are both known to
|
|
487
|
+
// reach it, so every index and every offset below is proved; a byte past
|
|
488
|
+
// either is the `-1` `jsonTokByteAt` would answer, which no range holds.
|
|
489
|
+
const n: i32 = toI32(src.length)
|
|
490
|
+
if (at < 0 || at >= end || at >= n) {
|
|
491
|
+
return JSON_TOK_ERR_UTF8
|
|
492
|
+
}
|
|
493
|
+
const lead: i32 = toI32(src[at])
|
|
461
494
|
let low: i32 = JSON_TOK_CONTINUATION_LOW
|
|
462
495
|
let high: i32 = JSON_TOK_CONTINUATION_HIGH
|
|
463
496
|
let length: i32 = 0
|
|
@@ -480,18 +513,31 @@ const jsonTokUtf8Length = (src: u8[], at: i32, end: i32): i32 => {
|
|
|
480
513
|
} else {
|
|
481
514
|
return JSON_TOK_ERR_UTF8
|
|
482
515
|
}
|
|
483
|
-
if (
|
|
484
|
-
return
|
|
516
|
+
if (at + 1 >= end || at + 1 >= n) {
|
|
517
|
+
return JSON_TOK_ERR_TRUNCATED
|
|
485
518
|
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
}
|
|
492
|
-
k += 1
|
|
519
|
+
if (!jsonTokInRange(toI32(src[at + 1]), low, high)) {
|
|
520
|
+
return JSON_TOK_ERR_UTF8
|
|
521
|
+
}
|
|
522
|
+
if (length === 2) {
|
|
523
|
+
return at + 2
|
|
493
524
|
}
|
|
494
|
-
|
|
525
|
+
if (at + 2 >= end || at + 2 >= n) {
|
|
526
|
+
return JSON_TOK_ERR_TRUNCATED
|
|
527
|
+
}
|
|
528
|
+
if (!jsonTokInRange(toI32(src[at + 2]), JSON_TOK_CONTINUATION_LOW, JSON_TOK_CONTINUATION_HIGH)) {
|
|
529
|
+
return JSON_TOK_ERR_UTF8
|
|
530
|
+
}
|
|
531
|
+
if (length === 3) {
|
|
532
|
+
return at + 3
|
|
533
|
+
}
|
|
534
|
+
if (at + 3 >= end || at + 3 >= n) {
|
|
535
|
+
return JSON_TOK_ERR_TRUNCATED
|
|
536
|
+
}
|
|
537
|
+
if (!jsonTokInRange(toI32(src[at + 3]), JSON_TOK_CONTINUATION_LOW, JSON_TOK_CONTINUATION_HIGH)) {
|
|
538
|
+
return JSON_TOK_ERR_UTF8
|
|
539
|
+
}
|
|
540
|
+
return at + 4
|
|
495
541
|
}
|
|
496
542
|
|
|
497
543
|
/** How many bytes code point `cp` takes in UTF-8. */
|
|
@@ -510,7 +556,8 @@ const jsonTokUtf8Byte = (cp: i32, width: i32, k: i32): i32 => {
|
|
|
510
556
|
if (width === 1) {
|
|
511
557
|
return cp
|
|
512
558
|
}
|
|
513
|
-
|
|
559
|
+
// A width is 1 to 4 and `k` below it, so the masks change neither.
|
|
560
|
+
const shift: i32 = ((width & 7) - 1 - (k & 7)) * JSON_TOK_CONTINUATION_BITS
|
|
514
561
|
const bits: i32 = (cp >> shift) & JSON_TOK_LOW_SIX
|
|
515
562
|
if (k > 0) {
|
|
516
563
|
return JSON_TOK_CONTINUATION_LOW | bits
|
|
@@ -545,18 +592,6 @@ const jsonTokShortEscape = (b: i32): i32 => {
|
|
|
545
592
|
}
|
|
546
593
|
}
|
|
547
594
|
|
|
548
|
-
/**
|
|
549
|
-
* How many raw bytes the escape at `at` takes, given the code point it decodes
|
|
550
|
-
* to: 12 for a surrogate pair, the only escape past U+FFFF, 6 for any other
|
|
551
|
-
* `\u`, 2 for a short one.
|
|
552
|
-
*/
|
|
553
|
-
const jsonTokEscapeLength = (src: u8[], at: i32, end: i32, cp: i32): i32 => {
|
|
554
|
-
if (cp >= JSON_TOK_FOUR_BYTE_FLOOR) {
|
|
555
|
-
return 12
|
|
556
|
-
}
|
|
557
|
-
return jsonTokByteAt(src, at + 1, end) === JSON_TOK_LOWER_U ? 6 : 2
|
|
558
|
-
}
|
|
559
|
-
|
|
560
595
|
/**
|
|
561
596
|
* The tokenizer. One per connection or per document; `reset` reuses it, and
|
|
562
597
|
* nothing it does after the constructor allocates, except `numberF64`'s
|
|
@@ -587,7 +622,7 @@ export class JsonTokenizer {
|
|
|
587
622
|
* @internal Dead: written by the constructor and `reset`, never read.
|
|
588
623
|
*/
|
|
589
624
|
off: i32
|
|
590
|
-
private end:
|
|
625
|
+
private end: JsonTokEnd
|
|
591
626
|
/** The next byte `next()` reads. */
|
|
592
627
|
private pos: i32
|
|
593
628
|
private maxDepth: i32
|
|
@@ -661,11 +696,11 @@ export class JsonTokenizer {
|
|
|
661
696
|
this.maxStringBytes = limits.maxStringBytes < 0 ? 0 : limits.maxStringBytes
|
|
662
697
|
this.maxNumberBytes = limits.maxNumberBytes < 0 ? 0 : limits.maxNumberBytes
|
|
663
698
|
this.maxTotalBytes = limits.maxTotalBytes < 0 ? 0 : limits.maxTotalBytes
|
|
664
|
-
this.stack =
|
|
699
|
+
this.stack = jsonTokDepthStack(this.maxDepth)
|
|
665
700
|
this.src = src
|
|
666
701
|
this.off = 0
|
|
667
702
|
this.end = 0
|
|
668
|
-
this.stringFrom =
|
|
703
|
+
this.stringFrom = ~this.maxStringBytes
|
|
669
704
|
this.pos = 0
|
|
670
705
|
this.depth = 0
|
|
671
706
|
this.inside = 0
|
|
@@ -707,22 +742,30 @@ export class JsonTokenizer {
|
|
|
707
742
|
this.errorAt = -1
|
|
708
743
|
this.stringBytes = 0
|
|
709
744
|
this.integral = false
|
|
710
|
-
// The
|
|
711
|
-
|
|
745
|
+
// The window's end is summed in an `i64`, where two `i32`s cannot
|
|
746
|
+
// overflow, and kept only once it is known to fit in `src` and in
|
|
747
|
+
// `JsonTokEnd`.
|
|
748
|
+
const stop: i64 = toI64(off) + toI64(len)
|
|
749
|
+
// At most the length of `src` whenever it is kept, so it fits.
|
|
750
|
+
const last: i32 = toI32(stop)
|
|
751
|
+
if (off < 0 || len < 0 || stop > toI64(toI32(src.length)) || last < 0 || last > 2147483583) {
|
|
712
752
|
this.off = 0
|
|
713
753
|
this.end = 0
|
|
714
|
-
|
|
754
|
+
// `-1 - maxStringBytes`, which a complement cannot overflow.
|
|
755
|
+
this.stringFrom = ~this.maxStringBytes
|
|
715
756
|
this.pos = 0
|
|
716
757
|
this.fail(JSON_TOK_ERR_WINDOW, off)
|
|
717
758
|
return
|
|
718
759
|
}
|
|
719
760
|
this.off = off
|
|
720
|
-
this.end =
|
|
721
|
-
//
|
|
722
|
-
|
|
761
|
+
this.end = last
|
|
762
|
+
// At least `-1 - maxStringBytes`, which fits because `maxStringBytes` is
|
|
763
|
+
// at least 0.
|
|
764
|
+
this.stringFrom = toI32(toI64(off) + toI64(len) - toI64(1) - toI64(this.maxStringBytes))
|
|
723
765
|
this.pos = off
|
|
724
766
|
if (len > this.maxTotalBytes) {
|
|
725
|
-
|
|
767
|
+
// Below `off + len`, so it fits.
|
|
768
|
+
this.fail(JSON_TOK_ERR_TOTAL_LIMIT, toI32(toI64(off) + toI64(this.maxTotalBytes)))
|
|
726
769
|
}
|
|
727
770
|
}
|
|
728
771
|
|
|
@@ -747,17 +790,25 @@ export class JsonTokenizer {
|
|
|
747
790
|
next(): i32 {
|
|
748
791
|
const src: u8[] = this.src
|
|
749
792
|
const end: i32 = this.end
|
|
750
|
-
// `jsonTokSkipBlank` and then `jsonTokByteAt`, fused:
|
|
751
|
-
//
|
|
793
|
+
// `jsonTokSkipBlank` and then `jsonTokByteAt`, fused: the byte the skip
|
|
794
|
+
// stops at is the one read. `end` and the length are each compared, so
|
|
795
|
+
// both reads are proved inside `src`.
|
|
752
796
|
const n: i32 = toI32(src.length)
|
|
753
|
-
const to: i32 = end < n ? end : n
|
|
754
797
|
let at: i32 = this.pos
|
|
755
798
|
let b: i32 = -1
|
|
756
|
-
|
|
799
|
+
// One past the byte read, taken where `at < end` proves it fits; only a
|
|
800
|
+
// colon or a comma reads it, and either means a byte was read.
|
|
801
|
+
let past: i32 = 0
|
|
802
|
+
if (at >= 0 && at < end && at < n) {
|
|
757
803
|
b = toI32(src[at])
|
|
804
|
+
past = at + 1
|
|
758
805
|
if (jsonTokIsBlank(b)) {
|
|
759
|
-
at = jsonTokSkipBlank(src, at + 1,
|
|
760
|
-
b =
|
|
806
|
+
at = jsonTokSkipBlank(src, at + 1, end)
|
|
807
|
+
b = -1
|
|
808
|
+
if (at >= 0 && at < end && at < n) {
|
|
809
|
+
b = toI32(src[at])
|
|
810
|
+
past = at + 1
|
|
811
|
+
}
|
|
761
812
|
}
|
|
762
813
|
}
|
|
763
814
|
const want: i32 = this.want
|
|
@@ -776,12 +827,12 @@ export class JsonTokenizer {
|
|
|
776
827
|
break
|
|
777
828
|
case JSON_TOK_COLON:
|
|
778
829
|
if (want === JSON_TOK_WANT_COLON) {
|
|
779
|
-
return this.value(jsonTokSkipBlank(src,
|
|
830
|
+
return this.value(jsonTokSkipBlank(src, past, end))
|
|
780
831
|
}
|
|
781
832
|
break
|
|
782
833
|
case JSON_TOK_COMMA:
|
|
783
834
|
if (want === JSON_TOK_WANT_COMMA_OR_CLOSE) {
|
|
784
|
-
const after: i32 = jsonTokSkipBlank(src,
|
|
835
|
+
const after: i32 = jsonTokSkipBlank(src, past, end)
|
|
785
836
|
if (this.inside === JSON_TOK_IN_OBJECT) {
|
|
786
837
|
return this.key(after)
|
|
787
838
|
}
|
|
@@ -790,12 +841,12 @@ export class JsonTokenizer {
|
|
|
790
841
|
break
|
|
791
842
|
case JSON_TOK_CLOSE_BRACE:
|
|
792
843
|
if (want === JSON_TOK_WANT_COMMA_OR_CLOSE || want === JSON_TOK_WANT_KEY_OR_CLOSE) {
|
|
793
|
-
return this.close(at, JSON_TOK_IN_OBJECT)
|
|
844
|
+
return this.close(at, past, JSON_TOK_IN_OBJECT)
|
|
794
845
|
}
|
|
795
846
|
break
|
|
796
847
|
case JSON_TOK_CLOSE_BRACKET:
|
|
797
848
|
if (want === JSON_TOK_WANT_COMMA_OR_CLOSE || want === JSON_TOK_WANT_VALUE_OR_CLOSE) {
|
|
798
|
-
return this.close(at, JSON_TOK_IN_ARRAY)
|
|
849
|
+
return this.close(at, past, JSON_TOK_IN_ARRAY)
|
|
799
850
|
}
|
|
800
851
|
break
|
|
801
852
|
default:
|
|
@@ -841,21 +892,24 @@ export class JsonTokenizer {
|
|
|
841
892
|
return k
|
|
842
893
|
}
|
|
843
894
|
|
|
844
|
-
/** A closer at `at`, which must match the innermost container, `inside`. */
|
|
845
|
-
private close(at: i32, inside: i32): i32 {
|
|
846
|
-
|
|
895
|
+
/** A closer at `at`, ending at `past`, which must match the innermost container, `inside`. */
|
|
896
|
+
private close(at: i32, past: i32, inside: i32): i32 {
|
|
897
|
+
const depth: i32 = this.depth
|
|
898
|
+
// A container is open whenever `inside` names one, so the depth test
|
|
899
|
+
// refuses nothing the first does not.
|
|
900
|
+
if (this.inside !== inside || depth <= 0) {
|
|
847
901
|
return this.fail(JSON_TOK_ERR_SYNTAX, at)
|
|
848
902
|
}
|
|
849
903
|
// The container closed was the stack's last byte, so the one now
|
|
850
904
|
// innermost is the byte before it, or none.
|
|
851
|
-
const d: i32 =
|
|
905
|
+
const d: i32 = depth - 1
|
|
852
906
|
this.depth = d
|
|
853
907
|
this.inside = d > 0 && d <= toI32(this.stack.length) ? toI32(this.stack[d - 1]) : 0
|
|
854
|
-
return this.emit(inside === JSON_TOK_IN_OBJECT ? JSON_TOK_OBJECT_END : JSON_TOK_ARRAY_END, at,
|
|
908
|
+
return this.emit(inside === JSON_TOK_IN_OBJECT ? JSON_TOK_OBJECT_END : JSON_TOK_ARRAY_END, at, past)
|
|
855
909
|
}
|
|
856
910
|
|
|
857
|
-
/** An opener at `at`, refused past the depth limit. */
|
|
858
|
-
private open(at: i32, inside: i32): i32 {
|
|
911
|
+
/** An opener at `at`, ending at `past`, refused past the depth limit. */
|
|
912
|
+
private open(at: i32, past: i32, inside: i32): i32 {
|
|
859
913
|
const d: i32 = this.depth
|
|
860
914
|
if (d >= this.maxDepth || d < 0 || d >= toI32(this.stack.length)) {
|
|
861
915
|
return this.fail(JSON_TOK_ERR_DEPTH, at)
|
|
@@ -863,7 +917,7 @@ export class JsonTokenizer {
|
|
|
863
917
|
this.stack[d] = toU8(inside)
|
|
864
918
|
this.depth = d + 1
|
|
865
919
|
this.inside = inside
|
|
866
|
-
return this.emit(inside === JSON_TOK_IN_OBJECT ? JSON_TOK_OBJECT_BEGIN : JSON_TOK_ARRAY_BEGIN, at,
|
|
920
|
+
return this.emit(inside === JSON_TOK_IN_OBJECT ? JSON_TOK_OBJECT_BEGIN : JSON_TOK_ARRAY_BEGIN, at, past)
|
|
867
921
|
}
|
|
868
922
|
|
|
869
923
|
/** A key at `at`: a string, whose colon the next call reads. */
|
|
@@ -878,8 +932,14 @@ export class JsonTokenizer {
|
|
|
878
932
|
/** A value at `at`. */
|
|
879
933
|
private value(at: i32): i32 {
|
|
880
934
|
const src: u8[] = this.src
|
|
881
|
-
const end:
|
|
882
|
-
|
|
935
|
+
const end: JsonTokEnd = this.end
|
|
936
|
+
// What `jsonTokByteAt` answers outside the window, read here so that
|
|
937
|
+
// inside it `at + 1` is proved to fit.
|
|
938
|
+
if (at < 0 || at >= end || at >= toI32(src.length)) {
|
|
939
|
+
return this.fail(JSON_TOK_ERR_TRUNCATED, at)
|
|
940
|
+
}
|
|
941
|
+
const b: i32 = toI32(src[at])
|
|
942
|
+
const past: i32 = at + 1
|
|
883
943
|
if (b === JSON_TOK_QUOTE) {
|
|
884
944
|
return this.string(at, JSON_TOK_STRING)
|
|
885
945
|
}
|
|
@@ -888,9 +948,9 @@ export class JsonTokenizer {
|
|
|
888
948
|
}
|
|
889
949
|
switch (b) {
|
|
890
950
|
case JSON_TOK_OPEN_BRACE:
|
|
891
|
-
return this.open(at, JSON_TOK_IN_OBJECT)
|
|
951
|
+
return this.open(at, past, JSON_TOK_IN_OBJECT)
|
|
892
952
|
case JSON_TOK_OPEN_BRACKET:
|
|
893
|
-
return this.open(at, JSON_TOK_IN_ARRAY)
|
|
953
|
+
return this.open(at, past, JSON_TOK_IN_ARRAY)
|
|
894
954
|
case JSON_TOK_LOWER_T:
|
|
895
955
|
return this.literal(at, "true", JSON_TOK_TRUE)
|
|
896
956
|
case JSON_TOK_LOWER_F:
|
|
@@ -898,24 +958,29 @@ export class JsonTokenizer {
|
|
|
898
958
|
case JSON_TOK_LOWER_N:
|
|
899
959
|
return this.literal(at, "null", JSON_TOK_NULL)
|
|
900
960
|
default:
|
|
901
|
-
return this.fail(
|
|
961
|
+
return this.fail(JSON_TOK_ERR_SYNTAX, at)
|
|
902
962
|
}
|
|
903
963
|
}
|
|
904
964
|
|
|
905
965
|
/** `true`, `false` or `null` at `at`; a window that ends inside the word is truncated. */
|
|
906
966
|
private literal(at: i32, word: string, k: i32): i32 {
|
|
967
|
+
const src: u8[] = this.src
|
|
968
|
+
const end: JsonTokEnd = this.end
|
|
969
|
+
const srcLen: i32 = toI32(src.length)
|
|
907
970
|
const n: i32 = toI32(word.length)
|
|
971
|
+
let i: i32 = at
|
|
908
972
|
let matched: i32 = 0
|
|
909
|
-
while (
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
973
|
+
while (matched < n) {
|
|
974
|
+
if (i < 0 || i >= end || i >= srcLen) {
|
|
975
|
+
return this.fail(i >= end ? JSON_TOK_ERR_TRUNCATED : JSON_TOK_ERR_SYNTAX, i)
|
|
976
|
+
}
|
|
977
|
+
if (toI32(src[i]) !== toI32(word.charCodeAt(matched))) {
|
|
978
|
+
return this.fail(JSON_TOK_ERR_SYNTAX, i)
|
|
979
|
+
}
|
|
980
|
+
i += 1
|
|
913
981
|
matched += 1
|
|
914
982
|
}
|
|
915
|
-
|
|
916
|
-
return this.emit(k, at, at + n)
|
|
917
|
-
}
|
|
918
|
-
return this.fail(at + matched >= this.end ? JSON_TOK_ERR_TRUNCATED : JSON_TOK_ERR_SYNTAX, at + matched)
|
|
983
|
+
return this.emit(k, at, i)
|
|
919
984
|
}
|
|
920
985
|
|
|
921
986
|
/**
|
|
@@ -931,24 +996,35 @@ export class JsonTokenizer {
|
|
|
931
996
|
*/
|
|
932
997
|
private number(at: i32): i32 {
|
|
933
998
|
const src: u8[] = this.src
|
|
934
|
-
const
|
|
999
|
+
const n: i32 = toI32(src.length)
|
|
1000
|
+
const stop: JsonTokEnd = this.end
|
|
1001
|
+
const cap: i32 = this.maxNumberBytes
|
|
1002
|
+
if (at < 0 || at >= stop || at >= n) {
|
|
1003
|
+
// `value` has read the byte at `at` before it calls here, so this is
|
|
1004
|
+
// not reached; it answers what reading past the window answers.
|
|
1005
|
+
return this.fail(JSON_TOK_ERR_TRUNCATED, at)
|
|
1006
|
+
}
|
|
1007
|
+
// One byte past the length limit, or the window's end when that comes
|
|
1008
|
+
// first. Past the limit `at + cap + 1` is at most `stop`, so it fits.
|
|
1009
|
+
const end: i32 = cap >= stop - at ? stop : toI32(toI64(at) + toI64(cap) + toI64(1))
|
|
935
1010
|
let i: i32 = at
|
|
936
|
-
const negative: boolean =
|
|
1011
|
+
const negative: boolean = toI32(src[at]) === JSON_TOK_MINUS
|
|
937
1012
|
if (negative) {
|
|
938
|
-
i
|
|
1013
|
+
i = at + 1
|
|
939
1014
|
}
|
|
940
1015
|
const digitsAt: i32 = i
|
|
941
|
-
|
|
1016
|
+
if (i >= end || i >= n) {
|
|
1017
|
+
return this.numberRefused(JSON_TOK_ERR_TRUNCATED, at, i)
|
|
1018
|
+
}
|
|
1019
|
+
const first: i32 = toI32(src[i])
|
|
942
1020
|
if (first < JSON_TOK_DIGIT_0 || first > JSON_TOK_DIGIT_9) {
|
|
943
|
-
return this.numberRefused(
|
|
1021
|
+
return this.numberRefused(JSON_TOK_ERR_NUMBER, at, i)
|
|
944
1022
|
}
|
|
945
1023
|
// The integer part's digits and then the fraction's are folded into one
|
|
946
1024
|
// `u64` as they are passed, eight at a time while eight bytes remain. It
|
|
947
1025
|
// wraps on a long run, which needs no count: the digits are counted from
|
|
948
1026
|
// the indices once the runs end, and `numberF64` reads the mantissa only
|
|
949
1027
|
// when there are 19 or fewer. A leading `0` is its whole integer part.
|
|
950
|
-
const n: i32 = toI32(src.length)
|
|
951
|
-
const to: i32 = end < n ? end : n
|
|
952
1028
|
let mantissa: u64 = toU64(0)
|
|
953
1029
|
let fractionAt: i32 = -1
|
|
954
1030
|
let run: boolean = first !== JSON_TOK_DIGIT_0
|
|
@@ -960,13 +1036,7 @@ export class JsonTokenizer {
|
|
|
960
1036
|
// A run shorter than eight digits, most of a document's, ends
|
|
961
1037
|
// before its eighth byte: that one byte is read first, and only a
|
|
962
1038
|
// run that reaches it pays for the eight-byte read and check.
|
|
963
|
-
while (
|
|
964
|
-
i >= 0 &&
|
|
965
|
-
i < JSON_TOK_END_CEILING &&
|
|
966
|
-
i + 8 <= to &&
|
|
967
|
-
i + 8 <= n &&
|
|
968
|
-
jsonTokIsDigit(toI32(src[i + 7]))
|
|
969
|
-
) {
|
|
1039
|
+
while (i >= 0 && i <= n - 8 && i + 8 <= end && jsonTokIsDigit(toI32(src[i + 7]))) {
|
|
970
1040
|
const word: u64 = jsonTokLoad8(src, i)
|
|
971
1041
|
if (!jsonTokEightDigits(word)) {
|
|
972
1042
|
break
|
|
@@ -974,7 +1044,7 @@ export class JsonTokenizer {
|
|
|
974
1044
|
mantissa = mantissa * toU64(100000000) + jsonTokParseEight(word)
|
|
975
1045
|
i += 8
|
|
976
1046
|
}
|
|
977
|
-
while (i >= 0 && i <
|
|
1047
|
+
while (i >= 0 && i < end && i < n) {
|
|
978
1048
|
const digit: i32 = toI32(src[i]) - JSON_TOK_DIGIT_0
|
|
979
1049
|
if (digit < 0 || digit > 9) {
|
|
980
1050
|
break
|
|
@@ -983,7 +1053,7 @@ export class JsonTokenizer {
|
|
|
983
1053
|
i += 1
|
|
984
1054
|
}
|
|
985
1055
|
}
|
|
986
|
-
if (fractionAt >= 0 ||
|
|
1056
|
+
if (fractionAt >= 0 || i < 0 || i >= end || i >= n || toI32(src[i]) !== JSON_TOK_DOT) {
|
|
987
1057
|
break
|
|
988
1058
|
}
|
|
989
1059
|
i += 1
|
|
@@ -996,15 +1066,25 @@ export class JsonTokenizer {
|
|
|
996
1066
|
}
|
|
997
1067
|
let integral: boolean = fractionAt < 0
|
|
998
1068
|
let digits: i32 = integral ? i - digitsAt : i - digitsAt - 1
|
|
999
|
-
|
|
1000
|
-
const
|
|
1001
|
-
|
|
1069
|
+
// The fraction's length, less or plus the exponent, is the scale.
|
|
1070
|
+
const fractionFrom: i32 = fractionAt >= 0 ? fractionAt : 0
|
|
1071
|
+
const fractionTo: i32 = fractionAt >= 0 ? i : 0
|
|
1072
|
+
let exponent: i32 = 0
|
|
1073
|
+
let minus: boolean = false
|
|
1074
|
+
if (
|
|
1075
|
+
i >= 0 &&
|
|
1076
|
+
i < end &&
|
|
1077
|
+
i < n &&
|
|
1078
|
+
(toI32(src[i]) === JSON_TOK_LOWER_E || toI32(src[i]) === JSON_TOK_UPPER_E)
|
|
1079
|
+
) {
|
|
1002
1080
|
integral = false
|
|
1003
1081
|
i += 1
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1082
|
+
if (i < end && i < n) {
|
|
1083
|
+
const sign: i32 = toI32(src[i])
|
|
1084
|
+
minus = sign === JSON_TOK_MINUS
|
|
1085
|
+
if (minus || sign === JSON_TOK_PLUS) {
|
|
1086
|
+
i += 1
|
|
1087
|
+
}
|
|
1008
1088
|
}
|
|
1009
1089
|
const digit: i32 = jsonTokByteAt(src, i, end)
|
|
1010
1090
|
if (!jsonTokIsDigit(digit)) {
|
|
@@ -1012,23 +1092,37 @@ export class JsonTokenizer {
|
|
|
1012
1092
|
}
|
|
1013
1093
|
// Accumulated until it reaches the cap; a digit after that sends
|
|
1014
1094
|
// `numberF64` to `parseFloat`, which reads any exponent.
|
|
1015
|
-
|
|
1016
|
-
|
|
1095
|
+
while (i >= 0 && i < end && i < n) {
|
|
1096
|
+
const d: i32 = toI32(src[i]) - JSON_TOK_DIGIT_0
|
|
1097
|
+
if (d < 0 || d > 9) {
|
|
1098
|
+
break
|
|
1099
|
+
}
|
|
1017
1100
|
if (exponent < JSON_TOK_EXPONENT_CAP) {
|
|
1018
|
-
|
|
1101
|
+
// Below ten times the cap plus ten, so it fits.
|
|
1102
|
+
exponent = toI32(toI64(exponent) * toI64(JSON_TOK_TEN) + toI64(d))
|
|
1019
1103
|
} else {
|
|
1020
1104
|
digits = JSON_TOK_NUMBER_WALK
|
|
1021
1105
|
}
|
|
1022
1106
|
i += 1
|
|
1023
1107
|
}
|
|
1024
|
-
scale += minus ? -exponent : exponent
|
|
1025
1108
|
}
|
|
1026
|
-
|
|
1027
|
-
|
|
1109
|
+
// Summed in an `i64`: a fraction of more than 2^31 digits, which no `i32`
|
|
1110
|
+
// scale can hold, is sent to `parseFloat` below rather than overflow.
|
|
1111
|
+
let scale: i64 = minus
|
|
1112
|
+
? toI64(fractionFrom) - toI64(fractionTo) - toI64(exponent)
|
|
1113
|
+
: toI64(fractionFrom) - toI64(fractionTo) + toI64(exponent)
|
|
1114
|
+
if (scale < toI64(-2147483648) || scale > toI64(2147483647)) {
|
|
1115
|
+
digits = JSON_TOK_NUMBER_WALK
|
|
1116
|
+
scale = toI64(0)
|
|
1117
|
+
}
|
|
1118
|
+
if (toI64(i) - toI64(at) > toI64(cap)) {
|
|
1119
|
+
// Past the limit, so `at + cap` is below `i` and fits.
|
|
1120
|
+
return this.fail(JSON_TOK_ERR_NUMBER_LIMIT, toI32(toI64(at) + toI64(cap)))
|
|
1028
1121
|
}
|
|
1029
1122
|
this.integral = integral
|
|
1030
1123
|
this.numberMantissa = mantissa
|
|
1031
|
-
|
|
1124
|
+
// Inside `i32` once the test above has passed.
|
|
1125
|
+
this.numberScale = toI32(scale)
|
|
1032
1126
|
this.numberDigits = digits
|
|
1033
1127
|
this.numberNegative = negative
|
|
1034
1128
|
return this.emit(JSON_TOK_NUMBER, at, i)
|
|
@@ -1036,8 +1130,10 @@ export class JsonTokenizer {
|
|
|
1036
1130
|
|
|
1037
1131
|
/** `code` at `i`, unless the number starting at `at` has run past its limit, which is the error then. */
|
|
1038
1132
|
private numberRefused(code: i32, at: i32, i: i32): i32 {
|
|
1039
|
-
|
|
1040
|
-
|
|
1133
|
+
const cap: i32 = this.maxNumberBytes
|
|
1134
|
+
if (toI64(i) - toI64(at) > toI64(cap)) {
|
|
1135
|
+
// Below `i`, so it fits.
|
|
1136
|
+
return this.fail(JSON_TOK_ERR_NUMBER_LIMIT, toI32(toI64(at) + toI64(cap)))
|
|
1041
1137
|
}
|
|
1042
1138
|
return this.fail(code, i)
|
|
1043
1139
|
}
|
|
@@ -1050,31 +1146,41 @@ export class JsonTokenizer {
|
|
|
1050
1146
|
*/
|
|
1051
1147
|
private string(at: i32, k: i32): i32 {
|
|
1052
1148
|
const src: u8[] = this.src
|
|
1053
|
-
const end:
|
|
1149
|
+
const end: JsonTokEnd = this.end
|
|
1150
|
+
const n: i32 = toI32(src.length)
|
|
1151
|
+
// Each caller read the opening quote at `at`, so this refuses nothing; it
|
|
1152
|
+
// proves `at + 1` fits.
|
|
1153
|
+
if (at < 0 || at >= end) {
|
|
1154
|
+
return this.fail(JSON_TOK_ERR_TRUNCATED, at)
|
|
1155
|
+
}
|
|
1156
|
+
const first: i32 = at + 1
|
|
1054
1157
|
// Where the string would pass its limit, or `end` when it cannot pass it
|
|
1055
|
-
// before the window ends
|
|
1056
|
-
//
|
|
1057
|
-
const limit: i32 = at >= this.stringFrom ? end :
|
|
1058
|
-
let i: i32 =
|
|
1059
|
-
|
|
1158
|
+
// before the window ends; below `stringFrom`, `at + 1 + maxStringBytes`
|
|
1159
|
+
// is less than `end`, so it fits.
|
|
1160
|
+
const limit: i32 = at >= this.stringFrom ? end : toI32(toI64(first) + toI64(this.maxStringBytes))
|
|
1161
|
+
let i: i32 = first
|
|
1162
|
+
// How many more bytes the escapes so far are written with than they
|
|
1163
|
+
// decode to: the decoded length is the raw one less this.
|
|
1164
|
+
let saved: i32 = 0
|
|
1165
|
+
let past: i32 = 0
|
|
1060
1166
|
while (true) {
|
|
1061
1167
|
// A run of plain bytes is stepped over in one call. Each ASCII byte or
|
|
1062
1168
|
// UTF-8 sequence of it is one the checks below would pass with
|
|
1063
|
-
// `
|
|
1169
|
+
// `i += width`, decoding to itself, and the run stops short of `limit`,
|
|
1064
1170
|
// so the first byte it leaves is judged by those checks exactly as
|
|
1065
1171
|
// before.
|
|
1066
|
-
const run: i32 = i
|
|
1067
1172
|
i = jsonTokSkipPlain(src, i, limit)
|
|
1068
|
-
|
|
1069
|
-
|
|
1173
|
+
// No byte, at the limit or not, is a window cut short.
|
|
1174
|
+
if (i < 0 || i >= end || i >= n) {
|
|
1175
|
+
return this.fail(JSON_TOK_ERR_TRUNCATED, i)
|
|
1176
|
+
}
|
|
1177
|
+
const b: i32 = toI32(src[i])
|
|
1070
1178
|
if (b === JSON_TOK_QUOTE) {
|
|
1179
|
+
past = i + 1
|
|
1071
1180
|
break
|
|
1072
1181
|
}
|
|
1073
1182
|
if (i >= limit) {
|
|
1074
|
-
return this.fail(
|
|
1075
|
-
}
|
|
1076
|
-
if (b < 0) {
|
|
1077
|
-
return this.fail(JSON_TOK_ERR_TRUNCATED, i)
|
|
1183
|
+
return this.fail(JSON_TOK_ERR_STRING_LIMIT, i)
|
|
1078
1184
|
}
|
|
1079
1185
|
if (b < JSON_TOK_SPACE) {
|
|
1080
1186
|
return this.fail(JSON_TOK_ERR_CONTROL, i)
|
|
@@ -1084,25 +1190,37 @@ export class JsonTokenizer {
|
|
|
1084
1190
|
if (cp < 0) {
|
|
1085
1191
|
return cp
|
|
1086
1192
|
}
|
|
1087
|
-
|
|
1088
|
-
|
|
1193
|
+
// An escape takes 12 bytes for a surrogate pair, the only one past
|
|
1194
|
+
// U+FFFF, 6 for any other `\u` and 2 for a short one. Each is added
|
|
1195
|
+
// alone, so `i < end` proves the sum.
|
|
1196
|
+
let length: i32 = 2
|
|
1197
|
+
if (cp >= JSON_TOK_FOUR_BYTE_FLOOR) {
|
|
1198
|
+
length = 12
|
|
1199
|
+
i += 12
|
|
1200
|
+
} else if (jsonTokByteAt(src, i + 1, end) === JSON_TOK_LOWER_U) {
|
|
1201
|
+
length = 6
|
|
1202
|
+
i += 6
|
|
1203
|
+
} else {
|
|
1204
|
+
i += 2
|
|
1205
|
+
}
|
|
1206
|
+
// At most `i - first`, so it fits.
|
|
1207
|
+
saved = toI32(toI64(saved) + toI64(length) - toI64(jsonTokUtf8Width(cp)))
|
|
1089
1208
|
} else if (b < JSON_TOK_ASCII_END) {
|
|
1090
|
-
decoded += 1
|
|
1091
1209
|
i += 1
|
|
1092
1210
|
} else {
|
|
1093
|
-
const
|
|
1094
|
-
if (
|
|
1095
|
-
return this.fail(
|
|
1211
|
+
const stop: i32 = jsonTokUtf8End(src, i, end)
|
|
1212
|
+
if (stop < 0) {
|
|
1213
|
+
return this.fail(stop, i)
|
|
1096
1214
|
}
|
|
1097
|
-
|
|
1098
|
-
i += width
|
|
1215
|
+
i = stop
|
|
1099
1216
|
}
|
|
1100
1217
|
}
|
|
1101
1218
|
if (i > limit) {
|
|
1102
1219
|
return this.fail(JSON_TOK_ERR_STRING_LIMIT, limit)
|
|
1103
1220
|
}
|
|
1104
|
-
|
|
1105
|
-
|
|
1221
|
+
// No more than the raw length, so it fits.
|
|
1222
|
+
this.stringBytes = toI32(toI64(i) - toI64(first) - toI64(saved))
|
|
1223
|
+
return this.emit(k, at, past)
|
|
1106
1224
|
}
|
|
1107
1225
|
|
|
1108
1226
|
/**
|
|
@@ -1112,7 +1230,12 @@ export class JsonTokenizer {
|
|
|
1112
1230
|
*/
|
|
1113
1231
|
private escapeCodePoint(at: i32): i32 {
|
|
1114
1232
|
const src: u8[] = this.src
|
|
1115
|
-
const end:
|
|
1233
|
+
const end: JsonTokEnd = this.end
|
|
1234
|
+
// Every caller passes a backslash it read inside the window, so this
|
|
1235
|
+
// refuses nothing; it proves each `at + k` below fits.
|
|
1236
|
+
if (at < 0 || at >= end) {
|
|
1237
|
+
return this.fail(JSON_TOK_ERR_TRUNCATED, at)
|
|
1238
|
+
}
|
|
1116
1239
|
const letter: i32 = jsonTokByteAt(src, at + 1, end)
|
|
1117
1240
|
if (letter < 0) {
|
|
1118
1241
|
return this.fail(JSON_TOK_ERR_TRUNCATED, at + 1)
|
|
@@ -1147,9 +1270,9 @@ export class JsonTokenizer {
|
|
|
1147
1270
|
if (low < JSON_TOK_LOW_SURROGATE || low >= JSON_TOK_SURROGATE_END) {
|
|
1148
1271
|
return this.fail(JSON_TOK_ERR_SURROGATE, at)
|
|
1149
1272
|
}
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
)
|
|
1273
|
+
// Each half is its surrogate block's start plus ten bits, so its low ten
|
|
1274
|
+
// bits are what it adds.
|
|
1275
|
+
return JSON_TOK_FOUR_BYTE_FLOOR + (unit & 0x3ff) * 1024 + (low & 0x3ff)
|
|
1153
1276
|
}
|
|
1154
1277
|
|
|
1155
1278
|
/**
|
|
@@ -1172,40 +1295,55 @@ export class JsonTokenizer {
|
|
|
1172
1295
|
*/
|
|
1173
1296
|
stringInto(out: u8[], at: i32): i32 {
|
|
1174
1297
|
const n: i32 = this.stringBytes
|
|
1175
|
-
|
|
1298
|
+
const outLength: i32 = toI32(out.length)
|
|
1299
|
+
if (!this.isText() || at < 0) {
|
|
1300
|
+
return -1
|
|
1301
|
+
}
|
|
1302
|
+
// The decoded length is never negative, so this is `at + n` past the end.
|
|
1303
|
+
const room: i32 = outLength - at
|
|
1304
|
+
if (n > room) {
|
|
1305
|
+
return -1
|
|
1306
|
+
}
|
|
1307
|
+
const window: JsonTokEnd = this.end
|
|
1308
|
+
const quote: i32 = this.tokStart
|
|
1309
|
+
const close: i32 = this.tokEnd
|
|
1310
|
+
// A text token lies inside the window, both quotes included, so this
|
|
1311
|
+
// refuses only fields a caller has overwritten; it proves the offsets
|
|
1312
|
+
// below fit.
|
|
1313
|
+
if (quote < 0 || close < 2 || close > window || close <= quote) {
|
|
1176
1314
|
return -1
|
|
1177
1315
|
}
|
|
1178
1316
|
const src: u8[] = this.src
|
|
1179
|
-
const end: i32 =
|
|
1180
|
-
let i: i32 =
|
|
1181
|
-
let o: i32 = at
|
|
1317
|
+
const end: i32 = close - 1
|
|
1318
|
+
let i: i32 = quote + 1
|
|
1182
1319
|
if (n === end - i) {
|
|
1183
1320
|
// No escape (see `keyEquals`): the bytes are copied as they are, as one
|
|
1184
|
-
// `
|
|
1321
|
+
// `tryCopyBytes` of as many as both arrays hold, which the caller's test
|
|
1185
1322
|
// above has made all of them.
|
|
1186
1323
|
// The source window still in `src` is the case met; the clamps below
|
|
1187
1324
|
// are for one shrunk since it was scanned.
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1325
|
+
const srcLength: i32 = toI32(src.length)
|
|
1326
|
+
if (end <= srcLength) {
|
|
1327
|
+
// A range that does not fit copies nothing, and says so.
|
|
1328
|
+
return tryCopyBytes(out, at, src, i, n) ? n : 0
|
|
1191
1329
|
}
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
}
|
|
1199
|
-
if (outLength - o < count) {
|
|
1200
|
-
count = outLength - o
|
|
1201
|
-
}
|
|
1202
|
-
if (count > 0) {
|
|
1203
|
-
copyBytes(out, o, src, i, count)
|
|
1204
|
-
o += count
|
|
1205
|
-
}
|
|
1330
|
+
let count: i32 = end - i
|
|
1331
|
+
if (srcLength - i < count) {
|
|
1332
|
+
count = srcLength - i
|
|
1333
|
+
}
|
|
1334
|
+
if (room < count) {
|
|
1335
|
+
count = room
|
|
1206
1336
|
}
|
|
1207
|
-
|
|
1337
|
+
if (count > 0) {
|
|
1338
|
+
return tryCopyBytes(out, at, src, i, count) ? count : 0
|
|
1339
|
+
}
|
|
1340
|
+
return 0
|
|
1208
1341
|
}
|
|
1342
|
+
// Bytes decoded so far. Each step decodes no more bytes than it reads, so
|
|
1343
|
+
// this stays at most `i - quote - 1` and fits. They are written from `o`
|
|
1344
|
+
// while `out` has room, which `o` stops at.
|
|
1345
|
+
let written: i32 = 0
|
|
1346
|
+
let o: i32 = at
|
|
1209
1347
|
while (i < end) {
|
|
1210
1348
|
const b: i32 = jsonTokByteAt(src, i, end)
|
|
1211
1349
|
// The plain run before the next escape is copied whole. In a string
|
|
@@ -1214,27 +1352,30 @@ export class JsonTokenizer {
|
|
|
1214
1352
|
// here, as in a run of escapes, skips the scan.
|
|
1215
1353
|
const stop: i32 = b === JSON_TOK_BACKSLASH ? i : jsonTokSkipPlain(src, i, end)
|
|
1216
1354
|
if (stop > i) {
|
|
1217
|
-
|
|
1218
|
-
|
|
1355
|
+
// The run stops at `end` at the latest, so this fits.
|
|
1356
|
+
const count: i32 = toI32(toI64(stop) - toI64(i))
|
|
1357
|
+
if (o >= 0 && o < outLength) {
|
|
1219
1358
|
// The entry guard sized `out` for the decoded length the scan
|
|
1220
1359
|
// measured, but the source can change after the scan: a backslash
|
|
1221
1360
|
// turned plain lets a run outgrow that length, so the copy is cut
|
|
1222
1361
|
// to the room left.
|
|
1223
|
-
const
|
|
1224
|
-
const
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1362
|
+
const left: i32 = outLength - o
|
|
1363
|
+
const fits: i32 = left < count ? left : count
|
|
1364
|
+
// What did not fit was not written, so it does not move `o`.
|
|
1365
|
+
const c: i32 = tryCopyBytes(out, o, src, i, fits) ? fits : 0
|
|
1366
|
+
// No more than the room left, so at most `outLength`.
|
|
1367
|
+
o = toI32(toI64(o) + toI64(c))
|
|
1228
1368
|
}
|
|
1229
|
-
|
|
1369
|
+
written = toI32(toI64(written) + toI64(count))
|
|
1230
1370
|
i = stop
|
|
1231
1371
|
continue
|
|
1232
1372
|
}
|
|
1233
1373
|
if (b !== JSON_TOK_BACKSLASH) {
|
|
1234
1374
|
if (o >= 0 && o < toI32(out.length)) {
|
|
1235
1375
|
out[o] = toU8(b)
|
|
1376
|
+
o += 1
|
|
1236
1377
|
}
|
|
1237
|
-
|
|
1378
|
+
written = toI32(toI64(written) + toI64(1))
|
|
1238
1379
|
i += 1
|
|
1239
1380
|
continue
|
|
1240
1381
|
}
|
|
@@ -1244,13 +1385,23 @@ export class JsonTokenizer {
|
|
|
1244
1385
|
while (k < width) {
|
|
1245
1386
|
if (o >= 0 && o < toI32(out.length)) {
|
|
1246
1387
|
out[o] = toU8(jsonTokUtf8Byte(cp, width, k))
|
|
1388
|
+
o += 1
|
|
1247
1389
|
}
|
|
1248
|
-
|
|
1390
|
+
written = toI32(toI64(written) + toI64(1))
|
|
1249
1391
|
k += 1
|
|
1250
1392
|
}
|
|
1251
|
-
|
|
1393
|
+
// An escape takes 12 bytes for a surrogate pair, the only one past
|
|
1394
|
+
// U+FFFF, 6 for any other `\u` and 2 for a short one. Each is added
|
|
1395
|
+
// alone, so `i < end` proves the sum.
|
|
1396
|
+
if (cp >= JSON_TOK_FOUR_BYTE_FLOOR) {
|
|
1397
|
+
i += 12
|
|
1398
|
+
} else if (jsonTokByteAt(src, i + 1, end) === JSON_TOK_LOWER_U) {
|
|
1399
|
+
i += 6
|
|
1400
|
+
} else {
|
|
1401
|
+
i += 2
|
|
1402
|
+
}
|
|
1252
1403
|
}
|
|
1253
|
-
return
|
|
1404
|
+
return written
|
|
1254
1405
|
}
|
|
1255
1406
|
|
|
1256
1407
|
/**
|
|
@@ -1281,17 +1432,29 @@ export class JsonTokenizer {
|
|
|
1281
1432
|
if (!this.isText() || n !== this.stringBytes) {
|
|
1282
1433
|
return false
|
|
1283
1434
|
}
|
|
1435
|
+
const window: JsonTokEnd = this.end
|
|
1436
|
+
const quote: i32 = this.tokStart
|
|
1437
|
+
const close: i32 = this.tokEnd
|
|
1438
|
+
// A text token lies inside the window, both quotes included, so this
|
|
1439
|
+
// refuses only fields a caller has overwritten; it proves the offsets
|
|
1440
|
+
// below fit.
|
|
1441
|
+
if (quote < 0 || close < 2 || close > window || close <= quote) {
|
|
1442
|
+
return false
|
|
1443
|
+
}
|
|
1284
1444
|
const src: u8[] = this.src
|
|
1285
|
-
const end: i32 =
|
|
1286
|
-
let i: i32 =
|
|
1445
|
+
const end: i32 = close - 1
|
|
1446
|
+
let i: i32 = quote + 1
|
|
1287
1447
|
let o: i32 = 0
|
|
1288
1448
|
// Every escape decodes to fewer bytes than it is written with, so a string
|
|
1289
1449
|
// whose decoded length is its raw length has none.
|
|
1290
|
-
if (n ===
|
|
1291
|
-
|
|
1292
|
-
|
|
1450
|
+
if (n === end - i) {
|
|
1451
|
+
// `end - i` is `n`, so the loop stops at both ends together; each
|
|
1452
|
+
// bound proves its own index.
|
|
1453
|
+
while (i < end && o < n) {
|
|
1454
|
+
if (jsonTokByteAt(src, i, end) !== toI32(name.charCodeAt(o))) {
|
|
1293
1455
|
return false
|
|
1294
1456
|
}
|
|
1457
|
+
i += 1
|
|
1295
1458
|
o += 1
|
|
1296
1459
|
}
|
|
1297
1460
|
return true
|
|
@@ -1316,7 +1479,16 @@ export class JsonTokenizer {
|
|
|
1316
1479
|
o += 1
|
|
1317
1480
|
k += 1
|
|
1318
1481
|
}
|
|
1319
|
-
|
|
1482
|
+
// An escape takes 12 bytes for a surrogate pair, the only one past
|
|
1483
|
+
// U+FFFF, 6 for any other `\u` and 2 for a short one. Each is added
|
|
1484
|
+
// alone, so `i < end` proves the sum.
|
|
1485
|
+
if (cp >= JSON_TOK_FOUR_BYTE_FLOOR) {
|
|
1486
|
+
i += 12
|
|
1487
|
+
} else if (jsonTokByteAt(src, i + 1, end) === JSON_TOK_LOWER_U) {
|
|
1488
|
+
i += 6
|
|
1489
|
+
} else {
|
|
1490
|
+
i += 2
|
|
1491
|
+
}
|
|
1320
1492
|
}
|
|
1321
1493
|
return o === n
|
|
1322
1494
|
}
|
|
@@ -1342,50 +1514,52 @@ export class JsonTokenizer {
|
|
|
1342
1514
|
* @returns the integer, or `0` when `isInteger()` does not hold.
|
|
1343
1515
|
*/
|
|
1344
1516
|
numberI64(): i64 {
|
|
1345
|
-
|
|
1346
|
-
return toI64(0)
|
|
1347
|
-
}
|
|
1348
|
-
const src: u8[] = this.src
|
|
1349
|
-
const negated: i64 = this.integerValue
|
|
1350
|
-
return jsonTokByteAt(src, this.tokStart, this.tokEnd) === JSON_TOK_MINUS ? negated : -negated
|
|
1517
|
+
return this.isInteger() ? this.integerValue : toI64(0)
|
|
1351
1518
|
}
|
|
1352
1519
|
|
|
1353
1520
|
/**
|
|
1354
|
-
* Reads the integral number's
|
|
1355
|
-
*
|
|
1356
|
-
*
|
|
1357
|
-
*
|
|
1521
|
+
* Reads the integral number's value into `integerValue` and answers
|
|
1522
|
+
* whether it fits `i64`. The magnitude is summed in a `u64` against the
|
|
1523
|
+
* bound for its sign, 2^63 for a negative number and 2^63 - 1 for a
|
|
1524
|
+
* positive one, so it stays below 2^64 and never wraps.
|
|
1358
1525
|
*/
|
|
1359
1526
|
private integerScan(): boolean {
|
|
1360
|
-
if (this.numberDigits <= JSON_TOK_EXACT_DIGITS) {
|
|
1361
|
-
// Below 10^18, so `number()`'s mantissa is the magnitude and fits.
|
|
1362
|
-
this.integerValue = -toI64(this.numberMantissa)
|
|
1363
|
-
return true
|
|
1364
|
-
}
|
|
1365
1527
|
const src: u8[] = this.src
|
|
1528
|
+
const window: JsonTokEnd = this.end
|
|
1529
|
+
const start: i32 = this.tokStart
|
|
1366
1530
|
const end: i32 = this.tokEnd
|
|
1367
|
-
const negative: boolean = jsonTokByteAt(src, this.tokStart, end) === JSON_TOK_MINUS
|
|
1368
|
-
// Every `i64` here comes from `toI64`, which is a BigInt under Node, where a
|
|
1369
|
-
// bare literal would be a double and the two do not mix.
|
|
1370
|
-
// -2^63 has no literal (NL2055): -2^31 shifted up 32 bits.
|
|
1371
|
-
const min: i64 = toI64(-2147483648) << toI64(32)
|
|
1372
|
-
const ten: i64 = toI64(10)
|
|
1373
|
-
const bound: i64 = negative ? min : min + toI64(1)
|
|
1374
|
-
let acc: i64 = toI64(0)
|
|
1375
|
-
let i: i32 = negative ? this.tokStart + 1 : this.tokStart
|
|
1376
1531
|
const n: i32 = toI32(src.length)
|
|
1377
|
-
|
|
1532
|
+
// A number lies inside the window, so this refuses only fields a caller
|
|
1533
|
+
// has overwritten; it proves `start + 1` fits.
|
|
1534
|
+
if (start < 0 || start >= window || start >= n || end > window || end > n) {
|
|
1378
1535
|
return false
|
|
1379
1536
|
}
|
|
1537
|
+
const negative: boolean = toI32(src[start]) === JSON_TOK_MINUS
|
|
1538
|
+
if (this.numberDigits <= JSON_TOK_EXACT_DIGITS) {
|
|
1539
|
+
// Below 10^18, so `number()`'s mantissa is the magnitude and fits.
|
|
1540
|
+
this.integerValue = jsonTokSigned(this.numberMantissa, negative)
|
|
1541
|
+
return true
|
|
1542
|
+
}
|
|
1543
|
+
// Every `u64` here comes from `toU64`, which is a BigInt under Node, where
|
|
1544
|
+
// a bare literal would be a double and the two do not mix.
|
|
1545
|
+
const one: u64 = toU64(1)
|
|
1546
|
+
const ten: u64 = toU64(10)
|
|
1547
|
+
const top: u64 = one << toU64(63)
|
|
1548
|
+
const bound: u64 = negative ? top : top - one
|
|
1549
|
+
let acc: u64 = toU64(0)
|
|
1550
|
+
let i: i32 = start
|
|
1551
|
+
if (negative) {
|
|
1552
|
+
i += 1
|
|
1553
|
+
}
|
|
1380
1554
|
while (i < end && i < n) {
|
|
1381
|
-
const digit:
|
|
1382
|
-
if (acc
|
|
1555
|
+
const digit: u64 = toU64(toI32(src[i]) - JSON_TOK_DIGIT_0)
|
|
1556
|
+
if (acc > (bound - digit) / ten) {
|
|
1383
1557
|
return false
|
|
1384
1558
|
}
|
|
1385
|
-
acc = acc * ten
|
|
1559
|
+
acc = acc * ten + digit
|
|
1386
1560
|
i += 1
|
|
1387
1561
|
}
|
|
1388
|
-
this.integerValue = acc
|
|
1562
|
+
this.integerValue = jsonTokSigned(acc, negative)
|
|
1389
1563
|
return true
|
|
1390
1564
|
}
|
|
1391
1565
|
|
|
@@ -1422,11 +1596,17 @@ export class JsonTokenizer {
|
|
|
1422
1596
|
*/
|
|
1423
1597
|
private unsignedScan(): boolean {
|
|
1424
1598
|
const src: u8[] = this.src
|
|
1599
|
+
const window: JsonTokEnd = this.end
|
|
1425
1600
|
const end: i32 = this.tokEnd
|
|
1426
1601
|
const n: i32 = toI32(src.length)
|
|
1427
1602
|
let i: i32 = this.tokStart
|
|
1603
|
+
// A number lies inside the window, so the first test refuses only fields
|
|
1604
|
+
// a caller has overwritten; it bounds `end - i`.
|
|
1605
|
+
if (i < 0 || i >= n || end <= 0 || end <= i || end > window || end > n) {
|
|
1606
|
+
return false
|
|
1607
|
+
}
|
|
1428
1608
|
const digits: i32 = end - i
|
|
1429
|
-
if (
|
|
1609
|
+
if (digits < 1 || digits > 20) {
|
|
1430
1610
|
return false
|
|
1431
1611
|
}
|
|
1432
1612
|
if (toI32(src[i]) === JSON_TOK_MINUS) {
|
|
@@ -1481,57 +1661,57 @@ export class JsonTokenizer {
|
|
|
1481
1661
|
return false
|
|
1482
1662
|
}
|
|
1483
1663
|
const src: u8[] = this.src
|
|
1484
|
-
const
|
|
1664
|
+
const window: JsonTokEnd = this.end
|
|
1485
1665
|
const n: i32 = toI32(src.length)
|
|
1486
1666
|
let i: i32 = this.tokStart
|
|
1487
|
-
|
|
1667
|
+
// A number lies inside the window, so this refuses only fields a caller
|
|
1668
|
+
// has overwritten.
|
|
1669
|
+
const close: i32 = this.tokEnd
|
|
1670
|
+
if (i < 0 || i >= window || close <= 0 || close > window || close <= i) {
|
|
1488
1671
|
return false
|
|
1489
1672
|
}
|
|
1673
|
+
const end: JsonTokEnd = close
|
|
1490
1674
|
if (jsonTokByteAt(src, i, end) === JSON_TOK_MINUS) {
|
|
1491
1675
|
i += 1
|
|
1492
1676
|
}
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
let
|
|
1496
|
-
let
|
|
1677
|
+
// F - z is the distance from the dot to the last nonzero digit, less one
|
|
1678
|
+
// when that digit comes first; with no dot, the digits end where it would be.
|
|
1679
|
+
let dot: i32 = -1
|
|
1680
|
+
let last: i32 = -1
|
|
1497
1681
|
while (i < end && i < n) {
|
|
1498
1682
|
const b: i32 = toI32(src[i])
|
|
1499
1683
|
if (b === JSON_TOK_DOT) {
|
|
1500
|
-
|
|
1501
|
-
} else if (jsonTokIsDigit(b)) {
|
|
1502
|
-
if (b === JSON_TOK_DIGIT_0) {
|
|
1503
|
-
zeros += 1
|
|
1504
|
-
} else {
|
|
1505
|
-
zeros = 0
|
|
1506
|
-
allZero = false
|
|
1507
|
-
}
|
|
1508
|
-
if (afterDot) {
|
|
1509
|
-
fraction += 1
|
|
1510
|
-
}
|
|
1511
|
-
} else {
|
|
1684
|
+
dot = i
|
|
1685
|
+
} else if (!jsonTokIsDigit(b)) {
|
|
1512
1686
|
break
|
|
1687
|
+
} else if (b !== JSON_TOK_DIGIT_0) {
|
|
1688
|
+
last = i
|
|
1513
1689
|
}
|
|
1514
1690
|
i += 1
|
|
1515
1691
|
}
|
|
1516
|
-
if (
|
|
1692
|
+
if (last < 0) {
|
|
1517
1693
|
return true
|
|
1518
1694
|
}
|
|
1519
|
-
|
|
1520
|
-
|
|
1695
|
+
if (dot < 0) {
|
|
1696
|
+
dot = i
|
|
1697
|
+
}
|
|
1521
1698
|
if (i >= end) {
|
|
1522
|
-
return
|
|
1699
|
+
return last < dot
|
|
1523
1700
|
}
|
|
1524
1701
|
// `i` is at the `e` or `E`.
|
|
1525
1702
|
i += 1
|
|
1526
1703
|
const sign: i32 = jsonTokByteAt(src, i, end)
|
|
1527
1704
|
const minus: boolean = sign === JSON_TOK_MINUS
|
|
1528
|
-
if (minus || sign === JSON_TOK_PLUS) {
|
|
1705
|
+
if (i < end && (minus || sign === JSON_TOK_PLUS)) {
|
|
1529
1706
|
i += 1
|
|
1530
1707
|
}
|
|
1531
|
-
|
|
1532
|
-
|
|
1708
|
+
// `dot` and `last` are positions in the token, so each difference fits.
|
|
1709
|
+
if (last > dot) {
|
|
1710
|
+
// A nonzero fraction digit `last - dot` places down: E >= last - dot.
|
|
1711
|
+
return !minus && jsonTokDigitsExceed(src, i, end, toI32(toI64(last) - toI64(dot) - toI64(1)))
|
|
1533
1712
|
}
|
|
1534
|
-
|
|
1713
|
+
// Whole already, with `dot - last - 1` zeros a negative exponent may take.
|
|
1714
|
+
return !minus || !jsonTokDigitsExceed(src, i, end, toI32(toI64(dot) - toI64(last) - toI64(1)))
|
|
1535
1715
|
}
|
|
1536
1716
|
|
|
1537
1717
|
/**
|
|
@@ -1565,7 +1745,8 @@ export class JsonTokenizer {
|
|
|
1565
1745
|
if (scale === 0) {
|
|
1566
1746
|
value = toF64(mantissa)
|
|
1567
1747
|
} else {
|
|
1568
|
-
|
|
1748
|
+
// Within the fast exponent's span, so the magnitude fits.
|
|
1749
|
+
const power: f64 = jsonTokPowerOfTen(scale < 0 ? toI32(toI64(0) - toI64(scale)) : scale)
|
|
1569
1750
|
value = scale < 0 ? toF64(mantissa) / power : toF64(mantissa) * power
|
|
1570
1751
|
}
|
|
1571
1752
|
} else {
|
|
@@ -1587,8 +1768,14 @@ export class JsonTokenizer {
|
|
|
1587
1768
|
return this.numberSlow()
|
|
1588
1769
|
}
|
|
1589
1770
|
const src: u8[] = this.src
|
|
1771
|
+
const window: JsonTokEnd = this.end
|
|
1590
1772
|
const start: i32 = this.tokStart
|
|
1591
1773
|
const end: i32 = this.tokEnd
|
|
1774
|
+
// A number lies inside the window, so this refuses only fields a caller
|
|
1775
|
+
// has overwritten, as the length test below does.
|
|
1776
|
+
if (start < 0 || start >= window) {
|
|
1777
|
+
return this.numberSlow()
|
|
1778
|
+
}
|
|
1592
1779
|
const negative: boolean = jsonTokByteAt(src, start, end) === JSON_TOK_MINUS
|
|
1593
1780
|
// At most 19 digits are accumulated, so the mantissa never wraps. Every
|
|
1594
1781
|
// `u64` constant is spelled through `toU64`, because under Node a `u64` is
|
|
@@ -1596,8 +1783,11 @@ export class JsonTokenizer {
|
|
|
1596
1783
|
const ten: u64 = toU64(10)
|
|
1597
1784
|
let mantissa: u64 = toU64(0)
|
|
1598
1785
|
let digits: i32 = 0
|
|
1599
|
-
let i: i32 =
|
|
1600
|
-
if (
|
|
1786
|
+
let i: i32 = start
|
|
1787
|
+
if (negative) {
|
|
1788
|
+
i += 1
|
|
1789
|
+
}
|
|
1790
|
+
if (end > toI32(src.length)) {
|
|
1601
1791
|
return this.numberSlow()
|
|
1602
1792
|
}
|
|
1603
1793
|
// The length is read in each test rather than hoisted: the call to
|
|
@@ -1606,7 +1796,8 @@ export class JsonTokenizer {
|
|
|
1606
1796
|
const b: i32 = toI32(src[i])
|
|
1607
1797
|
if (jsonTokIsDigit(b)) {
|
|
1608
1798
|
if (digits > 0 || b !== JSON_TOK_DIGIT_0) {
|
|
1609
|
-
digits
|
|
1799
|
+
// `digits` is at most 19 here, so the mask changes nothing.
|
|
1800
|
+
digits = (digits & 31) + 1
|
|
1610
1801
|
if (digits > JSON_TOK_FAST_DIGITS) {
|
|
1611
1802
|
return this.numberSlow()
|
|
1612
1803
|
}
|
|
@@ -1624,7 +1815,8 @@ export class JsonTokenizer {
|
|
|
1624
1815
|
if (exact && scale >= -JSON_TOK_FAST_EXPONENT && scale <= JSON_TOK_FAST_EXPONENT) {
|
|
1625
1816
|
// Clinger: both operands are exact doubles, so one rounding. The
|
|
1626
1817
|
// same decision as `numberF64`'s; change the two together.
|
|
1627
|
-
|
|
1818
|
+
// Within the fast exponent's span, so the magnitude fits.
|
|
1819
|
+
const power: f64 = jsonTokPowerOfTen(scale < 0 ? toI32(toI64(0) - toI64(scale)) : scale)
|
|
1628
1820
|
value = scale < 0 ? toF64(mantissa) / power : toF64(mantissa) * power
|
|
1629
1821
|
} else {
|
|
1630
1822
|
value = jsonTokEiselLemire(mantissa, scale)
|
|
@@ -1649,7 +1841,8 @@ export class JsonTokenizer {
|
|
|
1649
1841
|
const src: u8[] = this.src
|
|
1650
1842
|
const start: i32 = this.tokStart
|
|
1651
1843
|
const end: i32 = this.tokEnd
|
|
1652
|
-
|
|
1844
|
+
const n: i32 = toI32(src.length)
|
|
1845
|
+
if (start < 0 || start > end || end > n) {
|
|
1653
1846
|
return 0
|
|
1654
1847
|
}
|
|
1655
1848
|
return parseFloat(String.fromBytes(src, start, end))
|
|
@@ -1668,6 +1861,13 @@ export class JsonTokenizer {
|
|
|
1668
1861
|
* @internal Called by `JsonDecoder.enterUnion`, std/json-typed.ts.
|
|
1669
1862
|
*/
|
|
1670
1863
|
rewindObject(start: i32): void {
|
|
1864
|
+
const window: JsonTokEnd = this.end
|
|
1865
|
+
// The caller passes a `{` the tokenizer read, inside the window; any
|
|
1866
|
+
// other offset names no object, and fails as a syntax error there.
|
|
1867
|
+
if (start < 0 || start >= window) {
|
|
1868
|
+
this.fail(JSON_TOK_ERR_SYNTAX, start)
|
|
1869
|
+
return
|
|
1870
|
+
}
|
|
1671
1871
|
this.kind = JSON_TOK_OBJECT_BEGIN
|
|
1672
1872
|
this.tokStart = start
|
|
1673
1873
|
this.tokEnd = start + 1
|
|
@@ -1699,8 +1899,9 @@ export class JsonTokenizer {
|
|
|
1699
1899
|
if (k !== JSON_TOK_OBJECT_BEGIN && k !== JSON_TOK_ARRAY_BEGIN) {
|
|
1700
1900
|
return k
|
|
1701
1901
|
}
|
|
1702
|
-
|
|
1703
|
-
|
|
1902
|
+
// Inside the opener's depth until its closer steps back out of it.
|
|
1903
|
+
const depth: i32 = this.depth
|
|
1904
|
+
while (this.depth >= depth && k >= 0) {
|
|
1704
1905
|
k = this.next()
|
|
1705
1906
|
}
|
|
1706
1907
|
return k
|
|
@@ -1807,25 +2008,6 @@ const jsonTokParseEight = (word: u64): u64 => {
|
|
|
1807
2008
|
return ((low + high) >> toU64(32)) & jsonTokW64(0, 0xffffffff)
|
|
1808
2009
|
}
|
|
1809
2010
|
|
|
1810
|
-
/**
|
|
1811
|
-
* The high 64 bits of the 128-bit product `a × b`. Each limb product is at
|
|
1812
|
-
* most (2^32 - 1)^2, so `cross` is at most 2^64 - 1 and the answer at most
|
|
1813
|
-
* 2^64 - 2: no sum here can overflow, natively or as a `bigint`.
|
|
1814
|
-
*/
|
|
1815
|
-
const jsonTokMulHigh = (a: u64, b: u64): u64 => {
|
|
1816
|
-
const k32: u64 = toU64(32)
|
|
1817
|
-
const mask: u64 = jsonTokW64(0, 0xffffffff)
|
|
1818
|
-
const aHi: u64 = a >> k32
|
|
1819
|
-
const aLo: u64 = a & mask
|
|
1820
|
-
const bHi: u64 = b >> k32
|
|
1821
|
-
const bLo: u64 = b & mask
|
|
1822
|
-
const ll: u64 = aLo * bLo
|
|
1823
|
-
const hl: u64 = aHi * bLo
|
|
1824
|
-
const lh: u64 = aLo * bHi
|
|
1825
|
-
const cross: u64 = (ll >> k32) + (hl & mask) + lh
|
|
1826
|
-
return (hl >> k32) + (cross >> k32) + aHi * bHi
|
|
1827
|
-
}
|
|
1828
|
-
|
|
1829
2011
|
/** The leading zero bits of a `u64`, 64 for zero. */
|
|
1830
2012
|
const jsonTokLeadingZeros = (w: u64): i32 => {
|
|
1831
2013
|
const hi: i32 = toI32(w >> toU64(32))
|
|
@@ -1834,9 +2016,10 @@ const jsonTokLeadingZeros = (w: u64): i32 => {
|
|
|
1834
2016
|
|
|
1835
2017
|
/**
|
|
1836
2018
|
* `w × 10^q` as the nearest double, ties to even, for a `w` of 1 to 19
|
|
1837
|
-
* decimal digits, so from 1 to 10^19 - 1, and any `q`: a `q` below
|
|
1838
|
-
*
|
|
1839
|
-
*
|
|
2019
|
+
* decimal digits, so from 1 to 10^19 - 1, and any `q`: a `q` below -342
|
|
2020
|
+
* answers 0, one above 308 infinity, since `jsonTokPowerOfFive` has entries
|
|
2021
|
+
* for those exponents alone and past them such a number is zero or infinite.
|
|
2022
|
+
* Positive; the caller applies the sign.
|
|
1840
2023
|
*
|
|
1841
2024
|
* Mushtak and Lemire prove ("Fast Number Parsing Without Fallback", 2023)
|
|
1842
2025
|
* that for such a `w` the product with the first 64 bits of the power of five,
|
|
@@ -1848,28 +2031,31 @@ const jsonTokLeadingZeros = (w: u64): i32 => {
|
|
|
1848
2031
|
const jsonTokEiselLemire = (w0: u64, q: i32): f64 => {
|
|
1849
2032
|
const one: u64 = toU64(1)
|
|
1850
2033
|
const infinity: u64 = toU64(0x7ff) << toU64(52)
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
}
|
|
1854
|
-
if (q > JSON_TOK_LARGEST_POWER) {
|
|
2034
|
+
// The bounds are written out, not named, so that they bound `q` below.
|
|
2035
|
+
if (q > 308) {
|
|
1855
2036
|
return bitsToF64(toI64(infinity))
|
|
1856
2037
|
}
|
|
2038
|
+
// `q` counted from -342, the table's first row: a `q` below it is 0.
|
|
2039
|
+
const from: i32 = q + 342
|
|
2040
|
+
if (from < 0) {
|
|
2041
|
+
return 0
|
|
2042
|
+
}
|
|
1857
2043
|
// Normalize `w` so its top bit is set.
|
|
1858
2044
|
const lz: i32 = jsonTokLeadingZeros(w0)
|
|
1859
2045
|
const w: u64 = w0 << toU64(lz)
|
|
1860
2046
|
// compute_product_approximation: w × 5^q to the 55 bits a double needs
|
|
1861
2047
|
// (52 explicit, the implicit one, a rounding bit and the one `upperbit` may
|
|
1862
2048
|
// cost), with the next word of the power only when those bits are all ones.
|
|
1863
|
-
const index: i32 =
|
|
2049
|
+
const index: i32 = from * 2
|
|
1864
2050
|
const five: u64 = jsonTokPowerOfFive(index)
|
|
1865
|
-
let high: u64 =
|
|
2051
|
+
let high: u64 = mulHigh(w, five)
|
|
1866
2052
|
// The low half: natively a `u64` product wraps to it, and as a `bigint` the
|
|
1867
2053
|
// exact product is masked to it.
|
|
1868
2054
|
const allOnes: u64 = jsonTokW64(0xffffffff, 0xffffffff)
|
|
1869
2055
|
let low: u64 = (w * five) & allOnes
|
|
1870
2056
|
const precisionMask: u64 = toU64(0x1ff)
|
|
1871
2057
|
if ((high & precisionMask) === precisionMask) {
|
|
1872
|
-
const second: u64 =
|
|
2058
|
+
const second: u64 = mulHigh(w, jsonTokPowerOfFive(index + 1))
|
|
1873
2059
|
// low += second, carrying into high: `high` is at most 2^64 - 2, so the
|
|
1874
2060
|
// carry cannot overflow it.
|
|
1875
2061
|
const room: u64 = allOnes - low
|
|
@@ -1880,19 +2066,23 @@ const jsonTokEiselLemire = (w0: u64, q: i32): f64 => {
|
|
|
1880
2066
|
low = low + second
|
|
1881
2067
|
}
|
|
1882
2068
|
}
|
|
1883
|
-
const upperbit: i32 = toI32(high >> toU64(63))
|
|
2069
|
+
const upperbit: i32 = toI32(high >> toU64(63)) & 1
|
|
1884
2070
|
const shift: i32 = upperbit + 9
|
|
1885
2071
|
let mantissa: u64 = high >> toU64(shift)
|
|
1886
2072
|
// detail::power: floor(q × log2(5)) + q + 63, with upstream's bias so the
|
|
1887
|
-
// shift is of a non-negative value.
|
|
1888
|
-
//
|
|
1889
|
-
// since a sum of literals is an `f64` under
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
2073
|
+
// shift is of a non-negative value. The literals are upstream's
|
|
2074
|
+
// `152170 + 65536` and `2048 * 65536 - 342 * 217706`, the last term because
|
|
2075
|
+
// `from` is `q + 342`; folded, since a sum of literals is an `f64` under
|
|
2076
|
+
// `--number-mode f64`. `from` is at most 650, so the product fits.
|
|
2077
|
+
// From 911 to 3071, and `lz` at most 64: the masks change neither, and
|
|
2078
|
+
// bound the sum.
|
|
2079
|
+
const power: i32 = ((from * 217706 + 59762276) >> 16) & 4095
|
|
2080
|
+
const biased: i32 = power - 2048 + 63 + upperbit - (lz & 127) + 1023
|
|
2081
|
+
if (biased <= 0) {
|
|
1893
2082
|
// A subnormal, or zero. The rounded mantissa is at most 2^52, and when it
|
|
1894
2083
|
// reaches 2^52 those are already the bits of the smallest normal double.
|
|
1895
|
-
|
|
2084
|
+
// `biased` is at least -1089, so the shift fits.
|
|
2085
|
+
const subnormalShift: i32 = toI32(toI64(1) - toI64(biased))
|
|
1896
2086
|
if (subnormalShift >= 64) {
|
|
1897
2087
|
return 0
|
|
1898
2088
|
}
|
|
@@ -1909,6 +2099,7 @@ const jsonTokEiselLemire = (w0: u64, q: i32): f64 => {
|
|
|
1909
2099
|
}
|
|
1910
2100
|
mantissa = (mantissa + (mantissa & one)) >> one
|
|
1911
2101
|
const hidden: u64 = one << toU64(52)
|
|
2102
|
+
let power2: i32 = biased
|
|
1912
2103
|
if (mantissa >= hidden << one) {
|
|
1913
2104
|
mantissa = hidden
|
|
1914
2105
|
power2 += 1
|