Snapshot of upstream SQLite 3.44.2
[sqlcipher.git] / ext / wasm / api / sqlite3-wasm.c
blob300307e5ea9a8b38ae7af1b27e30bcb06e5e90b3
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_T... */
152 #ifndef SQLITE_TEMP_STORE
153 # define SQLITE_TEMP_STORE 2
154 #endif
155 #ifndef SQLITE_THREADSAFE
156 # define SQLITE_THREADSAFE 0
157 #endif
159 /**********************************************************************/
160 /* SQLITE_USE_... */
161 #ifndef SQLITE_USE_URI
162 # define SQLITE_USE_URI 1
163 #endif
165 #ifdef SQLITE_WASM_EXTRA_INIT
166 # define SQLITE_EXTRA_INIT sqlite3_wasm_extra_init
167 #endif
169 #include <assert.h>
172 ** SQLITE_WASM_EXPORT is functionally identical to EMSCRIPTEN_KEEPALIVE
173 ** but is not Emscripten-specific. It explicitly marks functions for
174 ** export into the target wasm file without requiring explicit listing
175 ** of those functions in Emscripten's -sEXPORTED_FUNCTIONS=... list
176 ** (or equivalent in other build platforms). Any function with neither
177 ** this attribute nor which is listed as an explicit export will not
178 ** be exported from the wasm file (but may still be used internally
179 ** within the wasm file).
181 ** The functions in this file (sqlite3-wasm.c) which require exporting
182 ** are marked with this flag. They may also be added to any explicit
183 ** build-time export list but need not be. All of these APIs are
184 ** intended for use only within the project's own JS/WASM code, and
185 ** not by client code, so an argument can be made for reducing their
186 ** visibility by not including them in any build-time export lists.
188 ** 2022-09-11: it's not yet _proven_ that this approach works in
189 ** non-Emscripten builds. If not, such builds will need to export
190 ** those using the --export=... wasm-ld flag (or equivalent). As of
191 ** this writing we are tied to Emscripten for various reasons
192 ** and cannot test the library with other build environments.
194 #define SQLITE_WASM_EXPORT __attribute__((used,visibility("default")))
195 // See also:
196 //__attribute__((export_name("theExportedName"), used, visibility("default")))
199 ** Which sqlite3.c we're using needs to be configurable to enable
200 ** building against a custom copy, e.g. the SEE variant. Note that we
201 ** #include the .c file, rather than the header, so that the WASM
202 ** extensions have access to private API internals.
204 ** The caveat here is that custom variants need to account for
205 ** exporting any necessary symbols (e.g. sqlite3_activate_see()). We
206 ** cannot export them from here using SQLITE_WASM_EXPORT because that
207 ** attribute (apparently) has to be part of the function definition.
209 #ifndef SQLITE_C
210 # define SQLITE_C sqlite3.c /* yes, .c instead of .h. */
211 #endif
212 #define INC__STRINGIFY_(f) #f
213 #define INC__STRINGIFY(f) INC__STRINGIFY_(f)
214 #include INC__STRINGIFY(SQLITE_C)
215 #undef INC__STRINGIFY_
216 #undef INC__STRINGIFY
217 #undef SQLITE_C
219 #if defined(__EMSCRIPTEN__)
220 # include <emscripten/console.h>
221 #endif
223 #if 0
225 ** An EXPERIMENT in implementing a stack-based allocator analog to
226 ** Emscripten's stackSave(), stackAlloc(), stackRestore().
227 ** Unfortunately, this cannot work together with Emscripten because
228 ** Emscripten defines its own native one and we'd stomp on each
229 ** other's memory. Other than that complication, basic tests show it
230 ** to work just fine.
232 ** Another option is to malloc() a chunk of our own and call that our
233 ** "stack".
235 SQLITE_WASM_EXPORT void * sqlite3_wasm_stack_end(void){
236 extern void __heap_base
237 /* see https://stackoverflow.com/questions/10038964 */;
238 return &__heap_base;
240 SQLITE_WASM_EXPORT void * sqlite3_wasm_stack_begin(void){
241 extern void __data_end;
242 return &__data_end;
244 static void * pWasmStackPtr = 0;
245 SQLITE_WASM_EXPORT void * sqlite3_wasm_stack_ptr(void){
246 if(!pWasmStackPtr) pWasmStackPtr = sqlite3_wasm_stack_end();
247 return pWasmStackPtr;
249 SQLITE_WASM_EXPORT void sqlite3_wasm_stack_restore(void * p){
250 pWasmStackPtr = p;
252 SQLITE_WASM_EXPORT void * sqlite3_wasm_stack_alloc(int n){
253 if(n<=0) return 0;
254 n = (n + 7) & ~7 /* align to 8-byte boundary */;
255 unsigned char * const p = (unsigned char *)sqlite3_wasm_stack_ptr();
256 unsigned const char * const b = (unsigned const char *)sqlite3_wasm_stack_begin();
257 if(b + n >= p || b + n < b/*overflow*/) return 0;
258 return pWasmStackPtr = p - n;
260 #endif /* stack allocator experiment */
263 ** State for the "pseudo-stack" allocator implemented in
264 ** sqlite3_wasm_pstack_xyz(). In order to avoid colliding with
265 ** Emscripten-controled stack space, it carves out a bit of stack
266 ** memory to use for that purpose. This memory ends up in the
267 ** WASM-managed memory, such that routines which manipulate the wasm
268 ** heap can also be used to manipulate this memory.
270 ** This particular allocator is intended for small allocations such as
271 ** storage for output pointers. We cannot reasonably size it large
272 ** enough for general-purpose string conversions because some of our
273 ** tests use input files (strings) of 16MB+.
275 static unsigned char PStack_mem[512 * 8] = {0};
276 static struct {
277 unsigned const char * const pBegin;/* Start (inclusive) of memory */
278 unsigned const char * const pEnd; /* One-after-the-end of memory */
279 unsigned char * pPos; /* Current stack pointer */
280 } PStack = {
281 &PStack_mem[0],
282 &PStack_mem[0] + sizeof(PStack_mem),
283 &PStack_mem[0] + sizeof(PStack_mem)
286 ** Returns the current pstack position.
288 SQLITE_WASM_EXPORT void * sqlite3_wasm_pstack_ptr(void){
289 return PStack.pPos;
292 ** Sets the pstack position poitner to p. Results are undefined if the
293 ** given value did not come from sqlite3_wasm_pstack_ptr().
295 SQLITE_WASM_EXPORT void sqlite3_wasm_pstack_restore(unsigned char * p){
296 assert(p>=PStack.pBegin && p<=PStack.pEnd && p>=PStack.pPos);
297 assert(0==((unsigned long long)p & 0x7));
298 if(p>=PStack.pBegin && p<=PStack.pEnd /*&& p>=PStack.pPos*/){
299 PStack.pPos = p;
303 ** Allocate and zero out n bytes from the pstack. Returns a pointer to
304 ** the memory on success, 0 on error (including a negative n value). n
305 ** is always adjusted to be a multiple of 8 and returned memory is
306 ** always zeroed out before returning (because this keeps the client
307 ** JS code from having to do so, and most uses of the pstack will
308 ** call for doing so).
310 SQLITE_WASM_EXPORT void * sqlite3_wasm_pstack_alloc(int n){
311 if( n<=0 ) return 0;
312 //if( n & 0x7 ) n += 8 - (n & 0x7) /* align to 8-byte boundary */;
313 n = (n + 7) & ~7 /* align to 8-byte boundary */;
314 if( PStack.pBegin + n > PStack.pPos /*not enough space left*/
315 || PStack.pBegin + n <= PStack.pBegin /*overflow*/ ) return 0;
316 memset((PStack.pPos = PStack.pPos - n), 0, (unsigned int)n);
317 return PStack.pPos;
320 ** Return the number of bytes left which can be
321 ** sqlite3_wasm_pstack_alloc()'d.
323 SQLITE_WASM_EXPORT int sqlite3_wasm_pstack_remaining(void){
324 assert(PStack.pPos >= PStack.pBegin);
325 assert(PStack.pPos <= PStack.pEnd);
326 return (int)(PStack.pPos - PStack.pBegin);
330 ** Return the total number of bytes available in the pstack, including
331 ** any space which is currently allocated. This value is a
332 ** compile-time constant.
334 SQLITE_WASM_EXPORT int sqlite3_wasm_pstack_quota(void){
335 return (int)(PStack.pEnd - PStack.pBegin);
339 ** This function is NOT part of the sqlite3 public API. It is strictly
340 ** for use by the sqlite project's own JS/WASM bindings.
342 ** For purposes of certain hand-crafted C/Wasm function bindings, we
343 ** need a way of reporting errors which is consistent with the rest of
344 ** the C API, as opposed to throwing JS exceptions. To that end, this
345 ** internal-use-only function is a thin proxy around
346 ** sqlite3ErrorWithMessage(). The intent is that it only be used from
347 ** Wasm bindings such as sqlite3_prepare_v2/v3(), and definitely not
348 ** from client code.
350 ** Returns err_code.
352 SQLITE_WASM_EXPORT
353 int sqlite3_wasm_db_error(sqlite3*db, int err_code, const char *zMsg){
354 if( db!=0 ){
355 if( 0!=zMsg ){
356 const int nMsg = sqlite3Strlen30(zMsg);
357 sqlite3_mutex_enter(sqlite3_db_mutex(db));
358 sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg);
359 sqlite3_mutex_leave(sqlite3_db_mutex(db));
360 }else{
361 sqlite3ErrorWithMsg(db, err_code, NULL);
364 return err_code;
367 #if SQLITE_WASM_TESTS
368 struct WasmTestStruct {
369 int v4;
370 void * ppV;
371 const char * cstr;
372 int64_t v8;
373 void (*xFunc)(void*);
375 typedef struct WasmTestStruct WasmTestStruct;
376 SQLITE_WASM_EXPORT
377 void sqlite3_wasm_test_struct(WasmTestStruct * s){
378 if(s){
379 s->v4 *= 2;
380 s->v8 = s->v4 * 2;
381 s->ppV = s;
382 s->cstr = __FILE__;
383 if(s->xFunc) s->xFunc(s);
385 return;
387 #endif /* SQLITE_WASM_TESTS */
390 ** This function is NOT part of the sqlite3 public API. It is strictly
391 ** for use by the sqlite project's own JS/WASM bindings. Unlike the
392 ** rest of the sqlite3 API, this part requires C99 for snprintf() and
393 ** variadic macros.
395 ** Returns a string containing a JSON-format "enum" of C-level
396 ** constants and struct-related metadata intended to be imported into
397 ** the JS environment. The JSON is initialized the first time this
398 ** function is called and that result is reused for all future calls.
400 ** If this function returns NULL then it means that the internal
401 ** buffer is not large enough for the generated JSON and needs to be
402 ** increased. In debug builds that will trigger an assert().
404 SQLITE_WASM_EXPORT
405 const char * sqlite3_wasm_enum_json(void){
406 static char aBuffer[1024 * 20] = {0} /* where the JSON goes */;
407 int n = 0, nChildren = 0, nStruct = 0
408 /* output counters for figuring out where commas go */;
409 char * zPos = &aBuffer[1] /* skip first byte for now to help protect
410 ** against a small race condition */;
411 char const * const zEnd = &aBuffer[0] + sizeof(aBuffer) /* one-past-the-end */;
412 if(aBuffer[0]) return aBuffer;
413 /* Leave aBuffer[0] at 0 until the end to help guard against a tiny
414 ** race condition. If this is called twice concurrently, they might
415 ** end up both writing to aBuffer, but they'll both write the same
416 ** thing, so that's okay. If we set byte 0 up front then the 2nd
417 ** instance might return and use the string before the 1st instance
418 ** is done filling it. */
420 /* Core output macros... */
421 #define lenCheck assert(zPos < zEnd - 128 \
422 && "sqlite3_wasm_enum_json() buffer is too small."); \
423 if( zPos >= zEnd - 128 ) return 0
424 #define outf(format,...) \
425 zPos += snprintf(zPos, ((size_t)(zEnd - zPos)), format, __VA_ARGS__); \
426 lenCheck
427 #define out(TXT) outf("%s",TXT)
428 #define CloseBrace(LEVEL) \
429 assert(LEVEL<5); memset(zPos, '}', LEVEL); zPos+=LEVEL; lenCheck
431 /* Macros for emitting maps of integer- and string-type macros to
432 ** their values. */
433 #define DefGroup(KEY) n = 0; \
434 outf("%s\"" #KEY "\": {",(nChildren++ ? "," : ""));
435 #define DefInt(KEY) \
436 outf("%s\"%s\": %d", (n++ ? ", " : ""), #KEY, (int)KEY)
437 #define DefStr(KEY) \
438 outf("%s\"%s\": \"%s\"", (n++ ? ", " : ""), #KEY, KEY)
439 #define _DefGroup CloseBrace(1)
441 /* The following groups are sorted alphabetic by group name. */
442 DefGroup(access){
443 DefInt(SQLITE_ACCESS_EXISTS);
444 DefInt(SQLITE_ACCESS_READWRITE);
445 DefInt(SQLITE_ACCESS_READ)/*docs say this is unused*/;
446 } _DefGroup;
448 DefGroup(authorizer){
449 DefInt(SQLITE_DENY);
450 DefInt(SQLITE_IGNORE);
451 DefInt(SQLITE_CREATE_INDEX);
452 DefInt(SQLITE_CREATE_TABLE);
453 DefInt(SQLITE_CREATE_TEMP_INDEX);
454 DefInt(SQLITE_CREATE_TEMP_TABLE);
455 DefInt(SQLITE_CREATE_TEMP_TRIGGER);
456 DefInt(SQLITE_CREATE_TEMP_VIEW);
457 DefInt(SQLITE_CREATE_TRIGGER);
458 DefInt(SQLITE_CREATE_VIEW);
459 DefInt(SQLITE_DELETE);
460 DefInt(SQLITE_DROP_INDEX);
461 DefInt(SQLITE_DROP_TABLE);
462 DefInt(SQLITE_DROP_TEMP_INDEX);
463 DefInt(SQLITE_DROP_TEMP_TABLE);
464 DefInt(SQLITE_DROP_TEMP_TRIGGER);
465 DefInt(SQLITE_DROP_TEMP_VIEW);
466 DefInt(SQLITE_DROP_TRIGGER);
467 DefInt(SQLITE_DROP_VIEW);
468 DefInt(SQLITE_INSERT);
469 DefInt(SQLITE_PRAGMA);
470 DefInt(SQLITE_READ);
471 DefInt(SQLITE_SELECT);
472 DefInt(SQLITE_TRANSACTION);
473 DefInt(SQLITE_UPDATE);
474 DefInt(SQLITE_ATTACH);
475 DefInt(SQLITE_DETACH);
476 DefInt(SQLITE_ALTER_TABLE);
477 DefInt(SQLITE_REINDEX);
478 DefInt(SQLITE_ANALYZE);
479 DefInt(SQLITE_CREATE_VTABLE);
480 DefInt(SQLITE_DROP_VTABLE);
481 DefInt(SQLITE_FUNCTION);
482 DefInt(SQLITE_SAVEPOINT);
483 //DefInt(SQLITE_COPY) /* No longer used */;
484 DefInt(SQLITE_RECURSIVE);
485 } _DefGroup;
487 DefGroup(blobFinalizers) {
488 /* SQLITE_STATIC/TRANSIENT need to be handled explicitly as
489 ** integers to avoid casting-related warnings. */
490 out("\"SQLITE_STATIC\":0, \"SQLITE_TRANSIENT\":-1");
491 outf(",\"SQLITE_WASM_DEALLOC\": %lld",
492 (sqlite3_int64)(sqlite3_free));
493 } _DefGroup;
495 DefGroup(changeset){
496 DefInt(SQLITE_CHANGESETSTART_INVERT);
497 DefInt(SQLITE_CHANGESETAPPLY_NOSAVEPOINT);
498 DefInt(SQLITE_CHANGESETAPPLY_INVERT);
499 DefInt(SQLITE_CHANGESETAPPLY_IGNORENOOP);
501 DefInt(SQLITE_CHANGESET_DATA);
502 DefInt(SQLITE_CHANGESET_NOTFOUND);
503 DefInt(SQLITE_CHANGESET_CONFLICT);
504 DefInt(SQLITE_CHANGESET_CONSTRAINT);
505 DefInt(SQLITE_CHANGESET_FOREIGN_KEY);
507 DefInt(SQLITE_CHANGESET_OMIT);
508 DefInt(SQLITE_CHANGESET_REPLACE);
509 DefInt(SQLITE_CHANGESET_ABORT);
510 } _DefGroup;
512 DefGroup(config){
513 DefInt(SQLITE_CONFIG_SINGLETHREAD);
514 DefInt(SQLITE_CONFIG_MULTITHREAD);
515 DefInt(SQLITE_CONFIG_SERIALIZED);
516 DefInt(SQLITE_CONFIG_MALLOC);
517 DefInt(SQLITE_CONFIG_GETMALLOC);
518 DefInt(SQLITE_CONFIG_SCRATCH);
519 DefInt(SQLITE_CONFIG_PAGECACHE);
520 DefInt(SQLITE_CONFIG_HEAP);
521 DefInt(SQLITE_CONFIG_MEMSTATUS);
522 DefInt(SQLITE_CONFIG_MUTEX);
523 DefInt(SQLITE_CONFIG_GETMUTEX);
524 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
525 DefInt(SQLITE_CONFIG_LOOKASIDE);
526 DefInt(SQLITE_CONFIG_PCACHE);
527 DefInt(SQLITE_CONFIG_GETPCACHE);
528 DefInt(SQLITE_CONFIG_LOG);
529 DefInt(SQLITE_CONFIG_URI);
530 DefInt(SQLITE_CONFIG_PCACHE2);
531 DefInt(SQLITE_CONFIG_GETPCACHE2);
532 DefInt(SQLITE_CONFIG_COVERING_INDEX_SCAN);
533 DefInt(SQLITE_CONFIG_SQLLOG);
534 DefInt(SQLITE_CONFIG_MMAP_SIZE);
535 DefInt(SQLITE_CONFIG_WIN32_HEAPSIZE);
536 DefInt(SQLITE_CONFIG_PCACHE_HDRSZ);
537 DefInt(SQLITE_CONFIG_PMASZ);
538 DefInt(SQLITE_CONFIG_STMTJRNL_SPILL);
539 DefInt(SQLITE_CONFIG_SMALL_MALLOC);
540 DefInt(SQLITE_CONFIG_SORTERREF_SIZE);
541 DefInt(SQLITE_CONFIG_MEMDB_MAXSIZE);
542 } _DefGroup;
544 DefGroup(dataTypes) {
545 DefInt(SQLITE_INTEGER);
546 DefInt(SQLITE_FLOAT);
547 DefInt(SQLITE_TEXT);
548 DefInt(SQLITE_BLOB);
549 DefInt(SQLITE_NULL);
550 } _DefGroup;
552 DefGroup(dbConfig){
553 DefInt(SQLITE_DBCONFIG_MAINDBNAME);
554 DefInt(SQLITE_DBCONFIG_LOOKASIDE);
555 DefInt(SQLITE_DBCONFIG_ENABLE_FKEY);
556 DefInt(SQLITE_DBCONFIG_ENABLE_TRIGGER);
557 DefInt(SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER);
558 DefInt(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION);
559 DefInt(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE);
560 DefInt(SQLITE_DBCONFIG_ENABLE_QPSG);
561 DefInt(SQLITE_DBCONFIG_TRIGGER_EQP);
562 DefInt(SQLITE_DBCONFIG_RESET_DATABASE);
563 DefInt(SQLITE_DBCONFIG_DEFENSIVE);
564 DefInt(SQLITE_DBCONFIG_WRITABLE_SCHEMA);
565 DefInt(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE);
566 DefInt(SQLITE_DBCONFIG_DQS_DML);
567 DefInt(SQLITE_DBCONFIG_DQS_DDL);
568 DefInt(SQLITE_DBCONFIG_ENABLE_VIEW);
569 DefInt(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT);
570 DefInt(SQLITE_DBCONFIG_TRUSTED_SCHEMA);
571 DefInt(SQLITE_DBCONFIG_STMT_SCANSTATUS);
572 DefInt(SQLITE_DBCONFIG_REVERSE_SCANORDER);
573 DefInt(SQLITE_DBCONFIG_MAX);
574 } _DefGroup;
576 DefGroup(dbStatus){
577 DefInt(SQLITE_DBSTATUS_LOOKASIDE_USED);
578 DefInt(SQLITE_DBSTATUS_CACHE_USED);
579 DefInt(SQLITE_DBSTATUS_SCHEMA_USED);
580 DefInt(SQLITE_DBSTATUS_STMT_USED);
581 DefInt(SQLITE_DBSTATUS_LOOKASIDE_HIT);
582 DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE);
583 DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL);
584 DefInt(SQLITE_DBSTATUS_CACHE_HIT);
585 DefInt(SQLITE_DBSTATUS_CACHE_MISS);
586 DefInt(SQLITE_DBSTATUS_CACHE_WRITE);
587 DefInt(SQLITE_DBSTATUS_DEFERRED_FKS);
588 DefInt(SQLITE_DBSTATUS_CACHE_USED_SHARED);
589 DefInt(SQLITE_DBSTATUS_CACHE_SPILL);
590 DefInt(SQLITE_DBSTATUS_MAX);
591 } _DefGroup;
593 DefGroup(encodings) {
594 /* Noting that the wasm binding only aims to support UTF-8. */
595 DefInt(SQLITE_UTF8);
596 DefInt(SQLITE_UTF16LE);
597 DefInt(SQLITE_UTF16BE);
598 DefInt(SQLITE_UTF16);
599 /*deprecated DefInt(SQLITE_ANY); */
600 DefInt(SQLITE_UTF16_ALIGNED);
601 } _DefGroup;
603 DefGroup(fcntl) {
604 DefInt(SQLITE_FCNTL_LOCKSTATE);
605 DefInt(SQLITE_FCNTL_GET_LOCKPROXYFILE);
606 DefInt(SQLITE_FCNTL_SET_LOCKPROXYFILE);
607 DefInt(SQLITE_FCNTL_LAST_ERRNO);
608 DefInt(SQLITE_FCNTL_SIZE_HINT);
609 DefInt(SQLITE_FCNTL_CHUNK_SIZE);
610 DefInt(SQLITE_FCNTL_FILE_POINTER);
611 DefInt(SQLITE_FCNTL_SYNC_OMITTED);
612 DefInt(SQLITE_FCNTL_WIN32_AV_RETRY);
613 DefInt(SQLITE_FCNTL_PERSIST_WAL);
614 DefInt(SQLITE_FCNTL_OVERWRITE);
615 DefInt(SQLITE_FCNTL_VFSNAME);
616 DefInt(SQLITE_FCNTL_POWERSAFE_OVERWRITE);
617 DefInt(SQLITE_FCNTL_PRAGMA);
618 DefInt(SQLITE_FCNTL_BUSYHANDLER);
619 DefInt(SQLITE_FCNTL_TEMPFILENAME);
620 DefInt(SQLITE_FCNTL_MMAP_SIZE);
621 DefInt(SQLITE_FCNTL_TRACE);
622 DefInt(SQLITE_FCNTL_HAS_MOVED);
623 DefInt(SQLITE_FCNTL_SYNC);
624 DefInt(SQLITE_FCNTL_COMMIT_PHASETWO);
625 DefInt(SQLITE_FCNTL_WIN32_SET_HANDLE);
626 DefInt(SQLITE_FCNTL_WAL_BLOCK);
627 DefInt(SQLITE_FCNTL_ZIPVFS);
628 DefInt(SQLITE_FCNTL_RBU);
629 DefInt(SQLITE_FCNTL_VFS_POINTER);
630 DefInt(SQLITE_FCNTL_JOURNAL_POINTER);
631 DefInt(SQLITE_FCNTL_WIN32_GET_HANDLE);
632 DefInt(SQLITE_FCNTL_PDB);
633 DefInt(SQLITE_FCNTL_BEGIN_ATOMIC_WRITE);
634 DefInt(SQLITE_FCNTL_COMMIT_ATOMIC_WRITE);
635 DefInt(SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE);
636 DefInt(SQLITE_FCNTL_LOCK_TIMEOUT);
637 DefInt(SQLITE_FCNTL_DATA_VERSION);
638 DefInt(SQLITE_FCNTL_SIZE_LIMIT);
639 DefInt(SQLITE_FCNTL_CKPT_DONE);
640 DefInt(SQLITE_FCNTL_RESERVE_BYTES);
641 DefInt(SQLITE_FCNTL_CKPT_START);
642 DefInt(SQLITE_FCNTL_EXTERNAL_READER);
643 DefInt(SQLITE_FCNTL_CKSM_FILE);
644 DefInt(SQLITE_FCNTL_RESET_CACHE);
645 } _DefGroup;
647 DefGroup(flock) {
648 DefInt(SQLITE_LOCK_NONE);
649 DefInt(SQLITE_LOCK_SHARED);
650 DefInt(SQLITE_LOCK_RESERVED);
651 DefInt(SQLITE_LOCK_PENDING);
652 DefInt(SQLITE_LOCK_EXCLUSIVE);
653 } _DefGroup;
655 DefGroup(ioCap) {
656 DefInt(SQLITE_IOCAP_ATOMIC);
657 DefInt(SQLITE_IOCAP_ATOMIC512);
658 DefInt(SQLITE_IOCAP_ATOMIC1K);
659 DefInt(SQLITE_IOCAP_ATOMIC2K);
660 DefInt(SQLITE_IOCAP_ATOMIC4K);
661 DefInt(SQLITE_IOCAP_ATOMIC8K);
662 DefInt(SQLITE_IOCAP_ATOMIC16K);
663 DefInt(SQLITE_IOCAP_ATOMIC32K);
664 DefInt(SQLITE_IOCAP_ATOMIC64K);
665 DefInt(SQLITE_IOCAP_SAFE_APPEND);
666 DefInt(SQLITE_IOCAP_SEQUENTIAL);
667 DefInt(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN);
668 DefInt(SQLITE_IOCAP_POWERSAFE_OVERWRITE);
669 DefInt(SQLITE_IOCAP_IMMUTABLE);
670 DefInt(SQLITE_IOCAP_BATCH_ATOMIC);
671 } _DefGroup;
673 DefGroup(limits) {
674 DefInt(SQLITE_MAX_ALLOCATION_SIZE);
675 DefInt(SQLITE_LIMIT_LENGTH);
676 DefInt(SQLITE_MAX_LENGTH);
677 DefInt(SQLITE_LIMIT_SQL_LENGTH);
678 DefInt(SQLITE_MAX_SQL_LENGTH);
679 DefInt(SQLITE_LIMIT_COLUMN);
680 DefInt(SQLITE_MAX_COLUMN);
681 DefInt(SQLITE_LIMIT_EXPR_DEPTH);
682 DefInt(SQLITE_MAX_EXPR_DEPTH);
683 DefInt(SQLITE_LIMIT_COMPOUND_SELECT);
684 DefInt(SQLITE_MAX_COMPOUND_SELECT);
685 DefInt(SQLITE_LIMIT_VDBE_OP);
686 DefInt(SQLITE_MAX_VDBE_OP);
687 DefInt(SQLITE_LIMIT_FUNCTION_ARG);
688 DefInt(SQLITE_MAX_FUNCTION_ARG);
689 DefInt(SQLITE_LIMIT_ATTACHED);
690 DefInt(SQLITE_MAX_ATTACHED);
691 DefInt(SQLITE_LIMIT_LIKE_PATTERN_LENGTH);
692 DefInt(SQLITE_MAX_LIKE_PATTERN_LENGTH);
693 DefInt(SQLITE_LIMIT_VARIABLE_NUMBER);
694 DefInt(SQLITE_MAX_VARIABLE_NUMBER);
695 DefInt(SQLITE_LIMIT_TRIGGER_DEPTH);
696 DefInt(SQLITE_MAX_TRIGGER_DEPTH);
697 DefInt(SQLITE_LIMIT_WORKER_THREADS);
698 DefInt(SQLITE_MAX_WORKER_THREADS);
699 } _DefGroup;
701 DefGroup(openFlags) {
702 /* Noting that not all of these will have any effect in
703 ** WASM-space. */
704 DefInt(SQLITE_OPEN_READONLY);
705 DefInt(SQLITE_OPEN_READWRITE);
706 DefInt(SQLITE_OPEN_CREATE);
707 DefInt(SQLITE_OPEN_URI);
708 DefInt(SQLITE_OPEN_MEMORY);
709 DefInt(SQLITE_OPEN_NOMUTEX);
710 DefInt(SQLITE_OPEN_FULLMUTEX);
711 DefInt(SQLITE_OPEN_SHAREDCACHE);
712 DefInt(SQLITE_OPEN_PRIVATECACHE);
713 DefInt(SQLITE_OPEN_EXRESCODE);
714 DefInt(SQLITE_OPEN_NOFOLLOW);
715 /* OPEN flags for use with VFSes... */
716 DefInt(SQLITE_OPEN_MAIN_DB);
717 DefInt(SQLITE_OPEN_MAIN_JOURNAL);
718 DefInt(SQLITE_OPEN_TEMP_DB);
719 DefInt(SQLITE_OPEN_TEMP_JOURNAL);
720 DefInt(SQLITE_OPEN_TRANSIENT_DB);
721 DefInt(SQLITE_OPEN_SUBJOURNAL);
722 DefInt(SQLITE_OPEN_SUPER_JOURNAL);
723 DefInt(SQLITE_OPEN_WAL);
724 DefInt(SQLITE_OPEN_DELETEONCLOSE);
725 DefInt(SQLITE_OPEN_EXCLUSIVE);
726 } _DefGroup;
728 DefGroup(prepareFlags) {
729 DefInt(SQLITE_PREPARE_PERSISTENT);
730 DefInt(SQLITE_PREPARE_NORMALIZE);
731 DefInt(SQLITE_PREPARE_NO_VTAB);
732 } _DefGroup;
734 DefGroup(resultCodes) {
735 DefInt(SQLITE_OK);
736 DefInt(SQLITE_ERROR);
737 DefInt(SQLITE_INTERNAL);
738 DefInt(SQLITE_PERM);
739 DefInt(SQLITE_ABORT);
740 DefInt(SQLITE_BUSY);
741 DefInt(SQLITE_LOCKED);
742 DefInt(SQLITE_NOMEM);
743 DefInt(SQLITE_READONLY);
744 DefInt(SQLITE_INTERRUPT);
745 DefInt(SQLITE_IOERR);
746 DefInt(SQLITE_CORRUPT);
747 DefInt(SQLITE_NOTFOUND);
748 DefInt(SQLITE_FULL);
749 DefInt(SQLITE_CANTOPEN);
750 DefInt(SQLITE_PROTOCOL);
751 DefInt(SQLITE_EMPTY);
752 DefInt(SQLITE_SCHEMA);
753 DefInt(SQLITE_TOOBIG);
754 DefInt(SQLITE_CONSTRAINT);
755 DefInt(SQLITE_MISMATCH);
756 DefInt(SQLITE_MISUSE);
757 DefInt(SQLITE_NOLFS);
758 DefInt(SQLITE_AUTH);
759 DefInt(SQLITE_FORMAT);
760 DefInt(SQLITE_RANGE);
761 DefInt(SQLITE_NOTADB);
762 DefInt(SQLITE_NOTICE);
763 DefInt(SQLITE_WARNING);
764 DefInt(SQLITE_ROW);
765 DefInt(SQLITE_DONE);
766 // Extended Result Codes
767 DefInt(SQLITE_ERROR_MISSING_COLLSEQ);
768 DefInt(SQLITE_ERROR_RETRY);
769 DefInt(SQLITE_ERROR_SNAPSHOT);
770 DefInt(SQLITE_IOERR_READ);
771 DefInt(SQLITE_IOERR_SHORT_READ);
772 DefInt(SQLITE_IOERR_WRITE);
773 DefInt(SQLITE_IOERR_FSYNC);
774 DefInt(SQLITE_IOERR_DIR_FSYNC);
775 DefInt(SQLITE_IOERR_TRUNCATE);
776 DefInt(SQLITE_IOERR_FSTAT);
777 DefInt(SQLITE_IOERR_UNLOCK);
778 DefInt(SQLITE_IOERR_RDLOCK);
779 DefInt(SQLITE_IOERR_DELETE);
780 DefInt(SQLITE_IOERR_BLOCKED);
781 DefInt(SQLITE_IOERR_NOMEM);
782 DefInt(SQLITE_IOERR_ACCESS);
783 DefInt(SQLITE_IOERR_CHECKRESERVEDLOCK);
784 DefInt(SQLITE_IOERR_LOCK);
785 DefInt(SQLITE_IOERR_CLOSE);
786 DefInt(SQLITE_IOERR_DIR_CLOSE);
787 DefInt(SQLITE_IOERR_SHMOPEN);
788 DefInt(SQLITE_IOERR_SHMSIZE);
789 DefInt(SQLITE_IOERR_SHMLOCK);
790 DefInt(SQLITE_IOERR_SHMMAP);
791 DefInt(SQLITE_IOERR_SEEK);
792 DefInt(SQLITE_IOERR_DELETE_NOENT);
793 DefInt(SQLITE_IOERR_MMAP);
794 DefInt(SQLITE_IOERR_GETTEMPPATH);
795 DefInt(SQLITE_IOERR_CONVPATH);
796 DefInt(SQLITE_IOERR_VNODE);
797 DefInt(SQLITE_IOERR_AUTH);
798 DefInt(SQLITE_IOERR_BEGIN_ATOMIC);
799 DefInt(SQLITE_IOERR_COMMIT_ATOMIC);
800 DefInt(SQLITE_IOERR_ROLLBACK_ATOMIC);
801 DefInt(SQLITE_IOERR_DATA);
802 DefInt(SQLITE_IOERR_CORRUPTFS);
803 DefInt(SQLITE_LOCKED_SHAREDCACHE);
804 DefInt(SQLITE_LOCKED_VTAB);
805 DefInt(SQLITE_BUSY_RECOVERY);
806 DefInt(SQLITE_BUSY_SNAPSHOT);
807 DefInt(SQLITE_BUSY_TIMEOUT);
808 DefInt(SQLITE_CANTOPEN_NOTEMPDIR);
809 DefInt(SQLITE_CANTOPEN_ISDIR);
810 DefInt(SQLITE_CANTOPEN_FULLPATH);
811 DefInt(SQLITE_CANTOPEN_CONVPATH);
812 //DefInt(SQLITE_CANTOPEN_DIRTYWAL)/*docs say not used*/;
813 DefInt(SQLITE_CANTOPEN_SYMLINK);
814 DefInt(SQLITE_CORRUPT_VTAB);
815 DefInt(SQLITE_CORRUPT_SEQUENCE);
816 DefInt(SQLITE_CORRUPT_INDEX);
817 DefInt(SQLITE_READONLY_RECOVERY);
818 DefInt(SQLITE_READONLY_CANTLOCK);
819 DefInt(SQLITE_READONLY_ROLLBACK);
820 DefInt(SQLITE_READONLY_DBMOVED);
821 DefInt(SQLITE_READONLY_CANTINIT);
822 DefInt(SQLITE_READONLY_DIRECTORY);
823 DefInt(SQLITE_ABORT_ROLLBACK);
824 DefInt(SQLITE_CONSTRAINT_CHECK);
825 DefInt(SQLITE_CONSTRAINT_COMMITHOOK);
826 DefInt(SQLITE_CONSTRAINT_FOREIGNKEY);
827 DefInt(SQLITE_CONSTRAINT_FUNCTION);
828 DefInt(SQLITE_CONSTRAINT_NOTNULL);
829 DefInt(SQLITE_CONSTRAINT_PRIMARYKEY);
830 DefInt(SQLITE_CONSTRAINT_TRIGGER);
831 DefInt(SQLITE_CONSTRAINT_UNIQUE);
832 DefInt(SQLITE_CONSTRAINT_VTAB);
833 DefInt(SQLITE_CONSTRAINT_ROWID);
834 DefInt(SQLITE_CONSTRAINT_PINNED);
835 DefInt(SQLITE_CONSTRAINT_DATATYPE);
836 DefInt(SQLITE_NOTICE_RECOVER_WAL);
837 DefInt(SQLITE_NOTICE_RECOVER_ROLLBACK);
838 DefInt(SQLITE_WARNING_AUTOINDEX);
839 DefInt(SQLITE_AUTH_USER);
840 DefInt(SQLITE_OK_LOAD_PERMANENTLY);
841 //DefInt(SQLITE_OK_SYMLINK) /* internal use only */;
842 } _DefGroup;
844 DefGroup(serialize){
845 DefInt(SQLITE_SERIALIZE_NOCOPY);
846 DefInt(SQLITE_DESERIALIZE_FREEONCLOSE);
847 DefInt(SQLITE_DESERIALIZE_READONLY);
848 DefInt(SQLITE_DESERIALIZE_RESIZEABLE);
849 } _DefGroup;
851 DefGroup(session){
852 DefInt(SQLITE_SESSION_CONFIG_STRMSIZE);
853 DefInt(SQLITE_SESSION_OBJCONFIG_SIZE);
854 } _DefGroup;
856 DefGroup(sqlite3Status){
857 DefInt(SQLITE_STATUS_MEMORY_USED);
858 DefInt(SQLITE_STATUS_PAGECACHE_USED);
859 DefInt(SQLITE_STATUS_PAGECACHE_OVERFLOW);
860 //DefInt(SQLITE_STATUS_SCRATCH_USED) /* NOT USED */;
861 //DefInt(SQLITE_STATUS_SCRATCH_OVERFLOW) /* NOT USED */;
862 DefInt(SQLITE_STATUS_MALLOC_SIZE);
863 DefInt(SQLITE_STATUS_PARSER_STACK);
864 DefInt(SQLITE_STATUS_PAGECACHE_SIZE);
865 //DefInt(SQLITE_STATUS_SCRATCH_SIZE) /* NOT USED */;
866 DefInt(SQLITE_STATUS_MALLOC_COUNT);
867 } _DefGroup;
869 DefGroup(stmtStatus){
870 DefInt(SQLITE_STMTSTATUS_FULLSCAN_STEP);
871 DefInt(SQLITE_STMTSTATUS_SORT);
872 DefInt(SQLITE_STMTSTATUS_AUTOINDEX);
873 DefInt(SQLITE_STMTSTATUS_VM_STEP);
874 DefInt(SQLITE_STMTSTATUS_REPREPARE);
875 DefInt(SQLITE_STMTSTATUS_RUN);
876 DefInt(SQLITE_STMTSTATUS_FILTER_MISS);
877 DefInt(SQLITE_STMTSTATUS_FILTER_HIT);
878 DefInt(SQLITE_STMTSTATUS_MEMUSED);
879 } _DefGroup;
881 DefGroup(syncFlags) {
882 DefInt(SQLITE_SYNC_NORMAL);
883 DefInt(SQLITE_SYNC_FULL);
884 DefInt(SQLITE_SYNC_DATAONLY);
885 } _DefGroup;
887 DefGroup(trace) {
888 DefInt(SQLITE_TRACE_STMT);
889 DefInt(SQLITE_TRACE_PROFILE);
890 DefInt(SQLITE_TRACE_ROW);
891 DefInt(SQLITE_TRACE_CLOSE);
892 } _DefGroup;
894 DefGroup(txnState){
895 DefInt(SQLITE_TXN_NONE);
896 DefInt(SQLITE_TXN_READ);
897 DefInt(SQLITE_TXN_WRITE);
898 } _DefGroup;
900 DefGroup(udfFlags) {
901 DefInt(SQLITE_DETERMINISTIC);
902 DefInt(SQLITE_DIRECTONLY);
903 DefInt(SQLITE_INNOCUOUS);
904 DefInt(SQLITE_SUBTYPE);
905 DefInt(SQLITE_RESULT_SUBTYPE);
906 } _DefGroup;
908 DefGroup(version) {
909 DefInt(SQLITE_VERSION_NUMBER);
910 DefStr(SQLITE_VERSION);
911 DefStr(SQLITE_SOURCE_ID);
912 } _DefGroup;
914 DefGroup(vtab) {
915 DefInt(SQLITE_INDEX_SCAN_UNIQUE);
916 DefInt(SQLITE_INDEX_CONSTRAINT_EQ);
917 DefInt(SQLITE_INDEX_CONSTRAINT_GT);
918 DefInt(SQLITE_INDEX_CONSTRAINT_LE);
919 DefInt(SQLITE_INDEX_CONSTRAINT_LT);
920 DefInt(SQLITE_INDEX_CONSTRAINT_GE);
921 DefInt(SQLITE_INDEX_CONSTRAINT_MATCH);
922 DefInt(SQLITE_INDEX_CONSTRAINT_LIKE);
923 DefInt(SQLITE_INDEX_CONSTRAINT_GLOB);
924 DefInt(SQLITE_INDEX_CONSTRAINT_REGEXP);
925 DefInt(SQLITE_INDEX_CONSTRAINT_NE);
926 DefInt(SQLITE_INDEX_CONSTRAINT_ISNOT);
927 DefInt(SQLITE_INDEX_CONSTRAINT_ISNOTNULL);
928 DefInt(SQLITE_INDEX_CONSTRAINT_ISNULL);
929 DefInt(SQLITE_INDEX_CONSTRAINT_IS);
930 DefInt(SQLITE_INDEX_CONSTRAINT_LIMIT);
931 DefInt(SQLITE_INDEX_CONSTRAINT_OFFSET);
932 DefInt(SQLITE_INDEX_CONSTRAINT_FUNCTION);
933 DefInt(SQLITE_VTAB_CONSTRAINT_SUPPORT);
934 DefInt(SQLITE_VTAB_INNOCUOUS);
935 DefInt(SQLITE_VTAB_DIRECTONLY);
936 DefInt(SQLITE_VTAB_USES_ALL_SCHEMAS);
937 DefInt(SQLITE_ROLLBACK);
938 //DefInt(SQLITE_IGNORE); // Also used by sqlite3_authorizer() callback
939 DefInt(SQLITE_FAIL);
940 //DefInt(SQLITE_ABORT); // Also an error code
941 DefInt(SQLITE_REPLACE);
942 } _DefGroup;
944 #undef DefGroup
945 #undef DefStr
946 #undef DefInt
947 #undef _DefGroup
950 ** Emit an array of "StructBinder" struct descripions, which look
951 ** like:
953 ** {
954 ** "name": "MyStruct",
955 ** "sizeof": 16,
956 ** "members": {
957 ** "member1": {"offset": 0,"sizeof": 4,"signature": "i"},
958 ** "member2": {"offset": 4,"sizeof": 4,"signature": "p"},
959 ** "member3": {"offset": 8,"sizeof": 8,"signature": "j"}
960 ** }
961 ** }
963 ** Detailed documentation for those bits are in the docs for the
964 ** Jaccwabyt JS-side component.
967 /** Macros for emitting StructBinder description. */
968 #define StructBinder__(TYPE) \
969 n = 0; \
970 outf("%s{", (nStruct++ ? ", " : "")); \
971 out("\"name\": \"" # TYPE "\","); \
972 outf("\"sizeof\": %d", (int)sizeof(TYPE)); \
973 out(",\"members\": {");
974 #define StructBinder_(T) StructBinder__(T)
975 /** ^^^ indirection needed to expand CurrentStruct */
976 #define StructBinder StructBinder_(CurrentStruct)
977 #define _StructBinder CloseBrace(2)
978 #define M(MEMBER,SIG) \
979 outf("%s\"%s\": " \
980 "{\"offset\":%d,\"sizeof\": %d,\"signature\":\"%s\"}", \
981 (n++ ? ", " : ""), #MEMBER, \
982 (int)offsetof(CurrentStruct,MEMBER), \
983 (int)sizeof(((CurrentStruct*)0)->MEMBER), \
984 SIG)
986 nStruct = 0;
987 out(", \"structs\": ["); {
989 #define CurrentStruct sqlite3_vfs
990 StructBinder {
991 M(iVersion, "i");
992 M(szOsFile, "i");
993 M(mxPathname, "i");
994 M(pNext, "p");
995 M(zName, "s");
996 M(pAppData, "p");
997 M(xOpen, "i(pppip)");
998 M(xDelete, "i(ppi)");
999 M(xAccess, "i(ppip)");
1000 M(xFullPathname, "i(ppip)");
1001 M(xDlOpen, "p(pp)");
1002 M(xDlError, "p(pip)");
1003 M(xDlSym, "p()");
1004 M(xDlClose, "v(pp)");
1005 M(xRandomness, "i(pip)");
1006 M(xSleep, "i(pi)");
1007 M(xCurrentTime, "i(pp)");
1008 M(xGetLastError, "i(pip)");
1009 M(xCurrentTimeInt64, "i(pp)");
1010 M(xSetSystemCall, "i(ppp)");
1011 M(xGetSystemCall, "p(pp)");
1012 M(xNextSystemCall, "p(pp)");
1013 } _StructBinder;
1014 #undef CurrentStruct
1016 #define CurrentStruct sqlite3_io_methods
1017 StructBinder {
1018 M(iVersion, "i");
1019 M(xClose, "i(p)");
1020 M(xRead, "i(ppij)");
1021 M(xWrite, "i(ppij)");
1022 M(xTruncate, "i(pj)");
1023 M(xSync, "i(pi)");
1024 M(xFileSize, "i(pp)");
1025 M(xLock, "i(pi)");
1026 M(xUnlock, "i(pi)");
1027 M(xCheckReservedLock, "i(pp)");
1028 M(xFileControl, "i(pip)");
1029 M(xSectorSize, "i(p)");
1030 M(xDeviceCharacteristics, "i(p)");
1031 M(xShmMap, "i(piiip)");
1032 M(xShmLock, "i(piii)");
1033 M(xShmBarrier, "v(p)");
1034 M(xShmUnmap, "i(pi)");
1035 M(xFetch, "i(pjip)");
1036 M(xUnfetch, "i(pjp)");
1037 } _StructBinder;
1038 #undef CurrentStruct
1040 #define CurrentStruct sqlite3_file
1041 StructBinder {
1042 M(pMethods, "p");
1043 } _StructBinder;
1044 #undef CurrentStruct
1046 #define CurrentStruct sqlite3_kvvfs_methods
1047 StructBinder {
1048 M(xRead, "i(sspi)");
1049 M(xWrite, "i(sss)");
1050 M(xDelete, "i(ss)");
1051 M(nKeySize, "i");
1052 } _StructBinder;
1053 #undef CurrentStruct
1056 #define CurrentStruct sqlite3_vtab
1057 StructBinder {
1058 M(pModule, "p");
1059 M(nRef, "i");
1060 M(zErrMsg, "p");
1061 } _StructBinder;
1062 #undef CurrentStruct
1064 #define CurrentStruct sqlite3_vtab_cursor
1065 StructBinder {
1066 M(pVtab, "p");
1067 } _StructBinder;
1068 #undef CurrentStruct
1070 #define CurrentStruct sqlite3_module
1071 StructBinder {
1072 M(iVersion, "i");
1073 M(xCreate, "i(ppippp)");
1074 M(xConnect, "i(ppippp)");
1075 M(xBestIndex, "i(pp)");
1076 M(xDisconnect, "i(p)");
1077 M(xDestroy, "i(p)");
1078 M(xOpen, "i(pp)");
1079 M(xClose, "i(p)");
1080 M(xFilter, "i(pisip)");
1081 M(xNext, "i(p)");
1082 M(xEof, "i(p)");
1083 M(xColumn, "i(ppi)");
1084 M(xRowid, "i(pp)");
1085 M(xUpdate, "i(pipp)");
1086 M(xBegin, "i(p)");
1087 M(xSync, "i(p)");
1088 M(xCommit, "i(p)");
1089 M(xRollback, "i(p)");
1090 M(xFindFunction, "i(pispp)");
1091 M(xRename, "i(ps)");
1092 // ^^^ v1. v2+ follows...
1093 M(xSavepoint, "i(pi)");
1094 M(xRelease, "i(pi)");
1095 M(xRollbackTo, "i(pi)");
1096 // ^^^ v2. v3+ follows...
1097 M(xShadowName, "i(s)");
1098 } _StructBinder;
1099 #undef CurrentStruct
1102 ** Workaround: in order to map the various inner structs from
1103 ** sqlite3_index_info, we have to uplift those into constructs we
1104 ** can access by type name. These structs _must_ match their
1105 ** in-sqlite3_index_info counterparts byte for byte.
1107 typedef struct {
1108 int iColumn;
1109 unsigned char op;
1110 unsigned char usable;
1111 int iTermOffset;
1112 } sqlite3_index_constraint;
1113 typedef struct {
1114 int iColumn;
1115 unsigned char desc;
1116 } sqlite3_index_orderby;
1117 typedef struct {
1118 int argvIndex;
1119 unsigned char omit;
1120 } sqlite3_index_constraint_usage;
1121 { /* Validate that the above struct sizeof()s match
1122 ** expectations. We could improve upon this by
1123 ** checking the offsetof() for each member. */
1124 const sqlite3_index_info siiCheck;
1125 #define IndexSzCheck(T,M) \
1126 (sizeof(T) == sizeof(*siiCheck.M))
1127 if(!IndexSzCheck(sqlite3_index_constraint,aConstraint)
1128 || !IndexSzCheck(sqlite3_index_orderby,aOrderBy)
1129 || !IndexSzCheck(sqlite3_index_constraint_usage,aConstraintUsage)){
1130 assert(!"sizeof mismatch in sqlite3_index_... struct(s)");
1131 return 0;
1133 #undef IndexSzCheck
1136 #define CurrentStruct sqlite3_index_constraint
1137 StructBinder {
1138 M(iColumn, "i");
1139 M(op, "C");
1140 M(usable, "C");
1141 M(iTermOffset, "i");
1142 } _StructBinder;
1143 #undef CurrentStruct
1145 #define CurrentStruct sqlite3_index_orderby
1146 StructBinder {
1147 M(iColumn, "i");
1148 M(desc, "C");
1149 } _StructBinder;
1150 #undef CurrentStruct
1152 #define CurrentStruct sqlite3_index_constraint_usage
1153 StructBinder {
1154 M(argvIndex, "i");
1155 M(omit, "C");
1156 } _StructBinder;
1157 #undef CurrentStruct
1159 #define CurrentStruct sqlite3_index_info
1160 StructBinder {
1161 M(nConstraint, "i");
1162 M(aConstraint, "p");
1163 M(nOrderBy, "i");
1164 M(aOrderBy, "p");
1165 M(aConstraintUsage, "p");
1166 M(idxNum, "i");
1167 M(idxStr, "p");
1168 M(needToFreeIdxStr, "i");
1169 M(orderByConsumed, "i");
1170 M(estimatedCost, "d");
1171 M(estimatedRows, "j");
1172 M(idxFlags, "i");
1173 M(colUsed, "j");
1174 } _StructBinder;
1175 #undef CurrentStruct
1177 #if SQLITE_WASM_TESTS
1178 #define CurrentStruct WasmTestStruct
1179 StructBinder {
1180 M(v4, "i");
1181 M(cstr, "s");
1182 M(ppV, "p");
1183 M(v8, "j");
1184 M(xFunc, "v(p)");
1185 } _StructBinder;
1186 #undef CurrentStruct
1187 #endif
1189 } out( "]"/*structs*/);
1191 out("}"/*top-level object*/);
1192 *zPos = 0;
1193 aBuffer[0] = '{'/*end of the race-condition workaround*/;
1194 return aBuffer;
1195 #undef StructBinder
1196 #undef StructBinder_
1197 #undef StructBinder__
1198 #undef M
1199 #undef _StructBinder
1200 #undef CloseBrace
1201 #undef out
1202 #undef outf
1203 #undef lenCheck
1207 ** This function is NOT part of the sqlite3 public API. It is strictly
1208 ** for use by the sqlite project's own JS/WASM bindings.
1210 ** This function invokes the xDelete method of the given VFS (or the
1211 ** default VFS if pVfs is NULL), passing on the given filename. If
1212 ** zName is NULL, no default VFS is found, or it has no xDelete
1213 ** method, SQLITE_MISUSE is returned, else the result of the xDelete()
1214 ** call is returned.
1216 SQLITE_WASM_EXPORT
1217 int sqlite3_wasm_vfs_unlink(sqlite3_vfs *pVfs, const char *zName){
1218 int rc = SQLITE_MISUSE /* ??? */;
1219 if( 0==pVfs && 0!=zName ) pVfs = sqlite3_vfs_find(0);
1220 if( zName && pVfs && pVfs->xDelete ){
1221 rc = pVfs->xDelete(pVfs, zName, 1);
1223 return rc;
1227 ** This function is NOT part of the sqlite3 public API. It is strictly
1228 ** for use by the sqlite project's own JS/WASM bindings.
1230 ** Returns a pointer to the given DB's VFS for the given DB name,
1231 ** defaulting to "main" if zDbName is 0. Returns 0 if no db with the
1232 ** given name is open.
1234 SQLITE_WASM_EXPORT
1235 sqlite3_vfs * sqlite3_wasm_db_vfs(sqlite3 *pDb, const char *zDbName){
1236 sqlite3_vfs * pVfs = 0;
1237 sqlite3_file_control(pDb, zDbName ? zDbName : "main",
1238 SQLITE_FCNTL_VFS_POINTER, &pVfs);
1239 return pVfs;
1243 ** This function is NOT part of the sqlite3 public API. It is strictly
1244 ** for use by the sqlite project's own JS/WASM bindings.
1246 ** This function resets the given db pointer's database as described at
1248 ** https://sqlite.org/c3ref/c_dbconfig_defensive.html#sqlitedbconfigresetdatabase
1250 ** But beware: virtual tables destroyed that way do not have their
1251 ** xDestroy() called, so will leak if they require that function for
1252 ** proper cleanup.
1254 ** Returns 0 on success, an SQLITE_xxx code on error. Returns
1255 ** SQLITE_MISUSE if pDb is NULL.
1257 SQLITE_WASM_EXPORT
1258 int sqlite3_wasm_db_reset(sqlite3 *pDb){
1259 int rc = SQLITE_MISUSE;
1260 if( pDb ){
1261 sqlite3_table_column_metadata(pDb, "main", 0, 0, 0, 0, 0, 0, 0);
1262 rc = sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
1263 if( 0==rc ){
1264 rc = sqlite3_exec(pDb, "VACUUM", 0, 0, 0);
1265 sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
1268 return rc;
1272 ** This function is NOT part of the sqlite3 public API. It is strictly
1273 ** for use by the sqlite project's own JS/WASM bindings.
1275 ** Uses the given database's VFS xRead to stream the db file's
1276 ** contents out to the given callback. The callback gets a single
1277 ** chunk of size n (its 2nd argument) on each call and must return 0
1278 ** on success, non-0 on error. This function returns 0 on success,
1279 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
1280 ** code from the callback. Note that this is not thread-friendly: it
1281 ** expects that it will be the only thread reading the db file and
1282 ** takes no measures to ensure that is the case.
1284 ** This implementation appears to work fine, but
1285 ** sqlite3_wasm_db_serialize() is arguably the better way to achieve
1286 ** this.
1288 SQLITE_WASM_EXPORT
1289 int sqlite3_wasm_db_export_chunked( sqlite3* pDb,
1290 int (*xCallback)(unsigned const char *zOut, int n) ){
1291 sqlite3_int64 nSize = 0;
1292 sqlite3_int64 nPos = 0;
1293 sqlite3_file * pFile = 0;
1294 unsigned char buf[1024 * 8];
1295 int nBuf = (int)sizeof(buf);
1296 int rc = pDb
1297 ? sqlite3_file_control(pDb, "main",
1298 SQLITE_FCNTL_FILE_POINTER, &pFile)
1299 : SQLITE_NOTFOUND;
1300 if( rc ) return rc;
1301 rc = pFile->pMethods->xFileSize(pFile, &nSize);
1302 if( rc ) return rc;
1303 if(nSize % nBuf){
1304 /* DB size is not an even multiple of the buffer size. Reduce
1305 ** buffer size so that we do not unduly inflate the db size
1306 ** with zero-padding when exporting. */
1307 if(0 == nSize % 4096) nBuf = 4096;
1308 else if(0 == nSize % 2048) nBuf = 2048;
1309 else if(0 == nSize % 1024) nBuf = 1024;
1310 else nBuf = 512;
1312 for( ; 0==rc && nPos<nSize; nPos += nBuf ){
1313 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
1314 if( SQLITE_IOERR_SHORT_READ == rc ){
1315 rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
1317 if( 0==rc ) rc = xCallback(buf, nBuf);
1319 return rc;
1323 ** This function is NOT part of the sqlite3 public API. It is strictly
1324 ** for use by the sqlite project's own JS/WASM bindings.
1326 ** A proxy for sqlite3_serialize() which serializes the schema zSchema
1327 ** of pDb, placing the serialized output in pOut and nOut. nOut may be
1328 ** NULL. If zSchema is NULL then "main" is assumed. If pDb or pOut are
1329 ** NULL then SQLITE_MISUSE is returned. If allocation of the
1330 ** serialized copy fails, SQLITE_NOMEM is returned. On success, 0 is
1331 ** returned and `*pOut` will contain a pointer to the memory unless
1332 ** mFlags includes SQLITE_SERIALIZE_NOCOPY and the database has no
1333 ** contiguous memory representation, in which case `*pOut` will be
1334 ** NULL but 0 will be returned.
1336 ** If `*pOut` is not NULL, the caller is responsible for passing it to
1337 ** sqlite3_free() to free it.
1339 SQLITE_WASM_EXPORT
1340 int sqlite3_wasm_db_serialize( sqlite3 *pDb, const char *zSchema,
1341 unsigned char **pOut,
1342 sqlite3_int64 *nOut, unsigned int mFlags ){
1343 unsigned char * z;
1344 if( !pDb || !pOut ) return SQLITE_MISUSE;
1345 if( nOut ) *nOut = 0;
1346 z = sqlite3_serialize(pDb, zSchema ? zSchema : "main", nOut, mFlags);
1347 if( z || (SQLITE_SERIALIZE_NOCOPY & mFlags) ){
1348 *pOut = z;
1349 return 0;
1350 }else{
1351 return SQLITE_NOMEM;
1356 ** This function is NOT part of the sqlite3 public API. It is strictly
1357 ** for use by the sqlite project's own JS/WASM bindings.
1359 ** ACHTUNG: it was discovered on 2023-08-11 that, with SQLITE_DEBUG,
1360 ** this function's out-of-scope use of the sqlite3_vfs/file/io_methods
1361 ** APIs leads to triggering of assertions in the core library. Its use
1362 ** is now deprecated and VFS-specific APIs for importing files need to
1363 ** be found to replace it. sqlite3_wasm_posix_create_file() is
1364 ** suitable for the "unix" family of VFSes.
1366 ** Creates a new file using the I/O API of the given VFS, containing
1367 ** the given number of bytes of the given data. If the file exists, it
1368 ** is truncated to the given length and populated with the given
1369 ** data.
1371 ** This function exists so that we can implement the equivalent of
1372 ** Emscripten's FS.createDataFile() in a VFS-agnostic way. This
1373 ** functionality is intended for use in uploading database files.
1375 ** Not all VFSes support this functionality, e.g. the "kvvfs" does
1376 ** not.
1378 ** If pVfs is NULL, sqlite3_vfs_find(0) is used.
1380 ** If zFile is NULL, pVfs is NULL (and sqlite3_vfs_find(0) returns
1381 ** NULL), or nData is negative, SQLITE_MISUSE are returned.
1383 ** On success, it creates a new file with the given name, populated
1384 ** with the fist nData bytes of pData. If pData is NULL, the file is
1385 ** created and/or truncated to nData bytes.
1387 ** Whether or not directory components of zFilename are created
1388 ** automatically or not is unspecified: that detail is left to the
1389 ** VFS. The "opfs" VFS, for example, creates them.
1391 ** If an error happens while populating or truncating the file, the
1392 ** target file will be deleted (if needed) if this function created
1393 ** it. If this function did not create it, it is not deleted but may
1394 ** be left in an undefined state.
1396 ** Returns 0 on success. On error, it returns a code described above
1397 ** or propagates a code from one of the I/O methods.
1399 ** Design note: nData is an integer, instead of int64, for WASM
1400 ** portability, so that the API can still work in builds where BigInt
1401 ** support is disabled or unavailable.
1403 SQLITE_WASM_EXPORT
1404 int sqlite3_wasm_vfs_create_file( sqlite3_vfs *pVfs,
1405 const char *zFilename,
1406 const unsigned char * pData,
1407 int nData ){
1408 int rc;
1409 sqlite3_file *pFile = 0;
1410 sqlite3_io_methods const *pIo;
1411 const int openFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
1412 #if 0 && defined(SQLITE_DEBUG)
1413 | SQLITE_OPEN_MAIN_JOURNAL
1414 /* ^^^^ This is for testing a horrible workaround to avoid
1415 triggering a specific assert() in os_unix.c:unixOpen(). Please
1416 do not enable this in real builds. */
1417 #endif
1419 int flagsOut = 0;
1420 int fileExisted = 0;
1421 int doUnlock = 0;
1422 const unsigned char *pPos = pData;
1423 const int blockSize = 512
1424 /* Because we are using pFile->pMethods->xWrite() for writing, and
1425 ** it may have a buffer limit related to sqlite3's pager size, we
1426 ** conservatively write in 512-byte blocks (smallest page
1427 ** size). */;
1428 //fprintf(stderr, "pVfs=%p, zFilename=%s, nData=%d\n", pVfs, zFilename, nData);
1429 if( !pVfs ) pVfs = sqlite3_vfs_find(0);
1430 if( !pVfs || !zFilename || nData<0 ) return SQLITE_MISUSE;
1431 pVfs->xAccess(pVfs, zFilename, SQLITE_ACCESS_EXISTS, &fileExisted);
1432 rc = sqlite3OsOpenMalloc(pVfs, zFilename, &pFile, openFlags, &flagsOut);
1433 #if 0
1434 # define RC fprintf(stderr,"create_file(%s,%s) @%d rc=%d\n", \
1435 pVfs->zName, zFilename, __LINE__, rc);
1436 #else
1437 # define RC
1438 #endif
1440 if(rc) return rc;
1441 pIo = pFile->pMethods;
1442 if( pIo->xLock ) {
1443 /* We need xLock() in order to accommodate the OPFS VFS, as it
1444 ** obtains a writeable handle via the lock operation and releases
1445 ** it in xUnlock(). If we don't do those here, we have to add code
1446 ** to the VFS to account check whether it was locked before
1447 ** xFileSize(), xTruncate(), and the like, and release the lock
1448 ** only if it was unlocked when the op was started. */
1449 rc = pIo->xLock(pFile, SQLITE_LOCK_EXCLUSIVE);
1451 doUnlock = 0==rc;
1453 if( 0==rc ){
1454 rc = pIo->xTruncate(pFile, nData);
1457 if( 0==rc && 0!=pData && nData>0 ){
1458 while( 0==rc && nData>0 ){
1459 const int n = nData>=blockSize ? blockSize : nData;
1460 rc = pIo->xWrite(pFile, pPos, n, (sqlite3_int64)(pPos - pData));
1462 nData -= n;
1463 pPos += n;
1465 if( 0==rc && nData>0 ){
1466 assert( nData<blockSize );
1467 rc = pIo->xWrite(pFile, pPos, nData,
1468 (sqlite3_int64)(pPos - pData));
1472 if( pIo->xUnlock && doUnlock!=0 ){
1473 pIo->xUnlock(pFile, SQLITE_LOCK_NONE);
1475 pIo->xClose(pFile);
1476 if( rc!=0 && 0==fileExisted ){
1477 pVfs->xDelete(pVfs, zFilename, 1);
1480 #undef RC
1481 return rc;
1485 ** This function is NOT part of the sqlite3 public API. It is strictly
1486 ** for use by the sqlite project's own JS/WASM bindings.
1488 ** Creates or overwrites a file using the POSIX file API,
1489 ** i.e. Emscripten's virtual filesystem. Creates or truncates
1490 ** zFilename, appends pData bytes to it, and returns 0 on success or
1491 ** SQLITE_IOERR on error.
1493 SQLITE_WASM_EXPORT
1494 int sqlite3_wasm_posix_create_file( const char *zFilename,
1495 const unsigned char * pData,
1496 int nData ){
1497 int rc;
1498 FILE * pFile = 0;
1499 int fileExisted = 0;
1500 size_t nWrote = 1;
1502 if( !zFilename || nData<0 || (pData==0 && nData>0) ) return SQLITE_MISUSE;
1503 pFile = fopen(zFilename, "w");
1504 if( 0==pFile ) return SQLITE_IOERR;
1505 if( nData>0 ){
1506 nWrote = fwrite(pData, (size_t)nData, 1, pFile);
1508 fclose(pFile);
1509 return 1==nWrote ? 0 : SQLITE_IOERR;
1513 ** This function is NOT part of the sqlite3 public API. It is strictly
1514 ** for use by the sqlite project's own JS/WASM bindings.
1516 ** Allocates sqlite3KvvfsMethods.nKeySize bytes from
1517 ** sqlite3_wasm_pstack_alloc() and returns 0 if that allocation fails,
1518 ** else it passes that string to kvstorageMakeKey() and returns a
1519 ** NUL-terminated pointer to that string. It is up to the caller to
1520 ** use sqlite3_wasm_pstack_restore() to free the returned pointer.
1522 SQLITE_WASM_EXPORT
1523 char * sqlite3_wasm_kvvfsMakeKeyOnPstack(const char *zClass,
1524 const char *zKeyIn){
1525 assert(sqlite3KvvfsMethods.nKeySize>24);
1526 char *zKeyOut =
1527 (char *)sqlite3_wasm_pstack_alloc(sqlite3KvvfsMethods.nKeySize);
1528 if(zKeyOut){
1529 kvstorageMakeKey(zClass, zKeyIn, zKeyOut);
1531 return zKeyOut;
1535 ** This function is NOT part of the sqlite3 public API. It is strictly
1536 ** for use by the sqlite project's own JS/WASM bindings.
1538 ** Returns the pointer to the singleton object which holds the kvvfs
1539 ** I/O methods and associated state.
1541 SQLITE_WASM_EXPORT
1542 sqlite3_kvvfs_methods * sqlite3_wasm_kvvfs_methods(void){
1543 return &sqlite3KvvfsMethods;
1547 ** This function is NOT part of the sqlite3 public API. It is strictly
1548 ** for use by the sqlite project's own JS/WASM bindings.
1550 ** This is a proxy for the variadic sqlite3_vtab_config() which passes
1551 ** its argument on, or not, to sqlite3_vtab_config(), depending on the
1552 ** value of its 2nd argument. Returns the result of
1553 ** sqlite3_vtab_config(), or SQLITE_MISUSE if the 2nd arg is not a
1554 ** valid value.
1556 SQLITE_WASM_EXPORT
1557 int sqlite3_wasm_vtab_config(sqlite3 *pDb, int op, int arg){
1558 switch(op){
1559 case SQLITE_VTAB_DIRECTONLY:
1560 case SQLITE_VTAB_INNOCUOUS:
1561 return sqlite3_vtab_config(pDb, op);
1562 case SQLITE_VTAB_CONSTRAINT_SUPPORT:
1563 return sqlite3_vtab_config(pDb, op, arg);
1564 default:
1565 return SQLITE_MISUSE;
1570 ** This function is NOT part of the sqlite3 public API. It is strictly
1571 ** for use by the sqlite project's own JS/WASM bindings.
1573 ** Wrapper for the variants of sqlite3_db_config() which take
1574 ** (int,int*) variadic args.
1576 SQLITE_WASM_EXPORT
1577 int sqlite3_wasm_db_config_ip(sqlite3 *pDb, int op, int arg1, int* pArg2){
1578 switch(op){
1579 case SQLITE_DBCONFIG_ENABLE_FKEY:
1580 case SQLITE_DBCONFIG_ENABLE_TRIGGER:
1581 case SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER:
1582 case SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION:
1583 case SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE:
1584 case SQLITE_DBCONFIG_ENABLE_QPSG:
1585 case SQLITE_DBCONFIG_TRIGGER_EQP:
1586 case SQLITE_DBCONFIG_RESET_DATABASE:
1587 case SQLITE_DBCONFIG_DEFENSIVE:
1588 case SQLITE_DBCONFIG_WRITABLE_SCHEMA:
1589 case SQLITE_DBCONFIG_LEGACY_ALTER_TABLE:
1590 case SQLITE_DBCONFIG_DQS_DML:
1591 case SQLITE_DBCONFIG_DQS_DDL:
1592 case SQLITE_DBCONFIG_ENABLE_VIEW:
1593 case SQLITE_DBCONFIG_LEGACY_FILE_FORMAT:
1594 case SQLITE_DBCONFIG_TRUSTED_SCHEMA:
1595 case SQLITE_DBCONFIG_STMT_SCANSTATUS:
1596 case SQLITE_DBCONFIG_REVERSE_SCANORDER:
1597 return sqlite3_db_config(pDb, op, arg1, pArg2);
1598 default: return SQLITE_MISUSE;
1603 ** This function is NOT part of the sqlite3 public API. It is strictly
1604 ** for use by the sqlite project's own JS/WASM bindings.
1606 ** Wrapper for the variants of sqlite3_db_config() which take
1607 ** (void*,int,int) variadic args.
1609 SQLITE_WASM_EXPORT
1610 int sqlite3_wasm_db_config_pii(sqlite3 *pDb, int op, void * pArg1, int arg2, int arg3){
1611 switch(op){
1612 case SQLITE_DBCONFIG_LOOKASIDE:
1613 return sqlite3_db_config(pDb, op, pArg1, arg2, arg3);
1614 default: return SQLITE_MISUSE;
1619 ** This function is NOT part of the sqlite3 public API. It is strictly
1620 ** for use by the sqlite project's own JS/WASM bindings.
1622 ** Wrapper for the variants of sqlite3_db_config() which take
1623 ** (const char *) variadic args.
1625 SQLITE_WASM_EXPORT
1626 int sqlite3_wasm_db_config_s(sqlite3 *pDb, int op, const char *zArg){
1627 switch(op){
1628 case SQLITE_DBCONFIG_MAINDBNAME:
1629 return sqlite3_db_config(pDb, op, zArg);
1630 default: return SQLITE_MISUSE;
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.
1639 ** Binding for combinations of sqlite3_config() arguments which take
1640 ** a single integer argument.
1642 SQLITE_WASM_EXPORT
1643 int sqlite3_wasm_config_i(int op, int arg){
1644 return sqlite3_config(op, arg);
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.
1651 ** Binding for combinations of sqlite3_config() arguments which take
1652 ** two int arguments.
1654 SQLITE_WASM_EXPORT
1655 int sqlite3_wasm_config_ii(int op, int arg1, int arg2){
1656 return sqlite3_config(op, arg1, arg2);
1660 ** This function is NOT part of the sqlite3 public API. It is strictly
1661 ** for use by the sqlite project's own JS/WASM bindings.
1663 ** Binding for combinations of sqlite3_config() arguments which take
1664 ** a single i64 argument.
1666 SQLITE_WASM_EXPORT
1667 int sqlite3_wasm_config_j(int op, sqlite3_int64 arg){
1668 return sqlite3_config(op, arg);
1671 #if 0
1672 // Pending removal after verification of a workaround discussed in the
1673 // forum post linked to below.
1675 ** This function is NOT part of the sqlite3 public API. It is strictly
1676 ** for use by the sqlite project's own JS/WASM bindings.
1678 ** Returns a pointer to sqlite3_free(). In compliant browsers the
1679 ** return value, when passed to sqlite3.wasm.exports.functionEntry(),
1680 ** must resolve to the same function as
1681 ** sqlite3.wasm.exports.sqlite3_free. i.e. from a dev console where
1682 ** sqlite3 is exported globally, the following must be true:
1684 ** ```
1685 ** sqlite3.wasm.functionEntry(
1686 ** sqlite3.wasm.exports.sqlite3_wasm_ptr_to_sqlite3_free()
1687 ** ) === sqlite3.wasm.exports.sqlite3_free
1688 ** ```
1690 ** Using a function to return this pointer, as opposed to exporting it
1691 ** via sqlite3_wasm_enum_json(), is an attempt to work around a
1692 ** Safari-specific quirk covered at
1693 ** https://sqlite.org/forum/info/e5b20e1feb37a19a.
1695 SQLITE_WASM_EXPORT
1696 void * sqlite3_wasm_ptr_to_sqlite3_free(void){
1697 return (void*)sqlite3_free;
1699 #endif
1701 #if defined(__EMSCRIPTEN__) && defined(SQLITE_ENABLE_WASMFS)
1702 #include <emscripten/wasmfs.h>
1705 ** This function is NOT part of the sqlite3 public API. It is strictly
1706 ** for use by the sqlite project's own JS/WASM bindings, specifically
1707 ** only when building with Emscripten's WASMFS support.
1709 ** This function should only be called if the JS side detects the
1710 ** existence of the Origin-Private FileSystem (OPFS) APIs in the
1711 ** client. The first time it is called, this function instantiates a
1712 ** WASMFS backend impl for OPFS. On success, subsequent calls are
1713 ** no-ops.
1715 ** This function may be passed a "mount point" name, which must have a
1716 ** leading "/" and is currently restricted to a single path component,
1717 ** e.g. "/foo" is legal but "/foo/" and "/foo/bar" are not. If it is
1718 ** NULL or empty, it defaults to "/opfs".
1720 ** Returns 0 on success, SQLITE_NOMEM if instantiation of the backend
1721 ** object fails, SQLITE_IOERR if mkdir() of the zMountPoint dir in
1722 ** the virtual FS fails. In builds compiled without SQLITE_ENABLE_WASMFS
1723 ** defined, SQLITE_NOTFOUND is returned without side effects.
1725 SQLITE_WASM_EXPORT
1726 int sqlite3_wasm_init_wasmfs(const char *zMountPoint){
1727 static backend_t pOpfs = 0;
1728 if( !zMountPoint || !*zMountPoint ) zMountPoint = "/opfs";
1729 if( !pOpfs ){
1730 pOpfs = wasmfs_create_opfs_backend();
1732 /** It's not enough to instantiate the backend. We have to create a
1733 mountpoint in the VFS and attach the backend to it. */
1734 if( pOpfs && 0!=access(zMountPoint, F_OK) ){
1735 /* Note that this check and is not robust but it will
1736 hypothetically suffice for the transient wasm-based virtual
1737 filesystem we're currently running in. */
1738 const int rc = wasmfs_create_directory(zMountPoint, 0777, pOpfs);
1739 /*emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc);*/
1740 if(rc) return SQLITE_IOERR;
1742 return pOpfs ? 0 : SQLITE_NOMEM;
1744 #else
1745 SQLITE_WASM_EXPORT
1746 int sqlite3_wasm_init_wasmfs(const char *zUnused){
1747 //emscripten_console_warn("WASMFS OPFS is not compiled in.");
1748 if(zUnused){/*unused*/}
1749 return SQLITE_NOTFOUND;
1751 #endif /* __EMSCRIPTEN__ && SQLITE_ENABLE_WASMFS */
1753 #if SQLITE_WASM_TESTS
1755 SQLITE_WASM_EXPORT
1756 int sqlite3_wasm_test_intptr(int * p){
1757 return *p = *p * 2;
1760 SQLITE_WASM_EXPORT
1761 void * sqlite3_wasm_test_voidptr(void * p){
1762 return p;
1765 SQLITE_WASM_EXPORT
1766 int64_t sqlite3_wasm_test_int64_max(void){
1767 return (int64_t)0x7fffffffffffffff;
1770 SQLITE_WASM_EXPORT
1771 int64_t sqlite3_wasm_test_int64_min(void){
1772 return ~sqlite3_wasm_test_int64_max();
1775 SQLITE_WASM_EXPORT
1776 int64_t sqlite3_wasm_test_int64_times2(int64_t x){
1777 return x * 2;
1780 SQLITE_WASM_EXPORT
1781 void sqlite3_wasm_test_int64_minmax(int64_t * min, int64_t *max){
1782 *max = sqlite3_wasm_test_int64_max();
1783 *min = sqlite3_wasm_test_int64_min();
1784 /*printf("minmax: min=%lld, max=%lld\n", *min, *max);*/
1787 SQLITE_WASM_EXPORT
1788 int64_t sqlite3_wasm_test_int64ptr(int64_t * p){
1789 /*printf("sqlite3_wasm_test_int64ptr( @%lld = 0x%llx )\n", (int64_t)p, *p);*/
1790 return *p = *p * 2;
1793 SQLITE_WASM_EXPORT
1794 void sqlite3_wasm_test_stack_overflow(int recurse){
1795 if(recurse) sqlite3_wasm_test_stack_overflow(recurse);
1798 /* For testing the 'string:dealloc' whwasmutil.xWrap() conversion. */
1799 SQLITE_WASM_EXPORT
1800 char * sqlite3_wasm_test_str_hello(int fail){
1801 char * s = fail ? 0 : (char *)sqlite3_malloc(6);
1802 if(s){
1803 memcpy(s, "hello", 5);
1804 s[5] = 0;
1806 return s;
1810 ** For testing using SQLTester scripts.
1812 ** Return non-zero if string z matches glob pattern zGlob and zero if the
1813 ** pattern does not match.
1815 ** To repeat:
1817 ** zero == no match
1818 ** non-zero == match
1820 ** Globbing rules:
1822 ** '*' Matches any sequence of zero or more characters.
1824 ** '?' Matches exactly one character.
1826 ** [...] Matches one character from the enclosed list of
1827 ** characters.
1829 ** [^...] Matches one character not in the enclosed list.
1831 ** '#' Matches any sequence of one or more digits with an
1832 ** optional + or - sign in front, or a hexadecimal
1833 ** literal of the form 0x...
1835 static int sqlite3_wasm_SQLTester_strnotglob(const char *zGlob, const char *z){
1836 int c, c2;
1837 int invert;
1838 int seen;
1839 typedef int (*recurse_f)(const char *,const char *);
1840 static const recurse_f recurse = sqlite3_wasm_SQLTester_strnotglob;
1842 while( (c = (*(zGlob++)))!=0 ){
1843 if( c=='*' ){
1844 while( (c=(*(zGlob++))) == '*' || c=='?' ){
1845 if( c=='?' && (*(z++))==0 ) return 0;
1847 if( c==0 ){
1848 return 1;
1849 }else if( c=='[' ){
1850 while( *z && recurse(zGlob-1,z)==0 ){
1851 z++;
1853 return (*z)!=0;
1855 while( (c2 = (*(z++)))!=0 ){
1856 while( c2!=c ){
1857 c2 = *(z++);
1858 if( c2==0 ) return 0;
1860 if( recurse(zGlob,z) ) return 1;
1862 return 0;
1863 }else if( c=='?' ){
1864 if( (*(z++))==0 ) return 0;
1865 }else if( c=='[' ){
1866 int prior_c = 0;
1867 seen = 0;
1868 invert = 0;
1869 c = *(z++);
1870 if( c==0 ) return 0;
1871 c2 = *(zGlob++);
1872 if( c2=='^' ){
1873 invert = 1;
1874 c2 = *(zGlob++);
1876 if( c2==']' ){
1877 if( c==']' ) seen = 1;
1878 c2 = *(zGlob++);
1880 while( c2 && c2!=']' ){
1881 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
1882 c2 = *(zGlob++);
1883 if( c>=prior_c && c<=c2 ) seen = 1;
1884 prior_c = 0;
1885 }else{
1886 if( c==c2 ){
1887 seen = 1;
1889 prior_c = c2;
1891 c2 = *(zGlob++);
1893 if( c2==0 || (seen ^ invert)==0 ) return 0;
1894 }else if( c=='#' ){
1895 if( z[0]=='0'
1896 && (z[1]=='x' || z[1]=='X')
1897 && sqlite3Isxdigit(z[2])
1899 z += 3;
1900 while( sqlite3Isxdigit(z[0]) ){ z++; }
1901 }else{
1902 if( (z[0]=='-' || z[0]=='+') && sqlite3Isdigit(z[1]) ) z++;
1903 if( !sqlite3Isdigit(z[0]) ) return 0;
1904 z++;
1905 while( sqlite3Isdigit(z[0]) ){ z++; }
1907 }else{
1908 if( c!=(*(z++)) ) return 0;
1911 return *z==0;
1914 SQLITE_WASM_EXPORT
1915 int sqlite3_wasm_SQLTester_strglob(const char *zGlob, const char *z){
1916 return !sqlite3_wasm_SQLTester_strnotglob(zGlob, z);
1920 #endif /* SQLITE_WASM_TESTS */
1922 #undef SQLITE_WASM_EXPORT