Browse Source

Platform: allow to reuse existing Emscripten Module properties.

By wrapping the module creation in a function that includes properties
from the original module as well as allowing to override the default
properties from the outside. This also means it's possible to create
differently named modules for multiple applications on the page.
pull/482/head
Pablo Escobar 6 years ago committed by Vladimír Vondruš
parent
commit
673a17fa11
  1. 71
      src/Magnum/Platform/EmscriptenApplication.js
  2. 77
      src/Magnum/Platform/WindowlessEmscriptenApplication.js

71
src/Magnum/Platform/EmscriptenApplication.js

@ -25,25 +25,30 @@
"use strict"; /* it summons the Cthulhu in a proper way, they say */ "use strict"; /* it summons the Cthulhu in a proper way, they say */
var Module = { function createMagnumModule(init) {
/* Take the Emscripten-supplied Module object as a base */
const module = Object.assign({}, Module);
/* Update it with our things */
Object.assign(module, {
preRun: [], preRun: [],
postRun: [], postRun: [],
arguments: [], arguments: [],
printErr: function(message) { printErr: function(_message) {
console.error(Array.prototype.slice.call(arguments).join(' ')); console.error(Array.prototype.slice.call(arguments).join(' '));
}, },
print: function(message) { print: function(_message) {
console.log(Array.prototype.slice.call(arguments).join(' ')); console.log(Array.prototype.slice.call(arguments).join(' '));
}, },
onAbort: function() { onAbort: function() {
Module.canvas.style.opacity = 0.333; module.canvas.style.opacity = 0.333;
Module.canvas.style.zIndex = -1; module.canvas.style.zIndex = -1;
Module.setStatus("Oops :("); module.setStatus("Oops :(");
Module.setStatusDescription("The app crashed. Refresh the page or check the browser console for details."); module.setStatusDescription("The app crashed. Refresh the page or check the browser console for details.");
}, },
canvas: document.getElementById('canvas'), canvas: document.getElementById('canvas'),
@ -53,13 +58,13 @@ var Module = {
setStatus: function(message) { setStatus: function(message) {
/* Emscripten calls setStatus("") after a timeout even if the app /* Emscripten calls setStatus("") after a timeout even if the app
aborts. That would erase the crash message, so don't allow that */ aborts. That would erase the crash message, so don't allow that */
if(Module.status && Module.status.innerHTML != "Oops :(") if(module.status && module.status.innerHTML != "Oops :(")
Module.status.innerHTML = message; module.status.innerHTML = message;
}, },
setStatusDescription: function(message) { setStatusDescription: function(message) {
if(Module.statusDescription) if(module.statusDescription)
Module.statusDescription.innerHTML = message; module.statusDescription.innerHTML = message;
}, },
totalDependencies: 0, totalDependencies: 0,
@ -68,27 +73,45 @@ var Module = {
this.totalDependencies = Math.max(this.totalDependencies, left); this.totalDependencies = Math.max(this.totalDependencies, left);
if(left) { if(left) {
Module.setStatus('Downloading...'); module.setStatus('Downloading...');
Module.setStatusDescription((this.totalDependencies - left) + ' / ' + this.totalDependencies); module.setStatusDescription((this.totalDependencies - left) + ' / ' + this.totalDependencies);
} else { } else {
Module.setStatus('Download complete'); module.setStatus('Download complete');
Module.setStatusDescription(''); module.setStatusDescription('');
} }
} }
}; });
/* Parse arguments, e.g. /app/?foo=bar&fizz&buzz=3 goes to the app as /* Parse arguments, e.g. /app/?foo=bar&fizz&buzz=3 goes to the app as
['--foo', 'bar', '--fizz', '--buzz', '3'] */ ['--foo', 'bar', '--fizz', '--buzz', '3'] */
var args = decodeURIComponent(window.location.search.substr(1)).trim().split('&'); const args = decodeURIComponent(window.location.search.substr(1)).trim().split('&');
for(var i = 0; i != args.length; ++i) { for(let i = 0; i != args.length; ++i) {
var j = args[i].indexOf('='); let j = args[i].indexOf('=');
/* Key + value */ /* Key + value */
if(j != -1) { if(j != -1) {
Module.arguments.push('--' + args[i].substring(0, j)); module.arguments.push('--' + args[i].substring(0, j));
Module.arguments.push(args[i].substring(j + 1)); module.arguments.push(args[i].substring(j + 1));
/* Just key */ /* Just key */
} else Module.arguments.push('--' + args[i]); } else module.arguments.push('--' + args[i]);
}
/* Let the user-supplied object overwrite all the above */
Object.assign(module, init);
/* We can do this here because at this point `module.status` should be correct */
module.setStatus("Downloading...");
return module;
} }
Module.setStatus('Downloading...'); /* Default global Module object */
var Module = createMagnumModule();
/* UMD export */
if(typeof exports === 'object' && typeof module === 'object') /* CommonJS/Node */
module.exports = createMagnumModule;
else if(typeof define === 'function' && define['amd']) /* AMD */
define([], function() { return createMagnumModule; });
else if(typeof exports === 'object') /* CommonJS strict */
exports["createMagnumModule"] = createMagnumModule;

