@bytecodealliance/preview2-shim 0.0.16 → 0.0.17

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,6 +1,8 @@
1
- import { createStream, getStream, dropStream } from './io.js';
1
+ import { _io } from './io.js';
2
2
  import { environment } from './cli.js';
3
3
 
4
+ const { createStream, getStream, dropStream } = _io;
5
+
4
6
  let _preopens = [[3, '/']], _rootPreopen = _preopens[0];
5
7
 
6
8
  export function _setPreopens (preopens) {
@@ -338,4 +340,4 @@ export const types = {
338
340
  }
339
341
  };
340
342
 
341
- export { types as filesystemTypes }
343
+ export { types as filesystemTypes }
@@ -8,7 +8,7 @@ import * as random from "./random.js";
8
8
  import * as sockets from "./sockets.js";
9
9
  import * as cli from "./cli.js";
10
10
 
11
- export const importObject = {
11
+ export {
12
12
  clocks,
13
13
  filesystem,
14
14
  http,
@@ -18,8 +18,6 @@ export const importObject = {
18
18
  random,
19
19
  sockets,
20
20
  cli,
21
- };
21
+ }
22
22
 
23
23
  export { WasiHttp } from "../http/wasi-http.js";
24
-
25
- export default importObject;
package/lib/browser/io.js CHANGED
@@ -1,5 +1,7 @@
1
+ import { Io } from '../common/io.js';
2
+
1
3
  // buffer until the next newline
2
- class NewlineBufferStream {
4
+ export class NewlineBufferStream {
3
5
  constructor (handler) {
4
6
  this.bufferLen = 0;
5
7
  this.bufferCapacity = 1024;
@@ -29,102 +31,9 @@ class NewlineBufferStream {
29
31
  }
30
32
  }
31
33
 
32
- class IgnoreStream {
33
- read () {
34
- return [new Uint8Array([]), 'ended'];
35
- }
36
- write () {}
37
- }
38
-
39
- export function createStream (stream) {
40
- streamEntries[streamCnt] = stream;
41
- return streamCnt++;
42
- }
43
-
44
- export function getStream (sid) {
45
- const stream = streamEntries[sid];
46
- if (!stream) throw new Error();
47
- return stream;
48
- }
49
-
50
- export function dropStream (sid) {
51
- delete streamEntries[sid];
52
- }
34
+ export const _io = new Io(
35
+ new NewlineBufferStream(console.log.bind(console)),
36
+ new NewlineBufferStream(console.error.bind(console))
37
+ );
53
38
 
54
- let streamCnt = 3;
55
- const streamEntries = {
56
- 0: new IgnoreStream(),
57
- 1: new NewlineBufferStream(console.log.bind(console)),
58
- 2: new NewlineBufferStream(console.error.bind(console)),
59
- };
60
-
61
- export function _setStdout (stdout) {
62
- streamEntries[1] = stdout;
63
- }
64
-
65
- export function _setStderr (stderr) {
66
- streamEntries[2] = stderr;
67
- }
68
-
69
- export function _setStdin (stdin) {
70
- streamEntries[0] = stdin;
71
- }
72
-
73
- export const streams = {
74
- read(s, len) {
75
- return getStream(s).read(len);
76
- },
77
- blockingRead(s, len) {
78
- return getStream(s).read(len);
79
- },
80
- skip(s, _len) {
81
- console.log(`[streams] Skip ${s}`);
82
- },
83
- blockingSkip(s, _len) {
84
- console.log(`[streams] Blocking skip ${s}`);
85
- },
86
- subscribeToInputStream(s) {
87
- console.log(`[streams] Subscribe to input stream ${s}`);
88
- },
89
- dropInputStream(s) {
90
- console.log(`[streams] Drop input stream ${s}`);
91
- },
92
- checkWrite(_s) {
93
- // TODO: implement
94
- return 1000000n;
95
- },
96
- write(s, buf) {
97
- getStream(s).write(buf);
98
- },
99
- blockingWriteAndFlush(s, buf) {
100
- // TODO: implement
101
- return streams.write(s, buf);
102
- },
103
- flush(s) {
104
- return streams.blockingFlush(s);
105
- },
106
- blockingFlush(_s) {
107
- // TODO: implement
108
- },
109
- writeZeroes(s, _len) {
110
- console.log(`[streams] Write zeroes ${s}`);
111
- },
112
- blockingWriteZeroes(s, _len) {
113
- console.log(`[streams] Blocking write zeroes ${s}`);
114
- },
115
- splice(s, _src, _len) {
116
- console.log(`[streams] Splice ${s}`);
117
- },
118
- blockingSplice(s, _src, _len) {
119
- console.log(`[streams] Blocking splice ${s}`);
120
- },
121
- forward(s, _src) {
122
- console.log(`[streams] Forward ${s}`);
123
- },
124
- subscribeToOutputStream(s) {
125
- console.log(`[streams] Subscribe to output stream ${s}`);
126
- },
127
- dropOutputStream(s) {
128
- console.log(`[streams] Drop output stream ${s}`);
129
- }
130
- };
39
+ export const streams = _io.streams;
@@ -0,0 +1,119 @@
1
+ export class IgnoreStream {
2
+ read (_len) {
3
+ return [new Uint8Array([]), 'ended'];
4
+ }
5
+ write (_bytes) {
6
+ }
7
+ }
8
+
9
+ /**
10
+ * @typedef {{ read: (len: number) => [Uint8Array, 'open' | 'ended'], drop?: () => {} }} ReadableStream
11
+ * @typedef {{ write: (buf: Uint8Array) {}, drop?: () => {}, flush?: () => {}, check?: () => BigInt }} WriteableStream
12
+ */
13
+
14
+ // NOTE: pending asyncify work, all stream methods are synchronous and blocking
15
+ export class Io {
16
+ constructor (
17
+ stdout = new IgnoreStream(),
18
+ stderr = new IgnoreStream(),
19
+ stdin = new IgnoreStream()
20
+ ) {
21
+ this.streamCnt = 3;
22
+ this.streamEntries = {
23
+ 0: stdin,
24
+ 1: stdout,
25
+ 2: stderr
26
+ }
27
+
28
+ const io = this;
29
+ this.streams = {
30
+ read(s, len) {
31
+ return io.getStream(s).read(len);
32
+ },
33
+ blockingRead(s, len) {
34
+ return io.getStream(s).read(len);
35
+ },
36
+ skip(s, _len) {
37
+ console.log(`[streams] Skip ${s}`);
38
+ },
39
+ blockingSkip(s, _len) {
40
+ console.log(`[streams] Blocking skip ${s}`);
41
+ },
42
+ subscribeToInputStream(s) {
43
+ console.log(`[streams] Subscribe to input stream ${s}`);
44
+ },
45
+ dropInputStream(s) {
46
+ io.dropStream(s);
47
+ },
48
+ checkWrite(s) {
49
+ return io.getStream(s).check() || 1000_000n;
50
+ },
51
+ write(s, buf) {
52
+ io.getStream(s).write(buf);
53
+ },
54
+ blockingWriteAndFlush(s, buf) {
55
+ const stream = io.getStream(s);
56
+ stream.write(buf);
57
+ if (stream.flush)
58
+ stream.flush();
59
+ },
60
+ flush(s) {
61
+ const stream = io.getStream(s);
62
+ if (stream.flush)
63
+ stream.flush();
64
+ },
65
+ blockingFlush(s) {
66
+ const stream = io.getStream(s);
67
+ if (stream.flush)
68
+ stream.flush();
69
+ },
70
+ writeZeroes(s, _len) {
71
+ console.log(`[streams] Write zeroes ${s}`);
72
+ },
73
+ blockingWriteZeroes(s, _len) {
74
+ console.log(`[streams] Blocking write zeroes ${s}`);
75
+ },
76
+ splice(s, _src, _len) {
77
+ console.log(`[streams] Splice ${s}`);
78
+ },
79
+ blockingSplice(s, _src, _len) {
80
+ console.log(`[streams] Blocking splice ${s}`);
81
+ },
82
+ forward(s, _src) {
83
+ console.log(`[streams] Forward ${s}`);
84
+ },
85
+ subscribeToOutputStream(s) {
86
+ console.log(`[streams] Subscribe to output stream ${s}`);
87
+ },
88
+ dropOutputStream(s) {
89
+ io.dropStream(s);
90
+ }
91
+ };
92
+ }
93
+
94
+ /**
95
+ *
96
+ * @param {ReadableStream | WriteableStream} stream
97
+ * @returns {number}
98
+ */
99
+ createStream (stream) {
100
+ this.streamEntries[this.streamCnt] = stream;
101
+ return this.streamCnt++;
102
+ }
103
+
104
+ /**
105
+ * @param {number} sid
106
+ * @returns {ReadableStream | WriteableStream}
107
+ */
108
+ getStream (sid) {
109
+ const stream = this.streamEntries[sid];
110
+ if (!stream) throw new Error();
111
+ return stream;
112
+ }
113
+
114
+ dropStream (sid) {
115
+ const stream = this.streamEntries[sid];
116
+ if (stream.drop) stream.drop();
117
+ delete this.streamEntries[sid];
118
+ }
119
+ }