2017-12-16 15:39:30 -05:00
|
|
|
let utils = require('../../utils/utils.js');
|
2018-04-02 15:06:56 -04:00
|
|
|
let fs = require('../../core/fs.js');
|
2017-12-16 15:39:30 -05:00
|
|
|
let currentSolcVersion = require('../../../package.json').dependencies.solc;
|
2018-05-16 18:09:56 -04:00
|
|
|
const ProcessLauncher = require('../../process/processLauncher');
|
2018-05-31 13:32:02 -04:00
|
|
|
const uuid = require('uuid/v1');
|
2017-02-24 19:27:27 -05:00
|
|
|
|
2017-03-30 20:12:39 +09:00
|
|
|
class SolcW {
|
2017-02-24 19:27:27 -05:00
|
|
|
|
2017-07-05 18:26:44 -04:00
|
|
|
constructor(options) {
|
|
|
|
this.logger = options.logger;
|
2017-12-30 16:48:53 -05:00
|
|
|
this.events = options.events;
|
2018-05-16 18:09:56 -04:00
|
|
|
this.compilerLoaded = false;
|
|
|
|
this.solcProcess = null;
|
2017-07-05 08:35:51 -04:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:12:39 +09:00
|
|
|
load_compiler(done) {
|
2017-12-30 16:48:53 -05:00
|
|
|
const self = this;
|
2018-05-16 18:09:56 -04:00
|
|
|
if (this.compilerLoaded) {
|
2018-05-18 14:22:58 -04:00
|
|
|
return done();
|
2017-02-24 22:49:34 -05:00
|
|
|
}
|
2018-05-16 18:09:56 -04:00
|
|
|
this.solcProcess = new ProcessLauncher({
|
|
|
|
modulePath: utils.joinPath(__dirname, 'solcP.js'),
|
|
|
|
logger: self.logger,
|
|
|
|
events: self.events
|
|
|
|
});
|
|
|
|
this.solcProcess.send({action: "init", options: {}});
|
|
|
|
|
2018-05-18 14:22:58 -04:00
|
|
|
this.solcProcess.once('result', 'loadedCompiler', () => {
|
2018-05-16 18:09:56 -04:00
|
|
|
self.compilerLoaded = true;
|
2017-03-30 20:12:39 +09:00
|
|
|
done();
|
|
|
|
});
|
2017-07-05 09:12:11 -04:00
|
|
|
|
2017-12-30 16:48:53 -05:00
|
|
|
this.events.request("version:get:solc", function(solcVersion) {
|
2017-12-30 18:12:16 -05:00
|
|
|
if (solcVersion === currentSolcVersion) {
|
2018-05-16 18:09:56 -04:00
|
|
|
self.solcProcess.send({action: 'loadCompiler', requirePath: 'solc'});
|
2017-12-30 16:48:53 -05:00
|
|
|
} else {
|
2017-12-30 19:34:15 -05:00
|
|
|
self.events.request("version:getPackageLocation", "solc", solcVersion, function(err, location) {
|
2017-12-30 16:48:53 -05:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2018-04-02 15:06:56 -04:00
|
|
|
let requirePath = fs.dappPath(location);
|
2018-05-16 18:09:56 -04:00
|
|
|
self.solcProcess.send({action: 'loadCompiler', requirePath: requirePath});
|
2017-12-30 16:48:53 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-03-30 20:12:39 +09:00
|
|
|
}
|
2017-02-24 22:49:34 -05:00
|
|
|
|
2017-03-30 20:12:39 +09:00
|
|
|
isCompilerLoaded() {
|
2018-05-16 18:09:56 -04:00
|
|
|
return (this.compilerLoaded === true);
|
2017-03-30 20:12:39 +09:00
|
|
|
}
|
2017-02-25 15:47:35 -05:00
|
|
|
|
2018-01-27 15:07:48 -05:00
|
|
|
compile(jsonObj, done) {
|
2018-05-31 13:32:02 -04:00
|
|
|
const id = uuid();
|
|
|
|
this.solcProcess.once('result', 'compilation-' + id, (msg) => {
|
2018-01-27 15:07:48 -05:00
|
|
|
done(JSON.parse(msg.output));
|
2017-03-30 20:12:39 +09:00
|
|
|
});
|
2018-05-16 18:09:56 -04:00
|
|
|
|
2018-05-31 13:32:02 -04:00
|
|
|
this.solcProcess.send({action: 'compile', jsonObj: jsonObj, id});
|
2017-03-30 20:12:39 +09:00
|
|
|
}
|
|
|
|
}
|
2017-02-24 19:27:27 -05:00
|
|
|
|
|
|
|
module.exports = SolcW;
|
|
|
|
|