sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / wasm / api / sqlite3-vfs-helper.c-pp.js
| 1 | /* |
| 2 | ** 2022-11-30 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of a |
| 5 | ** legal notice, here is a blessing: |
| 6 | ** |
| 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. |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | This file installs sqlite3.vfs, a namespace of helpers for use in |
| 14 | the creation of JavaScript implementations of sqlite3_vfs. |
| 15 | */ |
| 16 | 'use strict'; |
| 17 | sqlite3ApiBootstrap.initializers.push(function(sqlite3){ |
| 18 | const wasm = sqlite3.wasm, capi = sqlite3.capi, toss = sqlite3.util.toss3; |
| 19 | const vfs = Object.create(null); |
| 20 | sqlite3.vfs = vfs; |
| 21 | |
| 22 | /** |
| 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. |
| 27 | |
| 28 | On success, returns this object. Throws on error. |
| 29 | */ |
| 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."); |
| 33 | } |
| 34 | const rc = capi.sqlite3_vfs_register(this, asDefault ? 1 : 0); |
| 35 | if(rc){ |
| 36 | toss("sqlite3_vfs_register(",this,") failed with rc",rc); |
| 37 | } |
| 38 | if(this.pointer !== capi.sqlite3_vfs_find(this.$zName)){ |
| 39 | toss("BUG: sqlite3_vfs_find(vfs.$zName) failed for just-installed VFS", |
| 40 | this); |
| 41 | } |
| 42 | return this; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | A wrapper for |
| 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. |
| 50 | |
| 51 | Accepts an object which contains the properties "io" and/or |
| 52 | "vfs", each of which is itself an object with following properties: |
| 53 | |
| 54 | - `struct`: an sqlite3.StructBinder.StructType-type struct. This |
| 55 | must be a populated object (except for the methods) of type |
| 56 | sqlite3_io_methods (for the "io" entry) or sqlite3_vfs (for the |
| 57 | "vfs" entry). |
| 58 | |
| 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 |
| 62 | counterparts. That is: they must accept and return WASM data |
| 63 | types. |
| 64 | |
| 65 | For each of those objects, this function passes its (`struct`, |
| 66 | `methods`, (optional) `applyArgcCheck`) properties to |
| 67 | installMethods(). |
| 68 | |
| 69 | If the `vfs` entry is set then: |
| 70 | |
| 71 | - Its `struct` property's registerVfs() is called. The |
| 72 | `vfs` entry may optionally have an `asDefault` property, which |
| 73 | gets passed as the argument to registerVfs(). |
| 74 | |
| 75 | - If `struct.$zName` is falsy and the entry has a string-type |
| 76 | `name` property, `struct.$zName` is set to the C-string form of |
| 77 | that `name` value before registerVfs() is called. That string |
| 78 | gets added to the on-dispose state of the struct. |
| 79 | |
| 80 | On success returns this object. Throws on error. |
| 81 | */ |
| 82 | vfs.installVfs = function(opt){ |
| 83 | let count = 0; |
| 84 | const propList = ['io','vfs']; |
| 85 | for(const key of propList){ |
| 86 | const o = opt[key]; |
| 87 | if(o){ |
| 88 | ++count; |
| 89 | o.struct.installMethods(o.methods, !!o.applyArgcCheck); |
| 90 | if('vfs'===key){ |
| 91 | if(!o.struct.$zName && 'string'===typeof o.name){ |
| 92 | o.struct.addOnDispose( |
| 93 | o.struct.$zName = wasm.allocCString(o.name) |
| 94 | ); |
| 95 | } |
| 96 | o.struct.registerVfs(!!o.asDefault); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | if(!count) toss("Misuse: installVfs() options object requires at least", |
| 101 | "one of:", propList); |
| 102 | return this; |
| 103 | }; |
| 104 | }/*sqlite3ApiBootstrap.initializers.push()*/); |