@amritk/nish-aarch64-linux 0.39.0 → 0.40.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 (54) hide show
  1. package/INSTALL.md +74 -129
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +112 -33
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +21 -17
  6. package/runtime/nish.h +10 -0
  7. package/runtime/page-frame.d.ts +123 -0
  8. package/runtime/page-frame.mjs +736 -0
  9. package/runtime/page-game.d.ts +132 -0
  10. package/runtime/page-game.mjs +738 -0
  11. package/runtime/runtime-aes.c +85 -8
  12. package/runtime/runtime-bytes.c +88 -4
  13. package/runtime/runtime-net.c +32 -0
  14. package/runtime/runtime-sandbox.c +55 -28
  15. package/runtime/runtime-wasm.c +422 -0
  16. package/runtime/vite.mjs +1 -1
  17. package/scripts/build.sh +15 -0
  18. package/std/README.md +73 -87
  19. package/std/array.ts +36 -3
  20. package/std/collections.ts +51 -0
  21. package/std/crypto/hmac.ts +37 -10
  22. package/std/crypto/p256.ts +1388 -2664
  23. package/std/crypto/pki.ts +116 -4
  24. package/std/crypto/sha256.ts +34 -6
  25. package/std/crypto/x509.ts +7 -0
  26. package/std/host/game.ts +273 -0
  27. package/std/input.ts +56 -0
  28. package/std/json-tokenizer.ts +325 -112
  29. package/std/json-typed.ts +43 -8
  30. package/std/json-value.ts +144 -3
  31. package/std/json.ts +63 -8
  32. package/std/net/fetch.ts +22 -4
  33. package/std/net/http-server.ts +3382 -0
  34. package/std/net/http1-client-codec.ts +6 -2
  35. package/std/net/http1-codec.ts +137 -12
  36. package/std/net/http1-server.ts +83 -29
  37. package/std/net/http2.ts +47 -14
  38. package/std/net/http3-server.ts +29 -3
  39. package/std/net/http3.ts +180 -7
  40. package/std/net/quic-recovery.ts +240 -9
  41. package/std/net/quic-stream.ts +21 -1
  42. package/std/net/quic.ts +98 -4
  43. package/std/net/router.ts +378 -31
  44. package/std/net/serve.ts +873 -65
  45. package/std/net/tls/verify.ts +19 -1
  46. package/std/net/url.ts +83 -35
  47. package/std/net/websocket.ts +74 -1
  48. package/std/net/webtransport.ts +7 -1
  49. package/std/raster.ts +837 -0
  50. package/std/release.ts +593 -0
  51. package/std/synth.ts +143 -0
  52. package/std/threads.ts +298 -0
  53. package/std/yaml-json.ts +368 -28
  54. package/std/yaml.ts +122 -27
