Merge pull request #54 from ethereum/webworkify

Cleanup compiler (webworkify)
pull/1/head
chriseth 9 years ago
commit 7d66946ac3
  1. 3
      package.json
  2. 14
      src/app.js
  3. 42
      src/app/compiler-worker.js
  4. 55
      src/app/compiler.js
  5. 39
      worker.js

@ -17,7 +17,8 @@
"jquery": "^2.2.0",
"brace": "^0.8.0",
"browserify": "^13.0.0",
"js-base64": "^2.1.9"
"js-base64": "^2.1.9",
"webworkify": "^1.2.1"
},
"repository": {
"type": "git",

@ -443,19 +443,9 @@ var run = function () {
// Workers cannot load js on "file:"-URLs and we get a
// "Uncaught RangeError: Maximum call stack size exceeded" error on Chromium,
// resort to non-worker version in that case.
compiler.initializeWorker(version, setVersionText);
compiler.loadVersion(true, version, setVersionText);
} else {
Module = null;
compiler.setCompileJSON()
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = 'https://ethereum.github.io/solc-bin/bin/' + version;
document.getElementsByTagName('head')[0].appendChild(newScript);
var check = window.setInterval(function () {
if (!Module) return;
window.clearInterval(check);
compiler.onCompilerLoaded(setVersionText);
}, 200);
compiler.loadVersion(false, version, setVersionText);
}
};

@ -0,0 +1,42 @@
var version = function() { return '(loading)'; }
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 Module;
version = null;
compileJSON = null;
importScripts(data.data);
version = Module.cwrap("version", "string", []);
if ('_compileJSONCallback' in Module)
{
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) {
return compileJSONInternal(input, optimize, missingInputCallback);
};
}
else if ('_compileJSONMulti' in Module)
compileJSON = Module.cwrap("compileJSONMulti", "string", ["string", "number"]);
else
compileJSON = Module.cwrap("compileJSON", "string", ["string", "number"]);
postMessage({
cmd: 'versionLoaded',
data: version(),
acceptsMultipleFiles: ('_compileJSONMulti' in Module)
});
break;
case 'compile':
missingInputs.length = 0;
postMessage({cmd: 'compiled', data: compileJSON(data.source, data.optimize), missingInputs: missingInputs});
break;
}
}, false);
}

@ -1,3 +1,4 @@
var webworkify = require('webworkify');
var queryParams = require('./query-params');
var utils = require('./utils');
var Renderer = require('./renderer');
@ -62,11 +63,13 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
editor.setAnnotations(sourceAnnotations);
};
this.setCompileJSON = function () {
compileJSON = function (source, optimize) { compilationFinished('{}'); };
};
function onCompilerLoaded (setVersionText, version) {
setVersionText(version);
previousInput = '';
onChange();
}
function onCompilerLoaded (setVersionText) {
function onInternalCompilerLoaded (setVersionText) {
if (worker === null) {
var compile;
var missingInputs = [];
@ -95,12 +98,9 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
}
compilationFinished(result, missingInputs);
};
setVersionText(Module.cwrap('version', 'string', [])());
onCompilerLoaded(setVersionText, Module.cwrap('version', 'string', [])());
}
previousInput = '';
onChange();
};
this.onCompilerLoaded = onCompilerLoaded;
}
function compilationFinished (result, missingInputs) {
var data;
@ -135,18 +135,45 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
}
}
this.initializeWorker = function (version, setVersionText) {
this.loadVersion = function (usingWorker, version, setVersionText) {
var url = 'https://ethereum.github.io/solc-bin/bin/' + version;
if (usingWorker) {
loadWorker(url, setVersionText);
} else {
loadInternal(url, setVersionText);
}
};
function loadInternal (url, setVersionText) {
Module = null;
// Set a safe fallback until the new one is loaded
compileJSON = function(source, optimize) { compilationFinished('{}'); };
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = url;
document.getElementsByTagName('head')[0].appendChild(newScript);
var check = window.setInterval(function () {
if (!Module) {
return;
}
window.clearInterval(check);
onInternalCompilerLoaded(setVersionText);
}, 200);
}
function loadWorker (url, setVersionText) {
if (worker !== null) {
worker.terminate();
}
worker = new Worker('worker.js');
worker = webworkify(require('./compiler-worker.js'));
worker.addEventListener('message', function (msg) {
var data = msg.data;
switch (data.cmd) {
case 'versionLoaded':
setVersionText(data.data);
compilerAcceptsMultipleFiles = !!data.acceptsMultipleFiles;
onCompilerLoaded(setVersionText);
onCompilerLoaded(setVersionText, data.data);
break;
case 'compiled':
compilationFinished(data.data, data.missingInputs);
@ -158,7 +185,7 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
compileJSON = function (source, optimize) {
worker.postMessage({cmd: 'compile', source: source, optimize: optimize});
};
worker.postMessage({cmd: 'loadVersion', data: 'https://ethereum.github.io/solc-bin/bin/' + version});
worker.postMessage({cmd: 'loadVersion', data: url});
};
function gatherImports (files, importHints, cb) {

@ -1,39 +0,0 @@
var version = function() { return '(loading)'; }
var compileJSON = function() { return ''; }
var missingInputs = [];
addEventListener('message', function(e) {
var data = e.data;
switch (data.cmd) {
case 'loadVersion':
delete Module;
version = null;
compileJSON = null;
importScripts(data.data);
version = Module.cwrap("version", "string", []);
if ('_compileJSONCallback' in Module)
{
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) {
return compileJSONInternal(input, optimize, missingInputCallback);
};
}
else if ('_compileJSONMulti' in Module)
compileJSON = Module.cwrap("compileJSONMulti", "string", ["string", "number"]);
else
compileJSON = Module.cwrap("compileJSON", "string", ["string", "number"]);
postMessage({
cmd: 'versionLoaded',
data: version(),
acceptsMultipleFiles: ('_compileJSONMulti' in Module)
});
break;
case 'compile':
missingInputs.length = 0;
postMessage({cmd: 'compiled', data: compileJSON(data.source, data.optimize), missingInputs: missingInputs});
break;
}
}, false);
Loading…
Cancel
Save