diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index ab28f9c75..1bddd4382 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -27,7 +27,7 @@ class CodeGenerator { }); this.events.setCommandHandler('abi-contracts-vanila', function(cb) { - let vanillaContractsABI = self.generateContracts(false); + let vanillaContractsABI = self.generateContracts(false, true); let contractsJSON = self.generateContractsJSON(); cb(vanillaContractsABI, contractsJSON); @@ -88,11 +88,19 @@ result += "\n"; result += "\n })(0, memo);"; result += "\n};"; - result += "\nvar __LoadManager = function() { this.list = []; this.done = false; }"; - result += "\n__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }"; - result += "\n__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }"; - result += "\nthis.__loadManagerInstance = new __LoadManager();"; - result += "\nthis.__mainContext = this;"; + let mainContext = ""; + if (isDeployment) { + mainContext = "__mainContext."; + result += "\nvar __mainContext = __mainContext || this;"; + } else { + result += "\nvar __mainContext = __mainContext || this;"; + mainContext = "__mainContext."; + } + + result += "\n" + mainContext + "__LoadManager = function() { this.list = []; this.done = false; }"; + result += "\n" + mainContext + "__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }"; + result += "\n" + mainContext + "__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }"; + result += "\n" + mainContext + "__loadManagerInstance = new " + mainContext + "__LoadManager();"; result += "\nvar whenEnvIsLoaded = function(cb) {"; result += "\n if (typeof document !== 'undefined' && document !== null) {"; @@ -111,13 +119,14 @@ result += "\n"; result += plugin.generateProvider(self) + "\n"; }); } else { - result += "\nwhenEnvIsLoaded(__loadManagerInstance.doFirst(function(done) {\n"; + result += "\nwhenEnvIsLoaded(function(){" + mainContext + "__loadManagerInstance.doFirst(function(done) {\n"; result += "\nif (typeof window !== 'undefined') { window.web3 = undefined; }"; if (isDeployment) { let connection = "http://" + this.contractsConfig.deployment.host + ":" + this.contractsConfig.deployment.port; result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));'; + result += '\n\tdone();'; } else { let connectionCode = ""; @@ -151,17 +160,24 @@ result += "\n"; result += connectionCode; } - result += '\n}))'; + result += '\n})})'; } return result; } - generateContracts(useEmbarkJS) { + generateContracts(useEmbarkJS, isDeployment) { let self = this; let result = "\n"; let contractsPlugins; + let mainContext = ""; + if (isDeployment) { + mainContext = "__mainContext."; + } else { + mainContext = "__mainContext."; + } + if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) { return ""; } @@ -192,11 +208,11 @@ result += "\n"; //result += "\n }"; //result += "\n}"; - result += "\n__loadManagerInstance.execWhenReady(function() {"; + result += "\n" + mainContext + "__loadManagerInstance.execWhenReady(function() {"; result += "\nif (typeof window !== 'undefined') { window." + className + " = undefined; }"; if (useEmbarkJS) { let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined"; - result += "\n__mainContext." + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});"; + result += "\n" + mainContext + "" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});"; } else { result += "\n" + className + "Abi = " + abi + ";"; result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);"; @@ -269,7 +285,7 @@ result += "\n"; let result = ""; result += this.generateProvider(options.deployment); - result += this.generateContracts(options.useEmbarkJS); + result += this.generateContracts(options.useEmbarkJS, options.deployment); //result += this.generateStorageInitialization(options.useEmbarkJS); //result += this.generateCommunicationInitialization(options.useEmbarkJS); diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 5ed3b833b..4f2c5d195 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -82,7 +82,7 @@ class Deploy { if (contract.onDeploy !== undefined) { self.logger.info('executing onDeploy commands'); let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager}); - let code = codeGenerator.generateContracts(false); + let code = codeGenerator.generateContracts(false, true); let cmds = contract.onDeploy.join(';\n'); RunCode.doEval(code + "\n" + cmds, self.web3); diff --git a/lib/core/runCode.js b/lib/core/runCode.js index 6c89be039..9614b9122 100644 --- a/lib/core/runCode.js +++ b/lib/core/runCode.js @@ -1,5 +1,6 @@ let Web3 = require('web3'); let web3; +let __mainContext; // ====================== // the eval is used for evaluating some of the contact calls for different purposes @@ -10,6 +11,7 @@ function doEval(code, _web3) { if (_web3) { web3 = _web3; } + return eval(code); // jshint ignore:line }