Allow top level await

This commit is contained in:
dylan 2023-05-16 23:45:29 -07:00
parent 9cb77c7d59
commit 9b48560cce
2 changed files with 11 additions and 8 deletions

View File

@ -17,9 +17,9 @@ let game: any = null;
let mode: "play" | "edit" | "repl" = "repl"; let mode: "play" | "edit" | "repl" = "repl";
addToContext("play", () => { addToContext("play", async () => {
game = await runCode(getCodeSheet(0));
mode = "play"; mode = "play";
game = runCode(getCodeSheet(0));
game.init(); game.init();
}); });
@ -43,8 +43,8 @@ await mainloop(async (_t) => {
} else { } else {
if (mode === "play") { if (mode === "play") {
if (game) { if (game) {
game.update(); await game.update();
game.draw(); await game.draw();
} }
frame(); frame();
} else if (mode === "repl") { } else if (mode === "repl") {

View File

@ -13,6 +13,9 @@ export const getBuiltins = () => {
return builtins; return builtins;
} }
// deno-lint-ignore no-explicit-any
const AsyncFunction = (async function () {}).constructor as any;
addToContext("eval", eval); addToContext("eval", eval);
const context = new Proxy(G, { const context = new Proxy(G, {
@ -31,18 +34,18 @@ const context = new Proxy(G, {
}, },
}); });
export const runCode = (code: string) => { export const runCode = async (code: string) => {
try { try {
new Function(code); new AsyncFunction(code);
} catch (err) { } catch (err) {
throw err; throw err;
} }
const fn = new Function("context", ` const fn = new AsyncFunction("context", `
with (context) { with (context) {
${code} ${code}
} }
`); `);
return fn(context); return await fn(context);
} }
export const evalCode = (code: string) => { export const evalCode = (code: string) => {