@amritk/nish-aarch64-darwin 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/INSTALL.md +28 -16
- package/bin/nish +0 -0
- package/docs/AI.md +32 -10
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/nish.d.ts +344 -48
- package/runtime/nish.h +29 -7
- package/runtime/runtime-async.c +228 -45
- 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 +3 -3
- package/runtime/runtime.c +50 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +86 -28
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +2 -0
- package/std/array.ts +266 -0
- package/std/host/text.ts +86 -0
- 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 +691 -129
- package/std/net/handler.ts +12 -0
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +18 -4
- package/std/net/http1-server.ts +13 -3
- package/std/net/postgres-async.ts +1005 -0
- package/std/net/postgres.ts +7 -4
- package/std/net/serve.ts +763 -0
- package/std/text.ts +231 -5
package/INSTALL.md
CHANGED
|
@@ -56,8 +56,12 @@ the Homebrew LLVM is needed for the `wasm`, `wasm-host` and `wasi` profiles (`wa
|
|
|
56
56
|
|
|
57
57
|
`wasm-opt` (binaryen: `apt-get install binaryen`, `brew install binaryen`) is
|
|
58
58
|
optional. When it is on `PATH`, `scripts/build.sh --profile wasm-host` runs
|
|
59
|
-
`wasm-opt -Oz` on the module, and the build line says
|
|
60
|
-
it
|
|
59
|
+
`wasm-opt -Oz --strip-target-features` on the module, and the build line says
|
|
60
|
+
whether it ran: `add.wasm` goes from 102 bytes to 74. Without it the module
|
|
61
|
+
is complete and works the same. clang 18 also runs a `wasm-opt` it finds,
|
|
62
|
+
after any optimising wasm link, and keeps a `target_features` section for it
|
|
63
|
+
that build.sh's pass then drops. `NISH_WASM_OPT=0` links with neither pass,
|
|
64
|
+
even with binaryen installed: the bytes a machine without it builds.
|
|
61
65
|
|
|
62
66
|
<a id="wasi-optional-for---profile-wasi" style="scroll-margin-top: 65px"></a>
|
|
63
67
|
|
|
@@ -261,11 +265,11 @@ routes are
|
|
|
261
265
|
# in the glibc container, where build/nish runs
|
|
262
266
|
build/nish app.ts -o app.ll
|
|
263
267
|
# on the musl host, with its own clang and the runtime from this repository
|
|
264
|
-
clang app.ll runtime/runtime.c runtime/runtime-os.c runtime/runtime-parallel.c \
|
|
268
|
+
clang app.ll runtime/runtime.c runtime/runtime-text.c runtime/runtime-os.c runtime/runtime-parallel.c \
|
|
265
269
|
runtime/runtime-cross.c runtime/runtime-host.c runtime/runtime-net.c \
|
|
266
270
|
runtime/runtime-simd.c runtime/runtime-aes.c runtime/runtime-sha256.c runtime/runtime-chacha20.c \
|
|
267
271
|
runtime/runtime-term.c runtime/runtime-bytes.c runtime/runtime-checksum.c runtime/runtime-sandbox.c \
|
|
268
|
-
runtime/runtime-shm.c runtime/runtime-region.c -lm -o app
|
|
272
|
+
runtime/runtime-shm.c runtime/runtime-region.c -ffp-contract=off -lm -o app
|
|
269
273
|
```
|
|
270
274
|
|
|
271
275
|
`--link`ing inside the container is the mistake to avoid: it shells out to the
|
|
@@ -374,7 +378,8 @@ release exists, take its version from
|
|
|
374
378
|
Unpack it and run `bin/nish` from wherever you like; put that on `PATH` if you
|
|
375
379
|
want it there. Keep the directory intact rather than moving the binary out of
|
|
376
380
|
it: `--link` runs `scripts/build.sh` and compiles the C runtime
|
|
377
|
-
(`runtime/runtime.c
|
|
381
|
+
(`runtime/runtime.c`, `runtime/runtime-text.c`, the strings and number
|
|
382
|
+
formatting, `runtime/runtime-os.c`, the system-call half, and the rest), and the
|
|
378
383
|
compiler finds all of them relative to its own location — `bin/nish` alone in a
|
|
379
384
|
directory can still emit IR with `-o`, but `--link` will tell you it cannot
|
|
380
385
|
find `scripts/build.sh`.
|
|
@@ -401,7 +406,7 @@ it into `build/seed/`; `NISH_BOOTSTRAP=<path>` names one you already have and
|
|
|
401
406
|
skips the download. §2a has what the build does with it.
|
|
402
407
|
|
|
403
408
|
The published package ships `bin/` (the `nish` command, which is a launcher),
|
|
404
|
-
`runtime/` (the C runtime —
|
|
409
|
+
`runtime/` (the C runtime — seventeen translation units, the wasm subset and their
|
|
405
410
|
header — and `runtime/sqlite/`, SQLite's vendored source),
|
|
406
411
|
`scripts/build.sh` (the link pipeline), `std/` (the standard library) and
|
|
407
412
|
`docs/AI.md` (the rules card). The
|
|
@@ -416,19 +421,24 @@ any working directory — and the binary runs from inside its own platform
|
|
|
416
421
|
package for exactly that reason.
|
|
417
422
|
|
|
418
423
|
Building the IR yourself rather than through `--link` means naming the runtime
|
|
419
|
-
on the `clang` line, and it is
|
|
424
|
+
on the `clang` line, and it is seventeen files:
|
|
420
425
|
|
|
421
426
|
```bash
|
|
422
|
-
clang app.ll runtime/runtime.c runtime/runtime-os.c runtime/runtime-parallel.c \
|
|
427
|
+
clang app.ll runtime/runtime.c runtime/runtime-text.c runtime/runtime-os.c runtime/runtime-parallel.c \
|
|
423
428
|
runtime/runtime-cross.c runtime/runtime-host.c runtime/runtime-net.c \
|
|
424
429
|
runtime/runtime-simd.c runtime/runtime-aes.c runtime/runtime-sha256.c runtime/runtime-chacha20.c \
|
|
425
430
|
runtime/runtime-term.c runtime/runtime-bytes.c runtime/runtime-checksum.c runtime/runtime-sandbox.c \
|
|
426
|
-
runtime/runtime-shm.c runtime/runtime-region.c -lm -o app
|
|
431
|
+
runtime/runtime-shm.c runtime/runtime-region.c -ffp-contract=off -lm -o app
|
|
427
432
|
```
|
|
428
433
|
|
|
429
|
-
`runtime.c` is the part every program touches — the arena,
|
|
430
|
-
|
|
431
|
-
files, directories, subprocesses,
|
|
434
|
+
`runtime.c` is the part every program touches — the arena, arrays, the
|
|
435
|
+
panics — `runtime-text.c` holds strings and number formatting, and
|
|
436
|
+
`runtime-os.c` wraps the system calls: files, directories, subprocesses,
|
|
437
|
+
`getenv`, the monotonic clock.
|
|
438
|
+
|
|
439
|
+
`-ffp-contract=off` keeps clang from fusing a C `a*b+c` into one rounding
|
|
440
|
+
where the CPU has a fused multiply-add (every arm64, x86-64 at v3): the runtime
|
|
441
|
+
then prints the same float on every target, as `scripts/build.sh` compiles it.
|
|
432
442
|
|
|
433
443
|
`runtime-parallel.c` divides work across threads for `nish/threads` (a program
|
|
434
444
|
built with `--threads`, as every importer of `nish/threads` is, also needs
|
|
@@ -449,12 +459,14 @@ kernels of `nish/compress/checksum`, `runtime-sandbox.c` the confinement a
|
|
|
449
459
|
`nish/sync/shm`. They are separate so that each carries its own measured size
|
|
450
460
|
ceiling
|
|
451
461
|
([docs/wp7-runtime.md](https://github.com/amritk/nish/blob/main/docs/wp7-runtime.md));
|
|
452
|
-
|
|
453
|
-
`runtime.c`
|
|
454
|
-
|
|
462
|
+
the core is three files that call each other — `runtime-os.c` builds strings
|
|
463
|
+
with `runtime-text.c`'s `nish_str_new` — and calls into none of the others, so a
|
|
464
|
+
line that names only `runtime.c`, `runtime-text.c` and `runtime-os.c` links a
|
|
465
|
+
program that uses none of the other fourteen. Leave out `runtime-text.c` and it
|
|
466
|
+
does not link at all: `nish_str_new` is undefined.
|
|
455
467
|
|
|
456
468
|
`scripts/build.sh` compiles the other
|
|
457
|
-
|
|
469
|
+
sixteen beside any
|
|
458
470
|
`runtime.c` it is handed, so a build that goes through it — every `--link`,
|
|
459
471
|
and every `--profile` recipe in these documents — needs to name only the one.
|
|
460
472
|
|
package/bin/nish
CHANGED
|
Binary file
|
package/docs/AI.md
CHANGED
|
@@ -49,7 +49,7 @@ stdout, nothing on stderr, every field 1-based with `endLine`/`endColumn`
|
|
|
49
49
|
exclusive:
|
|
50
50
|
|
|
51
51
|
```json
|
|
52
|
-
{"file":"p.ts","line":3,"column":17,"endLine":3,"endColumn":
|
|
52
|
+
{"file":"p.ts","line":3,"column":17,"endLine":3,"endColumn":23,"severity":"error","code":"NL2249","message":"Unknown method `reduce` on i32[] (supported: push, pop, indexOf, join, set, fill, sort, map, filter, some, every, findIndex, slice)"}
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
- **Key on `code`, never on `message`.** A code is a promise: `NL2249` means
|
|
@@ -165,7 +165,8 @@ rejects. This table is the highest-value part of the page.
|
|
|
165
165
|
| `x == y` | `Loose equality is forbidden` | `x === y` |
|
|
166
166
|
| `throw new Error(m)` | `` `throw` is forbidden `` | `return Err(m)`, or `panic(m)` to end the process |
|
|
167
167
|
| `try { … } catch { … }` | `` `try`/`catch`/`finally` is forbidden `` | `Result<T, E>` and `isErr()` |
|
|
168
|
-
| `xs.
|
|
168
|
+
| `xs.reduce(f)`, `.forEach`, `.shift` | `` Unknown method `reduce` on i32[] (supported: push, pop, indexOf, join, set, fill, sort, map, filter, some, every, findIndex, slice) `` | a `for` loop; `map`, `filter`, `some`, `every`, `findIndex` and `slice` work, with a one- or two-parameter callback |
|
|
169
|
+
| `xs.find(f)` | `` `find` is not supported: TypeScript types its answer `T \| undefined` … `` (NL2697) | `const i = xs.findIndex(f); if (i >= 0) { ... xs[i] ... }` |
|
|
169
170
|
| `s.toUpperCase()`, `s.split()`, `s.trim()`, `s.replace()` | `` Unknown method … on string `` | index bytes with `charCodeAt` / `substring` |
|
|
170
171
|
| `a?.b`, `a ?? b` | `?.` only in a chain that ends in `??`; `??` only on a `T \| null`, a chain or `m.get(k)` | `a?.b ?? d`, `a ?? d`, or `if (a !== null)` first |
|
|
171
172
|
| `x as T`, `<T>x`, `x satisfies T` | `Unsupported expression in Phase 1: AsExpression` (to `any` or `unknown`: `` Type assertion to `any` is forbidden ``); `x!` is a syntax error | there are no casts; `implements` is the only widening |
|
|
@@ -861,8 +862,9 @@ export const main = (): i32 => {
|
|
|
861
862
|
(`` The arrow reads `k`, a `let` of the function it is written in: ... bind the value to a `const` before the call ``),
|
|
862
863
|
read `this` (bind `const self = this`), or read a function-typed parameter
|
|
863
864
|
of its function. An arrow inside another arrow, or given to a method, a
|
|
864
|
-
constructor, `expose` or `exposeWith`, captures nothing;
|
|
865
|
-
|
|
865
|
+
constructor, `expose` or `exposeWith`, captures nothing; the callbacks of
|
|
866
|
+
the array methods `sort`, `map`, `filter`, `some`, `every` and `findIndex`
|
|
867
|
+
are the exception, and capture as an arrow argument does.
|
|
866
868
|
- **The argument's type must match exactly**: `(x: i32) => i64` for a
|
|
867
869
|
`(x: i32) => i32` parameter is refused. A generic function cannot be passed
|
|
868
870
|
by name — write `(x) => identity(x)`.
|
|
@@ -2245,7 +2247,10 @@ answers `a` — the same order Node gives; the comparator is required (`a.sort()
|
|
|
2245
2247
|
is refused, since JavaScript would compare strings), a readonly array and
|
|
2246
2248
|
`process.argv` are refused, `a - b` panics on `i32` values a full range apart
|
|
2247
2249
|
(compare with `a > b ? 1 : a < b ? -1 : 0` there), and the comparator may read the caller's `const` locals and parameters but not
|
|
2248
|
-
a `let`, as any arrow argument may.
|
|
2250
|
+
a `let`, as any arrow argument may. `a.map(f)`, `a.filter(f)`, `a.some(f)`,
|
|
2251
|
+
`a.every(f)`, `a.findIndex(f)` and `a.slice(start, end)` answer as in JavaScript
|
|
2252
|
+
(a callback takes the element, and the index if it names two parameters). That is
|
|
2253
|
+
every array method there is. A
|
|
2249
2254
|
window into a buffer is `(buf, offset, length)`; there is no view type.
|
|
2250
2255
|
|
|
2251
2256
|
**Strings.** `s.length` (**bytes**), `s.charCodeAt(i)` (the byte, bounds-checked),
|
|
@@ -2756,15 +2761,16 @@ the module (NL4015).
|
|
|
2756
2761
|
|
|
2757
2762
|
## Recipes for what is missing
|
|
2758
2763
|
|
|
2759
|
-
**Iterate and
|
|
2764
|
+
**Iterate and accumulate** — there is no `reduce` or `forEach` (`map`, `filter`, `some`, `every`, `findIndex` and `slice` work); fold in a loop:
|
|
2760
2765
|
|
|
2761
2766
|
```ts nish:ok-body
|
|
2762
2767
|
const xs: i32[] = [1, 2, 3];
|
|
2763
|
-
const doubled
|
|
2764
|
-
|
|
2765
|
-
|
|
2768
|
+
const doubled = xs.map((x) => x * 2);
|
|
2769
|
+
let sum = 0;
|
|
2770
|
+
for (const x of doubled) {
|
|
2771
|
+
sum += x;
|
|
2766
2772
|
}
|
|
2767
|
-
console.log(`${
|
|
2773
|
+
console.log(`${sum}`);
|
|
2768
2774
|
```
|
|
2769
2775
|
|
|
2770
2776
|
**Convert a number to a string** — there is no `toString` and no `String()`:
|
|
@@ -2908,6 +2914,22 @@ event waits for `end`. The batcher reads no clock: when to take is the loop's ch
|
|
|
2908
2914
|
guide's [PostgreSQL page](guide/using-nish/postgres.md#batching-keyed-reads)
|
|
2909
2915
|
its walk-through.)
|
|
2910
2916
|
|
|
2917
|
+
**Serve HTTP from an `async` program** — there is no `app.get(path, cb)`:
|
|
2918
|
+
`serve(port, router, limits, handle)` from `nish/net/serve` runs the loop and
|
|
2919
|
+
calls your one `async function handle(req: Request): Promise<Response>` in a
|
|
2920
|
+
task of its own per request, so `handle` may `await sleep(ms)` or a socket
|
|
2921
|
+
while others are answered. Add routes with `router.add(method, pattern)` and
|
|
2922
|
+
`switch (req.route)` on the numbers it answered (0, 1, 2 ...); read
|
|
2923
|
+
`req.param(k)` by index, `req.paramNumber(k)`, `req.query(name)`,
|
|
2924
|
+
`req.header(lowercaseName)`, `req.text()`; answer with
|
|
2925
|
+
`new Response(status).text(s)`, `.json(s)` or `.bytes(type, buf, off, len)`,
|
|
2926
|
+
and `refuse(req)` for 404 and 405. `ServeLimits` caps connections, `inFlight`
|
|
2927
|
+
(503 past it), `headerBytes` (431), `bodyBytes` (413) and `responseBytes`
|
|
2928
|
+
(500), and `headTimeout` and `idleTimeout` close a slow or idle connection; pipelined answers keep request order. `--emit-ts` refuses it (NL4017).
|
|
2929
|
+
(No block, as for `nish/text`; `tests/link/net_serve` is its program, and the
|
|
2930
|
+
user guide's [async server page](guide/using-nish/async-server.md) its
|
|
2931
|
+
walk-through.)
|
|
2932
|
+
|
|
2911
2933
|
**Call another service from a route handler** — there is no `await fetch()`
|
|
2912
2934
|
in a handler (an `async` task does not wait on a socket yet) and no event-loop
|
|
2913
2935
|
callback: `nish/net/upstream`'s `Upstream(h,
|
package/package.json
CHANGED
package/runtime/core/arrays.d.ts
CHANGED
|
@@ -11,6 +11,10 @@ export declare function storeIdx<T>(a: T[], i: number | bigint, v: T): T;
|
|
|
11
11
|
export declare function arraySet<T>(dst: T[], src: readonly T[], offset?: number): void;
|
|
12
12
|
/** `dst.set(src, offset)` as `--emit-ts` writes it: a 64-bit offset is a bigint, checked exactly. */
|
|
13
13
|
export declare function arraySetAt<T>(dst: T[], src: readonly T[], offset?: number | bigint): void;
|
|
14
|
+
/** `xs.slice(start, end)` as `--emit-ts` writes it: an out-of-range bound panics, as the native one does. */
|
|
15
|
+
export declare function sliceArray<T>(a: readonly T[], start?: number | bigint, end?: number | bigint): T[];
|
|
16
|
+
/** The answer of `xs.map`, `xs.filter` or `xs.slice` with each record copied, as an array holds it inline. */
|
|
17
|
+
export declare function copyRecords<T>(a: T[]): T[];
|
|
14
18
|
/** `new Array<T>(n)`: `--emit-ts` writes `T` out, so the zero it fills with is not what `T` is read from. */
|
|
15
19
|
export declare function newArray<T>(n: number | bigint, zero: unknown): T[];
|
|
16
20
|
export declare function arenaUsed(): i64;
|
package/runtime/core/arrays.mjs
CHANGED
|
@@ -66,6 +66,39 @@ export function copyIn(v) {
|
|
|
66
66
|
return isRecord(v) ? { ...v } : v;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* `xs.slice(start, end)` as `nish --emit-ts` writes it: the native rule, where
|
|
71
|
+
* the range must satisfy `0 <= start <= end <= xs.length` and anything else
|
|
72
|
+
* panics, not JavaScript's, which counts a negative bound from the end and
|
|
73
|
+
* clamps the rest.
|
|
74
|
+
*/
|
|
75
|
+
export function sliceArray(a, start, end) {
|
|
76
|
+
const from = start === undefined ? 0n : sliceBound(start);
|
|
77
|
+
const to = end === undefined ? BigInt(a.length) : sliceBound(end);
|
|
78
|
+
if (!(from >= 0n && from <= to && to <= BigInt(a.length))) panicSlice(from, to, a.length);
|
|
79
|
+
return a.slice(Number(from), Number(to));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A `slice` bound as the native template converts it to `i64`: a `number` is
|
|
83
|
+
// truncated toward zero and saturates, NaN to 0 and an infinity to the i64
|
|
84
|
+
// limit, so a panic prints the same offsets natively and here.
|
|
85
|
+
function sliceBound(i) {
|
|
86
|
+
if (typeof i === "bigint") return i;
|
|
87
|
+
if (Number.isNaN(i)) return 0n;
|
|
88
|
+
if (i >= 2 ** 63) return (1n << 63n) - 1n;
|
|
89
|
+
if (i <= -(2 ** 63)) return -(1n << 63n);
|
|
90
|
+
return BigInt(Math.trunc(i));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The array `xs.map`, `xs.filter` or `xs.slice` answers, as the native one
|
|
95
|
+
* holds it: each record a fresh copy, as an array holds a record inline.
|
|
96
|
+
*/
|
|
97
|
+
export function copyRecords(a) {
|
|
98
|
+
for (let i = 0; i < a.length; i++) a[i] = copyIn(a[i]);
|
|
99
|
+
return a;
|
|
100
|
+
}
|
|
101
|
+
|
|
69
102
|
/**
|
|
70
103
|
* `a[i] = v` from a generic body: as `setIdx`, but a record is written field
|
|
71
104
|
* by field into the record already in the slot, as an inline element is.
|