nish 0.35.0 → 0.37.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/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/json.ts
CHANGED
|
@@ -70,28 +70,31 @@
|
|
|
70
70
|
* infinities as `null`. `jsonStringifyString` is the same string rule
|
|
71
71
|
* answered as a string.
|
|
72
72
|
*
|
|
73
|
-
* **Two modes**, both off by default and both kept across `reset`, so a
|
|
74
|
-
*
|
|
75
|
-
* `JSON.stringify(v, null, n)` and `JSON.stringify(v, null, "\t")` write:
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
73
|
+
* **Two modes**, both off by default and both kept across `reset`, so a server
|
|
74
|
+
* sets them once. `setIndent(n)` and `setIndentTab()` write what
|
|
75
|
+
* `JSON.stringify(v, null, n)` and `JSON.stringify(v, null, "\t")` write: one
|
|
76
|
+
* member or element a line, `": "` after a key, an empty `{}` or `[]` on one
|
|
77
|
+
* line, and `n` clamped to 10. The indent is one byte stored as many times as
|
|
78
|
+
* the depth asks, so the indented text allocates no more than the minified one,
|
|
79
|
+
* and a `raw` fragment is copied as it is, not re-indented.
|
|
80
|
+
*
|
|
81
|
+
* `setStrict(true)` makes NaN and the infinities, which JSON cannot spell, the
|
|
82
|
+
* sticky `JSON_WRITE_NON_FINITE` instead of `null`: for a caller to whom a
|
|
83
|
+
* `null` where a number was due would be a silently corrupt record.
|
|
83
84
|
*
|
|
84
85
|
* **Misuse is a sticky code**, as in `nish/net/hpack`: never a panic, and no
|
|
85
86
|
* `Result` to carry on the hot path. The first call that cannot be honoured
|
|
86
|
-
* sets `failed` to a negative `JSON_WRITE_*` code — a value where a key is
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
87
|
+
* sets `failed` to a negative `JSON_WRITE_*` code — a value where a key is due,
|
|
88
|
+
* a key where a value is due, an unbalanced or mismatched `end*`, nesting past
|
|
89
|
+
* `JSON_WRITER_MAX_DEPTH`, a buffer with no room, a second top-level value, a
|
|
90
|
+
* bad `raw` window or `start`, a non-finite `f64` in strict mode — and every
|
|
91
|
+
* later call does nothing.
|
|
92
|
+
*
|
|
93
|
+
* A call that fails writes nothing past `buf.length` (a full buffer is checked
|
|
94
|
+
* before the bytes go in, never by the bounds check), so the caller writes the
|
|
95
|
+
* whole value, reads `failed` once at the end, and grows the buffer and retries
|
|
96
|
+
* or answers a 500. `complete()` says whether one whole value was written with
|
|
97
|
+
* every container closed.
|
|
95
98
|
*
|
|
96
99
|
* **A string is its bytes** (WP33 class C, `docs/wp33-round-trip.md` §3.2,
|
|
97
100
|
* the first row). The writer escapes by byte and copies every byte at or
|
|
@@ -612,11 +615,19 @@ const jsonKeyEquals = (text: string, at: i32, end: i32, name: string): boolean =
|
|
|
612
615
|
* what keeps it a stack slot rather than an arena allocation.
|
|
613
616
|
*/
|
|
614
617
|
class JsonMember {
|
|
618
|
+
/** @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`. */
|
|
615
619
|
keyAt: i32 = 0
|
|
620
|
+
/** @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`. */
|
|
616
621
|
keyEnd: i32 = 0
|
|
622
|
+
/** @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`. */
|
|
617
623
|
valueAt: i32 = 0
|
|
624
|
+
/** @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`. */
|
|
618
625
|
valueEnd: i32 = 0
|
|
619
|
-
/**
|
|
626
|
+
/**
|
|
627
|
+
* The opening quote of the next key, or `-1` when no comma follows the value.
|
|
628
|
+
*
|
|
629
|
+
* @internal Set by `jsonReadMember` and read by `jsonField` and `jsonFields`.
|
|
630
|
+
*/
|
|
620
631
|
next: i32 = -1
|
|
621
632
|
}
|
|
622
633
|
|
|
@@ -711,6 +722,10 @@ const jsonValueText = (object: string, member: JsonMember): string => {
|
|
|
711
722
|
* A nested value is stepped over rather than searched, so `name` is a field of
|
|
712
723
|
* this object and not a path into it: that is the "flat" in the module header,
|
|
713
724
|
* and it is the shape the compiler's `--json` line has.
|
|
725
|
+
*
|
|
726
|
+
* @param object - the text of one JSON object.
|
|
727
|
+
* @param name - the field to look for, matched against each key's decoded text.
|
|
728
|
+
* @returns the first such field's value, or `null` when there is none.
|
|
714
729
|
*/
|
|
715
730
|
export const jsonField = (object: string, name: string): string | null => {
|
|
716
731
|
const member: JsonMember = new JsonMember()
|
|
@@ -742,15 +757,21 @@ export const jsonField = (object: string, name: string): string | null => {
|
|
|
742
757
|
*
|
|
743
758
|
* The answer is a fresh array and the only allocation besides the values. Each
|
|
744
759
|
* value is stored into it by a plain `values[k] = text` statement, and the
|
|
745
|
-
* array is only stored into, tested and returned, so the values travel with
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
*
|
|
750
|
-
*
|
|
751
|
-
*
|
|
752
|
-
*
|
|
753
|
-
*
|
|
760
|
+
* array is only stored into, tested and returned, so the values travel with the
|
|
761
|
+
* array: the call lets nothing else out (LANGUAGE.md, "Memory model", a value
|
|
762
|
+
* stored into an array that is returned with it). A loop calling this keeps its
|
|
763
|
+
* per-pass release on a line older than the pass, as `jsonField`'s does, and a
|
|
764
|
+
* `using a = arena()` block may hold the call.
|
|
765
|
+
*
|
|
766
|
+
* On a line built in the pass neither reader keeps the release (LANGUAGE.md,
|
|
767
|
+
* "Memory model"). Reading `values` back, passing it to a function, or storing
|
|
768
|
+
* it anywhere before the return would undo this; `tests/link/std_json` pins the
|
|
769
|
+
* flat arena and the `using` block.
|
|
770
|
+
*
|
|
771
|
+
* @param object - the text of one JSON object.
|
|
772
|
+
* @param names - the fields to look for.
|
|
773
|
+
* @returns one value per name, in the order of `names`, each `null` when that
|
|
774
|
+
* field is missing.
|
|
754
775
|
*/
|
|
755
776
|
export const jsonFields = (object: string, names: string[]): (string | null)[] => {
|
|
756
777
|
const count: i32 = toI32(names.length)
|
|
@@ -901,8 +922,9 @@ const jsonEscapedWidth = (code: i32): i32 => {
|
|
|
901
922
|
* a caller writes the whole value and reads `failed` once at the end.
|
|
902
923
|
*/
|
|
903
924
|
export class JsonWriter {
|
|
925
|
+
/** The caller's buffer the text is written into; the writer never grows it. */
|
|
904
926
|
buf: u8[]
|
|
905
|
-
start: i32 = 0
|
|
927
|
+
private start: i32 = 0
|
|
906
928
|
/** The cursor: the bytes written are `buf[start .. end)`. */
|
|
907
929
|
end: i32 = 0
|
|
908
930
|
/** `JSON_WRITE_OK`, or the first `JSON_WRITE_*` code a call hit. */
|
|
@@ -918,8 +940,15 @@ export class JsonWriter {
|
|
|
918
940
|
* would save. Two `i32` words rather than an `i64` or an array, so that a
|
|
919
941
|
* writer allocates nothing and the module still runs under Node, where an
|
|
920
942
|
* `i64` is a BigInt.
|
|
943
|
+
*
|
|
944
|
+
* @internal Read and written by the `jsonWriter*` free functions of this
|
|
945
|
+
* module, which do the writer's work.
|
|
921
946
|
*/
|
|
922
947
|
objectsLow: i32 = 0
|
|
948
|
+
/**
|
|
949
|
+
* @internal Read and written by the `jsonWriter*` free functions of this
|
|
950
|
+
* module, which do the writer's work.
|
|
951
|
+
*/
|
|
923
952
|
objectsHigh: i32 = 0
|
|
924
953
|
/**
|
|
925
954
|
* What goes before the next key or element of the innermost container,
|
|
@@ -927,6 +956,9 @@ export class JsonWriter {
|
|
|
927
956
|
* line break or a comma and a line break. The minified text tests it only
|
|
928
957
|
* where it would test a "comma due" flag, against 0 and 1, so the indented
|
|
929
958
|
* states cost it nothing; bit 1 is the indent and every step keeps it.
|
|
959
|
+
*
|
|
960
|
+
* @internal Read and written by the `jsonWriter*` free functions of this
|
|
961
|
+
* module, which do the writer's work.
|
|
930
962
|
*/
|
|
931
963
|
sep: i32 = 0
|
|
932
964
|
/**
|
|
@@ -935,15 +967,37 @@ export class JsonWriter {
|
|
|
935
967
|
* keeps it, so a writer set up once writes every text the same way.
|
|
936
968
|
*/
|
|
937
969
|
indentWidth: i32 = 0
|
|
938
|
-
/**
|
|
970
|
+
/**
|
|
971
|
+
* The byte an indent is made of: a space, or a tab after `setIndentTab`.
|
|
972
|
+
*
|
|
973
|
+
* @internal Read and written by the `jsonWriter*` free functions of this
|
|
974
|
+
* module, which do the writer's work.
|
|
975
|
+
*/
|
|
939
976
|
indentByte: i32 = 32
|
|
940
|
-
/**
|
|
977
|
+
/**
|
|
978
|
+
* Whether a key has been written in the innermost object and its value is
|
|
979
|
+
* due.
|
|
980
|
+
*
|
|
981
|
+
* @internal Read and written by the `jsonWriter*` free functions of this
|
|
982
|
+
* module, which do the writer's work.
|
|
983
|
+
*/
|
|
941
984
|
afterKey: boolean = false
|
|
942
|
-
/**
|
|
985
|
+
/**
|
|
986
|
+
* Whether the top-level value has been written whole.
|
|
987
|
+
*
|
|
988
|
+
* @internal Read and written by the `jsonWriter*` free functions of this
|
|
989
|
+
* module, which do the writer's work.
|
|
990
|
+
*/
|
|
943
991
|
done: boolean = false
|
|
944
992
|
/** Whether a NaN or infinite `f64` is `JSON_WRITE_NON_FINITE` rather than `null`. Kept by `reset`. */
|
|
945
993
|
strict: boolean = false
|
|
946
994
|
|
|
995
|
+
/**
|
|
996
|
+
* A writer that starts its text at `buf[start ..)`, as `reset` does.
|
|
997
|
+
*
|
|
998
|
+
* @param buf - the buffer to write into, which the caller keeps.
|
|
999
|
+
* @param start - the offset in `buf` the text starts at.
|
|
1000
|
+
*/
|
|
947
1001
|
constructor(buf: u8[], start: i32) {
|
|
948
1002
|
this.buf = buf
|
|
949
1003
|
this.reset(buf, start)
|
|
@@ -953,6 +1007,9 @@ export class JsonWriter {
|
|
|
953
1007
|
* Starts a new text at `buf[start ..)`, forgetting everything the last one
|
|
954
1008
|
* wrote and every error it hit: what a server calls once per response, on
|
|
955
1009
|
* the buffer it reuses. A `start` outside `buf` is `JSON_WRITE_BAD_WINDOW`.
|
|
1010
|
+
*
|
|
1011
|
+
* @param buf - the buffer to write into, which the caller keeps.
|
|
1012
|
+
* @param start - the offset in `buf` the text starts at.
|
|
956
1013
|
*/
|
|
957
1014
|
reset(buf: u8[], start: i32): void {
|
|
958
1015
|
this.buf = buf
|
|
@@ -974,6 +1031,8 @@ export class JsonWriter {
|
|
|
974
1031
|
* is clamped to 10 as `JSON.stringify` clamps it, and below 1 is the
|
|
975
1032
|
* minified text, the default. A mode that `reset` keeps; set it before the
|
|
976
1033
|
* text, since a change in the middle of one applies from the next byte.
|
|
1034
|
+
*
|
|
1035
|
+
* @param spaces - the spaces per level, 1 to 10; anything less is minified.
|
|
977
1036
|
*/
|
|
978
1037
|
setIndent(spaces: i32): void {
|
|
979
1038
|
let width: i32 = spaces
|
|
@@ -1001,17 +1060,29 @@ export class JsonWriter {
|
|
|
1001
1060
|
* the `null` `JSON.stringify` writes. Off by default; a mode that `reset`
|
|
1002
1061
|
* keeps. Strict is for a caller to whom a `null` where a number was due is
|
|
1003
1062
|
* a corrupt record rather than a missing one.
|
|
1063
|
+
*
|
|
1064
|
+
* @param strict - true to fail on a NaN or an infinity, false to write
|
|
1065
|
+
* `null`.
|
|
1004
1066
|
*/
|
|
1005
1067
|
setStrict(strict: boolean): void {
|
|
1006
1068
|
this.strict = strict
|
|
1007
1069
|
}
|
|
1008
1070
|
|
|
1009
|
-
/**
|
|
1071
|
+
/**
|
|
1072
|
+
* How many bytes have been written since `reset`.
|
|
1073
|
+
*
|
|
1074
|
+
* @returns the byte count, `end - start`.
|
|
1075
|
+
*/
|
|
1010
1076
|
written(): i32 {
|
|
1011
1077
|
return this.end - this.start
|
|
1012
1078
|
}
|
|
1013
1079
|
|
|
1014
|
-
/**
|
|
1080
|
+
/**
|
|
1081
|
+
* Whether one whole value has been written, every container closed, and
|
|
1082
|
+
* nothing failed.
|
|
1083
|
+
*
|
|
1084
|
+
* @returns true when the text is one whole JSON value.
|
|
1085
|
+
*/
|
|
1015
1086
|
complete(): boolean {
|
|
1016
1087
|
return this.failed === JSON_WRITE_OK && this.done
|
|
1017
1088
|
}
|
|
@@ -1020,6 +1091,8 @@ export class JsonWriter {
|
|
|
1020
1091
|
* The bytes written so far, `buf[start .. end)`, as a string. A convenience
|
|
1021
1092
|
* that allocates a byte at a time, for a caller that wants text rather than
|
|
1022
1093
|
* bytes; a server sends `buf` itself.
|
|
1094
|
+
*
|
|
1095
|
+
* @returns a new string of the bytes written.
|
|
1023
1096
|
*/
|
|
1024
1097
|
text(): string {
|
|
1025
1098
|
const buf: u8[] = this.buf
|
|
@@ -1030,10 +1103,21 @@ export class JsonWriter {
|
|
|
1030
1103
|
return parts.join("")
|
|
1031
1104
|
}
|
|
1032
1105
|
|
|
1106
|
+
/**
|
|
1107
|
+
* Opens an object where a value is due. `JSON_WRITE_KEY_EXPECTED` inside an
|
|
1108
|
+
* object before its key, `JSON_WRITE_TOO_DEEP` past `JSON_WRITER_MAX_DEPTH`
|
|
1109
|
+
* open containers, `JSON_WRITE_DONE` after the top-level value, and
|
|
1110
|
+
* `JSON_WRITE_FULL` when `buf` has no room.
|
|
1111
|
+
*/
|
|
1033
1112
|
beginObject(): void {
|
|
1034
1113
|
jsonWriterOpen(this, true, JSON_OPEN_BRACE)
|
|
1035
1114
|
}
|
|
1036
1115
|
|
|
1116
|
+
/**
|
|
1117
|
+
* Closes the innermost object. `JSON_WRITE_UNBALANCED` when that is not an
|
|
1118
|
+
* object, or nothing is open, and `JSON_WRITE_VALUE_EXPECTED` when a key is
|
|
1119
|
+
* still waiting for its value.
|
|
1120
|
+
*/
|
|
1037
1121
|
endObject(): void {
|
|
1038
1122
|
if (this.sep === JSON_WRITER_SEP_COMMA_BREAK) {
|
|
1039
1123
|
jsonWriterCloseIndented(this, true, JSON_CLOSE_BRACE)
|
|
@@ -1042,10 +1126,15 @@ export class JsonWriter {
|
|
|
1042
1126
|
}
|
|
1043
1127
|
}
|
|
1044
1128
|
|
|
1129
|
+
/** Opens an array where a value is due, with the same errors as `beginObject`. */
|
|
1045
1130
|
beginArray(): void {
|
|
1046
1131
|
jsonWriterOpen(this, false, JSON_OPEN_BRACKET)
|
|
1047
1132
|
}
|
|
1048
1133
|
|
|
1134
|
+
/**
|
|
1135
|
+
* Closes the innermost array. `JSON_WRITE_UNBALANCED` when that is not an
|
|
1136
|
+
* array, or nothing is open.
|
|
1137
|
+
*/
|
|
1049
1138
|
endArray(): void {
|
|
1050
1139
|
if (this.sep === JSON_WRITER_SEP_COMMA_BREAK) {
|
|
1051
1140
|
jsonWriterCloseIndented(this, false, JSON_CLOSE_BRACKET)
|
|
@@ -1054,7 +1143,11 @@ export class JsonWriter {
|
|
|
1054
1143
|
}
|
|
1055
1144
|
}
|
|
1056
1145
|
|
|
1057
|
-
/**
|
|
1146
|
+
/**
|
|
1147
|
+
* The next member's key, with the comma before it and the colon after it.
|
|
1148
|
+
*
|
|
1149
|
+
* @param name - the key, escaped as `string` escapes a value.
|
|
1150
|
+
*/
|
|
1058
1151
|
key(name: string): void {
|
|
1059
1152
|
if (this.failed !== JSON_WRITE_OK) {
|
|
1060
1153
|
return
|
|
@@ -1082,7 +1175,12 @@ export class JsonWriter {
|
|
|
1082
1175
|
}
|
|
1083
1176
|
}
|
|
1084
1177
|
|
|
1085
|
-
/**
|
|
1178
|
+
/**
|
|
1179
|
+
* `s` quoted and escaped as `JSON.stringify` escapes it; every other byte
|
|
1180
|
+
* goes through as itself.
|
|
1181
|
+
*
|
|
1182
|
+
* @param s - the text to write.
|
|
1183
|
+
*/
|
|
1086
1184
|
string(s: string): void {
|
|
1087
1185
|
if (jsonWriterValueDue(this) && jsonWriterPutString(this, s)) {
|
|
1088
1186
|
jsonWriterValueDone(this)
|
|
@@ -1096,6 +1194,10 @@ export class JsonWriter {
|
|
|
1096
1194
|
* `JsonDocument`, writes without making a string per key. A window outside
|
|
1097
1195
|
* `bytes` is `JSON_WRITE_BAD_WINDOW`, checked once, before anything is
|
|
1098
1196
|
* written.
|
|
1197
|
+
*
|
|
1198
|
+
* @param bytes - the array holding the text.
|
|
1199
|
+
* @param off - the offset in `bytes` of its first byte.
|
|
1200
|
+
* @param len - how many bytes it is.
|
|
1099
1201
|
*/
|
|
1100
1202
|
keyBytes(bytes: u8[], off: i32, len: i32): void {
|
|
1101
1203
|
if (this.failed !== JSON_WRITE_OK) {
|
|
@@ -1135,6 +1237,10 @@ export class JsonWriter {
|
|
|
1135
1237
|
* quotes and escapes, byte for byte, and no allocation. A window outside
|
|
1136
1238
|
* `bytes` is `JSON_WRITE_BAD_WINDOW`, checked once, before anything is
|
|
1137
1239
|
* written.
|
|
1240
|
+
*
|
|
1241
|
+
* @param bytes - the array holding the text.
|
|
1242
|
+
* @param off - the offset in `bytes` of its first byte.
|
|
1243
|
+
* @param len - how many bytes it is.
|
|
1138
1244
|
*/
|
|
1139
1245
|
stringBytes(bytes: u8[], off: i32, len: i32): void {
|
|
1140
1246
|
if (this.failed !== JSON_WRITE_OK) {
|
|
@@ -1148,6 +1254,11 @@ export class JsonWriter {
|
|
|
1148
1254
|
}
|
|
1149
1255
|
}
|
|
1150
1256
|
|
|
1257
|
+
/**
|
|
1258
|
+
* `v` in decimal, as `String(v)` spells it.
|
|
1259
|
+
*
|
|
1260
|
+
* @param v - the value to write.
|
|
1261
|
+
*/
|
|
1151
1262
|
i32(v: i32): void {
|
|
1152
1263
|
if (jsonWriterValueDue(this) && jsonWriterPutDigits(this, v, -1)) {
|
|
1153
1264
|
jsonWriterValueDone(this)
|
|
@@ -1157,6 +1268,8 @@ export class JsonWriter {
|
|
|
1157
1268
|
/**
|
|
1158
1269
|
* One past `i32`'s range is ten digits, so the last digit is split off in
|
|
1159
1270
|
* `u32` and the nine before it are an `i32`'s.
|
|
1271
|
+
*
|
|
1272
|
+
* @param v - the value to write.
|
|
1160
1273
|
*/
|
|
1161
1274
|
u32(v: u32): void {
|
|
1162
1275
|
if (!jsonWriterValueDue(this)) {
|
|
@@ -1177,7 +1290,12 @@ export class JsonWriter {
|
|
|
1177
1290
|
}
|
|
1178
1291
|
}
|
|
1179
1292
|
|
|
1180
|
-
/**
|
|
1293
|
+
/**
|
|
1294
|
+
* The digits of `v`, the minimum included: the arithmetic stays on the
|
|
1295
|
+
* negative side, where it fits.
|
|
1296
|
+
*
|
|
1297
|
+
* @param v - the value to write.
|
|
1298
|
+
*/
|
|
1181
1299
|
i64(v: i64): void {
|
|
1182
1300
|
if (jsonWriterValueDue(this) && jsonWriterPutDigits64(this, v)) {
|
|
1183
1301
|
jsonWriterValueDone(this)
|
|
@@ -1189,6 +1307,8 @@ export class JsonWriter {
|
|
|
1189
1307
|
* shortest digits that read back as `v`, `-0` as `0`, and `null` for NaN
|
|
1190
1308
|
* and both infinities, which JSON cannot spell — or, once `setStrict(true)`,
|
|
1191
1309
|
* `JSON_WRITE_NON_FINITE` for those three, with nothing written.
|
|
1310
|
+
*
|
|
1311
|
+
* @param v - the value to write.
|
|
1192
1312
|
*/
|
|
1193
1313
|
f64(v: f64): void {
|
|
1194
1314
|
// `v - v` is `0` for every finite `v`, and NaN for NaN and both infinities.
|
|
@@ -1203,6 +1323,11 @@ export class JsonWriter {
|
|
|
1203
1323
|
}
|
|
1204
1324
|
}
|
|
1205
1325
|
|
|
1326
|
+
/**
|
|
1327
|
+
* `true` or `false`.
|
|
1328
|
+
*
|
|
1329
|
+
* @param v - the value to write.
|
|
1330
|
+
*/
|
|
1206
1331
|
boolean(v: boolean): void {
|
|
1207
1332
|
if (jsonWriterValueDue(this) && jsonWriterPutText(this, v ? "true" : "false")) {
|
|
1208
1333
|
jsonWriterValueDone(this)
|
|
@@ -1226,6 +1351,10 @@ export class JsonWriter {
|
|
|
1226
1351
|
* In an indented text the fragment goes in where a value would, after its
|
|
1227
1352
|
* line break and indent, but its own bytes are not re-indented: an object
|
|
1228
1353
|
* fragment stays on one line, where `JSON.stringify` would spread it out.
|
|
1354
|
+
*
|
|
1355
|
+
* @param bytes - the array holding the fragment.
|
|
1356
|
+
* @param off - the offset in `bytes` of its first byte.
|
|
1357
|
+
* @param len - how many bytes it is.
|
|
1229
1358
|
*/
|
|
1230
1359
|
raw(bytes: u8[], off: i32, len: i32): void {
|
|
1231
1360
|
if (this.failed !== JSON_WRITE_OK) {
|
|
@@ -1836,6 +1965,9 @@ const jsonWriterPutF64 = (w: JsonWriter, v: f64): boolean => {
|
|
|
1836
1965
|
* `JsonWriter.string` writes — as a string. A thin layer on the writer: it
|
|
1837
1966
|
* sizes a buffer to fit exactly, writes into it and reads it back. A string
|
|
1838
1967
|
* with nothing to escape skips the writer and is only quoted.
|
|
1968
|
+
*
|
|
1969
|
+
* @param s - the text to quote.
|
|
1970
|
+
* @returns the quoted, escaped text.
|
|
1839
1971
|
*/
|
|
1840
1972
|
export const jsonStringifyString = (s: string): string => {
|
|
1841
1973
|
const n: i32 = toI32(s.length)
|
package/std/map.ts
CHANGED
|
@@ -3,15 +3,24 @@
|
|
|
3
3
|
* have, written so that a program using them still runs unmodified under Node
|
|
4
4
|
* (docs/wp32-map.md §9.2).
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* import { reserve, getOrInsert } from "nish/map"
|
|
7
|
+
*
|
|
8
|
+
* const counts = new Map<string, i32>()
|
|
9
|
+
* reserve(counts, 64) // room for 64 words before the table grows
|
|
10
|
+
* for (const w of ["to", "be", "or", "not", "to", "be"]) {
|
|
11
|
+
* counts.set(w, getOrInsert(counts, w, 0) + 1) // a new word starts at 0
|
|
12
|
+
* }
|
|
13
|
+
* counts.get("to") // 2
|
|
14
|
+
*
|
|
15
|
+
* **The bodies below are the meaning**, and they are what runs under Node:
|
|
7
16
|
* `reserve` does nothing, and `getOrInsert` is a `get`, and a `set` when the
|
|
8
|
-
* key was missing. Natively neither body is ever called
|
|
9
|
-
* every call in place, as it lowers `hashKey` and `sameKey`: `reserve`
|
|
10
|
-
* table's `reserveSlots`, which grows the buckets so that `n` entries
|
|
11
|
-
* without a rebuild, and `getOrInsert` is one `probe` of the key, then the
|
|
17
|
+
* key was missing. **Natively neither body is ever called.** The compiler
|
|
18
|
+
* lowers every call in place, as it lowers `hashKey` and `sameKey`: `reserve`
|
|
19
|
+
* is the table's `reserveSlots`, which grows the buckets so that `n` entries
|
|
20
|
+
* fit without a rebuild, and `getOrInsert` is one `probe` of the key, then the
|
|
12
21
|
* value it found or an insert through the empty bucket the probe stopped at,
|
|
13
|
-
* with the hash it already computed. So this module writes no `.ll` of its
|
|
14
|
-
*
|
|
22
|
+
* with the hash it already computed. So this module writes no `.ll` of its own,
|
|
23
|
+
* and a one-file program that imports it is still one module.
|
|
15
24
|
*/
|
|
16
25
|
|
|
17
26
|
/**
|
|
@@ -19,6 +28,10 @@
|
|
|
19
28
|
* grows the table. Only the speed of a program depends on it: natively it
|
|
20
29
|
* presizes the bucket table, and under Node, which has no such call, it does
|
|
21
30
|
* nothing. A count that is not positive does nothing either way.
|
|
31
|
+
*
|
|
32
|
+
* @param _m - the map to presize.
|
|
33
|
+
* @param _n - how many entries it should hold, those already in it counted,
|
|
34
|
+
* without growing.
|
|
22
35
|
*/
|
|
23
36
|
export const reserve = <K, V>(_m: Map<K, V>, _n: number): void => {
|
|
24
37
|
// Nothing to do here: natively the compiler lowers the call itself, and Node has no table to presize.
|
|
@@ -28,6 +41,11 @@ export const reserve = <K, V>(_m: Map<K, V>, _n: number): void => {
|
|
|
28
41
|
* The value of `key` in `m`; or, when `key` is missing, `value`, after
|
|
29
42
|
* setting `key` to it. One hash and one probe natively, whichever it was.
|
|
30
43
|
* `value` is evaluated either way, as every argument is.
|
|
44
|
+
*
|
|
45
|
+
* @param m - the map to look in, and to add to when `key` is missing.
|
|
46
|
+
* @param key - the key to find.
|
|
47
|
+
* @param value - the value to store when `key` is missing.
|
|
48
|
+
* @returns the value `key` already had, or `value` when it had none.
|
|
31
49
|
*/
|
|
32
50
|
export const getOrInsert = <K, V>(m: Map<K, V>, key: K, value: V): V => {
|
|
33
51
|
const found = m.get(key)
|
package/std/math.ts
CHANGED
|
@@ -3,7 +3,14 @@
|
|
|
3
3
|
* rather than libm, so a program answers the same on every target, wasm
|
|
4
4
|
* included, and the optimiser sees the whole function.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* export const main = (): i32 => {
|
|
7
|
+
* const slope: f64 = Math.tan(0.5);
|
|
8
|
+
* const angle: f64 = Math.atan2(1.0, -1.0); // 3 * pi / 4
|
|
9
|
+
* const side: f64 = Math.hypot(3.0, 4.0); // 5
|
|
10
|
+
* return side === 5.0 ? 0 : 1;
|
|
11
|
+
* };
|
|
12
|
+
*
|
|
13
|
+
* **A program does not import this module.** A call to one of the three is what
|
|
7
14
|
* loads it (the checker's `importMath`), and the compiler copies each function
|
|
8
15
|
* here that the call reaches into the calling module as an `internal`
|
|
9
16
|
* function, so this file writes no `.ll` of its own. Under Node and `tsc` the
|
|
@@ -344,7 +351,14 @@ const kernelTan = (x0: f64, y0: f64, odd: boolean): f64 => {
|
|
|
344
351
|
const t: f64 = clearLo(a)
|
|
345
352
|
return t + a * (t * zz + 1.0 + t * vv)
|
|
346
353
|
}
|
|
347
|
-
/**
|
|
354
|
+
/**
|
|
355
|
+
* `Math.tan(x)`: fdlibm's reduction and kernel, with `tanLarge` past
|
|
356
|
+
* 2^20 * pi/2.
|
|
357
|
+
*
|
|
358
|
+
* @param x The angle, in radians.
|
|
359
|
+
* @returns The tangent of `x`; NaN for a NaN or an infinite `x`, and `x`
|
|
360
|
+
* itself when `|x| < 2^-27`, a zero's sign kept.
|
|
361
|
+
*/
|
|
348
362
|
export const tanF64 = (x: f64): f64 => {
|
|
349
363
|
const ax: f64 = Math.abs(x)
|
|
350
364
|
if (ax < 0.7853984832763672) {
|
|
@@ -412,7 +426,16 @@ const atanlo = (id: i32): f64 => {
|
|
|
412
426
|
}
|
|
413
427
|
}
|
|
414
428
|
|
|
415
|
-
/**
|
|
429
|
+
/**
|
|
430
|
+
* `Math.atan2(y, x)`: fdlibm's, with a compensated quotient and a
|
|
431
|
+
* double-double pi - atan.
|
|
432
|
+
*
|
|
433
|
+
* @param y The point's vertical coordinate.
|
|
434
|
+
* @param x The point's horizontal coordinate.
|
|
435
|
+
* @returns The angle from the positive x axis to the point `(x, y)`, in
|
|
436
|
+
* radians in [-pi, pi], with JavaScript's value for every signed zero,
|
|
437
|
+
* infinity and NaN.
|
|
438
|
+
*/
|
|
416
439
|
export const atan2F64 = (y: f64, x: f64): f64 => {
|
|
417
440
|
const piO4: f64 = 7.85398163397448279e-1
|
|
418
441
|
const piO2: f64 = 1.570796326794896558
|
|
@@ -592,7 +615,16 @@ const hypotRest = (ax: f64, ay: f64, a: f64, b: f64): f64 => {
|
|
|
592
615
|
return pow2(-600) * hypotCoreF(a * up, b * up)
|
|
593
616
|
}
|
|
594
617
|
|
|
595
|
-
/**
|
|
618
|
+
/**
|
|
619
|
+
* `Math.hypot(x, y)`: fdlibm's core, rescaled by a power of two outside
|
|
620
|
+
* [1e-150, 1e150].
|
|
621
|
+
*
|
|
622
|
+
* @param x One side's length; only its magnitude matters.
|
|
623
|
+
* @param y The other side's length; only its magnitude matters.
|
|
624
|
+
* @returns The square root of `x * x + y * y`, computed without the squares
|
|
625
|
+
* overflowing or underflowing: Infinity when either is infinite, even beside
|
|
626
|
+
* a NaN, and NaN when either is otherwise NaN.
|
|
627
|
+
*/
|
|
596
628
|
export const hypotF64 = (x: f64, y: f64): f64 => {
|
|
597
629
|
const ax: f64 = Math.abs(x)
|
|
598
630
|
const ay: f64 = Math.abs(y)
|
package/std/net/buffer-pool.ts
CHANGED
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
* // ... write out[0 .. used) and send it ...
|
|
10
10
|
* pool.giveSecret(out, used); // or pool.give(out, used) for bytes that were never secret
|
|
11
11
|
*
|
|
12
|
-
* It has the shape of
|
|
13
|
-
* `give`, `made`, a
|
|
14
|
-
* with three things
|
|
12
|
+
* **The shape of `Http1OutputPool`.** It has the shape of
|
|
13
|
+
* `nish/net/http1-server`'s `Http1OutputPool` (`take`, `give`, `made`, a
|
|
14
|
+
* `u8[][]` free list), so the two read alike and can merge, with three things
|
|
15
|
+
* added:
|
|
15
16
|
*
|
|
16
17
|
* - **A ceiling.** At most `ceiling` buffers ever exist. A server sizes it to
|
|
17
18
|
* what preallocating every slot would have cost, so a full server behaves as
|
|
@@ -26,12 +27,13 @@
|
|
|
26
27
|
* buffer, or a give-back with nothing lent is a borrower's bug, and each
|
|
27
28
|
* panics with the pool's name rather than corrupting the free list quietly.
|
|
28
29
|
*
|
|
29
|
-
* The pool grows to its high-water mark
|
|
30
|
+
* **It never shrinks.** The pool grows to its high-water mark: after warm-up a
|
|
30
31
|
* `take` pops the free list and a `give` pushes onto capacity it already has,
|
|
31
32
|
* so a warmed take/give loop moves the arena by nothing.
|
|
32
33
|
*
|
|
33
|
-
* Private helpers share the importing program's flat symbol
|
|
34
|
-
* (`docs/wp26-stdlib.md` §3e), which is why each one carries the
|
|
34
|
+
* **Helper names.** Private helpers share the importing program's flat symbol
|
|
35
|
+
* namespace (`docs/wp26-stdlib.md` §3e), which is why each one carries the
|
|
36
|
+
* module's name.
|
|
35
37
|
*/
|
|
36
38
|
|
|
37
39
|
/**
|
|
@@ -42,13 +44,27 @@
|
|
|
42
44
|
*/
|
|
43
45
|
export class BufferPool {
|
|
44
46
|
// References, then 32-bit fields: the order the layout packs without padding.
|
|
45
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* The buffers given back and not yet taken again; the last one is lent next.
|
|
49
|
+
* @internal Read by std/net/quic-stream.ts, and grown ahead of need by
|
|
50
|
+
* std/net/http3.ts and std/net/http3-server.ts.
|
|
51
|
+
*/
|
|
46
52
|
free: u8[][]
|
|
47
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* Names the pool in every panic, so a leak or a stray buffer says whose it is.
|
|
55
|
+
* @internal Read by `bufferPoolCheckGive` in this module to name the pool in its panics.
|
|
56
|
+
*/
|
|
48
57
|
name: string
|
|
49
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* The length of every buffer the pool makes and takes back.
|
|
60
|
+
* @internal Read by `bufferPoolCheckGive` here, and by std/net/http3-server.ts,
|
|
61
|
+
* std/net/http3.ts and std/net/quic-stream.ts to know how much room a borrowed buffer has.
|
|
62
|
+
*/
|
|
50
63
|
size: i32 = 0
|
|
51
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* The most buffers the pool will ever make.
|
|
66
|
+
* @internal Read by `take` here, and by std/net/quic-stream.ts to cap what it primes and borrows.
|
|
67
|
+
*/
|
|
52
68
|
ceiling: i32 = 0
|
|
53
69
|
/** How many buffers the pool has made: its high-water mark, since it never frees one. */
|
|
54
70
|
made: i32 = 0
|
|
@@ -59,6 +75,10 @@ export class BufferPool {
|
|
|
59
75
|
* `"Http2TlsServer.output"`). Panics, naming the pool, unless `size` is
|
|
60
76
|
* above 0 and `ceiling` at least 1: a pool of neither makes a buffer
|
|
61
77
|
* nobody can use or none at all.
|
|
78
|
+
*
|
|
79
|
+
* @param name the label every panic of this pool prints, to say whose buffer leaked
|
|
80
|
+
* @param size how many bytes each buffer holds, the same for every buffer the pool lends
|
|
81
|
+
* @param ceiling the most buffers the pool may ever make, sized to what every slot holding one at once would need
|
|
62
82
|
*/
|
|
63
83
|
constructor(name: string, size: i32, ceiling: i32) {
|
|
64
84
|
if (size <= 0) {
|
|
@@ -82,6 +102,8 @@ export class BufferPool {
|
|
|
82
102
|
* Panics when all `ceiling` buffers are lent: the ceiling is what every
|
|
83
103
|
* slot holding a buffer at once would need, so one more means a buffer was
|
|
84
104
|
* never given back.
|
|
105
|
+
*
|
|
106
|
+
* @returns a buffer of `size` bytes the caller now borrows until it hands it back through `give` or `giveSecret`
|
|
85
107
|
*/
|
|
86
108
|
take(): u8[] {
|
|
87
109
|
if (this.free.length > 0) {
|
|
@@ -104,6 +126,9 @@ export class BufferPool {
|
|
|
104
126
|
*
|
|
105
127
|
* Panics when `buf` is not `size` bytes, when `used` is outside
|
|
106
128
|
* `0 .. size`, or when nothing is lent.
|
|
129
|
+
*
|
|
130
|
+
* @param buf the buffer `take` lent, which the pool owns again once this returns
|
|
131
|
+
* @param used how many bytes from the start of `buf` the borrower wrote, the extent that gets zeroed
|
|
107
132
|
*/
|
|
108
133
|
give(buf: u8[], used: i32): void {
|
|
109
134
|
bufferPoolCheckGive(this, buf, used)
|
|
@@ -119,6 +144,9 @@ export class BufferPool {
|
|
|
119
144
|
* above 0, and costs nothing for a buffer that was never written.
|
|
120
145
|
*
|
|
121
146
|
* Panics as `give` does.
|
|
147
|
+
*
|
|
148
|
+
* @param buf the buffer `take` lent, which held secret bytes and which the pool owns again once this returns
|
|
149
|
+
* @param used how many bytes from the start of `buf` the borrower wrote; above 0, the whole buffer is wiped
|
|
122
150
|
*/
|
|
123
151
|
giveSecret(buf: u8[], used: i32): void {
|
|
124
152
|
bufferPoolCheckGive(this, buf, used)
|
|
@@ -128,7 +156,10 @@ export class BufferPool {
|
|
|
128
156
|
this.free.push(buf)
|
|
129
157
|
}
|
|
130
158
|
|
|
131
|
-
/**
|
|
159
|
+
/**
|
|
160
|
+
* How many buffers are lent now: made, less those waiting on the free list.
|
|
161
|
+
* @returns the count of buffers borrowed and not yet given back, which is 0 when every borrower kept its promise
|
|
162
|
+
*/
|
|
132
163
|
lent(): i32 {
|
|
133
164
|
return this.made - toI32(this.free.length)
|
|
134
165
|
}
|