Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/wasm/api/pre-js.c-pp.js # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / wasm / api / pre-js.c-pp.js` 192 lines ยท 7215 bytes Raw Blame History 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). It is run 6 from inside of sqlite3InitModule(), after Emscripten's Module is 7 defined, but early enough that we can ammend, or even outright 8 replace, Module from here. 9 10 Because this runs in-between Emscripten's own bootstrapping and 11 Emscripten's main work, we must be careful with file-local symbol 12 names. e.g. don't overwrite anything Emscripten defines and do not 13 use 'const' for local symbols which Emscripten might try to use for 14 itself. i.e. try to keep file-local symbol names obnoxiously 15 collision-resistant. 16 */ 17 //#@ policy error 18 //#define canonical-sqlite3 19 /** 20 This file was preprocessed using: 21 22 //#@ policy error << 23 @c-pp::argv@ 24 //#/@ 25 */ 26 //#if unsupported-build 27 /** 28 UNSUPPORTED BUILD: 29 30 This SQLite JS build configuration is entirely unsupported! It has 31 not been tested beyond the ability to compile it. It may not 32 load. It may not work properly. Only builds _directly_ targeting 33 browser environments ("vanilla" JS and ESM modules) are supported 34 and tested. Builds which _indirectly_ target browsers (namely 35 bundler-friendly builds and any node builds) are not supported 36 deliverables. 37 */ 38 //#/if 39 //#if not target:es6-bundler-friendly and not target:node 40 (function(Module){ 41 const sIMS = 42 globalThis.sqlite3InitModuleState/*from extern-post-js.c-pp.js*/ 43 || Object.assign(Object.create(null),{ 44 /* In WASMFS builds this file gets loaded once per thread, 45 but sqlite3InitModuleState is not getting set for the 46 worker threads? That those workers seem to function fine 47 despite that is curious. */ 48 debugModule: function(){ 49 console.warn("globalThis.sqlite3InitModuleState is missing",arguments); 50 } 51 }); 52 delete globalThis.sqlite3InitModuleState; 53 sIMS.debugModule('pre-js.js sqlite3InitModuleState =',sIMS); 54 55 /** 56 This custom locateFile() tries to figure out where to load `path` 57 from. The intent is to provide a way for foo/bar/X.js loaded from a 58 Worker constructor or importScripts() to be able to resolve 59 foo/bar/X.wasm (in the latter case, with some help): 60 61 1) If URL param named the same as `path` is set and it looks like 62 a local path reference, it is returned. 63 64 2) If sqlite3InitModuleState.sqlite3Dir is set, then (thatName + path) 65 is returned (it's assumed to end with '/'). 66 67 3) If this code is running in the main UI thread AND it was loaded 68 from a SCRIPT tag, the directory part of that URL is used 69 as the prefix. (This form of resolution unfortunately does not 70 function for scripts loaded via importScripts().) 71 72 4) If none of the above apply, (prefix+path) is returned. 73 74 None of the above apply in ES6 builds, which uses a much simpler 75 approach. 76 */ 77 Module['locateFile'] = function(path, prefix) { 78 if( this.emscriptenLocateFile instanceof Function ){ 79 /* [tag:locateFile] Client-overridden impl. We do not support 80 this but offer it as a back-door which will go away the 81 moment either Emscripten changes that interface or we manage 82 to get non-Emscripten builds working. 83 https://sqlite.org/forum/forumpost/`1eec339854c935bd` 84 85 See extern-post-js.c-pp.js [tag:locateFile]. 86 */ 87 return this.emscriptenLocateFile(path, prefix); 88 } 89 //#if target:es6-module 90 return new URL(path, import.meta.url).href; 91 //#else 92 'use strict'; 93 let theFile = this.getPathArgument(path); 94 if( !theFile ){ 95 if(this.sqlite3Dir){ 96 theFile = this.sqlite3Dir + path; 97 }else if(this.scriptDir){ 98 theFile = this.scriptDir + path; 99 }else{ 100 theFile = prefix + path; 101 } 102 } 103 this.debugModule( 104 "locateFile(",arguments[0], ',', arguments[1],")", 105 'sqlite3InitModuleState.scriptDir =',this.scriptDir, 106 'urlParams.entries() =',Array.from(this.urlParams.entries()), 107 "result =", theFile 108 ); 109 return theFile; 110 //#/if target:es6-module 111 }.bind(sIMS); 112 113 //#if Module.instantiateWasm and not wasmfs and not target:node 114 /** 115 Override Module.instantiateWasm(). 116 117 A custom Module.instantiateWasm() does not work in WASMFS builds: 118 119 https://github.com/emscripten-core/emscripten/issues/17951 120 121 In such builds we must disable this. 122 123 It's disabled in the (unsupported/untested) node builds because 124 node does not do fetch(). 125 */ 126 Module['instantiateWasm'] = function callee(imports, onSuccess){ 127 if( this.emscriptenInstantiateWasm instanceof Function ){ 128 /* See [tag:locateFile]. Same story here */ 129 return this.emscriptenInstantiateWasm(imports, onSuccess); 130 } 131 const sims = this; 132 const finalThen = (arg)=>{ 133 arg.imports = imports; 134 sims.instantiateWasm = arg; /* used by sqlite3-api-prologue.c-pp.js */ 135 onSuccess(arg.instance, arg.module); 136 }; 137 //#if 0 138 /* 139 The following block uses //% as a preprocessor directive delimiter 140 and is replaced in a second preprocessor pass after compilation of 141 sqlite3.js. It implements two ways to reach the WASM file: 142 embedding it inline as a base64 blob or via a URL. The base64 143 approach results in massive memory spikes when the module loads, as 144 it needs to have the base64, the decoded base64, and the whole 145 resulting compiled WASM binary in memory at once. Loading from a 146 URL allows streaming, such that the compiler can discard the pieces 147 of the input it has already processed. i.e. don't embed as 148 base64. It's interesting but severely memory-costly. 149 */ 150 //#/if 151 //%define ? base64.wasm 0 152 //%if base64.wasm and defined sqlite3.wasm.base64 153 //%@ push policy error delimiter '<<<' '>>>' 154 let b64Wasm = `<<<[base64 -file sqlite3.wasm.base64]>>>`; 155 //%@ pop policy delimiter 156 const bytes = Uint8Array.fromBase64(b64Wasm); 157 b64Wasm = null; 158 159 WebAssembly.instantiate(bytes.buffer, imports) 160 .then(finalThen) 161 .catch((err)=>{ 162 console.error("Failed to instantiate embedded sqlite3.wasm", err); 163 throw err; 164 }); 165 166 return {}/* Emscripten interprets this to wait on async instantiation 167 via onSuccess(). */; 168 //%else 169 const uri = Module.locateFile( 170 sims.wasmFilename, ( 171 ('undefined'===typeof scriptDirectory/*var defined by Emscripten glue*/) 172 ? "" : scriptDirectory) 173 ); 174 sims.debugModule("instantiateWasm() uri =", uri, "sIMS =",this); 175 const wfetch = ()=>fetch(uri, {credentials: 'same-origin'}); 176 const loadWasm = WebAssembly.instantiateStreaming 177 ? async ()=> 178 WebAssembly 179 .instantiateStreaming(wfetch(), imports) 180 .then(finalThen) 181 : async ()=>// Safari < v15 182 wfetch() 183 .then(response => response.arrayBuffer()) 184 .then(bytes => WebAssembly.instantiate(bytes, imports)) 185 .then(finalThen) 186 return loadWasm(); 187 //%/if sqlite3.wasm.base64 pass 2 188 }.bind(sIMS); 189 //#/if Module.instantiateWasm and not wasmfs 190 })(Module); 191 //#/if not target:es6-bundler-friendly and not target:node 192 /* END FILE: api/pre-js.js. */