nish 0.41.0 → 0.44.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.
Files changed (47) hide show
  1. package/README.md +16 -4
  2. package/bin/nish-skills.js +233 -0
  3. package/docs/AI.md +282 -126
  4. package/llms.txt +4 -0
  5. package/package.json +14 -11
  6. package/runtime/core.d.ts +16 -0
  7. package/runtime/core.mjs +5 -0
  8. package/runtime/nish.d.ts +204 -33
  9. package/runtime/nish.h +19 -3
  10. package/runtime/nish.mjs +5 -1
  11. package/runtime/runtime-cross.c +15 -0
  12. package/runtime/runtime-net.c +876 -10
  13. package/runtime/runtime-region.c +21 -0
  14. package/runtime/runtime-sandbox.c +112 -15
  15. package/runtime/runtime-text.c +11 -0
  16. package/runtime/runtime-uring.c +533 -77
  17. package/runtime/runtime-wasm.c +3 -0
  18. package/runtime/runtime.c +474 -5
  19. package/runtime/sqlite/README.md +16 -3
  20. package/runtime/sqlite/nish-sqlite.c +940 -29
  21. package/scripts/build.sh +125 -29
  22. package/scripts/link-skills.mjs +1039 -0
  23. package/scripts/postinstall.mjs +43 -0
  24. package/skills/writing-nish/SKILL.md +144 -0
  25. package/std/README.md +3 -3
  26. package/std/crypto/base64url.ts +102 -1
  27. package/std/crypto/scram.ts +142 -0
  28. package/std/crypto/sha1.ts +99 -41
  29. package/std/host/ring.ts +1 -1
  30. package/std/host/text.ts +1 -1
  31. package/std/net/fetch.ts +9 -1
  32. package/std/net/handler.ts +79 -15
  33. package/std/net/http-server.ts +2 -3
  34. package/std/net/http1-server.ts +294 -51
  35. package/std/net/http3-server.ts +26 -15
  36. package/std/net/http3.ts +2 -2
  37. package/std/net/oidc.ts +1107 -0
  38. package/std/net/postgres-async.ts +64 -33
  39. package/std/net/postgres.ts +65 -76
  40. package/std/net/quic-conn-ack.ts +57 -7
  41. package/std/net/quic-frame.ts +104 -2
  42. package/std/net/quic-listener.ts +9 -2
  43. package/std/net/quic.ts +16 -1
  44. package/std/net/serve.ts +10 -8
  45. package/std/net/websocket.ts +188 -49
  46. package/std/sqlite.ts +325 -0
  47. package/std/text.ts +80 -11
package/scripts/build.sh CHANGED
@@ -94,6 +94,18 @@
94
94
  # thread-local global, and ELF will not link that against a non-TLS definition,
95
95
  # so a half-threaded build fails at the link rather than at run time.
96
96
  #
97
+ # Debug arena: `--debug-arena` compiles the runtime with -DNISH_ARENA_DEBUG, so
98
+ # every arena block gets pages of its own, never reused, and a release turns
99
+ # them PROT_NONE: a stale reference past its scope faults at the read and the
100
+ # runtime names it (docs/security/runtime.md, "The debug arena"). The IR is the
101
+ # same either way; `nish --debug-arena` passes it through. Native profiles only.
102
+ #
103
+ # Compact-class range (WP48 stage 1): `--arena-range` compiles the runtime with
104
+ # -DNISH_ARENA_RANGE, which carves arena chunks of 2 MiB or less from one
105
+ # 15 GiB reservation (at [1 GiB, 16 GiB) on Linux) so a compact class's 32-bit
106
+ # reference can name them. There is no user flag: nish passes it when the
107
+ # program has a compact class (docs/wp48-ref32-classes.md). Native profiles only.
108
+ #
97
109
  # Debug info (WP10): `-g` compiles every input with -g and skips the strip
98
110
  # step of the speed/size/napi profiles, so the DWARF that `nish -g`
