-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.mjs
99 lines (84 loc) · 2.68 KB
/
host.mjs
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
import { readFile, writeFile, open } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { WASI } from "wasi";
async function readOutput(filePath) {
const str = (await readFile(filePath, "utf8")).trim();
try {
return JSON.parse(str);
} catch {
return str;
}
}
async function run(wasmFilePath, input) {
const uniqueId = crypto.randomUUID();
// Use stdin/stdout/stderr to communicate with WASM process
// See https://k33g.hashnode.dev/wasi-communication-between-nodejs-and-wasm-modules-another-way-with-stdin-and-stdout
const workDir = tmpdir();
const stdinFilePath = join(workDir, `stdin.wasm.${uniqueId}.txt`);
const stdoutFilePath = join(workDir, `stdout.wasm.${uniqueId}.txt`);
const stderrFilePath = join(workDir, `stderr.wasm.${uniqueId}.txt`);
// 👋 send data to the WASM program
await writeFile(stdinFilePath, JSON.stringify(input), { encoding: "utf8" });
const [stdinFile, stdoutFile, stderrFile] = await Promise.all([
open(stdinFilePath, "r"),
open(stdoutFilePath, "a"),
open(stderrFilePath, "a"),
]);
try {
const wasi = new WASI({
version: "preview1",
args: [],
env: {},
stdin: stdinFile.fd,
stdout: stdoutFile.fd,
stderr: stderrFile.fd,
returnOnExit: true,
});
const embeddedModule = await WebAssembly.compile(
await readFile(new URL(wasmFilePath, import.meta.url)),
);
const providerModule = await WebAssembly.compile(
await readFile(new URL("./provider.wasm", import.meta.url)),
);
const providerInstance = await WebAssembly.instantiate(
providerModule,
wasi.getImportObject(),
);
const instance = await WebAssembly.instantiate(embeddedModule, {
javy_quickjs_provider_v1: providerInstance.exports,
});
// Javy provider is a WASI reactor see https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md?plain=1
wasi.initialize(providerInstance);
instance.exports._start();
const [out, err] = await Promise.all([
readOutput(stdoutFilePath),
readOutput(stderrFilePath),
]);
if (err) {
throw new Error(err);
}
return out;
} catch (e) {
if (e instanceof WebAssembly.RuntimeError) {
const errorMessage = await readOutput(stderrFilePath);
if (errorMessage) {
throw new Error(errorMessage);
}
} else {
throw e;
}
} finally {
await Promise.all([
stdinFile.close(),
stdoutFile.close(),
stderrFile.close(),
]);
}
}
try {
const result = await run("./embedded.wasm", { n: 100 });
console.log("Success!", JSON.stringify(result, null, 2));
} catch (e) {
console.log(e);
}