isolate eval in a module

This commit is contained in:
Iuri Matias 2017-02-18 16:53:49 -05:00
parent a83cc6a44a
commit 3f3a129276
4 changed files with 27 additions and 15 deletions

View File

@ -1,5 +1,5 @@
var Web3 = require('web3');
var utils = require('./utils.js');
var RunCode = require('./runCode.js');
var Console = function(options) {
this.plugins = options.plugins;
@ -7,7 +7,7 @@ var Console = function(options) {
};
Console.prototype.runCode = function(code) {
eval(code); // jshint ignore:line
RunCode.doEval(code); // jshint ignore:line
};
Console.prototype.executeCmd = function(cmd, callback) {
@ -37,7 +37,7 @@ Console.prototype.executeCmd = function(cmd, callback) {
}
try {
var result = eval(cmd); // jshint ignore:line
var result = RunCode.doEval(cmd);
return callback(result);
}
catch(e) {

View File

@ -1,7 +1,7 @@
var async = require('async');
var DeployTracker = require('./deploy_tracker.js');
var ABIGenerator = require('./abi.js');
var web3;
var RunCode = require('./runCode.js');
var Deploy = function(options) {
this.web3 = options.web3;
@ -76,17 +76,13 @@ Deploy.prototype.checkAndDeployContract = function(contract, params, callback) {
self.deployTracker.save();
self.logger.contractsState(self.contractsManager.contractsState());
// TODO: replace evals with separate process so it's isolated and with
// a callback
if (contract.onDeploy !== undefined) {
self.logger.info('executing onDeploy commands');
var abiGenerator = new ABIGenerator({contractsManager: self.contractsManager});
web3 = self.web3;
var abi = abiGenerator.generateContracts(false);
eval(abi); // jshint ignore:line
var cmds = contract.onDeploy.join(';\n');
eval(cmds); // jshint ignore:line
RunCode.doEval(abi + "\n" + cmds, self.web3);
}
callback();

18
lib/runCode.js Normal file
View File

@ -0,0 +1,18 @@
var Web3 = require('web3');
var web3;
// ======================
// the eval is used for evaluating some of the contact calls for different purposes
// this should be at least moved to a different process and scope
// for now it is defined here
// ======================
function doEval(code, _web3) {
if (_web3) {
web3 = _web3;
}
return eval(code); // jshint ignore:line
}
module.exports = {
doEval: doEval
};

View File

@ -6,6 +6,7 @@ var Deploy = require('./deploy.js');
var TestLogger = require('./test_logger.js');
var Config = require('./config.js');
var ABIGenerator = require('./abi.js');
var RunCode = require('./runCode.js');
var Test = function(_options) {
var options = _options || {};
@ -80,11 +81,8 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
if (err) {
throw new Error(err);
}
var web3 = self.web3;
web3.eth.defaultAccount = accounts[0];
// TODO: replace evals with separate process so it's isolated and with
// a callback
eval(result); // jshint ignore:line
self.web3.eth.defaultAccount = accounts[0];
RunCode.doEval(result, self.web3); // jshint ignore:line
cb();
});
});