Skip to content

Commit ef98346

Browse files
committed
fix issues with the console
1 parent 3d88138 commit ef98346

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

lib/contracts/code_generator.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CodeGenerator {
2727
});
2828

2929
this.events.setCommandHandler('abi-contracts-vanila', function(cb) {
30-
let vanillaContractsABI = self.generateContracts(false);
30+
let vanillaContractsABI = self.generateContracts(false, true);
3131
let contractsJSON = self.generateContractsJSON();
3232

3333
cb(vanillaContractsABI, contractsJSON);
@@ -88,11 +88,19 @@ result += "\n";
8888
result += "\n })(0, memo);";
8989
result += "\n};";
9090

91-
result += "\nvar __LoadManager = function() { this.list = []; this.done = false; }";
92-
result += "\n__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }";
93-
result += "\n__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }";
94-
result += "\nthis.__loadManagerInstance = new __LoadManager();";
95-
result += "\nthis.__mainContext = this;";
91+
let mainContext = "";
92+
if (isDeployment) {
93+
mainContext = "__mainContext.";
94+
result += "\nvar __mainContext = __mainContext || this;";
95+
} else {
96+
result += "\nvar __mainContext = __mainContext || this;";
97+
mainContext = "__mainContext.";
98+
}
99+
100+
result += "\n" + mainContext + "__LoadManager = function() { this.list = []; this.done = false; }";
101+
result += "\n" + mainContext + "__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }";
102+
result += "\n" + mainContext + "__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }";
103+
result += "\n" + mainContext + "__loadManagerInstance = new " + mainContext + "__LoadManager();";
96104

97105
result += "\nvar whenEnvIsLoaded = function(cb) {";
98106
result += "\n if (typeof document !== 'undefined' && document !== null) {";
@@ -111,13 +119,14 @@ result += "\n";
111119
result += plugin.generateProvider(self) + "\n";
112120
});
113121
} else {
114-
result += "\nwhenEnvIsLoaded(__loadManagerInstance.doFirst(function(done) {\n";
122+
result += "\nwhenEnvIsLoaded(function(){" + mainContext + "__loadManagerInstance.doFirst(function(done) {\n";
115123

116124
result += "\nif (typeof window !== 'undefined') { window.web3 = undefined; }";
117125

118126
if (isDeployment) {
119127
let connection = "http://" + this.contractsConfig.deployment.host + ":" + this.contractsConfig.deployment.port;
120128
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
129+
result += '\n\tdone();';
121130
} else {
122131

123132
let connectionCode = "";
@@ -151,17 +160,24 @@ result += "\n";
151160
result += connectionCode;
152161
}
153162

154-
result += '\n}))';
163+
result += '\n})})';
155164
}
156165

157166
return result;
158167
}
159168

160-
generateContracts(useEmbarkJS) {
169+
generateContracts(useEmbarkJS, isDeployment) {
161170
let self = this;
162171
let result = "\n";
163172
let contractsPlugins;
164173

174+
let mainContext = "";
175+
if (isDeployment) {
176+
mainContext = "__mainContext.";
177+
} else {
178+
mainContext = "__mainContext.";
179+
}
180+
165181
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
166182
return "";
167183
}
@@ -192,11 +208,11 @@ result += "\n";
192208
//result += "\n }";
193209
//result += "\n}";
194210

195-
result += "\n__loadManagerInstance.execWhenReady(function() {";
211+
result += "\n" + mainContext + "__loadManagerInstance.execWhenReady(function() {";
196212
result += "\nif (typeof window !== 'undefined') { window." + className + " = undefined; }";
197213
if (useEmbarkJS) {
198214
let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined";
199-
result += "\n__mainContext." + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
215+
result += "\n" + mainContext + "" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
200216
} else {
201217
result += "\n" + className + "Abi = " + abi + ";";
202218
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";
@@ -269,7 +285,7 @@ result += "\n";
269285
let result = "";
270286

271287
result += this.generateProvider(options.deployment);
272-
result += this.generateContracts(options.useEmbarkJS);
288+
result += this.generateContracts(options.useEmbarkJS, options.deployment);
273289
//result += this.generateStorageInitialization(options.useEmbarkJS);
274290
//result += this.generateCommunicationInitialization(options.useEmbarkJS);
275291

lib/contracts/deploy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Deploy {
8282
if (contract.onDeploy !== undefined) {
8383
self.logger.info('executing onDeploy commands');
8484
let codeGenerator = new CodeGenerator({contractsManager: self.contractsManager});
85-
let code = codeGenerator.generateContracts(false);
85+
let code = codeGenerator.generateContracts(false, true);
8686
let cmds = contract.onDeploy.join(';\n');
8787

8888
RunCode.doEval(code + "\n" + cmds, self.web3);

lib/core/runCode.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
let Web3 = require('web3');
22
let web3;
3+
let __mainContext;
34

45
// ======================
56
// the eval is used for evaluating some of the contact calls for different purposes
@@ -10,6 +11,7 @@ function doEval(code, _web3) {
1011
if (_web3) {
1112
web3 = _web3;
1213
}
14+
1315
return eval(code); // jshint ignore:line
1416
}
1517

0 commit comments

Comments
 (0)