-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlib.js
102 lines (91 loc) · 2.86 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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"
import path from "path";
const WASI = {
MicroWASI: ({ programName }) => {
const wasi = new MicroWASI({
args: [path.basename(programName)],
env: {},
features: [useAll()],
})
return {
wasiImport: wasi.wasiImport,
start(instance, swift) {
wasi.initialize(instance);
swift.main();
}
}
},
Node: ({ programName }) => {
const wasi = new NodeWASI({
args: [path.basename(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;
};
function isUsingSharedMemory(module) {
const imports = WebAssembly.Module.imports(module);
for (const entry of imports) {
if (entry.module === "env" && entry.name === "memory" && entry.kind == "memory") {
return true;
}
}
return false;
}
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 });
const module = await WebAssembly.compile(wasmBinary);
const importObject = {
wasi_snapshot_preview1: wasi.wasiImport,
javascript_kit: swift.importObjects(),
benchmark_helper: {
noop: () => {},
noop_with_int: (_) => {},
}
};
if (isUsingSharedMemory(module)) {
importObject["env"] = {
// We don't have JS API to get memory descriptor of imported memory
// at this moment, so we assume 256 pages (16MB) memory is enough
// large for initial memory size.
memory: new WebAssembly.Memory({ initial: 256, maximum: 16384, shared: true }),
};
importObject["wasi"] = {
"thread-spawn": () => {
throw new Error("thread-spawn not implemented");
}
}
}
// Instantiate the WebAssembly file
const instance = await WebAssembly.instantiate(module, importObject);
swift.setInstance(instance);
// Start the WebAssembly WASI instance!
wasi.start(instance, swift);
};