@amritk/nish-x86_64-linux 0.20.0 → 0.22.1

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 (74) hide show
  1. package/INSTALL.md +52 -10
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +2777 -0
  4. package/package.json +2 -1
  5. package/runtime/browser.d.ts +6 -0
  6. package/runtime/browser.mjs +77 -0
  7. package/runtime/core/arrays.d.ts +36 -0
  8. package/runtime/core/arrays.mjs +153 -0
  9. package/runtime/core/checks.mjs +20 -0
  10. package/runtime/core/host.d.ts +80 -0
  11. package/runtime/core/host.mjs +325 -0
  12. package/runtime/core/numbers.d.ts +66 -0
  13. package/runtime/core/numbers.mjs +343 -0
  14. package/runtime/core/result.d.ts +45 -0
  15. package/runtime/core/result.mjs +140 -0
  16. package/runtime/core/strings.d.ts +14 -0
  17. package/runtime/core/strings.mjs +169 -0
  18. package/runtime/core.d.ts +9 -0
  19. package/runtime/core.mjs +55 -0
  20. package/runtime/host.d.ts +6 -0
  21. package/runtime/nish.d.ts +46 -2
  22. package/runtime/nish.h +75 -2
  23. package/runtime/nish.mjs +23 -3
  24. package/runtime/node.mjs +386 -0
  25. package/runtime/runtime-bytes.c +401 -0
  26. package/runtime/runtime-cross.c +1 -1
  27. package/runtime/runtime-host.c +1 -1
  28. package/runtime/runtime-net.c +12 -1
  29. package/runtime/runtime-os.c +1 -1
  30. package/runtime/runtime-parallel.c +1 -1
  31. package/runtime/runtime-simd.c +231 -7
  32. package/runtime/runtime-term.c +504 -0
  33. package/runtime/runtime-wasm.c +0 -11
  34. package/runtime/runtime.c +190 -6
  35. package/runtime/shim.mjs +6 -985
  36. package/runtime/sqlite/LICENSE.md +85 -0
  37. package/runtime/sqlite/README.md +45 -0
  38. package/runtime/sqlite/nish-sqlite.c +359 -0
  39. package/runtime/sqlite/sqlite3.c +271131 -0
  40. package/runtime/sqlite/sqlite3.h +14447 -0
  41. package/scripts/build.sh +311 -59
  42. package/std/LICENSE-fast-float +23 -0
  43. package/std/LICENSE-fdlibm +8 -0
  44. package/std/README.md +32 -12
  45. package/std/base64.ts +116 -0
  46. package/std/bytes.ts +256 -0
  47. package/std/cli.ts +344 -0
  48. package/std/collections.ts +6 -5
  49. package/std/crypto/LICENSE-crypt_blowfish +26 -0
  50. package/std/crypto/base64url.ts +125 -0
  51. package/std/crypto/bcrypt.ts +1557 -0
  52. package/std/crypto/hmac.ts +138 -2
  53. package/std/crypto/x509.ts +11 -122
  54. package/std/json-tokenizer.ts +2998 -57
  55. package/std/json-value.ts +22 -3
  56. package/std/json.ts +87 -8
  57. package/std/math.ts +608 -0
  58. package/std/net/http1-server.ts +282 -40
  59. package/std/net/http1.ts +40 -21
  60. package/std/net/http3-server.ts +50 -12
  61. package/std/net/postgres.ts +2294 -0
  62. package/std/net/prefork.ts +23 -3
  63. package/std/net/quic-listener.ts +20 -4
  64. package/std/random.ts +66 -4
  65. package/std/regex.ts +4274 -0
  66. package/std/sqlite.ts +391 -0
  67. package/std/term-io.ts +978 -0
  68. package/std/term-pixels.ts +874 -0
  69. package/std/term-probe.ts +459 -0
  70. package/std/term-rate.ts +993 -0
  71. package/std/term.ts +2453 -0
  72. package/std/text.ts +90 -7
  73. package/std/yaml-lines.ts +141 -0
  74. package/std/yaml.ts +4675 -0
