Merge pull request #60 from ethereum/use-solc-compiler

Use solc.js compiler [WIP]
pull/1/head
chriseth 8 years ago committed by GitHub
commit c0a55f45a6
  1. 1
      package.json
  2. 28
      src/app/compiler-worker.js
  3. 68
      src/app/compiler.js

@ -27,6 +27,7 @@
"js-base64": "^2.1.9", "js-base64": "^2.1.9",
"nightwatch": "^0.9.3", "nightwatch": "^0.9.3",
"semistandard": "^7.0.0", "semistandard": "^7.0.0",
"solc": "https://github.com/ethereum/solc-js",
"tape": "^4.5.1", "tape": "^4.5.1",
"web3": "^0.16.0", "web3": "^0.16.0",
"webworkify": "^1.2.1" "webworkify": "^1.2.1"

@ -1,4 +1,5 @@
var version = function () { return '(loading)'; }; var solc = require('solc/wrapper');
var compileJSON = function () { return ''; }; var compileJSON = function () { return ''; };
var missingInputs = []; var missingInputs = [];
@ -8,30 +9,23 @@ module.exports = function (self) {
switch (data.cmd) { switch (data.cmd) {
case 'loadVersion': case 'loadVersion':
delete self.Module; delete self.Module;
version = null;
compileJSON = null; compileJSON = null;
self.importScripts(data.data); self.importScripts(data.data);
var Module = self.Module;
version = Module.cwrap('version', 'string', []); var compiler = solc(self.Module);
if ('_compileJSONCallback' in Module) {
var compileJSONInternal = Module.cwrap('compileJSONCallback', 'string', ['string', 'number', 'number']);
var missingInputCallback = Module.Runtime.addFunction(function (path) {
missingInputs.push(Module.Pointer_stringify(path));
});
compileJSON = function (input, optimize) { compileJSON = function (input, optimize) {
return compileJSONInternal(input, optimize, missingInputCallback); return JSON.stringify(compiler.compile(JSON.parse(input), optimize, function (path) {
missingInputs.push(path);
return { 'error': 'Deferred import' };
}));
}; };
} else if ('_compileJSONMulti' in Module) {
compileJSON = Module.cwrap('compileJSONMulti', 'string', ['string', 'number']);
} else {
compileJSON = Module.cwrap('compileJSON', 'string', ['string', 'number']);
}
self.postMessage({ self.postMessage({
cmd: 'versionLoaded', cmd: 'versionLoaded',
data: version(), data: compiler.version(),
acceptsMultipleFiles: ('_compileJSONMulti' in Module) acceptsMultipleFiles: compiler.supportsMulti
}); });
break; break;
case 'compile': case 'compile':

@ -1,3 +1,5 @@
var solc = require('solc/wrapper');
var webworkify = require('webworkify'); var webworkify = require('webworkify');
var utils = require('./utils'); var utils = require('./utils');
@ -65,49 +67,34 @@ function Compiler (editor, renderer, queryParams, handleGithubCall, outputField,
function onInternalCompilerLoaded (setVersionText) { function onInternalCompilerLoaded (setVersionText) {
if (worker === null) { if (worker === null) {
var compile; var compiler = solc(window.Module);
compilerAcceptsMultipleFiles = compiler.supportsMulti;
compileJSON = function (source, optimize, cb) {
var missingInputs = []; var missingInputs = [];
var Module = window.Module; var missingInputsCallback = function (path) {
if ('_compileJSONCallback' in Module) { missingInputs.push(path);
compilerAcceptsMultipleFiles = true; return { error: 'Deferred import' };
var missingInputsCallback = Module.Runtime.addFunction(function (path, contents, error) {
missingInputs.push(Module.Pointer_stringify(path));
});
var compileInternal = Module.cwrap('compileJSONCallback', 'string', [ 'string', 'number', 'number' ]);
compile = function (input, optimize) {
missingInputs.length = 0;
return compileInternal(input, optimize, missingInputsCallback);
}; };
} else if ('_compileJSONMulti' in Module) {
compilerAcceptsMultipleFiles = true; var result;
compile = Module.cwrap('compileJSONMulti', 'string', [ 'string', 'number' ]);
} else {
compilerAcceptsMultipleFiles = false;
compile = Module.cwrap('compileJSON', 'string', [ 'string', 'number' ]);
}
setCompileJSON(function (source, optimize, cb) {
try { try {
var result = compile(source, optimize); result = compiler.compile(source, optimize, missingInputsCallback);
} catch (exception) { } catch (exception) {
result = JSON.stringify({ error: 'Uncaught JavaScript exception:\n' + exception }); result = { error: 'Uncaught JavaScript exception:\n' + exception };
} }
compilationFinished(result, missingInputs); compilationFinished(result, missingInputs);
}); };
onCompilerLoaded(setVersionText, Module.cwrap('version', 'string', [])());
onCompilerLoaded(setVersionText, compiler.version());
} }
} }
function compilationFinished (result, missingInputs) { function compilationFinished (data, missingInputs) {
var data;
var noFatalErrors = true; // ie warnings are ok var noFatalErrors = true; // ie warnings are ok
try {
data = JSON.parse(result);
} catch (exception) {
renderer.error('Invalid JSON output from the compiler: ' + exception);
return;
}
if (data['error'] !== undefined) { if (data['error'] !== undefined) {
renderer.error(data['error']); renderer.error(data['error']);
if (utils.errortype(data['error']) !== 'warning') { if (utils.errortype(data['error']) !== 'warning') {
@ -132,6 +119,7 @@ function Compiler (editor, renderer, queryParams, handleGithubCall, outputField,
this.loadVersion = function (usingWorker, version, setVersionText) { this.loadVersion = function (usingWorker, version, setVersionText) {
var url = 'https://ethereum.github.io/solc-bin/bin/' + version; var url = 'https://ethereum.github.io/solc-bin/bin/' + version;
console.log('Loading ' + url + ' ' + (usingWorker ? 'with worker' : 'without worker'));
if (usingWorker) { if (usingWorker) {
loadWorker(url, setVersionText); loadWorker(url, setVersionText);
@ -171,15 +159,21 @@ function Compiler (editor, renderer, queryParams, handleGithubCall, outputField,
onCompilerLoaded(setVersionText, data.data); onCompilerLoaded(setVersionText, data.data);
break; break;
case 'compiled': case 'compiled':
compilationFinished(data.data, data.missingInputs); var result;
try {
result = JSON.parse(data.data);
} catch (exception) {
result = { 'error': 'Invalid JSON output from the compiler: ' + exception };
}
compilationFinished(result, data.missingInputs);
break; break;
} }
}); });
worker.onerror = function (msg) { console.log(msg.data); }; worker.onerror = function (msg) { console.log(msg.data); };
worker.addEventListener('error', function (msg) { console.log(msg.data); }); worker.addEventListener('error', function (msg) { console.log(msg.data); });
setCompileJSON(function (source, optimize) { compileJSON = function (source, optimize) {
worker.postMessage({cmd: 'compile', source: source, optimize: optimize}); worker.postMessage({cmd: 'compile', source: JSON.stringify(source), optimize: optimize});
}); };
worker.postMessage({cmd: 'loadVersion', data: url}); worker.postMessage({cmd: 'loadVersion', data: url});
} }
@ -235,7 +229,7 @@ function Compiler (editor, renderer, queryParams, handleGithubCall, outputField,
} }
} }
} while (reloop); } while (reloop);
cb(JSON.stringify({ 'sources': files })); cb({ 'sources': files });
} }
} }

Loading…
Cancel
Save