aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api/extern-post-js.c-pp.js
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2023-03-07 19:12:06 +0000
committerstephan <stephan@noemail.net>2023-03-07 19:12:06 +0000
commit4214cc32ba2082b90dde97495058c45920235bf4 (patch)
tree1728d17a0729bdbd450e11bf0eecb4c02ca06691 /ext/wasm/api/extern-post-js.c-pp.js
parent7272f6d64d88da451f18aa515db06ed6f2b459d4 (diff)
downloadsqlite-4214cc32ba2082b90dde97495058c45920235bf4.tar.gz
sqlite-4214cc32ba2082b90dde97495058c45920235bf4.zip
Replace use of 'self' in JS code with 'globalThis', as that works in browsers and node environments. Avoid using globalThis.location if it's not set (e.g. in node). Based on feedback in [forum:ac7a94d4f77db235|forum post ac7a94d4f77db235]. Minor JS build tweaks.
FossilOrigin-Name: dbbe8f25e58738c10b6192d41f1e3886983871f17631cbc45ce626d3f05a6e26
Diffstat (limited to 'ext/wasm/api/extern-post-js.c-pp.js')
-rw-r--r--ext/wasm/api/extern-post-js.c-pp.js26
1 files changed, 14 insertions, 12 deletions
diff --git a/ext/wasm/api/extern-post-js.c-pp.js b/ext/wasm/api/extern-post-js.c-pp.js
index a577a63e1..14d434991 100644
--- a/ext/wasm/api/extern-post-js.c-pp.js
+++ b/ext/wasm/api/extern-post-js.c-pp.js
@@ -28,7 +28,7 @@ const toExportForESM =
for non-ES6 Module cases but wrong for ES6 modules because those
resolve this symbol differently. */ sqlite3InitModule;
if(!originalInit){
- throw new Error("Expecting self.sqlite3InitModule to be defined by the Emscripten build.");
+ throw new Error("Expecting globalThis.sqlite3InitModule to be defined by the Emscripten build.");
}
/**
We need to add some state which our custom Module.locateFile()
@@ -41,11 +41,13 @@ const toExportForESM =
into the global scope and delete it when sqlite3InitModule()
is called.
*/
- const initModuleState = self.sqlite3InitModuleState = Object.assign(Object.create(null),{
+ const initModuleState = globalThis.sqlite3InitModuleState = Object.assign(Object.create(null),{
moduleScript: self?.document?.currentScript,
isWorker: ('undefined' !== typeof WorkerGlobalScope),
- location: self.location,
- urlParams: new URL(self.location.href).searchParams
+ location: globalThis.location,
+ urlParams: globalThis?.location?.href
+ ? new URL(globalThis.location.href).searchParams
+ : new URLSearchParams()
});
initModuleState.debugModule =
initModuleState.urlParams.has('sqlite3.debugModule')
@@ -60,14 +62,14 @@ const toExportForESM =
initModuleState.sqlite3Dir = li.join('/') + '/';
}
- self.sqlite3InitModule = function ff(...args){
+ globalThis.sqlite3InitModule = function ff(...args){
//console.warn("Using replaced sqlite3InitModule()",self.location);
return originalInit(...args).then((EmscriptenModule)=>{
- if(self.window!==self &&
+ if('undefined'!==typeof WorkerGlobalScope &&
(EmscriptenModule['ENVIRONMENT_IS_PTHREAD']
|| EmscriptenModule['_pthread_self']
|| 'function'===typeof threadAlert
- || self.location.pathname.endsWith('.worker.js')
+ || globalThis?.location?.pathname?.endsWith?.('.worker.js')
)){
/** Workaround for wasmfs-generated worker, which calls this
routine from each individual thread and requires that its
@@ -88,10 +90,10 @@ const toExportForESM =
throw e;
});
};
- self.sqlite3InitModule.ready = originalInit.ready;
+ globalThis.sqlite3InitModule.ready = originalInit.ready;
- if(self.sqlite3InitModuleState.moduleScript){
- const sim = self.sqlite3InitModuleState;
+ if(globalThis.sqlite3InitModuleState.moduleScript){
+ const sim = globalThis.sqlite3InitModuleState;
let src = sim.moduleScript.src.split('/');
src.pop();
sim.scriptDir = src.join('/') + '/';
@@ -99,7 +101,7 @@ const toExportForESM =
initModuleState.debugModule('sqlite3InitModuleState =',initModuleState);
if(0){
console.warn("Replaced sqlite3InitModule()");
- console.warn("self.location.href =",self.location.href);
+ console.warn("globalThis.location.href =",globalThis.location.href);
if('undefined' !== typeof document){
console.warn("document.currentScript.src =",
document?.currentScript?.src);
@@ -119,7 +121,7 @@ const toExportForESM =
/* AMD modules get injected in a way we cannot override,
so we can't handle those here. */
//#endif // !target=es6-module
- return self.sqlite3InitModule /* required for ESM */;
+ return globalThis.sqlite3InitModule /* required for ESM */;
})();
//#if target=es6-module
export default toExportForESM;