Home

Awesome

Building a WASM sqlite with an embedded extension

I wanted to build a wasm version of sqlite, but I also wanted to include an extension in it.

As far as I know, you can't dynamically load extensions into the wasm version of sqlite, so I wanted to build a version that had it statically linked in.

As I don't know C very well, this seemed pretty hopeless - but thankfully I found sqlite-lines by @asg017 (who also kindly helped me on twitter), which demonstrated that it was possible.

update (Nov 11 2024): there are official instructions for adding an extension to the official WASM build (this uses sql.js instead, as the official wasm build did not exist at the time of its writing). I have not yet tried them out

The pieces

To build it, I followed in Alex's footsteps and used these pieces:

The idea

The basic idea of building a sqlite that has an extension statically linked is that you can use an undocumented C preprocessor macro called SQLITE_EXTRA_INIT which will, if defined, load extra code into sqlite after finishing up its own initialization. If you point SQLITE_EXTRA_INIT at a function that calls sqlite3_auto_extension with a pointer to your extension's init function, it will load right after sqlite loads itself and the functions defined within will be available to sqlite.

The steps

To make the idea happen, this code:

Finally, I made an HTML page that makes the most basic possible use of the sqlite binary to make sure that the stats extension has been loaded.

Build it yourself

make clean wasm

To visit the web page that demonstrates how to use the file, serve the dist directory with a web server that serves proper mime types. I like to use devd, so I run devd -ol dist

SQLite fiddle with extension

I used the knowledge I gained in this effort to a SQLite fiddle with an embedded extension. Instructions for building the fiddle can be found in the build-fiddle branch.