@bytecodealliance/preview2-shim 0.0.16 → 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,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 }
@@ -1,25 +1,21 @@
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";
9
8
  import * as cli from "./cli.js";
10
9
 
11
- export const importObject = {
10
+ export {
12
11
  clocks,
13
12
  filesystem,
14
13
  http,
15
14
  io,
16
- logging,
17
15
  poll,
18
16
  random,
19
17
  sockets,
20
18
  cli,
21
- };
19
+ }
22
20
 
23
21
  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,165 @@
1
+ let id = 0;
2
+
3
+ class Error {
4
+ constructor (msg) {
5
+ this.msg = msg;
6
+ }
7
+ toDebugString () {
8
+ return this.msg;
9
+ }
10
+ }
11
+
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
+ **/
39
+
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);
64
+ }
65
+ return this.blockingSkip.call(this, len);
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
+ }
81
+
82
+ class OutputStream {
83
+ /**
84
+ * @param {OutputStreamHandler} handler
85
+ */
86
+ constructor (handler) {
87
+ if (!handler)
88
+ console.trace('no handler');
89
+ this.id = ++id;
90
+ this.open = true;
91
+ this.handler = handler;
92
+ }
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;
99
+ }
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() {
162
+ }
163
+ }
164
+
165
+ export const streams = { Error, InputStream, OutputStream };