package/std/sqlite.ts ADDED
@@ -0,0 +1,391 @@
1
+ /**
2
+ * `std/sqlite` — SQLite, the database in a file, from a Nish program.
3
+ *
4
+ * import { openDatabase, OPEN_READWRITE, OPEN_CREATE } from "nish/sqlite"
5
+ *
6
+ * const db = openDatabase("app.db", OPEN_READWRITE | OPEN_CREATE).expect("open")
7
+ * db.exec("CREATE TABLE IF NOT EXISTS t (id INTEGER PRIMARY KEY, name TEXT)").expect("create")
8
+ * const insert = db.prepare("INSERT INTO t (name) VALUES (?1)").expect("prepare")
9
+ * insert.bindText(1, "ada").expect("bind")
10
+ * insert.step().expect("insert")
11
+ * insert.reset().expect("reset")
12
+ *
13
+ * It is the SQLite C library, vendored in `runtime/sqlite/` and linked into a
14
+ * program that imports this module (`scripts/build.sh`), with one small C
15
+ * adapter beside it (`runtime/sqlite/nish-sqlite.c`). A connection and a
16
+ * prepared statement are handles into the adapter's tables, so a `Database`
17
+ * and a `Statement` are an `i32` each, and a handle used after `close` or
18
+ * `finalize` answers `MISUSE` instead of reaching freed memory.
19
+ *
20
+ * **The loop allocates nothing.** Every call a row makes — bind, `step`,
21
+ * `reset`, `columnI64`, `columnF64`, `columnInto` — answers a `Result` of two
22
+ * scalars, which travels as one machine word, and copies nothing but what it
23
+ * is asked for: `columnInto` copies a text or blob column into the caller's
24
+ * `u8[]` with one `memcpy`. `columnText` is the convenience that allocates a
25
+ * string. Building a `SqliteError` allocates, which is why the hot calls answer
26
+ * the bare result code and `error(code)` builds the message only for the
27
+ * caller who wants it.
28
+ *
29
+ * **Results are SQLite's own codes**, extended (`SQLITE_CONSTRAINT_UNIQUE`
30
+ * rather than `SQLITE_CONSTRAINT`), so a program can compare them with the
31
+ * constants below or with SQLite's documentation. `SqliteError.code` is the
32
+ * primary code, the low byte.
33
+ *
34
+ * **Closing is explicit.** `close` finalizes every statement the connection
35
+ * still has, so a statement never outlives its database; `finalize` frees one
36
+ * statement early. A connection and its statements belong to the thread that
37
+ * opened them (the handle tables are thread-local under `--threads`).
38
+ *
39
+ * Calling C is the `ffi` capability (docs/LANGUAGE.md, "Capabilities"), so a
40
+ * program that imports this module is reported with `ffi`, and `--deny ffi`
41
+ * refuses it. The files SQLite opens are opened by C, beneath the `fs`
42
+ * capability's view.
43
+ */
44
+
45
+ // biome-ignore-all lint/style/useNamingConvention: the declarations name C symbols
46
+ // The adapters are `nishsqlite_`, not `nish_sqlite_`: `nish_` names are the
47
+ // runtime's (NL2202). scripts/build.sh links SQLite for a module that declares
48
+ // an `sqlite3_` or a `nishsqlite_` function, as this one does.
49
+ declare function nishsqlite_open(path: string, flags: i32): i32
50
+ declare function nishsqlite_open_errmsg(): string
51
+ declare function nishsqlite_close(h: i32): i32
52
+ declare function nishsqlite_exec(h: i32, sql: string): i32
53
+ declare function nishsqlite_errmsg(h: i32): string
54
+ declare function nishsqlite_last_insert_rowid(h: i32): i64
55
+ declare function nishsqlite_changes(h: i32): i64
56
+ declare function nishsqlite_prepare(h: i32, sql: string): i32
57
+ declare function nishsqlite_stmt_errmsg(h: i32): string
58
+ declare function nishsqlite_finalize(h: i32): i32
59
+ declare function nishsqlite_step(h: i32): i32
60
+ declare function nishsqlite_reset(h: i32): i32
61
+ declare function nishsqlite_clear_bindings(h: i32): i32
62
+ declare function nishsqlite_bind_i64(h: i32, i: i32, v: i64): i32
63
+ declare function nishsqlite_bind_f64(h: i32, i: i32, v: f64): i32
64
+ declare function nishsqlite_bind_null(h: i32, i: i32): i32
65
+ declare function nishsqlite_bind_text(h: i32, i: i32, text: string): i32
66
+ declare function nishsqlite_bind_bytes(
67
+ h: i32,
68
+ i: i32,
69
+ buf: readonly u8[],
70
+ n: u64,
71
+ off: i32,
72
+ len: i32,
73
+ blob: i32
74
+ ): i32
75
+ declare function nishsqlite_column_count(h: i32): i32
76
+ declare function nishsqlite_column_type(h: i32, c: i32): i32
77
+ declare function nishsqlite_column_i64(h: i32, c: i32): i64
78
+ declare function nishsqlite_column_f64(h: i32, c: i32): f64
79
+ declare function nishsqlite_column_bytes(h: i32, c: i32): i32
80
+ declare function nishsqlite_column_text(h: i32, c: i32): string | null
81
+ declare function nishsqlite_column_into(h: i32, c: i32, buf: u8[], n: u64, at: i32): i32
82
+ declare function sqlite3_errstr(code: i32): string
83
+ declare function sqlite3_libversion(): string
84
+
85
+ /** Success. */
86
+ export const SQLITE_OK: i32 = 0
87
+ /** A generic failure; `error(code).message` says which. */
88
+ export const SQLITE_ERROR: i32 = 1
89
+ /** An internal fault in SQLite. */
90
+ export const SQLITE_INTERNAL: i32 = 2
91
+ /** The access mode the database was opened with does not allow the write. */
92
+ export const SQLITE_PERM: i32 = 3
93
+ /** The operation was interrupted. */
94
+ export const SQLITE_ABORT: i32 = 4
95
+ /** Another connection holds the lock the operation needs. */
96
+ export const SQLITE_BUSY: i32 = 5
97
+ /** A table is locked by this connection's own work. */
98
+ export const SQLITE_LOCKED: i32 = 6
99
+ /** SQLite could not allocate. */
100
+ export const SQLITE_NOMEM: i32 = 7
101
+ /** The database is read-only. */
102
+ export const SQLITE_READONLY: i32 = 8
103
+ /** The operating system failed a read or a write. */
104
+ export const SQLITE_IOERR: i32 = 10
105
+ /** The database file is malformed. */
106
+ export const SQLITE_CORRUPT: i32 = 11
107
+ /** The disk is full. */
108
+ export const SQLITE_FULL: i32 = 13
109
+ /** The file could not be opened. */
110
+ export const SQLITE_CANTOPEN: i32 = 14
111
+ /** The schema changed; prepare the statement again. */
112
+ export const SQLITE_SCHEMA: i32 = 17
113
+ /** A string or blob is larger than SQLite's limit. */
114
+ export const SQLITE_TOOBIG: i32 = 18
115
+ /** A constraint (UNIQUE, NOT NULL, CHECK, a foreign key) failed. */
116
+ export const SQLITE_CONSTRAINT: i32 = 19
117
+ /** A value of the wrong type for a STRICT column. */
118
+ export const SQLITE_MISMATCH: i32 = 20
119
+ /** A call out of order, or a handle that names nothing. */
120
+ export const SQLITE_MISUSE: i32 = 21
121
+ /** A parameter or column index that is not there. */
122
+ export const SQLITE_RANGE: i32 = 25
123
+ /** The file is not a database. */
124
+ export const SQLITE_NOTADB: i32 = 26
125
+ /** `step` has a row. */
126
+ export const SQLITE_ROW: i32 = 100
127
+ /** `step` has finished. */
128
+ export const SQLITE_DONE: i32 = 101
129
+
130
+ /** `columnType` of an integer. */
131
+ export const SQLITE_INTEGER: i32 = 1
132
+ /** `columnType` of a floating-point value. */
133
+ export const SQLITE_FLOAT: i32 = 2
134
+ /** `columnType` of text. */
135
+ export const SQLITE_TEXT: i32 = 3
136
+ /** `columnType` of a blob. */
137
+ export const SQLITE_BLOB: i32 = 4
138
+ /** `columnType` of SQL NULL. */
139
+ export const SQLITE_NULL: i32 = 5
140
+
141
+ /** Open for reading only. One of the three modes `openDatabase` needs. */
142
+ export const OPEN_READONLY: i32 = 0x1
143
+ /** Open for reading and writing; the file must exist unless `OPEN_CREATE` is given too. */
144
+ export const OPEN_READWRITE: i32 = 0x2
145
+ /** With `OPEN_READWRITE`, create the file when it is not there. */
146
+ export const OPEN_CREATE: i32 = 0x4
147
+ /** Read the path as a `file:` URI. */
148
+ export const OPEN_URI: i32 = 0x40
149
+ /** An in-memory database, the path only naming it. */
150
+ export const OPEN_MEMORY: i32 = 0x80
151
+ /** Refuse a path whose last component is a symbolic link. */
152
+ export const OPEN_NOFOLLOW: i32 = 0x01000000
153
+
154
+ /** The SQLite version linked in, such as `3.50.4`. */
155
+ export const sqliteVersion = (): string => sqlite3_libversion()
156
+
157
+ /** A failure, with SQLite's message for it. Built only when asked for, by `error(code)`. */
158
+ export class SqliteError {
159
+ /** The primary result code, such as `SQLITE_CONSTRAINT`. */
160
+ code: i32
161
+ /** The extended code, such as `SQLITE_CONSTRAINT_UNIQUE` (2067). */
162
+ extendedCode: i32
163
+ /** SQLite's message, such as `UNIQUE constraint failed: t.id`. */
164
+ message: string
165
+
166
+ constructor(extendedCode: i32, message: string) {
167
+ this.code = extendedCode & 0xff
168
+ this.extendedCode = extendedCode
169
+ this.message = message
170
+ }
171
+ }
172
+
173
+ /** `Ok()` for `SQLITE_OK`, the code otherwise. */
174
+ const done = (rc: i32): Result<void, i32> => {
175
+ if (rc !== SQLITE_OK) {
176
+ return Err(rc)
177
+ }
178
+ return Ok()
179
+ }
180
+
181
+ /**
182
+ * The message for `code`: the connection's own, unless the code is one the
183
+ * adapter can answer without SQLite (`MISUSE`, `RANGE`, `NOMEM` for a full
184
+ * handle table or a failed conversion), where the connection's message may
185
+ * still be that of some earlier failure.
186
+ */
187
+ const messageFor = (code: i32, connection: string): string => {
188
+ const primary: i32 = code & 0xff
189
+ if (primary === SQLITE_MISUSE || primary === SQLITE_RANGE || primary === SQLITE_NOMEM) {
190
+ return sqlite3_errstr(code)
191
+ }
192
+ return connection
193
+ }
194
+
195
+ /**
196
+ * Opens the database at `path`. `flags` holds one of `OPEN_READONLY`,
197
+ * `OPEN_READWRITE` or `OPEN_READWRITE | OPEN_CREATE`, with any of `OPEN_URI`,
198
+ * `OPEN_MEMORY` and `OPEN_NOFOLLOW`; anything else is `SQLITE_MISUSE`. The
199
+ * path `:memory:` is a database of its own that lives as long as the
200
+ * connection.
201
+ */
202
+ export const openDatabase = (path: string, flags: i32): Result<Database, SqliteError> => {
203
+ const h: i32 = nishsqlite_open(path, flags)
204
+ if (h < 0) {
205
+ return Err(new SqliteError(-h, nishsqlite_open_errmsg()))
206
+ }
207
+ return Ok(new Database(h))
208
+ }
209
+
210
+ /** A connection. Close it with `close`, which finalizes its statements too. */
211
+ export class Database {
212
+ /** The adapter's handle; 0 names nothing. */
213
+ handle: i32
214
+
215
+ constructor(handle: i32) {
216
+ this.handle = handle
217
+ }
218
+
219
+ /** Runs every statement of `sql` in turn, discarding any rows. */
220
+ exec(sql: string): Result<void, i32> {
221
+ return done(nishsqlite_exec(this.handle, sql))
222
+ }
223
+
224
+ /** `exec("BEGIN")`. */
225
+ begin(): Result<void, i32> {
226
+ return this.exec("BEGIN")
227
+ }
228
+
229
+ /** `exec("COMMIT")`. */
230
+ commit(): Result<void, i32> {
231
+ return this.exec("COMMIT")
232
+ }
233
+
234
+ /** `exec("ROLLBACK")`. */
235
+ rollback(): Result<void, i32> {
236
+ return this.exec("ROLLBACK")
237
+ }
238
+
239
+ /**
240
+ * Compiles the first statement of `sql`, to be bound, stepped and reset as
241
+ * often as wanted; text after it is ignored. Text with no statement in it is
242
+ * `SQLITE_MISUSE`.
243
+ */
244
+ prepare(sql: string): Result<Statement, SqliteError> {
245
+ const s: i32 = nishsqlite_prepare(this.handle, sql)
246
+ if (s < 0) {
247
+ return Err(this.error(-s))
248
+ }
249
+ return Ok(new Statement(s))
250
+ }
251
+
252
+ /** The error for `code`, which this connection's last call answered. Ask before the next call. */
253
+ error(code: i32): SqliteError {
254
+ return new SqliteError(code, messageFor(code, nishsqlite_errmsg(this.handle)))
255
+ }
256
+
257
+ /** The rowid of the last row an INSERT on this connection made, 0 before any. */
258
+ lastInsertRowid(): i64 {
259
+ return nishsqlite_last_insert_rowid(this.handle)
260
+ }
261
+
262
+ /** The rows the last INSERT, UPDATE or DELETE changed. */
263
+ changes(): i64 {
264
+ return nishsqlite_changes(this.handle)
265
+ }
266
+
267
+ /** Finalizes every statement of this connection and closes it; the handles name nothing after. */
268
+ close(): Result<void, i32> {
269
+ return done(nishsqlite_close(this.handle))
270
+ }
271
+ }
272
+
273
+ /**
274
+ * A prepared statement. Parameters count from 1 (`?1`, or the order of `?`),
275
+ * columns from 0, as in SQLite.
276
+ */
277
+ export class Statement {
278
+ /** The adapter's handle. */
279
+ handle: i32
280
+
281
+ constructor(handle: i32) {
282
+ this.handle = handle
283
+ }
284
+
285
+ /** Binds an integer to parameter `i`. */
286
+ bindI64(i: i32, v: i64): Result<void, i32> {
287
+ return done(nishsqlite_bind_i64(this.handle, i, v))
288
+ }
289
+
290
+ /** Binds a floating-point value to parameter `i`. */
291
+ bindF64(i: i32, v: f64): Result<void, i32> {
292
+ return done(nishsqlite_bind_f64(this.handle, i, v))
293
+ }
294
+
295
+ /** Binds SQL NULL to parameter `i`. */
296
+ bindNull(i: i32): Result<void, i32> {
297
+ return done(nishsqlite_bind_null(this.handle, i))
298
+ }
299
+
300
+ /** Binds `text` to parameter `i`; SQLite copies it. */
301
+ bindText(i: i32, text: string): Result<void, i32> {
302
+ return done(nishsqlite_bind_text(this.handle, i, text))
303
+ }
304
+
305
+ /** Binds `buf[off, off + len)` as text, copied; a window outside `buf` is `SQLITE_MISUSE`. */
306
+ bindTextBytes(i: i32, buf: readonly u8[], off: i32, len: i32): Result<void, i32> {
307
+ return done(nishsqlite_bind_bytes(this.handle, i, buf, toU64(buf.length), off, len, 0))
308
+ }
309
+
310
+ /** Binds `buf[off, off + len)` as a blob, copied; a window outside `buf` is `SQLITE_MISUSE`. */
311
+ bindBlob(i: i32, buf: readonly u8[], off: i32, len: i32): Result<void, i32> {
312
+ return done(nishsqlite_bind_bytes(this.handle, i, buf, toU64(buf.length), off, len, 1))
313
+ }
314
+
315
+ /** Clears every parameter back to NULL. */
316
+ clearBindings(): Result<void, i32> {
317
+ return done(nishsqlite_clear_bindings(this.handle))
318
+ }
319
+
320
+ /** Runs the statement to its next row: `true` with a row to read, `false` when it has finished. */
321
+ step(): Result<boolean, i32> {
322
+ const rc: i32 = nishsqlite_step(this.handle)
323
+ if (rc === SQLITE_ROW) {
324
+ return Ok(true)
325
+ }
326
+ if (rc === SQLITE_DONE) {
327
+ return Ok(false)
328
+ }
329
+ return Err(rc)
330
+ }
331
+
332
+ /** Rewinds the statement to run again, keeping its bindings. */
333
+ reset(): Result<void, i32> {
334
+ return done(nishsqlite_reset(this.handle))
335
+ }
336
+
337
+ /** Frees the statement; its handle names nothing after. */
338
+ finalize(): Result<void, i32> {
339
+ return done(nishsqlite_finalize(this.handle))
340
+ }
341
+
342
+ /** The number of columns a row has. */
343
+ columnCount(): i32 {
344
+ return nishsqlite_column_count(this.handle)
345
+ }
346
+
347
+ /** The type of column `c` in this row, `SQLITE_INTEGER` to `SQLITE_NULL`; `SQLITE_NULL` for a column that is not there. */
348
+ columnType(c: i32): i32 {
349
+ return nishsqlite_column_type(this.handle, c)
350
+ }
351
+
352
+ /** Column `c` as an integer, converted as SQLite converts; 0 for NULL or a column that is not there. */
353
+ columnI64(c: i32): i64 {
354
+ return nishsqlite_column_i64(this.handle, c)
355
+ }
356
+
357
+ /** Column `c` as a floating-point value; 0 for NULL or a column that is not there. */
358
+ columnF64(c: i32): f64 {
359
+ return nishsqlite_column_f64(this.handle, c)
360
+ }
361
+
362
+ /** The bytes `columnInto` would copy for column `c`. */
363
+ columnBytes(c: i32): i32 {
364
+ return nishsqlite_column_bytes(this.handle, c)
365
+ }
366
+
367
+ /** Column `c` as a new string, cut at its first NUL; `null` for NULL or a column that is not there. Allocates. */
368
+ columnText(c: i32): string | null {
369
+ return nishsqlite_column_text(this.handle, c)
370
+ }
371
+
372
+ /**
373
+ * Copies column `c` — a blob's bytes, anything else as text — into `buf`
374
+ * at `at`, and answers its length. It copies only when all of it fits, so
375
+ * a length larger than `buf.length - at` means nothing was written and
376
+ * says how much room is needed. `SQLITE_RANGE` for a column that is not
377
+ * there, `SQLITE_MISUSE` for an `at` outside `0..buf.length`.
378
+ */
379
+ columnInto(c: i32, buf: u8[], at: i32): Result<i32, i32> {
380
+ const n: i32 = nishsqlite_column_into(this.handle, c, buf, toU64(buf.length), at)
381
+ if (n < 0) {
382
+ return Err(-n)
383
+ }
384
+ return Ok(n)
385
+ }
386
+
387
+ /** The error for `code`, which this statement's last call answered. Ask before the next call. */
388
+ error(code: i32): SqliteError {
389
+ return new SqliteError(code, messageFor(code, nishsqlite_stmt_errmsg(this.handle)))
390
+ }
391
+ }