@amritk/nish-aarch64-linux 0.45.1 → 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/INSTALL.md +3 -1
- package/bin/nish +0 -0
- package/docs/AI.md +29 -18
- package/package.json +1 -1
- package/runtime/nish.d.ts +41 -0
- package/runtime/nish.h +11 -0
- package/runtime/nish.mjs +1 -0
- package/runtime/runtime-bytes.c +167 -7
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +272 -9
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +394 -85
- package/runtime/sqlite/nish-sqlite.c +448 -148
- package/std/README.md +14 -14
- 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/handler.ts +42 -7
- 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 +551 -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/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)
|