@bytecodealliance/preview2-shim 0.17.7 → 0.17.9

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.
@@ -1,110 +1,98 @@
1
- import { fileURLToPath } from 'node:url';
2
- import { createSyncFn } from '../synckit/index.js';
1
+ import { fileURLToPath } from "node:url";
2
+ import { createSyncFn } from "../synckit/index.js";
3
3
  import {
4
- CALL_MASK,
5
- CALL_TYPE_MASK,
6
- FILE,
7
- HTTP_SERVER_INCOMING_HANDLER,
8
- HTTP,
9
- INPUT_STREAM_BLOCKING_READ,
10
- INPUT_STREAM_BLOCKING_SKIP,
11
- INPUT_STREAM_DISPOSE,
12
- INPUT_STREAM_READ,
13
- INPUT_STREAM_SKIP,
14
- INPUT_STREAM_SUBSCRIBE,
15
- OUTPUT_STREAM_BLOCKING_FLUSH,
16
- OUTPUT_STREAM_BLOCKING_SPLICE,
17
- OUTPUT_STREAM_BLOCKING_WRITE_AND_FLUSH,
18
- OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH,
19
- OUTPUT_STREAM_CHECK_WRITE,
20
- OUTPUT_STREAM_DISPOSE,
21
- OUTPUT_STREAM_FLUSH,
22
- OUTPUT_STREAM_SPLICE,
23
- OUTPUT_STREAM_SUBSCRIBE,
24
- OUTPUT_STREAM_WRITE_ZEROES,
25
- OUTPUT_STREAM_WRITE,
26
- POLL_POLL_LIST,
27
- POLL_POLLABLE_BLOCK,
28
- POLL_POLLABLE_DISPOSE,
29
- POLL_POLLABLE_READY,
30
- SOCKET_TCP,
31
- STDERR,
32
- STDIN,
33
- STDOUT,
34
- reverseMap,
35
- } from './calls.js';
36
- import nodeProcess, { exit, stderr, stdout, env } from 'node:process';
4
+ CALL_MASK,
5
+ CALL_TYPE_MASK,
6
+ FILE,
7
+ HTTP_SERVER_INCOMING_HANDLER,
8
+ HTTP,
9
+ INPUT_STREAM_BLOCKING_READ,
10
+ INPUT_STREAM_BLOCKING_SKIP,
11
+ INPUT_STREAM_DISPOSE,
12
+ INPUT_STREAM_READ,
13
+ INPUT_STREAM_SKIP,
14
+ INPUT_STREAM_SUBSCRIBE,
15
+ OUTPUT_STREAM_BLOCKING_FLUSH,
16
+ OUTPUT_STREAM_BLOCKING_SPLICE,
17
+ OUTPUT_STREAM_BLOCKING_WRITE_AND_FLUSH,
18
+ OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH,
19
+ OUTPUT_STREAM_CHECK_WRITE,
20
+ OUTPUT_STREAM_DISPOSE,
21
+ OUTPUT_STREAM_FLUSH,
22
+ OUTPUT_STREAM_SPLICE,
23
+ OUTPUT_STREAM_SUBSCRIBE,
24
+ OUTPUT_STREAM_WRITE_ZEROES,
25
+ OUTPUT_STREAM_WRITE,
26
+ POLL_POLL_LIST,
27
+ POLL_POLLABLE_BLOCK,
28
+ POLL_POLLABLE_DISPOSE,
29
+ POLL_POLLABLE_READY,
30
+ SOCKET_TCP,
31
+ STDERR,
32
+ STDIN,
33
+ STDOUT,
34
+ reverseMap,
35
+ } from "./calls.js";
36
+ import nodeProcess, { exit, stderr, stdout, env } from "node:process";
37
37
 
38
38
  const _rawDebug = nodeProcess._rawDebug || console.error.bind(console);
39
39
 
40
- const workerPath = fileURLToPath(
41
- new URL('./worker-thread.js', import.meta.url)
42
- );
40
+ const workerPath = fileURLToPath(new URL("./worker-thread.js", import.meta.url));
43
41
 
44
42
  const httpIncomingHandlers = new Map();
45
43
  export function registerIncomingHttpHandler(id, handler) {
46
- httpIncomingHandlers.set(id, handler);
44
+ httpIncomingHandlers.set(id, handler);
47
45
  }
48
46
 
