fantasy-console/sheet.ts
2023-05-02 17:06:54 -07:00

38 lines
770 B
TypeScript

import faux from "./builtins.ts";
import { getCart } from "./cart.ts";
export type SheetType = "code" | "spritesheet" | "map" | "sfx" | "patterns" | "fonts";
const getSheet = (n: number) => {
return getCart()[n].value;
}
export const codeSheet = (sheet: number) => {
const code = getSheet(sheet);
try {
new Function(code);
} catch (err) {
throw err;
}
const fn = new Function("context", `
with (context) {
${code}
}
`);
return fn(context);
}
// deno-lint-ignore no-explicit-any
const G: any = {...faux, faux, log: console.log, code_sheet: codeSheet};
const context = new Proxy(G, {
get: (target, prop) => {
return target[prop];
},
set: (target, prop, value) => {
target[prop] = value;
return true;
},
has: () => {
return true;
},
});