1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-10-21 11:13:54 +03:00
Files
sqlite/ext/wasm/api/extern-post-js.c-pp.js
stephan 1ab3d7eafd Cleanups and docs in the internal parts of the JS lib bootstrapping. More tinkering with the custom (but still disabled) wasm file loader.
FossilOrigin-Name: 35651d9ab5529da915500fc50ca3833a004d0b7a19d98e8fbf39234d94697aec
2025-09-22 17:29:52 +00:00

144 lines
5.5 KiB
JavaScript

/* ^^^^ ACHTUNG: blank line at the start is necessary because
Emscripten will not add a newline in some cases and we need
a blank line for a sed-based kludge for the ES6 build. */
/* extern-post-js.js must be appended to the resulting sqlite3.js
file. It gets its name from being used as the value for the
--extern-post-js=... Emscripten flag. This code, unlike most of the
associated JS code, runs outside of the Emscripten-generated module
init scope, in the current global scope. */
//#if target=es6-module
const toExportForESM =
//#endif
(function(){
//console.warn("this is extern-post-js");
/**
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 hide that
function and expose a hand-written sqlite3InitModule() to return
the sqlite3 object (most of the time).
Unfortunately, we cannot modify the module-loader/exporter-based
impls which Emscripten installs at some point in the file above
this.
*/
const originalInit = sqlite3InitModule;
if(!originalInit){
throw new Error("Expecting globalThis.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 sIMS = globalThis.sqlite3InitModuleState = Object.assign(Object.create(null),{
moduleScript: globalThis?.document?.currentScript,
isWorker: ('undefined' !== typeof WorkerGlobalScope),
location: globalThis.location,
urlParams: globalThis?.location?.href
? new URL(globalThis.location.href).searchParams
: new URLSearchParams()
});
sIMS.debugModule =
sIMS.urlParams.has('sqlite3.debugModule')
? (...args)=>console.warn('sqlite3.debugModule:',...args)
: ()=>{};
if(sIMS.urlParams.has('sqlite3.dir')){
sIMS.sqlite3Dir = sIMS.urlParams.get('sqlite3.dir') +'/';
}else if(sIMS.moduleScript){
const li = sIMS.moduleScript.src.split('/');
li.pop();
sIMS.sqlite3Dir = li.join('/') + '/';
}
globalThis.sqlite3InitModule = function ff(...args){
//console.warn("Using replaced sqlite3InitModule()",globalThis.location);
return originalInit(...args).then((EmscriptenModule)=>{
sIMS.debugModule("sqlite3InitModule() sIMS =",sIMS);
sIMS.debugModule("sqlite3InitModule() EmscriptenModule =",EmscriptenModule);
EmscriptenModule.runSQLite3PostLoadInit(
EmscriptenModule /* see post-js-header/footer.js */
);
delete EmscriptenModule.runSQLite3PostLoadInit;
const s = EmscriptenModule.sqlite3;
delete EmscriptenModule.sqlite3;
s.scriptInfo = sIMS /* needed by async init below */;
if(ff.__isUnderTest){
s.__isUnderTest = true;
s.emscripten = EmscriptenModule;
//#if custom-Module.instantiateWasm
const iw = sIMS.instantiateWasm;
if( iw ){
/* Metadata injected by the custom Module.instantiateWasm()
in pre-js.c-pp.js. */
s.wasm.module = iw.module;
s.wasm.instance = iw.instance;
s.wasm.imports = iw.imports;
}
//#endif custom-Module.instantiateWasm
}
const rv = s.asyncPostInit();
delete s.asyncPostInit;
//#if wasmfs
if('undefined'!==typeof WorkerGlobalScope &&
EmscriptenModule['ENVIRONMENT_IS_PTHREAD']){
/** Workaround for wasmfs-generated worker, which calls this
routine from each individual thread and requires that its
argument be returned. The if() condition above is fragile,
based solely on inspection of the offending code, not
public Emscripten details. */
//console.warn("sqlite3InitModule() returning E-module.",EmscriptenModule);
return EmscriptenModule;
}
//#endif
return rv;
}).catch((e)=>{
console.error("Exception loading sqlite3 module:",e);
throw e;
});
};
globalThis.sqlite3InitModule.ready = originalInit.ready;
if(sIMS.moduleScript){
let src = sIMS.moduleScript.src.split('/');
src.pop();
sIMS.scriptDir = src.join('/') + '/';
}
sIMS.debugModule('extern-post-js.c-pp.js sqlite3InitModuleState =',sIMS);
if(0){
console.warn("Replaced sqlite3InitModule()");
console.warn("globalThis.location.href =",globalThis.location.href);
if('undefined' !== typeof document){
console.warn("document.currentScript.src =",
document?.currentScript?.src);
}
}
//#ifnot target=es6-module
// Emscripten does not inject these module-loader bits in ES6 module
// builds and including them here breaks JS bundlers, so elide them
// from ESM builds.
/* Replace the various module exports performed by the Emscripten
glue... */
if (typeof exports === 'object' && typeof module === 'object'){
module.exports = sqlite3InitModule;
}else if (typeof exports === 'object'){
exports["sqlite3InitModule"] = sqlite3InitModule;
}
/* AMD modules get injected in a way we cannot override,
so we can't handle those here. */
//#endif // !target=es6-module
return globalThis.sqlite3InitModule /* required for ESM */;
})();
//#if target=es6-module
sqlite3InitModule = toExportForESM;
export default sqlite3InitModule;
//#endif