From 9b48560cce41077cda8c605375f5e7f616574f5b Mon Sep 17 00:00:00 2001
From: dylan <>
Date: Tue, 16 May 2023 23:45:29 -0700
Subject: [PATCH] Allow top level await

---
 index.ts   |  8 ++++----
 runcode.ts | 11 +++++++----
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/index.ts b/index.ts
index f5070de..b8c0907 100644
--- a/index.ts
+++ b/index.ts
@@ -17,9 +17,9 @@ let game: any = null;
 
 let mode: "play" | "edit" | "repl" = "repl";
 
-addToContext("play", () => {
+addToContext("play", async () => {
+	game = await runCode(getCodeSheet(0));
 	mode = "play";
-	game = runCode(getCodeSheet(0));
 	game.init();
 });
 
@@ -43,8 +43,8 @@ await mainloop(async (_t) => {
 	} else {
 		if (mode === "play") {
 			if (game) {
-				game.update();
-				game.draw();
+				await game.update();
+				await game.draw();
 			}
 			frame();
 		} else if (mode === "repl") {
diff --git a/runcode.ts b/runcode.ts
index 5930be4..b908e1c 100644
--- a/runcode.ts
+++ b/runcode.ts
@@ -13,6 +13,9 @@ export const getBuiltins = () => {
 	return builtins;
 }
 
+// deno-lint-ignore no-explicit-any
+const AsyncFunction = (async function () {}).constructor as any;
+
 addToContext("eval", eval);
 
 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 {
-		new Function(code);
+		new AsyncFunction(code);
 	} catch (err) {
 		throw err;
 	}
-	const fn = new Function("context", `
+	const fn = new AsyncFunction("context", `
 		with (context) {
 			${code}
 		}
 	`);
-	return fn(context);
+	return await fn(context);
 }
 
 export const evalCode = (code: string) => {