-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathrustc.html
153 lines (140 loc) · 6.86 KB
/
rustc.html
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!--
See https://github.com/bjorn3/browser_wasi_shim/pull/28 for instructions on how to use this example.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="node_modules/xterm/css/xterm.css" />
<script src="node_modules/xterm/lib/xterm.js"></script>
<script src="node_modules/xterm-addon-fit/lib/xterm-addon-fit.js"></script>
<style>
body {
margin: 0;
}
p {
margin: 10px;
}
</style>
</head>
<body>
<div id="terminal"></div>
<div id="downloads"></div>
<p id="note">
Note: the failure to invoke the linker at the end is expected.
WASI doesn't have a way to invoke external processes and rustc doesn't have a builtin linker.
This demo highlights how far `rustc` can get on this polyfill before failing due to other reasons.
</p>
<script type="module">
import { Fd, File, Directory, PreopenDirectory, WASI, strace } from "../dist/index.js";
var term = new Terminal({
convertEol: true,
});
term.open(document.getElementById('terminal'));
var fitAddon = new FitAddon.FitAddon();
term.loadAddon(fitAddon);
fitAddon.fit();
class XtermStdio extends Fd {
/*:: term: Terminal*/
constructor(term/*: Terminal*/) {
super();
this.term = term;
}
fd_write(data/*: Uint8Array*/)/*: {ret: number, nwritten: number}*/ {
let nwritten = 0;
console.log(data);
this.term.writeUtf8(data);
return { ret: 0, nwritten: data.byteLength };
}
}
(async function () {
term.writeln("\x1B[93mDownloading\x1B[0m");
let wasm = await WebAssembly.compileStreaming(fetch("wasm-rustc/bin/rustc.wasm"));
//let wasm = await WebAssembly.compileStreaming(fetch("/rust_out.wasm"));
term.writeln("\x1B[93mInstantiating\x1B[0m");
async function load_external_file(path) {
return new File(await (await (await fetch(path)).blob()).arrayBuffer());
}
let args = ["rustc", "/hello.rs", "--sysroot", "/sysroot", "--target", "x86_64-unknown-linux-gnu", "-Cpanic=abort", "-Ccodegen-units=1"];
let env = ["RUSTC_LOG=info"];
let fds = [
new XtermStdio(term),
new XtermStdio(term),
new XtermStdio(term),
new PreopenDirectory("/tmp", []),
new PreopenDirectory("/sysroot", [
["lib", new Directory([
["rustlib", new Directory([
["wasm32-wasi", new Directory([
["lib", new Directory([])],
])],
["x86_64-unknown-linux-gnu", new Directory([
["lib", new Directory(await (async function () {
let dir = new Map();
for (let file of [
"libaddr2line-b8754aeb03c02354.rlib",
"libadler-05c3545f6cd12159.rlib",
"liballoc-0dab879bc41cd6bd.rlib",
"libcfg_if-c7fd2cef50341546.rlib",
"libcompiler_builtins-a99947d020d809d6.rlib",
"libcore-4b8e8a815d049db3.rlib",
"libgetopts-bbb75529e85d129d.rlib",
"libgimli-598847d27d7a3cbf.rlib",
"libhashbrown-d2ff91fdf93cacb2.rlib",
"liblibc-dc63949c664c3fce.rlib",
"libmemchr-2d3a423be1a6cb96.rlib",
"libminiz_oxide-b109506a0ccc4c6a.rlib",
"libobject-7b48def7544c748b.rlib",
"libpanic_abort-c93441899b93b849.rlib",
"libpanic_unwind-11d9ba05b60bf694.rlib",
"libproc_macro-1a7f7840bb9983dc.rlib",
"librustc_demangle-59342a335246393d.rlib",
"librustc_std_workspace_alloc-552b185085090ff6.rlib",
"librustc_std_workspace_core-5d8a121daa7eeaa9.rlib",
"librustc_std_workspace_std-97f43841ce452f7d.rlib",
"libstd-bdedb7706a556da2.rlib",
"libstd-bdedb7706a556da2.so",
"libstd_detect-cca21eebc4281add.rlib",
"libsysroot-f654e185be3ffebd.rlib",
"libtest-f06fa3fbc201c558.rlib",
"libunicode_width-19a0dcd589fa0877.rlib",
"libunwind-747b693f90af9445.rlib",
]) {
dir.set(file, await load_external_file("/examples/wasm-rustc/lib/rustlib/x86_64-unknown-linux-gnu/lib/" + file));
}
return dir;
})())],
])],
])],
])],
]),
new PreopenDirectory("/", [
["hello.rs", new File(new TextEncoder("utf-8").encode(`fn main() { println!("Hello World!"); }`))],
]),
];
let w = new WASI(args, env, fds, { debug: true });
let next_thread_id = 1;
let inst = await WebAssembly.instantiate(wasm, {
"env": { memory: new WebAssembly.Memory({ initial: 256, maximum: 16384, shared: true }) },
"wasi": {
"thread-spawn": function(start_arg) {
let thread_id = next_thread_id++;
inst.exports.wasi_thread_start(thread_id, start_arg);
return thread_id;
}
},
"wasi_snapshot_preview1": strace(w.wasiImport, ["fd_prestat_get"]),
});
term.writeln("\x1B[93mExecuting\x1B[0m");
console.log(inst.exports);
try { w.start(inst); } catch(e) { term.writeln("Exception: " + e.message); /*term.writeln("backtrace:"); term.writeln(e.stack);*/ }
term.writeln("\x1B[92mDone\x1B[0m");
console.log(fds);
console.log(fds[5].dir);
console.log(fds[5].dir.contents.get("hello.hello.65c991d23c885d45-cgu.0.rcgu.o").data);
document.querySelector("#downloads").innerHTML += "<br><a href='" + URL.createObjectURL(new Blob([fds[5].dir.contents.get("hello.hello.65c991d23c885d45-cgu.0.rcgu.o").data], { type: "application/elf" })) + "'>Download object</a>";
document.querySelector("#downloads").innerHTML += "<br><a href='" + URL.createObjectURL(new Blob([fds[5].dir.contents.get("hello.allocator_shim.rcgu.o").data], { type: "application/elf" })) + "'>Download allocator shim</a>";
})();
</script>
</body>
</html>