package/std/synth.ts ADDED
@@ -0,0 +1,143 @@
1
+ /**
2
+ * `nish/synth` — short sounds made from nothing: square, triangle and noise
3
+ * voices with a pitch slide and a decaying envelope, mixed into an array of
4
+ * `f64` samples. Nothing is sampled or copied; a sound is arithmetic.
5
+ *
6
+ * A sound starts as silence of its length and gains voices one at a time,
7
+ * each added into the samples already there:
8
+ *
9
+ * import { SYNTH_SQUARE, SYNTH_TRIANGLE, synthSemitone, synthSilence, synthTone } from "nish/synth";
10
+ *
11
+ * const RATE: i32 = 22050;
12
+ * const out: f64[] = synthSilence(0.2, RATE);
13
+ * synthTone(out, RATE, 0.0, 0.2, 440.0, 220.0, SYNTH_SQUARE, 0.25, 8.0);
14
+ * synthTone(out, RATE, 0.05, 0.1, synthSemitone(440.0, 7), synthSemitone(440.0, 7), SYNTH_TRIANGLE, 0.2, 12.0);
15
+ *
16
+ * The samples are not clipped: a voice stays inside `-volume..volume`, so
17
+ * voices whose volumes sum to at most 1 stay inside `-1..1`, and what plays
18
+ * them (a WAV writer, a page's audio buffer) clips the rest. The sample rate
19
+ * is the caller's, passed to every function, so one program can make sounds
20
+ * for a 22050 Hz file and a 48000 Hz page alike.
21
+ *
22
+ * Pure: no capability, no I/O and no clock. The noise voice is a fixed
23
+ * xorshift sequence, so the same call gives the same samples every time.
24
+ */
25
+
26
+ /** A square wave: +1 for the first half of each period, -1 for the second. */
27
+ export const SYNTH_SQUARE: i32 = 0
28
+
29
+ /** A triangle wave: a straight rise from -1 to +1 and a straight fall back. */
30
+ export const SYNTH_TRIANGLE: i32 = 1
31
+
32
+ /** Noise: a fresh random level each period of the pitch, so a falling pitch rumbles. */
33
+ export const SYNTH_NOISE: i32 = 2
34
+
35
+ /** The noise voice's starting state. */
36
+ const SYNTH_NOISE_SEED: i32 = 22695477
37
+
38
+ /** A position or length in samples past this is refused rather than converted. */
39
+ const SYNTH_SAMPLE_LIMIT: f64 = 2147483647.0
40
+
41
+ /**
42
+ * `seconds` of silence: an array of zero samples, one at least.
43
+ *
44
+ * @param seconds - how long; zero, negative or NaN is one sample
45
+ * @param sampleRate - samples a second
46
+ * @returns `seconds × sampleRate` samples of 0.0, at least one
47
+ */
48
+ export const synthSilence = (seconds: f64, sampleRate: i32): f64[] => {
49
+ const want: f64 = seconds * toF64(sampleRate)
50
+ const n: i32 = want >= 1.0 && want < SYNTH_SAMPLE_LIMIT ? toI32(want) : 1
51
+ const out: f64[] = new Array<f64>(n)
52
+ out.fill(0.0)
53
+ return out
54
+ }
55
+
56
+ /** The next value of the noise voice's xorshift generator. */
57
+ const synthXorshift = (x: i32): i32 => {
58
+ let y: i32 = x ^ (x << 13)
59
+ y = y ^ (y >> 17)
60
+ return y ^ (y << 5)
61
+ }
62
+
63
+ /**
64
+ * Add a voice into `out` from `start` seconds for `length` seconds. Its pitch
65
+ * slides from `f0` to `f1` Hz on a log scale, and its loudness starts at
66
+ * `volume` and falls by a factor of e every `1 / decay` seconds. A 2 ms attack
67
+ * and a 5 ms release keep the ends from clicking. The voice stops at the end
68
+ * of `out`; it never grows the array.
69
+ *
70
+ * Nothing is added for a sample rate, start or length that is not positive
71
+ * (a start of 0 is fine), for a frequency that is not positive, or for a
72
+ * start or length too long to count in samples.
73
+ *
74
+ * @param out - the samples to add into
75
+ * @param sampleRate - samples a second that `out` is at
76
+ * @param start - where the voice begins, in seconds from the first sample
77
+ * @param length - how long the voice lasts, in seconds
78
+ * @param f0 - the starting pitch in Hz
79
+ * @param f1 - the ending pitch in Hz
80
+ * @param wave - `SYNTH_SQUARE`, `SYNTH_TRIANGLE` or `SYNTH_NOISE`; any other value is noise
81
+ * @param volume - the starting peak level; a negative level inverts the wave
82
+ * @param decay - how fast the level falls, per second; 0 holds it
83
+ */
84
+ export const synthTone = (
85
+ out: f64[],
86
+ sampleRate: i32,
87
+ start: f64,
88
+ length: f64,
89
+ f0: f64,
90
+ f1: f64,
91
+ wave: i32,
92
+ volume: f64,
93
+ decay: f64
94
+ ): void => {
95
+ const rate: f64 = toF64(sampleRate)
96
+ if (!(rate > 0.0 && start >= 0.0 && length > 0.0 && f0 > 0.0 && f1 > 0.0)) {
97
+ return
98
+ }
99
+ if (!(start * rate < SYNTH_SAMPLE_LIMIT && length * rate < SYNTH_SAMPLE_LIMIT)) {
100
+ return
101
+ }
102
+ const from: i32 = toI32(start * rate)
103
+ const n: i32 = toI32(length * rate)
104
+ const ratio: f64 = Math.log(f1 / f0)
105
+ let phase: f64 = 0.0
106
+ let noise: i32 = SYNTH_NOISE_SEED
107
+ let held: f64 = 0.0
108
+ for (let i: i32 = 0; i < n && from + i < toI32(out.length); i++) {
109
+ const t: f64 = toF64(i) / rate
110
+ const f: f64 = f0 * Math.exp((ratio * toF64(i)) / toF64(n))
111
+ const before: f64 = phase
112
+ phase = phase + f / rate
113
+ if (phase >= 1.0) {
114
+ phase = phase - Math.floor(phase)
115
+ }
116
+ let v: f64 = 0.0
117
+ if (wave === SYNTH_SQUARE) {
118
+ v = phase < 0.5 ? 1.0 : -1.0
119
+ } else if (wave === SYNTH_TRIANGLE) {
120
+ const rise: f64 = 4.0 * phase
121
+ v = phase < 0.5 ? rise - 1.0 : 3.0 - rise
122
+ } else {
123
+ if (phase < before || i === 0) {
124
+ noise = synthXorshift(noise)
125
+ held = toF64(noise & 65535) / 32767.5 - 1.0
126
+ }
127
+ v = held
128
+ }
129
+ const attack: f64 = Math.min(t / 0.002, 1.0)
130
+ const release: f64 = Math.min((length - t) / 0.005, 1.0)
131
+ out[from + i] = out[from + i] + v * volume * attack * release * Math.exp(-t * decay)
132
+ }
133
+ }
134
+
135
+ /**
136
+ * The frequency `semitones` equal-tempered steps above `base`.
137
+ *
138
+ * @param base - the starting frequency in Hz
139
+ * @param semitones - how many steps up; negative steps go down
140
+ * @returns `base × 2^(semitones / 12)`
141
+ */
142
+ export const synthSemitone = (base: f64, semitones: i32): f64 =>
143
+ base * Math.exp((toF64(semitones) * Math.log(2.0)) / 12.0)
package/std/threads.ts CHANGED
@@ -782,6 +782,304 @@ export class AtomicI64 {
782
782
  }
