From a7b675d541b727171d191c31471d4fb8ecc6d529 Mon Sep 17 00:00:00 2001 From: dylan <> Date: Mon, 1 May 2023 18:42:55 -0700 Subject: [PATCH] Able to eval js with my scope! --- .gitignore | 1 + builtins.ts | 6 ++++-- cart_tools.ts | 31 +++++++++++++++++++++++++++++++ cart_unpacked.json | 6 ++++++ deno.json | 4 ++++ font.ts | 16 ++++++++++++++++ index.ts | 7 +++++-- 7 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 cart_tools.ts create mode 100644 cart_unpacked.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/builtins.ts b/builtins.ts index 495f08e..27399a5 100644 --- a/builtins.ts +++ b/builtins.ts @@ -52,8 +52,10 @@ const drawText = (x: number, y: number, text: string) => { }); } -export default { +const faux = { clearScreen, drawSprite, drawText, -} \ No newline at end of file +}; + +export default faux; \ No newline at end of file diff --git a/cart_tools.ts b/cart_tools.ts new file mode 100644 index 0000000..e3370ae --- /dev/null +++ b/cart_tools.ts @@ -0,0 +1,31 @@ +import faux from "./builtins.ts"; + +export type SheetType = "code" | "spritesheet" | "map" | "sfx" | "patterns" | "fonts"; + +export const parseCodeSheet = (sheet: string) => { + try { + new Function(sheet); + } catch (err) { + throw err; + } + // deno-lint-ignore no-explicit-any + const G: any = {...faux, faux, log: console.log}; + const context = new Proxy(G, { + get: (target, prop) => { + return target[prop]; + }, + set: (target, prop, value) => { + target[prop] = value; + return true; + }, + has: () => { + return true; + }, + }); + const fn = new Function("context", ` + with (context) { + ${sheet} + } + `); + return fn(context); +} \ No newline at end of file diff --git a/cart_unpacked.json b/cart_unpacked.json new file mode 100644 index 0000000..f2ef3e8 --- /dev/null +++ b/cart_unpacked.json @@ -0,0 +1,6 @@ +[ + { + "sheet_type": "code", + "value": "return {init: () => {}, update: () => {}, draw: () => {clearScreen(); drawText(0, 0, 'hello world')}}" + } +] \ No newline at end of file diff --git a/deno.json b/deno.json index 93350f5..5db6fef 100644 --- a/deno.json +++ b/deno.json @@ -1,5 +1,9 @@ { "fmt": { "useTabs": true + }, + "tasks": { + "run": "deno run -A --unstable index.ts", + "build": "deno compile --output build/faux -A --unstable index.ts" } } diff --git a/font.ts b/font.ts index 32e426f..3f0e5e7 100644 --- a/font.ts +++ b/font.ts @@ -1,3 +1,19 @@ +/** + * Perhaps fonts can be their own type of sheet. By the calculation below, we can fit ~4 fonts per fontsheet + * + * 3 bits for height + * 5 more metadata bits + * = 1 byte + * + * Per character: + * - 3 bits for width + * - 5 bits for metadata + * - 64 bits for pixels + * = 9 bytes per character + * + * 96 chars * 9 bytes = + */ + // deno-fmt-ignore export const font: {[key: string]: Array} = { "a": [ diff --git a/index.ts b/index.ts index 4346b71..60445a5 100644 --- a/index.ts +++ b/index.ts @@ -2,11 +2,14 @@ import { mainloop, frame, } from "./window.ts"; -import game from "./game.ts"; +import cart from "./cart_unpacked.json" assert { type: "json" }; +import { parseCodeSheet } from "./cart_tools.ts"; + +const game = parseCodeSheet(cart[0].value); game.init(); -await mainloop((t) => { +await mainloop((_t) => { // TODO: use t game.update(); game.draw();