@bytecodealliance/preview2-shim 0.0.17 → 0.0.18

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,8 +1,7 @@
1
1
  import * as clocks from "./clocks.js";
2
2
  import * as filesystem from "./filesystem.js";
3
3
  import * as http from "./http.js";
4
- import * as io from "./io.js";
5
- import * as logging from "./logging.js";
4
+ import * as io from "../common/io.js";
6
5
  import * as poll from "./poll.js";
7
6
  import * as random from "./random.js";
8
7
  import * as sockets from "./sockets.js";
@@ -13,7 +12,6 @@ export {
13
12
  filesystem,
14
13
  http,
15
14
  io,
16
- logging,
17
15
  poll,
18
16
  random,
19
17
  sockets,
package/lib/common/io.js CHANGED
@@ -1,119 +1,165 @@
1
- export class IgnoreStream {
2
- read (_len) {
3
- return [new Uint8Array([]), 'ended'];
1
+ let id = 0;
2
+
3
+ class Error {
4
+ constructor (msg) {
5
+ this.msg = msg;
4
6
  }
5
- write (_bytes) {
7
+ toDebugString () {
8
+ return this.msg;
6
9
  }
7
10
  }
8
11
 
9
12
  /**
10
- * @typedef {{ read: (len: number) => [Uint8Array, 'open' | 'ended'], drop?: () => {} }} ReadableStream
11
- * @typedef {{ write: (buf: Uint8Array) {}, drop?: () => {}, flush?: () => {}, check?: () => BigInt }} WriteableStream
12
- */
13
+ * @typedef {{
14
+ * read?: (len: BigInt) => Uint8Array,
15
+ * blockingRead: (len: BigInt) => Uint8Array,
16
+ * skip?: (len: BigInt) => BigInt,
17
+ * blockingSkip?: (len: BigInt) => BigInt,
18
+ * subscribe: () => void,
19
+ * drop: () => void,
20
+ * }} InputStreamHandler
21
+ *
22
+ * @typedef {{
23
+ * checkWrite?: () -> BigInt,
24
+ * write: (buf: Uint8Array) => BigInt,
25
+ * blockingWriteAndFlush?: (buf: Uint8Array) => void,
26
+ * flush?: () => void,
27
+ * blockingFlush: () => void,
28
+ * writeZeroes?: (len: BigInt) => void,
29
+ * blockingWriteZeroes?: (len: BigInt) => void,
30
+ * blockingWriteZeroesAndFlush?: (len: BigInt) => void,
31
+ * splice?: (src: InputStream, len: BigInt) => BigInt,
32
+ * blockingSplice?: (src: InputStream, len: BigInt) => BigInt,
33
+ * forward?: (src: InputStream) => void,
34
+ * subscribe?: () => void,
35
+ * drop: () => void,
36
+ * }} OutputStreamHandler
37
+ *
38
+ **/
13
39
 
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
40
+ class InputStream {
41
+ /**
42
+ * @param {InputStreamHandler} handler
43
+ */
44
+ constructor (handler) {
45
+ if (!handler)
46
+ console.trace('no handler');
47
+ this.id = ++id;
48
+ this.handler = handler;
49
+ }
50
+ read(len) {
51
+ if (this.handler.read)
52
+ return this.handler.read(len);
53
+ return this.handler.blockingRead.call(this, len);
54
+ }
55
+ blockingRead(len) {
56
+ return this.handler.blockingRead.call(this, len);
57
+ }
58
+ skip(len) {
59
+ if (this.handler.skip)
60
+ return this.handler.skip.call(this, len);
61
+ if (this.handler.read) {
62
+ const bytes = this.handler.read.call(this, len);
63
+ return BigInt(bytes.byteLength);
26
64
  }
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
- };
65
+ return this.blockingSkip.call(this, len);
92
66
  }
67
+ blockingSkip(len) {
68
+ if (this.handler.blockingSkip)
69
+ return this.handler.blockingSkip.call(this, len);
70
+ const bytes = this.handler.blockingRead.call(this, len);
71
+ return BigInt(bytes.byteLength);
72
+ }
73
+ subscribe() {
74
+ console.log(`[streams] Subscribe to input stream ${this.id}`);
75
+ }
76
+ drop () {
77
+ if (this.handler.drop)
78
+ this.handler.drop.call(this);
79
+ }
80
+ }
93
81
 
82
+ class OutputStream {
94
83
  /**
95
- *
96
- * @param {ReadableStream | WriteableStream} stream
97
- * @returns {number}
84
+ * @param {OutputStreamHandler} handler
98
85
  */
99
- createStream (stream) {
100
- this.streamEntries[this.streamCnt] = stream;
101
- return this.streamCnt++;
86
+ constructor (handler) {
87
+ if (!handler)
88
+ console.trace('no handler');
89
+ this.id = ++id;
90
+ this.open = true;
91
+ this.handler = handler;
102
92
  }
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;
93
+ checkWrite(len) {
94
+ if (!this.open)
95
+ return 0n;
96
+ if (this.handler.checkWrite)
97
+ return this.handler.checkWrite.call(this, len);
98
+ return 1_000_000n;
112
99
  }
113
-
114
- dropStream (sid) {
115
- const stream = this.streamEntries[sid];
116
- if (stream.drop) stream.drop();
117
- delete this.streamEntries[sid];
100
+ write(buf) {
101
+ this.handler.write.call(this, buf);
102
+ }
103
+ blockingWriteAndFlush(buf) {
104
+ /// Perform a write of up to 4096 bytes, and then flush the stream. Block
105
+ /// until all of these operations are complete, or an error occurs.
106
+ ///
107
+ /// This is a convenience wrapper around the use of `check-write`,
108
+ /// `subscribe`, `write`, and `flush`, and is implemented with the
109
+ /// following pseudo-code:
110
+ ///
111
+ /// ```text
112
+ /// let pollable = this.subscribe();
113
+ /// while !contents.is_empty() {
114
+ /// // Wait for the stream to become writable
115
+ /// poll-one(pollable);
116
+ /// let Ok(n) = this.check-write(); // eliding error handling
117
+ /// let len = min(n, contents.len());
118
+ /// let (chunk, rest) = contents.split_at(len);
119
+ /// this.write(chunk ); // eliding error handling
120
+ /// contents = rest;
121
+ /// }
122
+ /// this.flush();
123
+ /// // Wait for completion of `flush`
124
+ /// poll-one(pollable);
125
+ /// // Check for any errors that arose during `flush`
126
+ /// let _ = this.check-write(); // eliding error handling
127
+ /// ```
128
+ this.handler.write.call(this, buf);
129
+ }
130
+ flush() {
131
+ if (this.handler.flush)
132
+ this.handler.flush.call(this);
133
+ }
134
+ blockingFlush() {
135
+ this.open = true;
136
+ }
137
+ writeZeroes(len) {
138
+ this.write.call(this, new Uint8Array(Number(len)));
139
+ }
140
+ blockingWriteZeroes(len) {
141
+ this.blockingWrite.call(this, new Uint8Array(Number(len)));
142
+ }
143
+ blockingWriteZeroesAndFlush(len) {
144
+ this.blockingWriteAndFlush.call(this, new Uint8Array(Number(len)));
145
+ }
146
+ splice(src, len) {
147
+ const spliceLen = Math.min(len, this.checkWrite.call(this));
148
+ const bytes = src.read(spliceLen);
149
+ this.write.call(this, bytes);
150
+ return bytes.byteLength;
151
+ }
152
+ blockingSplice(_src, _len) {
153
+ console.log(`[streams] Blocking splice ${this.id}`);
154
+ }
155
+ forward(_src) {
156
+ console.log(`[streams] Forward ${this.id}`);
157
+ }
158
+ subscribe() {
159
+ console.log(`[streams] Subscribe to output stream ${this.id}`);
160
+ }
161
+ drop() {
118
162
  }
119
163
  }
164
+
165
+ export const streams = { Error, InputStream, OutputStream };
package/lib/nodejs/cli.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { argv, env, cwd } from 'node:process';
2
+ import { streams } from '../common/io.js';
3
+ const { InputStream, OutputStream } = streams;
2
4
 
3
5
  let _env = Object.entries(env), _args = argv, _cwd = cwd();
4
6
 
@@ -20,46 +22,93 @@ export const exit = {
20
22
  }
21
23
  };
22
24
 
25
+ const stdinStream = new InputStream({
26
+ blockingRead (_len) {
27
+ // TODO
28
+ },
29
+ subscribe () {
30
+ // TODO
31
+ },
32
+ drop () {
33
+ // TODO
34
+ }
35
+ });
36
+ const stdoutStream = new OutputStream({
37
+ write (contents) {
38
+ process.stdout.write(contents);
39
+ },
40
+ blockingFlush () {
41
+ },
42
+ drop () {
43
+ }
44
+ });
45
+ const stderrStream = new OutputStream({
46
+ write (contents) {
47
+ process.stderr.write(contents);
48
+ },
49
+ blockingFlush () {
50
+
51
+ },
52
+ drop () {
53
+
54
+ }
55
+ });
56
+
23
57
  export const stdin = {
58
+ InputStream,
24
59
  getStdin () {
25
- return 0;
60
+ return stdinStream;
26
61
  }
27
62
  };
28
63
 
29
64
  export const stdout = {
65
+ OutputStream,
30
66
  getStdout () {
31
- return 1;
67
+ return stdoutStream;
32
68
  }
33
69
  };
34
70
 
35
71
  export const stderr = {
72
+ OutputStream,
36
73
  getStderr () {
37
- return 2;
74
+ return stderrStream;
38
75
  }
39
76
  };
40
77
 
78
+ class TerminalInput {}
79
+ class TerminalOutput {}
80
+
81
+ const terminalStdoutInstance = new TerminalOutput();
82
+ const terminalStderrInstance = new TerminalOutput();
83
+ const terminalStdinInstance = new TerminalInput();
84
+
41
85
  export const terminalInput = {
86
+ TerminalInput,
42
87
  dropTerminalInput () {}
43
88
  };
44
89
 
45
90
  export const terminalOutput = {
91
+ TerminalOutput,
46
92
  dropTerminalOutput () {}
47
93
  };
48
94
 
49
95
  export const terminalStderr = {
96
+ TerminalOutput,
50
97
  getTerminalStderr () {
51
- return 0;
98
+ return terminalStderrInstance;
52
99
  }
53
100
  };
54
101
 
55
102
  export const terminalStdin = {
103
+ TerminalInput,
56
104
  getTerminalStdin () {
57
- return 1;
105
+ return terminalStdinInstance;
58
106
  }
59
107
  };
60
108
 
61
109
  export const terminalStdout = {
110
+ TerminalOutput,
62
111
  getTerminalStdout () {
63
- return 2;
112
+ return terminalStdoutInstance;
64
113
  }
65
114
  };