sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / wasm / api / extern-post-js.c-pp.js
| 1 | |
| 2 | //#if 0 |
| 3 | /* ^^^^ ACHTUNG: blank line at the start is necessary because |
| 4 | Emscripten will not add a newline in some cases and we need |
| 5 | a blank line for a sed-based kludge for the ES6 build. |
| 6 | |
| 7 | extern-post-js.js must be appended to the resulting sqlite3.js |
| 8 | file. It gets its name from being used as the value for the |
| 9 | --extern-post-js=... Emscripten flag. This code, unlike most of the |
| 10 | associated JS code, runs outside of the Emscripten-generated module |
| 11 | init scope, in the current global scope. |
| 12 | |
| 13 | At the time this is run, the sqlite3InitModule function will have |
| 14 | just been defined via a `var` decl. This file overwrites it with a |
| 15 | custom one and then (in ESM builds) exports it. Getting ESM to work |
| 16 | here requires a build-time kludge of stripping the 'export default' |
| 17 | which Emscripten forcibly injects into its glue. |
| 18 | */ |
| 19 | //#/if |
| 20 | //#if target:es6-module |
| 21 | export default |
| 22 | //#/if |
| 23 | (function(){ |
| 24 | //console.warn("this is extern-post-js"); |
| 25 | /** |
| 26 | In order to hide the sqlite3InitModule()'s resulting |
| 27 | Emscripten module from downstream clients (and simplify our |
| 28 | documentation by being able to elide those details), we hide that |
| 29 | function and expose a hand-written sqlite3InitModule() to return |
| 30 | the sqlite3 object (most of the time). |
| 31 | */ |
| 32 | const originalInit = sqlite3InitModule; |
| 33 | if(!originalInit){ |
| 34 | throw new Error("Expecting sqlite3InitModule to be defined by the Emscripten build."); |
| 35 | } |
| 36 | /** |
| 37 | sIMS => sqlite3InitModuleState |
| 38 | |
| 39 | We need to add some state which our custom Module.locateFile() |
| 40 | can see, but an Emscripten limitation currently prevents us from |
| 41 | attaching it to the sqlite3InitModule function object: |
| 42 | |
| 43 | https://github.com/emscripten-core/emscripten/issues/18071 |
| 44 | |
| 45 | The only(?) current workaround is to temporarily stash this state |
| 46 | into the global scope and delete it when sqlite3InitModule() |
| 47 | is called. |
| 48 | |
| 49 | This state is consumed by pre-js.c-pp.js. That file lives above |
| 50 | this one in the built JS module but it's executed after this one. |
| 51 | */ |
| 52 | const sIMS = Object.assign(Object.create(null),{ |
| 53 | moduleScript: globalThis?.document?.currentScript, |
| 54 | isWorker: ('undefined' !== typeof WorkerGlobalScope), |
| 55 | location: globalThis?.location, |
| 56 | urlParams: globalThis?.location?.href |
| 57 | ? new URL(globalThis.location.href).searchParams |
| 58 | : new URLSearchParams() |
| 59 | /* |
| 60 | It is literally impossible to reliably get the name of _this_ script |
| 61 | at runtime, so impossible to reliably derive X.wasm from script name |
| 62 | X.js. (This is apparently why Emscripten hard-codes the name of the |
| 63 | wasm file into their output.) Thus we need, at build-time, to set |
| 64 | the name of the WASM file which our custom instantiateWasm() should to |
| 65 | load. The build process populates this. |
| 66 | |
| 67 | The primary reason we do this is because disparate builds |
| 68 | usually result in different JS but identical WASM files. To |
| 69 | avoid, e.g., that we have to ship two separate WASM files for |
| 70 | vanilla and ESM builds, we rewire all builds which produce |
| 71 | identical binaries to use a single copy of those binaries. |
| 72 | |
| 73 | Module.instantiateWasm() is found in pre-js.c-pp.js. |
| 74 | */ |
| 75 | //#if defined sqlite3.wasm |
| 76 | //#@ push policy error delimiter "<<<" ">>>" |
| 77 | //#/if |
| 78 | ,wasmFilename: '<<<sqlite3.wasm>>>' /* replaced by the build process */ |
| 79 | //#if defined sqlite3.wasm |
| 80 | //#@ pop policy delimiter |
| 81 | //#/if |
| 82 | }); |
| 83 | //#if not target:es6-bundler-friendly and not target:node |
| 84 | //#// ^^^ mirrored in pre-js.c-pp.js |
| 85 | globalThis.sqlite3InitModuleState = sIMS; |
| 86 | //#/if |
| 87 | sIMS.debugModule = |
| 88 | sIMS.urlParams.has('sqlite3.debugModule') |
| 89 | ? (...args)=>console.warn('sqlite3.debugModule:',...args) |
| 90 | : ()=>{}; |
| 91 | |
| 92 | /** |
| 93 | Fetches sIMS.urlParams.get(key). If that looks like it might |
| 94 | be a remote path value then null is returned and a console |
| 95 | warning may be emitted. If it looks like it's a local path, |
| 96 | it is returned. |
| 97 | |
| 98 | [bugs:74f052d3d2]: do not allow URL arguments which are used for |
| 99 | determining file locations to be anything other than what appears |
| 100 | to be a local file reference. That is the only documented use for |
| 101 | these args and allowing URLs as values leads to chaos. This is |
| 102 | believed to resolve that report but is also considered a stopgap |
| 103 | measure. Potential TODO is only allow query parameters from the |
| 104 | same origin as this script. |
| 105 | */ |
| 106 | const getPathArgument = (key)=>{ |
| 107 | const s = sIMS.urlParams.get(key); |
| 108 | //console.warn("sIMS.getPathArgument(",key,")",s); |
| 109 | if( !s ) return s; |
| 110 | if( s.indexOf(':')>=0 || s.indexOf('//')>=0 ){ |
| 111 | console.warn( |
| 112 | "Rejecting path string",key," because it does not look", |
| 113 | "like a local path:",s |
| 114 | ); |
| 115 | return null; |
| 116 | } |
| 117 | return s; |
| 118 | }; |
| 119 | //#if not target:es6-module /* needed for non-ESM in pre-js.c-pp.js */ |
| 120 | sIMS.getPathArgument = getPathArgument; |
| 121 | //#/if |
| 122 | if( sIMS.urlParams.has('sqlite3.dir') ){ |
| 123 | const s = getPathArgument('sqlite3.dir'); |
| 124 | if( s ){ |
| 125 | sIMS.sqlite3Dir = s + (s[s.length-1]==='/' ? '' : '/'); |
| 126 | } |
| 127 | }else if( sIMS.moduleScript?.src ){ |
| 128 | const li = sIMS.moduleScript.src.split('/'); |
| 129 | li.pop(); |
| 130 | sIMS.sqlite3Dir = li.join('/') + '/'; |
| 131 | } |
| 132 | |
| 133 | /** The replacement for Emscripten-generated sqlite3InitModule(). */ |
| 134 | const sIM = sqlite3InitModule = function ff(...args){ |
| 135 | //console.warn("Using replaced sqlite3InitModule()",globalThis.location); |
| 136 | /** |
| 137 | Conventionally, the first argument to an Emscripten-generated |
| 138 | module init function is a client-provided Object which |
| 139 | overrides the Module object. Because we cannot offer any sort |
| 140 | of API stability for that, we have historically not processed |
| 141 | or accepted any arguments. As a "back door" we offer selective |
| 142 | overrides of some options, as handled below via args[0]. |
| 143 | |
| 144 | See pre-js.c-pp.js [tag:locateFile] for the details and a link |
| 145 | to the relevant discussion. |
| 146 | */ |
| 147 | sIMS.emscriptenLocateFile = args[0]?.locateFile; |
| 148 | sIMS.emscriptenInstantiateWasm = args[0]?.instantiateWasm; |
| 149 | return originalInit(...args).then((EmscriptenModule)=>{ |
| 150 | sIMS.debugModule("sqlite3InitModule() sIMS =",sIMS); |
| 151 | sIMS.debugModule("sqlite3InitModule() EmscriptenModule =",EmscriptenModule); |
| 152 | const s = EmscriptenModule.runSQLite3PostLoadInit( |
| 153 | sIMS, |
| 154 | EmscriptenModule /* see post-js-header/footer.js */, |
| 155 | !!ff.__isUnderTest |
| 156 | ); |
| 157 | sIMS.debugModule("sqlite3InitModule() sqlite3 =",s); |
| 158 | //#if wasmfs |
| 159 | if('undefined'!==typeof WorkerGlobalScope && |
| 160 | EmscriptenModule['ENVIRONMENT_IS_PTHREAD']){ |
| 161 | /** Workaround for wasmfs-generated worker, which calls this |
| 162 | routine from each individual thread and requires that its |
| 163 | argument be returned. The if() condition above is fragile, |
| 164 | based solely on inspection of the offending code, not |
| 165 | public Emscripten details. */ |
| 166 | //console.warn("sqlite3InitModule() returning E-module.",EmscriptenModule); |
| 167 | return EmscriptenModule; |
| 168 | } |
| 169 | //#/if |
| 170 | return s; |
| 171 | }).catch((e)=>{ |
| 172 | console.error("Exception loading sqlite3 module:",e); |
| 173 | throw e; |
| 174 | }); |
| 175 | }; |
| 176 | if( sIMS.moduleScript?.src ){ |
| 177 | /* Record this script's directory part */ |
| 178 | const src = sIMS.moduleScript.src.split('/'); |
| 179 | src.pop(); |
| 180 | sIMS.scriptDir = src.join('/') + '/'; |
| 181 | } |
| 182 | sIMS.debugModule('extern-post-js.c-pp.js sqlite3InitModuleState =',sIMS); |
| 183 | //#if not target:es6-module |
| 184 | //#if 0 |
| 185 | /* Emscripten does not inject these module-loader bits in ES6 module |
| 186 | builds and including them here breaks JS bundlers, so elide them |
| 187 | from ESM builds. */ |
| 188 | //#/if |
| 189 | /* Replace the various module exports performed by the Emscripten |
| 190 | glue so that they link to our overwritten copy... */ |
| 191 | if( typeof exports === 'object' && typeof module === 'object' ){ |
| 192 | module.exports = sIM; |
| 193 | module.exports.default = sIM /* allows TS to import this commonjs style */; |
| 194 | }else if( 'function'===typeof define && define['amd'] ){ |
| 195 | define([], ()=>sIM); |
| 196 | }else if( typeof exports === 'object' ){ |
| 197 | /* 2026-10-30: Emscripten no longer emits this one, so we can probably |
| 198 | remove it. */ |
| 199 | exports["sqlite3InitModule"] = sIM; |
| 200 | } |
| 201 | //#/if // !target:es6-module |
| 202 | return sIM; |
| 203 | })(); |