-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlib.js
73 lines (66 loc) · 1.93 KB
/
lib.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { SwiftRuntime } from "javascript-kit-swift"
import { WASI as NodeWASI } from "wasi"
import { WASI as MicroWASI, useAll } from "uwasi"
import * as fs from "fs/promises"
const WASI = {
MicroWASI: ({ programName }) => {
const wasi = new MicroWASI({
args: [programName],
env: {},
features: [useAll()],
})
return {
wasiImport: wasi.wasiImport,
start(instance, swift) {
wasi.initialize(instance);
swift.main();
}
}
},
Node: ({ programName }) => {
const wasi = new NodeWASI({
args: [programName],
env: {},
preopens: {
"/": "./",
},
returnOnExit: false,
version: "preview1",
})
return {
wasiImport: wasi.wasiImport,
start(instance, swift) {
wasi.initialize(instance);
swift.main();
}
}
},
};
const selectWASIBackend = () => {
const value = process.env["JAVASCRIPTKIT_WASI_BACKEND"]
if (value) {
const backend = WASI[value];
if (backend) {
return backend;
}
}
return WASI.Node;
};
export const startWasiTask = async (wasmPath, wasiConstructor = selectWASIBackend()) => {
const swift = new SwiftRuntime();
// Fetch our Wasm File
const wasmBinary = await fs.readFile(wasmPath);
const wasi = wasiConstructor({ programName: wasmPath });
// Instantiate the WebAssembly file
let { instance } = await WebAssembly.instantiate(wasmBinary, {
wasi_snapshot_preview1: wasi.wasiImport,
javascript_kit: swift.importObjects(),
benchmark_helper: {
noop: () => {},
noop_with_int: (_) => {},
}
});
swift.setInstance(instance);
// Start the WebAssembly WASI instance!
wasi.start(instance, swift);
};