diff options
author | stephan <stephan@noemail.net> | 2022-10-30 09:47:33 +0000 |
---|---|---|
committer | stephan <stephan@noemail.net> | 2022-10-30 09:47:33 +0000 |
commit | 1fc6ffccc55fc7105ce9426149e63446e062c01f (patch) | |
tree | ec2223831c80b6e521b3deb5aa28cbaf2762266f /ext/wasm/api | |
parent | af9cee12c1e57bbd92db0ebdd2a4f9f551b1d790 (diff) | |
download | sqlite-1fc6ffccc55fc7105ce9426149e63446e062c01f.tar.gz sqlite-1fc6ffccc55fc7105ce9426149e63446e062c01f.zip |
Minor WASM build cleanups. Enable custom Module.instantiateWasm() when not in WASMFS mode (where it doesn't work). Add sqlite3.debugModule URL param to enable some module-init-time debugging output.
FossilOrigin-Name: 50f678846a2b3c3d0818f0bae89f2ee86252a2e6a9c7029ebaae3953ca0fa14c
Diffstat (limited to 'ext/wasm/api')
-rw-r--r-- | ext/wasm/api/extern-post-js.js | 7 | ||||
-rw-r--r-- | ext/wasm/api/post-js-header.js | 2 | ||||
-rw-r--r-- | ext/wasm/api/pre-js.js | 46 | ||||
-rw-r--r-- | ext/wasm/api/sqlite3-wasm.c | 2 |
4 files changed, 37 insertions, 20 deletions
diff --git a/ext/wasm/api/extern-post-js.js b/ext/wasm/api/extern-post-js.js index 67b32e891..84b99b53a 100644 --- a/ext/wasm/api/extern-post-js.js +++ b/ext/wasm/api/extern-post-js.js @@ -36,6 +36,11 @@ location: self.location, urlParams: new URL(self.location.href).searchParams }); + initModuleState.debugModule = + (new URL(self.location.href).searchParams).has('sqlite3.debugModule') + ? (...args)=>console.warn('sqlite3.debugModule:',...args) + : ()=>{}; + if(initModuleState.urlParams.has('sqlite3.dir')){ initModuleState.sqlite3Dir = initModuleState.urlParams.get('sqlite3.dir') +'/'; }else if(initModuleState.moduleScript){ @@ -43,7 +48,6 @@ li.pop(); initModuleState.sqlite3Dir = li.join('/') + '/'; } - //console.warn("initModuleState =",initModuleState); self.sqlite3InitModule = (...args)=>{ //console.warn("Using replaced sqlite3InitModule()",self.location); @@ -79,6 +83,7 @@ src.pop(); sim.scriptDir = src.join('/') + '/'; } + initModuleState.debugModule('sqlite3InitModuleState =',initModuleState); if(0){ console.warn("Replaced sqlite3InitModule()"); console.warn("self.location.href =",self.location.href); diff --git a/ext/wasm/api/post-js-header.js b/ext/wasm/api/post-js-header.js index f377a6541..82a80e5a1 100644 --- a/ext/wasm/api/post-js-header.js +++ b/ext/wasm/api/post-js-header.js @@ -5,7 +5,7 @@ environment must have been set up already but it will not have loaded its WASM when the code in this file is run. The function it installs will be run after the WASM module is loaded, at which - point the sqlite3 WASM API bits will be set up. + point the sqlite3 JS API bits will get set up. */ if(!Module.postRun) Module.postRun = []; Module.postRun.push(function(Module/*the Emscripten-style module object*/){ diff --git a/ext/wasm/api/pre-js.js b/ext/wasm/api/pre-js.js index c07d0373c..f31dea179 100644 --- a/ext/wasm/api/pre-js.js +++ b/ext/wasm/api/pre-js.js @@ -8,6 +8,7 @@ // See notes in extern-post-js.js const sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null); delete self.sqlite3InitModuleState; +sqlite3InitModuleState.debugModule('self.location =',self.location); /** This custom locateFile() tries to figure out where to load `path` @@ -30,12 +31,6 @@ delete self.sqlite3InitModuleState; Module['locateFile'] = function(path, prefix) { 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){ @@ -45,22 +40,37 @@ Module['locateFile'] = function(path, prefix) { }else{ theFile = prefix + path; } + sqlite3InitModuleState.debugModule( + "locateFile(",arguments[0], ',', arguments[1],")", + 'sqlite3InitModuleState.scriptDir =',this.scriptDir, + 'up.entries() =',Array.from(up.entries()), + "result =", theFile + ); return theFile; }.bind(sqlite3InitModuleState); /** - Bug warning: this xInstantiateWasm bit must remain disabled - until this bug is resolved or wasmfs won't work: + Bug warning: a custom Module.instantiateWasm() does not work + in WASMFS builds: https://github.com/emscripten-core/emscripten/issues/17951 + + In such builds we must disable this. */ -const xInstantiateWasm = 1 - ? 'emscripten-bug-17951' - : 'instantiateWasm'; -Module[xInstantiateWasm] = function callee(imports,onSuccess){ +const xNameOfInstantiateWasm = true + ? 'instantiateWasm' + : 'emscripten-bug-17951'; +Module[xNameOfInstantiateWasm] = function callee(imports,onSuccess){ imports.env.foo = function(){}; - console.warn("instantiateWasm() uri =",callee.uri, self.location.href); - const wfetch = ()=>fetch(callee.uri, {credentials: 'same-origin'}); + const uri = Module.locateFile( + callee.uri, ( + ('undefined'===typeof scriptDirectory/*var defined by Emscripten glue*/) + ? '' : scriptDirectory) + ); + sqlite3InitModuleState.debugModule( + "instantiateWasm() uri =", uri + ); + const wfetch = ()=>fetch(uri, {credentials: 'same-origin'}); const loadWasm = WebAssembly.instantiateStreaming ? async ()=>{ return WebAssembly.instantiateStreaming(wfetch(), imports) @@ -79,10 +89,12 @@ Module[xInstantiateWasm] = function callee(imports,onSuccess){ It is literally impossible to reliably get the name of _this_ script at runtime, so impossible to derive X.wasm from script name X.js. Thus we need, at build-time, to redefine - Module[xInstantiateWasm].uri by appending it to a build-specific + Module[xNameOfInstantiateWasm].uri by appending it to a build-specific copy of this file with the name of the wasm file. This is apparently why Emscripten hard-codes the name of the wasm file into their glue scripts. */ -Module[xInstantiateWasm].uri = 'sqlite3.wasm'; -/* END FILE: api/pre-js.js */ +Module[xNameOfInstantiateWasm].uri = 'sqlite3.wasm'; +/* END FILE: api/pre-js.js, noting that the build process may add a + line after this one to change the above .uri to a build-specific + one. */ diff --git a/ext/wasm/api/sqlite3-wasm.c b/ext/wasm/api/sqlite3-wasm.c index 754f9cf0d..1ca51f0d2 100644 --- a/ext/wasm/api/sqlite3-wasm.c +++ b/ext/wasm/api/sqlite3-wasm.c @@ -1028,7 +1028,7 @@ int sqlite3_wasm_init_wasmfs(const char *zMountPoint){ #else SQLITE_WASM_KEEP int sqlite3_wasm_init_wasmfs(const char *zUnused){ - emscripten_console_warn("WASMFS OPFS is not compiled in."); + //emscripten_console_warn("WASMFS OPFS is not compiled in."); if(zUnused){/*unused*/} return SQLITE_NOTFOUND; } |