Smoke testing compiler.compile and discovering some simplifications

pull/1/head
Dave Hoover 9 years ago
parent e8d34daacf
commit 37c7e81733
  1. 7
      src/app.js
  2. 27
      src/app/compiler.js
  3. 28
      src/app/editor.js
  4. 7
      src/app/execution-context.js
  5. 6
      src/app/renderer.js
  6. 16
      test/compiler-test.js
  7. 1
      test/index.js

@ -10,7 +10,9 @@ var gistHandler = new GistHandler();
var StorageHandler = require('./app/storage-handler');
var Editor = require('./app/editor');
var Renderer = require('./app/renderer');
var Compiler = require('./app/compiler');
var ExecutionContext = require('./app/execution-context');
// The event listener needs to be registered as early as possible, because the
// parent will send the message upon the "load" event.
@ -423,7 +425,10 @@ var run = function () {
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) {
$('#version').text(text);

@ -1,19 +1,13 @@
var webworkify = require('webworkify');
var QueryParams = require('./query-params');
var utils = require('./utils');
var Renderer = require('./renderer');
var Base64 = require('js-base64').Base64;
function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles) {
var renderer = new Renderer(editor, this, updateFiles);
var queryParams = new QueryParams();
function Compiler (editor, renderer, queryParams, handleGithubCall, outputField, hidingRHP, updateFiles) {
var compileJSON;
var compilerAcceptsMultipleFiles;
var previousInput = '';
var sourceAnnotations = [];
var cachedRemoteFiles = {};
var worker = null;
@ -40,7 +34,6 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
var compile = function (missingInputs) {
editor.clearAnnotations();
sourceAnnotations = [];
outputField.empty();
var input = editor.getValue();
editor.setCacheFileContent(input);
@ -59,10 +52,10 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
};
this.compile = compile;
this.addAnnotation = function (annotation) {
sourceAnnotations[sourceAnnotations.length] = annotation;
editor.setAnnotations(sourceAnnotations);
};
function setCompileJSON (_compileJSON) {
compileJSON = _compileJSON;
}
this.setCompileJSON = setCompileJSON; // this is exposed for testing
function onCompilerLoaded (setVersionText, version) {
setVersionText(version);
@ -92,14 +85,14 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
compilerAcceptsMultipleFiles = false;
compile = Module.cwrap('compileJSON', 'string', [ 'string', 'number' ]);
}
compileJSON = function (source, optimize, cb) {
setCompileJSON(function (source, optimize, cb) {
try {
var result = compile(source, optimize);
} catch (exception) {
result = JSON.stringify({ error: 'Uncaught JavaScript exception:\n' + exception });
}
compilationFinished(result, missingInputs);
};
});
onCompilerLoaded(setVersionText, Module.cwrap('version', 'string', [])());
}
}
@ -150,7 +143,7 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
function loadInternal (url, setVersionText) {
delete window.Module;
// 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');
newScript.type = 'text/javascript';
@ -184,9 +177,9 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles
});
worker.onerror = 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: 'loadVersion', data: url});
}

@ -6,6 +6,15 @@ var ace = require('brace');
require('../mode-solidity.js');
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 () {
var untitledCount = '';
while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) {
@ -58,7 +67,7 @@ function Editor (loadingFromGist) {
return this.getFiles().indexOf(utils.fileKey(name)) !== -1;
};
this.getFiles = function () {
function getFiles () {
var files = [];
for (var f in window.localStorage) {
if (f.indexOf(utils.getCacheFilePrefix(), 0) === 0) {
@ -67,7 +76,8 @@ function Editor (loadingFromGist) {
}
}
return files;
};
}
this.getFiles = getFiles;
this.packageFiles = function () {
var files = {};
@ -100,9 +110,15 @@ function Editor (loadingFromGist) {
};
this.clearAnnotations = function () {
sourceAnnotations = [];
editor.getSession().clearAnnotations();
};
this.addAnnotation = function (annotation) {
sourceAnnotations[sourceAnnotations.length] = annotation;
this.setAnnotations(sourceAnnotations);
};
this.setAnnotations = function (sourceAnnotations) {
editor.getSession().setAnnotations(sourceAnnotations);
};
@ -152,14 +168,6 @@ function Editor (loadingFromGist) {
editor.setSession(sessions[SOL_CACHE_FILE]);
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;

@ -13,9 +13,14 @@ if (typeof window.web3 !== 'undefined') {
web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
}
function ExecutionContext (compiler) {
function ExecutionContext () {
var compiler;
var executionContext = injectedProvider ? 'injected' : 'vm';
this.setCompiler = function (_compiler) {
compiler = _compiler;
};
this.isVM = function () {
return executionContext === 'vm';
};

@ -3,11 +3,9 @@ var $ = require('jquery');
var UniversalDApp = require('../universal-dapp.js');
var utils = require('./utils');
var ExecutionContext = require('./execution-context');
function Renderer (editor, compiler, updateFiles) {
function Renderer (editor, executionContext, updateFiles) {
var detailsOpen = {};
var executionContext = new ExecutionContext(compiler);
function renderError (message) {
var type = utils.errortype(message);
@ -20,7 +18,7 @@ function Renderer (editor, compiler, updateFiles) {
var errLine = parseInt(err[2], 10) - 1;
var errCol = err[4] ? parseInt(err[4], 10) : 0;
if (errFile === '' || errFile === utils.fileNameFromKey(editor.getCacheFile())) {
compiler.addAnnotation({
editor.addAnnotation({
row: errLine,
column: errCol,
text: message,

@ -0,0 +1,16 @@
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);
});

@ -1,2 +1,3 @@
require('./compiler-test');
require('./gist-handler-test');
require('./query-params-test');

Loading…
Cancel
Save