-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
154 lines (129 loc) · 4.28 KB
/
index.js
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
154
import * as monaco from 'monaco-editor'
self.MonacoEnvironment = {
getWorkerUrl: function(moduleId, label) {
if (label === "javascript") {
return "js/ts.worker.js";
}
return "js/editor.worker.js";
},
};
function setOutput(stderr, stdout) {
document.getElementById("error").textContent = stderr;
document.getElementById("output").textContent = stdout;
}
let worker = null;
let workerLastSource = null;
let workerIsRunning = false;
let workerKillTimeout = null;
let editor = null;
function executeCode() {
let source = editor.getValue();
if (workerIsRunning || source === workerLastSource) {
return;
}
if (worker === null) {
worker = new Worker(new URL('./worker.js', import.meta.url));
worker.onmessage = function(e) {
if (e.data.status) {
setOutput("", e.data.status);
} else {
clearTimeout(workerKillTimeout);
workerIsRunning = false;
setOutput(e.data.stderr, e.data.stdout);
}
};
}
let select = document.getElementById("branch");
let branch = self.branches.find(el => el.branch === select.value);
let wasm_url = branch.url;
worker.postMessage({source, wasm_url});
workerLastSource = source;
workerIsRunning = true;
workerKillTimeout = setTimeout(function() {
if (!workerIsRunning) {
return;
}
setOutput("", "Timed out");
worker.terminate();
workerIsRunning = false;
worker = null;
executeCode();
}, 5000);
}
function shareCode() {
let url = window.location.href.split('?')[0];
url += "?branch=" + document.getElementById("branch").value;
url += "&source=" + encodeURIComponent(editor.getValue());
navigator.clipboard.writeText(url);
}
function showBuildInfo(name) {
let build = self.branches.find(el => el.branch === name);
let info = document.getElementById("build_info");
info.innerText = `build: ${build.buildid} (rev ${build.rev.substr(0, 6)})`;
}
function changeBranch() {
showBuildInfo(this.value);
if (worker) {
worker.terminate();
}
workerIsRunning = false;
worker = null;
workerLastSource = null;
executeCode();
}
const initSource = `// Welcome to the SpiderMonkey JS shell compiled to WebAssembly!
//
// JS code on this side is evaluated in a Web Worker as you type.
// The output is printed on the right-hand side.
//
// The JS shell has various builtin functions for testing purposes.
// help() will print a list of them.
print("Hello, world!");
print("=".repeat(13));
let re = /(?<wday>\\w{3}) (?<month>\\w{3}) (?<day>\\d+)/;
let groups = re.exec(new Date()).groups;
print(\`Today is \${groups.wday}, \${groups.month} \${groups.day}.\`);
print();
print("2 ** 128 =", 2n ** 128n);
`;
self.onload = async function() {
let response = await fetch("data.json");
let branches = await response.json();
let select = document.getElementById("branch");
for (let branch of branches) {
var option = document.createElement("option");
option.value = branch.branch;
option.text = branch.branch;
select.appendChild(option);
}
self.branches = branches;
let params = new URLSearchParams(window.location.search);
let source = params.has("source") ? decodeURIComponent(params.get("source")) : initSource;
if (params.has("branch")) {
let branch = params.get("branch");
select.value = branch;
}
editor = monaco.editor.create(document.getElementById("editor"), {
value: source,
language: "javascript",
minimap: {
enabled: false
},
hideCursorInOverviewRuler: true,
scrollbar: {vertical: "auto"},
scrollBeyondLastLine: false,
theme: "vs-dark",
});
// Move cursor to end and focus the editor.
let numLines = editor.getModel().getLineCount();
let col = editor.getModel().getLineMaxColumn(numLines);
editor.setPosition({lineNumber: numLines, column: col});
editor.focus();
editor.onDidChangeModelContent(function(model) {
executeCode();
});
executeCode();
showBuildInfo(select.value);
select.onchange = changeBranch;
document.getElementById("share").onclick = shareCode;
};