77
src/Magnum/Platform/WindowlessEmscriptenApplication.js

@ -23,28 +23,33 @@
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
*/ */
var Module = { "use strict";
function createMagnumModule(init) {
const module = Object.assign({}, Module);
Object.assign(module, {
preRun: [], preRun: [],
postRun: [], postRun: [],
arguments: [], arguments: [],
doNotCaptureKeyboard: true, printErr: function(_message) {
if(module.log) {
printErr: function(message) { module.log.innerHTML += Array.prototype.slice.call(arguments).join(' ')
var log = document.getElementById('log');
log.innerHTML += Array.prototype.slice.call(arguments).join(' ')
.replace(/[\"&<>]/g, function (a) { .replace(/[\"&<>]/g, function (a) {
return { '"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;' }[a]; return { '"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;' }[a];
}) + '\n'; }) + '\n';
}
}, },
print: function(message) { print: function(_message) {
var log = document.getElementById('log'); if(module.log) {
log.innerHTML += Array.prototype.slice.call(arguments).join(' ') module.log.innerHTML += Array.prototype.slice.call(arguments).join(' ')
.replace(/[\"&<>]/g, function (a) { .replace(/[\"&<>]/g, function (a) {
return { '"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;' }[a]; return { '"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;' }[a];
}) + '\n'; }) + '\n';
}
}, },
/* onAbort not handled here, as the output is printed directly on the page */ /* onAbort not handled here, as the output is printed directly on the page */
@ -55,12 +60,13 @@ var Module = {
log: document.getElementById('log'), log: document.getElementById('log'),
setStatus: function(message) { setStatus: function(message) {
if(Module.status) Module.status.innerHTML = message; if(module.status)
module.status.innerHTML = message;
}, },
setStatusDescription: function(message) { setStatusDescription: function(message) {
if(Module.statusDescription) if(module.statusDescription)
Module.statusDescription.innerHTML = message; module.statusDescription.innerHTML = message;
}, },
totalDependencies: 0, totalDependencies: 0,
@ -69,29 +75,46 @@ var Module = {
this.totalDependencies = Math.max(this.totalDependencies, left); this.totalDependencies = Math.max(this.totalDependencies, left);
if(left) { if(left) {
Module.setStatus('Downloading...'); module.setStatus('Downloading...');
Module.setStatusDescription((this.totalDependencies - left) + ' / ' + this.totalDependencies); module.setStatusDescription((this.totalDependencies - left) + ' / ' + this.totalDependencies);
} else { } else {
Module.setStatus('Download complete'); module.setStatus('Download complete');
Module.setStatusDescription(''); module.setStatusDescription('');
Module.log.style.display = 'block'; module.log.style.display = 'block';
} }
} }
}; });
/* Parse arguments, e.g. /app/?foo=bar&fizz&buzz=3 goes to the app as /* Parse arguments, e.g. /app/?foo=bar&fizz&buzz=3 goes to the app as
['--foo', 'bar', '--fizz', '--buzz', '3'] */ ['--foo', 'bar', '--fizz', '--buzz', '3'] */
var args = decodeURIComponent(window.location.search.substr(1)).trim().split('&'); const args = decodeURIComponent(window.location.search.substr(1)).trim().split('&');
for(var i = 0; i != args.length; ++i) { for(let i = 0; i != args.length; ++i) {
var j = args[i].indexOf('='); let j = args[i].indexOf('=');
/* Key + value */ /* Key + value */
if(j != -1) { if(j != -1) {
Module.arguments.push('--' + args[i].substring(0, j)); module.arguments.push('--' + args[i].substring(0, j));
Module.arguments.push(args[i].substring(j + 1)); module.arguments.push(args[i].substring(j + 1));
/* Just key */ /* Just key */
} else Module.arguments.push('--' + args[i]); } else module.arguments.push('--' + args[i]);
}
Object.assign(module, init);
module.setStatus("Downloading...");
if(module.log)
module.log.style.display = 'none';
return module;
} }
Module.setStatus('Downloading...'); /* Default global Module object */
Module.log.style.display = 'none'; var Module = createMagnumModule();
/* UMD export */
if(typeof exports === 'object' && typeof module === 'object') /* CommonJS/Node */
module.exports = createMagnumModule;
else if(typeof define === 'function' && define['amd']) /* AMD */
define([], function() { return createMagnumModule; });
else if(typeof exports === 'object') /* CommonJS strict */
exports["createMagnumModule"] = createMagnumModule;

Loading…
Cancel
Save