2 BEGIN FILE: api/pre-js.js
4 This file is intended to be prepended to the sqlite3.js build using
5 Emscripten's --pre-js=THIS_FILE flag (or equivalent).
8 // See notes in extern-post-js.js
9 const sqlite3InitModuleState = globalThis.sqlite3InitModuleState
10 || Object.assign(Object.create(null),{
13 delete globalThis.sqlite3InitModuleState;
14 sqlite3InitModuleState.debugModule('globalThis.location =',globalThis.location);
16 //#ifnot target=es6-bundler-friendly
18 This custom locateFile() tries to figure out where to load `path`
19 from. The intent is to provide a way for foo/bar/X.js loaded from a
20 Worker constructor or importScripts() to be able to resolve
21 foo/bar/X.wasm (in the latter case, with some help):
23 1) If URL param named the same as `path` is set, it is returned.
25 2) If sqlite3InitModuleState.sqlite3Dir is set, then (thatName + path)
26 is returned (note that it's assumed to end with '/').
28 3) If this code is running in the main UI thread AND it was loaded
29 from a SCRIPT tag, the directory part of that URL is used
30 as the prefix. (This form of resolution unfortunately does not
31 function for scripts loaded via importScripts().)
33 4) If none of the above apply, (prefix+path) is returned.
35 Module['locateFile'] = function(path, prefix) {
36 //#if target=es6-module
37 return new URL(path, import.meta.url).href;
41 const up = this.urlParams;
43 theFile = up.get(path);
44 }else if(this.sqlite3Dir){
45 theFile = this.sqlite3Dir + path;
46 }else if(this.scriptDir){
47 theFile = this.scriptDir + path;
49 theFile = prefix + path;
51 sqlite3InitModuleState.debugModule(
52 "locateFile(",arguments[0], ',', arguments[1],")",
53 'sqlite3InitModuleState.scriptDir =',this.scriptDir,
54 'up.entries() =',Array.from(up.entries()),
58 //#endif target=es6-module
59 }.bind(sqlite3InitModuleState);
60 //#endif ifnot target=es6-bundler-friendly
63 Bug warning: a custom Module.instantiateWasm() does not work
66 https://github.com/emscripten-core/emscripten/issues/17951
68 In such builds we must disable this.
70 const xNameOfInstantiateWasm = false
72 : 'emscripten-bug-17951';
73 Module[xNameOfInstantiateWasm] = function callee(imports,onSuccess){
74 imports.env.foo = function(){};
75 const uri = Module.locateFile(
77 ('undefined'===typeof scriptDirectory/*var defined by Emscripten glue*/)
78 ? "" : scriptDirectory)
80 sqlite3InitModuleState.debugModule(
81 "instantiateWasm() uri =", uri
83 const wfetch = ()=>fetch(uri, {credentials: 'same-origin'});
84 const loadWasm = WebAssembly.instantiateStreaming
86 return WebAssembly.instantiateStreaming(wfetch(), imports)
87 .then((arg)=>onSuccess(arg.instance, arg.module));
89 : async ()=>{ // Safari < v15
91 .then(response => response.arrayBuffer())
92 .then(bytes => WebAssembly.instantiate(bytes, imports))
93 .then((arg)=>onSuccess(arg.instance, arg.module));
99 It is literally impossible to reliably get the name of _this_ script
100 at runtime, so impossible to derive X.wasm from script name
101 X.js. Thus we need, at build-time, to redefine
102 Module[xNameOfInstantiateWasm].uri by appending it to a build-specific
103 copy of this file with the name of the wasm file. This is apparently
104 why Emscripten hard-codes the name of the wasm file into their glue
107 Module[xNameOfInstantiateWasm].uri = 'sqlite3.wasm';
108 /* END FILE: api/pre-js.js, noting that the build process may add a
109 line after this one to change the above .uri to a build-specific