99
111
  # put in the .ll (line table, variables) reaches the binary. `nish
@@ -113,11 +125,13 @@
113
125
  # passes it through.
114
126
  #
115
127
  # SQLite: an input .ll that declares an `sqlite3_*` or `nishsqlite_*` function
116
- # also compiles and links every runtime/sqlite/*.c, the vendored amalgamation
117
- # (runtime/sqlite/README.md). Any other program compiles none of it. sqlite3.c
118
- # is compiled to native code at the profile's level, even where the profile
119
- # compiles to LTO bitcode. The options are most of SQLite's recommended set
120
- # (see the defaults below), with THREADSAFE=0 (=2 under --threads).
128
+ # also compiles and links runtime/sqlite/nish-sqlite.c, the adapter, which
129
+ # includes sqlite3.c, the vendored amalgamation (runtime/sqlite/README.md), so
130
+ # it can call SQLite's internal B-tree functions (docs/sqlite-cursor.md): every
131
+ # *.c there but sqlite3.c is a unit. Any other program compiles none of it.
132
+ # The unit is compiled to native code at the profile's level, even where the
133
+ # profile compiles to LTO bitcode. The options are most of SQLite's recommended
134
+ # set (see the defaults below), with THREADSAFE=0 (=2 under --threads).
121
135
  # NISH_SQLITE_CFLAGS is appended to them: its first line, split on whitespace
122
136
  # with no quoting, and a relative -I in it names a directory below
123
137
  # runtime/sqlite/, where each unit is compiled. A -D or -U in it for one of
@@ -138,7 +152,7 @@
138
152
  # runtime.c input, after compiling every other input
139
153
  # with its own $CC -c, all at once. The link line is
140
154
  # the one the profile always uses.
141
- # --sqlite-objects <dir> compile runtime/sqlite/*.c alone, into
155
+ # --sqlite-objects <dir> compile runtime/sqlite/'s units alone, into
142
156
  # <dir>/<unit>.o, as a link of a program that calls
143
157
  # SQLite would, and link nothing. Takes no inputs and
144
158
  # no -o, and prints nothing on success.
@@ -151,6 +165,19 @@
151
165
  # not, so the caller keeps every object it compiled
152
166
  # (`nish run`'s module cache, src/build-cache.ts).
153
167
  # The inputs' file names must differ.
168
+ # --link-lib <name> debug, speed and size only: link the C library
169
+ # <name> (-l<name>, after the inputs and before -lm);
170
+ # <name> is [A-Za-z0-9_][A-Za-z0-9_.+-]*, so it is
171
+ # never a flag. A package's `nish.link.libraries`
172
+ # (WP66 S7, docs/security/package-link.md).
173
+ # --link-source <file.c> debug, speed and size only: compile the C file, an
174
+ # absolute path, with the profile's flags and link
175
+ # it with the program. A package's `nish.link.sources`.
176
+ # --link-depends <file.c> debug, speed and size only: print, as a make rule
177
+ # (clang -M), the files it lists for the compile of
178
+ # that source with this profile's flags, and link
179
+ # nothing.
180
+ # `nish run` keys its cache on them.
154
181
  #
155
182
  # Works on Linux (clang + lld preferred, GNU ld tolerated) and macOS (Apple ld64
156
183
  # or Homebrew llvm). Set CC to pick a compiler (default: clang on PATH).
@@ -162,6 +189,8 @@ inputs=()
162
189
  pgo=() # -fprofile-generate / -fprofile-use=<file>
163
190
  debug=0 # -g: keep DWARF (nish -g emits it in the IR; runtime.c gets it here)
164
191
  threads=0 # --threads: -DNISH_THREADS, the thread-local arena (WP20 T0)
192
+ debug_arena=0 # --debug-arena: -DNISH_ARENA_DEBUG, the arena that traps a stale reference
193
+ arena_range=0 # --arena-range: -DNISH_ARENA_RANGE, the compact-class range (WP48)
165
194
  objects_to="" # --runtime-objects <dir>: compile the runtime alone, into <dir>
