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/README.md # 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 / README.md` 221 lines ยท 10537 bytes Raw Blame History Rendered 1 # sqlite3-api.js And Friends 2 3 This is the README for the files `sqlite3-*.js` and 4 `sqlite3-wasm.c`. This collection of files is used to build a 5 single-file distribution of the sqlite3 JS/WASM API. It is broken into 6 multiple JS files because: 7 8 1. To facilitate including or excluding certain components for 9 specific use cases. e.g. by removing `sqlite3-api-oo1.js` if the 10 OO#1 API is not needed. 11 12 2. To facilitate modularizing the pieces for use in different WASM 13 build environments. e.g. the files `post-js-*.js` are for use with 14 Emscripten's `--post-js` feature, and nowhere else. 15 As-yet-hypothetical comparable toolchains would necessarily have 16 similar facilities. 17 18 3. Certain components must be in their own standalone files in order 19 to be loaded as JS Workers. 20 21 The structure described here is the current state of things, as of 22 this writing, but is not set in stone forever and may change at any 23 time. This doc targets maintainers of this code and those wanting to 24 dive in to the details, not end user. 25 26 First off, a [pikchr][] of the proverbial onion: 27 28 ```pikchr toggle center 29 scale = 0.85 30 D0: dot invis 31 define matryoshka { 32 $rad = $rad + $2 33 circle with se at D0.c radius $rad 34 text at last.nw $1 below ljust 35 # $anchor = last circle.e 36 } 37 $rad = 5mm 38 C0: circle with se at D0.c "sqlite3-api.js" fit 39 $rad = C0.width / 2 40 matryoshka("--post-js",5mm) 41 matryoshka("Emscripten",12mm) 42 circle with nw at 7mm s of 3mm e of last circle.nw "--pre-js" fit 43 matryoshka("--extern-pre/post-js",9mm) 44 matryoshka("sqlite3.js",6mm) 45 46 #CX: last circle 47 right 48 49 CW: file with w at 15mm e of 3rd circle.ne "sqlite3.wasm" fit 50 arrow from CW.w to 3rd circle.ne 51 52 CC: circle with e at 1cm w of last circle.w "End User" fit radius 1cm 53 arrow <- from CC.e to 6th circle.w 54 ``` 55 56 Actually, `sqlite3.js` and `--extern-js` are the same. The former is 57 what the client sees and the latter is how it looks from a code 58 maintenance point of view. 59 60 At the center of the onion is `sqlite3-api.js`, which gets generated 61 by concatenating the following files together in their listed order: 62 63 - **`sqlite3-api-prologue.js`** 64 Contains the initial bootstrap setup of the sqlite3 API 65 objects. This is exposed as a bootstrapping function so that 66 the next step can pass in a config object which abstracts away parts 67 of the WASM environment, to facilitate plugging it in to arbitrary 68 WASM toolchains. The bootstrapping function is part of the plumbing, 69 not a public API, and is not exposed to clients but may be used by 70 clients in custom builds. 71 - **`../common/whwasmutil.c-pp.js`** 72 A semi-third-party collection of JS/WASM utility code intended to 73 replace much of the Emscripten glue. The sqlite3 APIs internally use 74 these APIs instead of their Emscripten counterparts, in order to be 75 more portable to arbitrary WASM toolchains and help reduce 76 regressions caused by toolchain-level behavioral changes. This API 77 is configurable, in principle, for use with arbitrary WASM 78 toolchains. It is "semi-third-party" in that it was created in order 79 to support this tree but is standalone and maintained together 80 with... 81 - **`../jaccwabyt/jaccwabyt.c-pp.js`** 82 A semi-third-party API which creates bindings between JS and C 83 structs, such that changes to the struct state from either JS or C 84 are visible to the other end of the connection. This is also an 85 independent spinoff project, conceived for the sqlite3 project but 86 maintained separately. 87 - **`sqlite3-api-glue.c-pp.js`** 88 Invokes functionality exposed by the previous two files to flesh out 89 low-level parts of `sqlite3-api-prologue.js`. Most of these pieces 90 involve populating the `sqlite3.capi.wasm` object and creating 91 `sqlite3.capi.sqlite3_...()` bindings. This file also deletes most 92 global-scope symbols the above files create, effectively moving them 93 into the scope being used for initializing the API. 94 - **`/sqlite3-api-build-version.js`** 95 Gets created by the build process and populates the 96 `sqlite3.version` object. This part is not critical, but records the 97 version of the library against which this module was built. 98 - **`sqlite3-api-oo1.c-pp.js`** 99 Provides a high-level object-oriented wrapper to the lower-level C 100 API, colloquially known as OO API #1. Its API is similar to other 101 high-level sqlite3 JS wrappers and should feel relatively familiar 102 to anyone familiar with such APIs. It is not a "required component" 103 and can be elided from builds which do not want it. 104 - **`sqlite3-api-worker1.jc-pp.s`** 105 A Worker-thread-based API which uses OO API #1 to provide an 106 interface to a database which can be driven from the main Window 107 thread via the Worker message-passing interface. Like OO API #1, 108 this is an optional component, offering one of any number of 109 potential implementations for such an API. 110 - **`sqlite3-worker1.c-pp.js`** 111 Is not part of the amalgamated sources and is intended to be 112 loaded by a client Worker thread. It loads the sqlite3 module 113 and runs the Worker #1 API which is implemented in 114 `sqlite3-api-worker1.c-pp.js`. 115 - **`sqlite3-worker1-promiser.c-pp.js`** 116 Is likewise not part of the amalgamated sources and provides 117 a Promise-based interface into the Worker #1 API. This is 118 a far user-friendlier way to interface with databases running 119 in a Worker thread. 120 - **`sqlite3-vfs-helper.c-pp.js`** 121 Installs the `sqlite3.vfs` namespace, which contain helpers for use 122 by downstream code which creates `sqlite3_vfs` implementations. 123 - **`sqlite3-vtab-helper.c-pp.js`** 124 Installs the `sqlite3.vtab` namespace, which contain helpers for use 125 by downstream code which creates `sqlite3_module` implementations. 126 - **`sqlite3-vfs-opfs.c-pp.js`** 127 is an sqlite3 VFS implementation which supports the [Origin-Private 128 FileSystem (OPFS)][OPFS] as a storage layer to provide persistent 129 storage for database files in a browser. It requires... 130 - **`sqlite3-opfs-async-proxy.c-pp.js`** 131 is the asynchronous backend part of the [OPFS][] proxy. It 132 speaks directly to the (async) OPFS API and channels those 133 results back to its synchronous counterpart. This file, because 134 it must be started in its own Worker, is not part of the 135 amalgamation. 136 - **`sqlite3-vfs-opfs-sahpool.c-pp.js`** 137 is another sqlite3 VFS supporting the [OPFS][], but uses a 138 completely different approach than the above-listed one. 139 140 The previous files do not immediately extend the library. Instead they 141 use the amalgamation-local function `sqlite3ApiBootstrap()`, which the 142 code at the end of the process must call to configure the library for 143 the current JS/WASM environment. Each file listed above pushes a 144 callback into the bootstrapping queue, to be called as part of 145 `sqlite3ApiBootstrap()`. Some files also temporarily create global 146 objects in order to communicate their state to the files which follow 147 them. Those get cleaned up vi `post-js-footer.c-pp.js`, described 148 below. 149 150 Adapting the build for non-Emscripten toolchains essentially requires 151 packaging the above files, concatated together, into that toolchain's 152 "JS glue" and, in the final stage of that glue, call 153 `sqlite3ApiBootstrap()` and return its result to the end user. 154 155 **Files with the extension `.c-pp.js`** are intended [to be processed 156 with `c-pp`](#c-pp), noting that such preprocessing may be applied 157 after all of the relevant files are concatenated. The `.c-pp.js` 158 extension is used primarily to keep the code maintainers cognisant of 159 the fact that those files contain constructs which may not run as-is 160 in any given JavaScript environment. 161 162 The build process glues those files together, resulting in 163 `sqlite3-api.js`, which is everything except for the 164 Emscripten-specific files detailed below (into which `sqlite3-api.js` 165 gets injected). 166 167 The non-JS outlier file is `sqlite3-wasm.c`: it is a proxy for 168 `sqlite3.c` which `#include`'s that file and adds a handful of 169 WASM-specific helper functions, at least one of which requires access 170 to private/static `sqlite3.c` internals. `sqlite3.wasm` is compiled 171 from this file rather than `sqlite3.c`. 172 173 The following Emscripten-specific files are injected into the 174 build-generated `sqlite3.js` along with `sqlite3-api.js`. 175 176 - **`extern-pre-js.c-pp.js`** 177 Emscripten-specific header for Emscripten's `--extern-pre-js` 178 flag. As of this writing, that file is only used for experimentation 179 purposes and holds no code relevant to the production deliverables. 180 - **`pre-js.c-pp.js`** 181 Emscripten-specific header for Emscripten's `--pre-js` flag. This 182 file overrides certain Emscripten behavior before Emscripten does 183 most of its work. 184 - **`post-js-header.js`** 185 Emscripten-specific header for the `--post-js` input. It opens up, 186 but does not close, a function used for initializing the library. 187 - **`sqlite3-api.js`** gets sandwiched between these ↑ two 188 ↓ files. 189 - **`post-js-footer.js`** 190 Emscripten-specific footer for the `--post-js` input. This closes 191 off the function opened by `post-js-header.js`, cleans up 192 any dangling globals, and runs `sqlite3ApiBootstrap()`. As of this 193 writing, this code ensures that the previous files leave no more 194 than a single global symbol installed - `sqlite3InitModule()`. 195 196 - **`extern-post-js.c-pp.js`** 197 Emscripten-specific header for Emscripten's `--extern-post-js` 198 flag. This file is run in the global scope. It overwrites the 199 Emscripten-installed `sqlite3InitModule()` function with one which 200 first runs the original implementation, then runs the function 201 installed by `post-js-{header,footer}.js` to initialize the library, 202 then initializes the asynchronous parts of the sqlite3 module (for 203 example, the [OPFS][] VFS support). Its final step is to return a 204 Promise of the sqlite3 namespace object to the caller of 205 `sqlite3InitModule()` (the library user). 206 207 208 Preprocessing of Source Files 209 ------------------------------------------------------------------------ 210 211 Certain files in the build require preprocessing to filter in/out 212 parts which differ between vanilla JS, ES6 Modules, and node.js 213 builds. The preprocessor application itself is in 214 [`libcmpp.c`](/file/ext/wasm/libcmpp.c) and the complete technical 215 details of such preprocessing are maintained in 216 [`GNUMakefile`](/file/ext/wasm/GNUmakefile) and 217 [`mkwasmbuilds.c`](/file/ext/wasm/mkwasmbuilds.c) and. 218 219 220 [OPFS]: https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system 221 [pikchr]: https://pikchr.org