|
| 1 | + |
| 2 | +// We are modularizing this manually because the current modularize setting in Emscripten has some issues: |
| 3 | +// https://github.com/kripken/emscripten/issues/5820 |
| 4 | +// In addition, When you use emcc's modularization, it still expects to export a global object called `Module`, |
| 5 | +// which is able to be used/called before the WASM is loaded. |
| 6 | +// The modularization below exports a promise that loads and resolves to the actual sql.js module. |
| 7 | +// That way, this module can't be used before the WASM is finished loading. |
| 8 | + |
| 9 | +// We are going to define a function that a user will call to start loading initializing our Sql.js library |
| 10 | +// However, that function might be called multiple times, and on subsequent calls, we don't actually want it to instantiate a new instance of the Module |
| 11 | +// Instead, we want to return the previously loaded module |
| 12 | + |
| 13 | +// TODO: Make this not declare a global if used in the browser |
| 14 | +var initSqlJsPromise = undefined; |
| 15 | + |
| 16 | +var initSqlJs = function (moduleConfig) { |
| 17 | + |
| 18 | + if (initSqlJsPromise){ |
| 19 | + return initSqlJsPromise; |
| 20 | + } |
| 21 | + // If we're here, we've never called this function before |
| 22 | + initSqlJsPromise = new Promise(function (resolveModule, reject) { |
| 23 | + |
| 24 | + // We are modularizing this manually because the current modularize setting in Emscripten has some issues: |
| 25 | + // https://github.com/kripken/emscripten/issues/5820 |
| 26 | + |
| 27 | + // The way to affect the loading of emcc compiled modules is to create a variable called `Module` and add |
| 28 | + // properties to it, like `preRun`, `postRun`, etc |
| 29 | + // We are using that to get notified when the WASM has finished loading. |
| 30 | + // Only then will we return our promise |
| 31 | + |
| 32 | + // If they passed in a moduleConfig object, use that |
| 33 | + // Otherwise, initialize Module to the empty object |
| 34 | + var Module = typeof moduleConfig !== 'undefined' ? moduleConfig : {}; |
| 35 | + |
| 36 | + // EMCC only allows for a single onAbort function (not an array of functions) |
| 37 | + // So if the user defined their own onAbort function, we remember it and call it |
| 38 | + var originalOnAbortFunction = Module['onAbort']; |
| 39 | + Module['onAbort'] = function (errorThatCausedAbort) { |
| 40 | + reject(new Error(errorThatCausedAbort)); |
| 41 | + if (originalOnAbortFunction){ |
| 42 | + originalOnAbortFunction(errorThatCausedAbort); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + Module['postRun'] = Module['postRun'] || []; |
| 47 | + Module['postRun'].push(function () { |
| 48 | + // When Emscripted calls postRun, this promise resolves with the built Module |
| 49 | + resolveModule(Module); |
| 50 | + }); |
0 commit comments