aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api/extern-post-js.js
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2022-10-19 04:44:58 +0000
committerstephan <stephan@noemail.net>2022-10-19 04:44:58 +0000
commitcd0df83c156d12c89bcbe41420af51d83444855d (patch)
treef6e70858e5441d36916c58d053a44643a138bc86 /ext/wasm/api/extern-post-js.js
parent71de8e02416aa9a4ad90ac6958ff3fa025a33d2d (diff)
downloadsqlite-cd0df83c156d12c89bcbe41420af51d83444855d.tar.gz
sqlite-cd0df83c156d12c89bcbe41420af51d83444855d.zip
Apply considerable acrobatics to get the JS/WASM deliverables building to and loadable from a directory other than the one which contains the app-level code. Requires an only-slightly-leaky abstraction of passing a URL argument when loading sqlite3.js but provides much greater flexibility in where the JS/WASM files are located.
FossilOrigin-Name: 6d468dab9eb84d4548f68014959f02fe4f66455472ff24fe729382bb2972e3d1
Diffstat (limited to 'ext/wasm/api/extern-post-js.js')
-rw-r--r--ext/wasm/api/extern-post-js.js45
1 files changed, 37 insertions, 8 deletions
diff --git a/ext/wasm/api/extern-post-js.js b/ext/wasm/api/extern-post-js.js
index 7dba03b3a..5f5f72d4b 100644
--- a/ext/wasm/api/extern-post-js.js
+++ b/ext/wasm/api/extern-post-js.js
@@ -16,6 +16,27 @@
if(!originalInit){
throw new Error("Expecting self.sqlite3InitModule to be defined by the Emscripten build.");
}
+ /**
+ We need to add some state which our custom Module.locateFile()
+ can see, but an Emscripten limitation currently prevents us from
+ attaching it to the sqlite3InitModule function object:
+
+ https://github.com/emscripten-core/emscripten/issues/18071
+
+ The only current workaround is to temporarily stash this state
+ into the global scope and delete it when sqlite3InitModule()
+ is called.
+ */
+ const initModuleState = self.sqlite3InitModuleState = Object.assign(Object.create(null),{
+ moduleScript: self?.document?.currentScript,
+ isWorker: (!self.document && self.window !== self),
+ location: self.location,
+ urlParams: new URL(self.location.href).searchParams
+ });
+ if(initModuleState.urlParams.has('sqlite3.dir')){
+ initModuleState.sqlite3Dir = initModuleState.urlParams.get('sqlite3.dir') +'/';
+ };
+
self.sqlite3InitModule = (...args)=>{
//console.warn("Using replaced sqlite3InitModule()",self.location);
return originalInit(...args).then((EmscriptenModule)=>{
@@ -32,6 +53,8 @@
Emscripten details. */
return EmscriptenModule;
}
+ EmscriptenModule.sqlite3.scriptInfo = initModuleState;
+ //console.warn("sqlite3.scriptInfo =",EmscriptenModule.sqlite3.scriptInfo);
const f = EmscriptenModule.sqlite3.asyncPostInit;
delete EmscriptenModule.sqlite3.asyncPostInit;
return f();
@@ -41,13 +64,19 @@
});
};
self.sqlite3InitModule.ready = originalInit.ready;
- //console.warn("Replaced sqlite3InitModule()");
-})();
-if(0){
- console.warn("self.location.href =",self.location.href);
- if('undefined' !== typeof document){
- console.warn("document.currentScript.src =",
- document?.currentScript?.src);
+ if(self.sqlite3InitModuleState.moduleScript){
+ const sim = self.sqlite3InitModuleState;
+ let src = sim.moduleScript.src.split('/');
+ src.pop();
+ sim.scriptDir = src.join('/') + '/';
}
-}
+ if(0){
+ console.warn("Replaced sqlite3InitModule()");
+ console.warn("self.location.href =",self.location.href);
+ if('undefined' !== typeof document){
+ console.warn("document.currentScript.src =",
+ document?.currentScript?.src);
+ }
+ }
+})();