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