fantasy.js (1954B)
1 #! /usr/bin/env node 2 3 const {VM} = require('vm2'); 4 const {readFileSync} = require('fs'); 5 6 const gens = JSON.parse(readFileSync('generators/generators.js').toString()); 7 8 const run = (info) => 9 { 10 // console.log(info); 11 const js = readFileSync(`generators/${info.file}`).toString(); 12 const code = ` 13 class FakeNode 14 { 15 appendChild() {} 16 removeChild() {} 17 setAttribute() {} 18 }; 19 20 let radio_value; 21 const $ = () => ({ css: () => {}, each: () => {}, val: () => radio_value, 22 ready: () => {}, is: () => false /* todo */ }); 23 24 // let's implement jquery, e-a-e-a-o 25 $.inArray = (val, array, from) => 26 { 27 for (let i = from; i < array.length; ++i) 28 if (array[i] === val) return i; 29 return -1; 30 }; 31 32 $.merge = (a, b) => 33 { 34 for (const x in b) a.push(x); 35 return a; 36 }; 37 38 const document = 39 { 40 createElement: () => new FakeNode(), 41 getElementById: () => new FakeNode(), 42 createTextNode: log, 43 }; 44 const testSwear = () => {}; 45 46 // sometimes used without initializing 47 // not let, because they are sometimes declared... 48 rnd = 0; rnd0 = 0; rnd1 = 0; rnd2 = 0; rnd3 = 0; rnd4 = 0; rnd5 = 0; rnd6 = 0; 49 rnd7 = 0; rnd8 = 0; rnd9 = 0; rnd10 = 0; rnd11 = 0; first = 1; 50 // also typo 51 rmd3 = 0; 52 53 ${js} 54 triggered = 1; // prevent some easter egg shit 55 ${info.call} 56 `; 57 new VM({ 58 sandbox: { log: console.log }, timeout: 1000, 59 eval: false, wasm: false, require: false, nesting: false, 60 }).run(code); 61 }; 62 63 let args = process.argv.slice(2); 64 if (args.length === 0) 65 { 66 const print_rec = (path, rem) => 67 { 68 if (rem.file) 69 console.log(path.join(' > ')); 70 else 71 for (const k in rem) 72 print_rec(path.concat([k]), rem[k]); 73 }; 74 print_rec([], gens.paths); 75 return; 76 } 77 78 if (args.length === 1 && args[0].indexOf('>') !== -1) 79 args = args[0].split(/[ \t]*>[ \t]*/); 80 81 let x = gens.paths; 82 for (const p of args) 83 { 84 x = x[p]; 85 if (!x) throw `Unknown item ${p}`; 86 } 87 run(x);