-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.js
49 lines (46 loc) · 1.51 KB
/
loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define([], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory();
} else {
// Browser globals (root is window)
root.loader = factory();
}
}(this, function () {
return function loadModule(moduleUrl, path, callback) {
return new Promise(function(resolve, reject) {
var solverObj = null;
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
var DONE = request.DONE || 4;
if (request.readyState === DONE){
if (request.status == 200) {
// emscripten puts Module into global namespace if it
// determines that it runs in the web
// save potential old window.Module
var oldWindowModule;
if (window.Module) {
oldWindowModule = window.Module;
}
console.log("Evaluating asmjs code...");
var newCode = "var memoryInitializerPath = '" + path + "';" + request.responseText;
solverObj = new Function(newCode)();
if (oldWindowModule) {
window.Module = oldWindowModule;
} else {
delete window.Module;
}
resolve(solverObj);
} else {
console.error("Error while loading ", moduleUrl);
}
}
};
request.open("GET", path + moduleUrl);
request.send();
});
};
}));