nish 0.37.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/std/array.ts ADDED
@@ -0,0 +1,266 @@
1
+ /**
2
+ * `std/array` — the array methods that take a callback, and `slice`, in Nish:
3
+ * each a plain loop, compiled once per callback.
4
+ *
5
+ * export const main = (): i32 => {
6
+ * const xs: i32[] = [3, 1, 2];
7
+ * const doubled = xs.map((x) => x * 2); // [6, 2, 4]
8
+ * const big = xs.filter((x) => x > 1); // [3, 2]
9
+ * return doubled[0] + big.length + xs.findIndex((x) => x === 2); // 6 + 2 + 2
10
+ * };
11
+ *
12
+ * **A program does not need to import this module.** A `.map(...)`,
13
+ * `.filter(...)`, `.some(...)`, `.every(...)`, `.findIndex(...)` or
14
+ * `.slice(...)` call on an array is what loads it (the checker's
15
+ * `importArrayMethods`), and the checker routes the call to the template here
16
+ * that matches it: `xs.map(f)` is `map(xs, f)` when `f` takes one parameter
17
+ * and `mapIndexed(xs, f)` when it takes two. `f` is a compile-time function
18
+ * parameter, so each callback is an instantiation of its own whose body calls
19
+ * it directly — there is no function pointer, and importing a template here
20
+ * by name and calling it gives the same code as the method. Under Node,
21
+ * `--emit-ts` leaves `map`, `filter`, `some`, `every` and `findIndex` as the
22
+ * platform's own `Array.prototype` methods, writes `slice` as the runtime's
23
+ * checked `sliceArray`, and copies an answer that holds records inline with
24
+ * `copyRecords`, so nothing here runs there.
25
+ *
26
+ * **Same answers as Node.** Each walks the array from index 0 upward and
27
+ * calls the callback once per element, in order, until the length it had when
28
+ * the call began, as `Array.prototype` does; `some`, `every` and `findIndex`
29
+ * stop at the first element that decides the answer. A callback that shrinks
30
+ * the array ends the walk at the new length, where JavaScript goes on and
31
+ * skips the missing indices.
32
+ */
33
+
34
+ /**
35
+ * The elements of `xs`, each passed through `f`, in a new array of the same
36
+ * length: what `xs.map(f)` compiles to when `f` takes one parameter.
37
+ *
38
+ * @param xs the array read; it is not changed.
39
+ * @param f called once per element, in order.
40
+ * @returns a new array whose element `i` is `f(xs[i])`.
41
+ */
42
+ export const map = <T, U>(xs: readonly T[], f: (x: T) => U): U[] => {
43
+ const out: U[] = []
44
+ const n = xs.length
45
+ for (let i: number = 0; i < n && i < xs.length; i++) {
46
+ out.push(f(xs[i]))
47
+ }
48
+ return out
49
+ }
50
+
51
+ /**
52
+ * The elements of `xs`, each passed through `f` with its index: what
53
+ * `xs.map(f)` compiles to when `f` takes two parameters.
54
+ *
55
+ * @param xs the array read; it is not changed.
56
+ * @param f called once per element, in order, with the element and its index.
57
+ * @returns a new array whose element `i` is `f(xs[i], i)`.
58
+ */
59
+ export const mapIndexed = <T, U>(xs: readonly T[], f: (x: T, i: number) => U): U[] => {
60
+ const out: U[] = []
61
+ const n = xs.length
62
+ for (let i: number = 0; i < n && i < xs.length; i++) {
63
+ out.push(f(xs[i], i))
64
+ }
65
+ return out
66
+ }
67
+
68
+ /**
69
+ * The elements of `xs` for which `pred` answers `true`, in order, in a new
70
+ * array: what `xs.filter(pred)` compiles to when `pred` takes one parameter.
71
+ *
72
+ * @param xs the array read; it is not changed.
73
+ * @param pred called once per element, in order.
74
+ * @returns a new array of the elements kept.
75
+ */
76
+ export const filter = <T>(xs: readonly T[], pred: (x: T) => boolean): T[] => {
77
+ const out: T[] = []
78
+ const n = xs.length
79
+ for (let i: number = 0; i < n && i < xs.length; i++) {
80
+ const x = xs[i]
81
+ if (pred(x)) {
82
+ out.push(x)
83
+ }
84
+ }
85
+ return out
86
+ }
87
+
88
+ /**
89
+ * The elements of `xs` for which `pred` answers `true`, given each element and
90
+ * its index: what `xs.filter(pred)` compiles to when `pred` takes two
91
+ * parameters.
92
+ *
93
+ * @param xs the array read; it is not changed.
94
+ * @param pred called once per element, in order, with the element and its index.
95
+ * @returns a new array of the elements kept.
96
+ */
97
+ export const filterIndexed = <T>(xs: readonly T[], pred: (x: T, i: number) => boolean): T[] => {
98
+ const out: T[] = []
99
+ const n = xs.length
100
+ for (let i: number = 0; i < n && i < xs.length; i++) {
101
+ const x = xs[i]
102
+ if (pred(x, i)) {
103
+ out.push(x)
104
+ }
105
+ }
106
+ return out
107
+ }
108
+
109
+ /**
110
+ * Whether `pred` answers `true` for at least one element of `xs`, stopping at
111
+ * the first that does: what `xs.some(pred)` compiles to when `pred` takes one
112
+ * parameter. An empty array answers `false`.
113
+ *
114
+ * @param xs the array read; it is not changed.
115
+ * @param pred called in order until it answers `true`.
116
+ * @returns `true` when some element satisfies `pred`.
117
+ */
118
+ export const some = <T>(xs: readonly T[], pred: (x: T) => boolean): boolean => {
119
+ const n = xs.length
120
+ for (let i: number = 0; i < n && i < xs.length; i++) {
121
+ if (pred(xs[i])) {
122
+ return true
123
+ }
124
+ }
125
+ return false
126
+ }
127
+
128
+ /**
129
+ * Whether `pred` answers `true` for at least one element of `xs`, given each
130
+ * element and its index: what `xs.some(pred)` compiles to when `pred` takes
131
+ * two parameters.
132
+ *
133
+ * @param xs the array read; it is not changed.
134
+ * @param pred called in order, with the element and its index, until it answers `true`.
135
+ * @returns `true` when some element satisfies `pred`.
136
+ */
137
+ export const someIndexed = <T>(xs: readonly T[], pred: (x: T, i: number) => boolean): boolean => {
138
+ const n = xs.length
139
+ for (let i: number = 0; i < n && i < xs.length; i++) {
140
+ if (pred(xs[i], i)) {
141
+ return true
142
+ }
143
+ }
144
+ return false
145
+ }
146
+
147
+ /**
148
+ * Whether `pred` answers `true` for every element of `xs`, stopping at the
149
+ * first that does not: what `xs.every(pred)` compiles to when `pred` takes one
150
+ * parameter. An empty array answers `true`.
151
+ *
152
+ * @param xs the array read; it is not changed.
153
+ * @param pred called in order until it answers `false`.
154
+ * @returns `true` when no element fails `pred`.
155
+ */
156
+ export const every = <T>(xs: readonly T[], pred: (x: T) => boolean): boolean => {
157
+ const n = xs.length
158
+ for (let i: number = 0; i < n && i < xs.length; i++) {
159
+ if (!pred(xs[i])) {
160
+ return false
161
+ }
162
+ }
163
+ return true
164
+ }
165
+
166
+ /**
167
+ * Whether `pred` answers `true` for every element of `xs`, given each element
168
+ * and its index: what `xs.every(pred)` compiles to when `pred` takes two
169
+ * parameters.
170
+ *
171
+ * @param xs the array read; it is not changed.
172
+ * @param pred called in order, with the element and its index, until it answers `false`.
173
+ * @returns `true` when no element fails `pred`.
174
+ */
175
+ export const everyIndexed = <T>(xs: readonly T[], pred: (x: T, i: number) => boolean): boolean => {
176
+ const n = xs.length
177
+ for (let i: number = 0; i < n && i < xs.length; i++) {
178
+ if (!pred(xs[i], i)) {
179
+ return false
180
+ }
181
+ }
182
+ return true
183
+ }
184
+
185
+ /**
186
+ * The index of the first element of `xs` for which `pred` answers `true`:
187
+ * what `xs.findIndex(pred)` compiles to when `pred` takes one parameter.
188
+ *
189
+ * @param xs the array read; it is not changed.
190
+ * @param pred called in order until it answers `true`.
191
+ * @returns the first such index, or `-1` when there is none.
192
+ */
193
+ export const findIndex = <T>(xs: readonly T[], pred: (x: T) => boolean): number => {
194
+ const n = xs.length
195
+ for (let i: number = 0; i < n && i < xs.length; i++) {
196
+ if (pred(xs[i])) {
197
+ return i
198
+ }
199
+ }
200
+ return -1
201
+ }
202
+
203
+ /**
204
+ * The index of the first element of `xs` for which `pred` answers `true`,
205
+ * given each element and its index: what `xs.findIndex(pred)` compiles to when
206
+ * `pred` takes two parameters.
207
+ *
208
+ * @param xs the array read; it is not changed.
209
+ * @param pred called in order, with the element and its index, until it answers `true`.
210
+ * @returns the first such index, or `-1` when there is none.
211
+ */
212
+ export const findIndexIndexed = <T>(xs: readonly T[], pred: (x: T, i: number) => boolean): number => {
213
+ const n = xs.length
214
+ for (let i: number = 0; i < n && i < xs.length; i++) {
215
+ if (pred(xs[i], i)) {
216
+ return i
217
+ }
218
+ }
219
+ return -1
220
+ }
221
+
222
+ /**
223
+ * A copy of `xs[start..end)` in a new array: what `xs.slice(start, end)`
224
+ * compiles to, with string `slice`'s rule. The range must satisfy
225
+ * `0 <= start <= end <= xs.length`, and anything else panics with
226
+ * `slice out of range: [start, end) of length n` and exits 1, where
227
+ * JavaScript counts a negative end back from the length and clamps the rest.
228
+ * A float bound is truncated toward zero and saturates (NaN to 0, an infinity
229
+ * to the `i64` limit), natively and under Node alike.
230
+ *
231
+ * @param xs the array copied from; it is not changed.
232
+ * @param start the first index copied.
233
+ * @param end the index after the last copied.
234
+ * @returns a new array of `end - start` elements.
235
+ */
236
+ export const slice = <T>(xs: readonly T[], start: number, end: number): T[] => {
237
+ const from = toI64(start)
238
+ const to = toI64(end)
239
+ const len = toI64(xs.length)
240
+ if (from < 0 || from > to || to > len) {
241
+ panic(`slice out of range: [${from}, ${to}) of length ${len}`)
242
+ }
243
+ const out: T[] = []
244
+ for (let i: i32 = toI32(from); i >= 0 && i < toI32(to) && i < toI32(xs.length); i++) {
245
+ out.push(xs[i])
246
+ }
247
+ return out
248
+ }
249
+
250
+ /**
251
+ * A copy of `xs[start..)`: what `xs.slice(start)` compiles to. `start` must
252
+ * satisfy `0 <= start <= xs.length`, or the call panics as `slice` does.
253
+ *
254
+ * @param xs the array copied from; it is not changed.
255
+ * @param start the first index copied.
256
+ * @returns a new array of the elements from `start` on.
257
+ */
258
+ export const sliceFrom = <T>(xs: readonly T[], start: number): T[] => slice(xs, start, xs.length)
259
+
260
+ /**
261
+ * A copy of the whole of `xs`: what `xs.slice()` compiles to.
262
+ *
263
+ * @param xs the array copied; it is not changed.
264
+ * @returns a new array with the same elements.
265
+ */
266
+ export const sliceAll = <T>(xs: readonly T[]): T[] => slice(xs, 0, xs.length)
@@ -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
+ }