Repository navigation
Expand file tree
/
Copy pathtest_panic.ts
More file actions
90 lines (80 loc) · 3.05 KB
/
Copy pathtest_panic.ts
File metadata and controls
90 lines (80 loc) · 3.05 KB
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
import { ConsoleStdout, File, OpenFile, PreopenDirectory, WASI } from "https://esm.sh/@bjorn3/browser_wasi_shim@0.3.0";
import { instantiate } from "./page/src/worker_process/vfs_bindings/vfs.js";
const wasmPath = "./dist/vfs.core.wasm";
function snakeToCamel(snakeCaseString: string) {
return snakeCaseString
.toLowerCase()
.replace(/_([a-z])/g, (match, letter) => letter.toUpperCase());
}
async function runTest() {
console.log("Starting Panic test...");
const args = ["vfs.wasm"];
const env = ["HOME=/"];
const fds = [
new OpenFile(new File([])),
ConsoleStdout.lineBuffered((msg) => console.log(`[STDOUT] ${msg}`)),
ConsoleStdout.lineBuffered((msg) => console.warn(`[STDERR] ${msg}`)),
];
const wasi = new WASI(args, env, fds);
const wasmBytes = await Deno.readFile(wasmPath);
const wasmModule = await WebAssembly.compile(wasmBytes);
const wasip1Imports: any = {};
for (const key in wasi.wasiImport) {
const inner_key = `${snakeToCamel(key)}Import`;
wasip1Imports[inner_key] = (wasi.wasiImport as any)[key];
}
const root = await instantiate(
() => wasmModule,
{
"wasip1-vfs:host/virtual-file-system-wasip1-core": {
Wasip1: wasip1Imports,
},
"wasip1-vfs:host/virtual-file-system-wasip1-threads-import": {
Wasip1Threads: {
threadSpawnImport: (start_arg: number) => {
console.log("Thread spawn requested.");
return -1;
}
},
},
"vfs:host/bridge": {
callUnknownFn: (name: string, payload: Uint8Array) => {
return new Uint8Array();
},
Downloader: {
sysrootGetArchiveMeta: () => 0,
sysrootReadArchiveChunk: () => {},
sysrootStartFetch: () => {},
downloadFileStart: () => {},
downloadFileChunk: () => {},
downloadFileEnd: () => {},
},
Lsp: {
start: () => {},
sendMsg: () => {},
receiveMsg: () => "",
stop: () => {},
},
Terminal: {
print: () => {},
printErr: () => {},
prompt: () => "",
}
}
} as any,
async (module, imports) => {
return await WebAssembly.instantiate(module, imports);
},
);
console.log("\n--- Running 'rustc' in VFS ---");
try {
const res = root.runCommand(["rustc"]);
console.log("Command finished normally. Result:", res);
console.log("Running another command to check if VFS is still alive...");
root.runCommand(["ls"]);
console.log("VFS is still alive!");
} catch (e) {
console.error("VFS crashed or threw an error:", e);
}
}
runTest().catch(console.error);