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