Commit 37c7e817 authored by Dave Hoover's avatar Dave Hoover

Smoke testing compiler.compile and discovering some simplifications

parent e8d34daa
...@@ -10,7 +10,9 @@ var gistHandler = new GistHandler(); ...@@ -10,7 +10,9 @@ var gistHandler = new GistHandler();
var StorageHandler = require('./app/storage-handler'); var StorageHandler = require('./app/storage-handler');
var Editor = require('./app/editor'); var Editor = require('./app/editor');
var Renderer = require('./app/renderer');
var Compiler = require('./app/compiler'); var Compiler = require('./app/compiler');
var ExecutionContext = require('./app/execution-context');
// The event listener needs to be registered as early as possible, because the // The event listener needs to be registered as early as possible, because the
// parent will send the message upon the "load" event. // parent will send the message upon the "load" event.
...@@ -423,7 +425,10 @@ var run = function () { ...@@ -423,7 +425,10 @@ var run = function () {
return $.getJSON('https://api.github.com/repos/' + root + '/contents/' + path, cb); return $.getJSON('https://api.github.com/repos/' + root + '/contents/' + path, cb);
} }
var compiler = new Compiler(editor, handleGithubCall, $('#output'), getHidingRHP, updateFiles); var executionContext = new ExecutionContext();
var renderer = new Renderer(editor, executionContext, updateFiles);
var compiler = new Compiler(editor, renderer, queryParams, handleGithubCall, $('#output'), getHidingRHP, updateFiles);
executionContext.setCompiler(compiler);
function setVersionText (text) { function setVersionText (text) {
$('#version').text(text); $('#version').text(text);
......
var webworkify = require('webworkify'); var webworkify = require('webworkify');
var QueryParams = require('./query-params');
var utils = require('./utils'); var utils = require('./utils');
var Renderer = require('./renderer');
var Base64 = require('js-base64').Base64; var Base64 = require('js-base64').Base64;
function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles) { function Compiler (editor, renderer, queryParams, handleGithubCall, outputField, hidingRHP, updateFiles) {
var renderer = new Renderer(editor, this, updateFiles);
var queryParams = new QueryParams();
var compileJSON; var compileJSON;
var compilerAcceptsMultipleFiles; var compilerAcceptsMultipleFiles;
var previousInput = ''; var previousInput = '';
var sourceAnnotations = [];
var cachedRemoteFiles = {}; var cachedRemoteFiles = {};
var worker = null; var worker = null;
...@@ -40,7 +34,6 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles ...@@ -40,7 +34,6 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
var compile = function (missingInputs) { var compile = function (missingInputs) {
editor.clearAnnotations(); editor.clearAnnotations();
sourceAnnotations = [];
outputField.empty(); outputField.empty();
var input = editor.getValue(); var input = editor.getValue();
editor.setCacheFileContent(input); editor.setCacheFileContent(input);
...@@ -59,10 +52,10 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles ...@@ -59,10 +52,10 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
}; };
this.compile = compile; this.compile = compile;
this.addAnnotation = function (annotation) { function setCompileJSON (_compileJSON) {
sourceAnnotations[sourceAnnotations.length] = annotation; compileJSON = _compileJSON;
editor.setAnnotations(sourceAnnotations); }
}; this.setCompileJSON = setCompileJSON; // this is exposed for testing
function onCompilerLoaded (setVersionText, version) { function onCompilerLoaded (setVersionText, version) {
setVersionText(version); setVersionText(version);
...@@ -92,14 +85,14 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles ...@@ -92,14 +85,14 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
compilerAcceptsMultipleFiles = false; compilerAcceptsMultipleFiles = false;
compile = Module.cwrap('compileJSON', 'string', [ 'string', 'number' ]); compile = Module.cwrap('compileJSON', 'string', [ 'string', 'number' ]);
} }
compileJSON = function (source, optimize, cb) { setCompileJSON(function (source, optimize, cb) {
try { try {
var result = compile(source, optimize); var result = compile(source, optimize);
} catch (exception) { } catch (exception) {
result = JSON.stringify({ error: 'Uncaught JavaScript exception:\n' + exception }); result = JSON.stringify({ error: 'Uncaught JavaScript exception:\n' + exception });
} }
compilationFinished(result, missingInputs); compilationFinished(result, missingInputs);
}; });
onCompilerLoaded(setVersionText, Module.cwrap('version', 'string', [])()); onCompilerLoaded(setVersionText, Module.cwrap('version', 'string', [])());
} }
} }
...@@ -150,7 +143,7 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles ...@@ -150,7 +143,7 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
function loadInternal (url, setVersionText) { function loadInternal (url, setVersionText) {
delete window.Module; delete window.Module;
// Set a safe fallback until the new one is loaded // Set a safe fallback until the new one is loaded
compileJSON = function (source, optimize) { compilationFinished('{}'); }; setCompileJSON(function (source, optimize) { compilationFinished('{}'); });
var newScript = document.createElement('script'); var newScript = document.createElement('script');
newScript.type = 'text/javascript'; newScript.type = 'text/javascript';
...@@ -184,9 +177,9 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles ...@@ -184,9 +177,9 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
}); });
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); });
compileJSON = function (source, optimize) { setCompileJSON(function (source, optimize) {
worker.postMessage({cmd: 'compile', source: source, optimize: optimize}); worker.postMessage({cmd: 'compile', source: source, optimize: optimize});
}; });
worker.postMessage({cmd: 'loadVersion', data: url}); worker.postMessage({cmd: 'loadVersion', data: url});
} }
......
...@@ -6,6 +6,15 @@ var ace = require('brace'); ...@@ -6,6 +6,15 @@ var ace = require('brace');
require('../mode-solidity.js'); require('../mode-solidity.js');
function Editor (loadingFromGist) { function Editor (loadingFromGist) {
var SOL_CACHE_UNTITLED = utils.getCacheFilePrefix() + 'Untitled';
var SOL_CACHE_FILE = null;
var editor = ace.edit('input');
var sessions = {};
var sourceAnnotations = [];
setupStuff(getFiles());
this.newFile = function () { this.newFile = function () {
var untitledCount = ''; var untitledCount = '';
while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) { while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) {
...@@ -58,7 +67,7 @@ function Editor (loadingFromGist) { ...@@ -58,7 +67,7 @@ function Editor (loadingFromGist) {
return this.getFiles().indexOf(utils.fileKey(name)) !== -1; return this.getFiles().indexOf(utils.fileKey(name)) !== -1;
}; };
this.getFiles = function () { function getFiles () {
var files = []; var files = [];
for (var f in window.localStorage) { for (var f in window.localStorage) {
if (f.indexOf(utils.getCacheFilePrefix(), 0) === 0) { if (f.indexOf(utils.getCacheFilePrefix(), 0) === 0) {
...@@ -67,7 +76,8 @@ function Editor (loadingFromGist) { ...@@ -67,7 +76,8 @@ function Editor (loadingFromGist) {
} }
} }
return files; return files;
}; }
this.getFiles = getFiles;
this.packageFiles = function () { this.packageFiles = function () {
var files = {}; var files = {};
...@@ -100,9 +110,15 @@ function Editor (loadingFromGist) { ...@@ -100,9 +110,15 @@ function Editor (loadingFromGist) {
}; };
this.clearAnnotations = function () { this.clearAnnotations = function () {
sourceAnnotations = [];
editor.getSession().clearAnnotations(); editor.getSession().clearAnnotations();
}; };
this.addAnnotation = function (annotation) {
sourceAnnotations[sourceAnnotations.length] = annotation;
this.setAnnotations(sourceAnnotations);
};
this.setAnnotations = function (sourceAnnotations) { this.setAnnotations = function (sourceAnnotations) {
editor.getSession().setAnnotations(sourceAnnotations); editor.getSession().setAnnotations(sourceAnnotations);
}; };
...@@ -152,14 +168,6 @@ function Editor (loadingFromGist) { ...@@ -152,14 +168,6 @@ function Editor (loadingFromGist) {
editor.setSession(sessions[SOL_CACHE_FILE]); editor.setSession(sessions[SOL_CACHE_FILE]);
editor.resize(true); editor.resize(true);
} }
var SOL_CACHE_UNTITLED = utils.getCacheFilePrefix() + 'Untitled';
var SOL_CACHE_FILE = null;
var editor = ace.edit('input');
var sessions = {};
setupStuff(this.getFiles());
} }
module.exports = Editor; module.exports = Editor;
...@@ -13,9 +13,14 @@ if (typeof window.web3 !== 'undefined') { ...@@ -13,9 +13,14 @@ if (typeof window.web3 !== 'undefined') {
web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
} }
function ExecutionContext (compiler) { function ExecutionContext () {
var compiler;
var executionContext = injectedProvider ? 'injected' : 'vm'; var executionContext = injectedProvider ? 'injected' : 'vm';
this.setCompiler = function (_compiler) {
compiler = _compiler;
};
this.isVM = function () { this.isVM = function () {
return executionContext === 'vm'; return executionContext === 'vm';
}; };
......
...@@ -3,11 +3,9 @@ var $ = require('jquery'); ...@@ -3,11 +3,9 @@ var $ = require('jquery');
var UniversalDApp = require('../universal-dapp.js'); var UniversalDApp = require('../universal-dapp.js');
var utils = require('./utils'); var utils = require('./utils');
var ExecutionContext = require('./execution-context');
function Renderer (editor, compiler, updateFiles) { function Renderer (editor, executionContext, updateFiles) {
var detailsOpen = {}; var detailsOpen = {};
var executionContext = new ExecutionContext(compiler);
function renderError (message) { function renderError (message) {
var type = utils.errortype(message); var type = utils.errortype(message);
...@@ -20,7 +18,7 @@ function Renderer (editor, compiler, updateFiles) { ...@@ -20,7 +18,7 @@ function Renderer (editor, compiler, updateFiles) {
var errLine = parseInt(err[2], 10) - 1; var errLine = parseInt(err[2], 10) - 1;
var errCol = err[4] ? parseInt(err[4], 10) : 0; var errCol = err[4] ? parseInt(err[4], 10) : 0;
if (errFile === '' || errFile === utils.fileNameFromKey(editor.getCacheFile())) { if (errFile === '' || errFile === utils.fileNameFromKey(editor.getCacheFile())) {
compiler.addAnnotation({ editor.addAnnotation({
row: errLine, row: errLine,
column: errCol, column: errCol,
text: message, text: message,
......
var test = require('tape');
var Compiler = require('../src/app/compiler');
test('compiler.compile smoke', function (t) {
t.plan(1);
var noop = function () {};
var fakeEditor = {onChangeSetup: noop, clearAnnotations: noop, getValue: noop, setCacheFileContent: noop, getCacheFile: noop};
var fakeOutputField = {empty: noop};
var fakeQueryParams = {get: function () { return {}; }};
var compiler = new Compiler(fakeEditor, null, fakeQueryParams, null, fakeOutputField);
compiler.setCompileJSON(noop);
compiler.compile();
t.ok(compiler);
});
require('./compiler-test');
require('./gist-handler-test'); require('./gist-handler-test');
require('./query-params-test'); require('./query-params-test');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment