Able to eval js with my scope!

This commit is contained in:
dylan 2023-05-01 18:42:55 -07:00
parent 5d742d5964
commit a7b675d541
7 changed files with 67 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build

View File

@ -52,8 +52,10 @@ const drawText = (x: number, y: number, text: string) => {
}); });
} }
export default { const faux = {
clearScreen, clearScreen,
drawSprite, drawSprite,
drawText, drawText,
} };
export default faux;

31
cart_tools.ts Normal file
View File

@ -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);
}

6
cart_unpacked.json Normal file
View File

@ -0,0 +1,6 @@
[
{
"sheet_type": "code",
"value": "return {init: () => {}, update: () => {}, draw: () => {clearScreen(); drawText(0, 0, 'hello world')}}"
}
]

View File

@ -1,5 +1,9 @@
{ {
"fmt": { "fmt": {
"useTabs": true "useTabs": true
},
"tasks": {
"run": "deno run -A --unstable index.ts",
"build": "deno compile --output build/faux -A --unstable index.ts"
} }
} }

16
font.ts
View File

@ -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 // deno-fmt-ignore
export const font: {[key: string]: Array<number>} = { export const font: {[key: string]: Array<number>} = {
"a": [ "a": [

View File

@ -2,11 +2,14 @@ import {
mainloop, mainloop,
frame, frame,
} from "./window.ts"; } 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(); game.init();
await mainloop((t) => { await mainloop((_t) => {
// TODO: use t // TODO: use t
game.update(); game.update();
game.draw(); game.draw();