| 1 |
|
| 2 |
/* ^^^^ ACHTUNG: blank line at the start is necessary because |
| 3 |
Emscripten will not add a newline in some cases and we need |
| 4 |
a blank line for a sed-based kludge for the ES6 build. */ |
| 5 |
/* extern-post-js.js must be appended to the resulting sqlite3.js |
| 6 |
file. It gets its name from being used as the value for the |
| 7 |
--extern-post-js=... Emscripten flag. Note that this code, unlike |
| 8 |
most of the associated JS code, runs outside of the |
| 9 |
Emscripten-generated module init scope, in the current |
| 10 |
global scope. */ |
| 11 |
//#if target=es6-module |
| 12 |
const toExportForESM = |
| 13 |
//#endif |
| 14 |
(function(){ |
| 15 |
/** |
| 16 |
In order to hide the sqlite3InitModule()'s resulting |
| 17 |
Emscripten module from downstream clients (and simplify our |
| 18 |
documentation by being able to elide those details), we hide that |
| 19 |
function and expose a hand-written sqlite3InitModule() to return |
| 20 |
the sqlite3 object (most of the time). |
| 21 |
|
| 22 |
Unfortunately, we cannot modify the module-loader/exporter-based |
| 23 |
impls which Emscripten installs at some point in the file above |
| 24 |
this. |
| 25 |
*/ |
| 26 |
const originalInit = sqlite3InitModule; |
| 27 |
if(!originalInit){ |
| 28 |
throw new Error("Expecting globalThis.sqlite3InitModule to be defined by the Emscripten build."); |
| 29 |
} |
| 30 |
/** |
| 31 |
We need to add some state which our custom Module.locateFile() |
| 32 |
can see, but an Emscripten limitation currently prevents us from |
| 33 |
attaching it to the sqlite3InitModule function object: |
| 34 |
|
| 35 |
https://github.com/emscripten-core/emscripten/issues/18071 |
| 36 |
|
| 37 |
The only(?) current workaround is to temporarily stash this state |
| 38 |
into the global scope and delete it when sqlite3InitModule() |
| 39 |
is called. |
| 40 |
*/ |
| 41 |
const initModuleState = globalThis.sqlite3InitModuleState = Object.assign(Object.create(null),{ |
| 42 |
moduleScript: globalThis?.document?.currentScript, |
| 43 |
isWorker: ('undefined' !== typeof WorkerGlobalScope), |
| 44 |
location: globalThis.location, |
| 45 |
urlParams: globalThis?.location?.href |
| 46 |
? new URL(globalThis.location.href).searchParams |
| 47 |
: new URLSearchParams() |
| 48 |
}); |
| 49 |
initModuleState.debugModule = |
| 50 |
initModuleState.urlParams.has('sqlite3.debugModule') |
| 51 |
? (...args)=>console.warn('sqlite3.debugModule:',...args) |
| 52 |
: ()=>{}; |
| 53 |
|
| 54 |
if(initModuleState.urlParams.has('sqlite3.dir')){ |
| 55 |
initModuleState.sqlite3Dir = initModuleState.urlParams.get('sqlite3.dir') +'/'; |
| 56 |
}else if(initModuleState.moduleScript){ |
| 57 |
const li = initModuleState.moduleScript.src.split('/'); |
| 58 |
li.pop(); |
| 59 |
initModuleState.sqlite3Dir = li.join('/') + '/'; |
| 60 |
} |
| 61 |
|
| 62 |
globalThis.sqlite3InitModule = function ff(...args){ |
| 63 |
//console.warn("Using replaced sqlite3InitModule()",globalThis.location); |
| 64 |
return originalInit(...args).then((EmscriptenModule)=>{ |
| 65 |
//#if wasmfs |
| 66 |
if('undefined'!==typeof WorkerGlobalScope && |
| 67 |
EmscriptenModule['ENVIRONMENT_IS_PTHREAD']){ |
| 68 |
/** Workaround for wasmfs-generated worker, which calls this |
| 69 |
routine from each individual thread and requires that its |
| 70 |
argument be returned. The conditional criteria above are |
| 71 |
fragile, based solely on inspection of the offending code, |
| 72 |
not public Emscripten details. */ |
| 73 |
//console.warn("sqlite3InitModule() returning E-module.",EmscriptenModule); |
| 74 |
return EmscriptenModule; |
| 75 |
} |
| 76 |
//#endif |
| 77 |
//console.warn("sqlite3InitModule() returning sqlite3 object."); |
| 78 |
const s = EmscriptenModule.sqlite3; |
| 79 |
s.scriptInfo = initModuleState; |
| 80 |
//console.warn("sqlite3.scriptInfo =",s.scriptInfo); |
| 81 |
if(ff.__isUnderTest) s.__isUnderTest = true; |
| 82 |
const f = s.asyncPostInit; |
| 83 |
delete s.asyncPostInit; |
| 84 |
return f(); |
| 85 |
}).catch((e)=>{ |
| 86 |
console.error("Exception loading sqlite3 module:",e); |
| 87 |
throw e; |
| 88 |
}); |
| 89 |
}; |
| 90 |
globalThis.sqlite3InitModule.ready = originalInit.ready; |
| 91 |
|
| 92 |
if(globalThis.sqlite3InitModuleState.moduleScript){ |
| 93 |
const sim = globalThis.sqlite3InitModuleState; |
| 94 |
let src = sim.moduleScript.src.split('/'); |
| 95 |
src.pop(); |
| 96 |
sim.scriptDir = src.join('/') + '/'; |
| 97 |
} |
| 98 |
initModuleState.debugModule('sqlite3InitModuleState =',initModuleState); |
| 99 |
if(0){ |
| 100 |
console.warn("Replaced sqlite3InitModule()"); |
| 101 |
console.warn("globalThis.location.href =",globalThis.location.href); |
| 102 |
if('undefined' !== typeof document){ |
| 103 |
console.warn("document.currentScript.src =", |
| 104 |
document?.currentScript?.src); |
| 105 |
} |
| 106 |
} |
| 107 |
//#ifnot target=es6-module |
| 108 |
// Emscripten does not inject these module-loader bits in ES6 module |
| 109 |
// builds and including them here breaks JS bundlers, so elide them |
| 110 |
// from ESM builds. |
| 111 |
/* Replace the various module exports performed by the Emscripten |
| 112 |
glue... */ |
| 113 |
if (typeof exports === 'object' && typeof module === 'object'){ |
| 114 |
module.exports = sqlite3InitModule; |
| 115 |
}else if (typeof exports === 'object'){ |
| 116 |
exports["sqlite3InitModule"] = sqlite3InitModule; |
| 117 |
} |
| 118 |
/* AMD modules get injected in a way we cannot override, |
| 119 |
so we can't handle those here. */ |
| 120 |
//#endif // !target=es6-module |
| 121 |
return globalThis.sqlite3InitModule /* required for ESM */; |
| 122 |
})(); |
| 123 |
//#if target=es6-module |
| 124 |
sqlite3InitModule = toExportForESM; |
| 125 |
export default sqlite3InitModule; |
| 126 |
//#endif |