auu
This commit is contained in:
parent
d995726462
commit
0a343ecbc5
326 changed files with 15584 additions and 0 deletions
46
test/helpers/app-source.js
Normal file
46
test/helpers/app-source.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
'use strict';
|
||||
const fs=require('fs');
|
||||
const path=require('path');
|
||||
const vm=require('vm');
|
||||
const root=path.resolve(__dirname,'../..');
|
||||
const read=name=>fs.readFileSync(path.join(root,name),'utf8');
|
||||
const app=read('app.js'),html=read('index.html'),css=read('style.css'),worker=read('puzzle-worker.js'),appLogicSource=read('app-logic.js');
|
||||
function assert(value,message){if(!value)throw new Error(message)}
|
||||
function functionSource(name,source=app){
|
||||
let start=source.indexOf(`async function ${name}(`);if(start<0)start=source.indexOf(`function* ${name}(`);if(start<0)start=source.indexOf(`function ${name}(`);
|
||||
assert(start>=0,`Missing ${name}`);
|
||||
let paren=source.indexOf('(',start),parenDepth=0,body=-1;
|
||||
for(let i=paren;i<source.length;i++){
|
||||
if(source[i]==='(')parenDepth++;
|
||||
else if(source[i]===')'&&--parenDepth===0){body=source.indexOf('{',i);break}
|
||||
}
|
||||
assert(body>=0,`Missing body for ${name}`);let depth=0;
|
||||
for(let i=body;i<source.length;i++){
|
||||
if(source[i]==='{')depth++;
|
||||
else if(source[i]==='}'&&--depth===0)return source.slice(start,i+1);
|
||||
}
|
||||
throw new Error(`Unclosed ${name}`);
|
||||
}
|
||||
function createContext(context={}){vm.createContext(context);return context}
|
||||
function runFunctions(names,context,expose=names){
|
||||
createContext(context);
|
||||
const sources=names.map(name=>functionSource(name)).join('\n');
|
||||
vm.runInContext(`${sources}\nthis.logic={${expose.join(',')}};`,context);
|
||||
return context.logic;
|
||||
}
|
||||
function loadBendPuzzle(){
|
||||
delete global.BendPuzzle;
|
||||
delete require.cache[require.resolve(path.join(root,'puzzle-core.js'))];
|
||||
require(path.join(root,'puzzle-core.js'));
|
||||
return global.BendPuzzle;
|
||||
}
|
||||
function loadAppLogic(){
|
||||
delete require.cache[require.resolve(path.join(root,'app-logic.js'))];
|
||||
return require(path.join(root,'app-logic.js'));
|
||||
}
|
||||
function starterPuzzle(){
|
||||
const match=app.match(/const STARTER_PUZZLE=Object\.freeze\((\{.*\})\);\nconst SCORE_VERSION/s);
|
||||
assert(match,'Bundled origin puzzle missing');
|
||||
return JSON.parse(match[1]);
|
||||
}
|
||||
module.exports={root,read,app,html,css,worker,appLogicSource,assert,functionSource,createContext,runFunctions,loadBendPuzzle,loadAppLogic,starterPuzzle,vm,path,fs};
|
||||
Loading…
Add table
Add a link
Reference in a new issue