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