aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api/extern-post-js.js
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2022-09-29 13:17:50 +0000
committerstephan <stephan@noemail.net>2022-09-29 13:17:50 +0000
commitb94a98607a3bd4dd55f4d5ce16f171ba066bafb2 (patch)
tree097b3fae84f287075120e7685458c4d1bb98af60 /ext/wasm/api/extern-post-js.js
parent4b884bb4c77cb4b1e5059395212160f4ee48cc53 (diff)
downloadsqlite-b94a98607a3bd4dd55f4d5ce16f171ba066bafb2.tar.gz
sqlite-b94a98607a3bd4dd55f4d5ce16f171ba066bafb2.zip
Rework the Emscripten-emitted module loader/init function such that it passes on the sqlite3 module, instead of the Emscripten module, to the first then() of sqlite3InitModule()'s returned Promise. This eliminates any need to mention the Emscripten module object in client-side code unless they want to configure it in advance for loading-status reports.
FossilOrigin-Name: 0dbaa0e2b5abf5c23e2039ec90a3055ebb3c063aaf4e556c42546defe6fbb86d
Diffstat (limited to 'ext/wasm/api/extern-post-js.js')
-rw-r--r--ext/wasm/api/extern-post-js.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/ext/wasm/api/extern-post-js.js b/ext/wasm/api/extern-post-js.js
new file mode 100644
index 000000000..acb54c3c9
--- /dev/null
+++ b/ext/wasm/api/extern-post-js.js
@@ -0,0 +1,41 @@
+/* emscripten-js-addenda.js must be appended to the resulting sqlite3.js
+ file. */
+(function(){
+ /**
+ In order to hide the sqlite3InitModule()'s resulting Emscripten
+ module from downstream clients (and simplify our documentation by
+ being able to elide those details), we rewrite
+ sqlite3InitModule() to return the sqlite3 object.
+
+ Unfortunately, we cannot modify the module-loader/exporter-based
+ impls which Emscripten installs at some point in the file above
+ this.
+ */
+ const originalInit = self.sqlite3InitModule;
+ if(!originalInit){
+ throw new Error("Expecting self.sqlite3InitModule to be defined by the Emscripten build.");
+ }
+ self.sqlite3InitModule.ready = originalInit.ready;
+ self.sqlite3InitModule = (...args)=>{
+ //console.warn("Using replaced sqlite3InitModule()",self.location);
+ return originalInit(...args).then((EmscriptenModule)=>{
+ if(self.window!==self &&
+ (EmscriptenModule['ENVIRONMENT_IS_PTHREAD']
+ || EmscriptenModule['_pthread_self']
+ || 'function'===typeof threadAlert
+ || self.location.pathname.endsWith('.worker.js')
+ )){
+ /** Workaround for wasmfs-generated worker, which calls this
+ routine from each individual thread and requires that its
+ argument be returned. All of the criteria above are fragile,
+ based solely on inspection of the offending code, not public
+ Emscripten details. */
+ return EmscriptenModule;
+ }
+ const f = EmscriptenModule.sqlite3.asyncPostInit;
+ delete EmscriptenModule.sqlite3.asyncPostInit;
+ return f();
+ });
+ };
+ //console.warn("Replaced sqlite3InitModule()");
+})();