@amritk/nish-aarch64-linux 0.36.0 → 0.38.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 +92 -71
- package/bin/nish +0 -0
- package/docs/AI.md +75 -14
- package/package.json +1 -1
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/nish.d.ts +1045 -512
- package/runtime/nish.h +53 -6
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +740 -0
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-region.c +85 -7
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +65 -9
- package/runtime/runtime.c +50 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +231 -31
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +109 -82
- package/std/array.ts +266 -0
- package/std/bytes.ts +23 -19
- package/std/compress/checksum.ts +1 -1
- package/std/crypto/aes.ts +48 -43
- package/std/crypto/base64url.ts +7 -5
- package/std/crypto/bcrypt-batch.ts +13 -13
- package/std/crypto/bcrypt.ts +33 -27
- package/std/crypto/chacha20poly1305.ts +16 -14
- package/std/crypto/ed25519.ts +19 -16
- package/std/crypto/hmac.ts +7 -6
- package/std/crypto/jwt.ts +23 -20
- package/std/crypto/p256.ts +63 -56
- package/std/crypto/p384.ts +23 -20
- package/std/crypto/pki.ts +18 -15
- package/std/crypto/rsa.ts +23 -19
- package/std/crypto/x25519.ts +11 -10
- package/std/crypto/x509.ts +69 -62
- package/std/host/ring.ts +124 -0
- package/std/host/text.ts +86 -0
- package/std/json-equal.ts +12 -11
- package/std/json-number.ts +12 -12
- package/std/json-tokenizer.ts +26 -22
- package/std/json-typed.ts +92 -78
- package/std/json-value.ts +38 -33
- package/std/json.ts +32 -28
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +1744 -295
- package/std/net/handler.ts +30 -0
- package/std/net/hpack.ts +29 -26
- package/std/net/http-compress.ts +42 -28
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-client-codec.ts +29 -25
- package/std/net/http1-codec.ts +62 -91
- package/std/net/http1-server.ts +79 -57
- package/std/net/http2-frame.ts +17 -16
- package/std/net/http2-tls.ts +17 -15
- package/std/net/http2.ts +67 -54
- package/std/net/http3-frame.ts +11 -10
- package/std/net/http3-server.ts +36 -35
- package/std/net/http3.ts +70 -58
- package/std/net/postgres-async.ts +1005 -0
- package/std/net/postgres.ts +185 -164
- package/std/net/prefork.ts +38 -34
- package/std/net/qpack.ts +15 -14
- package/std/net/quic-conn-ack.ts +1 -0
- package/std/net/quic-conn-cid.ts +11 -0
- package/std/net/quic-conn-params.ts +41 -14
- package/std/net/quic-datagram.ts +7 -0
- package/std/net/quic-frame.ts +48 -12
- package/std/net/quic-listener.ts +61 -44
- package/std/net/quic-packet.ts +71 -52
- package/std/net/quic-recovery.ts +59 -21
- package/std/net/quic-stream.ts +83 -55
- package/std/net/quic.ts +204 -152
- package/std/net/serve.ts +763 -0
- package/std/net/tls/client.ts +1 -0
- package/std/net/tls/codec.ts +81 -12
- package/std/net/tls/record-client.ts +30 -18
- package/std/net/tls/record-server.ts +74 -46
- package/std/net/tls/record.ts +33 -24
- package/std/net/tls/ticket.ts +2 -0
- package/std/net/tls/verify.ts +8 -7
- package/std/net/tls-tcp.ts +58 -50
- package/std/net/tls.ts +55 -43
- package/std/net/url.ts +12 -9
- package/std/net/websocket.ts +23 -19
- package/std/net/webtransport.ts +24 -18
- package/std/random.ts +8 -6
- package/std/regex.ts +70 -59
- package/std/sync/ring.ts +24 -21
- package/std/sync/shm.ts +9 -7
- package/std/term-io.ts +17 -14
- package/std/term-probe.ts +11 -10
- package/std/term-rate.ts +71 -61
- package/std/term.ts +73 -59
- package/std/text.ts +242 -15
- package/std/threads.ts +19 -15
- package/std/yaml-json.ts +17 -15
- package/std/yaml-write.ts +21 -14
package/std/host/ring.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/host/ring` — a single-producer, single-consumer ring of `i32` words
|
|
3
|
+
* over module storage, shared by a wasm module and the page that drives it
|
|
4
|
+
* (`docs/wp50-wasm-host.md` §11).
|
|
5
|
+
*
|
|
6
|
+
* The ring is an `Int32Array` the program declares as module storage, so the
|
|
7
|
+
* page reaches the same words through the `Layout` the loader hands it:
|
|
8
|
+
*
|
|
9
|
+
* import { ringShift, ringSize } from "nish/host/ring";
|
|
10
|
+
*
|
|
11
|
+
* const INPUT = new Int32Array(2 + 64); // head, tail, then 64 slots
|
|
12
|
+
*
|
|
13
|
+
* export function tick(): void {
|
|
14
|
+
* while (ringSize(INPUT) > 0) {
|
|
15
|
+
* steer(ringShift(INPUT));
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* Word 0 is the **head** and word 1 the **tail**, each a count of words taken
|
|
20
|
+
* and put kept modulo twice the capacity (the length less two), so neither
|
|
21
|
+
* overflows and `tail - head` (modulo the same) is the number waiting: 0 is
|
|
22
|
+
* empty and the capacity is full. Slot `count % capacity` holds the word put
|
|
23
|
+
* at that count. One side only advances the tail and the other only the head:
|
|
24
|
+
* that is the whole protocol, and it needs no atomics because the page and the
|
|
25
|
+
* module run on one thread and never at once. `nish/page`'s `ringOver` is the
|
|
26
|
+
* page's half, over the same layout and arithmetic.
|
|
27
|
+
*
|
|
28
|
+
* A zero-filled ring (what module storage starts as) is empty. A ring whose
|
|
29
|
+
* counts a buggy page wrote out of protocol is still never read out of
|
|
30
|
+
* bounds: each count is reduced modulo twice the capacity before it is used,
|
|
31
|
+
* and a size past the capacity reads as full, so `ringPush` refuses and
|
|
32
|
+
* `ringShift` takes words until the counts agree again.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/** Head, tail: the two words in front of the slots. */
|
|
36
|
+
const RING_HEADER: i32 = 2
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The number of slots, the ring's length less its header.
|
|
40
|
+
*
|
|
41
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
42
|
+
* @returns the slot count, or 0 for a ring too short to have one
|
|
43
|
+
*/
|
|
44
|
+
export const ringCapacity = (ring: Int32Array): i32 => {
|
|
45
|
+
const length: i32 = toI32(ring.length)
|
|
46
|
+
return length > RING_HEADER ? length - RING_HEADER : 0
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** `count` brought into `[0, 2 * capacity)`, whatever a page wrote there. */
|
|
50
|
+
const ringCount = (count: i32, capacity: i32): i32 => {
|
|
51
|
+
const m: i32 = capacity * 2
|
|
52
|
+
const r: i32 = count % m
|
|
53
|
+
return r < 0 ? r + m : r
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The number of words put and not yet taken.
|
|
58
|
+
*
|
|
59
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
60
|
+
* @returns the words waiting, at most the capacity
|
|
61
|
+
*/
|
|
62
|
+
export const ringSize = (ring: Int32Array): i32 => {
|
|
63
|
+
const capacity: i32 = ringCapacity(ring)
|
|
64
|
+
if (capacity === 0) {
|
|
65
|
+
return 0
|
|
66
|
+
}
|
|
67
|
+
const size: i32 = ringCount(ringCount(ring[1], capacity) - ringCount(ring[0], capacity), capacity)
|
|
68
|
+
return size > capacity ? capacity : size
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The number of words `ringPush` would still take.
|
|
73
|
+
*
|
|
74
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
75
|
+
* @returns the free slots
|
|
76
|
+
*/
|
|
77
|
+
export const ringFree = (ring: Int32Array): i32 => ringCapacity(ring) - ringSize(ring)
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Puts `word` at the tail, or changes nothing when the ring is full.
|
|
81
|
+
*
|
|
82
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
83
|
+
* @param word - the word to put
|
|
84
|
+
* @returns true when the word was put, false when the ring was full
|
|
85
|
+
*/
|
|
86
|
+
export const ringPush = (ring: Int32Array, word: i32): boolean => {
|
|
87
|
+
const capacity: i32 = ringCapacity(ring)
|
|
88
|
+
if (capacity === 0 || ringSize(ring) >= capacity) {
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
const tail: i32 = ringCount(ring[1], capacity)
|
|
92
|
+
ring[RING_HEADER + (tail % capacity)] = word
|
|
93
|
+
ring[1] = ringCount(tail + 1, capacity)
|
|
94
|
+
return true
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Takes the word at the head. On an empty ring it answers 0 and changes
|
|
99
|
+
* nothing, so a caller that cannot tell 0 from empty asks `ringSize` first.
|
|
100
|
+
*
|
|
101
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
102
|
+
* @returns the word taken, or 0 on an empty ring
|
|
103
|
+
*/
|
|
104
|
+
export const ringShift = (ring: Int32Array): i32 => {
|
|
105
|
+
const capacity: i32 = ringCapacity(ring)
|
|
106
|
+
if (capacity === 0 || ringSize(ring) === 0) {
|
|
107
|
+
return 0
|
|
108
|
+
}
|
|
109
|
+
const head: i32 = ringCount(ring[0], capacity)
|
|
110
|
+
const word: i32 = ring[RING_HEADER + (head % capacity)]
|
|
111
|
+
ring[0] = ringCount(head + 1, capacity)
|
|
112
|
+
return word
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Empties the ring: the head catches up with the tail, and the slots keep their stale words.
|
|
117
|
+
*
|
|
118
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
119
|
+
*/
|
|
120
|
+
export const ringClear = (ring: Int32Array): void => {
|
|
121
|
+
if (toI32(ring.length) >= RING_HEADER) {
|
|
122
|
+
ring[0] = ring[1]
|
|
123
|
+
}
|
|
124
|
+
}
|
package/std/host/text.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/host/text` — text in module storage, for a wasm module and the page
|
|
3
|
+
* that drives it (`docs/wp50-wasm-host.md` §7).
|
|
4
|
+
*
|
|
5
|
+
* A string crosses out of a module as an export's result or a host import's
|
|
6
|
+
* argument, but an export cannot take one: the page writes text into a `u8[]`
|
|
7
|
+
* the module declares as module storage and passes the byte count. These are
|
|
8
|
+
* the module's halves of both directions:
|
|
9
|
+
*
|
|
10
|
+
* import { textRead, textWrite, textWriteInt } from "nish/host/text";
|
|
11
|
+
*
|
|
12
|
+
* export const INBOX = new Array<u8>(256);
|
|
13
|
+
* export const OUTBOX = new Array<u8>(256);
|
|
14
|
+
*
|
|
15
|
+
* // The page: n = new TextEncoder().encodeInto(text, views.INBOX).written
|
|
16
|
+
* export function echo(n: i32): i32 {
|
|
17
|
+
* const text = textRead(INBOX, n);
|
|
18
|
+
* if (text === null) return -1;
|
|
19
|
+
* return textWriteInt(OUTBOX, textWrite(OUTBOX, 0, text), toI64(n));
|
|
20
|
+
* }
|
|
21
|
+
* // The page: new TextDecoder().decode(views.OUTBOX.subarray(0, count))
|
|
22
|
+
*
|
|
23
|
+
* A writer takes the offset to write at and returns the offset after what it
|
|
24
|
+
* wrote, so writes chain. Text that does not fit writes nothing and returns
|
|
25
|
+
* -1, and every writer passes a -1 offset straight through, so a chain that
|
|
26
|
+
* overflowed anywhere ends in -1 with the bytes before the failed write left
|
|
27
|
+
* as they were. Nothing here traps on what a page wrote.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Writes `s` as its UTF-8 bytes at offset `at`, or nothing when it does not fit.
|
|
32
|
+
*
|
|
33
|
+
* @param buf - the text's storage
|
|
34
|
+
* @param at - the offset to write at; a negative one is an earlier failure
|
|
35
|
+
* @param s - the text
|
|
36
|
+
* @returns the offset after the text, or -1 when `at` is negative or past the
|
|
37
|
+
* buffer, or the text does not fit
|
|
38
|
+
*/
|
|
39
|
+
export const textWrite = (buf: u8[], at: i32, s: string): i32 => {
|
|
40
|
+
const length: i32 = toI32(buf.length)
|
|
41
|
+
const n: i32 = toI32(s.length)
|
|
42
|
+
if (at < 0 || at > length || n > length - at) {
|
|
43
|
+
return -1
|
|
44
|
+
}
|
|
45
|
+
for (let i: i32 = 0; i < n; i++) {
|
|
46
|
+
buf[at + i] = toU8(s.charCodeAt(i))
|
|
47
|
+
}
|
|
48
|
+
return at + n
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Writes `n` in decimal, as a template literal formats it.
|
|
53
|
+
*
|
|
54
|
+
* @param buf - the text's storage
|
|
55
|
+
* @param at - the offset to write at; a negative one is an earlier failure
|
|
56
|
+
* @param n - the integer
|
|
57
|
+
* @returns the offset after the digits, or -1 as `textWrite` answers it
|
|
58
|
+
*/
|
|
59
|
+
export const textWriteInt = (buf: u8[], at: i32, n: i64): i32 => textWrite(buf, at, `${n}`)
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Writes `x` in its shortest round-tripping form, as a template literal
|
|
63
|
+
* formats it: `0.30000000000000004`, `1e+21`, `NaN`, `-Infinity`.
|
|
64
|
+
*
|
|
65
|
+
* @param buf - the text's storage
|
|
66
|
+
* @param at - the offset to write at; a negative one is an earlier failure
|
|
67
|
+
* @param x - the number
|
|
68
|
+
* @returns the offset after the text, or -1 as `textWrite` answers it
|
|
69
|
+
*/
|
|
70
|
+
export const textWriteF64 = (buf: u8[], at: i32, x: f64): i32 => textWrite(buf, at, `${x}`)
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The first `n` bytes of `buf` as a string: what a page's `encodeInto` wrote.
|
|
74
|
+
* The bytes are not checked as UTF-8; a sequence that is not becomes U+FFFD
|
|
75
|
+
* when the string next crosses to the page.
|
|
76
|
+
*
|
|
77
|
+
* @param buf - the text's storage
|
|
78
|
+
* @param n - the byte count the page passed
|
|
79
|
+
* @returns the string, or null when `n` is negative or past the buffer
|
|
80
|
+
*/
|
|
81
|
+
export const textRead = (buf: readonly u8[], n: i32): string | null => {
|
|
82
|
+
if (n < 0 || n > toI32(buf.length)) {
|
|
83
|
+
return null
|
|
84
|
+
}
|
|
85
|
+
return String.fromBytes(buf, 0, n)
|
|
86
|
+
}
|
package/std/json-equal.ts
CHANGED
|
@@ -25,17 +25,18 @@
|
|
|
25
25
|
* built with `push` and `set` deeper than any parser allows compares as well
|
|
26
26
|
* as a flat one, and nothing recurses.
|
|
27
27
|
*
|
|
28
|
-
* **What a call costs.** `jsonEqual` stops at the first difference. Up to
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* the first repeat and keeps
|
|
28
|
+
* **What a call costs.** `jsonEqual` stops at the first difference. Up to there
|
|
29
|
+
* it is linear in the values' size, except that each pair of objects reads
|
|
30
|
+
* every key (one string each) and sorts them, `n log n` for `n` members; it
|
|
31
|
+
* also allocates one string per string value, its stack, and whatever
|
|
32
|
+
* `jsonNumberCompare` allocates per number pair. `jsonEqualCanonical` builds
|
|
33
|
+
* the canonical text, about the size of the value written as JSON, and sorts
|
|
34
|
+
* keys the same way. `jsonEqualHash` is FNV-1a of that text's units: its bytes
|
|
35
|
+
* natively, its code units under Node.
|
|
36
|
+
*
|
|
37
|
+
* Every order and length below is in those units too. `jsonEqualFirstDuplicate`
|
|
38
|
+
* makes the canonical text of every element up to the first repeat and keeps
|
|
39
|
+
* them in a `Map`.
|
|
39
40
|
*
|
|
40
41
|
* **The hash is not keyed.** FNV-1a (public domain) is the same in every
|
|
41
42
|
* process, so an attacker can make many values that share a hash, and a
|
package/std/json-number.ts
CHANGED
|
@@ -19,18 +19,18 @@
|
|
|
19
19
|
* grammar exactly (what `JsonDocument.numberText` answers); anything else is
|
|
20
20
|
* `JSON_NUMBER_ERR_SYNTAX`.
|
|
21
21
|
*
|
|
22
|
-
* **What a call costs.** `jsonNumberCompare` reads each text once and
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* byte operations.
|
|
22
|
+
* **What a call costs.** `jsonNumberCompare` reads each text once and allocates
|
|
23
|
+
* two records of a few fields, nothing in proportion to the text: time is
|
|
24
|
+
* linear in the texts' lengths. `jsonNumberCanonical` allocates the string it
|
|
25
|
+
* answers, at most the text's length plus an exponent.
|
|
26
|
+
*
|
|
27
|
+
* `jsonNumberMultipleOf` divides the value's digits, followed by as many zeros
|
|
28
|
+
* as its exponent is above the divisor's (the gap), by the divisor's digits:
|
|
29
|
+
* one step per digit of that dividend when the divisor has at most 18
|
|
30
|
+
* significant digits, and one per digit of the dividend per digit of the
|
|
31
|
+
* divisor otherwise, which then also allocates the remainder, one byte per
|
|
32
|
+
* divisor digit. A step of that long division is a shift, up to ten comparisons
|
|
33
|
+
* and up to nine subtractions of a byte each, so about twenty byte operations.
|
|
34
34
|
*
|
|
35
35
|
* **The bounds are stated.** An exponent is at most `JSON_NUMBER_MAX_EXPONENT`
|
|
36
36
|
* in size, which keeps every exponent sum far inside `i64`, and past
|
package/std/json-tokenizer.ts
CHANGED
|
@@ -42,31 +42,35 @@
|
|
|
42
42
|
* any well-formed UTF-8 (RFC 3629: no overlong form, no surrogate code point,
|
|
43
43
|
* nothing past U+10FFFF, no sequence cut short), no raw byte below 0x20, and
|
|
44
44
|
* the escapes of RFC 8259 §7. A `\u` escape of a high surrogate must be
|
|
45
|
-
* followed at once by one of a low surrogate, and a low surrogate must follow
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* string
|
|
45
|
+
* followed at once by one of a low surrogate, and a low surrogate must follow a
|
|
46
|
+
* high one: a lone surrogate escape is an error.
|
|
47
|
+
*
|
|
48
|
+
* RFC 8259 allows one, but it names no Unicode character, decodes to no valid
|
|
49
|
+
* UTF-8, and is what a peer smuggles past a check done on the decoded bytes, so
|
|
50
|
+
* this module takes the stricter reading (`docs/security/json.md`, "Lone
|
|
51
|
+
* surrogates"). Nothing is decoded until the caller asks: `stringInto` writes
|
|
52
|
+
* the decoded bytes of the current key or string into the caller's array, and
|
|
53
|
+
* `keyEquals` compares them with a string without writing them anywhere.
|
|
53
54
|
*
|
|
54
55
|
* **Numbers are checked against the grammar and parsed on request.**
|
|
55
56
|
* `numberF64` is correctly rounded, ties to even, in one of three ways. A
|
|
56
|
-
* number of at most 19 significant digits is read into a `u64`. When that is
|
|
57
|
-
*
|
|
58
|
-
* multiply or divide of two exact doubles (Clinger's fast path).
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* `
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
57
|
+
* number of at most 19 significant digits is read into a `u64`. When that is at
|
|
58
|
+
* most 2^53 and the decimal exponent is within ±22, the answer is one exact
|
|
59
|
+
* multiply or divide of two exact doubles (Clinger's fast path).
|
|
60
|
+
*
|
|
61
|
+
* Any other number of at most 19 digits is the Eisel–Lemire algorithm as
|
|
62
|
+
* fast_float computes it: one or two 64×64-bit products with a truncated power
|
|
63
|
+
* of five, which Mushtak and Lemire prove always decide the rounding for so few
|
|
64
|
+
* digits, so it never needs a fallback. A number of more digits, or with an
|
|
65
|
+
* exponent past `JSON_TOK_EXPONENT_CAP`, goes to the runtime's `parseFloat`,
|
|
66
|
+
* which is `strtod`, from a function whose own arena scope releases the copy it
|
|
67
|
+
* reads, so it leaves the arena where it was.
|
|
68
|
+
*
|
|
69
|
+
* `isInteger` and `numberI64` read a number written without a fraction or an
|
|
70
|
+
* exponent that fits in an `i64`, and `isU64` and `numberU64` one written
|
|
71
|
+
* without a sign either that fits in a `u64`. Those go by spelling;
|
|
72
|
+
* `isIntegral` asks whether the value is whole, however it is written (`1.0`,
|
|
73
|
+
* `1e2`), from the text and at any size.
|
|
70
74
|
*
|
|
71
75
|
* **Runs are read in bulk.** A string's plain bytes, the blanks between
|
|
72
76
|
* tokens and a number's digits are each stepped over by a loop that reads
|
package/std/json-typed.ts
CHANGED
|
@@ -15,19 +15,20 @@
|
|
|
15
15
|
* jsonEncode(w, p); // out[0 .. w.end) is {"x":..,"y":..}
|
|
16
16
|
* }
|
|
17
17
|
*
|
|
18
|
-
* **What is generated, and from what.** `jsonDecode(d, out)` and
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* of
|
|
30
|
-
*
|
|
18
|
+
* **What is generated, and from what.** `jsonDecode(d, out)` and `jsonEncode(w,
|
|
19
|
+
* value)` are generic, and the bodies written below are not what runs natively:
|
|
20
|
+
* the compiler lowers every call in place, in the module that makes it, to code
|
|
21
|
+
* it writes from the layout of the class the call binds `T` to
|
|
22
|
+
* (`src/json-typed.ts`, `src/emit-json.ts`).
|
|
23
|
+
*
|
|
24
|
+
* That code is straight-line calls into the methods of `JsonDecoder` and the
|
|
25
|
+
* functions here for each field, and into `JsonWriter`'s, so all of the parsing
|
|
26
|
+
* is the tokenizer's and all of the writing is the writer's. A field may be an
|
|
27
|
+
* `i32`, `u32`, `i64`, `u64`, `f64` (or `number`), `boolean` or `string`, a
|
|
28
|
+
* numeric enum (its number), a class decoded the same way, a `JsonNumber`, an
|
|
29
|
+
* array of any of those, a `Map<string, V>` (a JSON object) whose `V` is any of
|
|
30
|
+
* these, or a string, class, array or map `| null`; anything else is refused at
|
|
31
|
+
* the call, naming the field (docs/LANGUAGE.md, "Typed JSON").
|
|
31
32
|
*
|
|
32
33
|
* **Call shapes.** `jsonDecode(d, out)` reads the one JSON object `d` holds
|
|
33
34
|
* into `out`, a declared class, and answers `d.failed`: `JSON_DECODE_OK` (0)
|
|
@@ -72,18 +73,22 @@
|
|
|
72
73
|
*
|
|
73
74
|
* A `Map`'s `V` is any type in this list, `| null` or not, a `Map` included;
|
|
74
75
|
* `JsonNumber` may stand alone, be `| null`, be an array's element or be a
|
|
75
|
-
* `Map`'s value. The code written for a nested class, an array's element
|
|
76
|
-
*
|
|
77
|
-
* recurses as the data does.
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
* or a
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
76
|
+
* `Map`'s value. The code written for a nested class, an array's element class,
|
|
77
|
+
* a `| null` field's class and a `Map`'s values is the same code, so it
|
|
78
|
+
* recurses as the data does.
|
|
79
|
+
*
|
|
80
|
+
* Anything else is refused at the call's argument, naming the class and the
|
|
81
|
+
* field (NL2485 for a field type, such as an `f32`, an `i32[][]`, a
|
|
82
|
+
* `Level[][]`, a `Set`, an array of maps, a `Map` not keyed by `string` (an
|
|
83
|
+
* enum key included) or a `Map` whose values cannot be written, and a generic
|
|
84
|
+
* class of the program's own). An enum whose members are strings or a mix is
|
|
85
|
+
* not one the language declares (NL1043), and a `const enum` is refused where
|
|
86
|
+
* it is declared (NL2285).
|
|
87
|
+
*
|
|
88
|
+
* A value that is not a declared class (an interface, a generic class or any
|
|
89
|
+
* other type) is NL2486; decoding into an interface is left for a later
|
|
90
|
+
* version. A call inside a generic function is checked for each class an
|
|
91
|
+
* instantiation binds.
|
|
87
92
|
*
|
|
88
93
|
* **Reading the class.** A class the decoder has to make itself, an array's
|
|
89
94
|
* element past the end, a `| null` field that holds `null` or a map's value,
|
|
@@ -100,10 +105,11 @@
|
|
|
100
105
|
* once more, on the key's byte at the one position where those names differ
|
|
101
106
|
* most, and calls `keyIs` for each name with that byte there, in declaration
|
|
102
107
|
* order; a byte none of them has is an unknown key at once. One or two names
|
|
103
|
-
* are compared in turn with `keyIs`.
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
108
|
+
* are compared in turn with `keyIs`.
|
|
109
|
+
*
|
|
110
|
+
* A key with an escape has no byte to switch on there and is compared with
|
|
111
|
+
* every name of its length, so `"id"` is the field `id`; a field name that is
|
|
112
|
+
* not ASCII is dispatched by its UTF-8 bytes like any other.
|
|
107
113
|
*
|
|
108
114
|
* **The decoding rules.** A JSON object's keys are the class's field names,
|
|
109
115
|
* byte for byte, or the keys its options give them, found by the key's length
|
|
@@ -115,39 +121,44 @@
|
|
|
115
121
|
* }
|
|
116
122
|
*
|
|
117
123
|
* The compiler reads that literal; it is not a field. Every field is required
|
|
118
|
-
* but an optional one, whose missing key sets it to its initializer, a key
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* input's order: a nested object is decoded
|
|
126
|
-
* holds, an array's elements into the
|
|
127
|
-
* made past its end and the rest dropped
|
|
128
|
-
* object reuses its arrays and objects, and `Arena.used()` stays
|
|
129
|
-
* flat over 1,000 decodes of a three-level object), and a
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
* `
|
|
124
|
+
* but an optional one, whose missing key sets it to its initializer, a key may
|
|
125
|
+
* appear once, and a key that names no field is an error unless `ignoreUnknown`
|
|
126
|
+
* is set, when its value is stepped over, checked but not kept. A number must
|
|
127
|
+
* fit its field: an integer field takes an integer written without a fraction
|
|
128
|
+
* or an exponent and in the field's range, and `f64` takes any number.
|
|
129
|
+
*
|
|
130
|
+
* `null` is accepted only by a field whose type is `| null`. Fields are written
|
|
131
|
+
* to `out` as they are read, in the input's order: a nested object is decoded
|
|
132
|
+
* into the object the field already holds, an array's elements into the
|
|
133
|
+
* elements it already has, with new ones made past its end and the rest dropped
|
|
134
|
+
* (so a reused object reuses its arrays and objects, and `Arena.used()` stays
|
|
135
|
+
* flat: it is flat over 1,000 decodes of a three-level object), and a string
|
|
136
|
+
* field keeps the string it holds when the input's is equal.
|
|
137
|
+
*
|
|
138
|
+
* A string that changes is a new string, made by one `String.fromBytes`,
|
|
139
|
+
* straight from the input when it has no escape and from its unescaped bytes
|
|
140
|
+
* when it has one. A class the decoder has to make — an array element past the
|
|
141
|
+
* array's end, a `| null` field that holds `null`, or a map's value — is made
|
|
142
|
+
* with `new C()`.
|
|
143
|
+
*
|
|
144
|
+
* A map is emptied once the object opens, then each key is read as a new string
|
|
145
|
+
* and its value as a `V` made fresh for the key (a zero, `""`, the enum's first
|
|
146
|
+
* member, `null`, or a new object, array or map) and `set`, so the entries are
|
|
147
|
+
* the input's, in the input's order. A key the object gave already is
|
|
148
|
+
* `JSON_DECODE_DUPLICATE` before its value is read, however either is escaped.
|
|
149
|
+
* A value whose read fails is not `set`, so a map never holds a value the input
|
|
150
|
+
* did not give.
|
|
151
|
+
*
|
|
152
|
+
* Unlike the rest of a decode, a map's entries are made anew each time, so
|
|
153
|
+
* decoding into a map allocates its keys and values. Every map of one decoding
|
|
154
|
+
* takes at most `maxMapKeys` keys between them (4,096 unless set), the keys of
|
|
155
|
+
* a map inside another map included; the key past it is `JSON_DECODE_MAP_LIMIT`
|
|
156
|
+
* before it is read, and `reset` starts the count again. At `0` or below no map
|
|
157
|
+
* may take a key, and `{}` is the one map value accepted. Encoding writes the
|
|
158
|
+
* entries in the map's own order, which is insertion order.
|
|
159
|
+
*
|
|
160
|
+
* That differs from a JavaScript object in one case: a key that reads as an
|
|
161
|
+
* array index (`"1"`) keeps its place, where `JSON.parse` would move it first.
|
|
151
162
|
*
|
|
152
163
|
* **A class's options** are one `static readonly json` object literal written
|
|
153
164
|
* in the class, which the compiler reads when it writes the class's code. It
|
|
@@ -196,14 +207,15 @@
|
|
|
196
207
|
*
|
|
197
208
|
* A spread or a computed key in the literal is refused as it is anywhere
|
|
198
209
|
* (NL1061, NL1041), a subclass by `extends`, and a second `static readonly
|
|
199
|
-
* json` is a duplicate member. The options are not a value: `User.json` names
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
210
|
+
* json` is a duplicate member. The options are not a value: `User.json` names a
|
|
211
|
+
* class where a value is due, and is refused as any class name there is.
|
|
212
|
+
*
|
|
213
|
+
* Only `static readonly json` may leave out its type: written `static json = {
|
|
214
|
+
* ... }`, or as an instance field, it is an ordinary field to the parser and
|
|
203
215
|
* needs one, and an instance field called `json` with a type is an ordinary
|
|
204
|
-
* field, read and written under its own name. A field or a method called
|
|
205
|
-
*
|
|
206
|
-
*
|
|
216
|
+
* field, read and written under its own name. A field or a method called `json`
|
|
217
|
+
* may come before or after the options. Under Node the literal is a static
|
|
218
|
+
* property of the class that nothing reads.
|
|
207
219
|
*
|
|
208
220
|
* **Which keys were given.** An optional field's missing key and a key given
|
|
209
221
|
* the field's initializer leave the same value; the `presence` option tells
|
|
@@ -219,16 +231,18 @@
|
|
|
219
231
|
* hasEmail: boolean = false;
|
|
220
232
|
* }
|
|
221
233
|
*
|
|
222
|
-
* Any field can be recorded, of any kind and optional or not. A presence
|
|
223
|
-
*
|
|
234
|
+
* Any field can be recorded, of any kind and optional or not. A presence field
|
|
235
|
+
* is off the wire: no key reads it, encoding leaves it out, and no option
|
|
224
236
|
* renames it, makes it optional or records it. It is set when the object
|
|
225
|
-
* closes, from the flag the decoder already keeps per field to find a
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
237
|
+
* closes, from the flag the decoder already keeps per field to find a duplicate
|
|
238
|
+
* or a missing key, so recording costs one store per recorded field and
|
|
239
|
+
* allocates nothing, and a class that records nothing compiles to the code it
|
|
240
|
+
* did before.
|
|
241
|
+
*
|
|
242
|
+
* Each object sets its own, so a nested class or an array element answers for
|
|
243
|
+
* its own keys. A decoding that fails may set some of them and not others (when
|
|
244
|
+
* the input is not an object, none), so read them only when `jsonDecode`
|
|
245
|
+
* answered `JSON_DECODE_OK`.
|
|
232
246
|
*
|
|
233
247
|
* **A merge patch (RFC 7396)** is that option's use: a patch class whose
|
|
234
248
|
* every field is optional and recorded, decoded and then applied a field at a
|