Merge pull request #1027 from ethereum/move_compiler_to_remix_solidity

Move compiler to remix solidity
pull/1/head
yann300 7 years ago committed by GitHub
commit 06b773ea52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      ci/makeMockCompiler.js
  2. 2
      package.json
  3. 2
      src/app.js
  4. 2
      src/app/compiler/compiler-imports.js
  5. 45
      src/app/compiler/compiler-worker.js
  6. 358
      src/app/compiler/compiler.js
  7. 2
      test/compiler-test.js

@ -1,11 +1,9 @@
'use strict'
var fs = require('fs')
var solc = require('solc/wrapper')
var soljson = require('../soljson')
var compiler = solc(soljson)
var compiler = require('solc')
var compilerInput = require('../src/app/compiler/compiler-input')
var compilerInput = require('remix-solidity').CompilerInput
var compilationResult = {}
gatherCompilationResults('./test-browser/tests/', compilationResult)
gatherCompilationResults('./test-browser/tests/units/', compilationResult)

@ -147,7 +147,7 @@
"build": "browserify src/index.js -o build/app.js",
"browsertest": "sleep 5 && npm run nightwatch_local",
"csslint": "csslint --ignore=order-alphabetical --errors='errors,duplicate-properties,empty-rules' --exclude-list='assets/css/font-awesome.min.css' assets/css/",
"downloadsolc": "rimraf soljson.js; wget https://ethereum.github.io/solc-bin/soljson.js",
"downloadsolc": "rimraf soljson.js && cd node_modules/solc && wget https://ethereum.github.io/solc-bin/soljson.js && cd ../../",
"lint": "standard | notify-error",
"make-mock-compiler": "node ci/makeMockCompiler.js",
"minify": "uglifyjs --in-source-map inline --source-map-inline -c warnings=false",

@ -20,7 +20,7 @@ var SharedFolder = require('./app/files/shared-folder')
var Config = require('./config')
var Editor = require('./app/editor/editor')
var Renderer = require('./app/ui/renderer')
var Compiler = require('./app/compiler/compiler')
var Compiler = require('remix-solidity').Compiler
var executionContext = require('./execution-context')
var Debugger = require('./app/debugger/debugger')
var StaticAnalysis = require('./app/staticanalysis/staticAnalysisView')

