2018-08-24 13:05:19 +00:00
|
|
|
const vm = require('vm');
|
2018-05-23 15:16:56 +00:00
|
|
|
|
2018-08-24 13:05:19 +00:00
|
|
|
class RunCode {
|
|
|
|
constructor() {
|
2018-08-28 11:16:36 +00:00
|
|
|
this.context = Object.assign({}, {require, console, ...global.this});
|
2018-08-24 13:05:19 +00:00
|
|
|
}
|
2017-02-18 21:53:49 +00:00
|
|
|
|
2018-08-24 13:05:19 +00:00
|
|
|
doEval(code) {
|
2018-08-27 15:12:28 +00:00
|
|
|
try {
|
|
|
|
return vm.runInNewContext(code, this.context);
|
|
|
|
} catch(e) {
|
2018-08-28 10:47:40 +00:00
|
|
|
console.log(e.message)
|
2018-08-27 15:12:28 +00:00
|
|
|
}
|
2018-08-24 13:05:19 +00:00
|
|
|
}
|
2017-02-18 21:53:49 +00:00
|
|
|
|
2018-08-24 13:05:19 +00: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;
|
|
|
|
}
|
|
|
|
this.context[varName] = code;
|
|
|
|
}
|
2018-05-23 15:16:56 +00:00
|
|
|
|
2018-08-24 13:05:19 +00:00
|
|
|
getWeb3Config() {
|
|
|
|
return {defaultAccount: this.context.web3.eth.defaultAccount, provider: this.context.web3.currentProvider};
|
|
|
|
}
|
2018-08-08 12:42:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 13:05:19 +00:00
|
|
|
module.exports = RunCode;
|