Able to eval js with my scope!
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| build | ||||
| @@ -52,8 +52,10 @@ const drawText = (x: number, y: number, text: string) => { | ||||
| 	}); | ||||
| } | ||||
|  | ||||
| export default { | ||||
| const faux = { | ||||
| 	clearScreen, | ||||
| 	drawSprite, | ||||
| 	drawText, | ||||
| } | ||||
| }; | ||||
|  | ||||
| export default faux; | ||||
							
								
								
									
										31
									
								
								cart_tools.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								cart_tools.ts
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										6
									
								
								cart_unpacked.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| [ | ||||
| 	{ | ||||
| 		"sheet_type": "code", | ||||
| 		"value": "return {init: () => {}, update: () => {}, draw: () => {clearScreen(); drawText(0, 0, 'hello world')}}" | ||||
| 	} | ||||
| ] | ||||
| @@ -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" | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										16
									
								
								font.ts
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								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<number>} = { | ||||
| 	"a": [ | ||||
|   | ||||
							
								
								
									
										7
									
								
								index.ts
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								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(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 dylan
					dylan