49
47
  const instanceId = Math.round(Math.random() * 1000).toString();
50
48
  const DEBUG_DEFAULT = false;
51
49
  const DEBUG =
52
- env.PREVIEW2_SHIM_DEBUG === '0'
53
- ? false
54
- : env.PREVIEW2_SHIM_DEBUG === '1'
55
- ? true
56
- : DEBUG_DEFAULT;
50
+ env.PREVIEW2_SHIM_DEBUG === "0" ? false : env.PREVIEW2_SHIM_DEBUG === "1" ? true : DEBUG_DEFAULT;
57
51
 
58
52
  /**
59
53
  * @type {(call: number, id: number | null, payload: any) -> any}
60
54
  */
61
55
  export let ioCall = createSyncFn(workerPath, DEBUG, (type, id, payload) => {
62
- // 'callbacks' from the worker
63
- // ONLY happens for an http server incoming handler, and NOTHING else (not even sockets, since accept is sync!)
64
- if (type !== HTTP_SERVER_INCOMING_HANDLER) {
65
- throw new Error(
66
- 'Internal error: only incoming handler callback is permitted'
67
- );
68
- }
69
- const handler = httpIncomingHandlers.get(id);
70
- if (!handler) {
71
- throw new Error(
72
- `Internal error: no incoming handler registered for server ${id}`
73
- );
74
- }
75
- handler(payload);
56
+ // 'callbacks' from the worker
57
+ // ONLY happens for an http server incoming handler, and NOTHING else (not even sockets, since accept is sync!)
58
+ if (type !== HTTP_SERVER_INCOMING_HANDLER) {
59
+ throw new Error("Internal error: only incoming handler callback is permitted");
60
+ }
61
+ const handler = httpIncomingHandlers.get(id);
62
+ if (!handler) {
63
+ throw new Error(`Internal error: no incoming handler registered for server ${id}`);
64
+ }
65
+ handler(payload);
76
66
  });
77
67
  if (DEBUG) {
78
- const _ioCall = ioCall;
79
- ioCall = function ioCall(num, id, payload) {
80
- if (typeof id !== 'number' && id !== null) {
81
- throw new Error('id must be a number or null');
82
- }
83
- let ret;
84
- try {
85
- _rawDebug(
86
- instanceId,
87
- reverseMap[num & CALL_MASK],
88
- reverseMap[num & CALL_TYPE_MASK],
89
- id,
90
- payload
91
- );
92
- ret = _ioCall(num, id, payload);
93
- return ret;
94
- } catch (e) {
95
- ret = e;
96
- throw ret;
97
- } finally {
98
- _rawDebug(instanceId, '->', ret);
99
- }
100
- };
68
+ const _ioCall = ioCall;
69
+ ioCall = function ioCall(num, id, payload) {
70
+ if (typeof id !== "number" && id !== null) {
71
+ throw new Error("id must be a number or null");
72
+ }
73
+ let ret;
74
+ try {
75
+ _rawDebug(
76
+ instanceId,
77
+ reverseMap[num & CALL_MASK],
78
+ reverseMap[num & CALL_TYPE_MASK],
79
+ id,
80
+ payload,
81
+ );
82
+ ret = _ioCall(num, id, payload);
83
+ return ret;
84
+ } catch (e) {
85
+ ret = e;
86
+ throw ret;
87
+ } finally {
88
+ _rawDebug(instanceId, "->", ret);
89
+ }
90
+ };
101
91
  }
102
92
 
103
- const symbolDispose = Symbol.dispose || Symbol.for('dispose');
93
+ const symbolDispose = Symbol.dispose || Symbol.for("dispose");
104
94
 
105
- const finalizationRegistry = new FinalizationRegistry(
106
- (dispose) => void dispose()
107
- );
95
+ const finalizationRegistry = new FinalizationRegistry((dispose) => void dispose());
108
96
 
109
97
  const dummySymbol = Symbol();
110
98
 
@@ -116,152 +104,132 @@ const dummySymbol = Symbol();
116
104
  * @param {(number) => void} disposeFn
117
105
  */
