Snapshot of upstream SQLite 3.42.0
[sqlcipher.git] / ext / wasm / api / sqlite3-wasm.c
blobdbe594dc3dcf9d7ab14f733d1168f3aa38d3460b
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_... */
87 #ifndef SQLITE_ENABLE_BYTECODE_VTAB
88 # define SQLITE_ENABLE_BYTECODE_VTAB 1
89 #endif
90 #ifndef SQLITE_ENABLE_DBPAGE_VTAB
91 # define SQLITE_ENABLE_DBPAGE_VTAB 1
92 #endif
93 #ifndef SQLITE_ENABLE_DBSTAT_VTAB
94 # define SQLITE_ENABLE_DBSTAT_VTAB 1
95 #endif
96 #ifndef SQLITE_ENABLE_EXPLAIN_COMMENTS
97 # define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
98 #endif
99 #ifndef SQLITE_ENABLE_FTS4
100 # define SQLITE_ENABLE_FTS4 1
101 #endif
102 #ifndef SQLITE_ENABLE_MATH_FUNCTIONS
103 # define SQLITE_ENABLE_MATH_FUNCTIONS 1
104 #endif
105 #ifndef SQLITE_ENABLE_OFFSET_SQL_FUNC
106 # define SQLITE_ENABLE_OFFSET_SQL_FUNC 1
107 #endif
108 #ifndef SQLITE_ENABLE_PREUPDATE_HOOK
109 # define SQLITE_ENABLE_PREUPDATE_HOOK 1 /*required by session extension*/
110 #endif
111 #ifndef SQLITE_ENABLE_RTREE
112 # define SQLITE_ENABLE_RTREE 1
113 #endif
114 #ifndef SQLITE_ENABLE_SESSION
115 # define SQLITE_ENABLE_SESSION 1
116 #endif
117 #ifndef SQLITE_ENABLE_STMTVTAB
118 # define SQLITE_ENABLE_STMTVTAB 1
119 #endif
120 #ifndef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
121 # define SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
122 #endif
124 /**********************************************************************/
125 /* SQLITE_M... */
126 #ifndef SQLITE_MAX_ALLOCATION_SIZE
127 # define SQLITE_MAX_ALLOCATION_SIZE 0x1fffffff
128 #endif
130 /**********************************************************************/
131 /* SQLITE_O... */
132 #ifndef SQLITE_OMIT_DEPRECATED
133 # define SQLITE_OMIT_DEPRECATED 1
134 #endif
135 #ifndef SQLITE_OMIT_LOAD_EXTENSION
136 # define SQLITE_OMIT_LOAD_EXTENSION 1
137 #endif
138 #ifndef SQLITE_OMIT_SHARED_CACHE
139 # define SQLITE_OMIT_SHARED_CACHE 1
140 #endif
141 #ifndef SQLITE_OMIT_UTF16
142 # define SQLITE_OMIT_UTF16 1
143 #endif
144 #ifndef SQLITE_OMIT_WAL
145 # define SQLITE_OMIT_WAL 1
146 #endif
147 #ifndef SQLITE_OS_KV_OPTIONAL
148 # define SQLITE_OS_KV_OPTIONAL 1
149 #endif
151 /**********************************************************************/
152 /* SQLITE_T... */
153 #ifndef SQLITE_TEMP_STORE
154 # define SQLITE_TEMP_STORE 3
155 #endif
156 #ifndef SQLITE_THREADSAFE
157 # define SQLITE_THREADSAFE 0
158 #endif
160 /**********************************************************************/
161 /* SQLITE_USE_... */
162 #ifndef SQLITE_USE_URI
163 # define SQLITE_USE_URI 1
164 #endif
166 #ifdef SQLITE_WASM_EXTRA_INIT
167 # define SQLITE_EXTRA_INIT sqlite3_wasm_extra_init
168 #endif
170 #include <assert.h>
173 ** SQLITE_WASM_EXPORT is functionally identical to EMSCRIPTEN_KEEPALIVE
174 ** but is not Emscripten-specific. It explicitly marks functions for
175 ** export into the target wasm file without requiring explicit listing
176 ** of those functions in Emscripten's -sEXPORTED_FUNCTIONS=... list
177 ** (or equivalent in other build platforms). Any function with neither
178 ** this attribute nor which is listed as an explicit export will not
179 ** be exported from the wasm file (but may still be used internally
180 ** within the wasm file).
182 ** The functions in this file (sqlite3-wasm.c) which require exporting
183 ** are marked with this flag. They may also be added to any explicit
184 ** build-time export list but need not be. All of these APIs are
185 ** intended for use only within the project's own JS/WASM code, and
186 ** not by client code, so an argument can be made for reducing their
187 ** visibility by not including them in any build-time export lists.
189 ** 2022-09-11: it's not yet _proven_ that this approach works in
190 ** non-Emscripten builds. If not, such builds will need to export
191 ** those using the --export=... wasm-ld flag (or equivalent). As of
192 ** this writing we are tied to Emscripten for various reasons
193 ** and cannot test the library with other build environments.
195 #define SQLITE_WASM_EXPORT __attribute__((used,visibility("default")))
196 // See also:
197 //__attribute__((export_name("theExportedName"), used, visibility("default")))
200 ** Which sqlite3.c we're using needs to be configurable to enable
201 ** building against a custom copy, e.g. the SEE variant. Note that we
202 ** #include the .c file, rather than the header, so that the WASM
203 ** extensions have access to private API internals.
205 ** The caveat here is that custom variants need to account for
206 ** exporting any necessary symbols (e.g. sqlite3_activate_see()). We
207 ** cannot export them from here using SQLITE_WASM_EXPORT because that
208 ** attribute (apparently) has to be part of the function definition.
210 #ifndef SQLITE_C
211 # define SQLITE_C sqlite3.c /* yes, .c instead of .h. */
212 #endif
213 #define INC__STRINGIFY_(f) #f
214 #define INC__STRINGIFY(f) INC__STRINGIFY_(f)
215 #include INC__STRINGIFY(SQLITE_C)
216 #undef INC__STRINGIFY_
217 #undef INC__STRINGIFY
218 #undef SQLITE_C
220 #if defined(__EMSCRIPTEN__)
221 # include <emscripten/console.h>
222 #endif
224 #if 0
226 ** An EXPERIMENT in implementing a stack-based allocator analog to
227 ** Emscripten's stackSave(), stackAlloc(), stackRestore().
228 ** Unfortunately, this cannot work together with Emscripten because
229 ** Emscripten defines its own native one and we'd stomp on each
230 ** other's memory. Other than that complication, basic tests show it
231 ** to work just fine.
233 ** Another option is to malloc() a chunk of our own and call that our
234 ** "stack".
236 SQLITE_WASM_EXPORT void * sqlite3_wasm_stack_end(void){
237 extern void __heap_base
238 /* see https://stackoverflow.com/questions/10038964 */;
239 return &__heap_base;
241 SQLITE_WASM_EXPORT void * sqlite3_wasm_stack_begin(void){
242 extern void __data_end;
243 return &__data_end;
245 static void * pWasmStackPtr = 0;
246 SQLITE_WASM_EXPORT void * sqlite3_wasm_stack_ptr(void){
247 if(!pWasmStackPtr) pWasmStackPtr = sqlite3_wasm_stack_end();
248 return pWasmStackPtr;
250 SQLITE_WASM_EXPORT void sqlite3_wasm_stack_restore(void * p){
251 pWasmStackPtr = p;
253 SQLITE_WASM_EXPORT void * sqlite3_wasm_stack_alloc(int n){
254 if(n<=0) return 0;
255 n = (n + 7) & ~7 /* align to 8-byte boundary */;
256 unsigned char * const p = (unsigned char *)sqlite3_wasm_stack_ptr();
257 unsigned const char * const b = (unsigned const char *)sqlite3_wasm_stack_begin();
258 if(b + n >= p || b + n < b/*overflow*/) return 0;
259 return pWasmStackPtr = p - n;
261 #endif /* stack allocator experiment */
264 ** State for the "pseudo-stack" allocator implemented in
265 ** sqlite3_wasm_pstack_xyz(). In order to avoid colliding with
266 ** Emscripten-controled stack space, it carves out a bit of stack
267 ** memory to use for that purpose. This memory ends up in the
268 ** WASM-managed memory, such that routines which manipulate the wasm
269 ** heap can also be used to manipulate this memory.
271 ** This particular allocator is intended for small allocations such as
272 ** storage for output pointers. We cannot reasonably size it large
273 ** enough for general-purpose string conversions because some of our
274 ** tests use input files (strings) of 16MB+.
276 static unsigned char PStack_mem[512 * 8] = {0};
277 static struct {
278 unsigned const char * const pBegin;/* Start (inclusive) of memory */
279 unsigned const char * const pEnd; /* One-after-the-end of memory */
280 unsigned char * pPos; /* Current stack pointer */
281 } PStack = {
282 &PStack_mem[0],
283 &PStack_mem[0] + sizeof(PStack_mem),
284 &PStack_mem[0] + sizeof(PStack_mem)
287 ** Returns the current pstack position.
289 SQLITE_WASM_EXPORT void * sqlite3_wasm_pstack_ptr(void){
290 return PStack.pPos;
293 ** Sets the pstack position poitner to p. Results are undefined if the
294 ** given value did not come from sqlite3_wasm_pstack_ptr().
296 SQLITE_WASM_EXPORT void sqlite3_wasm_pstack_restore(unsigned char * p){
297 assert(p>=PStack.pBegin && p<=PStack.pEnd && p>=PStack.pPos);
298 assert(0==(p & 0x7));
299 if(p>=PStack.pBegin && p<=PStack.pEnd /*&& p>=PStack.pPos*/){
300 PStack.pPos = p;
304 ** Allocate and zero out n bytes from the pstack. Returns a pointer to
305 ** the memory on success, 0 on error (including a negative n value). n
306 ** is always adjusted to be a multiple of 8 and returned memory is
307 ** always zeroed out before returning (because this keeps the client
308 ** JS code from having to do so, and most uses of the pstack will
309 ** call for doing so).
311 SQLITE_WASM_EXPORT void * sqlite3_wasm_pstack_alloc(int n){
312 if( n<=0 ) return 0;
313 //if( n & 0x7 ) n += 8 - (n & 0x7) /* align to 8-byte boundary */;
314 n = (n + 7) & ~7 /* align to 8-byte boundary */;
315 if( PStack.pBegin + n > PStack.pPos /*not enough space left*/
316 || PStack.pBegin + n <= PStack.pBegin /*overflow*/ ) return 0;
317 memset((PStack.pPos = PStack.pPos - n), 0, (unsigned int)n);
318 return PStack.pPos;
321 ** Return the number of bytes left which can be
322 ** sqlite3_wasm_pstack_alloc()'d.
324 SQLITE_WASM_EXPORT int sqlite3_wasm_pstack_remaining(void){
325 assert(PStack.pPos >= PStack.pBegin);
326 assert(PStack.pPos <= PStack.pEnd);
327 return (int)(PStack.pPos - PStack.pBegin);
331 ** Return the total number of bytes available in the pstack, including
332 ** any space which is currently allocated. This value is a
333 ** compile-time constant.
335 SQLITE_WASM_EXPORT int sqlite3_wasm_pstack_quota(void){
336 return (int)(PStack.pEnd - PStack.pBegin);
340 ** This function is NOT part of the sqlite3 public API. It is strictly
341 ** for use by the sqlite project's own JS/WASM bindings.
343 ** For purposes of certain hand-crafted C/Wasm function bindings, we
344 ** need a way of reporting errors which is consistent with the rest of
345 ** the C API, as opposed to throwing JS exceptions. To that end, this
346 ** internal-use-only function is a thin proxy around
347 ** sqlite3ErrorWithMessage(). The intent is that it only be used from
348 ** Wasm bindings such as sqlite3_prepare_v2/v3(), and definitely not
349 ** from client code.
351 ** Returns err_code.
353 SQLITE_WASM_EXPORT
354 int sqlite3_wasm_db_error(sqlite3*db, int err_code, const char *zMsg){
355 if( db!=0 ){
356 if( 0!=zMsg ){
357 const int nMsg = sqlite3Strlen30(zMsg);
358 sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg);
359 }else{
360 sqlite3ErrorWithMsg(db, err_code, NULL);
363 return err_code;
366 #if SQLITE_WASM_TESTS
367 struct WasmTestStruct {
368 int v4;
369 void * ppV;
370 const char * cstr;
371 int64_t v8;
372 void (*xFunc)(void*);
374 typedef struct WasmTestStruct WasmTestStruct;
375 SQLITE_WASM_EXPORT
376 void sqlite3_wasm_test_struct(WasmTestStruct * s){
377 if(s){
378 s->v4 *= 2;
379 s->v8 = s->v4 * 2;
380 s->ppV = s;
381 s->cstr = __FILE__;
382 if(s->xFunc) s->xFunc(s);
384 return;
386 #endif /* SQLITE_WASM_TESTS */
389 ** This function is NOT part of the sqlite3 public API. It is strictly
390 ** for use by the sqlite project's own JS/WASM bindings. Unlike the
391 ** rest of the sqlite3 API, this part requires C99 for snprintf() and
392 ** variadic macros.
394 ** Returns a string containing a JSON-format "enum" of C-level
395 ** constants and struct-related metadata intended to be imported into
396 ** the JS environment. The JSON is initialized the first time this
397 ** function is called and that result is reused for all future calls.
399 ** If this function returns NULL then it means that the internal
400 ** buffer is not large enough for the generated JSON and needs to be
401 ** increased. In debug builds that will trigger an assert().
403 SQLITE_WASM_EXPORT
404 const char * sqlite3_wasm_enum_json(void){
405 static char aBuffer[1024 * 20] = {0} /* where the JSON goes */;
406 int n = 0, nChildren = 0, nStruct = 0
407 /* output counters for figuring out where commas go */;
408 char * zPos = &aBuffer[1] /* skip first byte for now to help protect
409 ** against a small race condition */;
410 char const * const zEnd = &aBuffer[0] + sizeof(aBuffer) /* one-past-the-end */;
411 if(aBuffer[0]) return aBuffer;
412 /* Leave aBuffer[0] at 0 until the end to help guard against a tiny
413 ** race condition. If this is called twice concurrently, they might
414 ** end up both writing to aBuffer, but they'll both write the same
415 ** thing, so that's okay. If we set byte 0 up front then the 2nd
416 ** instance might return and use the string before the 1st instance
417 ** is done filling it. */
419 /* Core output macros... */
420 #define lenCheck assert(zPos < zEnd - 128 \
421 && "sqlite3_wasm_enum_json() buffer is too small."); \
422 if( zPos >= zEnd - 128 ) return 0
423 #define outf(format,...) \
424 zPos += snprintf(zPos, ((size_t)(zEnd - zPos)), format, __VA_ARGS__); \
425 lenCheck
426 #define out(TXT) outf("%s",TXT)
427 #define CloseBrace(LEVEL) \
428 assert(LEVEL<5); memset(zPos, '}', LEVEL); zPos+=LEVEL; lenCheck
430 /* Macros for emitting maps of integer- and string-type macros to
431 ** their values. */
432 #define DefGroup(KEY) n = 0; \
433 outf("%s\"" #KEY "\": {",(nChildren++ ? "," : ""));
434 #define DefInt(KEY) \
435 outf("%s\"%s\": %d", (n++ ? ", " : ""), #KEY, (int)KEY)
436 #define DefStr(KEY) \
437 outf("%s\"%s\": \"%s\"", (n++ ? ", " : ""), #KEY, KEY)
438 #define _DefGroup CloseBrace(1)
440 /* The following groups are sorted alphabetic by group name. */
441 DefGroup(access){
442 DefInt(SQLITE_ACCESS_EXISTS);
443 DefInt(SQLITE_ACCESS_READWRITE);
444 DefInt(SQLITE_ACCESS_READ)/*docs say this is unused*/;
445 } _DefGroup;
447 DefGroup(authorizer){
448 DefInt(SQLITE_DENY);
449 DefInt(SQLITE_IGNORE);
450 DefInt(SQLITE_CREATE_INDEX);
451 DefInt(SQLITE_CREATE_TABLE);
452 DefInt(SQLITE_CREATE_TEMP_INDEX);
453 DefInt(SQLITE_CREATE_TEMP_TABLE);
454 DefInt(SQLITE_CREATE_TEMP_TRIGGER);
455 DefInt(SQLITE_CREATE_TEMP_VIEW);
456 DefInt(SQLITE_CREATE_TRIGGER);
457 DefInt(SQLITE_CREATE_VIEW);
458 DefInt(SQLITE_DELETE);
459 DefInt(SQLITE_DROP_INDEX);
460 DefInt(SQLITE_DROP_TABLE);
461 DefInt(SQLITE_DROP_TEMP_INDEX);
462 DefInt(SQLITE_DROP_TEMP_TABLE);
463 DefInt(SQLITE_DROP_TEMP_TRIGGER);
464 DefInt(SQLITE_DROP_TEMP_VIEW);
465 DefInt(SQLITE_DROP_TRIGGER);
466 DefInt(SQLITE_DROP_VIEW);
467 DefInt(SQLITE_INSERT);
468 DefInt(SQLITE_PRAGMA);
469 DefInt(SQLITE_READ);
470 DefInt(SQLITE_SELECT);
471 DefInt(SQLITE_TRANSACTION);
472 DefInt(SQLITE_UPDATE);
473 DefInt(SQLITE_ATTACH);
474 DefInt(SQLITE_DETACH);
475 DefInt(SQLITE_ALTER_TABLE);
476 DefInt(SQLITE_REINDEX);
477 DefInt(SQLITE_ANALYZE);
478 DefInt(SQLITE_CREATE_VTABLE);
479 DefInt(SQLITE_DROP_VTABLE);
480 DefInt(SQLITE_FUNCTION);
481 DefInt(SQLITE_SAVEPOINT);
482 //DefInt(SQLITE_COPY) /* No longer used */;
483 DefInt(SQLITE_RECURSIVE);
484 } _DefGroup;
486 DefGroup(blobFinalizers) {
487 /* SQLITE_STATIC/TRANSIENT need to be handled explicitly as
488 ** integers to avoid casting-related warnings. */
489 out("\"SQLITE_STATIC\":0, \"SQLITE_TRANSIENT\":-1");
490 outf(",\"SQLITE_WASM_DEALLOC\": %lld",
491 (sqlite3_int64)(sqlite3_free));
492 } _DefGroup;
494 DefGroup(changeset){
495 DefInt(SQLITE_CHANGESETSTART_INVERT);
496 DefInt(SQLITE_CHANGESETAPPLY_NOSAVEPOINT);
497 DefInt(SQLITE_CHANGESETAPPLY_INVERT);
498 DefInt(SQLITE_CHANGESETAPPLY_IGNORENOOP);
500 DefInt(SQLITE_CHANGESET_DATA);
501 DefInt(SQLITE_CHANGESET_NOTFOUND);
502 DefInt(SQLITE_CHANGESET_CONFLICT);
503 DefInt(SQLITE_CHANGESET_CONSTRAINT);
504 DefInt(SQLITE_CHANGESET_FOREIGN_KEY);
506 DefInt(SQLITE_CHANGESET_OMIT);
507 DefInt(SQLITE_CHANGESET_REPLACE);
508 DefInt(SQLITE_CHANGESET_ABORT);
509 } _DefGroup;
511 DefGroup(config){
512 DefInt(SQLITE_CONFIG_SINGLETHREAD);
513 DefInt(SQLITE_CONFIG_MULTITHREAD);
514 DefInt(SQLITE_CONFIG_SERIALIZED);
515 DefInt(SQLITE_CONFIG_MALLOC);
516 DefInt(SQLITE_CONFIG_GETMALLOC);
517 DefInt(SQLITE_CONFIG_SCRATCH);
518 DefInt(SQLITE_CONFIG_PAGECACHE);
519 DefInt(SQLITE_CONFIG_HEAP);
520 DefInt(SQLITE_CONFIG_MEMSTATUS);
521 DefInt(SQLITE_CONFIG_MUTEX);
522 DefInt(SQLITE_CONFIG_GETMUTEX);
523 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
524 DefInt(SQLITE_CONFIG_LOOKASIDE);
525 DefInt(SQLITE_CONFIG_PCACHE);
526 DefInt(SQLITE_CONFIG_GETPCACHE);
527 DefInt(SQLITE_CONFIG_LOG);
528 DefInt(SQLITE_CONFIG_URI);
529 DefInt(SQLITE_CONFIG_PCACHE2);
530 DefInt(SQLITE_CONFIG_GETPCACHE2);
531 DefInt(SQLITE_CONFIG_COVERING_INDEX_SCAN);
532 DefInt(SQLITE_CONFIG_SQLLOG);
533 DefInt(SQLITE_CONFIG_MMAP_SIZE);
534 DefInt(SQLITE_CONFIG_WIN32_HEAPSIZE);
535 DefInt(SQLITE_CONFIG_PCACHE_HDRSZ);
536 DefInt(SQLITE_CONFIG_PMASZ);
537 DefInt(SQLITE_CONFIG_STMTJRNL_SPILL);
538 DefInt(SQLITE_CONFIG_SMALL_MALLOC);
539 DefInt(SQLITE_CONFIG_SORTERREF_SIZE);
540 DefInt(SQLITE_CONFIG_MEMDB_MAXSIZE);
541 } _DefGroup;
543 DefGroup(dataTypes) {
544 DefInt(SQLITE_INTEGER);
545 DefInt(SQLITE_FLOAT);
546 DefInt(SQLITE_TEXT);
547 DefInt(SQLITE_BLOB);
548 DefInt(SQLITE_NULL);
549 } _DefGroup;
551 DefGroup(dbConfig){
552 DefInt(SQLITE_DBCONFIG_MAINDBNAME);
553 DefInt(SQLITE_DBCONFIG_LOOKASIDE);
554 DefInt(SQLITE_DBCONFIG_ENABLE_FKEY);
555 DefInt(SQLITE_DBCONFIG_ENABLE_TRIGGER);
556 DefInt(SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER);
557 DefInt(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION);
558 DefInt(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE);
559 DefInt(SQLITE_DBCONFIG_ENABLE_QPSG);
560 DefInt(SQLITE_DBCONFIG_TRIGGER_EQP);
561 DefInt(SQLITE_DBCONFIG_RESET_DATABASE);
562 DefInt(SQLITE_DBCONFIG_DEFENSIVE);
563 DefInt(SQLITE_DBCONFIG_WRITABLE_SCHEMA);
564 DefInt(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE);
565 DefInt(SQLITE_DBCONFIG_DQS_DML);
566 DefInt(SQLITE_DBCONFIG_DQS_DDL);
567 DefInt(SQLITE_DBCONFIG_ENABLE_VIEW);
568 DefInt(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT);
569 DefInt(SQLITE_DBCONFIG_TRUSTED_SCHEMA);
570 DefInt(SQLITE_DBCONFIG_STMT_SCANSTATUS);
571 DefInt(SQLITE_DBCONFIG_REVERSE_SCANORDER);
572 DefInt(SQLITE_DBCONFIG_MAX);
573 } _DefGroup;
575 DefGroup(dbStatus){
576 DefInt(SQLITE_DBSTATUS_LOOKASIDE_USED);
577 DefInt(SQLITE_DBSTATUS_CACHE_USED);
578 DefInt(SQLITE_DBSTATUS_SCHEMA_USED);
579 DefInt(SQLITE_DBSTATUS_STMT_USED);
580 DefInt(SQLITE_DBSTATUS_LOOKASIDE_HIT);
581 DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE);
582 DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL);
583 DefInt(SQLITE_DBSTATUS_CACHE_HIT);
584 DefInt(SQLITE_DBSTATUS_CACHE_MISS);
585 DefInt(SQLITE_DBSTATUS_CACHE_WRITE);
586 DefInt(SQLITE_DBSTATUS_DEFERRED_FKS);
587 DefInt(SQLITE_DBSTATUS_CACHE_USED_SHARED);
588 DefInt(SQLITE_DBSTATUS_CACHE_SPILL);
589 DefInt(SQLITE_DBSTATUS_MAX);
590 } _DefGroup;
592 DefGroup(encodings) {
593 /* Noting that the wasm binding only aims to support UTF-8. */
594 DefInt(SQLITE_UTF8);
595 DefInt(SQLITE_UTF16LE);
596 DefInt(SQLITE_UTF16BE);
597 DefInt(SQLITE_UTF16);
598 /*deprecated DefInt(SQLITE_ANY); */
599 DefInt(SQLITE_UTF16_ALIGNED);
600 } _DefGroup;
602 DefGroup(fcntl) {
603 DefInt(SQLITE_FCNTL_LOCKSTATE);
604 DefInt(SQLITE_FCNTL_GET_LOCKPROXYFILE);
605 DefInt(SQLITE_FCNTL_SET_LOCKPROXYFILE);
606 DefInt(SQLITE_FCNTL_LAST_ERRNO);
607 DefInt(SQLITE_FCNTL_SIZE_HINT);
608 DefInt(SQLITE_FCNTL_CHUNK_SIZE);
609 DefInt(SQLITE_FCNTL_FILE_POINTER);
610 DefInt(SQLITE_FCNTL_SYNC_OMITTED);
611 DefInt(SQLITE_FCNTL_WIN32_AV_RETRY);
612 DefInt(SQLITE_FCNTL_PERSIST_WAL);
613 DefInt(SQLITE_FCNTL_OVERWRITE);
614 DefInt(SQLITE_FCNTL_VFSNAME);
615 DefInt(SQLITE_FCNTL_POWERSAFE_OVERWRITE);
616 DefInt(SQLITE_FCNTL_PRAGMA);
617 DefInt(SQLITE_FCNTL_BUSYHANDLER);
618 DefInt(SQLITE_FCNTL_TEMPFILENAME);
619 DefInt(SQLITE_FCNTL_MMAP_SIZE);
620 DefInt(SQLITE_FCNTL_TRACE);
621 DefInt(SQLITE_FCNTL_HAS_MOVED);
622 DefInt(SQLITE_FCNTL_SYNC);
623 DefInt(SQLITE_FCNTL_COMMIT_PHASETWO);
624 DefInt(SQLITE_FCNTL_WIN32_SET_HANDLE);
625 DefInt(SQLITE_FCNTL_WAL_BLOCK);
626 DefInt(SQLITE_FCNTL_ZIPVFS);
627 DefInt(SQLITE_FCNTL_RBU);
628 DefInt(SQLITE_FCNTL_VFS_POINTER);
629 DefInt(SQLITE_FCNTL_JOURNAL_POINTER);
630 DefInt(SQLITE_FCNTL_WIN32_GET_HANDLE);
631 DefInt(SQLITE_FCNTL_PDB);
632 DefInt(SQLITE_FCNTL_BEGIN_ATOMIC_WRITE);
633 DefInt(SQLITE_FCNTL_COMMIT_ATOMIC_WRITE);
634 DefInt(SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE);
635 DefInt(SQLITE_FCNTL_LOCK_TIMEOUT);
636 DefInt(SQLITE_FCNTL_DATA_VERSION);
637 DefInt(SQLITE_FCNTL_SIZE_LIMIT);
638 DefInt(SQLITE_FCNTL_CKPT_DONE);
639 DefInt(SQLITE_FCNTL_RESERVE_BYTES);
640 DefInt(SQLITE_FCNTL_CKPT_START);
641 DefInt(SQLITE_FCNTL_EXTERNAL_READER);
642 DefInt(SQLITE_FCNTL_CKSM_FILE);
643 DefInt(SQLITE_FCNTL_RESET_CACHE);
644 } _DefGroup;
646 DefGroup(flock) {
647 DefInt(SQLITE_LOCK_NONE);
648 DefInt(SQLITE_LOCK_SHARED);
649 DefInt(SQLITE_LOCK_RESERVED);
650 DefInt(SQLITE_LOCK_PENDING);
651 DefInt(SQLITE_LOCK_EXCLUSIVE);
652 } _DefGroup;
654 DefGroup(ioCap) {
655 DefInt(SQLITE_IOCAP_ATOMIC);
656 DefInt(SQLITE_IOCAP_ATOMIC512);
657 DefInt(SQLITE_IOCAP_ATOMIC1K);
658 DefInt(SQLITE_IOCAP_ATOMIC2K);
659 DefInt(SQLITE_IOCAP_ATOMIC4K);
660 DefInt(SQLITE_IOCAP_ATOMIC8K);
661 DefInt(SQLITE_IOCAP_ATOMIC16K);
662 DefInt(SQLITE_IOCAP_ATOMIC32K);
663 DefInt(SQLITE_IOCAP_ATOMIC64K);
664 DefInt(SQLITE_IOCAP_SAFE_APPEND);
665 DefInt(SQLITE_IOCAP_SEQUENTIAL);
666 DefInt(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN);
667 DefInt(SQLITE_IOCAP_POWERSAFE_OVERWRITE);
668 DefInt(SQLITE_IOCAP_IMMUTABLE);
669 DefInt(SQLITE_IOCAP_BATCH_ATOMIC);
670 } _DefGroup;
672 DefGroup(limits) {
673 DefInt(SQLITE_MAX_ALLOCATION_SIZE);
674 DefInt(SQLITE_LIMIT_LENGTH);
675 DefInt(SQLITE_MAX_LENGTH);
676 DefInt(SQLITE_LIMIT_SQL_LENGTH);
677 DefInt(SQLITE_MAX_SQL_LENGTH);
678 DefInt(SQLITE_LIMIT_COLUMN);
679 DefInt(SQLITE_MAX_COLUMN);
680 DefInt(SQLITE_LIMIT_EXPR_DEPTH);
681 DefInt(SQLITE_MAX_EXPR_DEPTH);
682 DefInt(SQLITE_LIMIT_COMPOUND_SELECT);
683 DefInt(SQLITE_MAX_COMPOUND_SELECT);
684 DefInt(SQLITE_LIMIT_VDBE_OP);
685 DefInt(SQLITE_MAX_VDBE_OP);
686 DefInt(SQLITE_LIMIT_FUNCTION_ARG);
687 DefInt(SQLITE_MAX_FUNCTION_ARG);
688 DefInt(SQLITE_LIMIT_ATTACHED);
689 DefInt(SQLITE_MAX_ATTACHED);
690 DefInt(SQLITE_LIMIT_LIKE_PATTERN_LENGTH);
691 DefInt(SQLITE_MAX_LIKE_PATTERN_LENGTH);
692 DefInt(SQLITE_LIMIT_VARIABLE_NUMBER);
693 DefInt(SQLITE_MAX_VARIABLE_NUMBER);
694 DefInt(SQLITE_LIMIT_TRIGGER_DEPTH);
695 DefInt(SQLITE_MAX_TRIGGER_DEPTH);
696 DefInt(SQLITE_LIMIT_WORKER_THREADS);
697 DefInt(SQLITE_MAX_WORKER_THREADS);
698 } _DefGroup;
700 DefGroup(openFlags) {
701 /* Noting that not all of these will have any effect in
702 ** WASM-space. */
703 DefInt(SQLITE_OPEN_READONLY);
704 DefInt(SQLITE_OPEN_READWRITE);
705 DefInt(SQLITE_OPEN_CREATE);
706 DefInt(SQLITE_OPEN_URI);
707 DefInt(SQLITE_OPEN_MEMORY);
708 DefInt(SQLITE_OPEN_NOMUTEX);
709 DefInt(SQLITE_OPEN_FULLMUTEX);
710 DefInt(SQLITE_OPEN_SHAREDCACHE);
711 DefInt(SQLITE_OPEN_PRIVATECACHE);
712 DefInt(SQLITE_OPEN_EXRESCODE);
713 DefInt(SQLITE_OPEN_NOFOLLOW);
714 /* OPEN flags for use with VFSes... */
715 DefInt(SQLITE_OPEN_MAIN_DB);
716 DefInt(SQLITE_OPEN_MAIN_JOURNAL);
717 DefInt(SQLITE_OPEN_TEMP_DB);
718 DefInt(SQLITE_OPEN_TEMP_JOURNAL);
719 DefInt(SQLITE_OPEN_TRANSIENT_DB);
720 DefInt(SQLITE_OPEN_SUBJOURNAL);
721 DefInt(SQLITE_OPEN_SUPER_JOURNAL);
722 DefInt(SQLITE_OPEN_WAL);
723 DefInt(SQLITE_OPEN_DELETEONCLOSE);
724 DefInt(SQLITE_OPEN_EXCLUSIVE);
725 } _DefGroup;
727 DefGroup(prepareFlags) {
728 DefInt(SQLITE_PREPARE_PERSISTENT);
729 DefInt(SQLITE_PREPARE_NORMALIZE);
730 DefInt(SQLITE_PREPARE_NO_VTAB);
731 } _DefGroup;
733 DefGroup(resultCodes) {
734 DefInt(SQLITE_OK);
735 DefInt(SQLITE_ERROR);
736 DefInt(SQLITE_INTERNAL);
737 DefInt(SQLITE_PERM);
738 DefInt(SQLITE_ABORT);
739 DefInt(SQLITE_BUSY);
740 DefInt(SQLITE_LOCKED);
741 DefInt(SQLITE_NOMEM);
742 DefInt(SQLITE_READONLY);
743 DefInt(SQLITE_INTERRUPT);
744 DefInt(SQLITE_IOERR);
745 DefInt(SQLITE_CORRUPT);
746 DefInt(SQLITE_NOTFOUND);
747 DefInt(SQLITE_FULL);
748 DefInt(SQLITE_CANTOPEN);
749 DefInt(SQLITE_PROTOCOL);
750 DefInt(SQLITE_EMPTY);
751 DefInt(SQLITE_SCHEMA);
752 DefInt(SQLITE_TOOBIG);
753 DefInt(SQLITE_CONSTRAINT);
754 DefInt(SQLITE_MISMATCH);
755 DefInt(SQLITE_MISUSE);
756 DefInt(SQLITE_NOLFS);
757 DefInt(SQLITE_AUTH);
758 DefInt(SQLITE_FORMAT);
759 DefInt(SQLITE_RANGE);
760 DefInt(SQLITE_NOTADB);
761 DefInt(SQLITE_NOTICE);
762 DefInt(SQLITE_WARNING);
763 DefInt(SQLITE_ROW);
764 DefInt(SQLITE_DONE);
765 // Extended Result Codes
766 DefInt(SQLITE_ERROR_MISSING_COLLSEQ);
767 DefInt(SQLITE_ERROR_RETRY);
768 DefInt(SQLITE_ERROR_SNAPSHOT);
769 DefInt(SQLITE_IOERR_READ);
770 DefInt(SQLITE_IOERR_SHORT_READ);
771 DefInt(SQLITE_IOERR_WRITE);
772 DefInt(SQLITE_IOERR_FSYNC);
773 DefInt(SQLITE_IOERR_DIR_FSYNC);
774 DefInt(SQLITE_IOERR_TRUNCATE);
775 DefInt(SQLITE_IOERR_FSTAT);
776 DefInt(SQLITE_IOERR_UNLOCK);
777 DefInt(SQLITE_IOERR_RDLOCK);
778 DefInt(SQLITE_IOERR_DELETE);
779 DefInt(SQLITE_IOERR_BLOCKED);
780 DefInt(SQLITE_IOERR_NOMEM);
781 DefInt(SQLITE_IOERR_ACCESS);
782 DefInt(SQLITE_IOERR_CHECKRESERVEDLOCK);
783 DefInt(SQLITE_IOERR_LOCK);
784 DefInt(SQLITE_IOERR_CLOSE);
785 DefInt(SQLITE_IOERR_DIR_CLOSE);
786 DefInt(SQLITE_IOERR_SHMOPEN);
787 DefInt(SQLITE_IOERR_SHMSIZE);
788 DefInt(SQLITE_IOERR_SHMLOCK);
789 DefInt(SQLITE_IOERR_SHMMAP);
790 DefInt(SQLITE_IOERR_SEEK);
791 DefInt(SQLITE_IOERR_DELETE_NOENT);
792 DefInt(SQLITE_IOERR_MMAP);
793 DefInt(SQLITE_IOERR_GETTEMPPATH);
794 DefInt(SQLITE_IOERR_CONVPATH);
795 DefInt(SQLITE_IOERR_VNODE);
796 DefInt(SQLITE_IOERR_AUTH);
797 DefInt(SQLITE_IOERR_BEGIN_ATOMIC);
798 DefInt(SQLITE_IOERR_COMMIT_ATOMIC);
799 DefInt(SQLITE_IOERR_ROLLBACK_ATOMIC);
800 DefInt(SQLITE_IOERR_DATA);
801 DefInt(SQLITE_IOERR_CORRUPTFS);
802 DefInt(SQLITE_LOCKED_SHAREDCACHE);
803 DefInt(SQLITE_LOCKED_VTAB);
804 DefInt(SQLITE_BUSY_RECOVERY);
805 DefInt(SQLITE_BUSY_SNAPSHOT);
806 DefInt(SQLITE_BUSY_TIMEOUT);
807 DefInt(SQLITE_CANTOPEN_NOTEMPDIR);
808 DefInt(SQLITE_CANTOPEN_ISDIR);
809 DefInt(SQLITE_CANTOPEN_FULLPATH);
810 DefInt(SQLITE_CANTOPEN_CONVPATH);
811 //DefInt(SQLITE_CANTOPEN_DIRTYWAL)/*docs say not used*/;
812 DefInt(SQLITE_CANTOPEN_SYMLINK);
813 DefInt(SQLITE_CORRUPT_VTAB);
814 DefInt(SQLITE_CORRUPT_SEQUENCE);
815 DefInt(SQLITE_CORRUPT_INDEX);
816 DefInt(SQLITE_READONLY_RECOVERY);
817 DefInt(SQLITE_READONLY_CANTLOCK);
818 DefInt(SQLITE_READONLY_ROLLBACK);
819 DefInt(SQLITE_READONLY_DBMOVED);
820 DefInt(SQLITE_READONLY_CANTINIT);
821 DefInt(SQLITE_READONLY_DIRECTORY);
822 DefInt(SQLITE_ABORT_ROLLBACK);
823 DefInt(SQLITE_CONSTRAINT_CHECK);
824 DefInt(SQLITE_CONSTRAINT_COMMITHOOK);
825 DefInt(SQLITE_CONSTRAINT_FOREIGNKEY);
826 DefInt(SQLITE_CONSTRAINT_FUNCTION);
827 DefInt(SQLITE_CONSTRAINT_NOTNULL);
828 DefInt(SQLITE_CONSTRAINT_PRIMARYKEY);
829 DefInt(SQLITE_CONSTRAINT_TRIGGER);
830 DefInt(SQLITE_CONSTRAINT_UNIQUE);
831 DefInt(SQLITE_CONSTRAINT_VTAB);
832 DefInt(SQLITE_CONSTRAINT_ROWID);
833 DefInt(SQLITE_CONSTRAINT_PINNED);
834 DefInt(SQLITE_CONSTRAINT_DATATYPE);
835 DefInt(SQLITE_NOTICE_RECOVER_WAL);
836 DefInt(SQLITE_NOTICE_RECOVER_ROLLBACK);
837 DefInt(SQLITE_WARNING_AUTOINDEX);
838 DefInt(SQLITE_AUTH_USER);
839 DefInt(SQLITE_OK_LOAD_PERMANENTLY);
840 //DefInt(SQLITE_OK_SYMLINK) /* internal use only */;
841 } _DefGroup;
843 DefGroup(serialize){
844 DefInt(SQLITE_SERIALIZE_NOCOPY);
845 DefInt(SQLITE_DESERIALIZE_FREEONCLOSE);
846 DefInt(SQLITE_DESERIALIZE_READONLY);
847 DefInt(SQLITE_DESERIALIZE_RESIZEABLE);
848 } _DefGroup;
850 DefGroup(session){
851 DefInt(SQLITE_SESSION_CONFIG_STRMSIZE);
852 DefInt(SQLITE_SESSION_OBJCONFIG_SIZE);
853 } _DefGroup;
855 DefGroup(sqlite3Status){
856 DefInt(SQLITE_STATUS_MEMORY_USED);
857 DefInt(SQLITE_STATUS_PAGECACHE_USED);
858 DefInt(SQLITE_STATUS_PAGECACHE_OVERFLOW);
859 //DefInt(SQLITE_STATUS_SCRATCH_USED) /* NOT USED */;
860 //DefInt(SQLITE_STATUS_SCRATCH_OVERFLOW) /* NOT USED */;
861 DefInt(SQLITE_STATUS_MALLOC_SIZE);
862 DefInt(SQLITE_STATUS_PARSER_STACK);
863 DefInt(SQLITE_STATUS_PAGECACHE_SIZE);
864 //DefInt(SQLITE_STATUS_SCRATCH_SIZE) /* NOT USED */;
865 DefInt(SQLITE_STATUS_MALLOC_COUNT);
866 } _DefGroup;
868 DefGroup(stmtStatus){
869 DefInt(SQLITE_STMTSTATUS_FULLSCAN_STEP);
870 DefInt(SQLITE_STMTSTATUS_SORT);
871 DefInt(SQLITE_STMTSTATUS_AUTOINDEX);
872 DefInt(SQLITE_STMTSTATUS_VM_STEP);
873 DefInt(SQLITE_STMTSTATUS_REPREPARE);
874 DefInt(SQLITE_STMTSTATUS_RUN);
875 DefInt(SQLITE_STMTSTATUS_FILTER_MISS);
876 DefInt(SQLITE_STMTSTATUS_FILTER_HIT);
877 DefInt(SQLITE_STMTSTATUS_MEMUSED);
878 } _DefGroup;
880 DefGroup(syncFlags) {
881 DefInt(SQLITE_SYNC_NORMAL);
882 DefInt(SQLITE_SYNC_FULL);
883 DefInt(SQLITE_SYNC_DATAONLY);
884 } _DefGroup;
886 DefGroup(trace) {
887 DefInt(SQLITE_TRACE_STMT);
888 DefInt(SQLITE_TRACE_PROFILE);
889 DefInt(SQLITE_TRACE_ROW);
890 DefInt(SQLITE_TRACE_CLOSE);
891 } _DefGroup;
893 DefGroup(txnState){
894 DefInt(SQLITE_TXN_NONE);
895 DefInt(SQLITE_TXN_READ);
896 DefInt(SQLITE_TXN_WRITE);
897 } _DefGroup;
899 DefGroup(udfFlags) {
900 DefInt(SQLITE_DETERMINISTIC);
901 DefInt(SQLITE_DIRECTONLY);
902 DefInt(SQLITE_INNOCUOUS);
903 } _DefGroup;
905 DefGroup(version) {
906 DefInt(SQLITE_VERSION_NUMBER);
907 DefStr(SQLITE_VERSION);
908 DefStr(SQLITE_SOURCE_ID);
909 } _DefGroup;
911 DefGroup(vtab) {
912 DefInt(SQLITE_INDEX_SCAN_UNIQUE);
913 DefInt(SQLITE_INDEX_CONSTRAINT_EQ);
914 DefInt(SQLITE_INDEX_CONSTRAINT_GT);
915 DefInt(SQLITE_INDEX_CONSTRAINT_LE);
916 DefInt(SQLITE_INDEX_CONSTRAINT_LT);
917 DefInt(SQLITE_INDEX_CONSTRAINT_GE);
918 DefInt(SQLITE_INDEX_CONSTRAINT_MATCH);
919 DefInt(SQLITE_INDEX_CONSTRAINT_LIKE);
920 DefInt(SQLITE_INDEX_CONSTRAINT_GLOB);
921 DefInt(SQLITE_INDEX_CONSTRAINT_REGEXP);
922 DefInt(SQLITE_INDEX_CONSTRAINT_NE);
923 DefInt(SQLITE_INDEX_CONSTRAINT_ISNOT);
924 DefInt(SQLITE_INDEX_CONSTRAINT_ISNOTNULL);
925 DefInt(SQLITE_INDEX_CONSTRAINT_ISNULL);
926 DefInt(SQLITE_INDEX_CONSTRAINT_IS);
927 DefInt(SQLITE_INDEX_CONSTRAINT_LIMIT);
928 DefInt(SQLITE_INDEX_CONSTRAINT_OFFSET);
929 DefInt(SQLITE_INDEX_CONSTRAINT_FUNCTION);
930 DefInt(SQLITE_VTAB_CONSTRAINT_SUPPORT);
931 DefInt(SQLITE_VTAB_INNOCUOUS);
932 DefInt(SQLITE_VTAB_DIRECTONLY);
933 DefInt(SQLITE_VTAB_USES_ALL_SCHEMAS);
934 DefInt(SQLITE_ROLLBACK);
935 //DefInt(SQLITE_IGNORE); // Also used by sqlite3_authorizer() callback
936 DefInt(SQLITE_FAIL);
937 //DefInt(SQLITE_ABORT); // Also an error code
938 DefInt(SQLITE_REPLACE);
939 } _DefGroup;
941 #undef DefGroup
942 #undef DefStr
943 #undef DefInt
944 #undef _DefGroup
947 ** Emit an array of "StructBinder" struct descripions, which look
948 ** like:
950 ** {
951 ** "name": "MyStruct",
952 ** "sizeof": 16,
953 ** "members": {
954 ** "member1": {"offset": 0,"sizeof": 4,"signature": "i"},
955 ** "member2": {"offset": 4,"sizeof": 4,"signature": "p"},
956 ** "member3": {"offset": 8,"sizeof": 8,"signature": "j"}
957 ** }
958 ** }
960 ** Detailed documentation for those bits are in the docs for the
961 ** Jaccwabyt JS-side component.
964 /** Macros for emitting StructBinder description. */
965 #define StructBinder__(TYPE) \
966 n = 0; \
967 outf("%s{", (nStruct++ ? ", " : "")); \
968 out("\"name\": \"" # TYPE "\","); \
969 outf("\"sizeof\": %d", (int)sizeof(TYPE)); \
970 out(",\"members\": {");
971 #define StructBinder_(T) StructBinder__(T)
972 /** ^^^ indirection needed to expand CurrentStruct */
973 #define StructBinder StructBinder_(CurrentStruct)
974 #define _StructBinder CloseBrace(2)
975 #define M(MEMBER,SIG) \
976 outf("%s\"%s\": " \
977 "{\"offset\":%d,\"sizeof\": %d,\"signature\":\"%s\"}", \
978 (n++ ? ", " : ""), #MEMBER, \
979 (int)offsetof(CurrentStruct,MEMBER), \
980 (int)sizeof(((CurrentStruct*)0)->MEMBER), \
981 SIG)
983 nStruct = 0;
984 out(", \"structs\": ["); {
986 #define CurrentStruct sqlite3_vfs
987 StructBinder {
988 M(iVersion, "i");
989 M(szOsFile, "i");
990 M(mxPathname, "i");
991 M(pNext, "p");
992 M(zName, "s");
993 M(pAppData, "p");
994 M(xOpen, "i(pppip)");
995 M(xDelete, "i(ppi)");
996 M(xAccess, "i(ppip)");
997 M(xFullPathname, "i(ppip)");
998 M(xDlOpen, "p(pp)");
999 M(xDlError, "p(pip)");
1000 M(xDlSym, "p()");
1001 M(xDlClose, "v(pp)");
1002 M(xRandomness, "i(pip)");
1003 M(xSleep, "i(pi)");
1004 M(xCurrentTime, "i(pp)");
1005 M(xGetLastError, "i(pip)");
1006 M(xCurrentTimeInt64, "i(pp)");
1007 M(xSetSystemCall, "i(ppp)");
1008 M(xGetSystemCall, "p(pp)");
1009 M(xNextSystemCall, "p(pp)");
1010 } _StructBinder;
1011 #undef CurrentStruct
1013 #define CurrentStruct sqlite3_io_methods
1014 StructBinder {
1015 M(iVersion, "i");
1016 M(xClose, "i(p)");
1017 M(xRead, "i(ppij)");
1018 M(xWrite, "i(ppij)");
1019 M(xTruncate, "i(pj)");
1020 M(xSync, "i(pi)");
1021 M(xFileSize, "i(pp)");
1022 M(xLock, "i(pi)");
1023 M(xUnlock, "i(pi)");
1024 M(xCheckReservedLock, "i(pp)");
1025 M(xFileControl, "i(pip)");
1026 M(xSectorSize, "i(p)");
1027 M(xDeviceCharacteristics, "i(p)");
1028 M(xShmMap, "i(piiip)");
1029 M(xShmLock, "i(piii)");
1030 M(xShmBarrier, "v(p)");
1031 M(xShmUnmap, "i(pi)");
1032 M(xFetch, "i(pjip)");
1033 M(xUnfetch, "i(pjp)");
1034 } _StructBinder;
1035 #undef CurrentStruct
1037 #define CurrentStruct sqlite3_file
1038 StructBinder {
1039 M(pMethods, "p");
1040 } _StructBinder;
1041 #undef CurrentStruct
1043 #define CurrentStruct sqlite3_kvvfs_methods
1044 StructBinder {
1045 M(xRead, "i(sspi)");
1046 M(xWrite, "i(sss)");
1047 M(xDelete, "i(ss)");
1048 M(nKeySize, "i");
1049 } _StructBinder;
1050 #undef CurrentStruct
1053 #define CurrentStruct sqlite3_vtab
1054 StructBinder {
1055 M(pModule, "p");
1056 M(nRef, "i");
1057 M(zErrMsg, "p");
1058 } _StructBinder;
1059 #undef CurrentStruct
1061 #define CurrentStruct sqlite3_vtab_cursor
1062 StructBinder {
1063 M(pVtab, "p");
1064 } _StructBinder;
1065 #undef CurrentStruct
1067 #define CurrentStruct sqlite3_module
1068 StructBinder {
1069 M(iVersion, "i");
1070 M(xCreate, "i(ppippp)");
1071 M(xConnect, "i(ppippp)");
1072 M(xBestIndex, "i(pp)");
1073 M(xDisconnect, "i(p)");
1074 M(xDestroy, "i(p)");
1075 M(xOpen, "i(pp)");
1076 M(xClose, "i(p)");
1077 M(xFilter, "i(pisip)");
1078 M(xNext, "i(p)");
1079 M(xEof, "i(p)");
1080 M(xColumn, "i(ppi)");
1081 M(xRowid, "i(pp)");
1082 M(xUpdate, "i(pipp)");
1083 M(xBegin, "i(p)");
1084 M(xSync, "i(p)");
1085 M(xCommit, "i(p)");
1086 M(xRollback, "i(p)");
1087 M(xFindFunction, "i(pispp)");
1088 M(xRename, "i(ps)");
1089 // ^^^ v1. v2+ follows...
1090 M(xSavepoint, "i(pi)");
1091 M(xRelease, "i(pi)");
1092 M(xRollbackTo, "i(pi)");
1093 // ^^^ v2. v3+ follows...
1094 M(xShadowName, "i(s)");
1095 } _StructBinder;
1096 #undef CurrentStruct
1099 ** Workaround: in order to map the various inner structs from
1100 ** sqlite3_index_info, we have to uplift those into constructs we
1101 ** can access by type name. These structs _must_ match their
1102 ** in-sqlite3_index_info counterparts byte for byte.
1104 typedef struct {
1105 int iColumn;
1106 unsigned char op;
1107 unsigned char usable;
1108 int iTermOffset;
1109 } sqlite3_index_constraint;
1110 typedef struct {
1111 int iColumn;
1112 unsigned char desc;
1113 } sqlite3_index_orderby;
1114 typedef struct {
1115 int argvIndex;
1116 unsigned char omit;
1117 } sqlite3_index_constraint_usage;
1118 { /* Validate that the above struct sizeof()s match
1119 ** expectations. We could improve upon this by
1120 ** checking the offsetof() for each member. */
1121 const sqlite3_index_info siiCheck;
1122 #define IndexSzCheck(T,M) \
1123 (sizeof(T) == sizeof(*siiCheck.M))
1124 if(!IndexSzCheck(sqlite3_index_constraint,aConstraint)
1125 || !IndexSzCheck(sqlite3_index_orderby,aOrderBy)
1126 || !IndexSzCheck(sqlite3_index_constraint_usage,aConstraintUsage)){
1127 assert(!"sizeof mismatch in sqlite3_index_... struct(s)");
1128 return 0;
1130 #undef IndexSzCheck
1133 #define CurrentStruct sqlite3_index_constraint
1134 StructBinder {
1135 M(iColumn, "i");
1136 M(op, "C");
1137 M(usable, "C");
1138 M(iTermOffset, "i");
1139 } _StructBinder;
1140 #undef CurrentStruct
1142 #define CurrentStruct sqlite3_index_orderby
1143 StructBinder {
1144 M(iColumn, "i");
1145 M(desc, "C");
1146 } _StructBinder;
1147 #undef CurrentStruct
1149 #define CurrentStruct sqlite3_index_constraint_usage
1150 StructBinder {
1151 M(argvIndex, "i");
1152 M(omit, "C");
1153 } _StructBinder;
1154 #undef CurrentStruct
1156 #define CurrentStruct sqlite3_index_info
1157 StructBinder {
1158 M(nConstraint, "i");
1159 M(aConstraint, "p");
1160 M(nOrderBy, "i");
1161 M(aOrderBy, "p");
1162 M(aConstraintUsage, "p");
1163 M(idxNum, "i");
1164 M(idxStr, "p");
1165 M(needToFreeIdxStr, "i");
1166 M(orderByConsumed, "i");
1167 M(estimatedCost, "d");
1168 M(estimatedRows, "j");
1169 M(idxFlags, "i");
1170 M(colUsed, "j");
1171 } _StructBinder;
1172 #undef CurrentStruct
1174 #if SQLITE_WASM_TESTS
1175 #define CurrentStruct WasmTestStruct
1176 StructBinder {
1177 M(v4, "i");
1178 M(cstr, "s");
1179 M(ppV, "p");
1180 M(v8, "j");
1181 M(xFunc, "v(p)");
1182 } _StructBinder;
1183 #undef CurrentStruct
1184 #endif
1186 } out( "]"/*structs*/);
1188 out("}"/*top-level object*/);
1189 *zPos = 0;
1190 aBuffer[0] = '{'/*end of the race-condition workaround*/;
1191 return aBuffer;
1192 #undef StructBinder
1193 #undef StructBinder_
1194 #undef StructBinder__
1195 #undef M
1196 #undef _StructBinder
1197 #undef CloseBrace
1198 #undef out
1199 #undef outf
1200 #undef lenCheck
1204 ** This function is NOT part of the sqlite3 public API. It is strictly
1205 ** for use by the sqlite project's own JS/WASM bindings.
1207 ** This function invokes the xDelete method of the given VFS (or the
1208 ** default VFS if pVfs is NULL), passing on the given filename. If
1209 ** zName is NULL, no default VFS is found, or it has no xDelete
1210 ** method, SQLITE_MISUSE is returned, else the result of the xDelete()
1211 ** call is returned.
1213 SQLITE_WASM_EXPORT
1214 int sqlite3_wasm_vfs_unlink(sqlite3_vfs *pVfs, const char *zName){
1215 int rc = SQLITE_MISUSE /* ??? */;
1216 if( 0==pVfs && 0!=zName ) pVfs = sqlite3_vfs_find(0);
1217 if( zName && pVfs && pVfs->xDelete ){
1218 rc = pVfs->xDelete(pVfs, zName, 1);
1220 return rc;
1224 ** This function is NOT part of the sqlite3 public API. It is strictly
1225 ** for use by the sqlite project's own JS/WASM bindings.
1227 ** Returns a pointer to the given DB's VFS for the given DB name,
1228 ** defaulting to "main" if zDbName is 0. Returns 0 if no db with the
1229 ** given name is open.
1231 SQLITE_WASM_EXPORT
1232 sqlite3_vfs * sqlite3_wasm_db_vfs(sqlite3 *pDb, const char *zDbName){
1233 sqlite3_vfs * pVfs = 0;
1234 sqlite3_file_control(pDb, zDbName ? zDbName : "main",
1235 SQLITE_FCNTL_VFS_POINTER, &pVfs);
1236 return pVfs;
1240 ** This function is NOT part of the sqlite3 public API. It is strictly
1241 ** for use by the sqlite project's own JS/WASM bindings.
1243 ** This function resets the given db pointer's database as described at
1245 ** https://sqlite.org/c3ref/c_dbconfig_defensive.html#sqlitedbconfigresetdatabase
1247 ** But beware: virtual tables destroyed that way do not have their
1248 ** xDestroy() called, so will leak if they require that function for
1249 ** proper cleanup.
1251 ** Returns 0 on success, an SQLITE_xxx code on error. Returns
1252 ** SQLITE_MISUSE if pDb is NULL.
1254 SQLITE_WASM_EXPORT
1255 int sqlite3_wasm_db_reset(sqlite3 *pDb){
1256 int rc = SQLITE_MISUSE;
1257 if( pDb ){
1258 sqlite3_table_column_metadata(pDb, "main", 0, 0, 0, 0, 0, 0, 0);
1259 rc = sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
1260 if( 0==rc ){
1261 rc = sqlite3_exec(pDb, "VACUUM", 0, 0, 0);
1262 sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
1265 return rc;
1269 ** This function is NOT part of the sqlite3 public API. It is strictly
1270 ** for use by the sqlite project's own JS/WASM bindings.
1272 ** Uses the given database's VFS xRead to stream the db file's
1273 ** contents out to the given callback. The callback gets a single
1274 ** chunk of size n (its 2nd argument) on each call and must return 0
1275 ** on success, non-0 on error. This function returns 0 on success,
1276 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
1277 ** code from the callback. Note that this is not thread-friendly: it
1278 ** expects that it will be the only thread reading the db file and
1279 ** takes no measures to ensure that is the case.
1281 ** This implementation appears to work fine, but
1282 ** sqlite3_wasm_db_serialize() is arguably the better way to achieve
1283 ** this.
1285 SQLITE_WASM_EXPORT
1286 int sqlite3_wasm_db_export_chunked( sqlite3* pDb,
1287 int (*xCallback)(unsigned const char *zOut, int n) ){
1288 sqlite3_int64 nSize = 0;
1289 sqlite3_int64 nPos = 0;
1290 sqlite3_file * pFile = 0;
1291 unsigned char buf[1024 * 8];
1292 int nBuf = (int)sizeof(buf);
1293 int rc = pDb
1294 ? sqlite3_file_control(pDb, "main",
1295 SQLITE_FCNTL_FILE_POINTER, &pFile)
1296 : SQLITE_NOTFOUND;
1297 if( rc ) return rc;
1298 rc = pFile->pMethods->xFileSize(pFile, &nSize);
1299 if( rc ) return rc;
1300 if(nSize % nBuf){
1301 /* DB size is not an even multiple of the buffer size. Reduce
1302 ** buffer size so that we do not unduly inflate the db size
1303 ** with zero-padding when exporting. */
1304 if(0 == nSize % 4096) nBuf = 4096;
1305 else if(0 == nSize % 2048) nBuf = 2048;
1306 else if(0 == nSize % 1024) nBuf = 1024;
1307 else nBuf = 512;
1309 for( ; 0==rc && nPos<nSize; nPos += nBuf ){
1310 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
1311 if( SQLITE_IOERR_SHORT_READ == rc ){
1312 rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
1314 if( 0==rc ) rc = xCallback(buf, nBuf);
1316 return rc;
1320 ** This function is NOT part of the sqlite3 public API. It is strictly
1321 ** for use by the sqlite project's own JS/WASM bindings.
1323 ** A proxy for sqlite3_serialize() which serializes the schema zSchema
1324 ** of pDb, placing the serialized output in pOut and nOut. nOut may be
1325 ** NULL. If zSchema is NULL then "main" is assumed. If pDb or pOut are
1326 ** NULL then SQLITE_MISUSE is returned. If allocation of the
1327 ** serialized copy fails, SQLITE_NOMEM is returned. On success, 0 is
1328 ** returned and `*pOut` will contain a pointer to the memory unless
1329 ** mFlags includes SQLITE_SERIALIZE_NOCOPY and the database has no
1330 ** contiguous memory representation, in which case `*pOut` will be
1331 ** NULL but 0 will be returned.
1333 ** If `*pOut` is not NULL, the caller is responsible for passing it to
1334 ** sqlite3_free() to free it.
1336 SQLITE_WASM_EXPORT
1337 int sqlite3_wasm_db_serialize( sqlite3 *pDb, const char *zSchema,
1338 unsigned char **pOut,
1339 sqlite3_int64 *nOut, unsigned int mFlags ){
1340 unsigned char * z;
1341 if( !pDb || !pOut ) return SQLITE_MISUSE;
1342 if( nOut ) *nOut = 0;
1343 z = sqlite3_serialize(pDb, zSchema ? zSchema : "main", nOut, mFlags);
1344 if( z || (SQLITE_SERIALIZE_NOCOPY & mFlags) ){
1345 *pOut = z;
1346 return 0;
1347 }else{
1348 return SQLITE_NOMEM;
1353 ** This function is NOT part of the sqlite3 public API. It is strictly
1354 ** for use by the sqlite project's own JS/WASM bindings.
1356 ** Creates a new file using the I/O API of the given VFS, containing
1357 ** the given number of bytes of the given data. If the file exists, it
1358 ** is truncated to the given length and populated with the given
1359 ** data.
1361 ** This function exists so that we can implement the equivalent of
1362 ** Emscripten's FS.createDataFile() in a VFS-agnostic way. This
1363 ** functionality is intended for use in uploading database files.
1365 ** Not all VFSes support this functionality, e.g. the "kvvfs" does
1366 ** not.
1368 ** If pVfs is NULL, sqlite3_vfs_find(0) is used.
1370 ** If zFile is NULL, pVfs is NULL (and sqlite3_vfs_find(0) returns
1371 ** NULL), or nData is negative, SQLITE_MISUSE are returned.
1373 ** On success, it creates a new file with the given name, populated
1374 ** with the fist nData bytes of pData. If pData is NULL, the file is
1375 ** created and/or truncated to nData bytes.
1377 ** Whether or not directory components of zFilename are created
1378 ** automatically or not is unspecified: that detail is left to the
1379 ** VFS. The "opfs" VFS, for example, creates them.
1381 ** If an error happens while populating or truncating the file, the
1382 ** target file will be deleted (if needed) if this function created
1383 ** it. If this function did not create it, it is not deleted but may
1384 ** be left in an undefined state.
1386 ** Returns 0 on success. On error, it returns a code described above
1387 ** or propagates a code from one of the I/O methods.
1389 ** Design note: nData is an integer, instead of int64, for WASM
1390 ** portability, so that the API can still work in builds where BigInt
1391 ** support is disabled or unavailable.
1393 SQLITE_WASM_EXPORT
1394 int sqlite3_wasm_vfs_create_file( sqlite3_vfs *pVfs,
1395 const char *zFilename,
1396 const unsigned char * pData,
1397 int nData ){
1398 int rc;
1399 sqlite3_file *pFile = 0;
1400 sqlite3_io_methods const *pIo;
1401 const int openFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
1402 int flagsOut = 0;
1403 int fileExisted = 0;
1404 int doUnlock = 0;
1405 const unsigned char *pPos = pData;
1406 const int blockSize = 512
1407 /* Because we are using pFile->pMethods->xWrite() for writing, and
1408 ** it may have a buffer limit related to sqlite3's pager size, we
1409 ** conservatively write in 512-byte blocks (smallest page
1410 ** size). */;
1411 //fprintf(stderr, "pVfs=%p, zFilename=%s, nData=%d\n", pVfs, zFilename, nData);
1412 if( !pVfs ) pVfs = sqlite3_vfs_find(0);
1413 if( !pVfs || !zFilename || nData<0 ) return SQLITE_MISUSE;
1414 pVfs->xAccess(pVfs, zFilename, SQLITE_ACCESS_EXISTS, &fileExisted);
1415 rc = sqlite3OsOpenMalloc(pVfs, zFilename, &pFile, openFlags, &flagsOut);
1416 #if 0
1417 # define RC fprintf(stderr,"create_file(%s,%s) @%d rc=%d\n", \
1418 pVfs->zName, zFilename, __LINE__, rc);
1419 #else
1420 # define RC
1421 #endif
1423 if(rc) return rc;
1424 pIo = pFile->pMethods;
1425 if( pIo->xLock ) {
1426 /* We need xLock() in order to accommodate the OPFS VFS, as it
1427 ** obtains a writeable handle via the lock operation and releases
1428 ** it in xUnlock(). If we don't do those here, we have to add code
1429 ** to the VFS to account check whether it was locked before
1430 ** xFileSize(), xTruncate(), and the like, and release the lock
1431 ** only if it was unlocked when the op was started. */
1432 rc = pIo->xLock(pFile, SQLITE_LOCK_EXCLUSIVE);
1434 doUnlock = 0==rc;
1436 if( 0==rc ){
1437 rc = pIo->xTruncate(pFile, nData);
1440 if( 0==rc && 0!=pData && nData>0 ){
1441 while( 0==rc && nData>0 ){
1442 const int n = nData>=blockSize ? blockSize : nData;
1443 rc = pIo->xWrite(pFile, pPos, n, (sqlite3_int64)(pPos - pData));
1445 nData -= n;
1446 pPos += n;
1448 if( 0==rc && nData>0 ){
1449 assert( nData<blockSize );
1450 rc = pIo->xWrite(pFile, pPos, nData,
1451 (sqlite3_int64)(pPos - pData));
1455 if( pIo->xUnlock && doUnlock!=0 ){
1456 pIo->xUnlock(pFile, SQLITE_LOCK_NONE);
1458 pIo->xClose(pFile);
1459 if( rc!=0 && 0==fileExisted ){
1460 pVfs->xDelete(pVfs, zFilename, 1);
1463 #undef RC
1464 return rc;
1468 ** This function is NOT part of the sqlite3 public API. It is strictly
1469 ** for use by the sqlite project's own JS/WASM bindings.
1471 ** Allocates sqlite3KvvfsMethods.nKeySize bytes from
1472 ** sqlite3_wasm_pstack_alloc() and returns 0 if that allocation fails,
1473 ** else it passes that string to kvstorageMakeKey() and returns a
1474 ** NUL-terminated pointer to that string. It is up to the caller to
1475 ** use sqlite3_wasm_pstack_restore() to free the returned pointer.
1477 SQLITE_WASM_EXPORT
1478 char * sqlite3_wasm_kvvfsMakeKeyOnPstack(const char *zClass,
1479 const char *zKeyIn){
1480 assert(sqlite3KvvfsMethods.nKeySize>24);
1481 char *zKeyOut =
1482 (char *)sqlite3_wasm_pstack_alloc(sqlite3KvvfsMethods.nKeySize);
1483 if(zKeyOut){
1484 kvstorageMakeKey(zClass, zKeyIn, zKeyOut);
1486 return zKeyOut;
1490 ** This function is NOT part of the sqlite3 public API. It is strictly
1491 ** for use by the sqlite project's own JS/WASM bindings.
1493 ** Returns the pointer to the singleton object which holds the kvvfs
1494 ** I/O methods and associated state.
1496 SQLITE_WASM_EXPORT
1497 sqlite3_kvvfs_methods * sqlite3_wasm_kvvfs_methods(void){
1498 return &sqlite3KvvfsMethods;
1502 ** This function is NOT part of the sqlite3 public API. It is strictly
1503 ** for use by the sqlite project's own JS/WASM bindings.
1505 ** This is a proxy for the variadic sqlite3_vtab_config() which passes
1506 ** its argument on, or not, to sqlite3_vtab_config(), depending on the
1507 ** value of its 2nd argument. Returns the result of
1508 ** sqlite3_vtab_config(), or SQLITE_MISUSE if the 2nd arg is not a
1509 ** valid value.
1511 SQLITE_WASM_EXPORT
1512 int sqlite3_wasm_vtab_config(sqlite3 *pDb, int op, int arg){
1513 switch(op){
1514 case SQLITE_VTAB_DIRECTONLY:
1515 case SQLITE_VTAB_INNOCUOUS:
1516 return sqlite3_vtab_config(pDb, op);
1517 case SQLITE_VTAB_CONSTRAINT_SUPPORT:
1518 return sqlite3_vtab_config(pDb, op, arg);
1519 default:
1520 return SQLITE_MISUSE;
1525 ** This function is NOT part of the sqlite3 public API. It is strictly
1526 ** for use by the sqlite project's own JS/WASM bindings.
1528 ** Wrapper for the variants of sqlite3_db_config() which take
1529 ** (int,int*) variadic args.
1531 SQLITE_WASM_EXPORT
1532 int sqlite3_wasm_db_config_ip(sqlite3 *pDb, int op, int arg1, int* pArg2){
1533 switch(op){
1534 case SQLITE_DBCONFIG_ENABLE_FKEY:
1535 case SQLITE_DBCONFIG_ENABLE_TRIGGER:
1536 case SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER:
1537 case SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION:
1538 case SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE:
1539 case SQLITE_DBCONFIG_ENABLE_QPSG:
1540 case SQLITE_DBCONFIG_TRIGGER_EQP:
1541 case SQLITE_DBCONFIG_RESET_DATABASE:
1542 case SQLITE_DBCONFIG_DEFENSIVE:
1543 case SQLITE_DBCONFIG_WRITABLE_SCHEMA:
1544 case SQLITE_DBCONFIG_LEGACY_ALTER_TABLE:
1545 case SQLITE_DBCONFIG_DQS_DML:
1546 case SQLITE_DBCONFIG_DQS_DDL:
1547 case SQLITE_DBCONFIG_ENABLE_VIEW:
1548 case SQLITE_DBCONFIG_LEGACY_FILE_FORMAT:
1549 case SQLITE_DBCONFIG_TRUSTED_SCHEMA:
1550 case SQLITE_DBCONFIG_STMT_SCANSTATUS:
1551 case SQLITE_DBCONFIG_REVERSE_SCANORDER:
1552 return sqlite3_db_config(pDb, op, arg1, pArg2);
1553 default: return SQLITE_MISUSE;
1558 ** This function is NOT part of the sqlite3 public API. It is strictly
1559 ** for use by the sqlite project's own JS/WASM bindings.
1561 ** Wrapper for the variants of sqlite3_db_config() which take
1562 ** (void*,int,int) variadic args.
1564 SQLITE_WASM_EXPORT
1565 int sqlite3_wasm_db_config_pii(sqlite3 *pDb, int op, void * pArg1, int arg2, int arg3){
1566 switch(op){
1567 case SQLITE_DBCONFIG_LOOKASIDE:
1568 return sqlite3_db_config(pDb, op, pArg1, arg2, arg3);
1569 default: return SQLITE_MISUSE;
1574 ** This function is NOT part of the sqlite3 public API. It is strictly
1575 ** for use by the sqlite project's own JS/WASM bindings.
1577 ** Wrapper for the variants of sqlite3_db_config() which take
1578 ** (const char *) variadic args.
1580 SQLITE_WASM_EXPORT
1581 int sqlite3_wasm_db_config_s(sqlite3 *pDb, int op, const char *zArg){
1582 switch(op){
1583 case SQLITE_DBCONFIG_MAINDBNAME:
1584 return sqlite3_db_config(pDb, op, zArg);
1585 default: return SQLITE_MISUSE;
1591 ** This function is NOT part of the sqlite3 public API. It is strictly
1592 ** for use by the sqlite project's own JS/WASM bindings.
1594 ** Binding for combinations of sqlite3_config() arguments which take
1595 ** a single integer argument.
1597 SQLITE_WASM_EXPORT
1598 int sqlite3_wasm_config_i(int op, int arg){
1599 return sqlite3_config(op, arg);
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 ** Binding for combinations of sqlite3_config() arguments which take
1607 ** two int arguments.
1609 SQLITE_WASM_EXPORT
1610 int sqlite3_wasm_config_ii(int op, int arg1, int arg2){
1611 return sqlite3_config(op, arg1, arg2);
1615 ** This function is NOT part of the sqlite3 public API. It is strictly
1616 ** for use by the sqlite project's own JS/WASM bindings.
1618 ** Binding for combinations of sqlite3_config() arguments which take
1619 ** a single i64 argument.
1621 SQLITE_WASM_EXPORT
1622 int sqlite3_wasm_config_j(int op, sqlite3_int64 arg){
1623 return sqlite3_config(op, arg);
1626 #if 0
1627 // Pending removal after verification of a workaround discussed in the
1628 // forum post linked to below.
1630 ** This function is NOT part of the sqlite3 public API. It is strictly
1631 ** for use by the sqlite project's own JS/WASM bindings.
1633 ** Returns a pointer to sqlite3_free(). In compliant browsers the
1634 ** return value, when passed to sqlite3.wasm.exports.functionEntry(),
1635 ** must resolve to the same function as
1636 ** sqlite3.wasm.exports.sqlite3_free. i.e. from a dev console where
1637 ** sqlite3 is exported globally, the following must be true:
1639 ** ```
1640 ** sqlite3.wasm.functionEntry(
1641 ** sqlite3.wasm.exports.sqlite3_wasm_ptr_to_sqlite3_free()
1642 ** ) === sqlite3.wasm.exports.sqlite3_free
1643 ** ```
1645 ** Using a function to return this pointer, as opposed to exporting it
1646 ** via sqlite3_wasm_enum_json(), is an attempt to work around a
1647 ** Safari-specific quirk covered at
1648 ** https://sqlite.org/forum/info/e5b20e1feb37a19a.
1650 SQLITE_WASM_EXPORT
1651 void * sqlite3_wasm_ptr_to_sqlite3_free(void){
1652 return (void*)sqlite3_free;
1654 #endif
1656 #if defined(__EMSCRIPTEN__) && defined(SQLITE_ENABLE_WASMFS)
1657 #include <emscripten/wasmfs.h>
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, specifically
1662 ** only when building with Emscripten's WASMFS support.
1664 ** This function should only be called if the JS side detects the
1665 ** existence of the Origin-Private FileSystem (OPFS) APIs in the
1666 ** client. The first time it is called, this function instantiates a
1667 ** WASMFS backend impl for OPFS. On success, subsequent calls are
1668 ** no-ops.
1670 ** This function may be passed a "mount point" name, which must have a
1671 ** leading "/" and is currently restricted to a single path component,
1672 ** e.g. "/foo" is legal but "/foo/" and "/foo/bar" are not. If it is
1673 ** NULL or empty, it defaults to "/opfs".
1675 ** Returns 0 on success, SQLITE_NOMEM if instantiation of the backend
1676 ** object fails, SQLITE_IOERR if mkdir() of the zMountPoint dir in
1677 ** the virtual FS fails. In builds compiled without SQLITE_ENABLE_WASMFS
1678 ** defined, SQLITE_NOTFOUND is returned without side effects.
1680 SQLITE_WASM_EXPORT
1681 int sqlite3_wasm_init_wasmfs(const char *zMountPoint){
1682 static backend_t pOpfs = 0;
1683 if( !zMountPoint || !*zMountPoint ) zMountPoint = "/opfs";
1684 if( !pOpfs ){
1685 pOpfs = wasmfs_create_opfs_backend();
1687 /** It's not enough to instantiate the backend. We have to create a
1688 mountpoint in the VFS and attach the backend to it. */
1689 if( pOpfs && 0!=access(zMountPoint, F_OK) ){
1690 /* Note that this check and is not robust but it will
1691 hypothetically suffice for the transient wasm-based virtual
1692 filesystem we're currently running in. */
1693 const int rc = wasmfs_create_directory(zMountPoint, 0777, pOpfs);
1694 /*emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc);*/
1695 if(rc) return SQLITE_IOERR;
1697 return pOpfs ? 0 : SQLITE_NOMEM;
1699 #else
1700 SQLITE_WASM_EXPORT
1701 int sqlite3_wasm_init_wasmfs(const char *zUnused){
1702 //emscripten_console_warn("WASMFS OPFS is not compiled in.");
1703 if(zUnused){/*unused*/}
1704 return SQLITE_NOTFOUND;
1706 #endif /* __EMSCRIPTEN__ && SQLITE_ENABLE_WASMFS */
1708 #if SQLITE_WASM_TESTS
1710 SQLITE_WASM_EXPORT
1711 int sqlite3_wasm_test_intptr(int * p){
1712 return *p = *p * 2;
1715 SQLITE_WASM_EXPORT
1716 void * sqlite3_wasm_test_voidptr(void * p){
1717 return p;
1720 SQLITE_WASM_EXPORT
1721 int64_t sqlite3_wasm_test_int64_max(void){
1722 return (int64_t)0x7fffffffffffffff;
1725 SQLITE_WASM_EXPORT
1726 int64_t sqlite3_wasm_test_int64_min(void){
1727 return ~sqlite3_wasm_test_int64_max();
1730 SQLITE_WASM_EXPORT
1731 int64_t sqlite3_wasm_test_int64_times2(int64_t x){
1732 return x * 2;
1735 SQLITE_WASM_EXPORT
1736 void sqlite3_wasm_test_int64_minmax(int64_t * min, int64_t *max){
1737 *max = sqlite3_wasm_test_int64_max();
1738 *min = sqlite3_wasm_test_int64_min();
1739 /*printf("minmax: min=%lld, max=%lld\n", *min, *max);*/
1742 SQLITE_WASM_EXPORT
1743 int64_t sqlite3_wasm_test_int64ptr(int64_t * p){
1744 /*printf("sqlite3_wasm_test_int64ptr( @%lld = 0x%llx )\n", (int64_t)p, *p);*/
1745 return *p = *p * 2;
1748 SQLITE_WASM_EXPORT
1749 void sqlite3_wasm_test_stack_overflow(int recurse){
1750 if(recurse) sqlite3_wasm_test_stack_overflow(recurse);
1753 /* For testing the 'string:dealloc' whwasmutil.xWrap() conversion. */
1754 SQLITE_WASM_EXPORT
1755 char * sqlite3_wasm_test_str_hello(int fail){
1756 char * s = fail ? 0 : (char *)sqlite3_malloc(6);
1757 if(s){
1758 memcpy(s, "hello", 5);
1759 s[5] = 0;
1761 return s;
1763 #endif /* SQLITE_WASM_TESTS */
1765 #undef SQLITE_WASM_EXPORT