interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / wasm / api / README.md

221 lines · 10537 bytes Raw Blame History Source

sqlite3-api.js And Friends

This is the README for the files sqlite3-*.js and sqlite3-wasm.c. This collection of files is used to build a single-file distribution of the sqlite3 JS/WASM API. It is broken into multiple JS files because:

  1. To facilitate including or excluding certain components for specific use cases. e.g. by removing sqlite3-api-oo1.js if the OO#1 API is not needed.
  2. To facilitate modularizing the pieces for use in different WASM build environments. e.g. the files post-js-*.js are for use with Emscripten's --post-js feature, and nowhere else. As-yet-hypothetical comparable toolchains would necessarily have similar facilities.

  3. Certain components must be in their own standalone files in order to be loaded as JS Workers.

The structure described here is the current state of things, as of this writing, but is not set in stone forever and may change at any time. This doc targets maintainers of this code and those wanting to dive in to the details, not end user.

First off, a pikchr of the proverbial onion:

scale = 0.85
D0: dot invis
define matryoshka {
  $rad = $rad + $2
  circle with se at D0.c radius $rad
  text at last.nw $1 below ljust
#  $anchor = last circle.e
}
$rad = 5mm
C0: circle with se at D0.c "sqlite3-api.js" fit
$rad = C0.width / 2
matryoshka("--post-js",5mm)
matryoshka("Emscripten",12mm)
circle with nw at 7mm s of 3mm e of last circle.nw "--pre-js" fit
matryoshka("--extern-pre/post-js",9mm)
matryoshka("sqlite3.js",6mm)

#CX: last circle
right

CW: file with w at 15mm e of 3rd circle.ne "sqlite3.wasm" fit
arrow from CW.w to 3rd circle.ne

CC: circle with e at 1cm w of last circle.w "End User" fit radius 1cm
arrow <- from CC.e to 6th circle.w

Actually, sqlite3.js and --extern-js are the same. The former is what the client sees and the latter is how it looks from a code maintenance point of view.

At the center of the onion is sqlite3-api.js, which gets generated by concatenating the following files together in their listed order:

The previous files do not immediately extend the library. Instead they use the amalgamation-local function sqlite3ApiBootstrap(), which the code at the end of the process must call to configure the library for the current JS/WASM environment. Each file listed above pushes a callback into the bootstrapping queue, to be called as part of sqlite3ApiBootstrap(). Some files also temporarily create global objects in order to communicate their state to the files which follow them. Those get cleaned up vi post-js-footer.c-pp.js, described below.

Adapting the build for non-Emscripten toolchains essentially requires packaging the above files, concatated together, into that toolchain's "JS glue" and, in the final stage of that glue, call sqlite3ApiBootstrap() and return its result to the end user.

Files with the extension .c-pp.js are intended [to be processed with c-pp](#c-pp), noting that such preprocessing may be applied after all of the relevant files are concatenated. The .c-pp.js extension is used primarily to keep the code maintainers cognisant of the fact that those files contain constructs which may not run as-is in any given JavaScript environment.

The build process glues those files together, resulting in sqlite3-api.js, which is everything except for the Emscripten-specific files detailed below (into which sqlite3-api.js gets injected).

The non-JS outlier file is sqlite3-wasm.c: it is a proxy for sqlite3.c which #include's that file and adds a handful of WASM-specific helper functions, at least one of which requires access to private/static sqlite3.c internals. sqlite3.wasm is compiled from this file rather than sqlite3.c.

The following Emscripten-specific files are injected into the build-generated sqlite3.js along with sqlite3-api.js.

<a id='c-pp'></a> Preprocessing of Source Files


Certain files in the build require preprocessing to filter in/out parts which differ between vanilla JS, ES6 Modules, and node.js builds. The preprocessor application itself is in libcmpp.c and the complete technical details of such preprocessing are maintained in GNUMakefile and mkwasmbuilds.c and.