From 5f3361d0303d317b7993c9e8929a2f7cddc32e55 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 23 May 2018 11:16:56 -0400 Subject: [PATCH] conflict in en.json --- lib/coderunner/codeRunner.js | 26 ++++++++++++++------------ lib/coderunner/runCode.js | 26 +++++++++++++------------- lib/contracts/blockchain.js | 6 ++++++ lib/contracts/deploy.js | 7 ++----- lib/core/engine.js | 15 +++++++++------ lib/dashboard/console.js | 8 ++++---- lib/i18n/locales/en.json | 6 ++++-- lib/index.js | 3 +++ lib/modules/specialconfigs/index.js | 9 ++------- lib/tests/test.js | 4 ++-- 10 files changed, 59 insertions(+), 51 deletions(-) diff --git a/lib/coderunner/codeRunner.js b/lib/coderunner/codeRunner.js index 93f701d35..73b1de03e 100644 --- a/lib/coderunner/codeRunner.js +++ b/lib/coderunner/codeRunner.js @@ -1,23 +1,25 @@ -let __mainContext; +// still needs to be run on a separate file due to the global context +var RunCode = require('./runCode.js'); class CodeRunner { constructor(options) { this.plugins = options.plugins; this.logger = options.logger; this.events = options.events; - } - registerVar(varName, code) { - __mainContext[varName] = code; - } + // necessary to init the context + RunCode.initContext(); - doEval(code) { - try { - // TODO: add trace log here - return eval(code); - } catch(e) { - throw new Error(e + "\n" + code); - } + this.events.on("runcode:register", (varName, code) => { + RunCode.registerVar(varName, code); + }); + + this.events.setCommandHandler('runcode:eval', (code, cb) => { + let result = RunCode.doEval(code); + if (cb) { + cb(null, result); + } + }); } } diff --git a/lib/coderunner/runCode.js b/lib/coderunner/runCode.js index 9eddfd515..9560eec1f 100644 --- a/lib/coderunner/runCode.js +++ b/lib/coderunner/runCode.js @@ -1,22 +1,16 @@ /*eslint no-unused-vars: off*/ -let Web3 = require('web3'); -let web3; -let ipfs; -let __mainContext; +let __mainContext = this; + +function initContext() { + doEval("__mainContext = this"); +} // ====================== // 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, opts) { - if (opts && opts.web3) { - web3 = opts.web3; - } - if (opts && opts.ipfs) { - ipfs = opts.ipfs; - } - +function doEval(code) { try { // TODO: add trace log here return eval(code); @@ -25,6 +19,12 @@ function doEval(code, opts) { } } +function registerVar(varName, code) { + __mainContext[varName] = code; +} + module.exports = { - doEval: doEval + doEval: doEval, + registerVar: registerVar, + initContext: initContext }; diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index 7448bcebb..c6bb575f4 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -15,6 +15,7 @@ class Blockchain { } this.registerServiceCheck(); this.registerRequests(); + this.registerWeb3Object(); } initWeb3() { @@ -156,6 +157,11 @@ class Blockchain { }); } + registerWeb3Object() { + // doesn't feel quite right, should be a cmd or plugin method + // can just be a command without a callback + this.events.emit("runcode:register", "web3", this.web3); + } } module.exports = Blockchain; diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 07fac6377..afe04a20e 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -2,8 +2,6 @@ let async = require('async'); //require("../utils/debug_util.js")(__filename, async); let utils = require('../utils/utils.js'); -let RunCode = require('../coderunner/runCode.js'); - class Deploy { constructor(options) { this.blockchain = options.blockchain; @@ -129,7 +127,7 @@ class Deploy { // TODO: can be moved into a afterDeploy event // just need to figure out the gasLimit coupling issue self.events.request('code-generator:contract:vanilla', contract, self.gasLimit, (contractCode) => { - RunCode.doEval(contractCode, {web3: self.web3}); + self.events.request('runcode:eval', contractCode); return callback(); }); } @@ -152,8 +150,7 @@ class Deploy { // TODO: can be moved into a afterDeploy event // just need to figure out the gasLimit coupling issue self.events.request('code-generator:contract:vanilla', contract, self.gasLimit, (contractCode) => { - RunCode.doEval(contractCode, self.web3); - RunCode.doEval(contractCode, {web3: self.web3}); + self.events.request('runcode:eval', contractCode); let onDeployPlugins = self.plugins.getPluginsProperty('onDeployActions', 'onDeployActions'); diff --git a/lib/core/engine.js b/lib/core/engine.js index 81223ae68..6eb92eecb 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -114,6 +114,7 @@ class Engine { let services = { "pipeline": this.pipelineService, + "codeRunner": this.codeRunnerService, "codeGenerator": this.codeGeneratorService, "deployment": this.deploymentService, "fileWatcher": this.fileWatchService, @@ -160,15 +161,17 @@ class Engine { }); } + codeRunnerService(_options) { + this.codeRunner = new CodeRunner({ + plugins: this.plugins, + events: this.events, + logger: this.logger + }); + } + codeGeneratorService(_options) { let self = this; - this.codeRunner = new CodeRunner({ - plugins: self.plugins, - events: self.events, - logger: self.logger - }); - this.codeGenerator = new CodeGenerator({ blockchainConfig: self.config.blockchainConfig, contractsConfig: self.config.contractsConfig, diff --git a/lib/dashboard/console.js b/lib/dashboard/console.js index 40a1e8010..c77d2b2fa 100644 --- a/lib/dashboard/console.js +++ b/lib/dashboard/console.js @@ -1,5 +1,4 @@ let utils = require('../utils/utils.js'); -let RunCode = require('../coderunner/runCode.js'); class Console { constructor(options) { @@ -10,7 +9,7 @@ class Console { } runCode(code) { - RunCode.doEval(code); + this.events.request('runcode:eval', code); } processEmbarkCmd (cmd) { @@ -48,8 +47,9 @@ class Console { } try { - let result = RunCode.doEval(cmd); - return callback(result); + this.events.request('runcode:eval', cmd, (err, result) => { + callback(result); + }); } catch (e) { if (e.message.indexOf('not defined') > 0) { diff --git a/lib/i18n/locales/en.json b/lib/i18n/locales/en.json index 128151208..92e515109 100644 --- a/lib/i18n/locales/en.json +++ b/lib/i18n/locales/en.json @@ -114,5 +114,7 @@ "Couldn't connect to an Ethereum node are you sure it's on?": "Couldn't connect to an Ethereum node are you sure it's on?", "make sure you have an Ethereum node or simulator running. e.g '%s'": "make sure you have an Ethereum node or simulator running. e.g '%s'", "Embark is building, please wait...": "Embark is building, please wait...", - "finished building DApp and deploying to": "finished building DApp and deploying to" -} \ No newline at end of file + "finished building DApp and deploying to": "finished building DApp and deploying to", + "Type": "Type", + "to see the list of available commands": "to see the list of available commands" +} diff --git a/lib/index.js b/lib/index.js index 0d3c9d381..5e235dca1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -128,6 +128,7 @@ class Embark { engine.startMonitor(); engine.startService("libraryManager"); + engine.startService("codeRunner"); engine.startService("web3"); engine.startService("pipeline"); engine.startService("deployment"); @@ -202,6 +203,7 @@ class Embark { } engine.startService("libraryManager"); + engine.startService("codeRunner"); engine.startService("web3"); engine.startService("pipeline"); engine.startService("deployment", {onlyCompile: options.onlyCompile}); @@ -317,6 +319,7 @@ class Embark { function startServices(callback) { engine.startService("libraryManager"); + engine.startService("codeRunner"); engine.startService("web3"); engine.startService("pipeline"); engine.startService("deployment"); diff --git a/lib/modules/specialconfigs/index.js b/lib/modules/specialconfigs/index.js index bedacf07d..94b8328eb 100644 --- a/lib/modules/specialconfigs/index.js +++ b/lib/modules/specialconfigs/index.js @@ -1,4 +1,3 @@ -let RunCode = require('../../coderunner/runCode.js'); const stringReplaceAsync = require('string-replace-async'); const async = require('async'); @@ -64,9 +63,7 @@ class SpecialConfigs { for(let cmd of onDeployCode) { self.logger.info("==== executing: " + cmd); try { - // TODO: request and re-add web3 object if necessary - //RunCode.doEval(cmd, self.blockchain.web3); - RunCode.doEval(cmd); + self.events.request('runcode:eval', cmd); } catch(e) { if (e.message.indexOf("invalid opcode") >= 0) { self.logger.error('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation'); @@ -103,9 +100,7 @@ class SpecialConfigs { for(let cmd of onDeployCode) { self.logger.info("==== executing: " + cmd); try { - // TODO: request and re-add web3 object if necessary - //RunCode.doEval(cmd, self.blockchain.web3); - RunCode.doEval(cmd); + self.events.request('runcode:eval', cmd); } catch(e) { if (e.message.indexOf("invalid opcode") >= 0) { self.logger.error('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation'); diff --git a/lib/tests/test.js b/lib/tests/test.js index 8c0819113..956f4ac27 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -2,7 +2,6 @@ var async = require('async'); //require("../utils/debug_util.js")(__filename, async); var Web3 = require('web3'); var Engine = require('../core/engine.js'); -var RunCode = require('../coderunner/runCode.js'); var TestLogger = require('./test_logger.js'); var getSimulator = function() { @@ -68,6 +67,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) { function startServices(callback) { //{abiType: 'contracts', embarkJS: false} self.engine.startService("libraryManager"); + self.engine.startService("codeRunner"); self.engine.startService("web3", { web3: self.web3 }); @@ -106,7 +106,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) { throw new Error(err); } self.web3.eth.defaultAccount = accounts[0]; - RunCode.doEval(result, {web3: self.web3}); + self.engine.events.request('runcode:eval', result); //cb(); cb(accounts); });