-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
43 lines (38 loc) · 1.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const runner = require('./polyfill.js');
const { SetSourceMapURLRelativeTo } = require('wasm-sourcemap');
const { Readable } = require('stream');
const url = require('url');
// Delay processing of things until the underlying wasm harness is ready.
const ready = new Promise((resolve) => {
runner.onRuntimeInitialized = resolve;
});
module.exports = {
instantiate: async function instantiate(buffer, options) {
await ready;
if (options && options.sourceURL !== undefined) {
if (typeof window !== 'undefined') {
options.sourceURL = url.resolve(window.location.href, options.sourceURL);
} else {
options.sourceURL = url.resolve(__dirname, options.sourceURL);
}
buffer = SetSourceMapURLRelativeTo(buffer, options.sourceURL);
}
let ret = {};
if (!options.stdout) {
ret.stdout = new Readable({});
ret.stdout._read = () => { };
options.stdout = (val) => {
ret.stdout.push(val === null ? null : new Uint8Array([val]));
}
}
if (!options.stderr) {
ret.stderr = new Readable({});
ret.stderr._read = () => { };
options.stderr = (val) => {
ret.stderr.push(val === null ? null : new Uint8Array([val]));
}
}
ret.instance = runner.instantiateWasi(buffer, options);
return ret;
},
};