@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 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.
@@ -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
- const symbolDispose = Symbol.dispose ?? Symbol.for('dispose');
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
- export const environment = {
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
- constructor(code) {
33
- super(`Component exited ${code === 0 ? 'successfully' : 'with error'}`);
34
- this.exitError = true;
35
- this.code = code;
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
- exit (status) {
41
- throw new ComponentExit(status.tag === 'err' ? 1 : 0);
42
- },
43
- exitWithCode (code) {
44
- throw new ComponentExit(code);
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 (handler) {
52
- stdinStream.handler = handler;
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 (handler) {
58
- stderrStream.handler = handler;
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 (handler) {
64
- stdoutStream.handler = handler;
41
+ export function _setStdout(handler) {
42
+ stdoutStream.handler = handler;
65
43
  }
66
44
 
67
45
  const stdinStream = new InputStream({
68
- blockingRead (_len) {
69
- // TODO
70
- },
71
- subscribe () {
72
- // TODO
73
- },
74
- [symbolDispose] () {
75
- // TODO
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
- write (contents) {
81
- if (contents[contents.length - 1] == 10) {
82
- // console.log already appends a new line
83
- contents = contents.subarray(0, contents.length - 1);
84
- }
85
- console.log(textDecoder.decode(contents));
86
- },
87
- blockingFlush () {
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
- 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
- },
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
- InputStream,
109
- getStdin () {
110
- return stdinStream;
111
- }
81
+ InputStream,
82
+ getStdin() {
83
+ return stdinStream;
84
+ },
112
85
  };
113
86
 
114
87
  export const stdout = {
115
- OutputStream,
116
- getStdout () {
117
- return stdoutStream;
118
- }
88
+ OutputStream,
89
+ getStdout() {
90
+ return stdoutStream;
91
+ },
119
92
  };
120
93
 
121
94
  export const stderr = {
122
- OutputStream,
123
- getStderr () {
124
- return stderrStream;
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
- TerminalInput
109
+ TerminalInput,
137
110
  };
138
111
 
139
112
  export const terminalOutput = {
140
- TerminalOutput
113
+ TerminalOutput,
141
114
  };
142
115
 
143
116
  export const terminalStderr = {
144
- TerminalOutput,
145
- getTerminalStderr () {
146
- return terminalStderrInstance;
147
- }
117
+ TerminalOutput,
118
+ getTerminalStderr() {
119
+ return terminalStderrInstance;
120
+ },
148
121
  };
149
122
 
150
123
  export const terminalStdin = {
151
- TerminalInput,
152
- getTerminalStdin () {
153
- return terminalStdinInstance;
154
- }
124
+ TerminalInput,
125
+ getTerminalStdin() {
126
+ return terminalStdinInstance;
127
+ },
155
128
  };
156
129
 
157
130
  export const terminalStdout = {
158
- TerminalOutput,
159
- getTerminalStdout () {
160
- return terminalStdoutInstance;
161
- }
131
+ TerminalOutput,
132
+ getTerminalStdout() {
133
+ return terminalStdoutInstance;
134
+ },
162
135
  };
@@ -1,34 +1,35 @@
1
1
  export const monotonicClock = {
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
- return this.subscribeDuration(instant - now);
17
- },
18
- subscribeDuration (_duration) {
19
- _duration = BigInt(_duration);
20
- console.log(`[monotonic-clock] subscribe`);
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
- now() {
26
- let now = Date.now(); // in milliseconds
27
- const seconds = BigInt(Math.floor(now / 1e3));
28
- const nanoseconds = (now % 1e3) * 1e6;
29
- return { seconds, nanoseconds };
30
- },
31
- resolution() {
32
- return { seconds: 0n, nanoseconds: 1e6 };
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,6 @@
1
+ // eslint-disable-next-line no-unused-vars
2
+ let _cwd = '/';
3
+
4
+ export function _setCwd(cwd) {
5
+ _cwd = cwd;
6
+ }
@@ -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
+ };