blob: 7a3db05691613760029a0fb486f13296220fc47b (
plain)
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
|
import initGleamCompiler from "./compiler.js";
import stdlib from "./stdlib.js";
const compiler = await initGleamCompiler();
const project = compiler.newProject();
for (const [name, code] of Object.entries(stdlib)) {
project.writeModule(name, code);
}
// Monkey patch console.log to keep a copy of the output
let logged = "";
const log = console.log;
console.log = (...args) => {
log(...args);
logged += args.map((e) => `${e}`).join(" ") + "\n";
};
async function loadProgram(js) {
const url = new URL(import.meta.url);
url.pathname = "";
url.hash = "";
url.search = "";
const href = url.toString();
const js1 = js.replaceAll(
/from\s+"\.\/(.+)"/g,
`from "${href}precompiled/$1"`,
);
const js2 = btoa(unescape(encodeURIComponent(js1)));
const module = await import("data:text/javascript;base64," + js2);
return module.main;
}
async function compileEval(code) {
logged = "";
const result = {
log: null,
error: null,
warnings: [],
};
try {
project.writeModule("main", code);
project.compilePackage("javascript");
const js = project.readCompiledJavaScript("main");
const main = await loadProgram(js);
if (main) main();
} catch (error) {
console.error(error);
result.error = error.toString();
}
for (const warning of project.takeWarnings()) {
result.warnings.push(warning);
}
result.log = logged;
return result;
}
self.onmessage = async (event) => {
const result = compileEval(event.data);
postMessage(await result);
};
// Send an initial message to the main thread to indicate that the worker is
// ready to receive messages.
postMessage({});
|