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 / opfs-common-inline.c-pp.js
| 1 | //#if 0 |
| 2 | /** |
| 3 | This file is for preprocessor #include into the "opfs" and |
| 4 | "opfs-wl" impls, as well as their async-proxy part. It must be |
| 5 | inlined in those files, as opposed to being a shared copy in the |
| 6 | library, because (A) the async proxy does not load the library and |
| 7 | (B) it references an object which is local to each of those files |
| 8 | but which has a 99% identical structure for each. |
| 9 | */ |
| 10 | //#/if |
| 11 | //#define vfs.metrics.enable 0 /* a refactoring crutch */ |
| 12 | const initS11n = function(){ |
| 13 | /** |
| 14 | This proxy de/serializes cross-thread function arguments and |
| 15 | output-pointer values via the state.sabIO SharedArrayBuffer, |
| 16 | using the region defined by (state.sabS11nOffset, |
| 17 | state.sabS11nOffset + state.sabS11nSize]. Only one dataset is |
| 18 | recorded at a time. |
| 19 | |
| 20 | This is not a general-purpose format. It only supports the |
| 21 | range of operations, and data sizes, needed by the |
| 22 | sqlite3_vfs and sqlite3_io_methods operations. Serialized |
| 23 | data are transient and this serialization algorithm may |
| 24 | change at any time. |
| 25 | |
| 26 | The data format can be succinctly summarized as: |
| 27 | |
| 28 | Nt...Td...D |
| 29 | |
| 30 | Where: |
| 31 | |
| 32 | - N = number of entries (1 byte) |
| 33 | |
| 34 | - t = type ID of first argument (1 byte) |
| 35 | |
| 36 | - ...T = type IDs of the 2nd and subsequent arguments (1 byte |
| 37 | each). |
| 38 | |
| 39 | - d = raw bytes of first argument (per-type size). |
| 40 | |
| 41 | - ...D = raw bytes of the 2nd and subsequent arguments (per-type |
| 42 | size). |
| 43 | |
| 44 | All types except strings have fixed sizes. Strings are stored |
| 45 | using their TextEncoder/TextDecoder representations. It would |
| 46 | arguably make more sense to store them as Int16Arrays of |
| 47 | their JS character values, but how best/fastest to get that |
| 48 | in and out of string form is an open point. Initial |
| 49 | experimentation with that approach did not gain us any speed. |
| 50 | |
| 51 | Historical note: this impl was initially about 1% this size by |
| 52 | using using JSON.stringify/parse(), but using fit-to-purpose |
| 53 | serialization saves considerable runtime. |
| 54 | */ |
| 55 | if(state.s11n) return state.s11n; |
| 56 | const textDecoder = new TextDecoder(), |
| 57 | textEncoder = new TextEncoder(), |
| 58 | viewU8 = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize), |
| 59 | viewDV = new DataView(state.sabIO, state.sabS11nOffset, state.sabS11nSize); |
| 60 | state.s11n = Object.create(null); |
| 61 | /* Only arguments and return values of these types may be |
| 62 | serialized. This covers the whole range of types needed by the |
| 63 | sqlite3_vfs API. */ |
| 64 | const TypeIds = Object.create(null); |
| 65 | TypeIds.number = { id: 1, size: 8, getter: 'getFloat64', setter: 'setFloat64' }; |
| 66 | TypeIds.bigint = { id: 2, size: 8, getter: 'getBigInt64', setter: 'setBigInt64' }; |
| 67 | TypeIds.boolean = { id: 3, size: 4, getter: 'getInt32', setter: 'setInt32' }; |
| 68 | TypeIds.string = { id: 4 }; |
| 69 | |
| 70 | const getTypeId = (v)=>( |
| 71 | TypeIds[typeof v] |
| 72 | || toss("Maintenance required: this value type cannot be serialized.",v) |
| 73 | ); |
| 74 | const getTypeIdById = (tid)=>{ |
| 75 | switch(tid){ |
| 76 | case TypeIds.number.id: return TypeIds.number; |
| 77 | case TypeIds.bigint.id: return TypeIds.bigint; |
| 78 | case TypeIds.boolean.id: return TypeIds.boolean; |
| 79 | case TypeIds.string.id: return TypeIds.string; |
| 80 | default: toss("Invalid type ID:",tid); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | Returns an array of the deserialized state stored by the most |
| 86 | recent serialize() operation (from this thread or the |
| 87 | counterpart thread), or null if the serialization buffer is |
| 88 | empty. If passed a truthy argument, the serialization buffer |
| 89 | is cleared after deserialization. |
| 90 | */ |
| 91 | state.s11n.deserialize = function(clear=false){ |
| 92 | //#if vfs.metrics.enable |
| 93 | ++metrics.s11n.deserialize.count; |
| 94 | //#/if |
| 95 | const t = performance.now(); |
| 96 | const argc = viewU8[0]; |
| 97 | const rc = argc ? [] : null; |
| 98 | if(argc){ |
| 99 | const typeIds = []; |
| 100 | let offset = 1, i, n, v; |
| 101 | for(i = 0; i < argc; ++i, ++offset){ |
| 102 | typeIds.push(getTypeIdById(viewU8[offset])); |
| 103 | } |
| 104 | for(i = 0; i < argc; ++i){ |
| 105 | const t = typeIds[i]; |
| 106 | if(t.getter){ |
| 107 | v = viewDV[t.getter](offset, state.littleEndian); |
| 108 | offset += t.size; |
| 109 | }else{/*String*/ |
| 110 | n = viewDV.getInt32(offset, state.littleEndian); |
| 111 | offset += 4; |
| 112 | v = textDecoder.decode(viewU8.slice(offset, offset+n)); |
| 113 | offset += n; |
| 114 | } |
| 115 | rc.push(v); |
| 116 | } |
| 117 | } |
| 118 | if(clear) viewU8[0] = 0; |
| 119 | //log("deserialize:",argc, rc); |
| 120 | //#if vfs.metrics.enable |
| 121 | metrics.s11n.deserialize.time += performance.now() - t; |
| 122 | //#/if |
| 123 | return rc; |
| 124 | }; |
| 125 | |
| 126 | /** |
| 127 | Serializes all arguments to the shared buffer for consumption |
| 128 | by the counterpart thread. |
| 129 | |
| 130 | This routine is only intended for serializing OPFS VFS |
| 131 | arguments and (in at least one special case) result values, |
| 132 | and the buffer is sized to be able to comfortably handle |
| 133 | those. |
| 134 | |
| 135 | If passed no arguments then it zeroes out the serialization |
| 136 | state. |
| 137 | */ |
| 138 | state.s11n.serialize = function(...args){ |
| 139 | const t = performance.now(); |
| 140 | //#if vfs.metrics.enable |
| 141 | ++metrics.s11n.serialize.count; |
| 142 | //#/if |
| 143 | if(args.length){ |
| 144 | //log("serialize():",args); |
| 145 | const typeIds = []; |
| 146 | let i = 0, offset = 1; |
| 147 | viewU8[0] = args.length & 0xff /* header = # of args */; |
| 148 | for(; i < args.length; ++i, ++offset){ |
| 149 | /* Write the TypeIds.id value into the next args.length |
| 150 | bytes. */ |
| 151 | typeIds.push(getTypeId(args[i])); |
| 152 | viewU8[offset] = typeIds[i].id; |
| 153 | } |
| 154 | for(i = 0; i < args.length; ++i) { |
| 155 | /* Deserialize the following bytes based on their |
| 156 | corresponding TypeIds.id from the header. */ |
| 157 | const t = typeIds[i]; |
| 158 | if(t.setter){ |
| 159 | viewDV[t.setter](offset, args[i], state.littleEndian); |
| 160 | offset += t.size; |
| 161 | }else{/*String*/ |
| 162 | const s = textEncoder.encode(args[i]); |
| 163 | viewDV.setInt32(offset, s.byteLength, state.littleEndian); |
| 164 | offset += 4; |
| 165 | viewU8.set(s, offset); |
| 166 | offset += s.byteLength; |
| 167 | } |
| 168 | } |
| 169 | //log("serialize() result:",viewU8.slice(0,offset)); |
| 170 | }else{ |
| 171 | viewU8[0] = 0; |
| 172 | } |
| 173 | //#if vfs.metrics.enable |
| 174 | metrics.s11n.serialize.time += performance.now() - t; |
| 175 | //#/if |
| 176 | }; |
| 177 | |
| 178 | //#if defined opfs-async-proxy |
| 179 | state.s11n.storeException = state.asyncS11nExceptions |
| 180 | ? ((priority,e)=>{ |
| 181 | if(priority<=state.asyncS11nExceptions){ |
| 182 | state.s11n.serialize([e.name,': ',e.message].join("")); |
| 183 | } |
| 184 | }) |
| 185 | : ()=>{}; |
| 186 | //#/if |
| 187 | |
| 188 | return state.s11n; |
| 189 | //#undef vfs.metrics.enable |
| 190 | }/*initS11n()*/; |