@ -1,4 +1,5 @@
'use strict'
// TODO: can just use request or fetch instead
var $ = require('jquery')
var base64 = require('js-base64').Base64
var swarmgw = require('swarmgw')
@ -60,6 +61,7 @@ module.exports = {
if (match) {
found = true
// TODO: this needs to be moved to the caller
$('#output').append($('<div/>').append($('<pre/>').text('Loading ' + url + ' ...')))
handler.handler(match, function (err, content, cleanUrl) {
if (err) {

@ -1,45 +0,0 @@
'use strict'
var solc = require('solc/wrapper')
var compileJSON = function () { return '' }
var missingInputs = []
module.exports = function (self) {
self.addEventListener('message', function (e) {
var data = e.data
switch (data.cmd) {
case 'loadVersion':
delete self.Module
// NOTE: workaround some browsers?
self.Module = undefined
compileJSON = null
self.importScripts(data.data)
var compiler = solc(self.Module)
compileJSON = function (input) {
try {
return compiler.compileStandardWrapper(input, function (path) {
missingInputs.push(path)
return { 'error': 'Deferred import' }
})
} catch (exception) {
return JSON.stringify({ error: 'Uncaught JavaScript exception:\n' + exception })
}
}
self.postMessage({
cmd: 'versionLoaded',
data: compiler.version()
})
break
case 'compile':
missingInputs.length = 0
self.postMessage({cmd: 'compiled', job: data.job, data: compileJSON(data.input), missingInputs: missingInputs})
break
}
}, false)
}

@ -1,358 +0,0 @@
'use strict'
var solc = require('solc/wrapper')
var solcABI = require('solc/abi')
var webworkify = require('webworkify')
var compilerInput = require('./compiler-input')
var remixLib = require('remix-lib')
var EventManager = remixLib.EventManager
var txHelper = require('../execution/txHelper')
/*
trigger compilationFinished, compilerLoaded, compilationStarted, compilationDuration
*/
function Compiler (handleImportCall) {
var self = this
this.event = new EventManager()
var compileJSON
var worker = null
var currentVersion
var optimize = false
this.setOptimize = function (_optimize) {
optimize = _optimize
}
var compilationStartTime = null
this.event.register('compilationFinished', (success, data, source) => {
if (success && compilationStartTime) {
this.event.trigger('compilationDuration', [(new Date().getTime()) - compilationStartTime])
}
compilationStartTime = null
})
this.event.register('compilationStarted', () => {
compilationStartTime = new Date().getTime()
})
var internalCompile = function (files, target, missingInputs) {
gatherImports(files, target, missingInputs, function (error, input) {
if (error) {
self.lastCompilationResult = null
self.event.trigger('compilationFinished', [false, {'error': { formattedMessage: error, severity: 'error' }}, files])
} else {
compileJSON(input, optimize ? 1 : 0)
}
})
}
var compile = function (files, target) {
self.event.trigger('compilationStarted', [])
internalCompile(files, target)
}
this.compile = compile
function setCompileJSON (_compileJSON) {
compileJSON = _compileJSON
}
this.setCompileJSON = setCompileJSON // this is exposed for testing
function onCompilerLoaded (version) {
currentVersion = version
self.event.trigger('compilerLoaded', [version])
}
function onInternalCompilerLoaded () {
if (worker === null) {
var compiler = solc(window.Module)
compileJSON = function (source, optimize, cb) {
var missingInputs = []
var missingInputsCallback = function (path) {
missingInputs.push(path)
return { error: 'Deferred import' }
}
var result
try {
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 }
}
compilationFinished(result, missingInputs, source)
}
onCompilerLoaded(compiler.version())
}
}
this.lastCompilationResult = {
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.message)) {
return false
}
return error.severity !== 'warning'
}
if (data['error'] !== undefined) {
// Ignore warnings (and the 'Deferred import' error as those are generated by us as a workaround
if (isValidError(data['error'])) {
noFatalErrors = false
}
}
if (data['errors'] !== undefined) {
data['errors'].forEach(function (err) {
// Ignore warnings and the 'Deferred import' error as those are generated by us as a workaround
if (isValidError(err)) {
noFatalErrors = false
}
})
}
if (!noFatalErrors) {
// There are fatal errors - abort here
self.lastCompilationResult = null
self.event.trigger('compilationFinished', [false, data, source])
} else if (missingInputs !== undefined && missingInputs.length > 0) {
// try compiling again with the new set of inputs
internalCompile(source.sources, source.target, missingInputs)
} else {
data = updateInterface(data)
self.lastCompilationResult = {
data: data,
source: source
}
self.event.trigger('compilationFinished', [true, data, source])
}
}
this.loadVersion = function (usingWorker, url) {
console.log('Loading ' + url + ' ' + (usingWorker ? 'with worker' : 'without worker'))
self.event.trigger('loadingCompiler', [url, usingWorker])
if (usingWorker) {
loadWorker(url)
} else {
loadInternal(url)
}
}
function loadInternal (url) {
delete window.Module
// NOTE: workaround some browsers?
window.Module = undefined
// Set a safe fallback until the new one is loaded
setCompileJSON(function (source, optimize) {
compilationFinished({ error: { formattedMessage: 'Compiler not yet loaded.' } })
})
var newScript = document.createElement('script')
newScript.type = 'text/javascript'
newScript.src = url
document.getElementsByTagName('head')[0].appendChild(newScript)
var check = window.setInterval(function () {
if (!window.Module) {
return
}
window.clearInterval(check)
onInternalCompilerLoaded()
}, 200)
}
function loadWorker (url) {
if (worker !== null) {
worker.terminate()
}
worker = webworkify(require('./compiler-worker.js'))
var jobs = []
worker.addEventListener('message', function (msg) {
var data = msg.data
switch (data.cmd) {
case 'versionLoaded':
onCompilerLoaded(data.data)
break
case 'compiled':
var result
try {
result = JSON.parse(data.data)
} catch (exception) {
result = { 'error': 'Invalid JSON output from the compiler: ' + exception }
}
var sources = {}
if (data.job in jobs !== undefined) {
sources = jobs[data.job].sources
delete jobs[data.job]
}
compilationFinished(result, data.missingInputs, sources)
break
}
})
worker.onerror = function (msg) {
compilationFinished({ error: 'Worker error: ' + msg.data })
}
worker.addEventListener('error', function (msg) {
compilationFinished({ error: 'Worker error: ' + msg.data })
})
compileJSON = function (source, optimize) {
jobs.push({sources: source})
worker.postMessage({cmd: 'compile', job: jobs.length - 1, input: compilerInput(source.sources, {optimize: optimize, target: source.target})})
}
worker.postMessage({cmd: 'loadVersion', data: url})
}
function gatherImports (files, target, importHints, cb) {
importHints = importHints || []
// FIXME: This will only match imports if the file begins with one.
// It should tokenize by lines and check each.
// eslint-disable-next-line no-useless-escape
var importRegex = /^\s*import\s*[\'\"]([^\'\"]+)[\'\"];/g
for (var fileName in files) {
var match
while ((match = importRegex.exec(files[fileName].content))) {
var importFilePath = match[1]
if (importFilePath.startsWith('./')) {
var path = /(.*\/).*/.exec(target)
if (path !== null) {
importFilePath = importFilePath.replace('./', path[1])
} else {
importFilePath = importFilePath.slice(2)
}
}
// FIXME: should be using includes or sets, but there's also browser compatibility..
if (importHints.indexOf(importFilePath) === -1) {
importHints.push(importFilePath)
}
}
}
while (importHints.length > 0) {
var m = importHints.pop()
if (m in files) {
continue
}
handleImportCall(m, function (err, content) {
if (err) {
cb(err)
} else {
files[m] = { content }
gatherImports(files, target, importHints, cb)
}
})
return
}
cb(null, { 'sources': files, 'target': target })
}
function truncateVersion (version) {
var tmp = /^(\d+.\d+.\d+)/.exec(version)
if (tmp) {
return tmp[1]
}
return version
}
function updateInterface (data) {
txHelper.visitContracts(data.contracts, (contract) => {
data.contracts[contract.file][contract.name].abi = solcABI.update(truncateVersion(currentVersion), contract.object.abi)
})
return data
}
}
module.exports = Compiler

@ -2,7 +2,7 @@
var test = require('tape')
var Compiler = require('../src/app/compiler/compiler')
var Compiler = require('remix-solidity').Compiler
test('compiler.compile smoke', function (t) {
t.plan(1)

Loading…
Cancel
Save