1/* extern-post-js.js must be appended to the resulting sqlite3.js 2 file. It gets its name from being used as the value for 3 the --extern-post-js=... Emscripten flag. */ 4(function(){ 5 /** 6 In order to hide the sqlite3InitModule()'s resulting Emscripten 7 module from downstream clients (and simplify our documentation by 8 being able to elide those details), we rewrite 9 sqlite3InitModule() to return the sqlite3 object. 10 11 Unfortunately, we cannot modify the module-loader/exporter-based 12 impls which Emscripten installs at some point in the file above 13 this. 14 */ 15 const originalInit = self.sqlite3InitModule; 16 if(!originalInit){ 17 throw new Error("Expecting self.sqlite3InitModule to be defined by the Emscripten build."); 18 } 19 /** 20 We need to add some state which our custom Module.locateFile() 21 can see, but an Emscripten limitation currently prevents us from 22 attaching it to the sqlite3InitModule function object: 23 24 https://github.com/emscripten-core/emscripten/issues/18071 25 26 The only(?) current workaround is to temporarily stash this state 27 into the global scope and delete it when sqlite3InitModule() 28 is called. 29 */ 30 const initModuleState = self.sqlite3InitModuleState = Object.assign(Object.create(null),{ 31 moduleScript: self?.document?.currentScript, 32 isWorker: ('undefined' !== typeof WorkerGlobalScope), 33 location: self.location, 34 urlParams: new URL(self.location.href).searchParams 35 }); 36 if(initModuleState.urlParams.has('sqlite3.dir')){ 37 initModuleState.sqlite3Dir = initModuleState.urlParams.get('sqlite3.dir') +'/'; 38 }else if(initModuleState.moduleScript){ 39 const li = initModuleState.moduleScript.src.split('/'); 40 li.pop(); 41 initModuleState.sqlite3Dir = li.join('/') + '/'; 42 } 43 //console.warn("initModuleState =",initModuleState); 44 45 self.sqlite3InitModule = (...args)=>{ 46 //console.warn("Using replaced sqlite3InitModule()",self.location); 47 return originalInit(...args).then((EmscriptenModule)=>{ 48 if(self.window!==self && 49 (EmscriptenModule['ENVIRONMENT_IS_PTHREAD'] 50 || EmscriptenModule['_pthread_self'] 51 || 'function'===typeof threadAlert 52 || self.location.pathname.endsWith('.worker.js') 53 )){ 54 /** Workaround for wasmfs-generated worker, which calls this 55 routine from each individual thread and requires that its 56 argument be returned. All of the criteria above are fragile, 57 based solely on inspection of the offending code, not public 58 Emscripten details. */ 59 return EmscriptenModule; 60 } 61 EmscriptenModule.sqlite3.scriptInfo = initModuleState; 62 //console.warn("sqlite3.scriptInfo =",EmscriptenModule.sqlite3.scriptInfo); 63 const f = EmscriptenModule.sqlite3.asyncPostInit; 64 delete EmscriptenModule.sqlite3.asyncPostInit; 65 return f(); 66 }).catch((e)=>{ 67 console.error("Exception loading sqlite3 module:",e); 68 throw e; 69 }); 70 }; 71 self.sqlite3InitModule.ready = originalInit.ready; 72 73 if(self.sqlite3InitModuleState.moduleScript){ 74 const sim = self.sqlite3InitModuleState; 75 let src = sim.moduleScript.src.split('/'); 76 src.pop(); 77 sim.scriptDir = src.join('/') + '/'; 78 } 79 if(0){ 80 console.warn("Replaced sqlite3InitModule()"); 81 console.warn("self.location.href =",self.location.href); 82 if('undefined' !== typeof document){ 83 console.warn("document.currentScript.src =", 84 document?.currentScript?.src); 85 } 86 } 87 /* Replace the various module exports performed by the Emscripten 88 glue... */ 89 if (typeof exports === 'object' && typeof module === 'object') 90 module.exports = sqlite3InitModule; 91 else if (typeof exports === 'object') 92 exports["sqlite3InitModule"] = sqlite3InitModule; 93 /* AMD modules get injected in a way we cannot override, 94 so we can't handle those here. */ 95})(); 96