Skip to content

Commit

Permalink
jsonio solidity
Browse files Browse the repository at this point in the history
  • Loading branch information
yann300 committed Nov 19, 2017
1 parent a2d85a9 commit 9f160f0
Show file tree
Hide file tree
Showing 26 changed files with 346 additions and 193 deletions.
14 changes: 6 additions & 8 deletions ci/makeMockCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var solc = require('solc/wrapper')
var soljson = require('../soljson')
var compiler = solc(soljson)

var compilerInput = require('../src/app/compiler/compiler-input')

gatherCompilationResults(function (error, data) {
if (error) {
console.log(error)
Expand Down Expand Up @@ -44,20 +46,16 @@ function gatherCompilationResults (callback) {
function compile (source, optimization, addCompilationResult) {
var missingInputs = []
try {
var result = compiler.compile(source, optimization, function (path) {
var input = compilerInput(source, {optimize: optimization})
var result = compiler.compileStandardWrapper(input, function (path) {
missingInputs.push(path)
return { error: 'Deferred import' }
})
input = input.replace(/(\t)|(\n)|(\\n)|( )/g, '')
} catch (e) {
console.log(e)
}
var key = optimization.toString()
for (var k in source.sources) {
key += k + source.sources[k]
}
key = key.replace(/(\t)|(\n)|( )/g, '')
var ret = {
key: key,
key: input,
source: source,
optimization: optimization,
missingInputs: missingInputs,
Expand Down
30 changes: 21 additions & 9 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ function run () {
getCurrentFile: () => {
return config.get('currentFile')
},
getSourceName: (index) => {
return compiler.getSourceName(index)
},
highlight: (position, node) => {
if (compiler.lastCompilationResult && compiler.lastCompilationResult.data) {
var lineColumn = offsetToLineColumnConverter.offsetToLineColumn(position, position.file, compiler.lastCompilationResult)
Expand All @@ -315,7 +318,10 @@ function run () {
}
}
}
return editor.addMarker(lineColumn, compiler.lastCompilationResult.data.sourceList[position.file], css)
var fileName = compiler.getSourceName(position.file)
if (fileName) {
return editor.addMarker(lineColumn, fileName, css)
}
}
return null
},
Expand All @@ -333,7 +339,7 @@ function run () {
jumpTo: (position) => {
if (compiler.lastCompilationResult && compiler.lastCompilationResult.data) {
var lineColumn = offsetToLineColumnConverter.offsetToLineColumn(position, position.file, compiler.lastCompilationResult)
var filename = compiler.lastCompilationResult.data.sourceList[position.file]
var filename = compiler.getSourceName(position.file)
if (filename !== config.get('currentFile') && (filesProviders['browser'].exists(filename) || filesProviders['localhost'].exists(filename))) {
fileManager.switchFile(filename)
}
Expand Down Expand Up @@ -493,7 +499,7 @@ function run () {

var staticAnalysisAPI = {
renderWarning: (label, warningContainer, type) => {
return renderer.error(label, warningContainer, type)
return renderer.error({ severity: 'warning', formattedMessage: label }, warningContainer, type)
},
offsetToLineColumn: (location, file) => {
return offsetToLineColumnConverter.offsetToLineColumn(location, file, compiler.lastCompilationResult)
Expand All @@ -511,11 +517,17 @@ function run () {
document.querySelector(`.${css.dragbar2}`).style.right = delta + 'px'
onResize()
},
getSource: (fileName) => {
return compiler.getSource(fileName)
},
getContracts: () => {
if (compiler.lastCompilationResult && compiler.lastCompilationResult.data) {
return compiler.lastCompilationResult.data.contracts
}
return null
return compiler.getContracts()
},
getContract: (name) => {
return compiler.getContract(name)
},
visitContracts: (cb) => {
compiler.visitContracts(cb)
},
udapp: () => {
return udapp
Expand Down Expand Up @@ -595,7 +607,7 @@ function run () {
this.fullLineMarker = null
this.source = null
if (lineColumnPos) {
this.source = compiler.lastCompilationResult.data.sourceList[location.file] // auto switch to that tab
this.source = compiler.getSourceName(location.file)
if (config.get('currentFile') !== this.source) {
fileManager.switchFile(this.source)
}
Expand Down Expand Up @@ -664,7 +676,7 @@ function run () {
if (error) {
console.log(error)
} else {
sources[target] = content
sources[target] = { content }
compiler.compile(sources, target)
}
})
Expand Down
26 changes: 26 additions & 0 deletions src/app/compiler/compiler-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

/*
opts:
- optimize
- { file_name: { library_name: address } }
*/
module.exports = (sources, opts) => {
return JSON.stringify({
target: opts.target,
language: 'Solidity',
sources: sources,
settings: {
optimizer: {
enabled: opts.optimize === true,
runs: 500
}
},
libraries: opts.libraries,
outputSelection: {
'*': {
'*': [ 'metadata', 'evm.bytecode', 'evm.deployedBytecode', 'abi', 'legacyAST', 'metadata', 'evm.assembly', 'evm.methodIdentifiers', 'evm.gasEstimates' ]
}
}
})
}
7 changes: 5 additions & 2 deletions src/app/compiler/compiler-worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

var solc = require('solc/wrapper')
var compilerInput = require('./compiler-input')

var compileJSON = function () { return '' }
var missingInputs = []
Expand All @@ -22,10 +23,12 @@ module.exports = function (self) {

compileJSON = function (input, optimize) {
try {
return JSON.stringify(compiler.compile(JSON.parse(input), optimize, function (path) {
input = JSON.parse(input)
var inputStandard = compilerInput(input.sources, {optimize: optimize, target: input.target})
return compiler.compileStandardWrapper(inputStandard, function (path) {
missingInputs.push(path)
return { 'error': 'Deferred import' }
}))
})
} catch (exception) {
return JSON.stringify({ error: 'Uncaught JavaScript exception:\n' + exception })
}
Expand Down
97 changes: 84 additions & 13 deletions src/app/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ var solc = require('solc/wrapper')
var solcABI = require('solc/abi')

var webworkify = require('webworkify')
var utils = require('../../lib/utils')

var compilerInput = require('./compiler-input')

var EventManager = require('ethereum-remix').lib.EventManager

var txHelper = require('../execution/txHelper')

/*
trigger compilationFinished, compilerLoaded, compilationStarted, compilationDuration
*/
Expand Down Expand Up @@ -44,7 +47,7 @@ function Compiler (handleImportCall) {
gatherImports(files, target, missingInputs, function (error, input) {
if (error) {
self.lastCompilationResult = null
self.event.trigger('compilationFinished', [false, { 'error': error }, files])
self.event.trigger('compilationFinished', [false, {'error': { formattedMessage: error, severity: 'error' }}, files])
} else {
compileJSON(input, optimize ? 1 : 0)
}
Expand Down Expand Up @@ -82,7 +85,9 @@ function Compiler (handleImportCall) {

var result
try {
result = compiler.compile(source, optimize, missingInputsCallback)
var input = compilerInput(source.sources, {optimize: optimize, target: source.target})
result = compiler.compileStandardWrapper(input, missingInputsCallback)
result = JSON.parse(result)
} catch (exception) {
result = { error: 'Uncaught JavaScript exception:\n' + exception }
}
Expand All @@ -97,17 +102,86 @@ function Compiler (handleImportCall) {
data: null,
source: null
}

/**
* return the contract obj of the given @arg name. Uses last compilation result.
* return null if not found
* @param {String} name - contract name
* @returns contract obj and associated file: { contract, file } or null
*/
this.getContract = (name) => {
if (this.lastCompilationResult.data && this.lastCompilationResult.data.contracts) {
return txHelper.getContract(name, this.lastCompilationResult.data.contracts)
}
return null
}

/**
* call the given @arg cb (function) for all the contracts. Uses last compilation result
* @param {Function} cb - callback
*/
this.visitContracts = (cb) => {
if (this.lastCompilationResult.data && this.lastCompilationResult.data.contracts) {
return txHelper.visitContracts(this.lastCompilationResult.data.contracts, cb)
}
return null
}

/**
* return the compiled contracts from the last compilation result
* @return {Object} - contracts
*/
this.getContracts = () => {
if (this.lastCompilationResult.data && this.lastCompilationResult.data.contracts) {
return this.lastCompilationResult.data.contracts
}
return null
}

/**
* return the sources from the last compilation result
* @param {Object} cb - map of sources
*/
this.getSources = () => {
if (this.lastCompilationResult.source) {
return this.lastCompilationResult.source.sources
}
return null
}

/**
* return the sources @arg fileName from the last compilation result
* @param {Object} cb - map of sources
*/
this.getSource = (fileName) => {
if (this.lastCompilationResult.source) {
return this.lastCompilationResult.source.sources[fileName]
}
return null
}

/**
* return the source from the last compilation result that has the given index. null if source not found
* @param {Int} index - index of the source
*/
this.getSourceName = (index) => {
if (this.lastCompilationResult.data && this.lastCompilationResult.data.sources) {
return Object.keys(this.lastCompilationResult.data.sources)[index]
}
return null
}

function compilationFinished (data, missingInputs, source) {
var noFatalErrors = true // ie warnings are ok

function isValidError (error) {
// The deferred import is not a real error
// FIXME: maybe have a better check?
if (/Deferred import/.exec(error)) {
if (/Deferred import/.exec(error.message)) {
return false
}

return utils.errortype(error) !== 'warning'
return error.severity !== 'warning'
}

if (data['error'] !== undefined) {
Expand Down Expand Up @@ -233,7 +307,7 @@ function Compiler (handleImportCall) {

for (var fileName in files) {
var match
while ((match = importRegex.exec(files[fileName]))) {
while ((match = importRegex.exec(files[fileName].content))) {
var importFilePath = match[1]
if (importFilePath.startsWith('./')) {
var path = /(.*\/).*/.exec(target)
Expand Down Expand Up @@ -261,7 +335,7 @@ function Compiler (handleImportCall) {
if (err) {
cb(err)
} else {
files[m] = content
files[m] = { content }
gatherImports(files, target, importHints, cb)
}
})
Expand All @@ -281,12 +355,9 @@ function Compiler (handleImportCall) {
}

function updateInterface (data) {
for (var contract in data.contracts) {
var abi = JSON.parse(data.contracts[contract].interface)
abi = solcABI.update(truncateVersion(currentVersion), abi)
data.contracts[contract].interface = JSON.stringify(abi)
}

txHelper.visitContracts(data.contracts, (contract) => {
data.contracts[contract.file][contract.name].abi = solcABI.update(truncateVersion(currentVersion), contract.object.abi)
})
return data
}
}
Expand Down
Loading

0 comments on commit 9f160f0

Please sign in to comment.