diff --git a/static/gcc.js b/static/gcc.js index 5ce1c6e23..df1325427 100644 --- a/static/gcc.js +++ b/static/gcc.js @@ -22,14 +22,6 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -var pendingTimeout = null; -var asmCodeMirror = null; -var cppEditor = null; -var lastRequest = null; -var currentAssembly = null; -var ignoreChanges = false; -var compilersByExe = {}; - function parseLines(lines, callback) { var re = /^\:([0-9]+):([0-9]+):\s+(.*)/; $.each(lines.split('\n'), function(_, line) { @@ -50,98 +42,6 @@ function clearBackground(cm) { } } -function onCompileResponse(data) { - var stdout = data.stdout || ""; - var stderr = data.stderr || ""; - if (data.code == 0) { - stdout += "\nCompiled ok"; - } else { - stderr += "\nCompilation failed"; - } - $('.result .output :visible').remove(); - var highlightLine = (data.asm == null); - for (var i = 0; i < cppEditor.lineCount(); ++i) cppEditor.setMarker(i); - parseLines(stderr + stdout, function(lineNum, msg) { - var elem = $('.result .output .template').clone().appendTo('.result .output').removeClass('template'); - if (lineNum) { - cppEditor.setMarker(lineNum - 1, null, "error"); - elem.html($('').append(lineNum + " : " + msg)).click(function() { - cppEditor.setSelection({line: lineNum - 1, ch: 0}, {line: lineNum, ch: 0}); - return false; - }); - } else { - elem.text(msg); - } - }); - currentAssembly = data.asm || "[no output]"; - updateAsm(); -} - -function numberUsedLines(asm) { - var sourceLines = {}; - $.each(asm, function(_, x) { if (x.source) sourceLines[x.source - 1] = true; }); - var ordinal = 0; - $.each(sourceLines, function(k, _) { sourceLines[k] = ordinal++; }); - var asmLines = {}; - $.each(asm, function(index, x) { if (x.source) asmLines[index] = sourceLines[x.source - 1]; }); - return { source: sourceLines, asm: asmLines }; -} - -var lastUpdatedAsm = null; -function updateAsm(forceUpdate) { - if (!currentAssembly) return; - var newFilters = getAsmFilters(); - var hashedUpdate = JSON.stringify({ - asm: currentAssembly, - filter: newFilters - }); - if (!forceUpdate && lastUpdatedAsm == hashedUpdate) { return; } - lastUpdatedAsm = hashedUpdate; - - var asm = processAsm(currentAssembly, newFilters); - var asmText = $.map(asm, function(x){ return x.text; }).join("\n"); - var numberedLines = numberUsedLines(asm); - asmCodeMirror.setValue(asmText); - - clearBackground(cppEditor); - clearBackground(asmCodeMirror); - if (newFilters.colouriseAsm) { - $.each(numberedLines.source, function(line, ordinal) { - cppEditor.setLineClass(parseInt(line), null, "rainbow-" + (ordinal & 7)); - }); - $.each(numberedLines.asm, function(line, ordinal) { - asmCodeMirror.setLineClass(parseInt(line), null, "rainbow-" + (ordinal & 7)); - }); - } -} - -function onChange() { - if (ignoreChanges) return; // Ugly hack during startup. - if (pendingTimeout) clearTimeout(pendingTimeout); - pendingTimeout = setTimeout(function() { - var data = { - source: cppEditor.getValue(), - compiler: $('.compiler').val(), - options: $('.compiler_options').val(), - filters: getAsmFilters() - }; - window.localStorage['compiler'] = data.compiler; - window.localStorage['compilerOptions'] = data.options; - if (data == lastRequest) return; - lastRequest = data; - $.ajax({ - type: 'POST', - url: '/compile', - dataType: 'json', - data: data, - success: onCompileResponse}); - }, 750); - window.localStorage['code'] = cppEditor.getValue(); - window.localStorage['filter'] = JSON.stringify(getAsmFilters()); - updateAsm(); - $('a.permalink').attr('href', '#' + serialiseState()); -} - function getSource() { var source = $('.source').val(); if (source == "browser") { @@ -172,6 +72,8 @@ function getSource() { } } +var compilersByExe = {}; + var currentFileList = {}; function updateFileList() { getSource().list(function(results) { @@ -190,100 +92,22 @@ function onSourceChange() { window.localStorage['source'] = $('.source').val(); } -function loadFile() { - var name = $('.filename').val(); - window.localStorage['filename'] = name; - getSource().load(name, function(results) { - if (results.file) { - cppEditor.setValue(results.file); - } else { - // TODO: error? - console.log(results); - } - }); -} +function Compiler(domRoot) { + var pendingTimeout = null; + var asmCodeMirror = null; + var cppEditor = null; + var lastRequest = null; + var currentAssembly = null; + var ignoreChanges = true; // Horrible hack to avoid onChange doing anything on first starting, ie before we've set anything up. -function saveFile() { - saveAs($('.files .filename').val()); -} - -function saveAs(filename) { - var prevFilename = window.localStorage['filename'] || ""; - if (filename != prevFilename && currentFileList[filename]) { - // TODO! - alert("Coming soon - overwriting files"); - return; - } - var obj = { urlpart: filename, name: filename, file: cppEditor.getValue() }; - getSource().save(obj, function(ok) { - if (ok) { - window.localStorage['filename'] = filename; - updateFileList(); - } - }); -} - -function saveFileAs() { - $('#saveDialog').modal(); - $('#saveDialog .save-filename').val($('.files .filename').val()); - $('#saveDialog .save-filename').focus(); - function onSave() { - $('#saveDialog').modal('hide'); - saveAs($('#saveDialog .save-filename').val()); - }; - $('#saveDialog .save').click(onSave); - $('#saveDialog .save-filename').keyup(function(event) { - if (event.keyCode == 13) onSave(); - }); -} - -function serialiseState() { - var state = { - version: 2, - source: cppEditor.getValue(), - compiler: $('.compiler').val(), - options: $('.compiler_options').val(), - filterAsm: getAsmFilters() - }; - return encodeURIComponent(JSON.stringify(state)); -} - -function deserialiseState(state) { - try { - var state = $.parseJSON(decodeURIComponent(state)); - if (state.version == 1) { - state.filterAsm = {}; - } - else if (state.version != 2) return false; - } catch (ignored) { return false; } - cppEditor.setValue(state.source); - $('.compiler').val(state.compiler); - $('.compiler_options').val(state.options); - setFilterUi(state.filterAsm); - // Somewhat hackily persist compiler into local storage else when the ajax response comes in - // with the list of compilers it can splat over the deserialized version. - // The whole serialize/hash/localStorage code is a mess! TODO(mg): fix - window.localStorage['compiler'] = state.compiler; - updateAsm(true); // Force the update to reset colours after calling cppEditor.setValue - return true; -} - -function onCompilerChange() { - onChange(); - var compiler = compilersByExe[$('.compiler').val()]; - $('.filter button.btn[value="intel"]').toggleClass("disabled", !compiler.supportedOpts["-masm"]); -} - -function initialise() { - ignoreChanges = true; // Horrible hack to avoid onChange being called on first starting, ie before we've set anything up. - cppEditor = CodeMirror.fromTextArea($("#c")[0], { + cppEditor = CodeMirror.fromTextArea(domRoot.find(".editor textarea")[0], { lineNumbers: true, matchBrackets: true, useCPP: true, mode: "text/x-c++src", onChange: onChange }); - asmCodeMirror = CodeMirror.fromTextArea($(".asm textarea")[0], { + asmCodeMirror = CodeMirror.fromTextArea(domRoot.find(".asm textarea")[0], { lineNumbers: true, matchBrackets: true, mode: "text/x-asm", @@ -291,26 +115,224 @@ function initialise() { }); if (window.localStorage['code']) cppEditor.setValue(window.localStorage['code']); + domRoot.find('.compiler').change(onCompilerChange); + domRoot.find('.compiler_options').change(onChange).keyup(onChange); + ignoreChanges = false; + + $('.filter button.btn').click(function(e) { + $(e.target).toggleClass('active'); + onChange(); + }); + + function onCompileResponse(data) { + var stdout = data.stdout || ""; + var stderr = data.stderr || ""; + if (data.code == 0) { + stdout += "\nCompiled ok"; + } else { + stderr += "\nCompilation failed"; + } + $('.result .output :visible').remove(); + var highlightLine = (data.asm == null); + for (var i = 0; i < cppEditor.lineCount(); ++i) cppEditor.setMarker(i); + parseLines(stderr + stdout, function(lineNum, msg) { + var elem = $('.result .output .template').clone().appendTo('.result .output').removeClass('template'); + if (lineNum) { + cppEditor.setMarker(lineNum - 1, null, "error"); + elem.html($('').append(lineNum + " : " + msg)).click(function() { + cppEditor.setSelection({line: lineNum - 1, ch: 0}, {line: lineNum, ch: 0}); + return false; + }); + } else { + elem.text(msg); + } + }); + currentAssembly = data.asm || "[no output]"; + updateAsm(); + } + + function numberUsedLines(asm) { + var sourceLines = {}; + $.each(asm, function(_, x) { if (x.source) sourceLines[x.source - 1] = true; }); + var ordinal = 0; + $.each(sourceLines, function(k, _) { sourceLines[k] = ordinal++; }); + var asmLines = {}; + $.each(asm, function(index, x) { if (x.source) asmLines[index] = sourceLines[x.source - 1]; }); + return { source: sourceLines, asm: asmLines }; + } + + var lastUpdatedAsm = null; + function updateAsm(forceUpdate) { + if (!currentAssembly) return; + var newFilters = getAsmFilters(); + var hashedUpdate = JSON.stringify({ + asm: currentAssembly, + filter: newFilters + }); + if (!forceUpdate && lastUpdatedAsm == hashedUpdate) { return; } + lastUpdatedAsm = hashedUpdate; + + var asm = processAsm(currentAssembly, newFilters); + var asmText = $.map(asm, function(x){ return x.text; }).join("\n"); + var numberedLines = numberUsedLines(asm); + asmCodeMirror.setValue(asmText); + + clearBackground(cppEditor); + clearBackground(asmCodeMirror); + if (newFilters.colouriseAsm) { + $.each(numberedLines.source, function(line, ordinal) { + cppEditor.setLineClass(parseInt(line), null, "rainbow-" + (ordinal & 7)); + }); + $.each(numberedLines.asm, function(line, ordinal) { + asmCodeMirror.setLineClass(parseInt(line), null, "rainbow-" + (ordinal & 7)); + }); + } + } + + function onChange() { + if (ignoreChanges) return; // Ugly hack during startup. + if (pendingTimeout) clearTimeout(pendingTimeout); + pendingTimeout = setTimeout(function() { + var data = { + source: cppEditor.getValue(), + compiler: $('.compiler').val(), + options: $('.compiler_options').val(), + filters: getAsmFilters() + }; + window.localStorage['compiler'] = data.compiler; + window.localStorage['compilerOptions'] = data.options; + if (data == lastRequest) return; + lastRequest = data; + $.ajax({ + type: 'POST', + url: '/compile', + dataType: 'json', + data: data, + success: onCompileResponse}); + }, 750); + window.localStorage['code'] = cppEditor.getValue(); + window.localStorage['filter'] = JSON.stringify(getAsmFilters()); + updateAsm(); + $('a.permalink').attr('href', '#' + serialiseState()); + } + + function loadFile() { + var name = $('.filename').val(); + window.localStorage['filename'] = name; + getSource().load(name, function(results) { + if (results.file) { + cppEditor.setValue(results.file); + } else { + // TODO: error? + console.log(results); + } + }); + } + + function saveFile() { + saveAs($('.files .filename').val()); + } + + function saveAs(filename) { + var prevFilename = window.localStorage['filename'] || ""; + if (filename != prevFilename && currentFileList[filename]) { + // TODO! + alert("Coming soon - overwriting files"); + return; + } + var obj = { urlpart: filename, name: filename, file: cppEditor.getValue() }; + getSource().save(obj, function(ok) { + if (ok) { + window.localStorage['filename'] = filename; + updateFileList(); + } + }); + } + + function saveFileAs() { + $('#saveDialog').modal(); + $('#saveDialog .save-filename').val($('.files .filename').val()); + $('#saveDialog .save-filename').focus(); + function onSave() { + $('#saveDialog').modal('hide'); + saveAs($('#saveDialog .save-filename').val()); + }; + $('#saveDialog .save').click(onSave); + $('#saveDialog .save-filename').keyup(function(event) { + if (event.keyCode == 13) onSave(); + }); + } + + function serialiseState() { + var state = { + version: 2, + source: cppEditor.getValue(), + compiler: domRoot.find('.compiler').val(), + options: domRoot.find('.compiler_options').val(), + filterAsm: getAsmFilters() + }; + return encodeURIComponent(JSON.stringify(state)); + } + + function deserialiseState(state) { + try { + var state = $.parseJSON(decodeURIComponent(state)); + if (state.version == 1) { + state.filterAsm = {}; + } + else if (state.version != 2) return false; + } catch (ignored) { return false; } + cppEditor.setValue(state.source); + domRoot.find('.compiler').val(state.compiler); + domRoot.find('.compiler_options').val(state.options); + setFilterUi(state.filterAsm); + // Somewhat hackily persist compiler into local storage else when the ajax response comes in + // with the list of compilers it can splat over the deserialized version. + // The whole serialize/hash/localStorage code is a mess! TODO(mg): fix + window.localStorage['compiler'] = state.compiler; + updateAsm(true); // Force the update to reset colours after calling cppEditor.setValue + return true; + } + + function onCompilerChange() { + onChange(); + var compiler = compilersByExe[$('.compiler').val()]; + domRoot.find('.filter button.btn[value="intel"]').toggleClass("disabled", !compiler.supportedOpts["-masm"]); + } + + function setCompilers(compilers) { + domRoot.find('.compiler option').remove(); + $.each(compilers, function(index, arg) { + $('.compiler').append($('')); + }); + onCompilerChange(); + } + + return { + deserialiseState: deserialiseState, + setCompilers: setCompilers, + loadFile: loadFile, + saveFile: saveFile, + saveFileAs: saveFileAs + }; +} + +function initialise() { + var compiler = new Compiler($('body')); + if (window.localStorage['compilerOptions']) $('.compiler_options').val(window.localStorage['compilerOptions']); var defaultFilters = JSON.stringify(getAsmFilters()); setFilterUi($.parseJSON(window.localStorage['filter'] || defaultFilters)); $('form').submit(function() { return false; }); - $('.compiler').change(onCompilerChange); - $('.compiler_options').change(onChange).keyup(onChange); + $('.files .source').change(onSourceChange); $.getJSON("/compilers", function(results) { - $('.compiler option').remove(); compilersByExe = {}; $.each(results, function(index, arg) { - $('.compiler').append($('')); compilersByExe[arg.exe] = arg; - if (window.localStorage['compiler'] == arg.exe) { - $('.compiler').val(arg.exe); - } }); - onCompilerChange(); + compiler.setCompilers(results); }); - $('.files .source').change(onSourceChange); $.getJSON("/sources", function(results) { $('.source option').remove(); $.each(results, function(index, arg) { @@ -322,25 +344,20 @@ function initialise() { onSourceChange(); }); $('.files .load').click(function() { - loadFile(); + compiler.loadFile(); return false; }); $('.files .save').click(function() { - saveFile(); + compiler.saveFile(); return false; }); $('.files .saveas').click(function() { - saveFileAs(); + compiler.saveFileAs(); return false; }); - $('.filter button.btn').click(function(e) { - $(e.target).toggleClass('active'); - onChange(); - }); - function loadFromHash() { - deserialiseState(window.location.hash.substr(1)); + compiler.deserialiseState(window.location.hash.substr(1)); } $(window).bind('hashchange', function() { diff --git a/static/index.html b/static/index.html index 9045f5866..5f0268b5f 100644 --- a/static/index.html +++ b/static/index.html @@ -86,7 +86,7 @@
C++ editor
- +
Assembly output