aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api/pre-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/pre-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/pre-js.js')
-rw-r--r--ext/wasm/api/pre-js.js44
1 files changed, 42 insertions, 2 deletions
diff --git a/ext/wasm/api/pre-js.js b/ext/wasm/api/pre-js.js
index b6630416d..c07d0373c 100644
--- a/ext/wasm/api/pre-js.js
+++ b/ext/wasm/api/pre-js.js
@@ -4,9 +4,49 @@
This file is intended to be prepended to the sqlite3.js build using
Emscripten's --pre-js=THIS_FILE flag (or equivalent).
*/
+
+// See notes in extern-post-js.js
+const sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null);
+delete self.sqlite3InitModuleState;
+
+/**
+ This custom locateFile() tries to figure out where to load `path`
+ from. The intent is to provide a way for foo/bar/X.js loaded from a
+ Worker constructor or importScripts() to be able to resolve
+ foo/bar/X.wasm (in the latter case, with some help):
+
+ 1) If URL param named the same as `path` is set, it is returned.
+
+ 2) If sqlite3InitModuleState.sqlite3Dir is set, then (thatName + path)
+ is returned (note that it's assumed to end with '/').
+
+ 3) If this code is running in the main UI thread AND it was loaded
+ from a SCRIPT tag, the directory part of that URL is used
+ as the prefix. (This form of resolution unfortunately does not
+ function for scripts loaded via importScripts().)
+
+ 4) If none of the above apply, (prefix+path) is returned.
+*/
Module['locateFile'] = function(path, prefix) {
- return prefix + path;
-};
+ let theFile;
+ const up = this.urlParams;
+ if(0){
+ console.warn("locateFile(",arguments[0], ',', arguments[1],")",
+ 'self.location =',self.location,
+ 'sqlite3InitModuleState.scriptDir =',this.scriptDir,
+ 'up.entries() =',Array.from(up.entries()));
+ }
+ if(up.has(path)){
+ theFile = up.get(path);
+ }else if(this.sqlite3Dir){
+ theFile = this.sqlite3Dir + path;
+ }else if(this.scriptDir){
+ theFile = this.scriptDir + path;
+ }else{
+ theFile = prefix + path;
+ }
+ return theFile;
+}.bind(sqlite3InitModuleState);
/**
Bug warning: this xInstantiateWasm bit must remain disabled