1/* extern-post-js.js must be appended to the resulting sqlite3.js
2   file. */
3(function(){
4  /**
5     In order to hide the sqlite3InitModule()'s resulting Emscripten
6     module from downstream clients (and simplify our documentation by
7     being able to elide those details), we rewrite
8     sqlite3InitModule() to return the sqlite3 object.
9
10     Unfortunately, we cannot modify the module-loader/exporter-based
11     impls which Emscripten installs at some point in the file above
12     this.
13  */
14  const originalInit = self.sqlite3InitModule;
15  if(!originalInit){
16    throw new Error("Expecting self.sqlite3InitModule to be defined by the Emscripten build.");
17  }
18  self.sqlite3InitModule = (...args)=>{
19    //console.warn("Using replaced sqlite3InitModule()",self.location);
20    return originalInit(...args).then((EmscriptenModule)=>{
21      if(self.window!==self &&
22         (EmscriptenModule['ENVIRONMENT_IS_PTHREAD']
23          || EmscriptenModule['_pthread_self']
24          || 'function'===typeof threadAlert
25          || self.location.pathname.endsWith('.worker.js')
26         )){
27        /** Workaround for wasmfs-generated worker, which calls this
28            routine from each individual thread and requires that its
29            argument be returned. All of the criteria above are fragile,
30            based solely on inspection of the offending code, not public
31            Emscripten details. */
32        return EmscriptenModule;
33      }
34      const f = EmscriptenModule.sqlite3.asyncPostInit;
35      delete EmscriptenModule.sqlite3.asyncPostInit;
36      return f();
37    }).catch((e)=>{
38      console.error("Exception loading sqlite3 module:",e);
39      throw e;
40    });
41  };
42  self.sqlite3InitModule.ready = originalInit.ready;
43  //console.warn("Replaced sqlite3InitModule()");
44})();
45