166
195
  objects_from="" # --runtime-from <dir>: link against the runtime compiled there
167
196
  cpu="" # --cpu x86-64-v3: the one CPU level above the baseline (WP38 S2)
@@ -171,12 +200,17 @@ modules_at="" # --module-objects <dir>: each X.ll's obj
171
200
  memory_mib="" # --memory <MiB>: wasm-host's fixed memory (WP50 stage 1)
172
201
  wasm_opt="" # what the build line says of wasm-opt; set only by wasm-host
173
202
  inline_to="" # --inline <file.mjs>: wasm-host's bytes as an ES module
203
+ link_libs=() # --link-lib <name>: -l<name> on the link line (WP66 S7)
204
+ link_sources=() # --link-source <file.c>: a package's own C, compiled into the link
205
+ link_depends="" # --link-depends <file.c>: print what its compile reads, then stop
174
206
  while [ $# -gt 0 ]; do
175
207
  case "$1" in
176
208
  -o) out="$2"; shift 2 ;;
177
209
  --profile) profile="$2"; shift 2 ;;
178
210
  -g) debug=1; shift ;;
179
211
  --threads) threads=1; shift ;;
212
+ --debug-arena) debug_arena=1; shift ;;
213
+ --arena-range) arena_range=1; shift ;;
180
214
  --cpu)
181
215
  [ -n "${2-}" ] || { echo "error: --cpu needs a level; the one taken is x86-64-v3" >&2; exit 2; }
182
216
  cpu="$2"; shift 2 ;;
@@ -191,6 +225,21 @@ while [ $# -gt 0 ]; do
191
225
  --inline)
192
226
  [ -n "${2-}" ] || { echo "error: --inline needs a file to write" >&2; exit 2; }
193
227
  inline_to="$2"; shift 2 ;;
