2018-08-24 14:05:19 +01:00
|
|
|
const vm = require('vm');
|
2018-05-23 11:16:56 -04:00
|
|
|
|
2018-08-24 14:05:19 +01:00
|
|
|
class RunCode {
|
|
|
|
constructor() {
|
2018-08-28 15:09:07 +01:00
|
|
|
this.context = Object.assign({}, {global, console, exports, require, module, __filename, __dirname});
|
2018-08-24 14:05:19 +01:00
|
|
|
}
|
2017-02-18 16:53:49 -05:00
|
|
|
|
2018-08-24 14:05:19 +01:00
|
|
|
doEval(code) {
|
2018-08-27 16:12:28 +01:00
|
|
|
try {
|
|
|
|
return vm.runInNewContext(code, this.context);
|
|
|
|
} catch(e) {
|
2018-08-29 10:49:53 +01:00
|
|
|
console.error(e.message);
|
2018-08-27 16:12:28 +01:00
|
|
|
}
|
2018-08-24 14:05:19 +01:00
|
|
|
}
|
2017-02-18 16:53:49 -05:00
|
|
|
|
2018-08-24 14:05:19 +01:00
|
|
|
registerVar(varName, code) {
|
|
|
|
// TODO: Update all the code being dependent of web3
|
|
|
|
// To identify, look at the top of the file for something like:
|
|
|
|
// /*global web3*/
|
|
|
|
if (varName === 'web3') {
|
|
|
|
global.web3 = code;
|
|
|
|
}
|
2018-08-29 10:22:43 +01:00
|
|
|
this.context["global"][varName] = code;
|
2018-08-24 14:05:19 +01:00
|
|
|
this.context[varName] = code;
|
|
|
|
}
|
2018-05-23 11:16:56 -04:00
|
|
|
|
2018-08-24 14:05:19 +01:00
|
|
|
getWeb3Config() {
|
|
|
|
return {defaultAccount: this.context.web3.eth.defaultAccount, provider: this.context.web3.currentProvider};
|
|
|
|
}
|
2018-08-08 13:42:45 +01:00
|
|
|
}
|
|
|
|
|
2018-08-24 14:05:19 +01:00
|
|
|
module.exports = RunCode;
|