2022-11-19 05:26:45 +00:00
/ * ^ ^ ^ ^ A C H T U N G : b l a n k l i n e a t t h e s t a r t i s n e c e s s a r y b e c a u s e
Emscripten will not add a newline in some cases and we need
a blank line for a sed - based kludge for the ES6 build . * /
2022-09-29 22:08:22 +00:00
/ * e x t e r n - p o s t - j s . j s m u s t b e a p p e n d e d t o t h e r e s u l t i n g s q l i t e 3 . j s
2022-10-25 08:06:17 +00:00
file . It gets its name from being used as the value for the
-- extern - post - js = ... Emscripten flag . Note that this code , unlike
most of the associated JS code , runs outside of the
Emscripten - generated module init scope , in the current
global scope . * /
2022-11-20 01:15:22 +00:00
//#if target=es6-module
2023-01-28 04:20:46 +00:00
const toExportForESM =
2022-11-19 02:51:41 +00:00
//#endif
2022-09-29 13:17:50 +00:00
( function ( ) {
/ * *
2022-11-20 05:36:52 +00:00
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 ) .
2022-09-29 13:17:50 +00:00
Unfortunately , we cannot modify the module - loader / exporter - based
impls which Emscripten installs at some point in the file above
this .
* /
2023-07-13 04:26:13 +00:00
const originalInit = sqlite3InitModule ;
2022-09-29 13:17:50 +00:00
if ( ! originalInit ) {
2023-03-07 19:12:06 +00:00
throw new Error ( "Expecting globalThis.sqlite3InitModule to be defined by the Emscripten build." ) ;
2022-09-29 13:17:50 +00:00
}
2022-10-19 04:44:58 +00:00
/ * *
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
2022-10-20 04:00:05 +00:00
The only ( ? ) current workaround is to temporarily stash this state
2022-10-19 04:44:58 +00:00
into the global scope and delete it when sqlite3InitModule ( )
is called .
* /
2023-03-07 19:12:06 +00:00
const initModuleState = globalThis . sqlite3InitModuleState = Object . assign ( Object . create ( null ) , {
2023-03-09 22:09:13 +00:00
moduleScript : globalThis ? . document ? . currentScript ,
2022-10-20 04:00:05 +00:00
isWorker : ( 'undefined' !== typeof WorkerGlobalScope ) ,
2023-03-07 19:12:06 +00:00
location : globalThis . location ,
urlParams : globalThis ? . location ? . href
? new URL ( globalThis . location . href ) . searchParams
: new URLSearchParams ( )
2022-10-19 04:44:58 +00:00
} ) ;
2022-10-30 09:47:33 +00:00
initModuleState . debugModule =
2023-01-28 04:20:46 +00:00
initModuleState . urlParams . has ( 'sqlite3.debugModule' )
2022-10-30 09:47:33 +00:00
? ( ... args ) => console . warn ( 'sqlite3.debugModule:' , ... args )
: ( ) => { } ;
2022-10-19 04:44:58 +00:00
if ( initModuleState . urlParams . has ( 'sqlite3.dir' ) ) {
initModuleState . sqlite3Dir = initModuleState . urlParams . get ( 'sqlite3.dir' ) + '/' ;
2022-10-20 04:00:05 +00:00
} else if ( initModuleState . moduleScript ) {
const li = initModuleState . moduleScript . src . split ( '/' ) ;
li . pop ( ) ;
initModuleState . sqlite3Dir = li . join ( '/' ) + '/' ;
}
2022-10-19 04:44:58 +00:00
2023-03-07 19:12:06 +00:00
globalThis . sqlite3InitModule = function ff ( ... args ) {
2023-03-09 22:09:13 +00:00
//console.warn("Using replaced sqlite3InitModule()",globalThis.location);
2022-09-29 13:17:50 +00:00
return originalInit ( ... args ) . then ( ( EmscriptenModule ) => {
2023-08-01 16:41:12 +00:00
//#if wasmfs
2023-03-07 19:12:06 +00:00
if ( 'undefined' !== typeof WorkerGlobalScope &&
2023-07-13 10:41:41 +00:00
EmscriptenModule [ 'ENVIRONMENT_IS_PTHREAD' ] ) {
2022-09-29 13:17:50 +00:00
/ * * W o r k a r o u n d f o r w a s m f s - g e n e r a t e d w o r k e r , w h i c h c a l l s t h i s
routine from each individual thread and requires that its
2023-07-13 10:41:41 +00:00
argument be returned . The conditional criteria above are
fragile , based solely on inspection of the offending code ,
not public Emscripten details . * /
//console.warn("sqlite3InitModule() returning E-module.",EmscriptenModule);
2022-09-29 13:17:50 +00:00
return EmscriptenModule ;
}
2023-08-01 16:41:12 +00:00
//#endif
2023-07-13 10:41:41 +00:00
//console.warn("sqlite3InitModule() returning sqlite3 object.");
2022-11-30 05:27:36 +00:00
const s = EmscriptenModule . sqlite3 ;
s . scriptInfo = initModuleState ;
//console.warn("sqlite3.scriptInfo =",s.scriptInfo);
if ( ff . _ _isUnderTest ) s . _ _isUnderTest = true ;
const f = s . asyncPostInit ;
delete s . asyncPostInit ;
2022-09-29 13:17:50 +00:00
return f ( ) ;
2022-09-30 20:35:37 +00:00
} ) . catch ( ( e ) => {
console . error ( "Exception loading sqlite3 module:" , e ) ;
throw e ;
2022-09-29 13:17:50 +00:00
} ) ;
} ;
2023-03-07 19:12:06 +00:00
globalThis . sqlite3InitModule . ready = originalInit . ready ;
2022-10-19 01:07:30 +00:00
2023-03-07 19:12:06 +00:00
if ( globalThis . sqlite3InitModuleState . moduleScript ) {
const sim = globalThis . sqlite3InitModuleState ;
2022-10-19 04:44:58 +00:00
let src = sim . moduleScript . src . split ( '/' ) ;
src . pop ( ) ;
sim . scriptDir = src . join ( '/' ) + '/' ;
2022-10-19 01:07:30 +00:00
}
2022-10-30 09:47:33 +00:00
initModuleState . debugModule ( 'sqlite3InitModuleState =' , initModuleState ) ;
2022-10-19 04:44:58 +00:00
if ( 0 ) {
console . warn ( "Replaced sqlite3InitModule()" ) ;
2023-03-07 19:12:06 +00:00
console . warn ( "globalThis.location.href =" , globalThis . location . href ) ;
2022-10-19 04:44:58 +00:00
if ( 'undefined' !== typeof document ) {
console . warn ( "document.currentScript.src =" ,
document ? . currentScript ? . src ) ;
}
}
2023-01-27 01:33:12 +00:00
//#ifnot target=es6-module
// Emscripten does not inject these module-loader bits in ES6 module
2023-01-28 04:20:46 +00:00
// builds and including them here breaks JS bundlers, so elide them
// from ESM builds.
2022-10-20 04:00:05 +00:00
/ * R e p l a c e t h e v a r i o u s m o d u l e e x p o r t s p e r f o r m e d b y t h e E m s c r i p t e n
glue ... * /
2022-11-20 05:36:52 +00:00
if ( typeof exports === 'object' && typeof module === 'object' ) {
2022-10-20 04:00:05 +00:00
module . exports = sqlite3InitModule ;
2022-11-20 05:36:52 +00:00
} else if ( typeof exports === 'object' ) {
2022-10-20 04:00:05 +00:00
exports [ "sqlite3InitModule" ] = sqlite3InitModule ;
2022-11-20 05:36:52 +00:00
}
2022-10-20 04:00:05 +00:00
/ * A M D m o d u l e s g e t i n j e c t e d i n a w a y w e c a n n o t o v e r r i d e ,
so we can ' t handle those here . * /
2023-01-27 01:33:12 +00:00
//#endif // !target=es6-module
2023-03-07 19:12:06 +00:00
return globalThis . sqlite3InitModule /* required for ESM */ ;
2022-10-19 04:44:58 +00:00
} ) ( ) ;
2022-11-20 01:15:22 +00:00
//#if target=es6-module
2023-07-24 15:41:58 +00:00
sqlite3InitModule = toExportForESM ;
export default sqlite3InitModule ;
2022-11-19 02:51:41 +00:00
//#endif