228
+ --link-lib)
229
+ [[ "${2-}" =~ ^[A-Za-z0-9_][A-Za-z0-9_.+-]*$ ]] || { echo "error: --link-lib takes a library name of letters, digits, _ . + and -, not '${2-}'" >&2; exit 2; }
230
+ link_libs+=("-l$2"); shift 2 ;;
231
+ --link-source)
232
+ case "${2-}" in
233
+ /*.c) [ -f "$2" ] || { echo "error: --link-source: $2 is not a file" >&2; exit 2; } ;;
234
+ *) echo "error: --link-source takes the absolute path of a .c file, not '${2-}'" >&2; exit 2 ;;
235
+ esac
236
+ link_sources+=("$2"); shift 2 ;;
237
+ --link-depends)
238
+ case "${2-}" in
239
+ /*.c) [ -f "$2" ] || { echo "error: --link-depends: $2 is not a file" >&2; exit 2; } ;;
240
+ *) echo "error: --link-depends takes the absolute path of a .c file, not '${2-}'" >&2; exit 2 ;;
241
+ esac
242
+ link_depends="$2"; shift 2 ;;
194
243
  --pgo-generate) pgo=(-fprofile-generate); shift ;;
195
244
  --pgo-use)
196
245
  [ -f "$2" ] || { echo "error: --pgo-use: profile '$2' not found (run the instrumented binary, then llvm-profdata merge)" >&2; exit 2; }
@@ -199,13 +248,24 @@ while [ $# -gt 0 ]; do
199
248
  *) inputs+=("$1"); shift ;;
200
249
  esac
201
250
  done
202
- [ ${#inputs[@]} -gt 0 ] || [ -n "$sqlite_to" ] || { echo "error: no input files" >&2; exit 2; }
251
+ [ ${#inputs[@]} -gt 0 ] || [ -n "$sqlite_to" ] || [ -n "$link_depends" ] || { echo "error: no input files" >&2; exit 2; }
203
252
  if [ -n "$cpu" ]; then
253
+ [ "$cpu" != native ] || { echo "error: --cpu native is refused: under clang 18 the constant-time check (CT-16) refuses the assembly made for some CPUs native names (znver2 to znver4, sapphirerapids); --cpu takes x86-64-v3" >&2; exit 2; }
204
254
  [ "$cpu" = x86-64-v3 ] || { echo "error: --cpu takes x86-64-v3, not '$cpu'" >&2; exit 2; }
205
255
  case "$profile" in
206
256
  wasm|wasm-host|wasi) echo "error: --cpu x86-64-v3 is an x86-64 level, so the $profile profile does not take it" >&2; exit 2 ;;
207
257
  esac
208
258
  fi
259
+ if [ "$debug_arena" = 1 ]; then
260
+ case "$profile" in
261
+ wasm|wasm-host|wasi|napi) echo "error: --debug-arena maps pages and catches SIGSEGV, so the $profile profile does not take it" >&2; exit 2 ;;
262
+ esac
263
+ fi
264
+ if [ "$arena_range" = 1 ]; then
265
+ case "$profile" in
266
+ wasm|wasm-host|wasi|napi) echo "error: --arena-range reserves a fixed address range, so the $profile profile does not take it" >&2; exit 2 ;;
267
+ esac
268
+ fi
209
269
  if [ -n "$memory_mib" ] || [ -n "$inline_to" ]; then
210
270
  [ "$profile" = wasm-host ] || { echo "error: --memory and --inline take the wasm-host profile, not '$profile'" >&2; exit 2; }
211
271
  fi
@@ -271,7 +331,7 @@ if [ -n "$sqlite_to" ] || [ -n "$sqlite_from" ]; then
271
331
  [ -z "$sqlite_to" ] || [ -z "$sqlite_from" ] || { echo "error: --sqlite-objects and --sqlite-from are two different builds" >&2; exit 2; }
272
332
  [ -z "$sqlite_to" ] || [ -z "$objects_to$objects_from" ] || { echo "error: --sqlite-objects compiles runtime/sqlite/ alone, without --runtime-objects or --runtime-from" >&2; exit 2; }
273
333
  fi
274
- [ -n "$out" ] || [ -n "$objects_to" ] || [ -n "$sqlite_to" ] || { echo "error: -o <out> is required" >&2; exit 2; }
334
+ [ -n "$out" ] || [ -n "$objects_to" ] || [ -n "$sqlite_to" ] || [ -n "$link_depends" ] || { echo "error: -o <out> is required" >&2; exit 2; }
275
335
 
276
336
  # The runtime is eighteen translation units, and a caller names one: whoever passes
277
337
  # <dir>/runtime.c gets <dir>/runtime-text.c, <dir>/runtime-os.c, <dir>/runtime-parallel.c,
@@ -311,8 +371,8 @@ done
311
371
 
312
372
  # SQLite (runtime/sqlite/) is an optional unit, and the IR selects it. A module
313
373
  # that declares a function of SQLite's C API (`sqlite3_*`) or of the adapter
314
- # beside it (`nishsqlite_*`) gets every runtime/sqlite/*.c compiled and
315
- # linked. Any other program compiles none of it, so its link line and binary
374
+ # beside it (`nishsqlite_*`) gets runtime/sqlite/'s units (`sqlite_units`)
375
+ # compiled and linked. Any other program compiles none of it, so its link line and binary
316
376
  # stay as they were. The test reads `declare` lines, and only before the first
317
377
  # `"` on one, rather than any `@sqlite3_` in the file, because a string
318
378
  # constant or a quoted name may hold one. It is one rule with `callsSqlite` in
@@ -519,6 +579,8 @@ if [ "$threads" = 1 ]; then
519
579
  common+=(-DNISH_THREADS=1 -ftls-model=initial-exec -pthread)
520
580
  tls=(-DNISH_THREADS=1 -ftls-model=initial-exec)
521
581
  fi
582
+ if [ "$debug_arena" = 1 ]; then common+=(-DNISH_ARENA_DEBUG=1); fi
583
+ if [ "$arena_range" = 1 ]; then common+=(-DNISH_ARENA_RANGE=1); fi
522
584
 
523
585
  # What the speed and size profiles compile every input with, beside `common`.
524
586
  # One list per profile, because the runtime cache below compiles with exactly
@@ -540,6 +602,20 @@ case "$profile" in
540
602
  -fno-asynchronous-unwind-tables -fno-unwind-tables ${elf[@]+"${elf[@]}"}) ;;
541
603
  esac
542
604
 
605
+ # --link-depends: the files clang -M lists for a package source's compile,
606
+ # every header and every #included file of any name, with the flags the link
607
+ # line compiles that source with (`common` and the profile's `optimise`), so
608
+ # `nish run` keys those rather than guessing at them by name. clang -M does
609
+ # not list a file an assembler `.incbin` reads, nor a header that would shadow
610
+ # a listed one if it appeared later (docs/security/package-link.md, PL-6, PL-10).
611
+ if [ -n "$link_depends" ]; then
612
+ case "$profile" in
613
+ debug) exec "$CC" "${common[@]}" -Wno-unused-command-line-argument -M "$link_depends" ;;
614
+ speed|size) exec "$CC" "${common[@]}" "${optimise[@]}" -Wno-unused-command-line-argument -M "$link_depends" ;;
615
+ *) echo "error: --link-depends takes the debug, speed and size profiles, not '$profile'" >&2; exit 2 ;;
616
+ esac
617
+ fi
618
+
543
619
  # The speed link splits full LTO's code generation into one partition per core,
544
620
  # run in parallel: lld's --lto-partitions, after the whole program is optimised
545
621
  # as one module, so inlining does not change. But a partition boundary promotes
@@ -646,29 +722,40 @@ for d in -DSQLITE_DQS=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHR
646
722
  done
647
723
  sqlite_defs+=(${sqlite_extra[@]+"${sqlite_extra[@]}"})
648
724
 
649
- # Compiles every runtime/sqlite/*.c with the flags given, all at once, into
650
- # $1/<unit>.o. Each unit is compiled from inside runtime/sqlite/ by its bare
651
- # name, as the runtime is, so -g's DWARF names no directory the link was
652
- # started in. sqlite3.c, the 9.6 MB amalgamation, is compiled to native code
653
- # even where the profile compiles to LTO bitcode. As bitcode, every link of a
654
- # program that uses it would generate its code again: measured on a program
655
- # that opens a database and runs a query, that is 10.6 s a link at -O3,
656
- # against 0.04 s for the native object. The adapter beside it stays LTO, and
657
- # it is what inlines into the program. So `-flto` comes off for that one unit
658
- # alone.
725
+ # The units of runtime/sqlite/: every *.c but sqlite3.c, the amalgamation,
726
+ # which nish-sqlite.c compiles by including it. SQLite's B-tree and record
727
+ # functions are static there, and the cursor adapter calls them, so the two
728
+ # must be one translation unit (docs/sqlite-cursor.md, "The build decision").
729
+ # One name a line; callers read them with `while read`, so a name is never
730
+ # split on spaces.
731
+ sqlite_unit_names() {
732
+ local unit
733
+ for unit in "$sqlite_dir"/*.c; do
734
+ unit=${unit##*/}
735
+ [ "$unit" = sqlite3.c ] || printf '%s\n' "$unit"
736
+ done
737
+ }
738
+ # Compiles every unit of runtime/sqlite/ with the flags given, all at once,
739
+ # into $1/<unit>.o. Each unit is compiled from inside runtime/sqlite/ by its
740
+ # bare name, as the runtime is, so -g's DWARF names no directory the link was
741
+ # started in. Each is compiled to native code even where the profile compiles
742
+ # to LTO bitcode, because nish-sqlite.c holds the 9.6 MB amalgamation. As
743
+ # bitcode, every link of a program that uses it would generate its code
744
+ # again: measured on a program that opens a database and runs a query, that
745
+ # is 10.6 s a link at -O3, against 0.04 s for the native object. So `-flto`
746
+ # comes off.
659
747
  sqlite_units() {
660
748
  local to=$1 unit flag
661
749
  local -a flags
662
750
  shift
663
- for unit in "$sqlite_dir"/*.c; do
664
- unit=${unit##*/}
751
+ while IFS= read -r unit; do
665
752
  flags=()
