parameterized function calls! Closes #9
parent
2d7c0442e0
commit
de938bb993
39
Instance.js
39
Instance.js
|
|
@ -1,6 +1,10 @@
|
|||
|
||||
import Serializable from './Serializable.js';
|
||||
import minify from './minify.js';
|
||||
import debug from 'debug';
|
||||
import _ from 'lodash';
|
||||
const log = debug('vogue:instance');
|
||||
|
||||
|
||||
export default class Instance extends Serializable {
|
||||
module = null;
|
||||
|
|
@ -58,10 +62,12 @@ export default class Instance extends Serializable {
|
|||
}
|
||||
|
||||
invokeInternal(name, ...args) {
|
||||
// console.log('invoking', name);
|
||||
const content = this.module.functions[name];
|
||||
log('invoking', this.module.name.full + '.' + name, 'with args', args);
|
||||
const content = this.module.functions[name].code;
|
||||
const passingArguments = _.zipObject(this.module.functions[name].parameters, args);
|
||||
log('arguments obj', passingArguments);
|
||||
if(!content) throw new TypeError(name + ' is not a function!');
|
||||
return evalInContext(content, this.context, this.locals);
|
||||
return evalInContext(content, this.context, this.locals, passingArguments);
|
||||
}
|
||||
|
||||
get link () {
|
||||
|
|
@ -69,21 +75,26 @@ export default class Instance extends Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
minify()
|
||||
|
||||
function evalInContext(js, context, locals) {
|
||||
function evalInContext(js, context, locals, passingArguments) {
|
||||
//# Return the results of the in-line anonymous function we .call with the passed context
|
||||
log('='.repeat(80) + 'OG Block');
|
||||
log(js);
|
||||
log('='.repeat(80) + 'Arguments');
|
||||
log(passingArguments);
|
||||
const that = this;
|
||||
return function() {
|
||||
const preminJs = `
|
||||
'use strict';
|
||||
(() => {
|
||||
${locals.map((k) => `
|
||||
const ${k} = this.${k};
|
||||
`).join('\n')}
|
||||
${js};
|
||||
})();`;
|
||||
const preminJs =
|
||||
`'use strict';
|
||||
(() => {
|
||||
${locals.map((k) => `const ${k} = this.${k};`).join('\n\t')}
|
||||
${Object.keys(passingArguments).map(name => `let ${name} = passingArguments.${name};`).join('\n\t')}
|
||||
${js}
|
||||
})();`;
|
||||
log('='.repeat(80) + 'preminjs');
|
||||
log(preminJs);
|
||||
const newJs = minify(preminJs);
|
||||
log('='.repeat(80) + 'minjs');
|
||||
log(newJs);
|
||||
// newJs should inject into result...
|
||||
let result;
|
||||
eval(newJs);
|
||||
|
|
|
|||
|
|
@ -47,8 +47,11 @@ export default class Module {
|
|||
this.name.full = this.name.space + '.' + this.name.last;
|
||||
}
|
||||
|
||||
async function({name, block}) {
|
||||
this.functions[name] = block;
|
||||
async function({name, block, parameters}) {
|
||||
this.functions[name] = {
|
||||
code: block,
|
||||
parameters
|
||||
};
|
||||
}
|
||||
|
||||
async import({importName, name}) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
import uglify from 'uglify-js';
|
||||
|
||||
export default (code) => {
|
||||
return uglify.minify(code, {
|
||||
compress: {
|
||||
|
|
@ -7,6 +8,9 @@ export default (code) => {
|
|||
global_defs: {
|
||||
DEBUG: false
|
||||
}
|
||||
},
|
||||
sourceMap: {
|
||||
content: 'inline'
|
||||
}
|
||||
}).code;
|
||||
}
|
||||
|
|
@ -3,30 +3,39 @@ static Interface;
|
|||
|
||||
import 'terminal-kit' as terminalKit;
|
||||
|
||||
restore {
|
||||
const {terminal} = terminalKit;
|
||||
terminal.grabInput();
|
||||
terminal.on('key', function(name, matches, data) {
|
||||
|
||||
if (name === 'CTRL_C') {
|
||||
process.exit(2);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
choice(message, choices, type) {
|
||||
console.log('im being called!');
|
||||
// const {terminal} = terminalKit;
|
||||
// type ??= 'string';
|
||||
const {terminal} = terminalKit;
|
||||
type ??= 'string';
|
||||
|
||||
// return new Promise(res => {
|
||||
return new Promise(res => {
|
||||
|
||||
// terminal.saveCursor();
|
||||
// // terminal(message.split(/\x1b\[39m/g).join(', '));
|
||||
// // const loc = await new Promise (res => terminal.getCursorLocation((err, x, y) => res([x, y])));
|
||||
// for(const part of message.split(/\x1b\[39m/g)) {
|
||||
// terminal.cyan(part);
|
||||
// }
|
||||
// terminal.cyan('\n');
|
||||
terminal.saveCursor();
|
||||
|
||||
// terminal.singleColumnMenu(choices, (error, response) => {
|
||||
// terminal.restoreCursor();
|
||||
// terminal.cyan(`${message} `);
|
||||
// terminal(response.selectedText + '\n').eraseDisplayBelow();
|
||||
// if(type === 'string') {
|
||||
// res(response.selectedText);
|
||||
// } else {
|
||||
// res(response.selectedIndex);
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
for(const part of message.split(/\x1b\[39m/g)) {
|
||||
terminal.cyan(part);
|
||||
}
|
||||
terminal.cyan('\n');
|
||||
|
||||
terminal.singleColumnMenu(choices, (error, response) => {
|
||||
terminal.restoreCursor();
|
||||
terminal.cyan(`${message} `);
|
||||
terminal(response.selectedText + '\n').eraseDisplayBelow();
|
||||
if(type === 'string') {
|
||||
res(response.selectedText);
|
||||
} else {
|
||||
res(response.selectedIndex);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue