nish 0.46.0 → 0.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/AI.md +29 -18
- package/package.json +5 -5
- package/runtime/nish.h +1 -0
- package/runtime/runtime-bytes.c +167 -7
- package/runtime/runtime-net.c +69 -25
- package/runtime/runtime-uring.c +111 -62
- package/runtime/sqlite/nish-sqlite.c +166 -39
- package/std/README.md +2 -2
- package/std/base64.ts +28 -30
- package/std/crypto/aes.ts +81 -45
- package/std/crypto/bcrypt-batch.ts +23 -4
- package/std/crypto/bcrypt.ts +36 -11
- package/std/crypto/chacha20poly1305.ts +93 -52
- package/std/crypto/x25519.ts +7 -4
- package/std/host/game.ts +52 -20
- package/std/host/ring.ts +115 -31
- package/std/host/text.ts +9 -3
- package/std/json.ts +83 -0
- package/std/math.ts +161 -131
- package/std/net/http1-server.ts +36 -11
- package/std/net/prefork.ts +1 -2
- package/std/net/static.ts +7 -2
- package/std/random.ts +34 -51
- package/std/raster.ts +360 -110
- package/std/regex.ts +46 -15
- package/std/release.ts +19 -13
- package/std/sqlite.ts +380 -16
- package/std/synth.ts +11 -2
- package/std/temporal.ts +740 -363
- package/std/term-io.ts +81 -33
- package/std/term-pixels.ts +287 -112
- package/std/term-probe.ts +7 -3
- package/std/term-rate.ts +142 -66
- package/std/term.ts +782 -406
- package/std/testing.ts +14 -7
package/std/crypto/x25519.ts
CHANGED
|
@@ -518,6 +518,8 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
|
|
|
518
518
|
*/
|
|
519
519
|
const f25519DecodeInto = (f: i64[], bytes: u8[]): void => {
|
|
520
520
|
let acc: i64 = 0
|
|
521
|
+
// Never above 33. The masks are no-ops on such a count and on a width of 25
|
|
522
|
+
// or 26; they let the range prover bound every sum and difference.
|
|
521
523
|
let bits: i64 = 0
|
|
522
524
|
let limb: i32 = 0
|
|
523
525
|
for (let i: i32 = 0; i < 32; i++) {
|
|
@@ -526,13 +528,13 @@ const f25519DecodeInto = (f: i64[], bytes: u8[]): void => {
|
|
|
526
528
|
b = b & toI64(0x7f)
|
|
527
529
|
}
|
|
528
530
|
acc = acc | (b << bits)
|
|
529
|
-
bits = bits + toI64(8)
|
|
531
|
+
bits = (bits & toI64(0x3f)) + toI64(8)
|
|
530
532
|
if (limb >= 0 && limb < 10) {
|
|
531
533
|
const width: i64 = f25519Width(limb)
|
|
532
534
|
if (bits >= width) {
|
|
533
535
|
f[limb] = acc & f25519LimbMask(limb)
|
|
534
536
|
acc = acc >> width
|
|
535
|
-
bits = bits - width
|
|
537
|
+
bits = (bits & toI64(0x3f)) - (width & toI64(0x1f))
|
|
536
538
|
limb = limb + 1
|
|
537
539
|
}
|
|
538
540
|
}
|
|
@@ -582,15 +584,16 @@ const f25519EncodeInto = (out: u8[], f: i64[]): void => {
|
|
|
582
584
|
h[9] = h[9] & f25519LimbMask(9)
|
|
583
585
|
|
|
584
586
|
let acc: i64 = 0
|
|
587
|
+
// Below 34, as in `f25519DecodeInto`, whose masks these are.
|
|
585
588
|
let bits: i64 = 0
|
|
586
589
|
let at: i32 = 0
|
|
587
590
|
for (let i: i32 = 0; i < 10; i++) {
|
|
588
591
|
acc = acc | (h[i] << bits)
|
|
589
|
-
bits = bits + f25519Width(i)
|
|
592
|
+
bits = (bits & toI64(0x3f)) + (f25519Width(i) & toI64(0x1f))
|
|
590
593
|
while (bits >= toI64(8) && at >= 0 && at < 32) {
|
|
591
594
|
out[at] = toU8(acc & toI64(0xff))
|
|
592
595
|
acc = acc >> toI64(8)
|
|
593
|
-
bits = bits - toI64(8)
|
|
596
|
+
bits = (bits & toI64(0x3f)) - toI64(8)
|
|
594
597
|
at = at + 1
|
|
595
598
|
}
|
|
596
599
|
}
|
package/std/host/game.ts
CHANGED
|
@@ -148,6 +148,28 @@ export const GAME_SAMPLES = new Float32Array(96000)
|
|
|
148
148
|
/** `v` held to `0..most`. */
|
|
149
149
|
const gameClamp = (v: i32, most: i32): i32 => Math.min(Math.max(v, 0), most)
|
|
150
150
|
|
|
151
|
+
/** Word `word` of `info`, which is `GAME_INFO`, held to `0..most`. */
|
|
152
|
+
const gameInfoWord = (info: Int32Array, word: i32, most: i32): i32 => {
|
|
153
|
+
// Never true: `word` is one of the `GAME_INFO_*` indices and `GAME_INFO` is
|
|
154
|
+
// four words for the module's life. The test is what proves the read.
|
|
155
|
+
if (word < 0 || word >= toI32(info.length)) {
|
|
156
|
+
return 0
|
|
157
|
+
}
|
|
158
|
+
return gameClamp(info[word], most)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Store `a` and `b` in words `at` and `at + 1` of `info`, which is `GAME_INFO`. */
|
|
162
|
+
const gameInfoPair = (info: Int32Array, at: i32, a: i32, b: i32): void => {
|
|
163
|
+
// Never true: `at` is `GAME_INFO_WIDTH` or `GAME_INFO_SAMPLES` and
|
|
164
|
+
// `GAME_INFO` is four words for the module's life. The test is what proves
|
|
165
|
+
// the stores.
|
|
166
|
+
if (at < 0 || at >= toI32(info.length) - 1) {
|
|
167
|
+
return
|
|
168
|
+
}
|
|
169
|
+
info[at] = a
|
|
170
|
+
info[at + 1] = b
|
|
171
|
+
}
|
|
172
|
+
|
|
151
173
|
/**
|
|
152
174
|
* Set the frame's size, which the page reads to know how much of `GAME_FRAME`
|
|
153
175
|
* to show. Each side is held to `0..GAME_MAX_WIDTH` and `0..GAME_MAX_HEIGHT`.
|
|
@@ -156,24 +178,27 @@ const gameClamp = (v: i32, most: i32): i32 => Math.min(Math.max(v, 0), most)
|
|
|
156
178
|
* @param width - the frame's width in pixels
|
|
157
179
|
* @param height - the frame's height in pixels
|
|
158
180
|
*/
|
|
159
|
-
export const gameSetSize = (width: i32, height: i32): void =>
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
181
|
+
export const gameSetSize = (width: i32, height: i32): void =>
|
|
182
|
+
gameInfoPair(
|
|
183
|
+
GAME_INFO,
|
|
184
|
+
GAME_INFO_WIDTH,
|
|
185
|
+
gameClamp(width, GAME_MAX_WIDTH),
|
|
186
|
+
gameClamp(height, GAME_MAX_HEIGHT)
|
|
187
|
+
)
|
|
163
188
|
|
|
164
189
|
/**
|
|
165
190
|
* The frame's width, as `gameSetSize` set it.
|
|
166
191
|
*
|
|
167
192
|
* @returns the width in pixels, held to `0..GAME_MAX_WIDTH` even if the page wrote the word
|
|
168
193
|
*/
|
|
169
|
-
export const gameWidth = (): i32 =>
|
|
194
|
+
export const gameWidth = (): i32 => gameInfoWord(GAME_INFO, GAME_INFO_WIDTH, GAME_MAX_WIDTH)
|
|
170
195
|
|
|
171
196
|
/**
|
|
172
197
|
* The frame's height, as `gameSetSize` set it.
|
|
173
198
|
*
|
|
174
199
|
* @returns the height in pixels, held to `0..GAME_MAX_HEIGHT` even if the page wrote the word
|
|
175
200
|
*/
|
|
176
|
-
export const gameHeight = (): i32 =>
|
|
201
|
+
export const gameHeight = (): i32 => gameInfoWord(GAME_INFO, GAME_INFO_HEIGHT, GAME_MAX_HEIGHT)
|
|
177
202
|
|
|
178
203
|
/** The next record on `GAME_INPUT` as a `TermEvent`, or null for a kind the reader does not know. */
|
|
179
204
|
const gameEventOf = (): TermEvent | null => {
|
|
@@ -244,6 +269,25 @@ export const gameReadInput = (input: StepInput, nowMs: f64): void => {
|
|
|
244
269
|
*/
|
|
245
270
|
export const gamePlaySound = (id: i32): boolean => ringPush(GAME_SOUNDS, id)
|
|
246
271
|
|
|
272
|
+
/** Write the first `n` of `samples` into `out`, which is `GAME_SAMPLES`, clipped to `-1..1`. */
|
|
273
|
+
const gameCopySamples = (out: Float32Array, samples: f64[], n: i32): void => {
|
|
274
|
+
// `n` is at most `GAME_SAMPLE_LIMIT`, the length of `GAME_SAMPLES`, and at
|
|
275
|
+
// most `samples.length`: the length tests never end the loop first, and
|
|
276
|
+
// they are what prove the read and the store.
|
|
277
|
+
for (let i: i32 = 0; i < n && i < toI32(samples.length) && i < toI32(out.length); i++) {
|
|
278
|
+
const s: f64 = samples[i]
|
|
279
|
+
let x: f64 = 0.0
|
|
280
|
+
if (s > 1.0) {
|
|
281
|
+
x = 1.0
|
|
282
|
+
} else if (s < -1.0) {
|
|
283
|
+
x = -1.0
|
|
284
|
+
} else if (s >= -1.0) {
|
|
285
|
+
x = s
|
|
286
|
+
}
|
|
287
|
+
out[i] = toF32(x)
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
247
291
|
/**
|
|
248
292
|
* Hand one sound's samples to the page: each is clipped to `-1..1` (NaN is
|
|
249
293
|
* 0) and written to `GAME_SAMPLES` as an `f32`, and `GAME_INFO` records how
|
|
@@ -255,19 +299,7 @@ export const gamePlaySound = (id: i32): boolean => ringPush(GAME_SOUNDS, id)
|
|
|
255
299
|
*/
|
|
256
300
|
export const gameWriteSamples = (samples: f64[], sampleRate: i32): i32 => {
|
|
257
301
|
const n: i32 = Math.min(toI32(samples.length), GAME_SAMPLE_LIMIT)
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
let x: f64 = 0.0
|
|
261
|
-
if (s > 1.0) {
|
|
262
|
-
x = 1.0
|
|
263
|
-
} else if (s < -1.0) {
|
|
264
|
-
x = -1.0
|
|
265
|
-
} else if (s >= -1.0) {
|
|
266
|
-
x = s
|
|
267
|
-
}
|
|
268
|
-
GAME_SAMPLES[i] = toF32(x)
|
|
269
|
-
}
|
|
270
|
-
GAME_INFO[GAME_INFO_SAMPLES] = n
|
|
271
|
-
GAME_INFO[GAME_INFO_RATE] = sampleRate
|
|
302
|
+
gameCopySamples(GAME_SAMPLES, samples, n)
|
|
303
|
+
gameInfoPair(GAME_INFO, GAME_INFO_SAMPLES, n, sampleRate)
|
|
272
304
|
return n
|
|
273
305
|
}
|
package/std/host/ring.ts
CHANGED
|
@@ -32,25 +32,70 @@
|
|
|
32
32
|
* `ringShift` takes words until the counts agree again.
|
|
33
33
|
*/
|
|
34
34
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* The most slots a ring uses, 2^30 - 1: its counts run modulo twice the
|
|
37
|
+
* capacity, and twice the capacity must fit an `i32`. Wasm memory cannot hold
|
|
38
|
+
* a longer ring; a longer native one leaves the slots past that unused.
|
|
39
|
+
*/
|
|
40
|
+
type RingSlots = integer<0, 1073741823>
|
|
41
|
+
|
|
42
|
+
/** A count reduced modulo twice the capacity. */
|
|
43
|
+
type RingCount = integer<0, 2147483645>
|
|
44
|
+
|
|
45
|
+
/** The slot count of `ring`: its length less the head and tail, at most `RingSlots`' end. */
|
|
46
|
+
const ringSlots = (ring: Int32Array): RingSlots => {
|
|
47
|
+
const length: i32 = toI32(ring.length)
|
|
48
|
+
if (length <= 2) {
|
|
49
|
+
return 0
|
|
50
|
+
}
|
|
51
|
+
if (length > 1073741825) {
|
|
52
|
+
return 1073741823
|
|
53
|
+
}
|
|
54
|
+
const slots: i32 = length - 2
|
|
55
|
+
return slots
|
|
56
|
+
}
|
|
37
57
|
|
|
38
58
|
/**
|
|
39
|
-
* The number of slots, the ring's length less its header.
|
|
59
|
+
* The number of slots, the ring's length less its header, at most 2^30 - 1.
|
|
40
60
|
*
|
|
41
61
|
* @param ring - the ring's storage: head, tail, then its slots
|
|
42
62
|
* @returns the slot count, or 0 for a ring too short to have one
|
|
43
63
|
*/
|
|
44
|
-
export const ringCapacity = (ring: Int32Array): i32 =>
|
|
45
|
-
const length: i32 = toI32(ring.length)
|
|
46
|
-
return length > RING_HEADER ? length - RING_HEADER : 0
|
|
47
|
-
}
|
|
64
|
+
export const ringCapacity = (ring: Int32Array): i32 => ringSlots(ring)
|
|
48
65
|
|
|
49
66
|
/** `count` brought into `[0, 2 * capacity)`, whatever a page wrote there. */
|
|
50
|
-
const ringCount = (count: i32, capacity:
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
67
|
+
const ringCount = (count: i32, capacity: RingSlots): RingCount => {
|
|
68
|
+
const span: i32 = capacity * 2
|
|
69
|
+
// 0 for a ring with no slots. The second test is never true, the capacity
|
|
70
|
+
// being at most 2^30 - 1, and is the bound on `span` the last test reads.
|
|
71
|
+
if (span <= 0 || span > 2147483646) {
|
|
72
|
+
return 0
|
|
73
|
+
}
|
|
74
|
+
let r: i32 = count % span
|
|
75
|
+
if (r < 0) {
|
|
76
|
+
r = r + span
|
|
77
|
+
}
|
|
78
|
+
// Never true: `%` leaves `r` above `-span` and the step above lifts a
|
|
79
|
+
// negative one into `[0, span)`. A `%` by a variable gives the prover no
|
|
80
|
+
// range, so the test restates it where the ranged return reads it.
|
|
81
|
+
if (r < 0 || r >= span) {
|
|
82
|
+
return 0
|
|
83
|
+
}
|
|
84
|
+
return r
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The words waiting in `ring` of `capacity` slots: a size past it reads as full. */
|
|
88
|
+
const ringWaiting = (ring: Int32Array, capacity: RingSlots): RingSlots => {
|
|
89
|
+
if (capacity === 0 || toI32(ring.length) < 2) {
|
|
90
|
+
return 0
|
|
91
|
+
}
|
|
92
|
+
const tail: RingCount = ringCount(ring[1], capacity)
|
|
93
|
+
const head: RingCount = ringCount(ring[0], capacity)
|
|
94
|
+
const size: RingCount = ringCount(tail - head, capacity)
|
|
95
|
+
if (size > capacity) {
|
|
96
|
+
return capacity
|
|
97
|
+
}
|
|
98
|
+
return size
|
|
54
99
|
}
|
|
55
100
|
|
|
56
101
|
/**
|
|
@@ -59,14 +104,7 @@ const ringCount = (count: i32, capacity: i32): i32 => {
|
|
|
59
104
|
* @param ring - the ring's storage: head, tail, then its slots
|
|
60
105
|
* @returns the words waiting, at most the capacity
|
|
61
106
|
*/
|
|
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
|
-
}
|
|
107
|
+
export const ringSize = (ring: Int32Array): i32 => ringWaiting(ring, ringSlots(ring))
|
|
70
108
|
|
|
71
109
|
/**
|
|
72
110
|
* The number of words `ringPush` would still take.
|
|
@@ -74,7 +112,11 @@ export const ringSize = (ring: Int32Array): i32 => {
|
|
|
74
112
|
* @param ring - the ring's storage: head, tail, then its slots
|
|
75
113
|
* @returns the free slots
|
|
76
114
|
*/
|
|
77
|
-
export const ringFree = (ring: Int32Array): i32 =>
|
|
115
|
+
export const ringFree = (ring: Int32Array): i32 => {
|
|
116
|
+
const capacity: RingSlots = ringSlots(ring)
|
|
117
|
+
const waiting: RingSlots = ringWaiting(ring, capacity)
|
|
118
|
+
return capacity - waiting
|
|
119
|
+
}
|
|
78
120
|
|
|
79
121
|
/**
|
|
80
122
|
* Puts `word` at the tail, or changes nothing when the ring is full.
|
|
@@ -84,13 +126,35 @@ export const ringFree = (ring: Int32Array): i32 => ringCapacity(ring) - ringSize
|
|
|
84
126
|
* @returns true when the word was put, false when the ring was full
|
|
85
127
|
*/
|
|
86
128
|
export const ringPush = (ring: Int32Array, word: i32): boolean => {
|
|
87
|
-
const capacity:
|
|
88
|
-
if (capacity === 0 ||
|
|
129
|
+
const capacity: RingSlots = ringSlots(ring)
|
|
130
|
+
if (capacity === 0 || ringWaiting(ring, capacity) >= capacity) {
|
|
89
131
|
return false
|
|
90
132
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
133
|
+
if (toI32(ring.length) < 2) {
|
|
134
|
+
return false
|
|
135
|
+
}
|
|
136
|
+
const length: i32 = toI32(ring.length)
|
|
137
|
+
const tail: RingCount = ringCount(ring[1], capacity)
|
|
138
|
+
// The tail is `base + at`: `base` is 0 or the capacity, `at` the slot.
|
|
139
|
+
let base: i32 = 0
|
|
140
|
+
let at: i32 = tail
|
|
141
|
+
if (at >= capacity) {
|
|
142
|
+
base = capacity
|
|
143
|
+
at = at - capacity
|
|
144
|
+
}
|
|
145
|
+
// Never true: the tail is below twice the capacity, and the capacity is at
|
|
146
|
+
// most the length less two. The prover reads neither through the calls, so
|
|
147
|
+
// the test is what proves the store and the next count.
|
|
148
|
+
if (at < 0 || at >= capacity || at >= length - 2) {
|
|
149
|
+
return false
|
|
150
|
+
}
|
|
151
|
+
ring[2 + at] = word
|
|
152
|
+
let next: i32 = at + 1
|
|
153
|
+
if (next === capacity) {
|
|
154
|
+
next = 0
|
|
155
|
+
base = capacity - base
|
|
156
|
+
}
|
|
157
|
+
ring[1] = base + next
|
|
94
158
|
return true
|
|
95
159
|
}
|
|
96
160
|
|
|
@@ -102,13 +166,33 @@ export const ringPush = (ring: Int32Array, word: i32): boolean => {
|
|
|
102
166
|
* @returns the word taken, or 0 on an empty ring
|
|
103
167
|
*/
|
|
104
168
|
export const ringShift = (ring: Int32Array): i32 => {
|
|
105
|
-
const capacity:
|
|
106
|
-
if (capacity === 0 ||
|
|
169
|
+
const capacity: RingSlots = ringSlots(ring)
|
|
170
|
+
if (capacity === 0 || ringWaiting(ring, capacity) === 0) {
|
|
107
171
|
return 0
|
|
108
172
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
173
|
+
if (toI32(ring.length) < 2) {
|
|
174
|
+
return 0
|
|
175
|
+
}
|
|
176
|
+
const length: i32 = toI32(ring.length)
|
|
177
|
+
const head: RingCount = ringCount(ring[0], capacity)
|
|
178
|
+
// The head is `base + at`, as the tail is in `ringPush`.
|
|
179
|
+
let base: i32 = 0
|
|
180
|
+
let at: i32 = head
|
|
181
|
+
if (at >= capacity) {
|
|
182
|
+
base = capacity
|
|
183
|
+
at = at - capacity
|
|
184
|
+
}
|
|
185
|
+
// Never true, for the reasons `ringPush` gives; the test proves the read.
|
|
186
|
+
if (at < 0 || at >= capacity || at >= length - 2) {
|
|
187
|
+
return 0
|
|
188
|
+
}
|
|
189
|
+
const word: i32 = ring[2 + at]
|
|
190
|
+
let next: i32 = at + 1
|
|
191
|
+
if (next === capacity) {
|
|
192
|
+
next = 0
|
|
193
|
+
base = capacity - base
|
|
194
|
+
}
|
|
195
|
+
ring[0] = base + next
|
|
112
196
|
return word
|
|
113
197
|
}
|
|
114
198
|
|
|
@@ -118,7 +202,7 @@ export const ringShift = (ring: Int32Array): i32 => {
|
|
|
118
202
|
* @param ring - the ring's storage: head, tail, then its slots
|
|
119
203
|
*/
|
|
120
204
|
export const ringClear = (ring: Int32Array): void => {
|
|
121
|
-
if (toI32(ring.length) >=
|
|
205
|
+
if (toI32(ring.length) >= 2) {
|
|
122
206
|
ring[0] = ring[1]
|
|
123
207
|
}
|
|
124
208
|
}
|
package/std/host/text.ts
CHANGED
|
@@ -42,10 +42,16 @@ export const textWrite = (buf: u8[], at: i32, s: string): i32 => {
|
|
|
42
42
|
if (at < 0 || at > length || n > length - at) {
|
|
43
43
|
return -1
|
|
44
44
|
}
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
// `j` runs from `at` to `at + n`, which the test above holds inside the
|
|
46
|
+
// buffer: the loop ends on `i`, and `j < length` is what proves the store.
|
|
47
|
+
let j: i32 = at
|
|
48
|
+
let i: i32 = 0
|
|
49
|
+
while (i < n && j < length) {
|
|
50
|
+
buf[j] = toU8(s.charCodeAt(i))
|
|
51
|
+
j = j + 1
|
|
52
|
+
i = i + 1
|
|
47
53
|
}
|
|
48
|
-
return
|
|
54
|
+
return j
|
|
49
55
|
}
|
|
50
56
|
|
|
51
57
|
/**
|
package/std/json.ts
CHANGED
|
@@ -2124,6 +2124,89 @@ const jsonPutF64Walk = (buf: u8[], at: i32, v: f64): i32 => {
|
|
|
2124
2124
|
return j
|
|
2125
2125
|
}
|
|
2126
2126
|
|
|
2127
|
+
/**
|
|
2128
|
+
* `src[from .. to)` as a JSON string, quotes included, in `dst` from `at`:
|
|
2129
|
+
* the new end, or -1 when `at` is negative or the text does not fit before
|
|
2130
|
+
* `dst.length`, when the bytes from `at` may have been written. The caller
|
|
2131
|
+
* has checked `0 <= from <= to <= src.length`. Natively the compiler
|
|
2132
|
+
* replaces the one call to it with the runtime's `nish_json_put_string`
|
|
2133
|
+
* (`src/kernels.ts`), which copies sixteen bytes a block; as written, which
|
|
2134
|
+
* is what Node and `--emit-ts` run, it is `jsonWriterPutBytes`'s loop.
|
|
2135
|
+
*/
|
|
2136
|
+
const jsonPutStringWalk = (dst: u8[], at: i32, src: readonly u8[], from: i32, to: i32): i32 => {
|
|
2137
|
+
const cap: i32 = toI32(dst.length)
|
|
2138
|
+
let o: i32 = at
|
|
2139
|
+
if (o < 0 || o >= cap) {
|
|
2140
|
+
return -1
|
|
2141
|
+
}
|
|
2142
|
+
dst[o] = toU8(JSON_QUOTE)
|
|
2143
|
+
o += 1
|
|
2144
|
+
for (let i: i32 = from; i >= 0 && i < to && i < toI32(src.length); i += 1) {
|
|
2145
|
+
const code: i32 = toI32(src[i])
|
|
2146
|
+
if (jsonIsPlainByte(code)) {
|
|
2147
|
+
if (o < 0 || o >= cap) {
|
|
2148
|
+
return -1
|
|
2149
|
+
}
|
|
2150
|
+
dst[o] = toU8(code)
|
|
2151
|
+
o += 1
|
|
2152
|
+
continue
|
|
2153
|
+
}
|
|
2154
|
+
const letter: i32 = jsonShortEscape(code)
|
|
2155
|
+
if (letter !== 0) {
|
|
2156
|
+
if (o < 0 || o >= cap - 1) {
|
|
2157
|
+
return -1
|
|
2158
|
+
}
|
|
2159
|
+
dst[o] = toU8(JSON_BACKSLASH)
|
|
2160
|
+
dst[o + 1] = toU8(letter)
|
|
2161
|
+
o += 2
|
|
2162
|
+
continue
|
|
2163
|
+
}
|
|
2164
|
+
if (o < 0 || o >= cap - 5) {
|
|
2165
|
+
return -1
|
|
2166
|
+
}
|
|
2167
|
+
dst[o] = toU8(JSON_BACKSLASH)
|
|
2168
|
+
dst[o + 1] = toU8(JSON_LETTER_U)
|
|
2169
|
+
dst[o + 2] = toU8(JSON_DIGIT_ZERO)
|
|
2170
|
+
dst[o + 3] = toU8(JSON_DIGIT_ZERO)
|
|
2171
|
+
dst[o + 4] = toU8(jsonHexByte(code >> 4))
|
|
2172
|
+
dst[o + 5] = toU8(jsonHexByte(code & 15))
|
|
2173
|
+
o += 6
|
|
2174
|
+
}
|
|
2175
|
+
if (o < 0 || o >= cap) {
|
|
2176
|
+
return -1
|
|
2177
|
+
}
|
|
2178
|
+
dst[o] = toU8(JSON_QUOTE)
|
|
2179
|
+
return o + 1
|
|
2180
|
+
}
|
|
2181
|
+
|
|
2182
|
+
/**
|
|
2183
|
+
* `src[from .. from + len)`, UTF-8 or any bytes, as a JSON string with its
|
|
2184
|
+
* quotes, written into `dst` from `at`, as `JsonWriter.stringBytes` writes a
|
|
2185
|
+
* string value: `"`, `\` and the bytes below a space are escaped, the five
|
|
2186
|
+
* with a short form as `\n`, `\t`, `\r`, `\b` and `\f` and the rest as
|
|
2187
|
+
* `\u00XX`, and every other byte is copied as it is. It writes no comma or
|
|
2188
|
+
* key, so a program that builds its JSON from pre-encoded pieces writes its
|
|
2189
|
+
* strings with it. Natively the copy is the runtime's
|
|
2190
|
+
* `nish_json_put_string`, sixteen bytes a block, and allocates nothing.
|
|
2191
|
+
*
|
|
2192
|
+
* @param dst - the buffer written into; it is never grown.
|
|
2193
|
+
* @param at - where the opening quote goes.
|
|
2194
|
+
* @param src - the bytes of the string.
|
|
2195
|
+
* @param from - the offset in `src` of its first byte.
|
|
2196
|
+
* @param len - how many bytes it is.
|
|
2197
|
+
* @returns The index after the closing quote, or -1 when a window lies
|
|
2198
|
+
* outside its array or the string does not fit in `dst`: then the bytes
|
|
2199
|
+
* of `dst` from `at` may have been written, and the caller grows `dst` and
|
|
2200
|
+
* writes again from `at`.
|
|
2201
|
+
*/
|
|
2202
|
+
export const jsonPutString = (dst: u8[], at: i32, src: readonly u8[], from: i32, len: i32): i32 => {
|
|
2203
|
+
if (from < 0 || len < 0 || from > toI32(src.length) - len) {
|
|
2204
|
+
return -1
|
|
2205
|
+
}
|
|
2206
|
+
// In u32, which cannot wrap: the window was checked to lie inside `src`.
|
|
2207
|
+
return jsonPutStringWalk(dst, at, src, from, toI32(toU32(from) + toU32(len)))
|
|
2208
|
+
}
|
|
2209
|
+
|
|
2127
2210
|
/** The digits `${v}` spells for a finite `v`, at the cursor; all of them or, with `JSON_WRITE_FULL`, none. */
|
|
2128
2211
|
const jsonWriterPutF64 = (w: JsonWriter, v: f64): boolean => {
|
|
2129
2212
|
const stop: i32 = jsonPutF64Walk(w.buf, w.end, v)
|