1/** 2 BEGIN FILE: api/pre-js.js 3 4 This file is intended to be prepended to the sqlite3.js build using 5 Emscripten's --pre-js=THIS_FILE flag (or equivalent). 6*/ 7 8// See notes in extern-post-js.js 9const sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null); 10delete self.sqlite3InitModuleState; 11 12/** 13 This custom locateFile() tries to figure out where to load `path` 14 from. The intent is to provide a way for foo/bar/X.js loaded from a 15 Worker constructor or importScripts() to be able to resolve 16 foo/bar/X.wasm (in the latter case, with some help): 17 18 1) If URL param named the same as `path` is set, it is returned. 19 20 2) If sqlite3InitModuleState.sqlite3Dir is set, then (thatName + path) 21 is returned (note that it's assumed to end with '/'). 22 23 3) If this code is running in the main UI thread AND it was loaded 24 from a SCRIPT tag, the directory part of that URL is used 25 as the prefix. (This form of resolution unfortunately does not 26 function for scripts loaded via importScripts().) 27 28 4) If none of the above apply, (prefix+path) is returned. 29*/ 30Module['locateFile'] = function(path, prefix) { 31 let theFile; 32 const up = this.urlParams; 33 if(0){ 34 console.warn("locateFile(",arguments[0], ',', arguments[1],")", 35 'self.location =',self.location, 36 'sqlite3InitModuleState.scriptDir =',this.scriptDir, 37 'up.entries() =',Array.from(up.entries())); 38 } 39 if(up.has(path)){ 40 theFile = up.get(path); 41 }else if(this.sqlite3Dir){ 42 theFile = this.sqlite3Dir + path; 43 }else if(this.scriptDir){ 44 theFile = this.scriptDir + path; 45 }else{ 46 theFile = prefix + path; 47 } 48 return theFile; 49}.bind(sqlite3InitModuleState); 50 51/** 52 Bug warning: this xInstantiateWasm bit must remain disabled 53 until this bug is resolved or wasmfs won't work: 54 55 https://github.com/emscripten-core/emscripten/issues/17951 56*/ 57const xInstantiateWasm = 1 58 ? 'emscripten-bug-17951' 59 : 'instantiateWasm'; 60Module[xInstantiateWasm] = function callee(imports,onSuccess){ 61 imports.env.foo = function(){}; 62 console.warn("instantiateWasm() uri =",callee.uri, self.location.href); 63 const wfetch = ()=>fetch(callee.uri, {credentials: 'same-origin'}); 64 const loadWasm = WebAssembly.instantiateStreaming 65 ? async ()=>{ 66 return WebAssembly.instantiateStreaming(wfetch(), imports) 67 .then((arg)=>onSuccess(arg.instance, arg.module)); 68 } 69 : async ()=>{ // Safari < v15 70 return wfetch() 71 .then(response => response.arrayBuffer()) 72 .then(bytes => WebAssembly.instantiate(bytes, imports)) 73 .then((arg)=>onSuccess(arg.instance, arg.module)); 74 }; 75 loadWasm(); 76 return {}; 77}; 78/* 79 It is literally impossible to reliably get the name of _this_ script 80 at runtime, so impossible to derive X.wasm from script name 81 X.js. Thus we need, at build-time, to redefine 82 Module[xInstantiateWasm].uri by appending it to a build-specific 83 copy of this file with the name of the wasm file. This is apparently 84 why Emscripten hard-codes the name of the wasm file into their glue 85 scripts. 86*/ 87Module[xInstantiateWasm].uri = 'sqlite3.wasm'; 88/* END FILE: api/pre-js.js */ 89