@bytecodealliance/preview2-shim 0.17.2 → 0.17.3
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/lib/browser/cli.js +91 -94
- package/lib/browser/clocks.js +30 -29
- package/lib/browser/filesystem.js +298 -251
- package/lib/browser/http.js +129 -128
- package/lib/browser/index.js +8 -16
- package/lib/browser/io.js +143 -135
- package/lib/browser/random.js +44 -42
- package/lib/browser/sockets.js +68 -166
- package/lib/common/instantiation.js +127 -0
- package/lib/io/calls.js +7 -5
- package/lib/io/worker-http.js +175 -157
- package/lib/io/worker-io.js +402 -386
- package/lib/io/worker-socket-tcp.js +271 -219
- package/lib/io/worker-socket-udp.js +494 -429
- package/lib/io/worker-sockets.js +276 -262
- package/lib/io/worker-thread.js +943 -864
- package/lib/nodejs/cli.js +64 -63
- package/lib/nodejs/clocks.js +51 -45
- package/lib/nodejs/filesystem.js +788 -654
- package/lib/nodejs/http.js +693 -617
- package/lib/nodejs/index.js +8 -16
- package/lib/nodejs/random.js +32 -28
- package/lib/nodejs/sockets.js +538 -474
- package/lib/synckit/index.js +94 -85
- package/package.json +9 -5
- package/types/index.d.ts +0 -1
- package/types/instantiation.d.ts +112 -0
package/lib/browser/cli.js
CHANGED
|
@@ -4,125 +4,122 @@ const { InputStream, OutputStream } = streams;
|
|
|
4
4
|
|
|
5
5
|
const symbolDispose = Symbol.dispose ?? Symbol.for('dispose');
|
|
6
6
|
|
|
7
|
-
let _env = [],
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
let _env = [],
|
|
8
|
+
_args = [],
|
|
9
|
+
_cwd = '/';
|
|
10
|
+
export function _setEnv(envObj) {
|
|
11
|
+
_env = Object.entries(envObj);
|
|
10
12
|
}
|
|
11
|
-
export function _setArgs
|
|
12
|
-
|
|
13
|
+
export function _setArgs(args) {
|
|
14
|
+
_args = args;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
export function _setCwd
|
|
16
|
-
|
|
17
|
+
export function _setCwd(cwd) {
|
|
18
|
+
fsSetCwd((_cwd = cwd));
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export const environment = {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
getEnvironment() {
|
|
23
|
+
return _env;
|
|
24
|
+
},
|
|
25
|
+
getArguments() {
|
|
26
|
+
return _args;
|
|
27
|
+
},
|
|
28
|
+
initialCwd() {
|
|
29
|
+
return _cwd;
|
|
30
|
+
},
|
|
29
31
|
};
|
|
30
32
|
|
|
31
33
|
class ComponentExit extends Error {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
constructor(code) {
|
|
35
|
+
super(`Component exited ${code === 0 ? 'successfully' : 'with error'}`);
|
|
36
|
+
this.exitError = true;
|
|
37
|
+
this.code = code;
|
|
38
|
+
}
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
export const exit = {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
exit(status) {
|
|
43
|
+
throw new ComponentExit(status.tag === 'err' ? 1 : 0);
|
|
44
|
+
},
|
|
45
|
+
exitWithCode(code) {
|
|
46
|
+
throw new ComponentExit(code);
|
|
47
|
+
},
|
|
46
48
|
};
|
|
47
49
|
|
|
48
50
|
/**
|
|
49
|
-
* @param {import('../common/io.js').InputStreamHandler} handler
|
|
51
|
+
* @param {import('../common/io.js').InputStreamHandler} handler
|
|
50
52
|
*/
|
|
51
|
-
export function _setStdin
|
|
52
|
-
|
|
53
|
+
export function _setStdin(handler) {
|
|
54
|
+
stdinStream.handler = handler;
|
|
53
55
|
}
|
|
54
56
|
/**
|
|
55
|
-
* @param {import('../common/io.js').OutputStreamHandler} handler
|
|
57
|
+
* @param {import('../common/io.js').OutputStreamHandler} handler
|
|
56
58
|
*/
|
|
57
|
-
export function _setStderr
|
|
58
|
-
|
|
59
|
+
export function _setStderr(handler) {
|
|
60
|
+
stderrStream.handler = handler;
|
|
59
61
|
}
|
|
60
62
|
/**
|
|
61
|
-
* @param {import('../common/io.js').OutputStreamHandler} handler
|
|
63
|
+
* @param {import('../common/io.js').OutputStreamHandler} handler
|
|
62
64
|
*/
|
|
63
|
-
export function _setStdout
|
|
64
|
-
|
|
65
|
+
export function _setStdout(handler) {
|
|
66
|
+
stdoutStream.handler = handler;
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
const stdinStream = new InputStream({
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
70
|
+
blockingRead(_len) {
|
|
71
|
+
// TODO
|
|
72
|
+
},
|
|
73
|
+
subscribe() {
|
|
74
|
+
// TODO
|
|
75
|
+
},
|
|
76
|
+
[symbolDispose]() {
|
|
77
|
+
// TODO
|
|
78
|
+
},
|
|
77
79
|
});
|
|
78
80
|
let textDecoder = new TextDecoder();
|
|
79
81
|
const stdoutStream = new OutputStream({
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
[symbolDispose] () {
|
|
90
|
-
}
|
|
82
|
+
write(contents) {
|
|
83
|
+
if (contents[contents.length - 1] == 10) {
|
|
84
|
+
// console.log already appends a new line
|
|
85
|
+
contents = contents.subarray(0, contents.length - 1);
|
|
86
|
+
}
|
|
87
|
+
console.log(textDecoder.decode(contents));
|
|
88
|
+
},
|
|
89
|
+
blockingFlush() {},
|
|
90
|
+
[symbolDispose]() {},
|
|
91
91
|
});
|
|
92
92
|
const stderrStream = new OutputStream({
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
[symbolDispose] () {
|
|
103
|
-
|
|
104
|
-
}
|
|
93
|
+
write(contents) {
|
|
94
|
+
if (contents[contents.length - 1] == 10) {
|
|
95
|
+
// console.error already appends a new line
|
|
96
|
+
contents = contents.subarray(0, contents.length - 1);
|
|
97
|
+
}
|
|
98
|
+
console.error(textDecoder.decode(contents));
|
|
99
|
+
},
|
|
100
|
+
blockingFlush() {},
|
|
101
|
+
[symbolDispose]() {},
|
|
105
102
|
});
|
|
106
103
|
|
|
107
104
|
export const stdin = {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
105
|
+
InputStream,
|
|
106
|
+
getStdin() {
|
|
107
|
+
return stdinStream;
|
|
108
|
+
},
|
|
112
109
|
};
|
|
113
110
|
|
|
114
111
|
export const stdout = {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
112
|
+
OutputStream,
|
|
113
|
+
getStdout() {
|
|
114
|
+
return stdoutStream;
|
|
115
|
+
},
|
|
119
116
|
};
|
|
120
117
|
|
|
121
118
|
export const stderr = {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
119
|
+
OutputStream,
|
|
120
|
+
getStderr() {
|
|
121
|
+
return stderrStream;
|
|
122
|
+
},
|
|
126
123
|
};
|
|
127
124
|
|
|
128
125
|
class TerminalInput {}
|
|
@@ -133,30 +130,30 @@ const terminalStderrInstance = new TerminalOutput();
|
|
|
133
130
|
const terminalStdinInstance = new TerminalInput();
|
|
134
131
|
|
|
135
132
|
export const terminalInput = {
|
|
136
|
-
|
|
133
|
+
TerminalInput,
|
|
137
134
|
};
|
|
138
135
|
|
|
139
136
|
export const terminalOutput = {
|
|
140
|
-
|
|
137
|
+
TerminalOutput,
|
|
141
138
|
};
|
|
142
139
|
|
|
143
140
|
export const terminalStderr = {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
141
|
+
TerminalOutput,
|
|
142
|
+
getTerminalStderr() {
|
|
143
|
+
return terminalStderrInstance;
|
|
144
|
+
},
|
|
148
145
|
};
|
|
149
146
|
|
|
150
147
|
export const terminalStdin = {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
148
|
+
TerminalInput,
|
|
149
|
+
getTerminalStdin() {
|
|
150
|
+
return terminalStdinInstance;
|
|
151
|
+
},
|
|
155
152
|
};
|
|
156
153
|
|
|
157
154
|
export const terminalStdout = {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
155
|
+
TerminalOutput,
|
|
156
|
+
getTerminalStdout() {
|
|
157
|
+
return terminalStdoutInstance;
|
|
158
|
+
},
|
|
162
159
|
};
|
package/lib/browser/clocks.js
CHANGED
|
@@ -1,34 +1,35 @@
|
|
|
1
1
|
export const monotonicClock = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
2
|
+
resolution() {
|
|
3
|
+
// usually we dont get sub-millisecond accuracy in the browser
|
|
4
|
+
// Note: is there a better way to determine this?
|
|
5
|
+
return 1e6;
|
|
6
|
+
},
|
|
7
|
+
now() {
|
|
8
|
+
// performance.now() is in milliseconds, but we want nanoseconds
|
|
9
|
+
return BigInt(Math.floor(performance.now() * 1e6));
|
|
10
|
+
},
|
|
11
|
+
subscribeInstant(instant) {
|
|
12
|
+
instant = BigInt(instant);
|
|
13
|
+
const now = this.now();
|
|
14
|
+
if (instant <= now) {
|
|
15
|
+
return this.subscribeDuration(0);
|
|
16
|
+
}
|
|
17
|
+
return this.subscribeDuration(instant - now);
|
|
18
|
+
},
|
|
19
|
+
subscribeDuration(_duration) {
|
|
20
|
+
_duration = BigInt(_duration);
|
|
21
|
+
console.log(`[monotonic-clock] subscribe`);
|
|
22
|
+
},
|
|
22
23
|
};
|
|
23
24
|
|
|
24
25
|
export const wallClock = {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
now() {
|
|
27
|
+
let now = Date.now(); // in milliseconds
|
|
28
|
+
const seconds = BigInt(Math.floor(now / 1e3));
|
|
29
|
+
const nanoseconds = (now % 1e3) * 1e6;
|
|
30
|
+
return { seconds, nanoseconds };
|
|
31
|
+
},
|
|
32
|
+
resolution() {
|
|
33
|
+
return { seconds: 0n, nanoseconds: 1e6 };
|
|
34
|
+
},
|
|
34
35
|
};
|