@amritk/nish-aarch64-linux 0.34.1 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +22 -13
- package/bin/nish +0 -0
- package/docs/AI.md +86 -16
- package/package.json +1 -1
- package/runtime/async.d.mts +49 -0
- package/runtime/async.mjs +27 -0
- package/runtime/nish.d.ts +4681 -427
- package/runtime/nish.h +6 -2
- package/runtime/nish.mjs +4 -0
- package/runtime/runtime-net.c +22 -0
- package/runtime/runtime-shm.c +93 -2
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/README.md +131 -131
- package/std/base64.ts +34 -5
- package/std/bytes.ts +258 -42
- 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 +80 -9
- package/std/crypto/base64url.ts +22 -0
- package/std/crypto/bcrypt-batch.ts +88 -21
- package/std/crypto/bcrypt.ts +60 -0
- package/std/crypto/chacha20poly1305.ts +53 -0
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +47 -0
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +235 -31
- package/std/crypto/jwt.ts +192 -63
- package/std/crypto/p256.ts +159 -16
- package/std/crypto/p384.ts +28 -3
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +81 -15
- package/std/crypto/rsa.ts +33 -3
- 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 +152 -14
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +67 -7
- package/std/json-equal.ts +19 -0
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +64 -14
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +100 -29
- package/std/json-typed.ts +713 -66
- package/std/json-value.ts +400 -124
- package/std/json.ts +138 -10
- 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 +151 -61
- package/std/net/handler.ts +278 -80
- package/std/net/hpack.ts +262 -52
- package/std/net/http-compress.ts +125 -15
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +100 -19
- package/std/net/http1-codec.ts +297 -77
- package/std/net/http1-server.ts +430 -94
- package/std/net/http2-frame.ts +272 -35
- package/std/net/http2-tls.ts +135 -34
- package/std/net/http2.ts +247 -106
- package/std/net/http3-frame.ts +154 -17
- package/std/net/http3-server.ts +275 -101
- package/std/net/http3.ts +307 -115
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +801 -112
- package/std/net/prefork.ts +164 -31
- package/std/net/qpack.ts +105 -18
- package/std/net/quic-conn-ack.ts +59 -12
- package/std/net/quic-conn-cid.ts +124 -27
- package/std/net/quic-conn-params.ts +70 -10
- package/std/net/quic-datagram.ts +43 -3
- package/std/net/quic-frame.ts +286 -12
- package/std/net/quic-listener.ts +101 -32
- package/std/net/quic-packet.ts +302 -21
- package/std/net/quic-recovery.ts +264 -52
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +1124 -201
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +166 -51
- package/std/net/tls/codec.ts +413 -56
- package/std/net/tls/record-client.ts +110 -55
- package/std/net/tls/record-server.ts +159 -59
- package/std/net/tls/record.ts +109 -12
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +159 -40
- package/std/net/tls/verify.ts +49 -3
- package/std/net/tls-tcp.ts +250 -45
- package/std/net/tls.ts +221 -59
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +97 -13
- package/std/net/websocket.ts +122 -12
- package/std/net/webtransport.ts +220 -89
- package/std/pair.ts +10 -2
- package/std/random.ts +34 -9
- package/std/regex.ts +352 -198
- 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 +430 -40
- package/std/sync/shm.ts +222 -15
- package/std/term-io.ts +137 -64
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +66 -25
- package/std/term-rate.ts +128 -57
- package/std/term.ts +332 -121
- package/std/testing.ts +130 -32
- package/std/text.ts +73 -7
- package/std/threads.ts +229 -41
- package/std/yaml-json.ts +94 -82
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +24 -16
- package/std/yaml.ts +764 -384
package/std/sync/shm.ts
CHANGED
|
@@ -90,6 +90,17 @@ declare function nishshm_wait_i32(h: i32, at: i32, expected: i32, timeoutMs: i32
|
|
|
90
90
|
declare function nishshm_wake(h: i32, at: i32, count: i32): i32
|
|
91
91
|
declare function strerror(errnum: i32): string
|
|
92
92
|
|
|
93
|
+
// The window intrinsics (`src/shm-window.ts`): each body is a call of itself,
|
|
94
|
+
// which the compiler replaces with `nishshm_lend` or `nishshm_give_back`. They
|
|
95
|
+
// are not `declare function`s because C would be handed `data` and not the
|
|
96
|
+
// `nish_array` header, and could not answer a `u8[]`.
|
|
97
|
+
|
|
98
|
+
/** `nishshm_lend`: a `u8[]` whose bytes are the region's `[at, at + len)`. */
|
|
99
|
+
const shmLend = (h: i32, at: i32, len: i32, reuse: u8[] | null): u8[] => shmLend(h, at, len, reuse)
|
|
100
|
+
|
|
101
|
+
/** `nishshm_give_back`: empties `window` and answers the length it had. */
|
|
102
|
+
const shmGiveBack = (h: i32, window: u8[]): i32 => shmGiveBack(h, window)
|
|
103
|
+
|
|
93
104
|
/** No ordering beyond the access itself being whole. */
|
|
94
105
|
export const RELAXED: i32 = 0
|
|
95
106
|
/** A load that sees everything written before the `RELEASE` store it reads. */
|
|
@@ -128,6 +139,12 @@ export class ShmError {
|
|
|
128
139
|
/** What it means, such as `File exists`. */
|
|
129
140
|
message: string
|
|
130
141
|
|
|
142
|
+
/**
|
|
143
|
+
* The error for one errno, with its text: `strerror`'s, except that 95
|
|
144
|
+
* (`SHM_UNSUPPORTED` negated) reads "not supported on this platform".
|
|
145
|
+
* @param code the errno the runtime answered, positive (the negation of a
|
|
146
|
+
* failed call's result), such as `SHM_EXISTS`
|
|
147
|
+
*/
|
|
131
148
|
constructor(code: i32) {
|
|
132
149
|
this.code = code
|
|
133
150
|
this.message = code === 95 ? "not supported on this platform" : strerror(code)
|
|
@@ -145,6 +162,15 @@ const shmDone = (rc: i32): Result<void, ShmError> => {
|
|
|
145
162
|
/**
|
|
146
163
|
* Creates the region `name`, `bytes` long and zeroed, mode 0600, and maps it.
|
|
147
164
|
* `SHM_EXISTS` when the name is taken: unlink a stale one first.
|
|
165
|
+
* @param name the region's name, 1 to 30 of `A-Z a-z 0-9 . _ -`, not
|
|
166
|
+
* starting with `.`; anything else is `SHM_INVALID`
|
|
167
|
+
* @param bytes the region's length, from 1 to 1 GiB; outside that is
|
|
168
|
+
* `SHM_INVALID`
|
|
169
|
+
* @returns a new `SharedRegion` mapping the fresh region, which this thread
|
|
170
|
+
* alone can use and which stays mapped until its `close` or the process
|
|
171
|
+
* ends; or the `ShmError`: `SHM_EXISTS`, `SHM_INVALID`, `SHM_TOO_MANY`
|
|
172
|
+
* when this thread has 63 regions open, code 95 on wasi, or the errno the
|
|
173
|
+
* system gave
|
|
148
174
|
*/
|
|
149
175
|
export const createRegion = (name: string, bytes: i32): Result<SharedRegion, ShmError> => {
|
|
150
176
|
const h: i32 = nishshm_create(name, bytes)
|
|
@@ -157,6 +183,15 @@ export const createRegion = (name: string, bytes: i32): Result<SharedRegion, Shm
|
|
|
157
183
|
/**
|
|
158
184
|
* Maps the region `name` that some process of this user created. Its size is
|
|
159
185
|
* the region's (on Darwin, rounded up to a page).
|
|
186
|
+
* @param name the name the region was created under, with `createRegion`'s
|
|
187
|
+
* rules (`SHM_INVALID` otherwise)
|
|
188
|
+
* @returns a new `SharedRegion` on the same bytes every other mapping of the
|
|
189
|
+
* name sees, which this thread alone can use until its `close`; or the
|
|
190
|
+
* `ShmError`: `SHM_NOT_FOUND`, `SHM_ACCESS` for a region another user owns,
|
|
191
|
+
* that others may read or write, or (on Linux) that is not a regular file
|
|
192
|
+
* or has a second hard link, `SHM_INVALID` for a bad name or a region of
|
|
193
|
+
* size 0 or over 1 GiB, `SHM_TOO_MANY`, code 95 on wasi, or the errno the
|
|
194
|
+
* system gave
|
|
160
195
|
*/
|
|
161
196
|
export const openRegion = (name: string): Result<SharedRegion, ShmError> => {
|
|
162
197
|
const h: i32 = nishshm_open(name)
|
|
@@ -169,10 +204,21 @@ export const openRegion = (name: string): Result<SharedRegion, ShmError> => {
|
|
|
169
204
|
/**
|
|
170
205
|
* Removes the name, so no process can open it again; every mapping of it
|
|
171
206
|
* stays valid until it is closed. `SHM_NOT_FOUND` when there is none.
|
|
207
|
+
* @param name the region's name, with `createRegion`'s rules (`SHM_INVALID`
|
|
208
|
+
* otherwise)
|
|
209
|
+
* @returns `Ok()` once the name is gone, or the `ShmError`: `SHM_NOT_FOUND`,
|
|
210
|
+
* `SHM_INVALID`, code 95 on wasi, or the errno the system gave
|
|
172
211
|
*/
|
|
173
212
|
export const unlinkRegion = (name: string): Result<void, ShmError> => shmDone(nishshm_unlink(name))
|
|
174
213
|
|
|
175
|
-
/**
|
|
214
|
+
/**
|
|
215
|
+
* A fence of `order`, for the accesses around it: C11's
|
|
216
|
+
* `atomic_thread_fence`, so it orders the calling thread's accesses, to
|
|
217
|
+
* regions and to its own memory, exactly as that fence does; `RELAXED` is no
|
|
218
|
+
* fence at all.
|
|
219
|
+
* @param order `RELAXED`, `ACQUIRE`, `RELEASE`, `ACQ_REL` or `SEQ_CST`; any
|
|
220
|
+
* other value panics
|
|
221
|
+
*/
|
|
176
222
|
export const fence = (order: i32): void => {
|
|
177
223
|
nishshm_fence(order)
|
|
178
224
|
}
|
|
@@ -183,33 +229,72 @@ export const fence = (order: i32): void => {
|
|
|
183
229
|
*/
|
|
184
230
|
export class SharedRegion {
|
|
185
231
|
/** The runtime's handle; it names nothing after `close`. */
|
|
186
|
-
handle: i32
|
|
232
|
+
private handle: i32
|
|
187
233
|
|
|
234
|
+
/**
|
|
235
|
+
* Wraps a handle the runtime answered. A program gets a region from
|
|
236
|
+
* `createRegion` or `openRegion` instead; a handle that names no region
|
|
237
|
+
* open on this thread makes every access panic, and `close` answer code 9.
|
|
238
|
+
* @param handle the runtime's handle for a mapped region, as
|
|
239
|
+
* `nishshm_create` or `nishshm_open` answered it
|
|
240
|
+
*/
|
|
188
241
|
constructor(handle: i32) {
|
|
189
242
|
this.handle = handle
|
|
190
243
|
}
|
|
191
244
|
|
|
192
|
-
/**
|
|
245
|
+
/**
|
|
246
|
+
* The region's length in bytes.
|
|
247
|
+
* @returns the mapped length: what `createRegion` was given, or what
|
|
248
|
+
* `openRegion` found (on Darwin, rounded up to a page)
|
|
249
|
+
*/
|
|
193
250
|
size(): i32 {
|
|
194
251
|
return nishshm_size(this.handle)
|
|
195
252
|
}
|
|
196
253
|
|
|
197
|
-
/**
|
|
254
|
+
/**
|
|
255
|
+
* Loads the 32-bit slot at `at`, one atomic load.
|
|
256
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
257
|
+
* 4 with the whole slot inside the region; anything else panics
|
|
258
|
+
* @param order `RELAXED`, `ACQUIRE` or `SEQ_CST`; another panics
|
|
259
|
+
* @returns the value the slot held
|
|
260
|
+
*/
|
|
198
261
|
loadI32(at: i32, order: i32): i32 {
|
|
199
262
|
return nishshm_load_i32(this.handle, at, order)
|
|
200
263
|
}
|
|
201
264
|
|
|
202
|
-
/**
|
|
265
|
+
/**
|
|
266
|
+
* Sets the 32-bit slot at `at` to `value`, one atomic store.
|
|
267
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
268
|
+
* 4 with the whole slot inside the region; anything else panics
|
|
269
|
+
* @param value what the slot holds after
|
|
270
|
+
* @param order `RELAXED`, `RELEASE` or `SEQ_CST`; another panics
|
|
271
|
+
*/
|
|
203
272
|
storeI32(at: i32, value: i32, order: i32): void {
|
|
204
273
|
nishshm_store_i32(this.handle, at, value, order)
|
|
205
274
|
}
|
|
206
275
|
|
|
207
|
-
/**
|
|
276
|
+
/**
|
|
277
|
+
* Adds `delta` to the 32-bit slot at `at`, wrapping, and answers the value
|
|
278
|
+
* before, as one atomic read-modify-write.
|
|
279
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
280
|
+
* 4 with the whole slot inside the region; anything else panics
|
|
281
|
+
* @param delta what is added; negative subtracts
|
|
282
|
+
* @param order any of the five orderings; another value panics
|
|
283
|
+
* @returns the value the slot held just before the add
|
|
284
|
+
*/
|
|
208
285
|
addI32(at: i32, delta: i32, order: i32): i32 {
|
|
209
286
|
return nishshm_add_i32(this.handle, at, delta, order)
|
|
210
287
|
}
|
|
211
288
|
|
|
212
|
-
/**
|
|
289
|
+
/**
|
|
290
|
+
* Sets the 32-bit slot at `at` to `value` and answers the value before, as
|
|
291
|
+
* one atomic read-modify-write.
|
|
292
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
293
|
+
* 4 with the whole slot inside the region; anything else panics
|
|
294
|
+
* @param value what the slot holds after
|
|
295
|
+
* @param order any of the five orderings; another value panics
|
|
296
|
+
* @returns the value the slot held just before
|
|
297
|
+
*/
|
|
213
298
|
exchangeI32(at: i32, value: i32, order: i32): i32 {
|
|
214
299
|
return nishshm_exchange_i32(this.handle, at, value, order)
|
|
215
300
|
}
|
|
@@ -217,27 +302,65 @@ export class SharedRegion {
|
|
|
217
302
|
/**
|
|
218
303
|
* Sets the 32-bit slot at `at` to `desired` if it holds `expected`, and
|
|
219
304
|
* answers what it held: the exchange happened when that is `expected`.
|
|
305
|
+
* It never fails spuriously (a strong compare-exchange).
|
|
306
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
307
|
+
* 4 with the whole slot inside the region; anything else panics
|
|
308
|
+
* @param expected the value the slot must hold for the store to happen
|
|
309
|
+
* @param desired what the slot holds after, when it held `expected`
|
|
310
|
+
* @param order the ordering of a successful exchange, any of the five
|
|
311
|
+
* (another value panics); a failed one is a load ordered as C++ derives
|
|
312
|
+
* it (`ACQ_REL` fails `ACQUIRE`, `RELEASE` fails `RELAXED`, the others
|
|
313
|
+
* fail as themselves)
|
|
314
|
+
* @returns what the slot held: `expected` when the exchange happened
|
|
220
315
|
*/
|
|
221
316
|
compareExchangeI32(at: i32, expected: i32, desired: i32, order: i32): i32 {
|
|
222
317
|
return nishshm_cas_i32(this.handle, at, expected, desired, order)
|
|
223
318
|
}
|
|
224
319
|
|
|
225
|
-
/**
|
|
320
|
+
/**
|
|
321
|
+
* Loads the 64-bit slot at `at`, one atomic load.
|
|
322
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
323
|
+
* 8 with the whole slot inside the region; anything else panics
|
|
324
|
+
* @param order `RELAXED`, `ACQUIRE` or `SEQ_CST`; another panics
|
|
325
|
+
* @returns the value the slot held
|
|
326
|
+
*/
|
|
226
327
|
loadI64(at: i32, order: i32): i64 {
|
|
227
328
|
return nishshm_load_i64(this.handle, at, order)
|
|
228
329
|
}
|
|
229
330
|
|
|
230
|
-
/**
|
|
331
|
+
/**
|
|
332
|
+
* Sets the 64-bit slot at `at` to `value`, one atomic store.
|
|
333
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
334
|
+
* 8 with the whole slot inside the region; anything else panics
|
|
335
|
+
* @param value what the slot holds after
|
|
336
|
+
* @param order `RELAXED`, `RELEASE` or `SEQ_CST`; another panics
|
|
337
|
+
*/
|
|
231
338
|
storeI64(at: i32, value: i64, order: i32): void {
|
|
232
339
|
nishshm_store_i64(this.handle, at, value, order)
|
|
233
340
|
}
|
|
234
341
|
|
|
235
|
-
/**
|
|
342
|
+
/**
|
|
343
|
+
* Adds `delta` to the 64-bit slot at `at`, wrapping, and answers the value
|
|
344
|
+
* before, as one atomic read-modify-write.
|
|
345
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
346
|
+
* 8 with the whole slot inside the region; anything else panics
|
|
347
|
+
* @param delta what is added; negative subtracts
|
|
348
|
+
* @param order any of the five orderings; another value panics
|
|
349
|
+
* @returns the value the slot held just before the add
|
|
350
|
+
*/
|
|
236
351
|
addI64(at: i32, delta: i64, order: i32): i64 {
|
|
237
352
|
return nishshm_add_i64(this.handle, at, delta, order)
|
|
238
353
|
}
|
|
239
354
|
|
|
240
|
-
/**
|
|
355
|
+
/**
|
|
356
|
+
* Sets the 64-bit slot at `at` to `value` and answers the value before, as
|
|
357
|
+
* one atomic read-modify-write.
|
|
358
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
359
|
+
* 8 with the whole slot inside the region; anything else panics
|
|
360
|
+
* @param value what the slot holds after
|
|
361
|
+
* @param order any of the five orderings; another value panics
|
|
362
|
+
* @returns the value the slot held just before
|
|
363
|
+
*/
|
|
241
364
|
exchangeI64(at: i32, value: i64, order: i32): i64 {
|
|
242
365
|
return nishshm_exchange_i64(this.handle, at, value, order)
|
|
243
366
|
}
|
|
@@ -245,17 +368,45 @@ export class SharedRegion {
|
|
|
245
368
|
/**
|
|
246
369
|
* Sets the 64-bit slot at `at` to `desired` if it holds `expected`, and
|
|
247
370
|
* answers what it held: the exchange happened when that is `expected`.
|
|
371
|
+
* It never fails spuriously (a strong compare-exchange).
|
|
372
|
+
* @param at the slot's byte offset from the region's start, a multiple of
|
|
373
|
+
* 8 with the whole slot inside the region; anything else panics
|
|
374
|
+
* @param expected the value the slot must hold for the store to happen
|
|
375
|
+
* @param desired what the slot holds after, when it held `expected`
|
|
376
|
+
* @param order the ordering of a successful exchange, any of the five
|
|
377
|
+
* (another value panics); a failed one is a load ordered as C++ derives
|
|
378
|
+
* it (`ACQ_REL` fails `ACQUIRE`, `RELEASE` fails `RELAXED`, the others
|
|
379
|
+
* fail as themselves)
|
|
380
|
+
* @returns what the slot held: `expected` when the exchange happened
|
|
248
381
|
*/
|
|
249
382
|
compareExchangeI64(at: i32, expected: i64, desired: i64, order: i32): i64 {
|
|
250
383
|
return nishshm_cas_i64(this.handle, at, expected, desired, order)
|
|
251
384
|
}
|
|
252
385
|
|
|
253
|
-
/**
|
|
386
|
+
/**
|
|
387
|
+
* Copies `len` bytes of the region at `at` into `dst[off, off + len)`. A
|
|
388
|
+
* plain copy, not atomic: a writer in another process racing it can tear
|
|
389
|
+
* what it reads, and only the atomics around it order it.
|
|
390
|
+
* @param at where in the region the copy starts, in bytes
|
|
391
|
+
* @param dst the buffer the bytes land in
|
|
392
|
+
* @param off where in `dst` the first byte goes
|
|
393
|
+
* @param len how many bytes to copy; the range must fit both the region from
|
|
394
|
+
* `at` and `dst` from `off`, or it panics
|
|
395
|
+
*/
|
|
254
396
|
read(at: i32, dst: u8[], off: i32, len: i32): void {
|
|
255
397
|
nishshm_read(this.handle, at, dst, toU64(dst.length), toU64(off), toU64(len))
|
|
256
398
|
}
|
|
257
399
|
|
|
258
|
-
/**
|
|
400
|
+
/**
|
|
401
|
+
* Copies `src[off, off + len)` into the region at `at`. A plain copy, not
|
|
402
|
+
* atomic: publish it to another process with a `RELEASE` (or `SEQ_CST`)
|
|
403
|
+
* store after it.
|
|
404
|
+
* @param at where in the region the first byte goes
|
|
405
|
+
* @param src the bytes to copy
|
|
406
|
+
* @param off where in `src` the copy starts
|
|
407
|
+
* @param len how many bytes to copy; the range must fit both `src` from `off`
|
|
408
|
+
* and the region from `at`, or it panics
|
|
409
|
+
*/
|
|
259
410
|
write(at: i32, src: readonly u8[], off: i32, len: i32): void {
|
|
260
411
|
nishshm_write(this.handle, at, src, toU64(src.length), toU64(off), toU64(len))
|
|
261
412
|
}
|
|
@@ -263,7 +414,17 @@ export class SharedRegion {
|
|
|
263
414
|
/**
|
|
264
415
|
* Sleeps while the 32-bit slot at `at` holds `expected`, at most
|
|
265
416
|
* `timeoutMs` (negative: no limit): `SHM_WOKEN`, `SHM_NOT_EQUAL`,
|
|
266
|
-
* `SHM_TIMED_OUT`, or `SHM_UNSUPPORTED` off Linux.
|
|
417
|
+
* `SHM_TIMED_OUT`, or `SHM_UNSUPPORTED` off Linux. It promises no
|
|
418
|
+
* ordering: load the slot again, with the ordering the caller needs, after
|
|
419
|
+
* it returns.
|
|
420
|
+
* @param at the 32-bit slot's byte offset, a multiple of 4 inside the
|
|
421
|
+
* region; anything else panics, off Linux too
|
|
422
|
+
* @param expected the value the slot must still hold for the call to sleep
|
|
423
|
+
* @param timeoutMs the longest sleep, in milliseconds; negative for no limit
|
|
424
|
+
* @returns `SHM_WOKEN` when woken, interrupted by a signal or for no reason
|
|
425
|
+
* the caller can see; `SHM_NOT_EQUAL` when the slot did not hold
|
|
426
|
+
* `expected`; `SHM_TIMED_OUT`; `SHM_UNSUPPORTED` off Linux; or another
|
|
427
|
+
* negated errno the futex gave
|
|
267
428
|
*/
|
|
268
429
|
waitI32(at: i32, expected: i32, timeoutMs: i32): i32 {
|
|
269
430
|
return nishshm_wait_i32(this.handle, at, expected, timeoutMs)
|
|
@@ -272,12 +433,58 @@ export class SharedRegion {
|
|
|
272
433
|
/**
|
|
273
434
|
* Wakes at most `count` of the processes sleeping on the 32-bit slot at
|
|
274
435
|
* `at`: how many it woke, or `SHM_UNSUPPORTED` off Linux.
|
|
436
|
+
* @param at the 32-bit slot's byte offset, a multiple of 4 inside the
|
|
437
|
+
* region; anything else panics, off Linux too
|
|
438
|
+
* @param count the most sleepers to wake; negative wakes none
|
|
439
|
+
* @returns how many it woke, in any process; `SHM_UNSUPPORTED` off Linux, or
|
|
440
|
+
* another negated errno the futex gave
|
|
275
441
|
*/
|
|
276
442
|
wake(at: i32, count: i32): i32 {
|
|
277
443
|
return nishshm_wake(this.handle, at, count)
|
|
278
444
|
}
|
|
279
445
|
|
|
280
|
-
/**
|
|
446
|
+
/**
|
|
447
|
+
* A `u8[]` of length `len` whose bytes are the region's `[at, at + len)`, not
|
|
448
|
+
* a copy: an index or `copyBytes` into it reads and writes the region. A range
|
|
449
|
+
* outside the region panics. `giveBack` or `close` empties it (length 0), so
|
|
450
|
+
* an index after either panics. A `push` moves it into this process's own
|
|
451
|
+
* memory, after which it is no window and `giveBack` panics. Pass a window
|
|
452
|
+
* already given back as `reuse` to lend it again without a new header: each
|
|
453
|
+
* lend without one keeps a few bytes for the life of the process.
|
|
454
|
+
* @param at where in the region the window starts, in bytes
|
|
455
|
+
* @param len the window's length in bytes; `[at, at + len)` must lie inside
|
|
456
|
+
* the region, or it panics
|
|
457
|
+
* @param reuse null for a new window, or a window this region lent and that
|
|
458
|
+
* was given back, to re-point; any other array panics
|
|
459
|
+
* @returns the window: `reuse` itself when one was passed, otherwise a new
|
|
460
|
+
* array. Its bytes are the region's, shared with every process that maps
|
|
461
|
+
* it, until `giveBack` or `close`
|
|
462
|
+
*/
|
|
463
|
+
lend(at: i32, len: i32, reuse: u8[] | null = null): u8[] {
|
|
464
|
+
return shmLend(this.handle, at, len, reuse)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Empties `window`, which this region lent, and answers the length it had.
|
|
469
|
+
* After it the array has length 0 and may be passed to `lend` as `reuse`.
|
|
470
|
+
* @param window a window this region lent and that is still lent; one given
|
|
471
|
+
* back already, lent by another region, or moved off the region by a
|
|
472
|
+
* `push` panics
|
|
473
|
+
* @returns the window's length just before (a `pop` may have shortened it)
|
|
474
|
+
*/
|
|
475
|
+
giveBack(window: u8[]): i32 {
|
|
476
|
+
return shmGiveBack(this.handle, window)
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Unmaps the region; the handle names nothing after. The name stays until
|
|
481
|
+
* `unlinkRegion`. Every window still lent from it is emptied first, so an
|
|
482
|
+
* index into one after this panics rather than reading unmapped memory (a
|
|
483
|
+
* window a `push` moved off the region is the program's own array, and is
|
|
484
|
+
* left alone); any other access through this object panics too.
|
|
485
|
+
* @returns `Ok()` once it is unmapped, or a `ShmError` of code 9 when it was
|
|
486
|
+
* closed already (or on wasi, where no region opens)
|
|
487
|
+
*/
|
|
281
488
|
close(): Result<void, ShmError> {
|
|
282
489
|
return shmDone(nishshm_close(this.handle))
|
|
283
490
|
}
|