118
106
  export function registerDispose(resource, parentResource, id, disposeFn) {
119
- // While strictly speaking all components should handle their disposal,
120
- // this acts as a last-resort to catch all missed drops through the JS GC.
121
- // Mainly for two cases - (1) components which are long lived, that get shut
122
- // down and (2) users that interface with low-level WASI APIs directly in JS
123
- // for various reasons may end up leaning on JS GC inadvertantly.
124
- function finalizer() {
125
- // This has no functional purpose other than to pin a strong reference
126
- // from the child resource's finalizer to the parent resource, to ensure
127
- // that we can never finalize a parent resource before a child resource.
128
- // This makes the generational JS GC become piecewise over child resource
129
- // graphs (generational at each resource hierarchy level at least).
130
- if (parentResource?.[dummySymbol]) {
131
- return;
132
- }
133
- disposeFn(id);
107
+ // While strictly speaking all components should handle their disposal,
108
+ // this acts as a last-resort to catch all missed drops through the JS GC.
109
+ // Mainly for two cases - (1) components which are long lived, that get shut
110
+ // down and (2) users that interface with low-level WASI APIs directly in JS
111
+ // for various reasons may end up leaning on JS GC inadvertantly.
112
+ function finalizer() {
113
+ // This has no functional purpose other than to pin a strong reference
114
+ // from the child resource's finalizer to the parent resource, to ensure
115
+ // that we can never finalize a parent resource before a child resource.
116
+ // This makes the generational JS GC become piecewise over child resource
117
+ // graphs (generational at each resource hierarchy level at least).
118
+ if (parentResource?.[dummySymbol]) {
119
+ return;
134
120
  }
135
- finalizationRegistry.register(resource, finalizer, finalizer);
136
- return finalizer;
121
+ disposeFn(id);
122
+ }
123
+ finalizationRegistry.register(resource, finalizer, finalizer);
124
+ return finalizer;
137
125
  }
138
126
 
139
127
  export function earlyDispose(finalizer) {
140
- finalizationRegistry.unregister(finalizer);
141
- finalizer();
128
+ finalizationRegistry.unregister(finalizer);
129
+ finalizer();
142
130
  }
143
131
 
144
132
  const _Error = Error;
145
133
  const IoError = class Error extends _Error {
146
- constructor(payload) {
147
- super(payload);
148
- this.payload = payload;
149
- }
150
- toDebugString() {
151
- return this.message;
152
- }
134
+ constructor(payload) {
135
+ super(payload);
136
+ this.payload = payload;
137
+ }
138
+ toDebugString() {
139
+ return this.message;
140
+ }
153
141
  };
154
142
 
155
143
  function streamIoErrorCall(call, id, payload) {
156
- try {
157
- return ioCall(call, id, payload);
158
- } catch (e) {
159
- if (e.tag === 'closed') {
160
- throw e;
161
- }
162
- if (e.tag === 'last-operation-failed') {
163
- e.val = new IoError(Object.assign(new Error(e.val.message), e.val));
164
- throw e;
165
- }
166
- // any invalid error is a trap
167
- console.trace(e);
168
- exit(1);
144
+ try {
145
+ return ioCall(call, id, payload);
146
+ } catch (e) {
147
+ if (e.tag === "closed") {
148
+ throw e;
149
+ }
150
+ if (e.tag === "last-operation-failed") {
151
+ e.val = new IoError(Object.assign(new Error(e.val.message), e.val));
152
+ throw e;
169
153
  }
154
+ // any invalid error is a trap
155
+ console.trace(e);
156
+ exit(1);
157
+ }
170
158
  }
171
159
 
172
160
  class InputStream {
173
- #id;
174
- #streamType;
175
- #finalizer;
176
- read(len) {
177
- return streamIoErrorCall(
178
- INPUT_STREAM_READ | this.#streamType,
179
- this.#id,
180
- len
181
- );
182
- }
183
- blockingRead(len) {
184
- return streamIoErrorCall(
185
- INPUT_STREAM_BLOCKING_READ | this.#streamType,
186
- this.#id,
187
- len
188
- );
189
- }
190
- skip(len) {
191
- return streamIoErrorCall(
192
- INPUT_STREAM_SKIP | this.#streamType,
193
- this.#id,
194
- len
195
- );
196
- }
197
- blockingSkip(len) {
198
- return streamIoErrorCall(
199
- INPUT_STREAM_BLOCKING_SKIP | this.#streamType,
200
- this.#id,
201
- len
202
- );
203
- }
204
- subscribe() {
205
- return pollableCreate(
206
- ioCall(INPUT_STREAM_SUBSCRIBE | this.#streamType, this.#id),
207
- this
161
+ #id;
162
+ #streamType;
163
+ #finalizer;
164
+ read(len) {
165
+ return streamIoErrorCall(INPUT_STREAM_READ | this.#streamType, this.#id, len);
166
+ }
167
+ blockingRead(len) {
168
+ return streamIoErrorCall(INPUT_STREAM_BLOCKING_READ | this.#streamType, this.#id, len);
169
+ }
170
+ skip(len) {
171
+ return streamIoErrorCall(INPUT_STREAM_SKIP | this.#streamType, this.#id, len);
172
+ }
173
+ blockingSkip(len) {
174
+ return streamIoErrorCall(INPUT_STREAM_BLOCKING_SKIP | this.#streamType, this.#id, len);
175
+ }
176
+ subscribe() {
177
+ return pollableCreate(ioCall(INPUT_STREAM_SUBSCRIBE | this.#streamType, this.#id), this);
178
+ }
179
+ static _id(stream) {
180
+ return stream.#id;
181
+ }
182
+ /**
183
+ * @param {FILE | SOCKET_TCP | STDIN | HTTP} streamType
184
+ */
185
+ static _create(streamType, id) {
186
+ const stream = new InputStream();
187
+ stream.#id = id;
188
+ stream.#streamType = streamType;
189
+ let disposeFn;
190
+ switch (streamType) {
191
+ case FILE:
192
+ disposeFn = fileInputStreamDispose;
193
+ break;
194
+ case SOCKET_TCP:
195
+ disposeFn = socketTcpInputStreamDispose;
196
+ break;
197
+ case STDIN:
198
+ disposeFn = stdinInputStreamDispose;
199
+ break;
200
+ case HTTP:
201
+ disposeFn = httpInputStreamDispose;
202
+ break;
203
+ default:
204
+ throw new Error(
205
+ "wasi-io trap: Dispose function not created for stream type " + reverseMap[streamType],
208
206
  );
209
207
  }
210
- static _id(stream) {
211
- return stream.#id;
212
- }
213
- /**
214
- * @param {FILE | SOCKET_TCP | STDIN | HTTP} streamType
215
- */
216
- static _create(streamType, id) {
217
- const stream = new InputStream();
218
- stream.#id = id;
219
- stream.#streamType = streamType;
220
- let disposeFn;
221
- switch (streamType) {
222
- case FILE:
223
- disposeFn = fileInputStreamDispose;
224
- break;
225
- case SOCKET_TCP:
226
- disposeFn = socketTcpInputStreamDispose;
227
- break;
228
- case STDIN:
229
- disposeFn = stdinInputStreamDispose;
230
- break;
231
- case HTTP:
232
- disposeFn = httpInputStreamDispose;
233
- break;
234
- default:
235
- throw new Error(
236
- 'wasi-io trap: Dispose function not created for stream type ' +
237
- reverseMap[streamType]
238
- );
239
- }
240
- stream.#finalizer = registerDispose(stream, null, id, disposeFn);
241
- return stream;
242
- }
243
- [symbolDispose]() {
244
- if (this.#finalizer) {
245
- earlyDispose(this.#finalizer);
246
- this.#finalizer = null;
247
- }
208
+ stream.#finalizer = registerDispose(stream, null, id, disposeFn);
209
+ return stream;
210
+ }
211
+ [symbolDispose]() {
212
+ if (this.#finalizer) {
213
+ earlyDispose(this.#finalizer);
214
+ this.#finalizer = null;
248
215
  }
216
+ }
249
217
  }
250
218
 
251
219
  function fileInputStreamDispose(id) {
252
- ioCall(INPUT_STREAM_DISPOSE | FILE, id, null);
220
+ ioCall(INPUT_STREAM_DISPOSE | FILE, id, null);
253
221
  }
254
222
 
255
223
  function socketTcpInputStreamDispose(id) {
256
- ioCall(INPUT_STREAM_DISPOSE | SOCKET_TCP, id, null);
224
+ ioCall(INPUT_STREAM_DISPOSE | SOCKET_TCP, id, null);
257
225
  }
258
226
 
259
227
  function stdinInputStreamDispose(id) {
260
- ioCall(INPUT_STREAM_DISPOSE | STDIN, id, null);
228
+ ioCall(INPUT_STREAM_DISPOSE | STDIN, id, null);
261
229
  }
262
230
 
263
231
  function httpInputStreamDispose(id) {
264
- ioCall(INPUT_STREAM_DISPOSE | HTTP, id, null);
232
+ ioCall(INPUT_STREAM_DISPOSE | HTTP, id, null);
265
233
  }
266
234
 
267
235
  export const inputStreamCreate = InputStream._create;
@@ -271,142 +239,119 @@ export const inputStreamId = InputStream._id;
271
239
  delete InputStream._id;
272
240
 
273
241
  class OutputStream {
274
- #id;
275
- #streamType;
276
- #finalizer;
277
- checkWrite(len) {
278
- return streamIoErrorCall(
279
- OUTPUT_STREAM_CHECK_WRITE | this.#streamType,
280
- this.#id,
281
- len
282
- );
242
+ #id;
243
+ #streamType;
244
+ #finalizer;
245
+ checkWrite(len) {
246
+ return streamIoErrorCall(OUTPUT_STREAM_CHECK_WRITE | this.#streamType, this.#id, len);
247
+ }
248
+ write(buf) {
249
+ if (this.#streamType <= STDERR) {
250
+ return this.blockingWriteAndFlush(buf);
283
251
  }
284
- write(buf) {
285
- if (this.#streamType <= STDERR) {
286
- return this.blockingWriteAndFlush(buf);
287
- }
288
- return streamIoErrorCall(
289
- OUTPUT_STREAM_WRITE | this.#streamType,
290
- this.#id,
291
- buf
292
- );
252
+ return streamIoErrorCall(OUTPUT_STREAM_WRITE | this.#streamType, this.#id, buf);
253
+ }
254
+ blockingWriteAndFlush(buf) {
255
+ if (this.#streamType <= STDERR) {
256
+ const stream = this.#streamType === STDERR ? stderr : stdout;
257
+ return void stream.write(buf);
293
258
  }
294
- blockingWriteAndFlush(buf) {
295
- if (this.#streamType <= STDERR) {
296
- const stream = this.#streamType === STDERR ? stderr : stdout;
297
- return void stream.write(buf);
298
- }
299
- return streamIoErrorCall(
300
- OUTPUT_STREAM_BLOCKING_WRITE_AND_FLUSH | this.#streamType,
301
- this.#id,
302
- buf
303
- );
304
- }
305
- flush() {
306
- return streamIoErrorCall(
307
- OUTPUT_STREAM_FLUSH | this.#streamType,
308
- this.#id
309
- );
310
- }
311
- blockingFlush() {
312
- return streamIoErrorCall(
313
- OUTPUT_STREAM_BLOCKING_FLUSH | this.#streamType,
314
- this.#id
315
- );
316
- }
317
- writeZeroes(len) {
318
- return streamIoErrorCall(
319
- OUTPUT_STREAM_WRITE_ZEROES | this.#streamType,
320
- this.#id,
321
- len
322
- );
323
- }
324
- blockingWriteZeroesAndFlush(len) {
325
- return streamIoErrorCall(
326
- OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH | this.#streamType,
327
- this.#id,
328
- len
329
- );
330
- }
331
- splice(src, len) {
332
- return streamIoErrorCall(
333
- OUTPUT_STREAM_SPLICE | this.#streamType,
334
- this.#id,
335
- { src: src.#id, len }
336
- );
337
- }
338
- blockingSplice(src, len) {
339
- return streamIoErrorCall(
340
- OUTPUT_STREAM_BLOCKING_SPLICE | this.#streamType,
341
- this.#id,
342
- { src: inputStreamId(src), len }
343
- );
344
- }
345
- subscribe() {
346
- return pollableCreate(
347
- ioCall(OUTPUT_STREAM_SUBSCRIBE | this.#streamType, this.#id)
348
- );
349
- }
350
-
351
- static _id(outputStream) {
352
- return outputStream.#id;
353
- }
354
- /**
355
- * @param {OutputStreamType} streamType
356
- * @param {any} createPayload
357
- */
358
- static _create(streamType, id) {
359
- const stream = new OutputStream();
360
- stream.#id = id;
361
- stream.#streamType = streamType;
362
- let disposeFn;
363
- switch (streamType) {
364
- case STDOUT:
365
- disposeFn = stdoutOutputStreamDispose;
366
- break;
367
- case STDERR:
368
- disposeFn = stderrOutputStreamDispose;
369
- break;
370
- case SOCKET_TCP:
371
- disposeFn = socketTcpOutputStreamDispose;
372
- break;
373
- case FILE:
374
- disposeFn = fileOutputStreamDispose;
375
- break;
376
- case HTTP:
377
- return stream;
378
- default:
379
- throw new Error(
380
- 'wasi-io trap: Dispose function not created for stream type ' +
381
- reverseMap[streamType]
382
- );
383
- }
384
- stream.#finalizer = registerDispose(stream, null, id, disposeFn);
259
+ return streamIoErrorCall(
260
+ OUTPUT_STREAM_BLOCKING_WRITE_AND_FLUSH | this.#streamType,
261
+ this.#id,
262
+ buf,
263
+ );
264
+ }
265
+ flush() {
266
+ return streamIoErrorCall(OUTPUT_STREAM_FLUSH | this.#streamType, this.#id);
267
+ }
268
+ blockingFlush() {
269
+ return streamIoErrorCall(OUTPUT_STREAM_BLOCKING_FLUSH | this.#streamType, this.#id);
270
+ }
271
+ writeZeroes(len) {
272
+ return streamIoErrorCall(OUTPUT_STREAM_WRITE_ZEROES | this.#streamType, this.#id, len);
273
+ }
274
+ blockingWriteZeroesAndFlush(len) {
275
+ return streamIoErrorCall(
276
+ OUTPUT_STREAM_BLOCKING_WRITE_ZEROES_AND_FLUSH | this.#streamType,
277
+ this.#id,
278
+ len,
279
+ );
280
+ }
281
+ splice(src, len) {
282
+ return streamIoErrorCall(OUTPUT_STREAM_SPLICE | this.#streamType, this.#id, {
283
+ src: src.#id,
284
+ len,
285
+ });
286
+ }
287
+ blockingSplice(src, len) {
288
+ return streamIoErrorCall(OUTPUT_STREAM_BLOCKING_SPLICE | this.#streamType, this.#id, {
289
+ src: inputStreamId(src),
290
+ len,
291
+ });
292
+ }
293
+ subscribe() {
294
+ return pollableCreate(ioCall(OUTPUT_STREAM_SUBSCRIBE | this.#streamType, this.#id));
295
+ }
296
+
297
+ static _id(outputStream) {
298
+ return outputStream.#id;
299
+ }
300
+ /**
301
+ * @param {OutputStreamType} streamType
302
+ * @param {any} createPayload
303
+ */
304
+ static _create(streamType, id) {
305
+ const stream = new OutputStream();
306
+ stream.#id = id;
307
+ stream.#streamType = streamType;
308
+ let disposeFn;
309
+ switch (streamType) {
310
+ case STDOUT:
311
+ disposeFn = stdoutOutputStreamDispose;
312
+ break;
313
+ case STDERR:
314
+ disposeFn = stderrOutputStreamDispose;
315
+ break;
316
+ case SOCKET_TCP:
317
+ disposeFn = socketTcpOutputStreamDispose;
318
+ break;
319
+ case FILE:
320
+ disposeFn = fileOutputStreamDispose;
321
+ break;
322
+ case HTTP:
385
323
  return stream;
324
+ default:
325
+ throw new Error(
326
+ "wasi-io trap: Dispose function not created for stream type " + reverseMap[streamType],
327
+ );
386
328
  }
387
-
388
- [symbolDispose]() {
389
- if (this.#finalizer) {
390
- earlyDispose(this.#finalizer);
391
- this.#finalizer = null;
392
- }
329
+ stream.#finalizer = registerDispose(stream, null, id, disposeFn);
330
+ return stream;
331
+ }
332
+
333
+ [symbolDispose]() {
334
+ if (this.#finalizer) {
335
+ earlyDispose(this.#finalizer);
336
+ this.#finalizer = null;
393
337
  }
338
+ }
394
339
  }
395
340
 
396
341
  function stdoutOutputStreamDispose(id) {
397
- ioCall(OUTPUT_STREAM_DISPOSE | STDOUT, id);
342
+ ioCall(OUTPUT_STREAM_DISPOSE | STDOUT, id);
398
343
  }
399
344
 
400
345
  function stderrOutputStreamDispose(id) {
401
- ioCall(OUTPUT_STREAM_DISPOSE | STDERR, id);
346
+ ioCall(OUTPUT_STREAM_DISPOSE | STDERR, id);
402
347
  }
403
348
 
404
349
  function socketTcpOutputStreamDispose(id) {
405
- ioCall(OUTPUT_STREAM_DISPOSE | SOCKET_TCP, id);
350
+ ioCall(OUTPUT_STREAM_DISPOSE | SOCKET_TCP, id);
406
351
  }
407
352
 
408
353
  function fileOutputStreamDispose(id) {
409
- ioCall(OUTPUT_STREAM_DISPOSE | FILE, id);
354
+ ioCall(OUTPUT_STREAM_DISPOSE | FILE, id);
410
355
  }
411
356
 
412
357
  export const outputStreamCreate = OutputStream._create;
@@ -420,117 +365,104 @@ export const error = { Error: IoError };
420
365
  export const streams = { InputStream, OutputStream };
421
366
 
422
367
  function pollableDispose(id) {
423
- ioCall(POLL_POLLABLE_DISPOSE, id);
368
+ ioCall(POLL_POLLABLE_DISPOSE, id);
424
369
  }
425
370
 
426
- const rep = Symbol.for('cabiRep');
371
+ const rep = Symbol.for("cabiRep");
427
372
 
428
373
  class Pollable {
429
- #finalizer;
430
- ready() {
431
- return ioCall(POLL_POLLABLE_READY, this[rep]);
432
- }
433
- block() {
434
- ioCall(POLL_POLLABLE_BLOCK, this[rep]);
435
- }
436
- static _create(id, parent) {
437
- const pollable = new Pollable();
438
- pollable[rep] = id;
439
- pollable.#finalizer = registerDispose(
440
- pollable,
441
- parent,
442
- id,
443
- pollableDispose
444
- );
445
- return pollable;
446
- }
447
- [symbolDispose]() {
448
- if (this.#finalizer && this[rep]) {
449
- earlyDispose(this.#finalizer);
450
- this.#finalizer = null;
451
- }
374
+ #finalizer;
375
+ ready() {
376
+ return ioCall(POLL_POLLABLE_READY, this[rep]);
377
+ }
378
+ block() {
379
+ ioCall(POLL_POLLABLE_BLOCK, this[rep]);
380
+ }
381
+ static _create(id, parent) {
382
+ const pollable = new Pollable();
383
+ pollable[rep] = id;
384
+ pollable.#finalizer = registerDispose(pollable, parent, id, pollableDispose);
385
+ return pollable;
386
+ }
387
+ [symbolDispose]() {
388
+ if (this.#finalizer && this[rep]) {
389
+ earlyDispose(this.#finalizer);
390
+ this.#finalizer = null;
452
391
  }
392
+ }
453
393
  }
454
394
 
455
- const cabiLowerSymbol = Symbol.for('cabiLower');
395
+ const cabiLowerSymbol = Symbol.for("cabiLower");
456
396
  const T_FLAG = 1 << 30;
457
397
 
458
- Pollable.prototype.ready[cabiLowerSymbol] = function({
459
- resourceTables: [table],
460
- }) {
461
- return function pollableReady(handle) {
462
- const rep = table[(handle << 1) + 1] & ~T_FLAG;
463
- const ready = ioCall(POLL_POLLABLE_READY, rep);
464
- return ready ? 1 : 0;
465
- };
398
+ Pollable.prototype.ready[cabiLowerSymbol] = function ({ resourceTables: [table] }) {
399
+ return function pollableReady(handle) {
400
+ const rep = table[(handle << 1) + 1] & ~T_FLAG;
401
+ const ready = ioCall(POLL_POLLABLE_READY, rep);
402
+ return ready ? 1 : 0;
403
+ };
466
404
  };
467
405
 
468
- Pollable.prototype.block[cabiLowerSymbol] = function({
469
- resourceTables: [table],
470
- }) {
471
- return function pollableBlock(handle) {
472
- const rep = table[(handle << 1) + 1] & ~T_FLAG;
473
- ioCall(POLL_POLLABLE_BLOCK, rep);
474
- };
406
+ Pollable.prototype.block[cabiLowerSymbol] = function ({ resourceTables: [table] }) {
407
+ return function pollableBlock(handle) {
408
+ const rep = table[(handle << 1) + 1] & ~T_FLAG;
409
+ ioCall(POLL_POLLABLE_BLOCK, rep);
410
+ };
475
411
  };
476
412
 
477
- Pollable[Symbol.for('cabiDispose')] = function pollableDispose(rep) {
478
- ioCall(POLL_POLLABLE_DISPOSE, rep);
413
+ Pollable[Symbol.for("cabiDispose")] = function pollableDispose(rep) {
414
+ ioCall(POLL_POLLABLE_DISPOSE, rep);
479
415
  };
480
416
 
481
417
  export const pollableCreate = Pollable._create;
482
418
  delete Pollable._create;
483
419
 
484
420
  export const poll = {
485
- Pollable,
486
- poll(list) {
487
- return ioCall(
488
- POLL_POLL_LIST,
489
- null,
490
- list.map((pollable) => pollable[rep])
491
- );
492
- },
421
+ Pollable,
422
+ poll(list) {
423
+ return ioCall(
424
+ POLL_POLL_LIST,
425
+ null,
426
+ list.map((pollable) => pollable[rep]),
427
+ );
428
+ },
493
429
  };
494
430
 
495
- poll.poll[cabiLowerSymbol] = function({
496
- memory,
497
- realloc,
498
- resourceTables: [table],
499
- }) {
500
- return function pollPollList(listPtr, len, retptr) {
501
- const handleList = new Uint32Array(memory.buffer, listPtr, len);
502
- const repList = Array(len);
503
- for (let i = 0; i < len; i++) {
504
- const handle = handleList[i];
505
- repList[i] = table[(handle << 1) + 1] & ~T_FLAG;
506
- }
507
- const result = ioCall(POLL_POLL_LIST, null, repList);
508
- const ptr = realloc(0, 0, 4, result.byteLength);
509
- const out = new Uint32Array(memory.buffer, ptr, result.length);
510
- out.set(result);
511
- const ret = new Uint32Array(memory.buffer, retptr, 2);
512
- ret[0] = ptr;
513
- ret[1] = result.length;
514
- return retptr;
515
- };
431
+ poll.poll[cabiLowerSymbol] = function ({ memory, realloc, resourceTables: [table] }) {
432
+ return function pollPollList(listPtr, len, retptr) {
433
+ const handleList = new Uint32Array(memory.buffer, listPtr, len);
434
+ const repList = Array(len);
435
+ for (let i = 0; i < len; i++) {
436
+ const handle = handleList[i];
437
+ repList[i] = table[(handle << 1) + 1] & ~T_FLAG;
438
+ }
439
+ const result = ioCall(POLL_POLL_LIST, null, repList);
440
+ const ptr = realloc(0, 0, 4, result.byteLength);
441
+ const out = new Uint32Array(memory.buffer, ptr, result.length);
442
+ out.set(result);
443
+ const ret = new Uint32Array(memory.buffer, retptr, 2);
444
+ ret[0] = ptr;
445
+ ret[1] = result.length;
446
+ return retptr;
447
+ };
516
448
  };
517
449
 
518
450
  export function createPoll(call, id, initPayload) {
519
- return pollableCreate(ioCall(call, id, initPayload));
451
+ return pollableCreate(ioCall(call, id, initPayload));
520
452
  }
521
453
 
522
454
  export function createPollLower(call, id, table) {
523
- return function(initPayload) {
524
- const rep = ioCall(call, id, initPayload);
525
- const free = table[0] & ~T_FLAG;
526
- if (free === 0) {
527
- table.push(0);
528
- table.push(rep | T_FLAG);
529
- return (table.length >> 1) - 1;
530
- }
531
- table[0] = table[free << 1];
532
- table[free << 1] = 0;
533
- table[(free << 1) + 1] = rep | T_FLAG;
534
- return free;
535
- };
455
+ return function (initPayload) {
456
+ const rep = ioCall(call, id, initPayload);
457
+ const free = table[0] & ~T_FLAG;
458
+ if (free === 0) {
459
+ table.push(0);
460
+ table.push(rep | T_FLAG);
461
+ return (table.length >> 1) - 1;
462
+ }
463
+ table[0] = table[free << 1];
464
+ table[free << 1] = 0;
465
+ table[(free << 1) + 1] = rep | T_FLAG;
466
+ return free;
467
+ };
536
468
  }