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-wasm.c
| 1 | /* |
| 2 | ** This file requires access to sqlite3.c static state in order to |
| 3 | ** implement certain WASM-specific features, and thus directly |
| 4 | ** includes that file. Unlike the rest of sqlite3.c, this file |
| 5 | ** requires compiling with -std=c99 (or equivalent, or a later C |
| 6 | ** version) because it makes use of features not available in C89. |
| 7 | ** |
| 8 | ** At its simplest, to build sqlite3.wasm either place this file |
| 9 | ** in the same directory as sqlite3.c/h before compilation or use the |
| 10 | ** -I/path flag to tell the compiler where to find both of those |
| 11 | ** files, then compile this file. For example: |
| 12 | ** |
| 13 | ** emcc -o sqlite3.wasm ... -I/path/to/sqlite3-c-and-h sqlite3-wasm.c |
| 14 | */ |
| 15 | #define SQLITE_WASM |
| 16 | #ifdef SQLITE_WASM_ENABLE_C_TESTS |
| 17 | # undef SQLITE_WASM_ENABLE_C_TESTS |
| 18 | # define SQLITE_WASM_ENABLE_C_TESTS 1 |
| 19 | /* |
| 20 | ** Code blocked off by SQLITE_WASM_ENABLE_C_TESTS is intended solely |
| 21 | ** for use in unit/regression testing. They may be safely omitted from |
| 22 | ** client-side builds. The main unit test script, tester1.js, will |
| 23 | ** skip related tests if it doesn't find the corresponding functions |
| 24 | ** in the WASM exports. |
| 25 | */ |
| 26 | #else |
| 27 | # define SQLITE_WASM_ENABLE_C_TESTS 0 |
| 28 | #endif |
| 29 | |
| 30 | /* |
| 31 | ** Threading and file locking: JS is single-threaded. Each Worker |
| 32 | ** thread is a separate instance of the JS engine so can never access |
| 33 | ** the same db handle as another thread, thus multi-threading support |
| 34 | ** is unnecessary in the library. Because the filesystems are virtual |
| 35 | ** and local to a given wasm runtime instance, two Workers can never |
| 36 | ** access the same db file at once, with the exception of OPFS. |
| 37 | ** |
| 38 | ** Summary: except for the case of OPFS, which supports locking using |
| 39 | ** its own API, threading and file locking support are unnecessary in |
| 40 | ** the wasm build. |
| 41 | */ |
| 42 | |
| 43 | /* |
| 44 | ** Undefine any SQLITE_... config flags which we specifically do not |
| 45 | ** want defined. Please keep these alphabetized. |
| 46 | */ |
| 47 | #undef SQLITE_OMIT_DESERIALIZE |
| 48 | #undef SQLITE_OMIT_MEMORYDB |
| 49 | |
| 50 | /* |
| 51 | ** Define any SQLITE_... config defaults we want if they aren't |
| 52 | ** overridden by the builder. Please keep these alphabetized. |
| 53 | */ |
| 54 | |
| 55 | /**********************************************************************/ |
| 56 | /* SQLITE_D... */ |
| 57 | #ifndef SQLITE_DEFAULT_CACHE_SIZE |
| 58 | /* |
| 59 | ** The OPFS impls benefit tremendously from an increased cache size |
| 60 | ** when working on large workloads, e.g. speedtest1 --size 50 or |
| 61 | ** higher. On smaller workloads, e.g. speedtest1 --size 25, they |
| 62 | ** clearly benefit from having 4mb of cache, but not as much as a |
| 63 | ** larger cache benefits the larger workloads. Speed differences |
| 64 | ** between 2x and nearly 3x have been measured with ample page cache. |
| 65 | */ |
| 66 | # define SQLITE_DEFAULT_CACHE_SIZE -16384 |
| 67 | #endif |
| 68 | #if !defined(SQLITE_DEFAULT_PAGE_SIZE) |
| 69 | /* |
| 70 | ** OPFS performance is improved by approx. 12% with a page size of 8kb |
| 71 | ** instead of 4kb. Performance with 16kb is equivalent to 8kb. |
| 72 | ** |
| 73 | ** Performance difference of kvvfs with a page size of 8kb compared to |
| 74 | ** 4kb, as measured by speedtest1 --size 4, is indeterminate: |
| 75 | ** measurements are all over the place either way and not |
| 76 | ** significantly different. |
| 77 | */ |
| 78 | # define SQLITE_DEFAULT_PAGE_SIZE 8192 |
| 79 | #endif |
| 80 | #ifndef SQLITE_DEFAULT_UNIX_VFS |
| 81 | # define SQLITE_DEFAULT_UNIX_VFS "unix-none" |
| 82 | #endif |
| 83 | #undef SQLITE_DQS |
| 84 | #define SQLITE_DQS 0 |
| 85 | |
| 86 | /**********************************************************************/ |
| 87 | /* SQLITE_ENABLE_... */ |
| 88 | /* |
| 89 | ** Unconditionally enable API_ARMOR in the WASM build. It ensures that |
| 90 | ** public APIs behave predictable in the face of passing illegal NULLs |
| 91 | ** or ranges which might otherwise invoke undefined behavior. |
| 92 | */ |
| 93 | #undef SQLITE_ENABLE_API_ARMOR |
| 94 | #define SQLITE_ENABLE_API_ARMOR 1 |
| 95 | |
| 96 | /**********************************************************************/ |
| 97 | /* SQLITE_EXPERIMENTAL_PRAGMA_20251114 */ |
| 98 | /* |
| 99 | ** See: |
| 100 | ** https://sqlite.org/src/info/e2b3f1a9480a9be3 |
| 101 | ** https://github.com/rhashimoto/wa-sqlite/discussions/301 |
| 102 | ** |
| 103 | ** It is enabled here for the sake of VFS experimentors. |
| 104 | */ |
| 105 | #undef SQLITE_EXPERIMENTAL_PRAGMA_20251114 |
| 106 | #define SQLITE_EXPERIMENTAL_PRAGMA_20251114 |
| 107 | |
| 108 | /**********************************************************************/ |
| 109 | /* SQLITE_O... */ |
| 110 | #undef SQLITE_OMIT_DEPRECATED |
| 111 | #define SQLITE_OMIT_DEPRECATED 1 |
| 112 | #undef SQLITE_OMIT_LOAD_EXTENSION |
| 113 | #define SQLITE_OMIT_LOAD_EXTENSION 1 |
| 114 | #undef SQLITE_OMIT_SHARED_CACHE |
| 115 | #define SQLITE_OMIT_SHARED_CACHE 1 |
| 116 | #undef SQLITE_OMIT_UTF16 |
| 117 | #define SQLITE_OMIT_UTF16 1 |
| 118 | #undef SQLITE_OS_KV_OPTIONAL |
| 119 | #define SQLITE_OS_KV_OPTIONAL 1 |
| 120 | |
| 121 | /**********************************************************************/ |
| 122 | /* SQLITE_S... */ |
| 123 | #ifndef SQLITE_STRICT_SUBTYPE |
| 124 | # define SQLITE_STRICT_SUBTYPE 1 |
| 125 | #endif |
| 126 | |
| 127 | /**********************************************************************/ |
| 128 | /* SQLITE_T... */ |
| 129 | #ifndef SQLITE_TEMP_STORE |
| 130 | # define SQLITE_TEMP_STORE 2 |
| 131 | #endif |
| 132 | #ifndef SQLITE_THREADSAFE |
| 133 | # define SQLITE_THREADSAFE 0 |
| 134 | #endif |
| 135 | |
| 136 | /**********************************************************************/ |
| 137 | /* SQLITE_USE_... */ |
| 138 | #ifndef SQLITE_USE_URI |
| 139 | # define SQLITE_USE_URI 1 |
| 140 | #endif |
| 141 | |
| 142 | #ifdef SQLITE_WASM_EXTRA_INIT |
| 143 | /* SQLITE_EXTRA_INIT vs SQLITE_EXTRA_INIT_MUTEXED: |
| 144 | ** see https://sqlite.org/forum/forumpost/14183b98fc0b1dea */ |
| 145 | # define SQLITE_EXTRA_INIT_MUTEXED sqlite3_wasm_extra_init |
| 146 | #endif |
| 147 | |
| 148 | /* |
| 149 | ** If SQLITE_WASM_BARE_BONES is defined, undefine most of the ENABLE |
| 150 | ** macros. This will, when using the canonical makefile, also elide |
| 151 | ** any associated C functions from the WASM exports: see |
| 152 | ** ./EXPORTED_FUNCTIONS.c-pp. |
| 153 | */ |
| 154 | #ifdef SQLITE_WASM_BARE_BONES |
| 155 | # undef SQLITE_ENABLE_COLUMN_METADATA |
| 156 | # undef SQLITE_ENABLE_DBPAGE_VTAB |
| 157 | # undef SQLITE_ENABLE_DBSTAT_VTAB |
| 158 | # undef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 159 | # undef SQLITE_ENABLE_FTS5 |
| 160 | # undef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 161 | # undef SQLITE_ENABLE_PERCENTILE |
| 162 | # undef SQLITE_ENABLE_PREUPDATE_HOOK |
| 163 | # undef SQLITE_ENABLE_RTREE |
| 164 | # undef SQLITE_ENABLE_SESSION |
| 165 | # undef SQLITE_ENABLE_STMTVTAB |
| 166 | # undef SQLITE_OMIT_AUTHORIZATION |
| 167 | # define SQLITE_OMIT_AUTHORIZATION |
| 168 | # undef SQLITE_OMIT_GET_TABLE |
| 169 | # define SQLITE_OMIT_GET_TABLE |
| 170 | # undef SQLITE_OMIT_INCRBLOB |
| 171 | # define SQLITE_OMIT_INCRBLOB |
| 172 | # undef SQLITE_OMIT_INTROSPECTION_PRAGMAS |
| 173 | # define SQLITE_OMIT_INTROSPECTION_PRAGMAS |
| 174 | # undef SQLITE_OMIT_JSON |
| 175 | # define SQLITE_OMIT_JSON |
| 176 | # undef SQLITE_OMIT_PROGRESS_CALLBACK |
| 177 | # define SQLITE_OMIT_PROGRESS_CALLBACK |
| 178 | # undef SQLITE_OMIT_WAL |
| 179 | # define SQLITE_OMIT_WAL |
| 180 | /* |
| 181 | The following OMITs do not work with the standard amalgamation, so |
| 182 | require a custom build: |
| 183 | |
| 184 | fossil clean -x |
| 185 | ./configure |
| 186 | OPTS='...' make -e sqlite3 |
| 187 | |
| 188 | where ... has a -D... for each of the following OMIT flags: |
| 189 | |
| 190 | # undef SQLITE_OMIT_EXPLAIN |
| 191 | # define SQLITE_OMIT_EXPLAIN |
| 192 | |
| 193 | # undef SQLITE_OMIT_TRIGGER |
| 194 | # define SQLITE_OMIT_TRIGGER |
| 195 | |
| 196 | # undef SQLITE_OMIT_VIRTUALTABLE |
| 197 | # define SQLITE_OMIT_VIRTUALTABLE |
| 198 | |
| 199 | # undef SQLITE_OMIT_WINDOWFUNC |
| 200 | # define SQLITE_OMIT_WINDOWFUNC |
| 201 | |
| 202 | As of this writing (2024-07-25), such a build fails in various ways |
| 203 | for as-yet-unknown reasons. |
| 204 | */ |
| 205 | #endif |
| 206 | |
| 207 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_WASM_BARE_BONES) |
| 208 | # define SQLITE_WASM_HAS_VTAB 1 |
| 209 | #else |
| 210 | # define SQLITE_WASM_HAS_VTAB 0 |
| 211 | #endif |
| 212 | |
| 213 | #include <assert.h> |
| 214 | |
| 215 | /* |
| 216 | ** SQLITE_WASM_EXPORT is functionally identical to EMSCRIPTEN_KEEPALIVE |
| 217 | ** but is not Emscripten-specific. It explicitly marks functions for |
| 218 | ** export into the target wasm file without requiring explicit listing |
| 219 | ** of those functions in Emscripten's -sEXPORTED_FUNCTIONS=... list |
| 220 | ** (or equivalent in other build platforms). Any function with neither |
| 221 | ** this attribute nor which is listed as an explicit export will not |
| 222 | ** be exported from the wasm file (but may still be used internally |
| 223 | ** within the wasm file). |
| 224 | ** |
| 225 | ** The functions in this file (sqlite3-wasm.c) which require exporting |
| 226 | ** are marked with this flag. They may also be added to any explicit |
| 227 | ** build-time export list but need not be. All of these APIs are |
| 228 | ** intended for use only within the project's own JS/WASM code, and |
| 229 | ** not by client code, so an argument can be made for reducing their |
| 230 | ** visibility by not including them in any build-time export lists. |
| 231 | ** |
| 232 | ** 2025-12-01: for use in non-Emscripten builds, we need a more |
| 233 | ** invasive macro which explicitly names the export: |
| 234 | ** SQLITE_WASM_EXPORT2. |
| 235 | */ |
| 236 | #define SQLITE_WASM_EXPORT __attribute__((used,visibility("default"))) |
| 237 | #define SQLITE_WASM_EXPORT_NAMED(X) __attribute__((export_name(#X),used,visibility("default"))) |
| 238 | #define SQLITE_WASM_EXPORT2(RETTYPE,NAME,SIG) SQLITE_WASM_EXPORT_NAMED(NAME) RETTYPE NAME SIG |
| 239 | |
| 240 | #if 1 |
| 241 | /** Increase the kvvfs key size limit from 32. */ |
| 242 | #define KVRECORD_KEY_SZ 128 |
| 243 | #endif |
| 244 | |
| 245 | /* |
| 246 | ** A level of indirection for SQLITE_DISPOSAL_NOTIFIER (described |
| 247 | ** below) so that we can redirect it from JS once the module is |
| 248 | ** loaded. |
| 249 | */ |
| 250 | typedef void (*sqlite3__wasm_disposal_notifier_f)(int typeId, void const *p); |
| 251 | |
| 252 | /* |
| 253 | ** Global disposal notifier callback pointer. JS overwrites this. |
| 254 | */ |
| 255 | static sqlite3__wasm_disposal_notifier_f sqlite3WasmDisposalNotifier = 0; |
| 256 | |
| 257 | /* |
| 258 | ** Called by the JS layer to get the pointer to |
| 259 | ** sqlite3WasmDisposalNotifier. |
| 260 | */ |
| 261 | SQLITE_WASM_EXPORT2(sqlite3__wasm_disposal_notifier_f *, |
| 262 | sqlite3__wasm_disposal_notifier, (void)){ |
| 263 | return &sqlite3WasmDisposalNotifier; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | ** If SQLITE_DISPOSAL_NOTIFIER is defined, the core invokes it as: |
| 268 | ** |
| 269 | ** SQLITE_DISPOSAL_NOTIFIER(T,P); |
| 270 | ** |
| 271 | ** where T is a literal integer and P is the ptr being disposed of |
| 272 | ** (possibly in the form of &X). |
| 273 | ** |
| 274 | ** T specifies the type of P: 1==sqlite3*, 2==sqlite3_stmt* |
| 275 | ** |
| 276 | ** Because this happens during finalization, code must be careful |
| 277 | ** about how it uses P. e.g. a finalizing database must not be |
| 278 | ** close()d again or results are undefined. A finalizing statement |
| 279 | ** must not be step()d or finalize()d. etc. |
| 280 | ** |
| 281 | ** The JS side of this implementation plugs in a hook which cleans up |
| 282 | ** any JS-side state mapped to that pointer. e.g. disconnecting a |
| 283 | ** pointer which a sqlite3.oo1.DB instance uses will not only unmap |
| 284 | ** that oo1.DB object, but also other oo1.DB instances which share |
| 285 | ** that same native pointer. Before this capability, closing of |
| 286 | ** DB-class-hosted (sqlite3*) using sqlite3_close_v2() would leave the |
| 287 | ** DB instance pointing to a stale pointer. Ditto for oo1.DB.Stmt. |
| 288 | */ |
| 289 | #define SQLITE_DISPOSAL_NOTIFIER(T,P) \ |
| 290 | if( (P) && sqlite3WasmDisposalNotifier )\ |
| 291 | sqlite3WasmDisposalNotifier(T, P) |
| 292 | |
| 293 | /* |
| 294 | ** Which sqlite3.c we're using needs to be configurable to enable |
| 295 | ** building against a custom copy, e.g. the SEE variant. We #include |
| 296 | ** the .c file, rather than the header, so that the WASM extensions |
| 297 | ** have access to private API internals (namely for kvvfs, SQLTester |
| 298 | ** pieces, and SQLITE_DISPOSAL_NOTIFIER). Though kvvfs and the testing |
| 299 | ** pieces are optional, SQLITE_DISPOSAL_NOTIFIER is required for |
| 300 | ** JS-side memory-lifetime guarantees made as of 3.55.0 (and module |
| 301 | ** won't bootstrap if that's not active). |
| 302 | ** |
| 303 | ** The caveat here is that custom variants need to account for |
| 304 | ** exporting any necessary symbols (e.g. sqlite3_activate_see()). We |
| 305 | ** cannot export them from here using SQLITE_WASM_EXPORT because that |
| 306 | ** attribute (apparently) has to be part of the function definition. |
| 307 | ** The canonical build process specifically supports SEE, exporting |
| 308 | ** its additional functions when building against an SEE copy of |
| 309 | ** sqlite3.c. |
| 310 | */ |
| 311 | #ifndef SQLITE_C |
| 312 | # define SQLITE_C sqlite3.c /* yes, .c instead of .h. */ |
| 313 | #endif |
| 314 | #define INC__STRINGIFY_(f) #f |
| 315 | #define INC__STRINGIFY(f) INC__STRINGIFY_(f) |
| 316 | #include INC__STRINGIFY(SQLITE_C) |
| 317 | #undef INC__STRINGIFY_ |
| 318 | #undef INC__STRINGIFY |
| 319 | #undef SQLITE_C |
| 320 | |
| 321 | #if SQLITE_VERSION_NUMBER < 3055000 |
| 322 | # error "This wasm build requires sqlite3.c internals added in 3.55.0" |
| 323 | #endif |
| 324 | |
| 325 | /* |
| 326 | ** State for the "pseudo-stack" allocator implemented in |
| 327 | ** sqlite3__wasm_pstack_xyz(). In order to avoid colliding with |
| 328 | ** Emscripten-controlled stack space, it carves out a bit of stack |
| 329 | ** memory to use for that purpose. This memory ends up in the |
| 330 | ** WASM-managed memory, such that routines which manipulate the wasm |
| 331 | ** heap can also be used to manipulate this memory. |
| 332 | ** |
| 333 | ** This particular allocator is intended for small allocations such as |
| 334 | ** storage for output pointers. We cannot reasonably size it large |
| 335 | ** enough for general-purpose string conversions because some of our |
| 336 | ** tests use input files (strings) of 16MB+. |
| 337 | */ |
| 338 | static unsigned char PStack_mem[ |
| 339 | 1024 * 4 /* API docs guaranty at least 2kb and it's been set at 4kb |
| 340 | since it was introduced. */ |
| 341 | ] = {0}; |
| 342 | static struct { |
| 343 | unsigned const char * const pBegin;/* Start (inclusive) of memory */ |
| 344 | unsigned const char * const pEnd; /* One-after-the-end of memory */ |
| 345 | unsigned char * pPos; /* Current stack pointer */ |
| 346 | } PStack = { |
| 347 | &PStack_mem[0], |
| 348 | &PStack_mem[0] + sizeof(PStack_mem), |
| 349 | &PStack_mem[0] + sizeof(PStack_mem) |
| 350 | }; |
| 351 | /* |
| 352 | ** Returns the current pstack position. |
| 353 | */ |
| 354 | SQLITE_WASM_EXPORT void * sqlite3__wasm_pstack_ptr(void){ |
| 355 | return PStack.pPos; |
| 356 | } |
| 357 | /* |
| 358 | ** Sets the pstack position pointer to p. Results are undefined if the |
| 359 | ** given value did not come from sqlite3__wasm_pstack_ptr(). |
| 360 | */ |
| 361 | SQLITE_WASM_EXPORT void sqlite3__wasm_pstack_restore(unsigned char * p){ |
| 362 | assert(p>=PStack.pBegin && p<=PStack.pEnd && p>=PStack.pPos); |
| 363 | assert(0==((unsigned long long)p & 0x7) /* 8-byte aligned */); |
| 364 | if(p>=PStack.pBegin && p<=PStack.pEnd){ |
| 365 | PStack.pPos = p; |
| 366 | } |
| 367 | } |
| 368 | /* |
| 369 | ** Allocate and zero out n bytes from the pstack. Returns a pointer to |
| 370 | ** the memory on success, 0 on error (including a negative n value). n |
| 371 | ** is always adjusted to be a multiple of 8 and returned memory is (A) |
| 372 | ** similarly aligned and (B) always zeroed out before returning |
| 373 | ** (because this keeps the client JS code from having to do so, and |
| 374 | ** most uses of the pstack call for doing so). |
| 375 | */ |
| 376 | SQLITE_WASM_EXPORT2(void *,sqlite3__wasm_pstack_alloc,(int n)){ |
| 377 | if( n<=0 || n>(1024 * 1024/*https://sqlite.org/bugs/forumpost/4dbd096fe3*/) ){ |
| 378 | return 0; |
| 379 | } |
| 380 | n = (n + 7) & ~7 /* align to 8-byte boundary */; |
| 381 | if( PStack.pBegin + n > PStack.pPos /*not enough space left*/ |
| 382 | || PStack.pBegin + n <= PStack.pBegin /*overflow*/ ) return 0; |
| 383 | memset((PStack.pPos = PStack.pPos - n), 0, (unsigned int)n); |
| 384 | return PStack.pPos; |
| 385 | } |
| 386 | /* |
| 387 | ** Return the number of bytes left which can be |
| 388 | ** sqlite3__wasm_pstack_alloc()'d. |
| 389 | */ |
| 390 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_pstack_remaining,(void)){ |
| 391 | assert(PStack.pPos >= PStack.pBegin); |
| 392 | assert(PStack.pPos <= PStack.pEnd); |
| 393 | return (int)(PStack.pPos - PStack.pBegin); |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | ** Return the total number of bytes available in the pstack, including |
| 398 | ** any space which is currently allocated. This value is a |
| 399 | ** compile-time constant. |
| 400 | */ |
| 401 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_pstack_quota,(void)){ |
| 402 | return (int)(PStack.pEnd - PStack.pBegin); |
| 403 | } |
| 404 | |
| 405 | #if SQLITE_WASM_ENABLE_C_TESTS |
| 406 | struct WasmTestStruct { |
| 407 | int v4; |
| 408 | void * ppV; |
| 409 | const char * cstr; |
| 410 | int64_t v8; |
| 411 | void (*xFunc)(void*); |
| 412 | }; |
| 413 | typedef struct WasmTestStruct WasmTestStruct; |
| 414 | SQLITE_WASM_EXPORT2(void,sqlite3__wasm_test_struct,(WasmTestStruct * s)){ |
| 415 | if(s){ |
| 416 | if( 0 ){ |
| 417 | /* Do not be alarmed by the small (and odd) pointer values. |
| 418 | Function pointers in WASM are their index into the |
| 419 | indirect function table, not their address. */ |
| 420 | fprintf(stderr,"%s:%s()@%p s=@%p xFunc=@%p\n", |
| 421 | __FILE__, __func__, |
| 422 | (void*)sqlite3__wasm_test_struct, |
| 423 | s, (void*)s->xFunc); |
| 424 | } |
| 425 | s->v4 *= 2; |
| 426 | s->v8 = s->v4 * 2; |
| 427 | s->ppV = s; |
| 428 | s->cstr = __FILE__; |
| 429 | if(s->xFunc) s->xFunc(s); |
| 430 | } |
| 431 | } |
| 432 | #endif /* SQLITE_WASM_ENABLE_C_TESTS */ |
| 433 | |
| 434 | /* |
| 435 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 436 | ** for use by the sqlite project's own JS/WASM bindings. Unlike the |
| 437 | ** rest of the sqlite3 API, this part requires C99 for snprintf() and |
| 438 | ** variadic macros. |
| 439 | ** |
| 440 | ** Returns a string containing a JSON-format "enum" of C-level |
| 441 | ** constants and struct-related metadata intended to be imported into |
| 442 | ** the JS environment. The JSON is initialized the first time this |
| 443 | ** function is called and that result is reused for all future calls. |
| 444 | ** |
| 445 | ** If this function returns NULL then it means that the internal |
| 446 | ** buffer is not large enough for the generated JSON and needs to be |
| 447 | ** increased. In debug builds that will trigger an assert(). |
| 448 | */ |
| 449 | SQLITE_WASM_EXPORT2(const char *,sqlite3__wasm_enum_json,(void)){ |
| 450 | static char aBuffer[1024 * 24] = |
| 451 | {0} /* where the JSON goes. 2026-10-31: output size=20276, but |
| 452 | that can vary slightly from build to build, so a little |
| 453 | leeway is needed here. */; |
| 454 | int n = 0, nChildren = 0, nStruct = 0 |
| 455 | /* output counters for figuring out where commas go */; |
| 456 | char * zPos = &aBuffer[1] /* skip first byte for now to help protect |
| 457 | ** against a small race condition */; |
| 458 | char const * const zEnd = &aBuffer[0] + sizeof(aBuffer) /* one-past-the-end */; |
| 459 | if(aBuffer[0]) return aBuffer; |
| 460 | /* Leave aBuffer[0] at 0 until the end to help guard against a tiny |
| 461 | ** race condition. If this is called twice concurrently, they might |
| 462 | ** end up both writing to aBuffer, but they'll both write the same |
| 463 | ** thing, so that's okay. If we set byte 0 up front then the 2nd |
| 464 | ** instance might return and use the string before the 1st instance |
| 465 | ** is done filling it. */ |
| 466 | |
| 467 | /* Core output macros... */ |
| 468 | #define lenCheck assert(zPos < zEnd - 128 \ |
| 469 | && "sqlite3__wasm_enum_json() buffer is too small."); \ |
| 470 | if( zPos >= zEnd - 128 ) return 0 |
| 471 | #define outf(format,...) \ |
| 472 | zPos += snprintf(zPos, ((size_t)(zEnd - zPos)), format, __VA_ARGS__); \ |
| 473 | lenCheck |
| 474 | #define out(TXT) outf("%s",TXT) |
| 475 | #define CloseBrace(LEVEL) \ |
| 476 | assert(LEVEL<5); memset(zPos, '}', LEVEL); zPos+=LEVEL; lenCheck |
| 477 | |
| 478 | /* Macros for emitting maps of integer- and string-type macros to |
| 479 | ** their values. */ |
| 480 | #define DefGroup(KEY) n = 0; \ |
| 481 | outf("%s\"" #KEY "\": {",(nChildren++ ? "," : "")); |
| 482 | #define DefInt(KEY) \ |
| 483 | outf("%s\"%s\": %d", (n++ ? ", " : ""), #KEY, (int)KEY) |
| 484 | #define DefStr(KEY) \ |
| 485 | outf("%s\"%s\": \"%s\"", (n++ ? ", " : ""), #KEY, KEY) |
| 486 | #define _DefGroup CloseBrace(1) |
| 487 | |
| 488 | /* The following groups are sorted alphabetic by group name. */ |
| 489 | DefGroup(access){ |
| 490 | DefInt(SQLITE_ACCESS_EXISTS); |
| 491 | DefInt(SQLITE_ACCESS_READWRITE); |
| 492 | DefInt(SQLITE_ACCESS_READ)/*docs say this is unused*/; |
| 493 | } _DefGroup; |
| 494 | |
| 495 | DefGroup(authorizer){ |
| 496 | DefInt(SQLITE_DENY); |
| 497 | DefInt(SQLITE_IGNORE); |
| 498 | DefInt(SQLITE_CREATE_INDEX); |
| 499 | DefInt(SQLITE_CREATE_TABLE); |
| 500 | DefInt(SQLITE_CREATE_TEMP_INDEX); |
| 501 | DefInt(SQLITE_CREATE_TEMP_TABLE); |
| 502 | DefInt(SQLITE_CREATE_TEMP_TRIGGER); |
| 503 | DefInt(SQLITE_CREATE_TEMP_VIEW); |
| 504 | DefInt(SQLITE_CREATE_TRIGGER); |
| 505 | DefInt(SQLITE_CREATE_VIEW); |
| 506 | DefInt(SQLITE_DELETE); |
| 507 | DefInt(SQLITE_DROP_INDEX); |
| 508 | DefInt(SQLITE_DROP_TABLE); |
| 509 | DefInt(SQLITE_DROP_TEMP_INDEX); |
| 510 | DefInt(SQLITE_DROP_TEMP_TABLE); |
| 511 | DefInt(SQLITE_DROP_TEMP_TRIGGER); |
| 512 | DefInt(SQLITE_DROP_TEMP_VIEW); |
| 513 | DefInt(SQLITE_DROP_TRIGGER); |
| 514 | DefInt(SQLITE_DROP_VIEW); |
| 515 | DefInt(SQLITE_INSERT); |
| 516 | DefInt(SQLITE_PRAGMA); |
| 517 | DefInt(SQLITE_READ); |
| 518 | DefInt(SQLITE_SELECT); |
| 519 | DefInt(SQLITE_TRANSACTION); |
| 520 | DefInt(SQLITE_UPDATE); |
| 521 | DefInt(SQLITE_ATTACH); |
| 522 | DefInt(SQLITE_DETACH); |
| 523 | DefInt(SQLITE_ALTER_TABLE); |
| 524 | DefInt(SQLITE_REINDEX); |
| 525 | DefInt(SQLITE_ANALYZE); |
| 526 | DefInt(SQLITE_CREATE_VTABLE); |
| 527 | DefInt(SQLITE_DROP_VTABLE); |
| 528 | DefInt(SQLITE_FUNCTION); |
| 529 | DefInt(SQLITE_SAVEPOINT); |
| 530 | //DefInt(SQLITE_COPY) /* No longer used */; |
| 531 | DefInt(SQLITE_RECURSIVE); |
| 532 | } _DefGroup; |
| 533 | |
| 534 | DefGroup(blobFinalizers) { |
| 535 | /* SQLITE_STATIC/TRANSIENT need to be handled explicitly as |
| 536 | ** integers to avoid casting-related warnings. */ |
| 537 | out("\"SQLITE_STATIC\":0, \"SQLITE_TRANSIENT\":-1"); |
| 538 | outf(",\"SQLITE_WASM_DEALLOC\": %lld", |
| 539 | (sqlite3_int64)(sqlite3_free)); |
| 540 | } _DefGroup; |
| 541 | |
| 542 | DefGroup(changeset){ |
| 543 | #ifdef SQLITE_CHANGESETSTART_INVERT |
| 544 | DefInt(SQLITE_CHANGESETSTART_INVERT); |
| 545 | DefInt(SQLITE_CHANGESETAPPLY_NOSAVEPOINT); |
| 546 | DefInt(SQLITE_CHANGESETAPPLY_INVERT); |
| 547 | DefInt(SQLITE_CHANGESETAPPLY_IGNORENOOP); |
| 548 | |
| 549 | DefInt(SQLITE_CHANGESET_DATA); |
| 550 | DefInt(SQLITE_CHANGESET_NOTFOUND); |
| 551 | DefInt(SQLITE_CHANGESET_CONFLICT); |
| 552 | DefInt(SQLITE_CHANGESET_CONSTRAINT); |
| 553 | DefInt(SQLITE_CHANGESET_FOREIGN_KEY); |
| 554 | |
| 555 | DefInt(SQLITE_CHANGESET_OMIT); |
| 556 | DefInt(SQLITE_CHANGESET_REPLACE); |
| 557 | DefInt(SQLITE_CHANGESET_ABORT); |
| 558 | #endif |
| 559 | } _DefGroup; |
| 560 | |
| 561 | DefGroup(config){ |
| 562 | DefInt(SQLITE_CONFIG_SINGLETHREAD); |
| 563 | DefInt(SQLITE_CONFIG_MULTITHREAD); |
| 564 | DefInt(SQLITE_CONFIG_SERIALIZED); |
| 565 | DefInt(SQLITE_CONFIG_MALLOC); |
| 566 | DefInt(SQLITE_CONFIG_GETMALLOC); |
| 567 | DefInt(SQLITE_CONFIG_SCRATCH); |
| 568 | DefInt(SQLITE_CONFIG_PAGECACHE); |
| 569 | DefInt(SQLITE_CONFIG_HEAP); |
| 570 | DefInt(SQLITE_CONFIG_MEMSTATUS); |
| 571 | DefInt(SQLITE_CONFIG_MUTEX); |
| 572 | DefInt(SQLITE_CONFIG_GETMUTEX); |
| 573 | /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ |
| 574 | DefInt(SQLITE_CONFIG_LOOKASIDE); |
| 575 | DefInt(SQLITE_CONFIG_PCACHE); |
| 576 | DefInt(SQLITE_CONFIG_GETPCACHE); |
| 577 | DefInt(SQLITE_CONFIG_LOG); |
| 578 | DefInt(SQLITE_CONFIG_URI); |
| 579 | DefInt(SQLITE_CONFIG_PCACHE2); |
| 580 | DefInt(SQLITE_CONFIG_GETPCACHE2); |
| 581 | DefInt(SQLITE_CONFIG_COVERING_INDEX_SCAN); |
| 582 | DefInt(SQLITE_CONFIG_SQLLOG); |
| 583 | DefInt(SQLITE_CONFIG_MMAP_SIZE); |
| 584 | DefInt(SQLITE_CONFIG_WIN32_HEAPSIZE); |
| 585 | DefInt(SQLITE_CONFIG_PCACHE_HDRSZ); |
| 586 | DefInt(SQLITE_CONFIG_PMASZ); |
| 587 | DefInt(SQLITE_CONFIG_STMTJRNL_SPILL); |
| 588 | DefInt(SQLITE_CONFIG_SMALL_MALLOC); |
| 589 | DefInt(SQLITE_CONFIG_SORTERREF_SIZE); |
| 590 | DefInt(SQLITE_CONFIG_MEMDB_MAXSIZE); |
| 591 | /* maintenance note: we specifically do not include |
| 592 | SQLITE_CONFIG_ROWID_IN_VIEW here, on the grounds that |
| 593 | it's only for legacy support and no apps written with |
| 594 | this API require that. */ |
| 595 | } _DefGroup; |
| 596 | |
| 597 | DefGroup(dataTypes) { |
| 598 | DefInt(SQLITE_INTEGER); |
| 599 | DefInt(SQLITE_FLOAT); |
| 600 | DefInt(SQLITE_TEXT); |
| 601 | DefInt(SQLITE_BLOB); |
| 602 | DefInt(SQLITE_NULL); |
| 603 | } _DefGroup; |
| 604 | |
| 605 | DefGroup(dbConfig){ |
| 606 | DefInt(SQLITE_DBCONFIG_MAINDBNAME); |
| 607 | DefInt(SQLITE_DBCONFIG_LOOKASIDE); |
| 608 | DefInt(SQLITE_DBCONFIG_ENABLE_FKEY); |
| 609 | DefInt(SQLITE_DBCONFIG_ENABLE_TRIGGER); |
| 610 | DefInt(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION); |
| 611 | DefInt(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE); |
| 612 | DefInt(SQLITE_DBCONFIG_ENABLE_QPSG); |
| 613 | DefInt(SQLITE_DBCONFIG_TRIGGER_EQP); |
| 614 | DefInt(SQLITE_DBCONFIG_RESET_DATABASE); |
| 615 | DefInt(SQLITE_DBCONFIG_DEFENSIVE); |
| 616 | DefInt(SQLITE_DBCONFIG_WRITABLE_SCHEMA); |
| 617 | DefInt(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE); |
| 618 | DefInt(SQLITE_DBCONFIG_DQS_DML); |
| 619 | DefInt(SQLITE_DBCONFIG_DQS_DDL); |
| 620 | DefInt(SQLITE_DBCONFIG_ENABLE_VIEW); |
| 621 | DefInt(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT); |
| 622 | DefInt(SQLITE_DBCONFIG_TRUSTED_SCHEMA); |
| 623 | DefInt(SQLITE_DBCONFIG_STMT_SCANSTATUS); |
| 624 | DefInt(SQLITE_DBCONFIG_REVERSE_SCANORDER); |
| 625 | DefInt(SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE); |
| 626 | DefInt(SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE); |
| 627 | DefInt(SQLITE_DBCONFIG_ENABLE_COMMENTS); |
| 628 | DefInt(SQLITE_DBCONFIG_MAX); |
| 629 | DefInt(SQLITE_DBCONFIG_FP_DIGITS); |
| 630 | } _DefGroup; |
| 631 | |
| 632 | DefGroup(dbStatus){ |
| 633 | DefInt(SQLITE_DBSTATUS_LOOKASIDE_USED); |
| 634 | DefInt(SQLITE_DBSTATUS_CACHE_USED); |
| 635 | DefInt(SQLITE_DBSTATUS_SCHEMA_USED); |
| 636 | DefInt(SQLITE_DBSTATUS_STMT_USED); |
| 637 | DefInt(SQLITE_DBSTATUS_LOOKASIDE_HIT); |
| 638 | DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE); |
| 639 | DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL); |
| 640 | DefInt(SQLITE_DBSTATUS_CACHE_HIT); |
| 641 | DefInt(SQLITE_DBSTATUS_CACHE_MISS); |
| 642 | DefInt(SQLITE_DBSTATUS_CACHE_WRITE); |
| 643 | DefInt(SQLITE_DBSTATUS_DEFERRED_FKS); |
| 644 | DefInt(SQLITE_DBSTATUS_CACHE_USED_SHARED); |
| 645 | DefInt(SQLITE_DBSTATUS_CACHE_SPILL); |
| 646 | DefInt(SQLITE_DBSTATUS_TEMPBUF_SPILL); |
| 647 | DefInt(SQLITE_DBSTATUS_MAX); |
| 648 | } _DefGroup; |
| 649 | |
| 650 | DefGroup(encodings) { |
| 651 | /* Noting that the wasm binding only aims to support UTF-8. */ |
| 652 | DefInt(SQLITE_UTF8); |
| 653 | DefInt(SQLITE_UTF8_ZT); |
| 654 | DefInt(SQLITE_UTF16LE); |
| 655 | DefInt(SQLITE_UTF16BE); |
| 656 | DefInt(SQLITE_UTF16); |
| 657 | /*deprecated DefInt(SQLITE_ANY); */ |
| 658 | DefInt(SQLITE_UTF16_ALIGNED); |
| 659 | } _DefGroup; |
| 660 | |
| 661 | DefGroup(fcntl) { |
| 662 | DefInt(SQLITE_FCNTL_LOCKSTATE); |
| 663 | DefInt(SQLITE_FCNTL_GET_LOCKPROXYFILE); |
| 664 | DefInt(SQLITE_FCNTL_SET_LOCKPROXYFILE); |
| 665 | DefInt(SQLITE_FCNTL_LAST_ERRNO); |
| 666 | DefInt(SQLITE_FCNTL_SIZE_HINT); |
| 667 | DefInt(SQLITE_FCNTL_CHUNK_SIZE); |
| 668 | DefInt(SQLITE_FCNTL_FILE_POINTER); |
| 669 | DefInt(SQLITE_FCNTL_SYNC_OMITTED); |
| 670 | DefInt(SQLITE_FCNTL_WIN32_AV_RETRY); |
| 671 | DefInt(SQLITE_FCNTL_PERSIST_WAL); |
| 672 | DefInt(SQLITE_FCNTL_OVERWRITE); |
| 673 | DefInt(SQLITE_FCNTL_VFSNAME); |
| 674 | DefInt(SQLITE_FCNTL_POWERSAFE_OVERWRITE); |
| 675 | DefInt(SQLITE_FCNTL_PRAGMA); |
| 676 | DefInt(SQLITE_FCNTL_BUSYHANDLER); |
| 677 | DefInt(SQLITE_FCNTL_TEMPFILENAME); |
| 678 | DefInt(SQLITE_FCNTL_MMAP_SIZE); |
| 679 | DefInt(SQLITE_FCNTL_TRACE); |
| 680 | DefInt(SQLITE_FCNTL_HAS_MOVED); |
| 681 | DefInt(SQLITE_FCNTL_SYNC); |
| 682 | DefInt(SQLITE_FCNTL_COMMIT_PHASETWO); |
| 683 | DefInt(SQLITE_FCNTL_WIN32_SET_HANDLE); |
| 684 | DefInt(SQLITE_FCNTL_WAL_BLOCK); |
| 685 | DefInt(SQLITE_FCNTL_ZIPVFS); |
| 686 | DefInt(SQLITE_FCNTL_RBU); |
| 687 | DefInt(SQLITE_FCNTL_VFS_POINTER); |
| 688 | DefInt(SQLITE_FCNTL_JOURNAL_POINTER); |
| 689 | DefInt(SQLITE_FCNTL_WIN32_GET_HANDLE); |
| 690 | DefInt(SQLITE_FCNTL_PDB); |
| 691 | DefInt(SQLITE_FCNTL_BEGIN_ATOMIC_WRITE); |
| 692 | DefInt(SQLITE_FCNTL_COMMIT_ATOMIC_WRITE); |
| 693 | DefInt(SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE); |
| 694 | DefInt(SQLITE_FCNTL_LOCK_TIMEOUT); |
| 695 | DefInt(SQLITE_FCNTL_DATA_VERSION); |
| 696 | DefInt(SQLITE_FCNTL_SIZE_LIMIT); |
| 697 | DefInt(SQLITE_FCNTL_CKPT_DONE); |
| 698 | DefInt(SQLITE_FCNTL_RESERVE_BYTES); |
| 699 | DefInt(SQLITE_FCNTL_CKPT_START); |
| 700 | DefInt(SQLITE_FCNTL_EXTERNAL_READER); |
| 701 | DefInt(SQLITE_FCNTL_CKSM_FILE); |
| 702 | DefInt(SQLITE_FCNTL_RESET_CACHE); |
| 703 | } _DefGroup; |
| 704 | |
| 705 | DefGroup(flock) { |
| 706 | DefInt(SQLITE_LOCK_NONE); |
| 707 | DefInt(SQLITE_LOCK_SHARED); |
| 708 | DefInt(SQLITE_LOCK_RESERVED); |
| 709 | DefInt(SQLITE_LOCK_PENDING); |
| 710 | DefInt(SQLITE_LOCK_EXCLUSIVE); |
| 711 | } _DefGroup; |
| 712 | |
| 713 | DefGroup(ioCap) { |
| 714 | DefInt(SQLITE_IOCAP_ATOMIC); |
| 715 | DefInt(SQLITE_IOCAP_ATOMIC512); |
| 716 | DefInt(SQLITE_IOCAP_ATOMIC1K); |
| 717 | DefInt(SQLITE_IOCAP_ATOMIC2K); |
| 718 | DefInt(SQLITE_IOCAP_ATOMIC4K); |
| 719 | DefInt(SQLITE_IOCAP_ATOMIC8K); |
| 720 | DefInt(SQLITE_IOCAP_ATOMIC16K); |
| 721 | DefInt(SQLITE_IOCAP_ATOMIC32K); |
| 722 | DefInt(SQLITE_IOCAP_ATOMIC64K); |
| 723 | DefInt(SQLITE_IOCAP_SAFE_APPEND); |
| 724 | DefInt(SQLITE_IOCAP_SEQUENTIAL); |
| 725 | DefInt(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN); |
| 726 | DefInt(SQLITE_IOCAP_POWERSAFE_OVERWRITE); |
| 727 | DefInt(SQLITE_IOCAP_IMMUTABLE); |
| 728 | DefInt(SQLITE_IOCAP_BATCH_ATOMIC); |
| 729 | } _DefGroup; |
| 730 | |
| 731 | DefGroup(limits) { |
| 732 | DefInt(SQLITE_MAX_ALLOCATION_SIZE); |
| 733 | DefInt(SQLITE_LIMIT_LENGTH); |
| 734 | DefInt(SQLITE_MAX_LENGTH); |
| 735 | DefInt(SQLITE_LIMIT_SQL_LENGTH); |
| 736 | DefInt(SQLITE_MAX_SQL_LENGTH); |
| 737 | DefInt(SQLITE_LIMIT_COLUMN); |
| 738 | DefInt(SQLITE_MAX_COLUMN); |
| 739 | DefInt(SQLITE_LIMIT_EXPR_DEPTH); |
| 740 | DefInt(SQLITE_MAX_EXPR_DEPTH); |
| 741 | DefInt(SQLITE_LIMIT_COMPOUND_SELECT); |
| 742 | DefInt(SQLITE_MAX_COMPOUND_SELECT); |
| 743 | DefInt(SQLITE_LIMIT_VDBE_OP); |
| 744 | DefInt(SQLITE_MAX_VDBE_OP); |
| 745 | DefInt(SQLITE_LIMIT_FUNCTION_ARG); |
| 746 | DefInt(SQLITE_MAX_FUNCTION_ARG); |
| 747 | DefInt(SQLITE_LIMIT_ATTACHED); |
| 748 | DefInt(SQLITE_MAX_ATTACHED); |
| 749 | DefInt(SQLITE_LIMIT_LIKE_PATTERN_LENGTH); |
| 750 | DefInt(SQLITE_MAX_LIKE_PATTERN_LENGTH); |
| 751 | DefInt(SQLITE_LIMIT_VARIABLE_NUMBER); |
| 752 | DefInt(SQLITE_MAX_VARIABLE_NUMBER); |
| 753 | DefInt(SQLITE_LIMIT_TRIGGER_DEPTH); |
| 754 | DefInt(SQLITE_MAX_TRIGGER_DEPTH); |
| 755 | DefInt(SQLITE_LIMIT_WORKER_THREADS); |
| 756 | DefInt(SQLITE_MAX_WORKER_THREADS); |
| 757 | DefInt(SQLITE_LIMIT_PARSER_DEPTH); |
| 758 | DefInt(SQLITE_MAX_PARSER_DEPTH); |
| 759 | } _DefGroup; |
| 760 | |
| 761 | DefGroup(openFlags) { |
| 762 | /* Noting that not all of these will have any effect in |
| 763 | ** WASM-space. */ |
| 764 | DefInt(SQLITE_OPEN_READONLY); |
| 765 | DefInt(SQLITE_OPEN_READWRITE); |
| 766 | DefInt(SQLITE_OPEN_CREATE); |
| 767 | DefInt(SQLITE_OPEN_URI); |
| 768 | DefInt(SQLITE_OPEN_MEMORY); |
| 769 | DefInt(SQLITE_OPEN_NOMUTEX); |
| 770 | DefInt(SQLITE_OPEN_FULLMUTEX); |
| 771 | DefInt(SQLITE_OPEN_SHAREDCACHE); |
| 772 | DefInt(SQLITE_OPEN_PRIVATECACHE); |
| 773 | DefInt(SQLITE_OPEN_EXRESCODE); |
| 774 | DefInt(SQLITE_OPEN_NOFOLLOW); |
| 775 | /* OPEN flags for use with VFSes... */ |
| 776 | DefInt(SQLITE_OPEN_MAIN_DB); |
| 777 | DefInt(SQLITE_OPEN_MAIN_JOURNAL); |
| 778 | DefInt(SQLITE_OPEN_TEMP_DB); |
| 779 | DefInt(SQLITE_OPEN_TEMP_JOURNAL); |
| 780 | DefInt(SQLITE_OPEN_TRANSIENT_DB); |
| 781 | DefInt(SQLITE_OPEN_SUBJOURNAL); |
| 782 | DefInt(SQLITE_OPEN_SUPER_JOURNAL); |
| 783 | DefInt(SQLITE_OPEN_WAL); |
| 784 | DefInt(SQLITE_OPEN_DELETEONCLOSE); |
| 785 | DefInt(SQLITE_OPEN_EXCLUSIVE); |
| 786 | } _DefGroup; |
| 787 | |
| 788 | DefGroup(prepareFlags) { |
| 789 | DefInt(SQLITE_PREPARE_PERSISTENT); |
| 790 | DefInt(SQLITE_PREPARE_NORMALIZE); |
| 791 | DefInt(SQLITE_PREPARE_NO_VTAB); |
| 792 | DefInt(SQLITE_PREPARE_FROM_DDL); |
| 793 | } _DefGroup; |
| 794 | |
| 795 | DefGroup(resultCodes) { |
| 796 | DefInt(SQLITE_OK); |
| 797 | DefInt(SQLITE_ERROR); |
| 798 | DefInt(SQLITE_INTERNAL); |
| 799 | DefInt(SQLITE_PERM); |
| 800 | DefInt(SQLITE_ABORT); |
| 801 | DefInt(SQLITE_BUSY); |
| 802 | DefInt(SQLITE_LOCKED); |
| 803 | DefInt(SQLITE_NOMEM); |
| 804 | DefInt(SQLITE_READONLY); |
| 805 | DefInt(SQLITE_INTERRUPT); |
| 806 | DefInt(SQLITE_IOERR); |
| 807 | DefInt(SQLITE_CORRUPT); |
| 808 | DefInt(SQLITE_NOTFOUND); |
| 809 | DefInt(SQLITE_FULL); |
| 810 | DefInt(SQLITE_CANTOPEN); |
| 811 | DefInt(SQLITE_PROTOCOL); |
| 812 | DefInt(SQLITE_EMPTY); |
| 813 | DefInt(SQLITE_SCHEMA); |
| 814 | DefInt(SQLITE_TOOBIG); |
| 815 | DefInt(SQLITE_CONSTRAINT); |
| 816 | DefInt(SQLITE_MISMATCH); |
| 817 | DefInt(SQLITE_MISUSE); |
| 818 | DefInt(SQLITE_NOLFS); |
| 819 | DefInt(SQLITE_AUTH); |
| 820 | DefInt(SQLITE_FORMAT); |
| 821 | DefInt(SQLITE_RANGE); |
| 822 | DefInt(SQLITE_NOTADB); |
| 823 | DefInt(SQLITE_NOTICE); |
| 824 | DefInt(SQLITE_WARNING); |
| 825 | DefInt(SQLITE_ROW); |
| 826 | DefInt(SQLITE_DONE); |
| 827 | // Extended Result Codes |
| 828 | DefInt(SQLITE_ERROR_MISSING_COLLSEQ); |
| 829 | DefInt(SQLITE_ERROR_RETRY); |
| 830 | DefInt(SQLITE_ERROR_SNAPSHOT); |
| 831 | DefInt(SQLITE_IOERR_READ); |
| 832 | DefInt(SQLITE_IOERR_SHORT_READ); |
| 833 | DefInt(SQLITE_IOERR_WRITE); |
| 834 | DefInt(SQLITE_IOERR_FSYNC); |
| 835 | DefInt(SQLITE_IOERR_DIR_FSYNC); |
| 836 | DefInt(SQLITE_IOERR_TRUNCATE); |
| 837 | DefInt(SQLITE_IOERR_FSTAT); |
| 838 | DefInt(SQLITE_IOERR_UNLOCK); |
| 839 | DefInt(SQLITE_IOERR_RDLOCK); |
| 840 | DefInt(SQLITE_IOERR_DELETE); |
| 841 | DefInt(SQLITE_IOERR_BLOCKED); |
| 842 | DefInt(SQLITE_IOERR_NOMEM); |
| 843 | DefInt(SQLITE_IOERR_ACCESS); |
| 844 | DefInt(SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 845 | DefInt(SQLITE_IOERR_LOCK); |
| 846 | DefInt(SQLITE_IOERR_CLOSE); |
| 847 | DefInt(SQLITE_IOERR_DIR_CLOSE); |
| 848 | DefInt(SQLITE_IOERR_SHMOPEN); |
| 849 | DefInt(SQLITE_IOERR_SHMSIZE); |
| 850 | DefInt(SQLITE_IOERR_SHMLOCK); |
| 851 | DefInt(SQLITE_IOERR_SHMMAP); |
| 852 | DefInt(SQLITE_IOERR_SEEK); |
| 853 | DefInt(SQLITE_IOERR_DELETE_NOENT); |
| 854 | DefInt(SQLITE_IOERR_MMAP); |
| 855 | DefInt(SQLITE_IOERR_GETTEMPPATH); |
| 856 | DefInt(SQLITE_IOERR_CONVPATH); |
| 857 | DefInt(SQLITE_IOERR_VNODE); |
| 858 | DefInt(SQLITE_IOERR_AUTH); |
| 859 | DefInt(SQLITE_IOERR_BEGIN_ATOMIC); |
| 860 | DefInt(SQLITE_IOERR_COMMIT_ATOMIC); |
| 861 | DefInt(SQLITE_IOERR_ROLLBACK_ATOMIC); |
| 862 | DefInt(SQLITE_IOERR_DATA); |
| 863 | DefInt(SQLITE_IOERR_CORRUPTFS); |
| 864 | DefInt(SQLITE_LOCKED_SHAREDCACHE); |
| 865 | DefInt(SQLITE_LOCKED_VTAB); |
| 866 | DefInt(SQLITE_BUSY_RECOVERY); |
| 867 | DefInt(SQLITE_BUSY_SNAPSHOT); |
| 868 | DefInt(SQLITE_BUSY_TIMEOUT); |
| 869 | DefInt(SQLITE_CANTOPEN_NOTEMPDIR); |
| 870 | DefInt(SQLITE_CANTOPEN_ISDIR); |
| 871 | DefInt(SQLITE_CANTOPEN_FULLPATH); |
| 872 | DefInt(SQLITE_CANTOPEN_CONVPATH); |
| 873 | //DefInt(SQLITE_CANTOPEN_DIRTYWAL)/*docs say not used*/; |
| 874 | DefInt(SQLITE_CANTOPEN_SYMLINK); |
| 875 | DefInt(SQLITE_CORRUPT_VTAB); |
| 876 | DefInt(SQLITE_CORRUPT_SEQUENCE); |
| 877 | DefInt(SQLITE_CORRUPT_INDEX); |
| 878 | DefInt(SQLITE_READONLY_RECOVERY); |
| 879 | DefInt(SQLITE_READONLY_CANTLOCK); |
| 880 | DefInt(SQLITE_READONLY_ROLLBACK); |
| 881 | DefInt(SQLITE_READONLY_DBMOVED); |
| 882 | DefInt(SQLITE_READONLY_CANTINIT); |
| 883 | DefInt(SQLITE_READONLY_DIRECTORY); |
| 884 | DefInt(SQLITE_ABORT_ROLLBACK); |
| 885 | DefInt(SQLITE_CONSTRAINT_CHECK); |
| 886 | DefInt(SQLITE_CONSTRAINT_COMMITHOOK); |
| 887 | DefInt(SQLITE_CONSTRAINT_FOREIGNKEY); |
| 888 | DefInt(SQLITE_CONSTRAINT_FUNCTION); |
| 889 | DefInt(SQLITE_CONSTRAINT_NOTNULL); |
| 890 | DefInt(SQLITE_CONSTRAINT_PRIMARYKEY); |
| 891 | DefInt(SQLITE_CONSTRAINT_TRIGGER); |
| 892 | DefInt(SQLITE_CONSTRAINT_UNIQUE); |
| 893 | DefInt(SQLITE_CONSTRAINT_VTAB); |
| 894 | DefInt(SQLITE_CONSTRAINT_ROWID); |
| 895 | DefInt(SQLITE_CONSTRAINT_PINNED); |
| 896 | DefInt(SQLITE_CONSTRAINT_DATATYPE); |
| 897 | DefInt(SQLITE_NOTICE_RECOVER_WAL); |
| 898 | DefInt(SQLITE_NOTICE_RECOVER_ROLLBACK); |
| 899 | DefInt(SQLITE_WARNING_AUTOINDEX); |
| 900 | DefInt(SQLITE_AUTH_USER); |
| 901 | DefInt(SQLITE_OK_LOAD_PERMANENTLY); |
| 902 | //DefInt(SQLITE_OK_SYMLINK) /* internal use only */; |
| 903 | } _DefGroup; |
| 904 | |
| 905 | DefGroup(serialize){ |
| 906 | DefInt(SQLITE_SERIALIZE_NOCOPY); |
| 907 | DefInt(SQLITE_DESERIALIZE_FREEONCLOSE); |
| 908 | DefInt(SQLITE_DESERIALIZE_READONLY); |
| 909 | DefInt(SQLITE_DESERIALIZE_RESIZEABLE); |
| 910 | } _DefGroup; |
| 911 | |
| 912 | DefGroup(session){ |
| 913 | #ifdef SQLITE_SESSION_CONFIG_STRMSIZE |
| 914 | DefInt(SQLITE_SESSION_CONFIG_STRMSIZE); |
| 915 | DefInt(SQLITE_SESSION_OBJCONFIG_SIZE); |
| 916 | #endif |
| 917 | } _DefGroup; |
| 918 | |
| 919 | DefGroup(sqlite3Status){ |
| 920 | DefInt(SQLITE_STATUS_MEMORY_USED); |
| 921 | DefInt(SQLITE_STATUS_PAGECACHE_USED); |
| 922 | DefInt(SQLITE_STATUS_PAGECACHE_OVERFLOW); |
| 923 | //DefInt(SQLITE_STATUS_SCRATCH_USED) /* NOT USED */; |
| 924 | //DefInt(SQLITE_STATUS_SCRATCH_OVERFLOW) /* NOT USED */; |
| 925 | DefInt(SQLITE_STATUS_MALLOC_SIZE); |
| 926 | DefInt(SQLITE_STATUS_PARSER_STACK); |
| 927 | DefInt(SQLITE_STATUS_PAGECACHE_SIZE); |
| 928 | //DefInt(SQLITE_STATUS_SCRATCH_SIZE) /* NOT USED */; |
| 929 | DefInt(SQLITE_STATUS_MALLOC_COUNT); |
| 930 | } _DefGroup; |
| 931 | |
| 932 | DefGroup(stmtStatus){ |
| 933 | DefInt(SQLITE_STMTSTATUS_FULLSCAN_STEP); |
| 934 | DefInt(SQLITE_STMTSTATUS_SORT); |
| 935 | DefInt(SQLITE_STMTSTATUS_AUTOINDEX); |
| 936 | DefInt(SQLITE_STMTSTATUS_VM_STEP); |
| 937 | DefInt(SQLITE_STMTSTATUS_REPREPARE); |
| 938 | DefInt(SQLITE_STMTSTATUS_RUN); |
| 939 | DefInt(SQLITE_STMTSTATUS_FILTER_MISS); |
| 940 | DefInt(SQLITE_STMTSTATUS_FILTER_HIT); |
| 941 | DefInt(SQLITE_STMTSTATUS_MEMUSED); |
| 942 | } _DefGroup; |
| 943 | |
| 944 | DefGroup(syncFlags) { |
| 945 | DefInt(SQLITE_SYNC_NORMAL); |
| 946 | DefInt(SQLITE_SYNC_FULL); |
| 947 | DefInt(SQLITE_SYNC_DATAONLY); |
| 948 | } _DefGroup; |
| 949 | |
| 950 | DefGroup(trace) { |
| 951 | DefInt(SQLITE_TRACE_STMT); |
| 952 | DefInt(SQLITE_TRACE_PROFILE); |
| 953 | DefInt(SQLITE_TRACE_ROW); |
| 954 | DefInt(SQLITE_TRACE_CLOSE); |
| 955 | } _DefGroup; |
| 956 | |
| 957 | DefGroup(txnState){ |
| 958 | DefInt(SQLITE_TXN_NONE); |
| 959 | DefInt(SQLITE_TXN_READ); |
| 960 | DefInt(SQLITE_TXN_WRITE); |
| 961 | } _DefGroup; |
| 962 | |
| 963 | DefGroup(udfFlags) { |
| 964 | DefInt(SQLITE_DETERMINISTIC); |
| 965 | DefInt(SQLITE_DIRECTONLY); |
| 966 | DefInt(SQLITE_INNOCUOUS); |
| 967 | DefInt(SQLITE_SUBTYPE); |
| 968 | DefInt(SQLITE_RESULT_SUBTYPE); |
| 969 | DefInt(SQLITE_SELFORDER1); |
| 970 | } _DefGroup; |
| 971 | |
| 972 | DefGroup(version) { |
| 973 | DefInt(SQLITE_VERSION_NUMBER); |
| 974 | DefStr(SQLITE_VERSION); |
| 975 | DefStr(SQLITE_SOURCE_ID); |
| 976 | } _DefGroup; |
| 977 | |
| 978 | DefGroup(vtab) { |
| 979 | #if SQLITE_WASM_HAS_VTAB |
| 980 | DefInt(SQLITE_INDEX_SCAN_UNIQUE); |
| 981 | DefInt(SQLITE_INDEX_CONSTRAINT_EQ); |
| 982 | DefInt(SQLITE_INDEX_CONSTRAINT_GT); |
| 983 | DefInt(SQLITE_INDEX_CONSTRAINT_LE); |
| 984 | DefInt(SQLITE_INDEX_CONSTRAINT_LT); |
| 985 | DefInt(SQLITE_INDEX_CONSTRAINT_GE); |
| 986 | DefInt(SQLITE_INDEX_CONSTRAINT_MATCH); |
| 987 | DefInt(SQLITE_INDEX_CONSTRAINT_LIKE); |
| 988 | DefInt(SQLITE_INDEX_CONSTRAINT_GLOB); |
| 989 | DefInt(SQLITE_INDEX_CONSTRAINT_REGEXP); |
| 990 | DefInt(SQLITE_INDEX_CONSTRAINT_NE); |
| 991 | DefInt(SQLITE_INDEX_CONSTRAINT_ISNOT); |
| 992 | DefInt(SQLITE_INDEX_CONSTRAINT_ISNOTNULL); |
| 993 | DefInt(SQLITE_INDEX_CONSTRAINT_ISNULL); |
| 994 | DefInt(SQLITE_INDEX_CONSTRAINT_IS); |
| 995 | DefInt(SQLITE_INDEX_CONSTRAINT_LIMIT); |
| 996 | DefInt(SQLITE_INDEX_CONSTRAINT_OFFSET); |
| 997 | DefInt(SQLITE_INDEX_CONSTRAINT_FUNCTION); |
| 998 | DefInt(SQLITE_VTAB_CONSTRAINT_SUPPORT); |
| 999 | DefInt(SQLITE_VTAB_INNOCUOUS); |
| 1000 | DefInt(SQLITE_VTAB_DIRECTONLY); |
| 1001 | DefInt(SQLITE_VTAB_USES_ALL_SCHEMAS); |
| 1002 | DefInt(SQLITE_ROLLBACK); |
| 1003 | //DefInt(SQLITE_IGNORE); // Also used by sqlite3_authorizer() callback |
| 1004 | DefInt(SQLITE_FAIL); |
| 1005 | //DefInt(SQLITE_ABORT); // Also an error code |
| 1006 | DefInt(SQLITE_REPLACE); |
| 1007 | #endif /*SQLITE_WASM_HAS_VTAB*/ |
| 1008 | } _DefGroup; |
| 1009 | |
| 1010 | #undef DefGroup |
| 1011 | #undef DefStr |
| 1012 | #undef DefInt |
| 1013 | #undef _DefGroup |
| 1014 | |
| 1015 | /* |
| 1016 | ** Emit an array of "StructBinder" struct descriptions, which look |
| 1017 | ** like: |
| 1018 | ** |
| 1019 | ** { |
| 1020 | ** "name": "MyStruct", |
| 1021 | ** "sizeof": 16, |
| 1022 | ** "members": { |
| 1023 | ** "member1": {"offset": 0,"sizeof": 4,"signature": "i"}, |
| 1024 | ** "member2": {"offset": 4,"sizeof": 4,"signature": "p"}, |
| 1025 | ** "member3": {"offset": 8,"sizeof": 8,"signature": "j"} |
| 1026 | ** } |
| 1027 | ** } |
| 1028 | ** |
| 1029 | ** Detailed documentation for those bits are in the Jaccwabyt |
| 1030 | ** JS-side component. |
| 1031 | */ |
| 1032 | |
| 1033 | /** Macros for emitting StructBinder description. */ |
| 1034 | #define StructBinder__(TYPE) \ |
| 1035 | n = 0; \ |
| 1036 | outf("%s{", (nStruct++ ? ", " : "")); \ |
| 1037 | out("\"name\": \"" # TYPE "\","); \ |
| 1038 | outf("\"sizeof\": %d", (int)sizeof(TYPE)); \ |
| 1039 | out(",\"members\": {"); |
| 1040 | #define StructBinder_(T) StructBinder__(T) |
| 1041 | /** ^^^ indirection needed to expand CurrentStruct */ |
| 1042 | #define StructBinder StructBinder_(CurrentStruct) |
| 1043 | #define _StructBinder CloseBrace(2) |
| 1044 | #define M3(MEMBER,SIG,READONLY) \ |
| 1045 | outf("%s\"%s\": " \ |
| 1046 | "{\"offset\":%d,\"sizeof\":%d,\"signature\":\"%s\"%s}", \ |
| 1047 | (n++ ? ", " : ""), #MEMBER, \ |
| 1048 | (int)offsetof(CurrentStruct,MEMBER), \ |
| 1049 | (int)sizeof(((CurrentStruct*)0)->MEMBER), \ |
| 1050 | SIG, (READONLY ? ",\"readOnly\":true" : "")) |
| 1051 | #define M(MEMBER,SIG) M3(MEMBER,SIG,0) |
| 1052 | #define MRO(MEMBER,SIG) M3(MEMBER,SIG,1) |
| 1053 | |
| 1054 | nStruct = 0; |
| 1055 | out(", \"structs\": ["); { |
| 1056 | |
| 1057 | #define CurrentStruct sqlite3_vfs |
| 1058 | StructBinder { |
| 1059 | M(iVersion, "i"); |
| 1060 | M(szOsFile, "i"); |
| 1061 | M(mxPathname, "i"); |
| 1062 | M(pNext, "p"); |
| 1063 | M(zName, "s"); |
| 1064 | M(pAppData, "p"); |
| 1065 | M(xOpen, "i(pppip)"); |
| 1066 | M(xDelete, "i(ppi)"); |
| 1067 | M(xAccess, "i(ppip)"); |
| 1068 | M(xFullPathname, "i(ppip)"); |
| 1069 | M(xDlOpen, "v(pp)"); |
| 1070 | M(xDlError, "v(pip)"); |
| 1071 | M(xDlSym, "p()"); |
| 1072 | M(xDlClose, "v(pp)"); |
| 1073 | M(xRandomness, "i(pip)"); |
| 1074 | M(xSleep, "i(pi)"); |
| 1075 | M(xCurrentTime, "i(pp)"); |
| 1076 | M(xGetLastError, "i(pip)"); |
| 1077 | M(xCurrentTimeInt64, "i(pp)"); |
| 1078 | M(xSetSystemCall, "i(ppp)"); |
| 1079 | M(xGetSystemCall, "p(pp)"); |
| 1080 | M(xNextSystemCall, "p(pp)"); |
| 1081 | } _StructBinder; |
| 1082 | #undef CurrentStruct |
| 1083 | |
| 1084 | #define CurrentStruct sqlite3_io_methods |
| 1085 | StructBinder { |
| 1086 | M(iVersion, "i"); |
| 1087 | M(xClose, "i(p)"); |
| 1088 | M(xRead, "i(ppij)"); |
| 1089 | M(xWrite, "i(ppij)"); |
| 1090 | M(xTruncate, "i(pj)"); |
| 1091 | M(xSync, "i(pi)"); |
| 1092 | M(xFileSize, "i(pp)"); |
| 1093 | M(xLock, "i(pi)"); |
| 1094 | M(xUnlock, "i(pi)"); |
| 1095 | M(xCheckReservedLock, "i(pp)"); |
| 1096 | M(xFileControl, "i(pip)"); |
| 1097 | M(xSectorSize, "i(p)"); |
| 1098 | M(xDeviceCharacteristics, "i(p)"); |
| 1099 | M(xShmMap, "i(piiip)"); |
| 1100 | M(xShmLock, "i(piii)"); |
| 1101 | M(xShmBarrier, "v(p)"); |
| 1102 | M(xShmUnmap, "i(pi)"); |
| 1103 | M(xFetch, "i(pjip)"); |
| 1104 | M(xUnfetch, "i(pjp)"); |
| 1105 | } _StructBinder; |
| 1106 | #undef CurrentStruct |
| 1107 | |
| 1108 | #define CurrentStruct sqlite3_file |
| 1109 | StructBinder { |
| 1110 | M(pMethods, "p"); |
| 1111 | } _StructBinder; |
| 1112 | #undef CurrentStruct |
| 1113 | |
| 1114 | #define CurrentStruct sqlite3_kvvfs_methods |
| 1115 | /* From os_kv.c */ |
| 1116 | StructBinder { |
| 1117 | M(xRcrdRead, "i(sspi)"); |
| 1118 | M(xRcrdWrite, "i(sss)"); |
| 1119 | M(xRcrdDelete, "i(ss)"); |
| 1120 | MRO(nKeySize, "i"); |
| 1121 | MRO(nBufferSize, "i"); |
| 1122 | M(pVfs, "p"); |
| 1123 | M(pIoDb, "p"); |
| 1124 | M(pIoJrnl, "p"); |
| 1125 | } _StructBinder; |
| 1126 | #undef CurrentStruct |
| 1127 | |
| 1128 | #define CurrentStruct KVVfsFile |
| 1129 | /* From os_kv.c */ |
| 1130 | StructBinder { |
| 1131 | M(base, "p")/*sqlite3_file base*/; |
| 1132 | M(zClass, "s"); |
| 1133 | M(isJournal, "i"); |
| 1134 | M(nJrnl, "i")/*actually unsigned!*/; |
| 1135 | M(aJrnl, "p"); |
| 1136 | M(szPage, "i"); |
| 1137 | M(szDb, "j"); |
| 1138 | M(aData, "p"); |
| 1139 | } _StructBinder; |
| 1140 | #undef CurrentStruct |
| 1141 | |
| 1142 | #if SQLITE_WASM_HAS_VTAB |
| 1143 | #define CurrentStruct sqlite3_vtab |
| 1144 | StructBinder { |
| 1145 | M(pModule, "p"); |
| 1146 | M(nRef, "i"); |
| 1147 | M(zErrMsg, "p"); |
| 1148 | } _StructBinder; |
| 1149 | #undef CurrentStruct |
| 1150 | |
| 1151 | #define CurrentStruct sqlite3_vtab_cursor |
| 1152 | StructBinder { |
| 1153 | M(pVtab, "p"); |
| 1154 | } _StructBinder; |
| 1155 | #undef CurrentStruct |
| 1156 | |
| 1157 | #define CurrentStruct sqlite3_module |
| 1158 | StructBinder { |
| 1159 | M(iVersion, "i"); |
| 1160 | M(xCreate, "i(ppippp)"); |
| 1161 | M(xConnect, "i(ppippp)"); |
| 1162 | M(xBestIndex, "i(pp)"); |
| 1163 | M(xDisconnect, "i(p)"); |
| 1164 | M(xDestroy, "i(p)"); |
| 1165 | M(xOpen, "i(pp)"); |
| 1166 | M(xClose, "i(p)"); |
| 1167 | M(xFilter, "i(pisip)"); |
| 1168 | M(xNext, "i(p)"); |
| 1169 | M(xEof, "i(p)"); |
| 1170 | M(xColumn, "i(ppi)"); |
| 1171 | M(xRowid, "i(pp)"); |
| 1172 | M(xUpdate, "i(pipp)"); |
| 1173 | M(xBegin, "i(p)"); |
| 1174 | M(xSync, "i(p)"); |
| 1175 | M(xCommit, "i(p)"); |
| 1176 | M(xRollback, "i(p)"); |
| 1177 | M(xFindFunction, "i(pispp)"); |
| 1178 | M(xRename, "i(ps)"); |
| 1179 | // ^^^ v1. v2+ follows... |
| 1180 | M(xSavepoint, "i(pi)"); |
| 1181 | M(xRelease, "i(pi)"); |
| 1182 | M(xRollbackTo, "i(pi)"); |
| 1183 | // ^^^ v2. v3+ follows... |
| 1184 | M(xShadowName, "i(s)"); |
| 1185 | // ^^^ v3. v4+ follows... |
| 1186 | M(xIntegrity, "i(pppip)"); |
| 1187 | } _StructBinder; |
| 1188 | #undef CurrentStruct |
| 1189 | |
| 1190 | /** |
| 1191 | ** Workaround: in order to map the various inner structs from |
| 1192 | ** sqlite3_index_info, we have to uplift those into constructs we |
| 1193 | ** can access by type name. These structs _must_ match their |
| 1194 | ** in-sqlite3_index_info counterparts byte for byte. |
| 1195 | ** |
| 1196 | ** 2025-11-21: this uplifing is no longer necessary, as Jaccwabyt |
| 1197 | ** can now handle nested structs, but "it ain't broke" so there's |
| 1198 | ** no pressing need to rewire this. Also, it's conceivable that |
| 1199 | ** rewiring it might break downstream vtab impls, so it shouldn't |
| 1200 | ** be rewired. |
| 1201 | */ |
| 1202 | typedef struct { |
| 1203 | int iColumn; |
| 1204 | unsigned char op; |
| 1205 | unsigned char usable; |
| 1206 | int iTermOffset; |
| 1207 | } sqlite3_index_constraint; |
| 1208 | typedef struct { |
| 1209 | int iColumn; |
| 1210 | unsigned char desc; |
| 1211 | } sqlite3_index_orderby; |
| 1212 | typedef struct { |
| 1213 | int argvIndex; |
| 1214 | unsigned char omit; |
| 1215 | } sqlite3_index_constraint_usage; |
| 1216 | { /* Validate that the above struct sizeof()s match |
| 1217 | ** expectations. We could improve upon this by |
| 1218 | ** checking the offsetof() for each member. */ |
| 1219 | const sqlite3_index_info siiCheck = {0}; |
| 1220 | #define IndexSzCheck(T,M) \ |
| 1221 | (sizeof(T) == sizeof(*siiCheck.M)) |
| 1222 | if(!IndexSzCheck(sqlite3_index_constraint,aConstraint) |
| 1223 | || !IndexSzCheck(sqlite3_index_orderby,aOrderBy) |
| 1224 | || !IndexSzCheck(sqlite3_index_constraint_usage,aConstraintUsage)){ |
| 1225 | assert(!"sizeof mismatch in sqlite3_index_... struct(s)"); |
| 1226 | return 0; |
| 1227 | } |
| 1228 | #undef IndexSzCheck |
| 1229 | } |
| 1230 | |
| 1231 | #define CurrentStruct sqlite3_index_constraint |
| 1232 | StructBinder { |
| 1233 | M(iColumn, "i"); |
| 1234 | M(op, "C"); |
| 1235 | M(usable, "C"); |
| 1236 | M(iTermOffset, "i"); |
| 1237 | } _StructBinder; |
| 1238 | #undef CurrentStruct |
| 1239 | |
| 1240 | #define CurrentStruct sqlite3_index_orderby |
| 1241 | StructBinder { |
| 1242 | M(iColumn, "i"); |
| 1243 | M(desc, "C"); |
| 1244 | } _StructBinder; |
| 1245 | #undef CurrentStruct |
| 1246 | |
| 1247 | #define CurrentStruct sqlite3_index_constraint_usage |
| 1248 | StructBinder { |
| 1249 | M(argvIndex, "i"); |
| 1250 | M(omit, "C"); |
| 1251 | } _StructBinder; |
| 1252 | #undef CurrentStruct |
| 1253 | |
| 1254 | #define CurrentStruct sqlite3_index_info |
| 1255 | StructBinder { |
| 1256 | M(nConstraint, "i"); |
| 1257 | M(aConstraint, "p"); |
| 1258 | M(nOrderBy, "i"); |
| 1259 | M(aOrderBy, "p"); |
| 1260 | M(aConstraintUsage, "p"); |
| 1261 | M(idxNum, "i"); |
| 1262 | M(idxStr, "p"); |
| 1263 | M(needToFreeIdxStr, "i"); |
| 1264 | M(orderByConsumed, "i"); |
| 1265 | M(estimatedCost, "d"); |
| 1266 | M(estimatedRows, "j"); |
| 1267 | M(idxFlags, "i"); |
| 1268 | M(colUsed, "j"); |
| 1269 | } _StructBinder; |
| 1270 | #undef CurrentStruct |
| 1271 | |
| 1272 | #endif /*SQLITE_WASM_HAS_VTAB*/ |
| 1273 | |
| 1274 | #if SQLITE_WASM_ENABLE_C_TESTS |
| 1275 | #define CurrentStruct WasmTestStruct |
| 1276 | StructBinder { |
| 1277 | M(v4, "i"); |
| 1278 | M(cstr, "s"); |
| 1279 | M(ppV, "p"); |
| 1280 | M(v8, "j"); |
| 1281 | M(xFunc, "v(p)"); |
| 1282 | } _StructBinder; |
| 1283 | #undef CurrentStruct |
| 1284 | #endif /*SQLITE_WASM_ENABLE_C_TESTS*/ |
| 1285 | |
| 1286 | } out( "]"/*structs*/); |
| 1287 | |
| 1288 | out("}"/*top-level object*/); |
| 1289 | *zPos = 0; |
| 1290 | aBuffer[0] = '{'/*end of the race-condition workaround*/; |
| 1291 | return aBuffer; |
| 1292 | #undef StructBinder |
| 1293 | #undef StructBinder_ |
| 1294 | #undef StructBinder__ |
| 1295 | #undef M |
| 1296 | #undef MRO |
| 1297 | #undef M3 |
| 1298 | #undef _StructBinder |
| 1299 | #undef CloseBrace |
| 1300 | #undef out |
| 1301 | #undef outf |
| 1302 | #undef lenCheck |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1307 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1308 | ** |
| 1309 | ** This function invokes the xDelete method of the given VFS (or the |
| 1310 | ** default VFS if pVfs is NULL), passing on the given filename. If |
| 1311 | ** zName is NULL, no default VFS is found, or it has no xDelete |
| 1312 | ** method, SQLITE_MISUSE is returned, else the result of the xDelete() |
| 1313 | ** call is returned. |
| 1314 | */ |
| 1315 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_vfs_unlink,(sqlite3_vfs *pVfs, const char *zName)){ |
| 1316 | int rc = SQLITE_MISUSE /* ??? */; |
| 1317 | if( 0==pVfs && 0!=zName ) pVfs = sqlite3_vfs_find(0); |
| 1318 | if( zName && pVfs && pVfs->xDelete ){ |
| 1319 | rc = pVfs->xDelete(pVfs, zName, 1); |
| 1320 | } |
| 1321 | return rc; |
| 1322 | } |
| 1323 | |
| 1324 | /* |
| 1325 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1326 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1327 | ** |
| 1328 | ** Returns a pointer to the given DB's VFS for the given DB name, |
| 1329 | ** defaulting to "main" if zDbName is 0. Returns 0 if no db with the |
| 1330 | ** given name is open. |
| 1331 | */ |
| 1332 | SQLITE_WASM_EXPORT2(sqlite3_vfs *,sqlite3__wasm_db_vfs,(sqlite3 *pDb, const char *zDbName)){ |
| 1333 | sqlite3_vfs * pVfs = 0; |
| 1334 | sqlite3_file_control(pDb, zDbName ? zDbName : "main", |
| 1335 | SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 1336 | return pVfs; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1341 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1342 | ** |
| 1343 | ** This function resets the given db pointer's database as described at |
| 1344 | ** |
| 1345 | ** https://sqlite.org/c3ref/c_dbconfig_defensive.html#sqlitedbconfigresetdatabase |
| 1346 | ** |
| 1347 | ** But beware: virtual tables destroyed that way do not have their |
| 1348 | ** xDestroy() called, so will leak if they require that function for |
| 1349 | ** proper cleanup. |
| 1350 | ** |
| 1351 | ** Returns 0 on success, an SQLITE_xxx code on error. Returns |
| 1352 | ** SQLITE_MISUSE if pDb is NULL. |
| 1353 | */ |
| 1354 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_db_reset,(sqlite3 *pDb)){ |
| 1355 | int rc = SQLITE_MISUSE; |
| 1356 | if( pDb ){ |
| 1357 | sqlite3_table_column_metadata(pDb, "main", 0, 0, 0, 0, 0, 0, 0); |
| 1358 | rc = sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); |
| 1359 | if( 0==rc ){ |
| 1360 | rc = sqlite3_exec(pDb, "VACUUM", 0, 0, 0); |
| 1361 | sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); |
| 1362 | } |
| 1363 | } |
| 1364 | return rc; |
| 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1369 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1370 | ** |
| 1371 | ** Uses the given database's VFS xRead to stream the db file's |
| 1372 | ** contents out to the given callback. The callback gets a single |
| 1373 | ** chunk of size n (its 2nd argument) on each call and must return 0 |
| 1374 | ** on success, non-0 on error. This function returns 0 on success, |
| 1375 | ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0 |
| 1376 | ** code from the callback. Note that this is not thread-friendly: it |
| 1377 | ** expects that it will be the only thread reading the db file and |
| 1378 | ** takes no measures to ensure that is the case. |
| 1379 | ** |
| 1380 | ** This implementation appears to work fine, but |
| 1381 | ** sqlite3__wasm_db_serialize() is arguably the better way to achieve |
| 1382 | ** this. |
| 1383 | */ |
| 1384 | SQLITE_WASM_EXPORT |
| 1385 | int sqlite3__wasm_db_export_chunked( sqlite3* pDb, |
| 1386 | int (*xCallback)(unsigned const char *zOut, int n) ){ |
| 1387 | sqlite3_int64 nSize = 0; |
| 1388 | sqlite3_int64 nPos = 0; |
| 1389 | sqlite3_file * pFile = 0; |
| 1390 | unsigned char buf[1024 * 8]; |
| 1391 | int nBuf = (int)sizeof(buf); |
| 1392 | int rc = pDb |
| 1393 | ? sqlite3_file_control(pDb, "main", |
| 1394 | SQLITE_FCNTL_FILE_POINTER, &pFile) |
| 1395 | : SQLITE_NOTFOUND; |
| 1396 | if( rc ) return rc; |
| 1397 | rc = pFile->pMethods->xFileSize(pFile, &nSize); |
| 1398 | if( rc ) return rc; |
| 1399 | if(nSize % nBuf){ |
| 1400 | /* DB size is not an even multiple of the buffer size. Reduce |
| 1401 | ** buffer size so that we do not unduly inflate the db size |
| 1402 | ** with zero-padding when exporting. */ |
| 1403 | if(0 == nSize % 4096) nBuf = 4096; |
| 1404 | else if(0 == nSize % 2048) nBuf = 2048; |
| 1405 | else if(0 == nSize % 1024) nBuf = 1024; |
| 1406 | else nBuf = 512; |
| 1407 | } |
| 1408 | for( ; 0==rc && nPos<nSize; nPos += nBuf ){ |
| 1409 | rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos); |
| 1410 | if( SQLITE_IOERR_SHORT_READ == rc ){ |
| 1411 | rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/; |
| 1412 | } |
| 1413 | if( 0==rc ) rc = xCallback(buf, nBuf); |
| 1414 | } |
| 1415 | return rc; |
| 1416 | } |
| 1417 | |
| 1418 | /* |
| 1419 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1420 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1421 | ** |
| 1422 | ** A proxy for sqlite3_serialize() which serializes the schema zSchema |
| 1423 | ** of pDb, placing the serialized output in pOut and nOut. nOut may be |
| 1424 | ** NULL. If zSchema is NULL then "main" is assumed. If pDb or pOut are |
| 1425 | ** NULL then SQLITE_MISUSE is returned. If allocation of the |
| 1426 | ** serialized copy fails, SQLITE_NOMEM is returned. On success, 0 is |
| 1427 | ** returned and `*pOut` will contain a pointer to the memory unless |
| 1428 | ** mFlags includes SQLITE_SERIALIZE_NOCOPY and the database has no |
| 1429 | ** contiguous memory representation, in which case `*pOut` will be |
| 1430 | ** NULL but 0 will be returned. |
| 1431 | ** |
| 1432 | ** If `*pOut` is not NULL, the caller is responsible for passing it to |
| 1433 | ** sqlite3_free() to free it. |
| 1434 | */ |
| 1435 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_db_serialize, |
| 1436 | (sqlite3 *pDb, const char *zSchema, |
| 1437 | unsigned char **pOut, |
| 1438 | sqlite3_int64 *nOut, unsigned int mFlags)){ |
| 1439 | unsigned char * z; |
| 1440 | if( !pDb || !pOut ) return SQLITE_MISUSE; |
| 1441 | if( nOut ) *nOut = 0; |
| 1442 | z = sqlite3_serialize(pDb, zSchema ? zSchema : "main", nOut, mFlags); |
| 1443 | if( z || (SQLITE_SERIALIZE_NOCOPY & mFlags) ){ |
| 1444 | *pOut = z; |
| 1445 | return 0; |
| 1446 | }else{ |
| 1447 | return SQLITE_NOMEM; |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1453 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1454 | ** |
| 1455 | ** ACHTUNG: it was discovered on 2023-08-11 that, with SQLITE_DEBUG, |
| 1456 | ** this function's out-of-scope use of the sqlite3_vfs/file/io_methods |
| 1457 | ** APIs leads to triggering of assertions in the core library. Its use |
| 1458 | ** is now deprecated and VFS-specific APIs for importing files need to |
| 1459 | ** be found to replace it. sqlite3__wasm_posix_create_file() is |
| 1460 | ** suitable for the "unix" family of VFSes. |
| 1461 | ** |
| 1462 | ** Creates a new file using the I/O API of the given VFS, containing |
| 1463 | ** the given number of bytes of the given data. If the file exists, it |
| 1464 | ** is truncated to the given length and populated with the given |
| 1465 | ** data. |
| 1466 | ** |
| 1467 | ** This function exists so that we can implement the equivalent of |
| 1468 | ** Emscripten's FS.createDataFile() in a VFS-agnostic way. This |
| 1469 | ** functionality is intended for use in uploading database files. |
| 1470 | ** |
| 1471 | ** Not all VFSes support this functionality, e.g. the "kvvfs" does |
| 1472 | ** not. |
| 1473 | ** |
| 1474 | ** If pVfs is NULL, sqlite3_vfs_find(0) is used. |
| 1475 | ** |
| 1476 | ** If zFile is NULL, pVfs is NULL (and sqlite3_vfs_find(0) returns |
| 1477 | ** NULL), or nData is negative, SQLITE_MISUSE are returned. |
| 1478 | ** |
| 1479 | ** On success, it creates a new file with the given name, populated |
| 1480 | ** with the first nData bytes of pData. If pData is NULL, the file is |
| 1481 | ** created and/or truncated to nData bytes. |
| 1482 | ** |
| 1483 | ** Whether or not directory components of zFilename are created |
| 1484 | ** automatically or not is unspecified: that detail is left to the |
| 1485 | ** VFS. The "opfs" VFS, for example, creates them. |
| 1486 | ** |
| 1487 | ** If an error happens while populating or truncating the file, the |
| 1488 | ** target file will be deleted (if needed) if this function created |
| 1489 | ** it. If this function did not create it, it is not deleted but may |
| 1490 | ** be left in an undefined state. |
| 1491 | ** |
| 1492 | ** Returns 0 on success. On error, it returns a code described above |
| 1493 | ** or propagates a code from one of the I/O methods. |
| 1494 | ** |
| 1495 | ** Design note: nData is an integer, instead of int64, for WASM |
| 1496 | ** portability, so that the API can still work in builds where BigInt |
| 1497 | ** support is disabled or unavailable. |
| 1498 | */ |
| 1499 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_vfs_create_file, |
| 1500 | (sqlite3_vfs *pVfs, const char *zFilename, |
| 1501 | const unsigned char * pData, int nData)){ |
| 1502 | int rc; |
| 1503 | sqlite3_file *pFile = 0; |
| 1504 | sqlite3_io_methods const *pIo; |
| 1505 | const int openFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
| 1506 | #if 0 && defined(SQLITE_DEBUG) |
| 1507 | | SQLITE_OPEN_MAIN_JOURNAL |
| 1508 | /* ^^^^ This is for testing a horrible workaround to avoid |
| 1509 | triggering a specific assert() in os_unix.c:unixOpen(). Please |
| 1510 | do not enable this in real builds. */ |
| 1511 | #endif |
| 1512 | ; |
| 1513 | int flagsOut = 0; |
| 1514 | int fileExisted = 0; |
| 1515 | int doUnlock = 0; |
| 1516 | const unsigned char *pPos = pData; |
| 1517 | const int blockSize = 512 |
| 1518 | /* Because we are using pFile->pMethods->xWrite() for writing, and |
| 1519 | ** it may have a buffer limit related to sqlite3's pager size, we |
| 1520 | ** conservatively write in 512-byte blocks (smallest page |
| 1521 | ** size). */; |
| 1522 | //fprintf(stderr, "pVfs=%p, zFilename=%s, nData=%d\n", pVfs, zFilename, nData); |
| 1523 | if( !pVfs ) pVfs = sqlite3_vfs_find(0); |
| 1524 | if( !pVfs || !zFilename || nData<0 ) return SQLITE_MISUSE; |
| 1525 | pVfs->xAccess(pVfs, zFilename, SQLITE_ACCESS_EXISTS, &fileExisted); |
| 1526 | rc = sqlite3OsOpenMalloc(pVfs, zFilename, &pFile, openFlags, &flagsOut); |
| 1527 | #if 0 |
| 1528 | # define RC fprintf(stderr,"create_file(%s,%s) @%d rc=%d\n", \ |
| 1529 | pVfs->zName, zFilename, __LINE__, rc); |
| 1530 | #else |
| 1531 | # define RC |
| 1532 | #endif |
| 1533 | RC; |
| 1534 | if(rc) return rc; |
| 1535 | pIo = pFile->pMethods; |
| 1536 | if( pIo->xLock ) { |
| 1537 | /* We need xLock() in order to accommodate the OPFS VFS, as it |
| 1538 | ** obtains a writeable handle via the lock operation and releases |
| 1539 | ** it in xUnlock(). If we don't do those here, we have to add code |
| 1540 | ** to the VFS to account check whether it was locked before |
| 1541 | ** xFileSize(), xTruncate(), and the like, and release the lock |
| 1542 | ** only if it was unlocked when the op was started. */ |
| 1543 | rc = pIo->xLock(pFile, SQLITE_LOCK_EXCLUSIVE); |
| 1544 | RC; |
| 1545 | doUnlock = 0==rc; |
| 1546 | } |
| 1547 | if( 0==rc ){ |
| 1548 | rc = pIo->xTruncate(pFile, nData); |
| 1549 | RC; |
| 1550 | } |
| 1551 | if( 0==rc && 0!=pData && nData>0 ){ |
| 1552 | while( 0==rc && nData>0 ){ |
| 1553 | const int n = nData>=blockSize ? blockSize : nData; |
| 1554 | rc = pIo->xWrite(pFile, pPos, n, (sqlite3_int64)(pPos - pData)); |
| 1555 | RC; |
| 1556 | nData -= n; |
| 1557 | pPos += n; |
| 1558 | } |
| 1559 | if( 0==rc && nData>0 ){ |
| 1560 | assert( nData<blockSize ); |
| 1561 | rc = pIo->xWrite(pFile, pPos, nData, |
| 1562 | (sqlite3_int64)(pPos - pData)); |
| 1563 | RC; |
| 1564 | } |
| 1565 | } |
| 1566 | if( pIo->xUnlock && doUnlock!=0 ){ |
| 1567 | pIo->xUnlock(pFile, SQLITE_LOCK_NONE); |
| 1568 | } |
| 1569 | pIo->xClose(pFile); |
| 1570 | if( rc!=0 && 0==fileExisted ){ |
| 1571 | pVfs->xDelete(pVfs, zFilename, 1); |
| 1572 | } |
| 1573 | RC; |
| 1574 | #undef RC |
| 1575 | return rc; |
| 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1580 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1581 | ** |
| 1582 | ** Creates or overwrites a file using the POSIX file API, |
| 1583 | ** i.e. Emscripten's virtual filesystem. Creates or truncates |
| 1584 | ** zFilename, appends pData bytes to it, and returns 0 on success or |
| 1585 | ** SQLITE_IOERR on error. |
| 1586 | */ |
| 1587 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_posix_create_file, |
| 1588 | (const char *zFilename, const unsigned char * pData, |
| 1589 | int nData)){ |
| 1590 | int rc; |
| 1591 | FILE * pFile = 0; |
| 1592 | int fileExisted = 0; |
| 1593 | size_t nWrote = 1; |
| 1594 | |
| 1595 | if( !zFilename || nData<0 || (pData==0 && nData>0) ) return SQLITE_MISUSE; |
| 1596 | pFile = fopen(zFilename, "w"); |
| 1597 | if( 0==pFile ) return SQLITE_IOERR; |
| 1598 | if( nData>0 ){ |
| 1599 | nWrote = fwrite(pData, (size_t)nData, 1, pFile); |
| 1600 | } |
| 1601 | fclose(pFile); |
| 1602 | return 1==nWrote ? 0 : SQLITE_IOERR; |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1607 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1608 | ** |
| 1609 | ** This returns either a pointer to a static buffer or zKeyIn directly |
| 1610 | ** (if zClass is NULL or empty). |
| 1611 | */ |
| 1612 | SQLITE_WASM_EXPORT2(const char *,sqlite3__wasm_kvvfsMakeKey, |
| 1613 | (const char *zClass, const char *zKeyIn)){ |
| 1614 | static char buf[SQLITE_KVOS_SZ+1] = {0}; |
| 1615 | assert(sqlite3KvvfsMethods.nKeySize>32); |
| 1616 | if( zClass && *zClass ){ |
| 1617 | kvrecordMakeKey(zClass, zKeyIn, buf); |
| 1618 | return buf; |
| 1619 | }else{ |
| 1620 | #if 1 |
| 1621 | /* We can return zKeyIn here only because the JS API takes special |
| 1622 | ** care with its lifetime.*/ |
| 1623 | return zKeyIn; |
| 1624 | #else |
| 1625 | /* It would be nice to be able to return zKeyIn directly here, but |
| 1626 | ** it may have been allocated as part of the automated JS-to-WASM |
| 1627 | ** conversions, in which case it will be freed before reaching the |
| 1628 | ** caller. */ |
| 1629 | sqlite3_snprintf(KVRECORD_KEY_SZ, buf, "%s", zKeyIn); |
| 1630 | return buf; |
| 1631 | #endif |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | /* |
| 1636 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1637 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1638 | ** |
| 1639 | ** Returns the pointer to the singleton object which holds the kvvfs |
| 1640 | ** I/O methods and associated state. |
| 1641 | */ |
| 1642 | SQLITE_WASM_EXPORT2(sqlite3_kvvfs_methods *,sqlite3__wasm_kvvfs_methods,(void)){ |
| 1643 | return &sqlite3KvvfsMethods; |
| 1644 | } |
| 1645 | |
| 1646 | #if SQLITE_WASM_HAS_VTAB |
| 1647 | /* |
| 1648 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1649 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1650 | ** |
| 1651 | ** This is a proxy for the variadic sqlite3_vtab_config() which passes |
| 1652 | ** its argument on, or not, to sqlite3_vtab_config(), depending on the |
| 1653 | ** value of its 2nd argument. Returns the result of |
| 1654 | ** sqlite3_vtab_config(), or SQLITE_MISUSE if the 2nd arg is not a |
| 1655 | ** valid value. |
| 1656 | */ |
| 1657 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_vtab_config, |
| 1658 | (sqlite3 *pDb, int op, int arg)){ |
| 1659 | switch(op){ |
| 1660 | case SQLITE_VTAB_DIRECTONLY: |
| 1661 | case SQLITE_VTAB_INNOCUOUS: |
| 1662 | return sqlite3_vtab_config(pDb, op); |
| 1663 | case SQLITE_VTAB_CONSTRAINT_SUPPORT: |
| 1664 | return sqlite3_vtab_config(pDb, op, arg); |
| 1665 | default: |
| 1666 | return SQLITE_MISUSE; |
| 1667 | } |
| 1668 | } |
| 1669 | #endif /*SQLITE_WASM_HAS_VTAB*/ |
| 1670 | |
| 1671 | /* |
| 1672 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1673 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1674 | ** |
| 1675 | ** Wrapper for the variants of sqlite3_db_config() which take |
| 1676 | ** (int,int*) variadic args. |
| 1677 | */ |
| 1678 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_db_config_ip, |
| 1679 | (sqlite3 *pDb, int op, int arg1, int* pArg2)){ |
| 1680 | switch(op){ |
| 1681 | case SQLITE_DBCONFIG_ENABLE_FKEY: |
| 1682 | case SQLITE_DBCONFIG_ENABLE_TRIGGER: |
| 1683 | case SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER: |
| 1684 | case SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION: |
| 1685 | case SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE: |
| 1686 | case SQLITE_DBCONFIG_ENABLE_QPSG: |
| 1687 | case SQLITE_DBCONFIG_TRIGGER_EQP: |
| 1688 | case SQLITE_DBCONFIG_RESET_DATABASE: |
| 1689 | case SQLITE_DBCONFIG_DEFENSIVE: |
| 1690 | case SQLITE_DBCONFIG_WRITABLE_SCHEMA: |
| 1691 | case SQLITE_DBCONFIG_LEGACY_ALTER_TABLE: |
| 1692 | case SQLITE_DBCONFIG_DQS_DML: |
| 1693 | case SQLITE_DBCONFIG_DQS_DDL: |
| 1694 | case SQLITE_DBCONFIG_ENABLE_VIEW: |
| 1695 | case SQLITE_DBCONFIG_LEGACY_FILE_FORMAT: |
| 1696 | case SQLITE_DBCONFIG_TRUSTED_SCHEMA: |
| 1697 | case SQLITE_DBCONFIG_STMT_SCANSTATUS: |
| 1698 | case SQLITE_DBCONFIG_REVERSE_SCANORDER: |
| 1699 | case SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE: |
| 1700 | case SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE: |
| 1701 | case SQLITE_DBCONFIG_ENABLE_COMMENTS: |
| 1702 | case SQLITE_DBCONFIG_FP_DIGITS: |
| 1703 | return sqlite3_db_config(pDb, op, arg1, pArg2); |
| 1704 | default: return SQLITE_MISUSE; |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | /* |
| 1709 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1710 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1711 | ** |
| 1712 | ** Wrapper for the variants of sqlite3_db_config() which take |
| 1713 | ** (void*,int,int) variadic args. |
| 1714 | */ |
| 1715 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_db_config_pii, |
| 1716 | (sqlite3 *pDb, int op, void * pArg1, int arg2, |
| 1717 | int arg3)){ |
| 1718 | switch(op){ |
| 1719 | case SQLITE_DBCONFIG_LOOKASIDE: |
| 1720 | return sqlite3_db_config(pDb, op, pArg1, arg2, arg3); |
| 1721 | default: return SQLITE_MISUSE; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1727 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1728 | ** |
| 1729 | ** Wrapper for the variants of sqlite3_db_config() which take |
| 1730 | ** (const char *) variadic args. |
| 1731 | */ |
| 1732 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_db_config_s,(sqlite3 *pDb, int op, |
| 1733 | const char *zArg)){ |
| 1734 | switch(op){ |
| 1735 | case SQLITE_DBCONFIG_MAINDBNAME: |
| 1736 | return sqlite3_db_config(pDb, op, zArg); |
| 1737 | default: return SQLITE_MISUSE; |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | |
| 1742 | /* |
| 1743 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1744 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1745 | ** |
| 1746 | ** Binding for combinations of sqlite3_config() arguments which take |
| 1747 | ** a single integer argument. |
| 1748 | */ |
| 1749 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_config_i,(int op, int arg)){ |
| 1750 | return sqlite3_config(op, arg); |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1755 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1756 | ** |
| 1757 | ** Binding for combinations of sqlite3_config() arguments which take |
| 1758 | ** two int arguments. |
| 1759 | */ |
| 1760 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_config_ii,(int op, int arg1, int arg2)){ |
| 1761 | return sqlite3_config(op, arg1, arg2); |
| 1762 | } |
| 1763 | |
| 1764 | /* |
| 1765 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1766 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1767 | ** |
| 1768 | ** Binding for combinations of sqlite3_config() arguments which take |
| 1769 | ** a single i64 argument. |
| 1770 | */ |
| 1771 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_config_j,(int op, sqlite3_int64 arg)){ |
| 1772 | return sqlite3_config(op, arg); |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1777 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1778 | ** |
| 1779 | ** If z is not NULL, returns the result of passing z to |
| 1780 | ** sqlite3_mprintf()'s %Q modifier (if addQuotes is true) or %q (if |
| 1781 | ** addQuotes is 0). Returns NULL if z is NULL or on OOM. |
| 1782 | */ |
| 1783 | SQLITE_WASM_EXPORT2(char *,sqlite3__wasm_qfmt_token,(char *z, int addQuotes)){ |
| 1784 | char * rc = 0; |
| 1785 | if( z ){ |
| 1786 | rc = addQuotes |
| 1787 | ? sqlite3_mprintf("%Q", z) |
| 1788 | : sqlite3_mprintf("%q", z); |
| 1789 | } |
| 1790 | return rc; |
| 1791 | } |
| 1792 | |
| 1793 | /* |
| 1794 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1795 | ** for use by the sqlite project's own JS/WASM bindings. |
| 1796 | ** |
| 1797 | ** A WASM wrapper for the interal os_kv.c:kvvfsDecode() for internal |
| 1798 | ** use by the kvvfs v2 API. |
| 1799 | */ |
| 1800 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_kvvfs_decode,(const char *a, char *aOut, int nOut)){ |
| 1801 | return kvvfsDecode(a, aOut, nOut); |
| 1802 | } |
| 1803 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_kvvfs_encode,(const char *a, int nA, char *aOut)){ |
| 1804 | return kvvfsEncode(a, nA, aOut); |
| 1805 | } |
| 1806 | |
| 1807 | |
| 1808 | #if defined(__EMSCRIPTEN__) && defined(SQLITE_ENABLE_WASMFS) |
| 1809 | #include <emscripten/console.h> |
| 1810 | #include <emscripten/wasmfs.h> |
| 1811 | |
| 1812 | /* |
| 1813 | ** This function is NOT part of the sqlite3 public API. It is strictly |
| 1814 | ** for use by the sqlite project's own JS/WASM bindings, specifically |
| 1815 | ** only when building with Emscripten's WASMFS support. |
| 1816 | ** |
| 1817 | ** This function should only be called if the JS side detects the |
| 1818 | ** existence of the Origin-Private FileSystem (OPFS) APIs in the |
| 1819 | ** client. The first time it is called, this function instantiates a |
| 1820 | ** WASMFS backend impl for OPFS. On success, subsequent calls are |
| 1821 | ** no-ops. |
| 1822 | ** |
| 1823 | ** This function may be passed a "mount point" name, which must have a |
| 1824 | ** leading "/" and is currently restricted to a single path component, |
| 1825 | ** e.g. "/foo" is legal but "/foo/" and "/foo/bar" are not. If it is |
| 1826 | ** NULL or empty, it defaults to "/opfs". |
| 1827 | ** |
| 1828 | ** Returns 0 on success, SQLITE_NOMEM if instantiation of the backend |
| 1829 | ** object fails, SQLITE_IOERR if mkdir() of the zMountPoint dir in |
| 1830 | ** the virtual FS fails. In builds compiled without SQLITE_ENABLE_WASMFS |
| 1831 | ** defined, SQLITE_NOTFOUND is returned without side effects. |
| 1832 | */ |
| 1833 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_init_wasmfs,(const char *zMountPoint)){ |
| 1834 | static backend_t pOpfs = 0; |
| 1835 | if( !zMountPoint || !*zMountPoint ) zMountPoint = "/opfs"; |
| 1836 | if( !pOpfs ){ |
| 1837 | pOpfs = wasmfs_create_opfs_backend(); |
| 1838 | } |
| 1839 | /** It's not enough to instantiate the backend. We have to create a |
| 1840 | mountpoint in the VFS and attach the backend to it. */ |
| 1841 | if( pOpfs && 0!=access(zMountPoint, F_OK) ){ |
| 1842 | /* Note that this check and is not robust but it will |
| 1843 | hypothetically suffice for the transient wasm-based virtual |
| 1844 | filesystem we're currently running in. */ |
| 1845 | const int rc = wasmfs_create_directory(zMountPoint, 0777, pOpfs); |
| 1846 | /*emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc);*/ |
| 1847 | if(rc) return SQLITE_IOERR; |
| 1848 | } |
| 1849 | return pOpfs ? 0 : SQLITE_NOMEM; |
| 1850 | } |
| 1851 | #else |
| 1852 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_init_wasmfs,(const char *zUnused)){ |
| 1853 | //emscripten_console_warn("WASMFS OPFS is not compiled in."); |
| 1854 | (void)zUnused; |
| 1855 | return SQLITE_NOTFOUND; |
| 1856 | } |
| 1857 | #endif /* __EMSCRIPTEN__ && SQLITE_ENABLE_WASMFS */ |
| 1858 | |
| 1859 | #if SQLITE_WASM_ENABLE_C_TESTS |
| 1860 | |
| 1861 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_test_intptr,(int * p)){ |
| 1862 | return *p = *p * 2; |
| 1863 | } |
| 1864 | |
| 1865 | SQLITE_WASM_EXPORT2(void *,sqlite3__wasm_test_voidptr,(void * p)){ |
| 1866 | return p; |
| 1867 | } |
| 1868 | |
| 1869 | SQLITE_WASM_EXPORT2(int64_t,sqlite3__wasm_test_int64_max,(void)){ |
| 1870 | return (int64_t)0x7fffffffffffffff; |
| 1871 | } |
| 1872 | |
| 1873 | SQLITE_WASM_EXPORT2(int64_t,sqlite3__wasm_test_int64_min,(void)){ |
| 1874 | return ~sqlite3__wasm_test_int64_max(); |
| 1875 | } |
| 1876 | |
| 1877 | SQLITE_WASM_EXPORT2(int64_t,sqlite3__wasm_test_int64_times2,(int64_t x)){ |
| 1878 | return x * 2; |
| 1879 | } |
| 1880 | |
| 1881 | SQLITE_WASM_EXPORT2(void,sqlite3__wasm_test_int64_minmax,(int64_t * min, int64_t *max)){ |
| 1882 | *max = sqlite3__wasm_test_int64_max(); |
| 1883 | *min = sqlite3__wasm_test_int64_min(); |
| 1884 | /*printf("minmax: min=%lld, max=%lld\n", *min, *max);*/ |
| 1885 | } |
| 1886 | |
| 1887 | SQLITE_WASM_EXPORT2(int64_t,sqlite3__wasm_test_int64ptr,(int64_t * p)){ |
| 1888 | /*printf("sqlite3__wasm_test_int64ptr( @%lld = 0x%llx )\n", (int64_t)p, *p);*/ |
| 1889 | return *p = *p * 2; |
| 1890 | } |
| 1891 | |
| 1892 | SQLITE_WASM_EXPORT2(void,sqlite3__wasm_test_stack_overflow,(int recurse)){ |
| 1893 | if(recurse) sqlite3__wasm_test_stack_overflow(recurse); |
| 1894 | } |
| 1895 | |
| 1896 | /* For testing the 'string:dealloc' whwasmutil.xWrap() conversion. */ |
| 1897 | SQLITE_WASM_EXPORT2(char *,sqlite3__wasm_test_str_hello,(int fail)){ |
| 1898 | char * s = fail ? 0 : (char *)sqlite3_malloc(6); |
| 1899 | if(s){ |
| 1900 | memcpy(s, "hello", 5); |
| 1901 | s[5] = 0; |
| 1902 | } |
| 1903 | return s; |
| 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | ** For testing using SQLTester scripts. |
| 1908 | ** |
| 1909 | ** Return non-zero if string z matches glob pattern zGlob and zero if the |
| 1910 | ** pattern does not match. |
| 1911 | ** |
| 1912 | ** To repeat: |
| 1913 | ** |
| 1914 | ** zero == no match |
| 1915 | ** non-zero == match |
| 1916 | ** |
| 1917 | ** Globbing rules: |
| 1918 | ** |
| 1919 | ** '*' Matches any sequence of zero or more characters. |
| 1920 | ** |
| 1921 | ** '?' Matches exactly one character. |
| 1922 | ** |
| 1923 | ** [...] Matches one character from the enclosed list of |
| 1924 | ** characters. |
| 1925 | ** |
| 1926 | ** [^...] Matches one character not in the enclosed list. |
| 1927 | ** |
| 1928 | ** '#' Matches any sequence of one or more digits with an |
| 1929 | ** optional + or - sign in front, or a hexadecimal |
| 1930 | ** literal of the form 0x... |
| 1931 | */ |
| 1932 | static int sqlite3__wasm_SQLTester_strnotglob(const char *zGlob, const char *z){ |
| 1933 | int c, c2; |
| 1934 | int invert; |
| 1935 | int seen; |
| 1936 | typedef int (*recurse_f)(const char *,const char *); |
| 1937 | static const recurse_f recurse = sqlite3__wasm_SQLTester_strnotglob; |
| 1938 | |
| 1939 | while( (c = (*(zGlob++)))!=0 ){ |
| 1940 | if( c=='*' ){ |
| 1941 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 1942 | if( c=='?' && (*(z++))==0 ) return 0; |
| 1943 | } |
| 1944 | if( c==0 ){ |
| 1945 | return 1; |
| 1946 | }else if( c=='[' ){ |
| 1947 | while( *z && recurse(zGlob-1,z)==0 ){ |
| 1948 | z++; |
| 1949 | } |
| 1950 | return (*z)!=0; |
| 1951 | } |
| 1952 | while( (c2 = (*(z++)))!=0 ){ |
| 1953 | while( c2!=c ){ |
| 1954 | c2 = *(z++); |
| 1955 | if( c2==0 ) return 0; |
| 1956 | } |
| 1957 | if( recurse(zGlob,z) ) return 1; |
| 1958 | } |
| 1959 | return 0; |
| 1960 | }else if( c=='?' ){ |
| 1961 | if( (*(z++))==0 ) return 0; |
| 1962 | }else if( c=='[' ){ |
| 1963 | int prior_c = 0; |
| 1964 | seen = 0; |
| 1965 | invert = 0; |
| 1966 | c = *(z++); |
| 1967 | if( c==0 ) return 0; |
| 1968 | c2 = *(zGlob++); |
| 1969 | if( c2=='^' ){ |
| 1970 | invert = 1; |
| 1971 | c2 = *(zGlob++); |
| 1972 | } |
| 1973 | if( c2==']' ){ |
| 1974 | if( c==']' ) seen = 1; |
| 1975 | c2 = *(zGlob++); |
| 1976 | } |
| 1977 | while( c2 && c2!=']' ){ |
| 1978 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 1979 | c2 = *(zGlob++); |
| 1980 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 1981 | prior_c = 0; |
| 1982 | }else{ |
| 1983 | if( c==c2 ){ |
| 1984 | seen = 1; |
| 1985 | } |
| 1986 | prior_c = c2; |
| 1987 | } |
| 1988 | c2 = *(zGlob++); |
| 1989 | } |
| 1990 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 1991 | }else if( c=='#' ){ |
| 1992 | if( z[0]=='0' |
| 1993 | && (z[1]=='x' || z[1]=='X') |
| 1994 | && sqlite3Isxdigit(z[2]) |
| 1995 | ){ |
| 1996 | z += 3; |
| 1997 | while( sqlite3Isxdigit(z[0]) ){ z++; } |
| 1998 | }else{ |
| 1999 | if( (z[0]=='-' || z[0]=='+') && sqlite3Isdigit(z[1]) ) z++; |
| 2000 | if( !sqlite3Isdigit(z[0]) ) return 0; |
| 2001 | z++; |
| 2002 | while( sqlite3Isdigit(z[0]) ){ z++; } |
| 2003 | } |
| 2004 | }else{ |
| 2005 | if( c!=(*(z++)) ) return 0; |
| 2006 | } |
| 2007 | } |
| 2008 | return *z==0; |
| 2009 | } |
| 2010 | |
| 2011 | SQLITE_WASM_EXPORT2(int,sqlite3__wasm_SQLTester_strglob, |
| 2012 | (const char *zGlob, const char *z)){ |
| 2013 | return !sqlite3__wasm_SQLTester_strnotglob(zGlob, z); |
| 2014 | } |
| 2015 | |
| 2016 | #endif /* SQLITE_WASM_ENABLE_C_TESTS */ |
| 2017 | |
| 2018 | #undef SQLITE_WASM_EXPORT |
| 2019 | #undef SQLITE_WASM_HAS_VTAB |
| 2020 | #undef SQLITE_WASM_BARE_BONES |
| 2021 | #undef SQLITE_WASM_ENABLE_C_TESTS |