Browse Source

Save multiple compiler states. Try and be backward compatible

refactor
Matt Godbolt 11 years ago
parent
commit
35156ffe4f
  1. 2
      etc/oldhash.txt
  2. 44
      static/compiler.js
  3. 40
      static/gcc.js

2
etc/oldhash.txt

@ -0,0 +1,2 @@
http://localhost:10240/#%7B%22version%22%3A2%2C%22source%22%3A%22%23include%20%3Cxmmintrin.h%3E%5Cn%5Cnvoid%20f(__m128%20a%2C%20__m128%20b)%5Cn%7B%5Cn%20%20%2F%2F%20I%20am%20a%20walrus.%5Cn%7D%22%2C%22compiler%22%3A%22%2Fhome%2Fmgodbolt%2Fapps%2Fintel-icc-oss%2Fbin%2Ficc%22%2C%22options%22%3A%22-O3%20-std%3Dc%2B%2B0x%22%2C%22filterAsm%22%3A%7B%22labels%22%3Atrue%2C%22directives%22%3Atrue%7D%7D
-- should be icc, "-O3 -std=c++0x", all filters but comments

44
static/compiler.js

@ -42,7 +42,7 @@ function clearBackground(cm) {
}
}
function Compiler(domRoot, origFilters) {
function Compiler(domRoot, origFilters, windowLocalPrefix, onChangeCallback) {
var compilersByExe = {};
var pendingTimeout = null;
var asmCodeMirror = null;
@ -66,13 +66,20 @@ function Compiler(domRoot, origFilters) {
readOnly: true
});
if (window.localStorage['code']) cppEditor.setValue(window.localStorage['code']);
function getSetting(name) {
return window.localStorage[windowLocalPrefix + "." + name];
}
function setSetting(name, value) {
window.localStorage[windowLocalPrefix + "." + name] = value;
}
if (getSetting('code')) cppEditor.setValue(getSetting('code'));
domRoot.find('.compiler').change(onCompilerChange);
domRoot.find('.compiler_options').change(onChange).keyup(onChange);
ignoreChanges = false;
if (window.localStorage['compilerOptions']) {
domRoot.find('.compiler_options').val(window.localStorage['compilerOptions']);
if (getSetting('compilerOptions')) {
domRoot.find('.compiler_options').val(getSetting('compilerOptions'));
}
function onCompileResponse(data) {
@ -149,8 +156,8 @@ function Compiler(domRoot, origFilters) {
options: $('.compiler_options').val(),
filters: filters
};
window.localStorage['compiler'] = data.compiler;
window.localStorage['compilerOptions'] = data.options;
setSetting('compiler', data.compiler);
setSetting('compilerOptions', data.options);
if (data == lastRequest) return;
lastRequest = data;
$.ajax({
@ -160,9 +167,9 @@ function Compiler(domRoot, origFilters) {
data: data,
success: onCompileResponse});
}, 750);
window.localStorage['code'] = cppEditor.getValue();
setSetting('code', cppEditor.getValue());
updateAsm();
$('a.permalink').attr('href', '#' + serialiseState());
onChangeCallback();
}
function setSource(code) {
@ -175,31 +182,21 @@ function Compiler(domRoot, origFilters) {
function serialiseState() {
var state = {
version: 2,
source: cppEditor.getValue(),
compiler: domRoot.find('.compiler').val(),
options: domRoot.find('.compiler_options').val(),
filterAsm: filters
};
return encodeURIComponent(JSON.stringify(state));
return state;
}
function deserialiseState(state) {
try {
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;
setSetting('compiler', state.compiler);
updateAsm(true); // Force the update to reset colours after calling cppEditor.setValue
return true;
}
@ -215,10 +212,10 @@ function Compiler(domRoot, origFilters) {
compilersByExe = {};
$.each(compilers, function(index, arg) {
compilersByExe[arg.exe] = arg;
$('.compiler').append($('<option value="' + arg.exe + '">' + arg.version + '</option>'));
domRoot.find('.compiler').append($('<option value="' + arg.exe + '">' + arg.version + '</option>'));
});
if (window.localStorage['compiler']) {
domRoot.find('.compiler').val(window.localStorage['compiler']);
if (getSetting('compiler')) {
domRoot.find('.compiler').val(getSetting('compiler'));
}
onCompilerChange();
}
@ -229,6 +226,7 @@ function Compiler(domRoot, origFilters) {
}
return {
serialiseState: serialiseState,
deserialiseState: deserialiseState,
setCompilers: setCompilers,
getSource: getSource,

40
static/gcc.js

@ -23,6 +23,7 @@
// POSSIBILITY OF SUCH DAMAGE.
var currentCompiler = null;
var allCompilers = [];
function getSource() {
var source = $('.source').val();
@ -119,12 +120,47 @@ function saveFileAs() {
});
}
function serialiseState() {
var state = {
version: 3,
filterAsm: getAsmFilters(),
compilers: $.map(allCompilers, function(compiler) { return compiler.serialiseState(); })
};
return encodeURIComponent(JSON.stringify(state));
}
function deserialiseState(state) {
try {
state = $.parseJSON(decodeURIComponent(state));
switch (state.version) {
case 1:
state.filterAsm = {};
// falls into
case 2:
state.compilers = [state];
// falls into
case 3:
break;
default:
return false;
}
} catch (ignored) { return false; }
setFilterUi(state.filterAsm);
for (var i = 0; i < Math.min(allCompilers.length, state.compilers.length); i++) {
allCompilers[i].deserialiseState(state.compilers[i]);
}
return true;
}
function initialise() {
var defaultFilters = JSON.stringify(getAsmFilters());
var actualFilters = $.parseJSON(window.localStorage['filter'] || defaultFilters);
setFilterUi(actualFilters);
var compiler = new Compiler($('body'), actualFilters);
var compiler = new Compiler($('body'), actualFilters, "a", function() {
$('a.permalink').attr('href', '#' + serialiseState());
});
allCompilers.push(compiler);
currentCompiler = compiler;
$('form').submit(function() { return false; });
@ -167,7 +203,7 @@ function initialise() {
});
function loadFromHash() {
compiler.deserialiseState(window.location.hash.substr(1));
deserialiseState(window.location.hash.substr(1));
}
$(window).bind('hashchange', function() {

Loading…
Cancel
Save