saghul / txiki.js

A tiny JavaScript runtime
MIT License
2.39k stars 160 forks source link

sqlite extensions #566

Open KaruroChori opened 1 week ago

KaruroChori commented 1 week ago

Sqlite has a native mechanism to handle extensions to be added at runtime. They can be either added via sqlite3_load_extension or calling the sql function load_extension(X,Y).
In both cases this functionality must be enabled first:

I think it would be nice to have support for sqlite native extensions in txiki.

saghul commented 1 week ago

How does that work with regard to linking to the right sqlite library?

KaruroChori commented 1 week ago

It is my understanding that, while extensions can be statically linked if so desired, the general approach is for them to be dynamically linked at runtime by sqlite3_load_extension or the equivalent sql function. This will be slighly different for each operating system, but on linux it is likely a wrapper of dlopen and a callback to the init function each extension must provide.

Distributing the extensions and making sure they are compatible with the sqlite in txiki is a concern for who develops the final application, not of txiki. ~As we already provide the sqlite version in tjs.versions()~ (now we do in #570) and any relevant platform information, there is nothing more that the runtime is expected to do in my opinion (other than exposing those functions to load them).

saghul commented 6 days ago

What I mean is that txiki statically links sqlite with certain build flags.

What version are the ones building the extension going to link against?

Is there a chance for incompatibilities or is ABI stable enough?

Sorry for the many questions, it's the first time I tackle this 😅

KaruroChori commented 6 days ago

For most scenarios, I would expect extensions compiled for the same architecture to work, regardless of the flags set by any specific sqlite distribution. Obviously, this will not be the case if specific features have been disabled and they are explicitly needed by any extension. But I do not expect it to be a problem related to incompatible ABIs for most configurations. I might be wrong on this.

That being said, as I expressed before, this is not a concern of txiki, but of whoever is shipping the final application. As long as the flags to build sqlite are reported in txiki's sourcecode (and possibly documented), the distribution & validation of those extensions is beyond our control.

KaruroChori commented 6 days ago

~Basically: https://github.com/cloudmeter/sqlite/blob/master/sqlite3ext.h should be stable enough. Last change was done 12 year ago and is not affected by flags (where it matters).~ LOL github was giving that random branch as the first result. The "good one": https://github.com/sqlite/sqlite/blob/master/src/sqlite3ext.h But the substance is the same, fields are incrementally added to that struct and this ensures a long lasting stability of the ABI.

saghul commented 6 days ago

Cool, I can give this a try!