783
783
  }
784
784
 
785
+ /**
786
+ * `length` `i32` counters in one contiguous buffer, each of which a scope's
787
+ * tasks and a parallel body may update together (docs/LANGUAGE.md,
788
+ * "Atomic arrays"). It is `AtomicI32` with the element's index first: each
789
+ * method checks `i` as an array index is checked, so an index out of range
790
+ * stops the program, and is then one indivisible operation on that element.
791
+ * Natively the compiler emits each body itself, as a sequentially consistent
792
+ * `load atomic`, `store atomic`, `atomicrmw` or `cmpxchg` on the element
793
+ * (docs/IR_COOKBOOK.md, "Atomics"), and what is written below is the meaning,
794
+ * which is what runs under Node. `add` and `sub` wrap
795
+ * at 32 bits, as the native instruction does.
796
+ *
797
+ * Choose it over an `AtomicI32[]` when the counters are many or hot: an
798
+ * `AtomicI32[]` is one object per counter, each its own allocation, so
799
+ * neighbouring counters land on several cache lines; here they are adjacent
800
+ * `i32`s, as Rust's `[AtomicI32; N]` is. It reaches a task and a parallel body
801
+ * by the routes `AtomicI32` does, and is refused where it is.
802
+ */
803
+ export class AtomicI32Array {
804
+ /** How many elements the array holds, fixed when it is made. */
805
+ readonly length: i32
806
+ /** The elements; named only by this class's methods, which the compiler emits. */
807
+ private values: i32[]
808
+
809
+ /**
810
+ * An array of `length` elements, each `0`.
811
+ *
812
+ * @param length how many elements; a negative one stops the program, as
813
+ * `new Array` of one does.
814
+ */
815
+ constructor(length: i32) {
816
+ this.length = length
817
+ this.values = new Array<i32>(length)
818
+ this.values.fill(0)
819
+ }
820
+
821
+ /**
822
+ * Element `i`.
823
+ *
824
+ * @param i the element's index, from `0` to `length - 1`.
825
+ * @returns the element at the moment of the read.
826
+ */
827
+ load(i: i32): i32 {
828
+ return this.values[i]
829
+ }
830
+
831
+ /**
832
+ * Sets element `i` to `value`.
833
+ *
834
+ * @param i the element's index, from `0` to `length - 1`.
835
+ * @param value the new value.
836
+ */
837
+ store(i: i32, value: i32): void {
838
+ this.values[i] = value
839
+ }
840
+
841
+ /**
842
+ * Adds `delta` to element `i`, wrapping at 32 bits, and answers the
843
+ * element before.
844
+ *
845
+ * @param i the element's index, from `0` to `length - 1`.
846
+ * @param delta how much to add; a negative one subtracts.
847
+ * @returns the element just before the addition.
848
+ */
849
+ add(i: i32, delta: i32): i32 {
850
+ const old = this.values[i]
851
+ this.values[i] = wrappingAdd(old, delta)
852
+ return old
853
+ }
854
+
855
+ /**
856
+ * Subtracts `delta` from element `i`, wrapping at 32 bits, and answers
857
+ * the element before.
858
+ *
859
+ * @param i the element's index, from `0` to `length - 1`.
860
+ * @param delta how much to subtract; a negative one adds.
861
+ * @returns the element just before the subtraction.
862
+ */
863
+ sub(i: i32, delta: i32): i32 {
864
+ const old = this.values[i]
865
+ this.values[i] = wrappingSub(old, delta)
866
+ return old
867
+ }
868
+
869
+ /**
870
+ * Keeps the smaller of element `i` and `value`, and answers the element
871
+ * before.
872
+ *
873
+ * @param i the element's index, from `0` to `length - 1`.
874
+ * @param value the candidate, stored only when it is below the element.
875
+ * @returns the element just before the update.
876
+ */
877
+ min(i: i32, value: i32): i32 {
878
+ const old = this.values[i]
879
+ if (value < old) {
880
+ this.values[i] = value
881
+ }
882
+ return old
883
+ }
884
+
885
+ /**
886
+ * Keeps the larger of element `i` and `value`, and answers the element
887
+ * before.
888
+ *
889
+ * @param i the element's index, from `0` to `length - 1`.
890
+ * @param value the candidate, stored only when it is above the element.
891
+ * @returns the element just before the update.
892
+ */
893
+ max(i: i32, value: i32): i32 {
894
+ const old = this.values[i]
895
+ if (value > old) {
896
+ this.values[i] = value
897
+ }
898
+ return old
899
+ }
900
+
901
+ /**
902
+ * Sets element `i` to `value` and answers the element before.
903
+ *
904
+ * @param i the element's index, from `0` to `length - 1`.
905
+ * @param value the new value.
906
+ * @returns the value it replaced.
907
+ */
908
+ exchange(i: i32, value: i32): i32 {
909
+ const old = this.values[i]
910
+ this.values[i] = value
911
+ return old
912
+ }
913
+
914
+ /**
915
+ * Sets element `i` to `desired` if it is `expected`, and answers the value
916
+ * it saw.
917
+ *
918
+ * @param i the element's index, from `0` to `length - 1`.
919
+ * @param expected the value the update requires.
920
+ * @param desired the value stored when the element is `expected`.
921
+ * @returns the element seen before the update: `expected` exactly when the
922
+ * update happened.
923
+ */
924
+ compareExchange(i: i32, expected: i32, desired: i32): i32 {
925
+ const old = this.values[i]
926
+ if (old === expected) {
927
+ this.values[i] = desired
928
+ }
929
+ return old
930
+ }
931
+ }
932
+
933
+ /**
934
+ * `length` `i64` counters in one contiguous buffer, each of which a scope's
935
+ * tasks and a parallel body may update together (docs/LANGUAGE.md,
936
+ * "Atomic arrays"). It is `AtomicI64` with the element's index first: each
937
+ * method checks `i` as an array index is checked, so an index out of range
938
+ * stops the program, and is then one indivisible operation on that element.
939
+ * Natively the compiler emits each body itself, as a sequentially consistent
940
+ * `load atomic`, `store atomic`, `atomicrmw` or `cmpxchg` on the element
941
+ * (docs/IR_COOKBOOK.md, "Atomics"), and what is written below is the meaning,
942
+ * which is what runs under Node. Node holds each
943
+ * element as a `number`, so `add` and `sub` are `+` and `-` there, and agree
944
+ * with native while values stay within ±2^53; wrapping at 2^63 is native-only,
945
+ * as it is for `AtomicI64`.
946
+ *
947
+ * Choose it over an `AtomicI64[]` when the counters are many or hot: an
948
+ * `AtomicI64[]` is one object per counter, each its own allocation, so
949
+ * neighbouring counters land on several cache lines; here they are adjacent
950
+ * `i64`s, as Rust's `[AtomicI64; N]` is. It reaches a task and a parallel body
951
+ * by the routes `AtomicI64` does, and is refused where it is.
952
+ */
953
+ export class AtomicI64Array {
954
+ /** How many elements the array holds, fixed when it is made. */
955
+ readonly length: i32
956
+ /** The elements; named only by this class's methods, which the compiler emits. */
957
+ private values: i64[]
958
+
959
+ /**
960
+ * An array of `length` elements, each `0`.
961
+ *
962
+ * @param length how many elements; a negative one stops the program, as
963
+ * `new Array` of one does.
964
+ */
965
+ constructor(length: i32) {
966
+ this.length = length
967
+ this.values = new Array<i64>(length)
968
+ this.values.fill(0)
969
+ }
970
+
971
+ /**
972
+ * Element `i`.
973
+ *
974
+ * @param i the element's index, from `0` to `length - 1`.
975
+ * @returns the element at the moment of the read.
976
+ */
977
+ load(i: i32): i64 {
978
+ return this.values[i]
979
+ }
980
+
981
+ /**
982
+ * Sets element `i` to `value`.
983
+ *
984
+ * @param i the element's index, from `0` to `length - 1`.
985
+ * @param value the new value.
986
+ */
987
+ store(i: i32, value: i64): void {
988
+ this.values[i] = value
989
+ }
990
+
991
+ /**
992
+ * Adds `delta` to element `i` and answers the element before; natively
993
+ * the sum wraps at 64 bits.
994
+ *
995
+ * @param i the element's index, from `0` to `length - 1`.
996
+ * @param delta how much to add; a negative one subtracts.
997
+ * @returns the element just before the addition.
998
+ */
999
+ add(i: i32, delta: i64): i64 {
1000
+ const old = this.values[i]
1001
+ this.values[i] = old + delta
1002
+ return old
1003
+ }
1004
+
1005
+ /**
1006
+ * Subtracts `delta` from element `i` and answers the element before;
1007
+ * natively the difference wraps at 64 bits.
1008
+ *
1009
+ * @param i the element's index, from `0` to `length - 1`.
1010
+ * @param delta how much to subtract; a negative one adds.
1011
+ * @returns the element just before the subtraction.
1012
+ */
1013
+ sub(i: i32, delta: i64): i64 {
1014
+ const old = this.values[i]
1015
+ this.values[i] = old - delta
1016
+ return old
1017
+ }
1018
+
1019
+ /**
1020
+ * Keeps the smaller of element `i` and `value`, and answers the element
1021
+ * before.
1022
+ *
1023
+ * @param i the element's index, from `0` to `length - 1`.
1024
+ * @param value the candidate, stored only when it is below the element.
1025
+ * @returns the element just before the update.
1026
+ */
1027
+ min(i: i32, value: i64): i64 {
1028
+ const old = this.values[i]
1029
+ if (value < old) {
1030
+ this.values[i] = value
1031
+ }
1032
+ return old
1033
+ }
1034
+
1035
+ /**
1036
+ * Keeps the larger of element `i` and `value`, and answers the element
1037
+ * before.
1038
+ *
1039
+ * @param i the element's index, from `0` to `length - 1`.
1040
+ * @param value the candidate, stored only when it is above the element.
1041
+ * @returns the element just before the update.
1042
+ */
1043
+ max(i: i32, value: i64): i64 {
1044
+ const old = this.values[i]
1045
+ if (value > old) {
1046
+ this.values[i] = value
1047
+ }
1048
+ return old
1049
+ }
1050
+
1051
+ /**
1052
+ * Sets element `i` to `value` and answers the element before.
1053
+ *
1054
+ * @param i the element's index, from `0` to `length - 1`.
1055
+ * @param value the new value.
1056
+ * @returns the value it replaced.
1057
+ */
1058
+ exchange(i: i32, value: i64): i64 {
1059
+ const old = this.values[i]
1060
+ this.values[i] = value
1061
+ return old
1062
+ }
1063
+
1064
+ /**
1065
+ * Sets element `i` to `desired` if it is `expected`, and answers the value
1066
+ * it saw.
1067
+ *
1068
+ * @param i the element's index, from `0` to `length - 1`.
1069
+ * @param expected the value the update requires.
1070
+ * @param desired the value stored when the element is `expected`.
1071
+ * @returns the element seen before the update: `expected` exactly when the
1072
+ * update happened.
1073
+ */
1074
+ compareExchange(i: i32, expected: i64, desired: i64): i64 {
1075
+ const old = this.values[i]
1076
+ if (old === expected) {
1077
+ this.values[i] = desired
1078
+ }
1079
+ return old
1080
+ }
1081
+ }
1082
+
785
1083
  /**
786
1084
  * A channel of scalars or strings between a scope's tasks (docs/wp29-thread-surface.md
787
1085
  * §4.4); a string is copied out of the sender's arena at `send` and into the