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
11 //#if target=es6-module
12 const toExportForESM =
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).
22 Unfortunately, we cannot modify the module-loader/exporter-based
23 impls which Emscripten installs at some point in the file above
26 const originalInit = sqlite3InitModule;
28 throw new Error("Expecting globalThis.sqlite3InitModule to be defined by the Emscripten build.");
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:
35 https://github.com/emscripten-core/emscripten/issues/18071
37 The only(?) current workaround is to temporarily stash this state
38 into the global scope and delete it when sqlite3InitModule()
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()
49 initModuleState.debugModule =
50 initModuleState.urlParams.has('sqlite3.debugModule')
51 ? (...args)=>console.warn('sqlite3.debugModule:',...args)
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('/');
59 initModuleState.sqlite3Dir = li.join('/') + '/';
62 globalThis.sqlite3InitModule = function ff(...args){
63 //console.warn("Using replaced sqlite3InitModule()",globalThis.location);
64 return originalInit(...args).then((EmscriptenModule)=>{
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;
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;
86 console.error("Exception loading sqlite3 module:",e);
90 globalThis.sqlite3InitModule.ready = originalInit.ready;
92 if(globalThis.sqlite3InitModuleState.moduleScript){
93 const sim = globalThis.sqlite3InitModuleState;
94 let src = sim.moduleScript.src.split('/');
96 sim.scriptDir = src.join('/') + '/';
98 initModuleState.debugModule('sqlite3InitModuleState =',initModuleState);
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);
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
111 /* Replace the various module exports performed by the Emscripten
113 if (typeof exports === 'object' && typeof module === 'object'){
114 module.exports = sqlite3InitModule;
115 }else if (typeof exports === 'object'){
116 exports["sqlite3InitModule"] = sqlite3InitModule;
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 */;
123 //#if target=es6-module
124 sqlite3InitModule = toExportForESM;
125 export default sqlite3InitModule;