xref: /sqlite-3.40.0/ext/wasm/api/sqlite3-wasm.c (revision 1fc6ffcc)
1 /*
2 ** This file requires access to sqlite3.c static state in order to
3 ** implement certain WASM-specific features, and thus directly
4 ** includes that file. Unlike the rest of sqlite3.c, this file
5 ** requires compiling with -std=c99 (or equivalent, or a later C
6 ** version) because it makes use of features not available in C89.
7 **
8 ** At its simplest, to build sqlite3.wasm either place this file
9 ** in the same directory as sqlite3.c/h before compilation or use the
10 ** -I/path flag to tell the compiler where to find both of those
11 ** files, then compile this file. For example:
12 **
13 ** emcc -o sqlite3.wasm ... -I/path/to/sqlite3-c-and-h sqlite3-wasm.c
14 */
15 
16 #define SQLITE_WASM
17 #ifdef SQLITE_WASM_ENABLE_C_TESTS
18 /*
19 ** Functions blocked off by SQLITE_WASM_TESTS are intended solely for
20 ** use in unit/regression testing. They may be safely omitted from
21 ** client-side builds.
22 */
23 #  define SQLITE_WASM_TESTS 1
24 #else
25 #  define SQLITE_WASM_TESTS 0
26 #endif
27 
28 /*
29 ** Threading and file locking: JS is single-threaded. Each Worker
30 ** thread is a separate instance of the JS engine so can never access
31 ** the same db handle as another thread, thus multi-threading support
32 ** is unnecessary in the library. Because the filesystems are virtual
33 ** and local to a given wasm runtime instance, two Workers can never
34 ** access the same db file at once, with the exception of OPFS. As of
35 ** this writing (2022-09-30), OPFS exclusively locks a file when
36 ** opening it, so two Workers can never open the same OPFS-backed file
37 ** at once. That situation will change if and when lower-level locking
38 ** features are added to OPFS (as is currently planned, per folks
39 ** involved with its development).
40 **
41 ** Summary: except for the case of future OPFS, which supports
42 ** locking, and any similar future filesystems, threading and file
43 ** locking support are unnecessary in the wasm build.
44 */
45 
46 /*
47 ** Undefine any SQLITE_... config flags which we specifically do not
48 ** want undefined. Please keep these alphabetized.
49 */
50 #undef SQLITE_OMIT_DESERIALIZE
51 #undef SQLITE_OMIT_MEMORYDB
52 
53 /*
54 ** Define any SQLITE_... config defaults we want if they aren't
55 ** overridden by the builder. Please keep these alphabetized.
56 */
57 
58 /**********************************************************************/
59 /* SQLITE_D... */
60 #ifndef SQLITE_DEFAULT_CACHE_SIZE
61 /*
62 ** The OPFS impls benefit tremendously from an increased cache size
63 ** when working on large workloads, e.g. speedtest1 --size 50 or
64 ** higher. On smaller workloads, e.g. speedtest1 --size 25, they
65 ** clearly benefit from having 4mb of cache, but not as much as a
66 ** larger cache benefits the larger workloads. Speed differences
67 ** between 2x and nearly 3x have been measured with ample page cache.
68 */
69 # define SQLITE_DEFAULT_CACHE_SIZE -16777216
70 #endif
71 #if 0 && !defined(SQLITE_DEFAULT_PAGE_SIZE)
72 /* TODO: experiment with this. */
73 # define SQLITE_DEFAULT_PAGE_SIZE 8192 /*4096*/
74 #endif
75 #ifndef SQLITE_DEFAULT_UNIX_VFS
76 # define SQLITE_DEFAULT_UNIX_VFS "unix-none"
77 #endif
78 #undef SQLITE_DQS
79 #define SQLITE_DQS 0
80 
81 /**********************************************************************/
82 /* SQLITE_ENABLE_... */
83 #ifndef SQLITE_ENABLE_BYTECODE_VTAB
84 #  define SQLITE_ENABLE_BYTECODE_VTAB 1
85 #endif
86 #ifndef SQLITE_ENABLE_DBPAGE_VTAB
87 #  define SQLITE_ENABLE_DBPAGE_VTAB 1
88 #endif
89 #ifndef SQLITE_ENABLE_DBSTAT_VTAB
90 #  define SQLITE_ENABLE_DBSTAT_VTAB 1
91 #endif
92 #ifndef SQLITE_ENABLE_EXPLAIN_COMMENTS
93 #  define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
94 #endif
95 #ifndef SQLITE_ENABLE_FTS4
96 #  define SQLITE_ENABLE_FTS4 1
97 #endif
98 #ifndef SQLITE_ENABLE_OFFSET_SQL_FUNC
99 #  define SQLITE_ENABLE_OFFSET_SQL_FUNC 1
100 #endif
101 #ifndef SQLITE_ENABLE_RTREE
102 #  define SQLITE_ENABLE_RTREE 1
103 #endif
104 #ifndef SQLITE_ENABLE_STMTVTAB
105 #  define SQLITE_ENABLE_STMTVTAB 1
106 #endif
107 #ifndef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
108 #  define SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
109 #endif
110 
111 /**********************************************************************/
112 /* SQLITE_O... */
113 #ifndef SQLITE_OMIT_DEPRECATED
114 # define SQLITE_OMIT_DEPRECATED 1
115 #endif
116 #ifndef SQLITE_OMIT_LOAD_EXTENSION
117 # define SQLITE_OMIT_LOAD_EXTENSION 1
118 #endif
119 #ifndef SQLITE_OMIT_SHARED_CACHE
120 # define SQLITE_OMIT_SHARED_CACHE 1
121 #endif
122 #ifndef SQLITE_OMIT_UTF16
123 # define SQLITE_OMIT_UTF16 1
124 #endif
125 #ifndef SQLITE_OMIT_WAL
126 # define SQLITE_OMIT_WAL 1
127 #endif
128 #ifndef SQLITE_OS_KV_OPTIONAL
129 # define SQLITE_OS_KV_OPTIONAL 1
130 #endif
131 
132 /**********************************************************************/
133 /* SQLITE_T... */
134 #ifndef SQLITE_TEMP_STORE
135 # define SQLITE_TEMP_STORE 3
136 #endif
137 #ifndef SQLITE_THREADSAFE
138 # define SQLITE_THREADSAFE 0
139 #endif
140 
141 /**********************************************************************/
142 /* SQLITE_USE_... */
143 #ifndef SQLITE_USE_URI
144 #  define SQLITE_USE_URI 1
145 #endif
146 
147 #include <assert.h>
148 #include "sqlite3.c" /* yes, .c instead of .h. */
149 
150 #if defined(__EMSCRIPTEN__)
151 #  include <emscripten/console.h>
152 #endif
153 
154 /*
155 ** SQLITE_WASM_KEEP is functionally identical to EMSCRIPTEN_KEEPALIVE
156 ** but is not Emscripten-specific. It explicitly marks functions for
157 ** export into the target wasm file without requiring explicit listing
158 ** of those functions in Emscripten's -sEXPORTED_FUNCTIONS=... list
159 ** (or equivalent in other build platforms). Any function with neither
160 ** this attribute nor which is listed as an explicit export will not
161 ** be exported from the wasm file (but may still be used internally
162 ** within the wasm file).
163 **
164 ** The functions in this file (sqlite3-wasm.c) which require exporting
165 ** are marked with this flag. They may also be added to any explicit
166 ** build-time export list but need not be. All of these APIs are
167 ** intended for use only within the project's own JS/WASM code, and
168 ** not by client code, so an argument can be made for reducing their
169 ** visibility by not including them in any build-time export lists.
170 **
171 ** 2022-09-11: it's not yet _proven_ that this approach works in
172 ** non-Emscripten builds. If not, such builds will need to export
173 ** those using the --export=... wasm-ld flag (or equivalent). As of
174 ** this writing we are tied to Emscripten for various reasons
175 ** and cannot test the library with other build environments.
176 */
177 #define SQLITE_WASM_KEEP __attribute__((used,visibility("default")))
178 // See also:
179 //__attribute__((export_name("theExportedName"), used, visibility("default")))
180 
181 
182 #if 0
183 /*
184 ** An EXPERIMENT in implementing a stack-based allocator analog to
185 ** Emscripten's stackSave(), stackAlloc(), stackRestore().
186 ** Unfortunately, this cannot work together with Emscripten because
187 ** Emscripten defines its own native one and we'd stomp on each
188 ** other's memory. Other than that complication, basic tests show it
189 ** to work just fine.
190 **
191 ** Another option is to malloc() a chunk of our own and call that our
192 ** "stack".
193 */
194 SQLITE_WASM_KEEP void * sqlite3_wasm_stack_end(void){
195   extern void __heap_base
196     /* see https://stackoverflow.com/questions/10038964 */;
197   return &__heap_base;
198 }
199 SQLITE_WASM_KEEP void * sqlite3_wasm_stack_begin(void){
200   extern void __data_end;
201   return &__data_end;
202 }
203 static void * pWasmStackPtr = 0;
204 SQLITE_WASM_KEEP void * sqlite3_wasm_stack_ptr(void){
205   if(!pWasmStackPtr) pWasmStackPtr = sqlite3_wasm_stack_end();
206   return pWasmStackPtr;
207 }
208 SQLITE_WASM_KEEP void sqlite3_wasm_stack_restore(void * p){
209   pWasmStackPtr = p;
210 }
211 SQLITE_WASM_KEEP void * sqlite3_wasm_stack_alloc(int n){
212   if(n<=0) return 0;
213   n = (n + 7) & ~7 /* align to 8-byte boundary */;
214   unsigned char * const p = (unsigned char *)sqlite3_wasm_stack_ptr();
215   unsigned const char * const b = (unsigned const char *)sqlite3_wasm_stack_begin();
216   if(b + n >= p || b + n < b/*overflow*/) return 0;
217   return pWasmStackPtr = p - n;
218 }
219 #endif /* stack allocator experiment */
220 
221 /*
222 ** State for the "pseudo-stack" allocator implemented in
223 ** sqlite3_wasm_pstack_xyz(). In order to avoid colliding with
224 ** Emscripten-controled stack space, it carves out a bit of stack
225 ** memory to use for that purpose. This memory ends up in the
226 ** WASM-managed memory, such that routines which manipulate the wasm
227 ** heap can also be used to manipulate this memory.
228 **
229 ** This particular allocator is intended for small allocations such as
230 ** storage for output pointers. We cannot reasonably size it large
231 ** enough for general-purpose string conversions because some of our
232 ** tests use input files (strings) of 16MB+.
233 */
234 static unsigned char PStack_mem[512 * 8] = {0};
235 static struct {
236   unsigned const char * const pBegin;/* Start (inclusive) of memory */
237   unsigned const char * const pEnd;  /* One-after-the-end of memory */
238   unsigned char * pPos;              /* Current stack pointer */
239 } PStack = {
240   &PStack_mem[0],
241   &PStack_mem[0] + sizeof(PStack_mem),
242   &PStack_mem[0] + sizeof(PStack_mem)
243 };
244 /*
245 ** Returns the current pstack position.
246 */
247 SQLITE_WASM_KEEP void * sqlite3_wasm_pstack_ptr(void){
248   return PStack.pPos;
249 }
250 /*
251 ** Sets the pstack position poitner to p. Results are undefined if the
252 ** given value did not come from sqlite3_wasm_pstack_ptr().
253 */
254 SQLITE_WASM_KEEP void sqlite3_wasm_pstack_restore(unsigned char * p){
255   assert(p>=PStack.pBegin && p<=PStack.pEnd && p>=PStack.pPos);
256   assert(0==(p & 0x7));
257   if(p>=PStack.pBegin && p<=PStack.pEnd /*&& p>=PStack.pPos*/){
258     PStack.pPos = p;
259   }
260 }
261 /*
262 ** Allocate and zero out n bytes from the pstack. Returns a pointer to
263 ** the memory on success, 0 on error (including a negative n value). n
264 ** is always adjusted to be a multiple of 8 and returned memory is
265 ** always zeroed out before returning (because this keeps the client
266 ** JS code from having to do so, and most uses of the pstack will
267 ** call for doing so).
268 */
269 SQLITE_WASM_KEEP void * sqlite3_wasm_pstack_alloc(int n){
270   if( n<=0 ) return 0;
271   //if( n & 0x7 ) n += 8 - (n & 0x7) /* align to 8-byte boundary */;
272   n = (n + 7) & ~7 /* align to 8-byte boundary */;
273   if( PStack.pBegin + n > PStack.pPos /*not enough space left*/
274       || PStack.pBegin + n <= PStack.pBegin /*overflow*/ ) return 0;
275   memset((PStack.pPos = PStack.pPos - n), 0, (unsigned int)n);
276   return PStack.pPos;
277 }
278 /*
279 ** Return the number of bytes left which can be
280 ** sqlite3_wasm_pstack_alloc()'d.
281 */
282 SQLITE_WASM_KEEP int sqlite3_wasm_pstack_remaining(void){
283   assert(PStack.pPos >= PStack.pBegin);
284   assert(PStack.pPos <= PStack.pEnd);
285   return (int)(PStack.pPos - PStack.pBegin);
286 }
287 
288 /*
289 ** Return the total number of bytes available in the pstack, including
290 ** any space which is currently allocated. This value is a
291 ** compile-time constant.
292 */
293 SQLITE_WASM_KEEP int sqlite3_wasm_pstack_quota(void){
294   return (int)(PStack.pEnd - PStack.pBegin);
295 }
296 
297 /*
298 ** This function is NOT part of the sqlite3 public API. It is strictly
299 ** for use by the sqlite project's own JS/WASM bindings.
300 **
301 ** For purposes of certain hand-crafted C/Wasm function bindings, we
302 ** need a way of reporting errors which is consistent with the rest of
303 ** the C API, as opposed to throwing JS exceptions. To that end, this
304 ** internal-use-only function is a thin proxy around
305 ** sqlite3ErrorWithMessage(). The intent is that it only be used from
306 ** Wasm bindings such as sqlite3_prepare_v2/v3(), and definitely not
307 ** from client code.
308 **
309 ** Returns err_code.
310 */
311 SQLITE_WASM_KEEP
312 int sqlite3_wasm_db_error(sqlite3*db, int err_code, const char *zMsg){
313   if( 0!=zMsg ){
314     const int nMsg = sqlite3Strlen30(zMsg);
315     sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg);
316   }else{
317     sqlite3ErrorWithMsg(db, err_code, NULL);
318   }
319   return err_code;
320 }
321 
322 #if SQLITE_WASM_TESTS
323 struct WasmTestStruct {
324   int v4;
325   void * ppV;
326   const char * cstr;
327   int64_t v8;
328   void (*xFunc)(void*);
329 };
330 typedef struct WasmTestStruct WasmTestStruct;
331 SQLITE_WASM_KEEP
332 void sqlite3_wasm_test_struct(WasmTestStruct * s){
333   if(s){
334     s->v4 *= 2;
335     s->v8 = s->v4 * 2;
336     s->ppV = s;
337     s->cstr = __FILE__;
338     if(s->xFunc) s->xFunc(s);
339   }
340   return;
341 }
342 #endif /* SQLITE_WASM_TESTS */
343 
344 /*
345 ** This function is NOT part of the sqlite3 public API. It is strictly
346 ** for use by the sqlite project's own JS/WASM bindings. Unlike the
347 ** rest of the sqlite3 API, this part requires C99 for snprintf() and
348 ** variadic macros.
349 **
350 ** Returns a string containing a JSON-format "enum" of C-level
351 ** constants and struct-related metadata intended to be imported into
352 ** the JS environment. The JSON is initialized the first time this
353 ** function is called and that result is reused for all future calls.
354 **
355 ** If this function returns NULL then it means that the internal
356 ** buffer is not large enough for the generated JSON and needs to be
357 ** increased. In debug builds that will trigger an assert().
358 */
359 SQLITE_WASM_KEEP
360 const char * sqlite3_wasm_enum_json(void){
361   static char aBuffer[1024 * 12] = {0} /* where the JSON goes */;
362   int n = 0, nChildren = 0, nStruct = 0
363     /* output counters for figuring out where commas go */;
364   char * zPos = &aBuffer[1] /* skip first byte for now to help protect
365                           ** against a small race condition */;
366   char const * const zEnd = &aBuffer[0] + sizeof(aBuffer) /* one-past-the-end */;
367   if(aBuffer[0]) return aBuffer;
368   /* Leave aBuffer[0] at 0 until the end to help guard against a tiny
369   ** race condition. If this is called twice concurrently, they might
370   ** end up both writing to aBuffer, but they'll both write the same
371   ** thing, so that's okay. If we set byte 0 up front then the 2nd
372   ** instance might return and use the string before the 1st instance
373   ** is done filling it. */
374 
375 /* Core output macros... */
376 #define lenCheck assert(zPos < zEnd - 128 \
377   && "sqlite3_wasm_enum_json() buffer is too small."); \
378   if( zPos >= zEnd - 128 ) return 0
379 #define outf(format,...) \
380   zPos += snprintf(zPos, ((size_t)(zEnd - zPos)), format, __VA_ARGS__); \
381   lenCheck
382 #define out(TXT) outf("%s",TXT)
383 #define CloseBrace(LEVEL) \
384   assert(LEVEL<5); memset(zPos, '}', LEVEL); zPos+=LEVEL; lenCheck
385 
386 /* Macros for emitting maps of integer- and string-type macros to
387 ** their values. */
388 #define DefGroup(KEY) n = 0; \
389   outf("%s\"" #KEY "\": {",(nChildren++ ? "," : ""));
390 #define DefInt(KEY)                                     \
391   outf("%s\"%s\": %d", (n++ ? ", " : ""), #KEY, (int)KEY)
392 #define DefStr(KEY)                                     \
393   outf("%s\"%s\": \"%s\"", (n++ ? ", " : ""), #KEY, KEY)
394 #define _DefGroup CloseBrace(1)
395 
396   /* The following groups are sorted alphabetic by group name. */
397   DefGroup(access){
398     DefInt(SQLITE_ACCESS_EXISTS);
399     DefInt(SQLITE_ACCESS_READWRITE);
400     DefInt(SQLITE_ACCESS_READ)/*docs say this is unused*/;
401   } _DefGroup;
402 
403   DefGroup(blobFinalizers) {
404     /* SQLITE_STATIC/TRANSIENT need to be handled explicitly as
405     ** integers to avoid casting-related warnings. */
406     out("\"SQLITE_STATIC\":0, \"SQLITE_TRANSIENT\":-1");
407   } _DefGroup;
408 
409   DefGroup(dataTypes) {
410     DefInt(SQLITE_INTEGER);
411     DefInt(SQLITE_FLOAT);
412     DefInt(SQLITE_TEXT);
413     DefInt(SQLITE_BLOB);
414     DefInt(SQLITE_NULL);
415   } _DefGroup;
416 
417   DefGroup(encodings) {
418     /* Noting that the wasm binding only aims to support UTF-8. */
419     DefInt(SQLITE_UTF8);
420     DefInt(SQLITE_UTF16LE);
421     DefInt(SQLITE_UTF16BE);
422     DefInt(SQLITE_UTF16);
423     /*deprecated DefInt(SQLITE_ANY); */
424     DefInt(SQLITE_UTF16_ALIGNED);
425   } _DefGroup;
426 
427   DefGroup(fcntl) {
428     DefInt(SQLITE_FCNTL_LOCKSTATE);
429     DefInt(SQLITE_FCNTL_GET_LOCKPROXYFILE);
430     DefInt(SQLITE_FCNTL_SET_LOCKPROXYFILE);
431     DefInt(SQLITE_FCNTL_LAST_ERRNO);
432     DefInt(SQLITE_FCNTL_SIZE_HINT);
433     DefInt(SQLITE_FCNTL_CHUNK_SIZE);
434     DefInt(SQLITE_FCNTL_FILE_POINTER);
435     DefInt(SQLITE_FCNTL_SYNC_OMITTED);
436     DefInt(SQLITE_FCNTL_WIN32_AV_RETRY);
437     DefInt(SQLITE_FCNTL_PERSIST_WAL);
438     DefInt(SQLITE_FCNTL_OVERWRITE);
439     DefInt(SQLITE_FCNTL_VFSNAME);
440     DefInt(SQLITE_FCNTL_POWERSAFE_OVERWRITE);
441     DefInt(SQLITE_FCNTL_PRAGMA);
442     DefInt(SQLITE_FCNTL_BUSYHANDLER);
443     DefInt(SQLITE_FCNTL_TEMPFILENAME);
444     DefInt(SQLITE_FCNTL_MMAP_SIZE);
445     DefInt(SQLITE_FCNTL_TRACE);
446     DefInt(SQLITE_FCNTL_HAS_MOVED);
447     DefInt(SQLITE_FCNTL_SYNC);
448     DefInt(SQLITE_FCNTL_COMMIT_PHASETWO);
449     DefInt(SQLITE_FCNTL_WIN32_SET_HANDLE);
450     DefInt(SQLITE_FCNTL_WAL_BLOCK);
451     DefInt(SQLITE_FCNTL_ZIPVFS);
452     DefInt(SQLITE_FCNTL_RBU);
453     DefInt(SQLITE_FCNTL_VFS_POINTER);
454     DefInt(SQLITE_FCNTL_JOURNAL_POINTER);
455     DefInt(SQLITE_FCNTL_WIN32_GET_HANDLE);
456     DefInt(SQLITE_FCNTL_PDB);
457     DefInt(SQLITE_FCNTL_BEGIN_ATOMIC_WRITE);
458     DefInt(SQLITE_FCNTL_COMMIT_ATOMIC_WRITE);
459     DefInt(SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE);
460     DefInt(SQLITE_FCNTL_LOCK_TIMEOUT);
461     DefInt(SQLITE_FCNTL_DATA_VERSION);
462     DefInt(SQLITE_FCNTL_SIZE_LIMIT);
463     DefInt(SQLITE_FCNTL_CKPT_DONE);
464     DefInt(SQLITE_FCNTL_RESERVE_BYTES);
465     DefInt(SQLITE_FCNTL_CKPT_START);
466     DefInt(SQLITE_FCNTL_EXTERNAL_READER);
467     DefInt(SQLITE_FCNTL_CKSM_FILE);
468   } _DefGroup;
469 
470   DefGroup(flock) {
471     DefInt(SQLITE_LOCK_NONE);
472     DefInt(SQLITE_LOCK_SHARED);
473     DefInt(SQLITE_LOCK_RESERVED);
474     DefInt(SQLITE_LOCK_PENDING);
475     DefInt(SQLITE_LOCK_EXCLUSIVE);
476   } _DefGroup;
477 
478   DefGroup(ioCap) {
479     DefInt(SQLITE_IOCAP_ATOMIC);
480     DefInt(SQLITE_IOCAP_ATOMIC512);
481     DefInt(SQLITE_IOCAP_ATOMIC1K);
482     DefInt(SQLITE_IOCAP_ATOMIC2K);
483     DefInt(SQLITE_IOCAP_ATOMIC4K);
484     DefInt(SQLITE_IOCAP_ATOMIC8K);
485     DefInt(SQLITE_IOCAP_ATOMIC16K);
486     DefInt(SQLITE_IOCAP_ATOMIC32K);
487     DefInt(SQLITE_IOCAP_ATOMIC64K);
488     DefInt(SQLITE_IOCAP_SAFE_APPEND);
489     DefInt(SQLITE_IOCAP_SEQUENTIAL);
490     DefInt(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN);
491     DefInt(SQLITE_IOCAP_POWERSAFE_OVERWRITE);
492     DefInt(SQLITE_IOCAP_IMMUTABLE);
493     DefInt(SQLITE_IOCAP_BATCH_ATOMIC);
494   } _DefGroup;
495 
496   DefGroup(openFlags) {
497     /* Noting that not all of these will have any effect in
498     ** WASM-space. */
499     DefInt(SQLITE_OPEN_READONLY);
500     DefInt(SQLITE_OPEN_READWRITE);
501     DefInt(SQLITE_OPEN_CREATE);
502     DefInt(SQLITE_OPEN_URI);
503     DefInt(SQLITE_OPEN_MEMORY);
504     DefInt(SQLITE_OPEN_NOMUTEX);
505     DefInt(SQLITE_OPEN_FULLMUTEX);
506     DefInt(SQLITE_OPEN_SHAREDCACHE);
507     DefInt(SQLITE_OPEN_PRIVATECACHE);
508     DefInt(SQLITE_OPEN_EXRESCODE);
509     DefInt(SQLITE_OPEN_NOFOLLOW);
510     /* OPEN flags for use with VFSes... */
511     DefInt(SQLITE_OPEN_MAIN_DB);
512     DefInt(SQLITE_OPEN_MAIN_JOURNAL);
513     DefInt(SQLITE_OPEN_TEMP_DB);
514     DefInt(SQLITE_OPEN_TEMP_JOURNAL);
515     DefInt(SQLITE_OPEN_TRANSIENT_DB);
516     DefInt(SQLITE_OPEN_SUBJOURNAL);
517     DefInt(SQLITE_OPEN_SUPER_JOURNAL);
518     DefInt(SQLITE_OPEN_WAL);
519     DefInt(SQLITE_OPEN_DELETEONCLOSE);
520     DefInt(SQLITE_OPEN_EXCLUSIVE);
521   } _DefGroup;
522 
523   DefGroup(prepareFlags) {
524     DefInt(SQLITE_PREPARE_PERSISTENT);
525     DefInt(SQLITE_PREPARE_NORMALIZE);
526     DefInt(SQLITE_PREPARE_NO_VTAB);
527   } _DefGroup;
528 
529   DefGroup(resultCodes) {
530     DefInt(SQLITE_OK);
531     DefInt(SQLITE_ERROR);
532     DefInt(SQLITE_INTERNAL);
533     DefInt(SQLITE_PERM);
534     DefInt(SQLITE_ABORT);
535     DefInt(SQLITE_BUSY);
536     DefInt(SQLITE_LOCKED);
537     DefInt(SQLITE_NOMEM);
538     DefInt(SQLITE_READONLY);
539     DefInt(SQLITE_INTERRUPT);
540     DefInt(SQLITE_IOERR);
541     DefInt(SQLITE_CORRUPT);
542     DefInt(SQLITE_NOTFOUND);
543     DefInt(SQLITE_FULL);
544     DefInt(SQLITE_CANTOPEN);
545     DefInt(SQLITE_PROTOCOL);
546     DefInt(SQLITE_EMPTY);
547     DefInt(SQLITE_SCHEMA);
548     DefInt(SQLITE_TOOBIG);
549     DefInt(SQLITE_CONSTRAINT);
550     DefInt(SQLITE_MISMATCH);
551     DefInt(SQLITE_MISUSE);
552     DefInt(SQLITE_NOLFS);
553     DefInt(SQLITE_AUTH);
554     DefInt(SQLITE_FORMAT);
555     DefInt(SQLITE_RANGE);
556     DefInt(SQLITE_NOTADB);
557     DefInt(SQLITE_NOTICE);
558     DefInt(SQLITE_WARNING);
559     DefInt(SQLITE_ROW);
560     DefInt(SQLITE_DONE);
561     // Extended Result Codes
562     DefInt(SQLITE_ERROR_MISSING_COLLSEQ);
563     DefInt(SQLITE_ERROR_RETRY);
564     DefInt(SQLITE_ERROR_SNAPSHOT);
565     DefInt(SQLITE_IOERR_READ);
566     DefInt(SQLITE_IOERR_SHORT_READ);
567     DefInt(SQLITE_IOERR_WRITE);
568     DefInt(SQLITE_IOERR_FSYNC);
569     DefInt(SQLITE_IOERR_DIR_FSYNC);
570     DefInt(SQLITE_IOERR_TRUNCATE);
571     DefInt(SQLITE_IOERR_FSTAT);
572     DefInt(SQLITE_IOERR_UNLOCK);
573     DefInt(SQLITE_IOERR_RDLOCK);
574     DefInt(SQLITE_IOERR_DELETE);
575     DefInt(SQLITE_IOERR_BLOCKED);
576     DefInt(SQLITE_IOERR_NOMEM);
577     DefInt(SQLITE_IOERR_ACCESS);
578     DefInt(SQLITE_IOERR_CHECKRESERVEDLOCK);
579     DefInt(SQLITE_IOERR_LOCK);
580     DefInt(SQLITE_IOERR_CLOSE);
581     DefInt(SQLITE_IOERR_DIR_CLOSE);
582     DefInt(SQLITE_IOERR_SHMOPEN);
583     DefInt(SQLITE_IOERR_SHMSIZE);
584     DefInt(SQLITE_IOERR_SHMLOCK);
585     DefInt(SQLITE_IOERR_SHMMAP);
586     DefInt(SQLITE_IOERR_SEEK);
587     DefInt(SQLITE_IOERR_DELETE_NOENT);
588     DefInt(SQLITE_IOERR_MMAP);
589     DefInt(SQLITE_IOERR_GETTEMPPATH);
590     DefInt(SQLITE_IOERR_CONVPATH);
591     DefInt(SQLITE_IOERR_VNODE);
592     DefInt(SQLITE_IOERR_AUTH);
593     DefInt(SQLITE_IOERR_BEGIN_ATOMIC);
594     DefInt(SQLITE_IOERR_COMMIT_ATOMIC);
595     DefInt(SQLITE_IOERR_ROLLBACK_ATOMIC);
596     DefInt(SQLITE_IOERR_DATA);
597     DefInt(SQLITE_IOERR_CORRUPTFS);
598     DefInt(SQLITE_LOCKED_SHAREDCACHE);
599     DefInt(SQLITE_LOCKED_VTAB);
600     DefInt(SQLITE_BUSY_RECOVERY);
601     DefInt(SQLITE_BUSY_SNAPSHOT);
602     DefInt(SQLITE_BUSY_TIMEOUT);
603     DefInt(SQLITE_CANTOPEN_NOTEMPDIR);
604     DefInt(SQLITE_CANTOPEN_ISDIR);
605     DefInt(SQLITE_CANTOPEN_FULLPATH);
606     DefInt(SQLITE_CANTOPEN_CONVPATH);
607     //DefInt(SQLITE_CANTOPEN_DIRTYWAL)/*docs say not used*/;
608     DefInt(SQLITE_CANTOPEN_SYMLINK);
609     DefInt(SQLITE_CORRUPT_VTAB);
610     DefInt(SQLITE_CORRUPT_SEQUENCE);
611     DefInt(SQLITE_CORRUPT_INDEX);
612     DefInt(SQLITE_READONLY_RECOVERY);
613     DefInt(SQLITE_READONLY_CANTLOCK);
614     DefInt(SQLITE_READONLY_ROLLBACK);
615     DefInt(SQLITE_READONLY_DBMOVED);
616     DefInt(SQLITE_READONLY_CANTINIT);
617     DefInt(SQLITE_READONLY_DIRECTORY);
618     DefInt(SQLITE_ABORT_ROLLBACK);
619     DefInt(SQLITE_CONSTRAINT_CHECK);
620     DefInt(SQLITE_CONSTRAINT_COMMITHOOK);
621     DefInt(SQLITE_CONSTRAINT_FOREIGNKEY);
622     DefInt(SQLITE_CONSTRAINT_FUNCTION);
623     DefInt(SQLITE_CONSTRAINT_NOTNULL);
624     DefInt(SQLITE_CONSTRAINT_PRIMARYKEY);
625     DefInt(SQLITE_CONSTRAINT_TRIGGER);
626     DefInt(SQLITE_CONSTRAINT_UNIQUE);
627     DefInt(SQLITE_CONSTRAINT_VTAB);
628     DefInt(SQLITE_CONSTRAINT_ROWID);
629     DefInt(SQLITE_CONSTRAINT_PINNED);
630     DefInt(SQLITE_CONSTRAINT_DATATYPE);
631     DefInt(SQLITE_NOTICE_RECOVER_WAL);
632     DefInt(SQLITE_NOTICE_RECOVER_ROLLBACK);
633     DefInt(SQLITE_WARNING_AUTOINDEX);
634     DefInt(SQLITE_AUTH_USER);
635     DefInt(SQLITE_OK_LOAD_PERMANENTLY);
636     //DefInt(SQLITE_OK_SYMLINK) /* internal use only */;
637   } _DefGroup;
638 
639   DefGroup(serialize){
640     DefInt(SQLITE_SERIALIZE_NOCOPY);
641     DefInt(SQLITE_DESERIALIZE_FREEONCLOSE);
642     DefInt(SQLITE_DESERIALIZE_READONLY);
643     DefInt(SQLITE_DESERIALIZE_RESIZEABLE);
644   } _DefGroup;
645 
646   DefGroup(syncFlags) {
647     DefInt(SQLITE_SYNC_NORMAL);
648     DefInt(SQLITE_SYNC_FULL);
649     DefInt(SQLITE_SYNC_DATAONLY);
650   } _DefGroup;
651 
652   DefGroup(trace) {
653     DefInt(SQLITE_TRACE_STMT);
654     DefInt(SQLITE_TRACE_PROFILE);
655     DefInt(SQLITE_TRACE_ROW);
656     DefInt(SQLITE_TRACE_CLOSE);
657   } _DefGroup;
658 
659   DefGroup(udfFlags) {
660     DefInt(SQLITE_DETERMINISTIC);
661     DefInt(SQLITE_DIRECTONLY);
662     DefInt(SQLITE_INNOCUOUS);
663   } _DefGroup;
664 
665   DefGroup(version) {
666     DefInt(SQLITE_VERSION_NUMBER);
667     DefStr(SQLITE_VERSION);
668     DefStr(SQLITE_SOURCE_ID);
669   } _DefGroup;
670 
671 #undef DefGroup
672 #undef DefStr
673 #undef DefInt
674 #undef _DefGroup
675 
676   /*
677   ** Emit an array of "StructBinder" struct descripions, which look
678   ** like:
679   **
680   ** {
681   **   "name": "MyStruct",
682   **   "sizeof": 16,
683   **   "members": {
684   **     "member1": {"offset": 0,"sizeof": 4,"signature": "i"},
685   **     "member2": {"offset": 4,"sizeof": 4,"signature": "p"},
686   **     "member3": {"offset": 8,"sizeof": 8,"signature": "j"}
687   **   }
688   ** }
689   **
690   ** Detailed documentation for those bits are in the docs for the
691   ** Jaccwabyt JS-side component.
692   */
693 
694   /** Macros for emitting StructBinder description. */
695 #define StructBinder__(TYPE)                 \
696   n = 0;                                     \
697   outf("%s{", (nStruct++ ? ", " : ""));  \
698   out("\"name\": \"" # TYPE "\",");         \
699   outf("\"sizeof\": %d", (int)sizeof(TYPE)); \
700   out(",\"members\": {");
701 #define StructBinder_(T) StructBinder__(T)
702   /** ^^^ indirection needed to expand CurrentStruct */
703 #define StructBinder StructBinder_(CurrentStruct)
704 #define _StructBinder CloseBrace(2)
705 #define M(MEMBER,SIG)                                         \
706   outf("%s\"%s\": "                                           \
707        "{\"offset\":%d,\"sizeof\": %d,\"signature\":\"%s\"}", \
708        (n++ ? ", " : ""), #MEMBER,                            \
709        (int)offsetof(CurrentStruct,MEMBER),                   \
710        (int)sizeof(((CurrentStruct*)0)->MEMBER),              \
711        SIG)
712 
713   nStruct = 0;
714   out(", \"structs\": ["); {
715 
716 #define CurrentStruct sqlite3_vfs
717     StructBinder {
718       M(iVersion,"i");
719       M(szOsFile,"i");
720       M(mxPathname,"i");
721       M(pNext,"p");
722       M(zName,"s");
723       M(pAppData,"p");
724       M(xOpen,"i(pppip)");
725       M(xDelete,"i(ppi)");
726       M(xAccess,"i(ppip)");
727       M(xFullPathname,"i(ppip)");
728       M(xDlOpen,"p(pp)");
729       M(xDlError,"p(pip)");
730       M(xDlSym,"p()");
731       M(xDlClose,"v(pp)");
732       M(xRandomness,"i(pip)");
733       M(xSleep,"i(pi)");
734       M(xCurrentTime,"i(pp)");
735       M(xGetLastError,"i(pip)");
736       M(xCurrentTimeInt64,"i(pp)");
737       M(xSetSystemCall,"i(ppp)");
738       M(xGetSystemCall,"p(pp)");
739       M(xNextSystemCall,"p(pp)");
740     } _StructBinder;
741 #undef CurrentStruct
742 
743 #define CurrentStruct sqlite3_io_methods
744     StructBinder {
745       M(iVersion,"i");
746       M(xClose,"i(p)");
747       M(xRead,"i(ppij)");
748       M(xWrite,"i(ppij)");
749       M(xTruncate,"i(pj)");
750       M(xSync,"i(pi)");
751       M(xFileSize,"i(pp)");
752       M(xLock,"i(pi)");
753       M(xUnlock,"i(pi)");
754       M(xCheckReservedLock,"i(pp)");
755       M(xFileControl,"i(pip)");
756       M(xSectorSize,"i(p)");
757       M(xDeviceCharacteristics,"i(p)");
758       M(xShmMap,"i(piiip)");
759       M(xShmLock,"i(piii)");
760       M(xShmBarrier,"v(p)");
761       M(xShmUnmap,"i(pi)");
762       M(xFetch,"i(pjip)");
763       M(xUnfetch,"i(pjp)");
764     } _StructBinder;
765 #undef CurrentStruct
766 
767 #define CurrentStruct sqlite3_file
768     StructBinder {
769       M(pMethods,"p");
770     } _StructBinder;
771 #undef CurrentStruct
772 
773 #define CurrentStruct sqlite3_kvvfs_methods
774     StructBinder {
775       M(xRead,"i(sspi)");
776       M(xWrite,"i(sss)");
777       M(xDelete,"i(ss)");
778       M(nKeySize,"i");
779     } _StructBinder;
780 #undef CurrentStruct
781 
782 #if SQLITE_WASM_TESTS
783 #define CurrentStruct WasmTestStruct
784     StructBinder {
785       M(v4,"i");
786       M(cstr,"s");
787       M(ppV,"p");
788       M(v8,"j");
789       M(xFunc,"v(p)");
790     } _StructBinder;
791 #undef CurrentStruct
792 #endif
793 
794   } out( "]"/*structs*/);
795 
796   out("}"/*top-level object*/);
797   *zPos = 0;
798   aBuffer[0] = '{'/*end of the race-condition workaround*/;
799   return aBuffer;
800 #undef StructBinder
801 #undef StructBinder_
802 #undef StructBinder__
803 #undef M
804 #undef _StructBinder
805 #undef CloseBrace
806 #undef out
807 #undef outf
808 #undef lenCheck
809 }
810 
811 /*
812 ** This function is NOT part of the sqlite3 public API. It is strictly
813 ** for use by the sqlite project's own JS/WASM bindings.
814 **
815 ** This function invokes the xDelete method of the given VFS (or the
816 ** default VFS if pVfs is NULL), passing on the given filename. If
817 ** zName is NULL, no default VFS is found, or it has no xDelete
818 ** method, SQLITE_MISUSE is returned, else the result of the xDelete()
819 ** call is returned.
820 */
821 SQLITE_WASM_KEEP
822 int sqlite3_wasm_vfs_unlink(sqlite3_vfs *pVfs, const char * zName){
823   int rc = SQLITE_MISUSE /* ??? */;
824   if( 0==pVfs && 0!=zName ) pVfs = sqlite3_vfs_find(0);
825   if( zName && pVfs && pVfs->xDelete ){
826     rc = pVfs->xDelete(pVfs, zName, 1);
827   }
828   return rc;
829 }
830 
831 /*
832 ** This function is NOT part of the sqlite3 public API. It is strictly
833 ** for use by the sqlite project's own JS/WASM bindings.
834 **
835 ** Returns a pointer to the given DB's VFS for the given DB name,
836 ** defaulting to "main" if zDbName is 0. Returns 0 if no db with the
837 ** given name is open.
838 */
839 SQLITE_WASM_KEEP
840 sqlite3_vfs * sqlite3_wasm_db_vfs(sqlite3 *pDb, const char *zDbName){
841   sqlite3_vfs * pVfs = 0;
842   sqlite3_file_control(pDb, zDbName ? zDbName : "main",
843                        SQLITE_FCNTL_VFS_POINTER, &pVfs);
844   return pVfs;
845 }
846 
847 /*
848 ** This function is NOT part of the sqlite3 public API. It is strictly
849 ** for use by the sqlite project's own JS/WASM bindings.
850 **
851 ** This function resets the given db pointer's database as described at
852 **
853 ** https://www.sqlite.org/c3ref/c_dbconfig_defensive.html#sqlitedbconfigresetdatabase
854 **
855 ** Returns 0 on success, an SQLITE_xxx code on error. Returns
856 ** SQLITE_MISUSE if pDb is NULL.
857 */
858 SQLITE_WASM_KEEP
859 int sqlite3_wasm_db_reset(sqlite3*pDb){
860   int rc = SQLITE_MISUSE;
861   if( pDb ){
862     rc = sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
863     if( 0==rc ) rc = sqlite3_exec(pDb, "VACUUM", 0, 0, 0);
864     sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
865   }
866   return rc;
867 }
868 
869 /*
870 ** Uses the given database's VFS xRead to stream the db file's
871 ** contents out to the given callback. The callback gets a single
872 ** chunk of size n (its 2nd argument) on each call and must return 0
873 ** on success, non-0 on error. This function returns 0 on success,
874 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
875 ** code from the callback. Note that this is not thread-friendly: it
876 ** expects that it will be the only thread reading the db file and
877 ** takes no measures to ensure that is the case.
878 **
879 ** This implementation appears to work fine, but
880 ** sqlite3_wasm_db_serialize() is arguably the better way to achieve
881 ** this.
882 */
883 SQLITE_WASM_KEEP
884 int sqlite3_wasm_db_export_chunked( sqlite3* pDb,
885                                     int (*xCallback)(unsigned const char *zOut, int n) ){
886   sqlite3_int64 nSize = 0;
887   sqlite3_int64 nPos = 0;
888   sqlite3_file * pFile = 0;
889   unsigned char buf[1024 * 8];
890   int nBuf = (int)sizeof(buf);
891   int rc = pDb
892     ? sqlite3_file_control(pDb, "main",
893                            SQLITE_FCNTL_FILE_POINTER, &pFile)
894     : SQLITE_NOTFOUND;
895   if( rc ) return rc;
896   rc = pFile->pMethods->xFileSize(pFile, &nSize);
897   if( rc ) return rc;
898   if(nSize % nBuf){
899     /* DB size is not an even multiple of the buffer size. Reduce
900     ** buffer size so that we do not unduly inflate the db size
901     ** with zero-padding when exporting. */
902     if(0 == nSize % 4096) nBuf = 4096;
903     else if(0 == nSize % 2048) nBuf = 2048;
904     else if(0 == nSize % 1024) nBuf = 1024;
905     else nBuf = 512;
906   }
907   for( ; 0==rc && nPos<nSize; nPos += nBuf ){
908     rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
909     if(SQLITE_IOERR_SHORT_READ == rc){
910       rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
911     }
912     if( 0==rc ) rc = xCallback(buf, nBuf);
913   }
914   return rc;
915 }
916 
917 /*
918 ** A proxy for sqlite3_serialize() which serializes the "main" schema
919 ** of pDb, placing the serialized output in pOut and nOut. nOut may be
920 ** NULL. If pDb or pOut are NULL then SQLITE_MISUSE is returned. If
921 ** allocation of the serialized copy fails, SQLITE_NOMEM is returned.
922 ** On success, 0 is returned and `*pOut` will contain a pointer to the
923 ** memory unless mFlags includes SQLITE_SERIALIZE_NOCOPY and the
924 ** database has no contiguous memory representation, in which case
925 ** `*pOut` will be NULL but 0 will be returned.
926 **
927 ** If `*pOut` is not NULL, the caller is responsible for passing it to
928 ** sqlite3_free() to free it.
929 */
930 SQLITE_WASM_KEEP
931 int sqlite3_wasm_db_serialize( sqlite3* pDb, unsigned char **pOut,
932                                sqlite3_int64 * nOut, unsigned int mFlags ){
933   unsigned char * z;
934   if( !pDb || !pOut ) return SQLITE_MISUSE;
935   if(nOut) *nOut = 0;
936   z = sqlite3_serialize(pDb, "main", nOut, mFlags);
937   if( z || (SQLITE_SERIALIZE_NOCOPY & mFlags) ){
938     *pOut = z;
939     return 0;
940   }else{
941     return SQLITE_NOMEM;
942   }
943 }
944 
945 /*
946 ** This function is NOT part of the sqlite3 public API. It is strictly
947 ** for use by the sqlite project's own JS/WASM bindings.
948 **
949 ** Allocates sqlite3KvvfsMethods.nKeySize bytes from
950 ** sqlite3_wasm_pstack_alloc() and returns 0 if that allocation fails,
951 ** else it passes that string to kvstorageMakeKey() and returns a
952 ** NUL-terminated pointer to that string. It is up to the caller to
953 ** use sqlite3_wasm_pstack_restore() to free the returned pointer.
954 */
955 SQLITE_WASM_KEEP
956 char * sqlite3_wasm_kvvfsMakeKeyOnPstack(const char *zClass,
957                                          const char *zKeyIn){
958   assert(sqlite3KvvfsMethods.nKeySize>24);
959   char *zKeyOut =
960     (char *)sqlite3_wasm_pstack_alloc(sqlite3KvvfsMethods.nKeySize);
961   if(zKeyOut){
962     kvstorageMakeKey(zClass, zKeyIn, zKeyOut);
963   }
964   return zKeyOut;
965 }
966 
967 /*
968 ** This function is NOT part of the sqlite3 public API. It is strictly
969 ** for use by the sqlite project's own JS/WASM bindings.
970 **
971 ** Returns the pointer to the singleton object which holds the kvvfs
972 ** I/O methods and associated state.
973 */
974 SQLITE_WASM_KEEP
975 sqlite3_kvvfs_methods * sqlite3_wasm_kvvfs_methods(void){
976   return &sqlite3KvvfsMethods;
977 }
978 
979 #if defined(__EMSCRIPTEN__) && defined(SQLITE_WASM_WASMFS)
980 #include <emscripten/wasmfs.h>
981 
982 /*
983 ** This function is NOT part of the sqlite3 public API. It is strictly
984 ** for use by the sqlite project's own JS/WASM bindings, specifically
985 ** only when building with Emscripten's WASMFS support.
986 **
987 ** This function should only be called if the JS side detects the
988 ** existence of the Origin-Private FileSystem (OPFS) APIs in the
989 ** client. The first time it is called, this function instantiates a
990 ** WASMFS backend impl for OPFS. On success, subsequent calls are
991 ** no-ops.
992 **
993 ** This function may be passed a "mount point" name, which must have a
994 ** leading "/" and is currently restricted to a single path component,
995 ** e.g. "/foo" is legal but "/foo/" and "/foo/bar" are not. If it is
996 ** NULL or empty, it defaults to "/opfs".
997 **
998 ** Returns 0 on success, SQLITE_NOMEM if instantiation of the backend
999 ** object fails, SQLITE_IOERR if mkdir() of the zMountPoint dir in
1000 ** the virtual FS fails. In builds compiled without SQLITE_WASM_WASMFS
1001 ** defined, SQLITE_NOTFOUND is returned without side effects.
1002 */
1003 SQLITE_WASM_KEEP
1004 int sqlite3_wasm_init_wasmfs(const char *zMountPoint){
1005   static backend_t pOpfs = 0;
1006   if( !zMountPoint || !*zMountPoint ) zMountPoint = "/opfs";
1007   if( !pOpfs ){
1008     pOpfs = wasmfs_create_opfs_backend();
1009     if( pOpfs ){
1010       emscripten_console_log("Created WASMFS OPFS backend.");
1011     }
1012   }
1013   /** It's not enough to instantiate the backend. We have to create a
1014       mountpoint in the VFS and attach the backend to it. */
1015   if( pOpfs && 0!=access(zMountPoint, F_OK) ){
1016     /* mkdir() simply hangs when called from fiddle app. Cause is
1017        not yet determined but the hypothesis is an init-order
1018        issue. */
1019     /* Note that this check and is not robust but it will
1020        hypothetically suffice for the transient wasm-based virtual
1021        filesystem we're currently running in. */
1022     const int rc = wasmfs_create_directory(zMountPoint, 0777, pOpfs);
1023     /*emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc);*/
1024     if(rc) return SQLITE_IOERR;
1025   }
1026   return pOpfs ? 0 : SQLITE_NOMEM;
1027 }
1028 #else
1029 SQLITE_WASM_KEEP
1030 int sqlite3_wasm_init_wasmfs(const char *zUnused){
1031   //emscripten_console_warn("WASMFS OPFS is not compiled in.");
1032   if(zUnused){/*unused*/}
1033   return SQLITE_NOTFOUND;
1034 }
1035 #endif /* __EMSCRIPTEN__ && SQLITE_WASM_WASMFS */
1036 
1037 #if SQLITE_WASM_TESTS
1038 
1039 SQLITE_WASM_KEEP
1040 int sqlite3_wasm_test_intptr(int * p){
1041   return *p = *p * 2;
1042 }
1043 
1044 SQLITE_WASM_KEEP
1045 int64_t sqlite3_wasm_test_int64_max(void){
1046   return (int64_t)0x7fffffffffffffff;
1047 }
1048 
1049 SQLITE_WASM_KEEP
1050 int64_t sqlite3_wasm_test_int64_min(void){
1051   return ~sqlite3_wasm_test_int64_max();
1052 }
1053 
1054 SQLITE_WASM_KEEP
1055 int64_t sqlite3_wasm_test_int64_times2(int64_t x){
1056   return x * 2;
1057 }
1058 
1059 SQLITE_WASM_KEEP
1060 void sqlite3_wasm_test_int64_minmax(int64_t * min, int64_t *max){
1061   *max = sqlite3_wasm_test_int64_max();
1062   *min = sqlite3_wasm_test_int64_min();
1063   /*printf("minmax: min=%lld, max=%lld\n", *min, *max);*/
1064 }
1065 
1066 SQLITE_WASM_KEEP
1067 int64_t sqlite3_wasm_test_int64ptr(int64_t * p){
1068   /*printf("sqlite3_wasm_test_int64ptr( @%lld = 0x%llx )\n", (int64_t)p, *p);*/
1069   return *p = *p * 2;
1070 }
1071 
1072 SQLITE_WASM_KEEP
1073 void sqlite3_wasm_test_stack_overflow(int recurse){
1074   if(recurse) sqlite3_wasm_test_stack_overflow(recurse);
1075 }
1076 
1077 /* For testing the 'string-free' whwasmutil.xWrap() conversion. */
1078 SQLITE_WASM_KEEP
1079 char * sqlite3_wasm_test_str_hello(int fail){
1080   char * s = fail ? 0 : (char *)malloc(6);
1081   if(s){
1082     memcpy(s, "hello", 5);
1083     s[5] = 0;
1084   }
1085   return s;
1086 }
1087 #endif /* SQLITE_WASM_TESTS */
1088 
1089 #undef SQLITE_WASM_KEEP
1090