Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save icculus/369126fc3c1b5aeb8c79b6e6c2472f79 to your computer and use it in GitHub Desktop.
Save icculus/369126fc3c1b5aeb8c79b6e6c2472f79 to your computer and use it in GitHub Desktop.
Choose WebAssembly or asm.js at runtime
var hasWebAssembly = false;
if (typeof WebAssembly==="object" && typeof WebAssembly.Memory==="function") {
hasWebAssembly = true;
}
//console.log("Do we have WebAssembly? " + ((hasWebAssembly) ? "YES" : "NO"));
var buildtype = hasWebAssembly ? "wasm" : "asmjs";
var module = "dragonruby-" + buildtype + ".js";
//console.log("Our main module is: " + module);
var script = document.createElement('script');
script.src = module;
if (hasWebAssembly) {
script.async = true;
} else {
script.async = false; // !!! FIXME: can this be async?
(function() {
var memoryInitializer = module + '.mem';
if (typeof Module['locateFile'] === 'function') {
memoryInitializer = Module['locateFile'](memoryInitializer);
} else if (Module['memoryInitializerPrefixURL']) {
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
}
var meminitXHR = Module['memoryInitializerRequest'] = new XMLHttpRequest();
meminitXHR.open('GET', memoryInitializer, true);
meminitXHR.responseType = 'arraybuffer';
meminitXHR.send(null);
})();
}
document.body.appendChild(script);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment