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