-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathbrowser.ts
44 lines (41 loc) · 1.13 KB
/
browser.ts
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
import { Fd, File, OpenFile, PreopenDirectory, WASI } from "@bjorn3/browser_wasi_shim";
import { consolePrinter } from "./console.js";
import { RubyVM } from "./vm.js";
export const DefaultRubyVM = async (
rubyModule: WebAssembly.Module,
options: {
consolePrint?: boolean;
env?: Record<string, string> | undefined;
} = {},
): Promise<{
vm: RubyVM;
wasi: WASI;
instance: WebAssembly.Instance;
}> => {
const args: string[] = [];
const env: string[] = Object.entries(options.env ?? {}).map(
([k, v]) => `${k}=${v}`,
);
const fds: Fd[] = [
new OpenFile(new File([])),
new OpenFile(new File([])),
new OpenFile(new File([])),
new PreopenDirectory("/", new Map()),
];
const wasi = new WASI(args, env, fds, { debug: false });
const printer = options.consolePrint ?? true ? consolePrinter() : undefined;
const { vm, instance } = await RubyVM.instantiateModule({
module: rubyModule, wasip1: wasi,
addToImports: (imports) => {
printer?.addToImports(imports);
},
setMemory: (memory) => {
printer?.setMemory(memory);
}
});
return {
vm,
wasi,
instance,
};
};