666
753
  for flag in "$@"; do
667
- case "$unit:$flag" in sqlite3.c:-flto) ;; *) flags+=("$flag") ;; esac
754
+ case "$flag" in -flto) ;; *) flags+=("$flag") ;; esac
668
755
  done
669
756
  (cd -- "$sqlite_dir" && exec "$CC" "${flags[@]}" "${sqlite_defs[@]}" -I. -c "$unit" -o "$to/${unit%.c}.o") &
670
757
  pids+=("$!")
671
- done
758
+ done < <(sqlite_unit_names)
672
759
  join_jobs || { echo "error: runtime/sqlite/ did not compile" >&2; exit 1; }
673
760
  }
674
761
  # Adds the objects of runtime/sqlite/ to this link, in the order of the units.
@@ -684,11 +771,10 @@ sqlite_objects() {
684
771
  mkdir -p -- "$from"
685
772
  sqlite_units "$from" "$@"
686
773
  fi
687
- for unit in "$sqlite_dir"/*.c; do
688
- unit=${unit##*/}
774
+ while IFS= read -r unit; do
689
775
  [ -f "$from/${unit%.c}.o" ] || { echo "error: --sqlite-from: $from/${unit%.c}.o is missing" >&2; exit 2; }
690
776
  inputs+=("$from/${unit%.c}.o")
