4 ** The author disclaims copyright to this source code. In place of a
5 ** legal notice, here is a blessing:
7 ** * May you do good and not evil.
8 ** * May you find forgiveness for yourself and forgive others.
9 ** * May you share freely, never taking more than you give.
13 This file installs sqlite3.vfs, a namespace of helpers for use in
14 the creation of JavaScript implementations of sqlite3_vfs.
17 globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
18 const wasm = sqlite3.wasm, capi = sqlite3.capi, toss = sqlite3.util.toss3;
19 const vfs = Object.create(null);
23 Uses sqlite3_vfs_register() to register this
24 sqlite3.capi.sqlite3_vfs instance. This object must have already
25 been filled out properly. If the first argument is truthy, the
26 VFS is registered as the default VFS, else it is not.
28 On success, returns this object. Throws on error.
30 capi.sqlite3_vfs.prototype.registerVfs = function(asDefault=false){
31 if(!(this instanceof sqlite3.capi.sqlite3_vfs)){
32 toss("Expecting a sqlite3_vfs-type argument.");
34 const rc = capi.sqlite3_vfs_register(this, asDefault ? 1 : 0);
36 toss("sqlite3_vfs_register(",this,") failed with rc",rc);
38 if(this.pointer !== capi.sqlite3_vfs_find(this.$zName)){
39 toss("BUG: sqlite3_vfs_find(vfs.$zName) failed for just-installed VFS",
47 sqlite3.StructBinder.StructType.prototype.installMethods() or
48 registerVfs() to reduce installation of a VFS and/or its I/O
49 methods to a single call.
51 Accepts an object which contains the properties "io" and/or
52 "vfs", each of which is itself an object with following properties:
54 - `struct`: an sqlite3.StructBinder.StructType-type struct. This
55 must be a populated (except for the methods) object of type
56 sqlite3_io_methods (for the "io" entry) or sqlite3_vfs (for the
59 - `methods`: an object mapping sqlite3_io_methods method names
60 (e.g. 'xClose') to JS implementations of those methods. The JS
61 implementations must be call-compatible with their native
64 For each of those object, this function passes its (`struct`,
65 `methods`, (optional) `applyArgcCheck`) properties to
68 If the `vfs` entry is set then:
70 - Its `struct` property's registerVfs() is called. The
71 `vfs` entry may optionally have an `asDefault` property, which
72 gets passed as the argument to registerVfs().
74 - If `struct.$zName` is falsy and the entry has a string-type
75 `name` property, `struct.$zName` is set to the C-string form of
76 that `name` value before registerVfs() is called. That string
77 gets added to the on-dispose state of the struct.
79 On success returns this object. Throws on error.
81 vfs.installVfs = function(opt){
83 const propList = ['io','vfs'];
84 for(const key of propList){
88 o.struct.installMethods(o.methods, !!o.applyArgcCheck);
90 if(!o.struct.$zName && 'string'===typeof o.name){
91 o.struct.addOnDispose(
92 o.struct.$zName = wasm.allocCString(o.name)
95 o.struct.registerVfs(!!o.asDefault);
99 if(!count) toss("Misuse: installVfs() options object requires at least",
100 "one of:", propList);
103 }/*sqlite3ApiBootstrap.initializers.push()*/);