-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathwebstart.ts
41 lines (35 loc) · 1.35 KB
/
webstart.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
import {run} from './runner';
function webStart() {
document.addEventListener("DOMContentLoaded", function() {
var importObject = {
imports: {
print: (arg : any) => {
console.log("Logging from WASM: ", arg);
const elt = document.createElement("pre");
document.getElementById("output").appendChild(elt);
elt.innerText = arg;
return arg;
},
},
};
function renderResult(result : any) : void {
if(result === undefined) { console.log("skip"); return; }
const elt = document.createElement("pre");
document.getElementById("output").appendChild(elt);
elt.innerText = String(result);
}
function renderError(result : any) : void {
const elt = document.createElement("pre");
document.getElementById("output").appendChild(elt);
elt.setAttribute("style", "color: red");
elt.innerText = String(result);
}
document.getElementById("run").addEventListener("click", function(e) {
const source = document.getElementById("user-code") as HTMLTextAreaElement;
const output = document.getElementById("output").innerHTML = "";
run(source.value, {importObject}).then((r) => { renderResult(r); console.log ("run finished") })
.catch((e) => { renderError(e); console.log("run failed", e) });;
});
});
}
webStart();