|
| 1 | + |
1 | 2 | // We are modularizing this manually because the current modularize setting in Emscripten has some issues: |
2 | 3 | // https://github.com/kripken/emscripten/issues/5820 |
3 | | -var Module = (function(Module) { |
4 | | - |
5 | | - // If Module already exists, use it |
6 | | - var Module = typeof Module !== 'undefined' ? Module : {}; |
7 | | - |
8 | | - Module['preRunHasRun'] = new Promise(function(resolve){ |
9 | | - // TODO: Add on to any postRun objects already there |
10 | | - Module['preRun'] = function(){ |
11 | | - // When Emscripted calls preRun, this resolves |
12 | | - resolve(); |
13 | | - } |
14 | | - }.bind(this)); |
15 | | - |
16 | | - var postRunHasRun = Module['postRunHasRun'] = new Promise(function(resolve){ |
17 | | - // TODO: Add on to any postRun objects already there |
18 | | - Module['postRun'] = function(){ |
19 | | - // When Emscripted calls postRun, this resolves |
20 | | - resolve(); |
21 | | - } |
22 | | - }.bind(this)); |
23 | | - |
24 | | - // This is a promise that a module loader can listen for |
25 | | - Module['ready'] = new Promise(function(resolve, reject){ |
26 | | - // TODO: Add on to any onAbort function already there |
27 | | - Module['onAbort'] = function(what){ |
28 | | - reject(new Error(what)); |
29 | | - } |
30 | | - |
31 | | - return postRunHasRun.then(function(){ |
32 | | - resolve(Module); |
33 | | - }.bind(this)); |
34 | | - |
35 | | - }.bind(this)); |
| 4 | +// This module exports a promise that loads and resolves to the actual sql.js module. |
| 5 | +// That way, this module can't be used before the WASM is finished loading. |
| 6 | + |
| 7 | + |
| 8 | +// So the first thing that this module does is define a place for the loadedModule to reside |
| 9 | + |
| 10 | +// We are going to define a function that a user will call to start loading initializing our Sql.js library |
| 11 | +// However, that function might be called multiple times, and on subsequent calls, we want it to return the same module that it's already loaded once before. |
| 12 | + |
| 13 | +var initSqlJsPromise = undefined; |
| 14 | + |
| 15 | +var initSqlJs = function (moduleConfig) { |
| 16 | + |
| 17 | + if (initSqlJsPromise){ |
| 18 | + return initSqlJsPromise; |
| 19 | + } |
| 20 | + // If we're here, we've never called this function before |
| 21 | + initSqlJsPromise = new Promise((resolveModule) => { |
| 22 | + |
| 23 | + // We are modularizing this manually because the current modularize setting in Emscripten has some issues: |
| 24 | + // https://github.com/kripken/emscripten/issues/5820 |
| 25 | + |
| 26 | + // The way to affect the loading of emcc compiled modules is to create a variable called `Module` and add |
| 27 | + // properties to it, like `preRun`, `postRun`, etc |
| 28 | + // We are using that to get notified when the WASM has finished loading. |
| 29 | + // Only then will we return our promise |
| 30 | + |
| 31 | + // If they passed in a moduleConfig object, use that |
| 32 | + // Otherwise, initialize Module to the empty object |
| 33 | + var Module = typeof moduleConfig !== 'undefined' ? moduleConfig : {}; |
| 34 | + |
| 35 | + // EMCC only allows for a single onAbort function (not an array of functions) |
| 36 | + // So if the user defined their own onAbort function, we remember it and call it |
| 37 | + var originalOnAbortFunction = Module['onAbort']; |
| 38 | + Module['onAbort'] = function (errorThatCausedAbort) { |
| 39 | + reject(new Error(errorThatCausedAbort)); |
| 40 | + if (originalOnAbortFunction){ |
| 41 | + originalOnAbortFunction(errorThatCausedAbort); |
| 42 | + } |
| 43 | + }; |
36 | 44 |
|
| 45 | + Module['postRun'] = Module['postRun'] || []; |
| 46 | + Module['postRun'].push(function () { |
| 47 | + // When Emscripted calls postRun, this promise resolves with the built Module |
| 48 | + resolveModule(Module); |
| 49 | + }); |
| 50 | + |
| 51 | + // The emcc-generated code and shell-post.js code goes below, |
| 52 | + // meaning that all of it runs inside of this promise. If anything throws an exception, our promise will abort |
0 commit comments