@bytecodealliance/preview2-shim 0.17.2 → 0.17.4
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/README.md +47 -0
- package/lib/browser/cli.js +76 -103
- package/lib/browser/clocks.js +30 -29
- package/lib/browser/config.js +6 -0
- package/lib/browser/environment.js +29 -0
- package/lib/browser/filesystem.js +299 -255
- 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 +134 -0
- package/lib/io/calls.js +8 -5
- package/lib/io/worker-http.js +179 -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 +255 -241
- package/lib/io/worker-thread.js +837 -754
- package/lib/nodejs/cli.js +64 -63
- package/lib/nodejs/clocks.js +51 -45
- package/lib/nodejs/filesystem.js +785 -651
- package/lib/nodejs/http.js +697 -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 +11 -5
- package/types/index.d.ts +0 -1
- package/types/instantiation.d.ts +136 -0
package/README.md
CHANGED
|
@@ -6,6 +6,53 @@ Node.js support is fully tested and conformant against the Wasmtime test suite.
|
|
|
6
6
|
|
|
7
7
|
Browser support is considered experimental, and not currently suitable for production applications.
|
|
8
8
|
|
|
9
|
+
# Features
|
|
10
|
+
|
|
11
|
+
## WASI Shim object for easy instantiation
|
|
12
|
+
|
|
13
|
+
An default instantiation object can be used via the `WASIShim` class in `@bytecodealliance/preview2-shim/instantiation`:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { WASIShim } from '@bytecodealliance/preview2-shim/instantiation';
|
|
17
|
+
import type {
|
|
18
|
+
VersionedWASIImportObject,
|
|
19
|
+
WASIImportObject,
|
|
20
|
+
} from '@bytecodealliance/preview2-shim/instantiation';
|
|
21
|
+
|
|
22
|
+
const shim = new WASIShim();
|
|
23
|
+
|
|
24
|
+
const unversioned: WASIImportObject = shim.getImportObject();
|
|
25
|
+
// console.log('unversioned', unversioned);
|
|
26
|
+
unversioned satisfies WASIImportObject;
|
|
27
|
+
unversioned satisfies VersionedWASIImportObject<''>;
|
|
28
|
+
|
|
29
|
+
const versioned: VersionedWASIImportObject<'0.2.3'> = shim.getImportObject({
|
|
30
|
+
asVersion: '0.2.3',
|
|
31
|
+
});
|
|
32
|
+
//console.log('versioned', versioned);
|
|
33
|
+
versioned satisfies VersionedWASIImportObject<'0.2.3'>;
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The import object generated by `getImportObject` can be easily used in `instantiate()` calls
|
|
37
|
+
produced by [`jco transpile`][jco] (with `--instantiation=async`):
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
import { WASIShim } from '@bytecodealliance/preview2-shim/instantiation';
|
|
41
|
+
|
|
42
|
+
// The code below assumes that you have output your transpiled WebAssembly component to `dist/transpiled`
|
|
43
|
+
import { instantiate } from './dist/transpiled/component.js';
|
|
44
|
+
|
|
45
|
+
const loader = async (path: string) => {
|
|
46
|
+
const buf = await readFile(`./dist/transpiled/${path}`);
|
|
47
|
+
return await WebAssembly.compile(buf.buffer as ArrayBuffer);
|
|
48
|
+
};
|
|
49
|
+
const component = await instantiate(loader, new WASIShim().getImportObject());
|
|
50
|
+
|
|
51
|
+
// TODO: Code that uses your component's exports goes here.
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
[jco]: https://www.npmjs.com/package/@bytecodealliance/jco
|
|
55
|
+
|
|
9
56
|
# License
|
|
10
57
|
|
|
11
58
|
This project is licensed under the Apache 2.0 license with the LLVM exception.
|
package/lib/browser/cli.js
CHANGED
|
@@ -1,128 +1,101 @@
|
|
|
1
|
-
import { _setCwd as fsSetCwd } from './filesystem.js';
|
|
2
1
|
import { streams } from './io.js';
|
|
3
2
|
const { InputStream, OutputStream } = streams;
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
let _env = [], _args = [], _cwd = "/";
|
|
8
|
-
export function _setEnv (envObj) {
|
|
9
|
-
_env = Object.entries(envObj);
|
|
10
|
-
}
|
|
11
|
-
export function _setArgs (args) {
|
|
12
|
-
_args = args;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function _setCwd (cwd) {
|
|
16
|
-
fsSetCwd(_cwd = cwd);
|
|
17
|
-
}
|
|
4
|
+
export { _setEnv, _setArgs, environment } from './environment.js';
|
|
5
|
+
export { _setCwd } from './config.js';
|
|
18
6
|
|
|
19
|
-
|
|
20
|
-
getEnvironment () {
|
|
21
|
-
return _env;
|
|
22
|
-
},
|
|
23
|
-
getArguments () {
|
|
24
|
-
return _args;
|
|
25
|
-
},
|
|
26
|
-
initialCwd () {
|
|
27
|
-
return _cwd;
|
|
28
|
-
}
|
|
29
|
-
};
|
|
7
|
+
const symbolDispose = Symbol.dispose ?? Symbol.for('dispose');
|
|
30
8
|
|
|
31
9
|
class ComponentExit extends Error {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
10
|
+
constructor(code) {
|
|
11
|
+
super(`Component exited ${code === 0 ? 'successfully' : 'with error'}`);
|
|
12
|
+
this.exitError = true;
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
37
15
|
}
|
|
38
16
|
|
|
39
17
|
export const exit = {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
18
|
+
exit(status) {
|
|
19
|
+
throw new ComponentExit(status.tag === 'err' ? 1 : 0);
|
|
20
|
+
},
|
|
21
|
+
exitWithCode(code) {
|
|
22
|
+
throw new ComponentExit(code);
|
|
23
|
+
},
|
|
46
24
|
};
|
|
47
25
|
|
|
48
26
|
/**
|
|
49
|
-
* @param {import('../common/io.js').InputStreamHandler} handler
|
|
27
|
+
* @param {import('../common/io.js').InputStreamHandler} handler
|
|
50
28
|
*/
|
|
51
|
-
export function _setStdin
|
|
52
|
-
|
|
29
|
+
export function _setStdin(handler) {
|
|
30
|
+
stdinStream.handler = handler;
|
|
53
31
|
}
|
|
54
32
|
/**
|
|
55
|
-
* @param {import('../common/io.js').OutputStreamHandler} handler
|
|
33
|
+
* @param {import('../common/io.js').OutputStreamHandler} handler
|
|
56
34
|
*/
|
|
57
|
-
export function _setStderr
|
|
58
|
-
|
|
35
|
+
export function _setStderr(handler) {
|
|
36
|
+
stderrStream.handler = handler;
|
|
59
37
|
}
|
|
60
38
|
/**
|
|
61
|
-
* @param {import('../common/io.js').OutputStreamHandler} handler
|
|
39
|
+
* @param {import('../common/io.js').OutputStreamHandler} handler
|
|
62
40
|
*/
|
|
63
|
-
export function _setStdout
|
|
64
|
-
|
|
41
|
+
export function _setStdout(handler) {
|
|
42
|
+
stdoutStream.handler = handler;
|
|
65
43
|
}
|
|
66
44
|
|
|
67
45
|
const stdinStream = new InputStream({
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
46
|
+
blockingRead(_len) {
|
|
47
|
+
// TODO
|
|
48
|
+
},
|
|
49
|
+
subscribe() {
|
|
50
|
+
// TODO
|
|
51
|
+
},
|
|
52
|
+
[symbolDispose]() {
|
|
53
|
+
// TODO
|
|
54
|
+
},
|
|
77
55
|
});
|
|
78
56
|
let textDecoder = new TextDecoder();
|
|
79
57
|
const stdoutStream = new OutputStream({
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
[symbolDispose] () {
|
|
90
|
-
}
|
|
58
|
+
write(contents) {
|
|
59
|
+
if (contents[contents.length - 1] == 10) {
|
|
60
|
+
// console.log already appends a new line
|
|
61
|
+
contents = contents.subarray(0, contents.length - 1);
|
|
62
|
+
}
|
|
63
|
+
console.log(textDecoder.decode(contents));
|
|
64
|
+
},
|
|
65
|
+
blockingFlush() {},
|
|
66
|
+
[symbolDispose]() {},
|
|
91
67
|
});
|
|
92
68
|
const stderrStream = new OutputStream({
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
[symbolDispose] () {
|
|
103
|
-
|
|
104
|
-
}
|
|
69
|
+
write(contents) {
|
|
70
|
+
if (contents[contents.length - 1] == 10) {
|
|
71
|
+
// console.error already appends a new line
|
|
72
|
+
contents = contents.subarray(0, contents.length - 1);
|
|
73
|
+
}
|
|
74
|
+
console.error(textDecoder.decode(contents));
|
|
75
|
+
},
|
|
76
|
+
blockingFlush() {},
|
|
77
|
+
[symbolDispose]() {},
|
|
105
78
|
});
|
|
106
79
|
|
|
107
80
|
export const stdin = {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
81
|
+
InputStream,
|
|
82
|
+
getStdin() {
|
|
83
|
+
return stdinStream;
|
|
84
|
+
},
|
|
112
85
|
};
|
|
113
86
|
|
|
114
87
|
export const stdout = {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
88
|
+
OutputStream,
|
|
89
|
+
getStdout() {
|
|
90
|
+
return stdoutStream;
|
|
91
|
+
},
|
|
119
92
|
};
|
|
120
93
|
|
|
121
94
|
export const stderr = {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
95
|
+
OutputStream,
|
|
96
|
+
getStderr() {
|
|
97
|
+
return stderrStream;
|
|
98
|
+
},
|
|
126
99
|
};
|
|
127
100
|
|
|
128
101
|
class TerminalInput {}
|
|
@@ -133,30 +106,30 @@ const terminalStderrInstance = new TerminalOutput();
|
|
|
133
106
|
const terminalStdinInstance = new TerminalInput();
|
|
134
107
|
|
|
135
108
|
export const terminalInput = {
|
|
136
|
-
|
|
109
|
+
TerminalInput,
|
|
137
110
|
};
|
|
138
111
|
|
|
139
112
|
export const terminalOutput = {
|
|
140
|
-
|
|
113
|
+
TerminalOutput,
|
|
141
114
|
};
|
|
142
115
|
|
|
143
116
|
export const terminalStderr = {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
117
|
+
TerminalOutput,
|
|
118
|
+
getTerminalStderr() {
|
|
119
|
+
return terminalStderrInstance;
|
|
120
|
+
},
|
|
148
121
|
};
|
|
149
122
|
|
|
150
123
|
export const terminalStdin = {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
124
|
+
TerminalInput,
|
|
125
|
+
getTerminalStdin() {
|
|
126
|
+
return terminalStdinInstance;
|
|
127
|
+
},
|
|
155
128
|
};
|
|
156
129
|
|
|
157
130
|
export const terminalStdout = {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
131
|
+
TerminalOutput,
|
|
132
|
+
getTerminalStdout() {
|
|
133
|
+
return terminalStdoutInstance;
|
|
134
|
+
},
|
|
162
135
|
};
|
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
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { _setCwd as fsSetCwd } from './config.js';
|
|
2
|
+
|
|
3
|
+
let _env = [],
|
|
4
|
+
_args = [],
|
|
5
|
+
_cwd = '/';
|
|
6
|
+
|
|
7
|
+
export function _setEnv(envObj) {
|
|
8
|
+
_env = Object.entries(envObj);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function _setArgs(args) {
|
|
12
|
+
_args = args;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function _setCwd(cwd) {
|
|
16
|
+
fsSetCwd((_cwd = cwd));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const environment = {
|
|
20
|
+
getEnvironment() {
|
|
21
|
+
return _env;
|
|
22
|
+
},
|
|
23
|
+
getArguments() {
|
|
24
|
+
return _args;
|
|
25
|
+
},
|
|
26
|
+
initialCwd() {
|
|
27
|
+
return _cwd;
|
|
28
|
+
},
|
|
29
|
+
};
|