691
- done
777
+ done < <(sqlite_unit_names)
692
778
  }
693
779
 
694
780
  if [ -n "$sqlite_to" ]; then
@@ -770,12 +856,22 @@ fi
770
856
  # why Linux never noticed. `pgo`, `elf`, `strip_flag` and `libs` are all empty on
771
857
  # ordinary builds, so please do not simplify these back.
772
858
  case "$profile" in debug|speed|size) sqlite_objects "${compile[@]}" ;; esac
859
+ # A package's own C sources join the inputs here, after the runtime's halves
860
+ # and SQLite were decided from the IR alone, so a source can neither stand in
861
+ # for runtime.c nor be taken for one of its siblings. clang compiles each by
862
+ # its .c name on the one link line, with the profile's flags.
863
+ if [ ${#link_libs[@]} -gt 0 ] || [ ${#link_sources[@]} -gt 0 ]; then
864
+ case "$profile" in
865
+ debug|speed|size) inputs+=(${link_sources[@]+"${link_sources[@]}"}) ;;
866
+ *) echo "error: --link-lib and --link-source take the debug, speed and size profiles, not '$profile'" >&2; exit 2 ;;
867
+ esac
868
+ fi
773
869
  case "$profile" in
774
870
  debug)
775
- "$CC" "${common[@]}" "${inputs[@]}" -lm -o "$out" ;;
871
+ "$CC" "${common[@]}" "${inputs[@]}" ${link_libs[@]+"${link_libs[@]}"} -lm -o "$out" ;;
776
872
  speed|size)
777
873
  "$CC" "${common[@]}" "${optimise[@]}" ${lto_parts[@]+"${lto_parts[@]}"} \
778
- "${gc[@]}" ${strip_flag[@]+"${strip_flag[@]}"} "${inputs[@]}" -lm -o "$out" ;;
874
+ "${gc[@]}" ${strip_flag[@]+"${strip_flag[@]}"} "${inputs[@]}" ${link_libs[@]+"${link_libs[@]}"} -lm -o "$out" ;;
779
875
  wasm|wasm-host)
780
876
  # A region's chunks come from malloc, and the freestanding wasm runtime
781
877
  # (runtime-wasm.c) has none, so IR that makes or enters one is refused here