support solc compiler; display syntax errors

This commit is contained in:
Iuri Matias 2015-08-30 21:35:34 -04:00
parent 67861af6a5
commit 467713fe87
4 changed files with 79 additions and 4 deletions

View File

@ -1,3 +1,5 @@
var shelljs = require('shelljs');
var shelljs_global = require('shelljs/global');
var web3 = require('web3'); var web3 = require('web3');
Compiler = function(blockchainConfig) { Compiler = function(blockchainConfig) {
@ -18,8 +20,31 @@ Compiler.prototype.init = function(env) {
console.log("address is : " + primaryAddress); console.log("address is : " + primaryAddress);
}; };
Compiler.prototype.compile = function(source) { Compiler.prototype.compile = function(contractFile) {
return web3.eth.compile.solidity(source); var cmd, result, output, json, compiled_object;
cmd = "solc --input-file " + contractFile + " --combined-json binary,json-abi";
result = exec(cmd, {silent: true});
output = result.output;
if (result.code === 1) {
throw new Error(result.output);
}
json = JSON.parse(output).contracts;
compiled_object = {}
for (var className in json) {
var contract = json[className];
compiled_object[className] = {};
compiled_object[className].code = contract.binary;
compiled_object[className].info = {};
compiled_object[className].info.abiDefinition = JSON.parse(contract["json-abi"]);
}
return compiled_object;
}; };
module.exports = Compiler; module.exports = Compiler;

View File

@ -79,9 +79,8 @@ ContractsConfig.prototype.compileContracts = function(env) {
// compile files // compile files
for (j = 0; j < this.contractFiles.length; j++) { for (j = 0; j < this.contractFiles.length; j++) {
contractFile = this.contractFiles[j]; contractFile = this.contractFiles[j];
source = fs.readFileSync(contractFile).toString()
compiled_contracts = this.compiler.compile(source); compiled_contracts = this.compiler.compile(contractFile);
for (var className in compiled_contracts) { for (var className in compiled_contracts) {
var contract = compiled_contracts[className]; var contract = compiled_contracts[className];

37
test/compiler.js Normal file
View File

@ -0,0 +1,37 @@
var Compiler = require('../lib/compiler.js');
var assert = require('assert');
describe('embark.compiler', function() {
describe('compile a file', function() {
var files = [
'test/support/contracts/simple_storage.sol'
];
it("should build a correct compiled object", function() {
var compiler = new Compiler();
var compiledFile = compiler.compile(files[0]);
assert.equal(compiledFile.SimpleStorage.code, '606060405260405160208060f78339016040526060805190602001505b806000600050819055505b5060c28060356000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900480632a1afcd914604b57806360fe47b114606a5780636d4ce63c14607b576049565b005b605460045060b9565b6040518082815260200191505060405180910390f35b6079600480359060200150609a565b005b608460045060a8565b6040518082815260200191505060405180910390f35b806000600050819055505b50565b6000600060005054905060b6565b90565b6000600050548156');
assert.equal(JSON.stringify(compiledFile.SimpleStorage.info.abiDefinition), '[{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}]');
});
});
describe('compile a file with an error', function() {
var files = [
'test/support/contracts/error.sol'
];
it("throw an error", function() {
var compiler = new Compiler();
assert.throws(function() { compiler.compile(files[0]) }, Error);
});
});
});

View File

@ -0,0 +1,14 @@
contract SimpleStorage {
uint public storedData;
function SimpleStorage(uint initialValue) {
storedData2 = initialValue;
}
function set(uint x) {
storedData = x;
}
function get() constant returns (uint retVal) {
return storedData;
}
}