sqlite3-wasm.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  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. # undef SQLITE_WASM_ENABLE_C_TESTS
  18. # define SQLITE_WASM_ENABLE_C_TESTS 1
  19. /*
  20. ** Code blocked off by SQLITE_WASM_ENABLE_C_TESTS is intended solely
  21. ** for use in unit/regression testing. They may be safely omitted from
  22. ** client-side builds. The main unit test script, tester1.js, will
  23. ** skip related tests if it doesn't find the corresponding functions
  24. ** in the WASM exports.
  25. */
  26. #else
  27. # define SQLITE_WASM_ENABLE_C_TESTS 0
  28. #endif
  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.
  36. **
  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.
  40. */
  41. /*
  42. ** Undefine any SQLITE_... config flags which we specifically do not
  43. ** want defined. Please keep these alphabetized.
  44. */
  45. #undef SQLITE_OMIT_DESERIALIZE
  46. #undef SQLITE_OMIT_MEMORYDB
  47. /*
  48. ** Define any SQLITE_... config defaults we want if they aren't
  49. ** overridden by the builder. Please keep these alphabetized.
  50. */
  51. /**********************************************************************/
  52. /* SQLITE_D... */
  53. #ifndef SQLITE_DEFAULT_CACHE_SIZE
  54. /*
  55. ** The OPFS impls benefit tremendously from an increased cache size
  56. ** when working on large workloads, e.g. speedtest1 --size 50 or
  57. ** higher. On smaller workloads, e.g. speedtest1 --size 25, they
  58. ** clearly benefit from having 4mb of cache, but not as much as a
  59. ** larger cache benefits the larger workloads. Speed differences
  60. ** between 2x and nearly 3x have been measured with ample page cache.
  61. */
  62. # define SQLITE_DEFAULT_CACHE_SIZE -16384
  63. #endif
  64. #if !defined(SQLITE_DEFAULT_PAGE_SIZE)
  65. /*
  66. ** OPFS performance is improved by approx. 12% with a page size of 8kb
  67. ** instead of 4kb. Performance with 16kb is equivalent to 8kb.
  68. **
  69. ** Performance difference of kvvfs with a page size of 8kb compared to
  70. ** 4kb, as measured by speedtest1 --size 4, is indeterminate:
  71. ** measurements are all over the place either way and not
  72. ** significantly different.
  73. */
  74. # define SQLITE_DEFAULT_PAGE_SIZE 8192
  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. /* SQLITE_ENABLE_... */
  83. /*
  84. ** Unconditionally enable API_ARMOR in the WASM build. It ensures that
  85. ** public APIs behave predictable in the face of passing illegal NULLs
  86. ** or ranges which might otherwise invoke undefined behavior.
  87. */
  88. #undef SQLITE_ENABLE_API_ARMOR
  89. #define SQLITE_ENABLE_API_ARMOR 1
  90. /**********************************************************************/
  91. /* SQLITE_O... */
  92. #undef SQLITE_OMIT_DEPRECATED
  93. #define SQLITE_OMIT_DEPRECATED 1
  94. #undef SQLITE_OMIT_LOAD_EXTENSION
  95. #define SQLITE_OMIT_LOAD_EXTENSION 1
  96. #undef SQLITE_OMIT_SHARED_CACHE
  97. #define SQLITE_OMIT_SHARED_CACHE 1
  98. #undef SQLITE_OMIT_UTF16
  99. #define SQLITE_OMIT_UTF16 1
  100. #undef SQLITE_OS_KV_OPTIONAL
  101. #define SQLITE_OS_KV_OPTIONAL 1
  102. /**********************************************************************/
  103. /* SQLITE_S... */
  104. #ifndef SQLITE_STRICT_SUBTYPE
  105. # define SQLITE_STRICT_SUBTYPE 1
  106. #endif
  107. /**********************************************************************/
  108. /* SQLITE_T... */
  109. #ifndef SQLITE_TEMP_STORE
  110. # define SQLITE_TEMP_STORE 2
  111. #endif
  112. #ifndef SQLITE_THREADSAFE
  113. # define SQLITE_THREADSAFE 0
  114. #endif
  115. /**********************************************************************/
  116. /* SQLITE_USE_... */
  117. #ifndef SQLITE_USE_URI
  118. # define SQLITE_USE_URI 1
  119. #endif
  120. #ifdef SQLITE_WASM_EXTRA_INIT
  121. # define SQLITE_EXTRA_INIT sqlite3_wasm_extra_init
  122. #endif
  123. /*
  124. ** If SQLITE_WASM_BARE_BONES is defined, undefine most of the ENABLE
  125. ** macros.
  126. */
  127. #ifdef SQLITE_WASM_BARE_BONES
  128. # undef SQLITE_ENABLE_DBPAGE_VTAB
  129. # undef SQLITE_ENABLE_DBSTAT_VTAB
  130. # undef SQLITE_ENABLE_EXPLAIN_COMMENTS
  131. # undef SQLITE_ENABLE_FTS5
  132. # undef SQLITE_ENABLE_OFFSET_SQL_FUNC
  133. # undef SQLITE_ENABLE_PREUPDATE_HOOK
  134. # undef SQLITE_ENABLE_RTREE
  135. # undef SQLITE_ENABLE_SESSION
  136. # undef SQLITE_ENABLE_STMTVTAB
  137. # undef SQLITE_OMIT_AUTHORIZATION
  138. # define SQLITE_OMIT_AUTHORIZATION
  139. # undef SQLITE_OMIT_GET_TABLE
  140. # define SQLITE_OMIT_GET_TABLE
  141. # undef SQLITE_OMIT_INCRBLOB
  142. # define SQLITE_OMIT_INCRBLOB
  143. # undef SQLITE_OMIT_INTROSPECTION_PRAGMAS
  144. # define SQLITE_OMIT_INTROSPECTION_PRAGMAS
  145. # undef SQLITE_OMIT_JSON
  146. # define SQLITE_OMIT_JSON
  147. # undef SQLITE_OMIT_PROGRESS_CALLBACK
  148. # define SQLITE_OMIT_PROGRESS_CALLBACK
  149. # undef SQLITE_OMIT_WAL
  150. # define SQLITE_OMIT_WAL
  151. /*
  152. The following OMITs do not work with the standard amalgamation, so
  153. require a custom build:
  154. fossil clean -x
  155. ./configure
  156. OPTS='...' make -e sqlite3
  157. where ... has a -D... for each of the following OMIT flags:
  158. # undef SQLITE_OMIT_EXPLAIN
  159. # define SQLITE_OMIT_EXPLAIN
  160. # undef SQLITE_OMIT_TRIGGER
  161. # define SQLITE_OMIT_TRIGGER
  162. # undef SQLITE_OMIT_VIRTUALTABLE
  163. # define SQLITE_OMIT_VIRTUALTABLE
  164. # undef SQLITE_OMIT_WINDOWFUNC
  165. # define SQLITE_OMIT_WINDOWFUNC
  166. As of this writing (2024-07-25), such a build fails in various ways
  167. for as-yet-unknown reasons.
  168. */
  169. #endif
  170. #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_WASM_BARE_BONES)
  171. # define SQLITE_WASM_HAS_VTAB 1
  172. #else
  173. # define SQLITE_WASM_HAS_VTAB 0
  174. #endif
  175. #include <assert.h>
  176. /*
  177. ** SQLITE_WASM_EXPORT is functionally identical to EMSCRIPTEN_KEEPALIVE
  178. ** but is not Emscripten-specific. It explicitly marks functions for
  179. ** export into the target wasm file without requiring explicit listing
  180. ** of those functions in Emscripten's -sEXPORTED_FUNCTIONS=... list
  181. ** (or equivalent in other build platforms). Any function with neither
  182. ** this attribute nor which is listed as an explicit export will not
  183. ** be exported from the wasm file (but may still be used internally
  184. ** within the wasm file).
  185. **
  186. ** The functions in this file (sqlite3-wasm.c) which require exporting
  187. ** are marked with this flag. They may also be added to any explicit
  188. ** build-time export list but need not be. All of these APIs are
  189. ** intended for use only within the project's own JS/WASM code, and
  190. ** not by client code, so an argument can be made for reducing their
  191. ** visibility by not including them in any build-time export lists.
  192. **
  193. ** 2022-09-11: it's not yet _proven_ that this approach works in
  194. ** non-Emscripten builds. If not, such builds will need to export
  195. ** those using the --export=... wasm-ld flag (or equivalent). As of
  196. ** this writing we are tied to Emscripten for various reasons
  197. ** and cannot test the library with other build environments.
  198. */
  199. #define SQLITE_WASM_EXPORT __attribute__((used,visibility("default")))
  200. // See also:
  201. //__attribute__((export_name("theExportedName"), used, visibility("default")))
  202. /*
  203. ** Which sqlite3.c we're using needs to be configurable to enable
  204. ** building against a custom copy, e.g. the SEE variant. Note that we
  205. ** #include the .c file, rather than the header, so that the WASM
  206. ** extensions have access to private API internals.
  207. **
  208. ** The caveat here is that custom variants need to account for
  209. ** exporting any necessary symbols (e.g. sqlite3_activate_see()). We
  210. ** cannot export them from here using SQLITE_WASM_EXPORT because that
  211. ** attribute (apparently) has to be part of the function definition.
  212. */
  213. #ifndef SQLITE_C
  214. # define SQLITE_C sqlite3.c /* yes, .c instead of .h. */
  215. #endif
  216. #define INC__STRINGIFY_(f) #f
  217. #define INC__STRINGIFY(f) INC__STRINGIFY_(f)
  218. #include INC__STRINGIFY(SQLITE_C)
  219. #undef INC__STRINGIFY_
  220. #undef INC__STRINGIFY
  221. #undef SQLITE_C
  222. #if 0
  223. /*
  224. ** An EXPERIMENT in implementing a stack-based allocator analog to
  225. ** Emscripten's stackSave(), stackAlloc(), stackRestore().
  226. ** Unfortunately, this cannot work together with Emscripten because
  227. ** Emscripten defines its own native one and we'd stomp on each
  228. ** other's memory. Other than that complication, basic tests show it
  229. ** to work just fine.
  230. **
  231. ** Another option is to malloc() a chunk of our own and call that our
  232. ** "stack".
  233. */
  234. SQLITE_WASM_EXPORT void * sqlite3__wasm_stack_end(void){
  235. extern void __heap_base
  236. /* see https://stackoverflow.com/questions/10038964 */;
  237. return &__heap_base;
  238. }
  239. SQLITE_WASM_EXPORT void * sqlite3__wasm_stack_begin(void){
  240. extern void __data_end;
  241. return &__data_end;
  242. }
  243. static void * pWasmStackPtr = 0;
  244. SQLITE_WASM_EXPORT void * sqlite3__wasm_stack_ptr(void){
  245. if(!pWasmStackPtr) pWasmStackPtr = sqlite3__wasm_stack_end();
  246. return pWasmStackPtr;
  247. }
  248. SQLITE_WASM_EXPORT void sqlite3__wasm_stack_restore(void * p){
  249. pWasmStackPtr = p;
  250. }
  251. SQLITE_WASM_EXPORT void * sqlite3__wasm_stack_alloc(int n){
  252. if(n<=0) return 0;
  253. n = (n + 7) & ~7 /* align to 8-byte boundary */;
  254. unsigned char * const p = (unsigned char *)sqlite3__wasm_stack_ptr();
  255. unsigned const char * const b = (unsigned const char *)sqlite3__wasm_stack_begin();
  256. if(b + n >= p || b + n < b/*overflow*/) return 0;
  257. return pWasmStackPtr = p - n;
  258. }
  259. #endif /* stack allocator experiment */
  260. /*
  261. ** State for the "pseudo-stack" allocator implemented in
  262. ** sqlite3__wasm_pstack_xyz(). In order to avoid colliding with
  263. ** Emscripten-controled stack space, it carves out a bit of stack
  264. ** memory to use for that purpose. This memory ends up in the
  265. ** WASM-managed memory, such that routines which manipulate the wasm
  266. ** heap can also be used to manipulate this memory.
  267. **
  268. ** This particular allocator is intended for small allocations such as
  269. ** storage for output pointers. We cannot reasonably size it large
  270. ** enough for general-purpose string conversions because some of our
  271. ** tests use input files (strings) of 16MB+.
  272. */
  273. static unsigned char PStack_mem[512 * 8] = {0};
  274. static struct {
  275. unsigned const char * const pBegin;/* Start (inclusive) of memory */
  276. unsigned const char * const pEnd; /* One-after-the-end of memory */
  277. unsigned char * pPos; /* Current stack pointer */
  278. } PStack = {
  279. &PStack_mem[0],
  280. &PStack_mem[0] + sizeof(PStack_mem),
  281. &PStack_mem[0] + sizeof(PStack_mem)
  282. };
  283. /*
  284. ** Returns the current pstack position.
  285. */
  286. SQLITE_WASM_EXPORT void * sqlite3__wasm_pstack_ptr(void){
  287. return PStack.pPos;
  288. }
  289. /*
  290. ** Sets the pstack position poitner to p. Results are undefined if the
  291. ** given value did not come from sqlite3__wasm_pstack_ptr().
  292. */
  293. SQLITE_WASM_EXPORT void sqlite3__wasm_pstack_restore(unsigned char * p){
  294. assert(p>=PStack.pBegin && p<=PStack.pEnd && p>=PStack.pPos);
  295. assert(0==((unsigned long long)p & 0x7));
  296. if(p>=PStack.pBegin && p<=PStack.pEnd /*&& p>=PStack.pPos*/){
  297. PStack.pPos = p;
  298. }
  299. }
  300. /*
  301. ** Allocate and zero out n bytes from the pstack. Returns a pointer to
  302. ** the memory on success, 0 on error (including a negative n value). n
  303. ** is always adjusted to be a multiple of 8 and returned memory is
  304. ** always zeroed out before returning (because this keeps the client
  305. ** JS code from having to do so, and most uses of the pstack will
  306. ** call for doing so).
  307. */
  308. SQLITE_WASM_EXPORT void * sqlite3__wasm_pstack_alloc(int n){
  309. if( n<=0 ) return 0;
  310. n = (n + 7) & ~7 /* align to 8-byte boundary */;
  311. if( PStack.pBegin + n > PStack.pPos /*not enough space left*/
  312. || PStack.pBegin + n <= PStack.pBegin /*overflow*/ ) return 0;
  313. memset((PStack.pPos = PStack.pPos - n), 0, (unsigned int)n);
  314. return PStack.pPos;
  315. }
  316. /*
  317. ** Return the number of bytes left which can be
  318. ** sqlite3__wasm_pstack_alloc()'d.
  319. */
  320. SQLITE_WASM_EXPORT int sqlite3__wasm_pstack_remaining(void){
  321. assert(PStack.pPos >= PStack.pBegin);
  322. assert(PStack.pPos <= PStack.pEnd);
  323. return (int)(PStack.pPos - PStack.pBegin);
  324. }
  325. /*
  326. ** Return the total number of bytes available in the pstack, including
  327. ** any space which is currently allocated. This value is a
  328. ** compile-time constant.
  329. */
  330. SQLITE_WASM_EXPORT int sqlite3__wasm_pstack_quota(void){
  331. return (int)(PStack.pEnd - PStack.pBegin);
  332. }
  333. /*
  334. ** This function is NOT part of the sqlite3 public API. It is strictly
  335. ** for use by the sqlite project's own JS/WASM bindings.
  336. **
  337. ** For purposes of certain hand-crafted C/Wasm function bindings, we
  338. ** need a way of reporting errors which is consistent with the rest of
  339. ** the C API, as opposed to throwing JS exceptions. To that end, this
  340. ** internal-use-only function is a thin proxy around
  341. ** sqlite3ErrorWithMessage(). The intent is that it only be used from
  342. ** Wasm bindings such as sqlite3_prepare_v2/v3(), and definitely not
  343. ** from client code.
  344. **
  345. ** Returns err_code.
  346. */
  347. SQLITE_WASM_EXPORT
  348. int sqlite3__wasm_db_error(sqlite3*db, int err_code, const char *zMsg){
  349. if( db!=0 ){
  350. if( 0!=zMsg ){
  351. const int nMsg = sqlite3Strlen30(zMsg);
  352. sqlite3_mutex_enter(sqlite3_db_mutex(db));
  353. sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg);
  354. sqlite3_mutex_leave(sqlite3_db_mutex(db));
  355. }else{
  356. sqlite3ErrorWithMsg(db, err_code, NULL);
  357. }
  358. }
  359. return err_code;
  360. }
  361. #if SQLITE_WASM_ENABLE_C_TESTS
  362. struct WasmTestStruct {
  363. int v4;
  364. void * ppV;
  365. const char * cstr;
  366. int64_t v8;
  367. void (*xFunc)(void*);
  368. };
  369. typedef struct WasmTestStruct WasmTestStruct;
  370. SQLITE_WASM_EXPORT
  371. void sqlite3__wasm_test_struct(WasmTestStruct * s){
  372. if(s){
  373. s->v4 *= 2;
  374. s->v8 = s->v4 * 2;
  375. s->ppV = s;
  376. s->cstr = __FILE__;
  377. if(s->xFunc) s->xFunc(s);
  378. }
  379. return;
  380. }
  381. #endif /* SQLITE_WASM_ENABLE_C_TESTS */
  382. /*
  383. ** This function is NOT part of the sqlite3 public API. It is strictly
  384. ** for use by the sqlite project's own JS/WASM bindings. Unlike the
  385. ** rest of the sqlite3 API, this part requires C99 for snprintf() and
  386. ** variadic macros.
  387. **
  388. ** Returns a string containing a JSON-format "enum" of C-level
  389. ** constants and struct-related metadata intended to be imported into
  390. ** the JS environment. The JSON is initialized the first time this
  391. ** function is called and that result is reused for all future calls.
  392. **
  393. ** If this function returns NULL then it means that the internal
  394. ** buffer is not large enough for the generated JSON and needs to be
  395. ** increased. In debug builds that will trigger an assert().
  396. */
  397. SQLITE_WASM_EXPORT
  398. const char * sqlite3__wasm_enum_json(void){
  399. static char aBuffer[1024 * 20] = {0} /* where the JSON goes */;
  400. int n = 0, nChildren = 0, nStruct = 0
  401. /* output counters for figuring out where commas go */;
  402. char * zPos = &aBuffer[1] /* skip first byte for now to help protect
  403. ** against a small race condition */;
  404. char const * const zEnd = &aBuffer[0] + sizeof(aBuffer) /* one-past-the-end */;
  405. if(aBuffer[0]) return aBuffer;
  406. /* Leave aBuffer[0] at 0 until the end to help guard against a tiny
  407. ** race condition. If this is called twice concurrently, they might
  408. ** end up both writing to aBuffer, but they'll both write the same
  409. ** thing, so that's okay. If we set byte 0 up front then the 2nd
  410. ** instance might return and use the string before the 1st instance
  411. ** is done filling it. */
  412. /* Core output macros... */
  413. #define lenCheck assert(zPos < zEnd - 128 \
  414. && "sqlite3__wasm_enum_json() buffer is too small."); \
  415. if( zPos >= zEnd - 128 ) return 0
  416. #define outf(format,...) \
  417. zPos += snprintf(zPos, ((size_t)(zEnd - zPos)), format, __VA_ARGS__); \
  418. lenCheck
  419. #define out(TXT) outf("%s",TXT)
  420. #define CloseBrace(LEVEL) \
  421. assert(LEVEL<5); memset(zPos, '}', LEVEL); zPos+=LEVEL; lenCheck
  422. /* Macros for emitting maps of integer- and string-type macros to
  423. ** their values. */
  424. #define DefGroup(KEY) n = 0; \
  425. outf("%s\"" #KEY "\": {",(nChildren++ ? "," : ""));
  426. #define DefInt(KEY) \
  427. outf("%s\"%s\": %d", (n++ ? ", " : ""), #KEY, (int)KEY)
  428. #define DefStr(KEY) \
  429. outf("%s\"%s\": \"%s\"", (n++ ? ", " : ""), #KEY, KEY)
  430. #define _DefGroup CloseBrace(1)
  431. /* The following groups are sorted alphabetic by group name. */
  432. DefGroup(access){
  433. DefInt(SQLITE_ACCESS_EXISTS);
  434. DefInt(SQLITE_ACCESS_READWRITE);
  435. DefInt(SQLITE_ACCESS_READ)/*docs say this is unused*/;
  436. } _DefGroup;
  437. DefGroup(authorizer){
  438. DefInt(SQLITE_DENY);
  439. DefInt(SQLITE_IGNORE);
  440. DefInt(SQLITE_CREATE_INDEX);
  441. DefInt(SQLITE_CREATE_TABLE);
  442. DefInt(SQLITE_CREATE_TEMP_INDEX);
  443. DefInt(SQLITE_CREATE_TEMP_TABLE);
  444. DefInt(SQLITE_CREATE_TEMP_TRIGGER);
  445. DefInt(SQLITE_CREATE_TEMP_VIEW);
  446. DefInt(SQLITE_CREATE_TRIGGER);
  447. DefInt(SQLITE_CREATE_VIEW);
  448. DefInt(SQLITE_DELETE);
  449. DefInt(SQLITE_DROP_INDEX);
  450. DefInt(SQLITE_DROP_TABLE);
  451. DefInt(SQLITE_DROP_TEMP_INDEX);
  452. DefInt(SQLITE_DROP_TEMP_TABLE);
  453. DefInt(SQLITE_DROP_TEMP_TRIGGER);
  454. DefInt(SQLITE_DROP_TEMP_VIEW);
  455. DefInt(SQLITE_DROP_TRIGGER);
  456. DefInt(SQLITE_DROP_VIEW);
  457. DefInt(SQLITE_INSERT);
  458. DefInt(SQLITE_PRAGMA);
  459. DefInt(SQLITE_READ);
  460. DefInt(SQLITE_SELECT);
  461. DefInt(SQLITE_TRANSACTION);
  462. DefInt(SQLITE_UPDATE);
  463. DefInt(SQLITE_ATTACH);
  464. DefInt(SQLITE_DETACH);
  465. DefInt(SQLITE_ALTER_TABLE);
  466. DefInt(SQLITE_REINDEX);
  467. DefInt(SQLITE_ANALYZE);
  468. DefInt(SQLITE_CREATE_VTABLE);
  469. DefInt(SQLITE_DROP_VTABLE);
  470. DefInt(SQLITE_FUNCTION);
  471. DefInt(SQLITE_SAVEPOINT);
  472. //DefInt(SQLITE_COPY) /* No longer used */;
  473. DefInt(SQLITE_RECURSIVE);
  474. } _DefGroup;
  475. DefGroup(blobFinalizers) {
  476. /* SQLITE_STATIC/TRANSIENT need to be handled explicitly as
  477. ** integers to avoid casting-related warnings. */
  478. out("\"SQLITE_STATIC\":0, \"SQLITE_TRANSIENT\":-1");
  479. outf(",\"SQLITE_WASM_DEALLOC\": %lld",
  480. (sqlite3_int64)(sqlite3_free));
  481. } _DefGroup;
  482. DefGroup(changeset){
  483. #ifdef SQLITE_CHANGESETSTART_INVERT
  484. DefInt(SQLITE_CHANGESETSTART_INVERT);
  485. DefInt(SQLITE_CHANGESETAPPLY_NOSAVEPOINT);
  486. DefInt(SQLITE_CHANGESETAPPLY_INVERT);
  487. DefInt(SQLITE_CHANGESETAPPLY_IGNORENOOP);
  488. DefInt(SQLITE_CHANGESET_DATA);
  489. DefInt(SQLITE_CHANGESET_NOTFOUND);
  490. DefInt(SQLITE_CHANGESET_CONFLICT);
  491. DefInt(SQLITE_CHANGESET_CONSTRAINT);
  492. DefInt(SQLITE_CHANGESET_FOREIGN_KEY);
  493. DefInt(SQLITE_CHANGESET_OMIT);
  494. DefInt(SQLITE_CHANGESET_REPLACE);
  495. DefInt(SQLITE_CHANGESET_ABORT);
  496. #endif
  497. } _DefGroup;
  498. DefGroup(config){
  499. DefInt(SQLITE_CONFIG_SINGLETHREAD);
  500. DefInt(SQLITE_CONFIG_MULTITHREAD);
  501. DefInt(SQLITE_CONFIG_SERIALIZED);
  502. DefInt(SQLITE_CONFIG_MALLOC);
  503. DefInt(SQLITE_CONFIG_GETMALLOC);
  504. DefInt(SQLITE_CONFIG_SCRATCH);
  505. DefInt(SQLITE_CONFIG_PAGECACHE);
  506. DefInt(SQLITE_CONFIG_HEAP);
  507. DefInt(SQLITE_CONFIG_MEMSTATUS);
  508. DefInt(SQLITE_CONFIG_MUTEX);
  509. DefInt(SQLITE_CONFIG_GETMUTEX);
  510. /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
  511. DefInt(SQLITE_CONFIG_LOOKASIDE);
  512. DefInt(SQLITE_CONFIG_PCACHE);
  513. DefInt(SQLITE_CONFIG_GETPCACHE);
  514. DefInt(SQLITE_CONFIG_LOG);
  515. DefInt(SQLITE_CONFIG_URI);
  516. DefInt(SQLITE_CONFIG_PCACHE2);
  517. DefInt(SQLITE_CONFIG_GETPCACHE2);
  518. DefInt(SQLITE_CONFIG_COVERING_INDEX_SCAN);
  519. DefInt(SQLITE_CONFIG_SQLLOG);
  520. DefInt(SQLITE_CONFIG_MMAP_SIZE);
  521. DefInt(SQLITE_CONFIG_WIN32_HEAPSIZE);
  522. DefInt(SQLITE_CONFIG_PCACHE_HDRSZ);
  523. DefInt(SQLITE_CONFIG_PMASZ);
  524. DefInt(SQLITE_CONFIG_STMTJRNL_SPILL);
  525. DefInt(SQLITE_CONFIG_SMALL_MALLOC);
  526. DefInt(SQLITE_CONFIG_SORTERREF_SIZE);
  527. DefInt(SQLITE_CONFIG_MEMDB_MAXSIZE);
  528. /* maintenance note: we specifically do not include
  529. SQLITE_CONFIG_ROWID_IN_VIEW here, on the grounds that
  530. it's only for legacy support and no apps written with
  531. this API require that. */
  532. } _DefGroup;
  533. DefGroup(dataTypes) {
  534. DefInt(SQLITE_INTEGER);
  535. DefInt(SQLITE_FLOAT);
  536. DefInt(SQLITE_TEXT);
  537. DefInt(SQLITE_BLOB);
  538. DefInt(SQLITE_NULL);
  539. } _DefGroup;
  540. DefGroup(dbConfig){
  541. DefInt(SQLITE_DBCONFIG_MAINDBNAME);
  542. DefInt(SQLITE_DBCONFIG_LOOKASIDE);
  543. DefInt(SQLITE_DBCONFIG_ENABLE_FKEY);
  544. DefInt(SQLITE_DBCONFIG_ENABLE_TRIGGER);
  545. DefInt(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION);
  546. DefInt(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE);
  547. DefInt(SQLITE_DBCONFIG_ENABLE_QPSG);
  548. DefInt(SQLITE_DBCONFIG_TRIGGER_EQP);
  549. DefInt(SQLITE_DBCONFIG_RESET_DATABASE);
  550. DefInt(SQLITE_DBCONFIG_DEFENSIVE);
  551. DefInt(SQLITE_DBCONFIG_WRITABLE_SCHEMA);
  552. DefInt(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE);
  553. DefInt(SQLITE_DBCONFIG_DQS_DML);
  554. DefInt(SQLITE_DBCONFIG_DQS_DDL);
  555. DefInt(SQLITE_DBCONFIG_ENABLE_VIEW);
  556. DefInt(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT);
  557. DefInt(SQLITE_DBCONFIG_TRUSTED_SCHEMA);
  558. DefInt(SQLITE_DBCONFIG_STMT_SCANSTATUS);
  559. DefInt(SQLITE_DBCONFIG_REVERSE_SCANORDER);
  560. DefInt(SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE);
  561. DefInt(SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE);
  562. DefInt(SQLITE_DBCONFIG_ENABLE_COMMENTS);
  563. DefInt(SQLITE_DBCONFIG_MAX);
  564. } _DefGroup;
  565. DefGroup(dbStatus){
  566. DefInt(SQLITE_DBSTATUS_LOOKASIDE_USED);
  567. DefInt(SQLITE_DBSTATUS_CACHE_USED);
  568. DefInt(SQLITE_DBSTATUS_SCHEMA_USED);
  569. DefInt(SQLITE_DBSTATUS_STMT_USED);
  570. DefInt(SQLITE_DBSTATUS_LOOKASIDE_HIT);
  571. DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE);
  572. DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL);
  573. DefInt(SQLITE_DBSTATUS_CACHE_HIT);
  574. DefInt(SQLITE_DBSTATUS_CACHE_MISS);
  575. DefInt(SQLITE_DBSTATUS_CACHE_WRITE);
  576. DefInt(SQLITE_DBSTATUS_DEFERRED_FKS);
  577. DefInt(SQLITE_DBSTATUS_CACHE_USED_SHARED);
  578. DefInt(SQLITE_DBSTATUS_CACHE_SPILL);
  579. DefInt(SQLITE_DBSTATUS_MAX);
  580. } _DefGroup;
  581. DefGroup(encodings) {
  582. /* Noting that the wasm binding only aims to support UTF-8. */
  583. DefInt(SQLITE_UTF8);
  584. DefInt(SQLITE_UTF16LE);
  585. DefInt(SQLITE_UTF16BE);
  586. DefInt(SQLITE_UTF16);
  587. /*deprecated DefInt(SQLITE_ANY); */
  588. DefInt(SQLITE_UTF16_ALIGNED);
  589. } _DefGroup;
  590. DefGroup(fcntl) {
  591. DefInt(SQLITE_FCNTL_LOCKSTATE);
  592. DefInt(SQLITE_FCNTL_GET_LOCKPROXYFILE);
  593. DefInt(SQLITE_FCNTL_SET_LOCKPROXYFILE);
  594. DefInt(SQLITE_FCNTL_LAST_ERRNO);
  595. DefInt(SQLITE_FCNTL_SIZE_HINT);
  596. DefInt(SQLITE_FCNTL_CHUNK_SIZE);
  597. DefInt(SQLITE_FCNTL_FILE_POINTER);
  598. DefInt(SQLITE_FCNTL_SYNC_OMITTED);
  599. DefInt(SQLITE_FCNTL_WIN32_AV_RETRY);
  600. DefInt(SQLITE_FCNTL_PERSIST_WAL);
  601. DefInt(SQLITE_FCNTL_OVERWRITE);
  602. DefInt(SQLITE_FCNTL_VFSNAME);
  603. DefInt(SQLITE_FCNTL_POWERSAFE_OVERWRITE);
  604. DefInt(SQLITE_FCNTL_PRAGMA);
  605. DefInt(SQLITE_FCNTL_BUSYHANDLER);
  606. DefInt(SQLITE_FCNTL_TEMPFILENAME);
  607. DefInt(SQLITE_FCNTL_MMAP_SIZE);
  608. DefInt(SQLITE_FCNTL_TRACE);
  609. DefInt(SQLITE_FCNTL_HAS_MOVED);
  610. DefInt(SQLITE_FCNTL_SYNC);
  611. DefInt(SQLITE_FCNTL_COMMIT_PHASETWO);
  612. DefInt(SQLITE_FCNTL_WIN32_SET_HANDLE);
  613. DefInt(SQLITE_FCNTL_WAL_BLOCK);
  614. DefInt(SQLITE_FCNTL_ZIPVFS);
  615. DefInt(SQLITE_FCNTL_RBU);
  616. DefInt(SQLITE_FCNTL_VFS_POINTER);
  617. DefInt(SQLITE_FCNTL_JOURNAL_POINTER);
  618. DefInt(SQLITE_FCNTL_WIN32_GET_HANDLE);
  619. DefInt(SQLITE_FCNTL_PDB);
  620. DefInt(SQLITE_FCNTL_BEGIN_ATOMIC_WRITE);
  621. DefInt(SQLITE_FCNTL_COMMIT_ATOMIC_WRITE);
  622. DefInt(SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE);
  623. DefInt(SQLITE_FCNTL_LOCK_TIMEOUT);
  624. DefInt(SQLITE_FCNTL_DATA_VERSION);
  625. DefInt(SQLITE_FCNTL_SIZE_LIMIT);
  626. DefInt(SQLITE_FCNTL_CKPT_DONE);
  627. DefInt(SQLITE_FCNTL_RESERVE_BYTES);
  628. DefInt(SQLITE_FCNTL_CKPT_START);
  629. DefInt(SQLITE_FCNTL_EXTERNAL_READER);
  630. DefInt(SQLITE_FCNTL_CKSM_FILE);
  631. DefInt(SQLITE_FCNTL_RESET_CACHE);
  632. } _DefGroup;
  633. DefGroup(flock) {
  634. DefInt(SQLITE_LOCK_NONE);
  635. DefInt(SQLITE_LOCK_SHARED);
  636. DefInt(SQLITE_LOCK_RESERVED);
  637. DefInt(SQLITE_LOCK_PENDING);
  638. DefInt(SQLITE_LOCK_EXCLUSIVE);
  639. } _DefGroup;
  640. DefGroup(ioCap) {
  641. DefInt(SQLITE_IOCAP_ATOMIC);
  642. DefInt(SQLITE_IOCAP_ATOMIC512);
  643. DefInt(SQLITE_IOCAP_ATOMIC1K);
  644. DefInt(SQLITE_IOCAP_ATOMIC2K);
  645. DefInt(SQLITE_IOCAP_ATOMIC4K);
  646. DefInt(SQLITE_IOCAP_ATOMIC8K);
  647. DefInt(SQLITE_IOCAP_ATOMIC16K);
  648. DefInt(SQLITE_IOCAP_ATOMIC32K);
  649. DefInt(SQLITE_IOCAP_ATOMIC64K);
  650. DefInt(SQLITE_IOCAP_SAFE_APPEND);
  651. DefInt(SQLITE_IOCAP_SEQUENTIAL);
  652. DefInt(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN);
  653. DefInt(SQLITE_IOCAP_POWERSAFE_OVERWRITE);
  654. DefInt(SQLITE_IOCAP_IMMUTABLE);
  655. DefInt(SQLITE_IOCAP_BATCH_ATOMIC);
  656. } _DefGroup;
  657. DefGroup(limits) {
  658. DefInt(SQLITE_MAX_ALLOCATION_SIZE);
  659. DefInt(SQLITE_LIMIT_LENGTH);
  660. DefInt(SQLITE_MAX_LENGTH);
  661. DefInt(SQLITE_LIMIT_SQL_LENGTH);
  662. DefInt(SQLITE_MAX_SQL_LENGTH);
  663. DefInt(SQLITE_LIMIT_COLUMN);
  664. DefInt(SQLITE_MAX_COLUMN);
  665. DefInt(SQLITE_LIMIT_EXPR_DEPTH);
  666. DefInt(SQLITE_MAX_EXPR_DEPTH);
  667. DefInt(SQLITE_LIMIT_COMPOUND_SELECT);
  668. DefInt(SQLITE_MAX_COMPOUND_SELECT);
  669. DefInt(SQLITE_LIMIT_VDBE_OP);
  670. DefInt(SQLITE_MAX_VDBE_OP);
  671. DefInt(SQLITE_LIMIT_FUNCTION_ARG);
  672. DefInt(SQLITE_MAX_FUNCTION_ARG);
  673. DefInt(SQLITE_LIMIT_ATTACHED);
  674. DefInt(SQLITE_MAX_ATTACHED);
  675. DefInt(SQLITE_LIMIT_LIKE_PATTERN_LENGTH);
  676. DefInt(SQLITE_MAX_LIKE_PATTERN_LENGTH);
  677. DefInt(SQLITE_LIMIT_VARIABLE_NUMBER);
  678. DefInt(SQLITE_MAX_VARIABLE_NUMBER);
  679. DefInt(SQLITE_LIMIT_TRIGGER_DEPTH);
  680. DefInt(SQLITE_MAX_TRIGGER_DEPTH);
  681. DefInt(SQLITE_LIMIT_WORKER_THREADS);
  682. DefInt(SQLITE_MAX_WORKER_THREADS);
  683. } _DefGroup;
  684. DefGroup(openFlags) {
  685. /* Noting that not all of these will have any effect in
  686. ** WASM-space. */
  687. DefInt(SQLITE_OPEN_READONLY);
  688. DefInt(SQLITE_OPEN_READWRITE);
  689. DefInt(SQLITE_OPEN_CREATE);
  690. DefInt(SQLITE_OPEN_URI);
  691. DefInt(SQLITE_OPEN_MEMORY);
  692. DefInt(SQLITE_OPEN_NOMUTEX);
  693. DefInt(SQLITE_OPEN_FULLMUTEX);
  694. DefInt(SQLITE_OPEN_SHAREDCACHE);
  695. DefInt(SQLITE_OPEN_PRIVATECACHE);
  696. DefInt(SQLITE_OPEN_EXRESCODE);
  697. DefInt(SQLITE_OPEN_NOFOLLOW);
  698. /* OPEN flags for use with VFSes... */
  699. DefInt(SQLITE_OPEN_MAIN_DB);
  700. DefInt(SQLITE_OPEN_MAIN_JOURNAL);
  701. DefInt(SQLITE_OPEN_TEMP_DB);
  702. DefInt(SQLITE_OPEN_TEMP_JOURNAL);
  703. DefInt(SQLITE_OPEN_TRANSIENT_DB);
  704. DefInt(SQLITE_OPEN_SUBJOURNAL);
  705. DefInt(SQLITE_OPEN_SUPER_JOURNAL);
  706. DefInt(SQLITE_OPEN_WAL);
  707. DefInt(SQLITE_OPEN_DELETEONCLOSE);
  708. DefInt(SQLITE_OPEN_EXCLUSIVE);
  709. } _DefGroup;
  710. DefGroup(prepareFlags) {
  711. DefInt(SQLITE_PREPARE_PERSISTENT);
  712. DefInt(SQLITE_PREPARE_NORMALIZE);
  713. DefInt(SQLITE_PREPARE_NO_VTAB);
  714. } _DefGroup;
  715. DefGroup(resultCodes) {
  716. DefInt(SQLITE_OK);
  717. DefInt(SQLITE_ERROR);
  718. DefInt(SQLITE_INTERNAL);
  719. DefInt(SQLITE_PERM);
  720. DefInt(SQLITE_ABORT);
  721. DefInt(SQLITE_BUSY);
  722. DefInt(SQLITE_LOCKED);
  723. DefInt(SQLITE_NOMEM);
  724. DefInt(SQLITE_READONLY);
  725. DefInt(SQLITE_INTERRUPT);
  726. DefInt(SQLITE_IOERR);
  727. DefInt(SQLITE_CORRUPT);
  728. DefInt(SQLITE_NOTFOUND);
  729. DefInt(SQLITE_FULL);
  730. DefInt(SQLITE_CANTOPEN);
  731. DefInt(SQLITE_PROTOCOL);
  732. DefInt(SQLITE_EMPTY);
  733. DefInt(SQLITE_SCHEMA);
  734. DefInt(SQLITE_TOOBIG);
  735. DefInt(SQLITE_CONSTRAINT);
  736. DefInt(SQLITE_MISMATCH);
  737. DefInt(SQLITE_MISUSE);
  738. DefInt(SQLITE_NOLFS);
  739. DefInt(SQLITE_AUTH);
  740. DefInt(SQLITE_FORMAT);
  741. DefInt(SQLITE_RANGE);
  742. DefInt(SQLITE_NOTADB);
  743. DefInt(SQLITE_NOTICE);
  744. DefInt(SQLITE_WARNING);
  745. DefInt(SQLITE_ROW);
  746. DefInt(SQLITE_DONE);
  747. // Extended Result Codes
  748. DefInt(SQLITE_ERROR_MISSING_COLLSEQ);
  749. DefInt(SQLITE_ERROR_RETRY);
  750. DefInt(SQLITE_ERROR_SNAPSHOT);
  751. DefInt(SQLITE_IOERR_READ);
  752. DefInt(SQLITE_IOERR_SHORT_READ);
  753. DefInt(SQLITE_IOERR_WRITE);
  754. DefInt(SQLITE_IOERR_FSYNC);
  755. DefInt(SQLITE_IOERR_DIR_FSYNC);
  756. DefInt(SQLITE_IOERR_TRUNCATE);
  757. DefInt(SQLITE_IOERR_FSTAT);
  758. DefInt(SQLITE_IOERR_UNLOCK);
  759. DefInt(SQLITE_IOERR_RDLOCK);
  760. DefInt(SQLITE_IOERR_DELETE);
  761. DefInt(SQLITE_IOERR_BLOCKED);
  762. DefInt(SQLITE_IOERR_NOMEM);
  763. DefInt(SQLITE_IOERR_ACCESS);
  764. DefInt(SQLITE_IOERR_CHECKRESERVEDLOCK);
  765. DefInt(SQLITE_IOERR_LOCK);
  766. DefInt(SQLITE_IOERR_CLOSE);
  767. DefInt(SQLITE_IOERR_DIR_CLOSE);
  768. DefInt(SQLITE_IOERR_SHMOPEN);
  769. DefInt(SQLITE_IOERR_SHMSIZE);
  770. DefInt(SQLITE_IOERR_SHMLOCK);
  771. DefInt(SQLITE_IOERR_SHMMAP);
  772. DefInt(SQLITE_IOERR_SEEK);
  773. DefInt(SQLITE_IOERR_DELETE_NOENT);
  774. DefInt(SQLITE_IOERR_MMAP);
  775. DefInt(SQLITE_IOERR_GETTEMPPATH);
  776. DefInt(SQLITE_IOERR_CONVPATH);
  777. DefInt(SQLITE_IOERR_VNODE);
  778. DefInt(SQLITE_IOERR_AUTH);
  779. DefInt(SQLITE_IOERR_BEGIN_ATOMIC);
  780. DefInt(SQLITE_IOERR_COMMIT_ATOMIC);
  781. DefInt(SQLITE_IOERR_ROLLBACK_ATOMIC);
  782. DefInt(SQLITE_IOERR_DATA);
  783. DefInt(SQLITE_IOERR_CORRUPTFS);
  784. DefInt(SQLITE_LOCKED_SHAREDCACHE);
  785. DefInt(SQLITE_LOCKED_VTAB);
  786. DefInt(SQLITE_BUSY_RECOVERY);
  787. DefInt(SQLITE_BUSY_SNAPSHOT);
  788. DefInt(SQLITE_BUSY_TIMEOUT);
  789. DefInt(SQLITE_CANTOPEN_NOTEMPDIR);
  790. DefInt(SQLITE_CANTOPEN_ISDIR);
  791. DefInt(SQLITE_CANTOPEN_FULLPATH);
  792. DefInt(SQLITE_CANTOPEN_CONVPATH);
  793. //DefInt(SQLITE_CANTOPEN_DIRTYWAL)/*docs say not used*/;
  794. DefInt(SQLITE_CANTOPEN_SYMLINK);
  795. DefInt(SQLITE_CORRUPT_VTAB);
  796. DefInt(SQLITE_CORRUPT_SEQUENCE);
  797. DefInt(SQLITE_CORRUPT_INDEX);
  798. DefInt(SQLITE_READONLY_RECOVERY);
  799. DefInt(SQLITE_READONLY_CANTLOCK);
  800. DefInt(SQLITE_READONLY_ROLLBACK);
  801. DefInt(SQLITE_READONLY_DBMOVED);
  802. DefInt(SQLITE_READONLY_CANTINIT);
  803. DefInt(SQLITE_READONLY_DIRECTORY);
  804. DefInt(SQLITE_ABORT_ROLLBACK);
  805. DefInt(SQLITE_CONSTRAINT_CHECK);
  806. DefInt(SQLITE_CONSTRAINT_COMMITHOOK);
  807. DefInt(SQLITE_CONSTRAINT_FOREIGNKEY);
  808. DefInt(SQLITE_CONSTRAINT_FUNCTION);
  809. DefInt(SQLITE_CONSTRAINT_NOTNULL);
  810. DefInt(SQLITE_CONSTRAINT_PRIMARYKEY);
  811. DefInt(SQLITE_CONSTRAINT_TRIGGER);
  812. DefInt(SQLITE_CONSTRAINT_UNIQUE);
  813. DefInt(SQLITE_CONSTRAINT_VTAB);
  814. DefInt(SQLITE_CONSTRAINT_ROWID);
  815. DefInt(SQLITE_CONSTRAINT_PINNED);
  816. DefInt(SQLITE_CONSTRAINT_DATATYPE);
  817. DefInt(SQLITE_NOTICE_RECOVER_WAL);
  818. DefInt(SQLITE_NOTICE_RECOVER_ROLLBACK);
  819. DefInt(SQLITE_WARNING_AUTOINDEX);
  820. DefInt(SQLITE_AUTH_USER);
  821. DefInt(SQLITE_OK_LOAD_PERMANENTLY);
  822. //DefInt(SQLITE_OK_SYMLINK) /* internal use only */;
  823. } _DefGroup;
  824. DefGroup(serialize){
  825. DefInt(SQLITE_SERIALIZE_NOCOPY);
  826. DefInt(SQLITE_DESERIALIZE_FREEONCLOSE);
  827. DefInt(SQLITE_DESERIALIZE_READONLY);
  828. DefInt(SQLITE_DESERIALIZE_RESIZEABLE);
  829. } _DefGroup;
  830. DefGroup(session){
  831. #ifdef SQLITE_SESSION_CONFIG_STRMSIZE
  832. DefInt(SQLITE_SESSION_CONFIG_STRMSIZE);
  833. DefInt(SQLITE_SESSION_OBJCONFIG_SIZE);
  834. #endif
  835. } _DefGroup;
  836. DefGroup(sqlite3Status){
  837. DefInt(SQLITE_STATUS_MEMORY_USED);
  838. DefInt(SQLITE_STATUS_PAGECACHE_USED);
  839. DefInt(SQLITE_STATUS_PAGECACHE_OVERFLOW);
  840. //DefInt(SQLITE_STATUS_SCRATCH_USED) /* NOT USED */;
  841. //DefInt(SQLITE_STATUS_SCRATCH_OVERFLOW) /* NOT USED */;
  842. DefInt(SQLITE_STATUS_MALLOC_SIZE);
  843. DefInt(SQLITE_STATUS_PARSER_STACK);
  844. DefInt(SQLITE_STATUS_PAGECACHE_SIZE);
  845. //DefInt(SQLITE_STATUS_SCRATCH_SIZE) /* NOT USED */;
  846. DefInt(SQLITE_STATUS_MALLOC_COUNT);
  847. } _DefGroup;
  848. DefGroup(stmtStatus){
  849. DefInt(SQLITE_STMTSTATUS_FULLSCAN_STEP);
  850. DefInt(SQLITE_STMTSTATUS_SORT);
  851. DefInt(SQLITE_STMTSTATUS_AUTOINDEX);
  852. DefInt(SQLITE_STMTSTATUS_VM_STEP);
  853. DefInt(SQLITE_STMTSTATUS_REPREPARE);
  854. DefInt(SQLITE_STMTSTATUS_RUN);
  855. DefInt(SQLITE_STMTSTATUS_FILTER_MISS);
  856. DefInt(SQLITE_STMTSTATUS_FILTER_HIT);
  857. DefInt(SQLITE_STMTSTATUS_MEMUSED);
  858. } _DefGroup;
  859. DefGroup(syncFlags) {
  860. DefInt(SQLITE_SYNC_NORMAL);
  861. DefInt(SQLITE_SYNC_FULL);
  862. DefInt(SQLITE_SYNC_DATAONLY);
  863. } _DefGroup;
  864. DefGroup(trace) {
  865. DefInt(SQLITE_TRACE_STMT);
  866. DefInt(SQLITE_TRACE_PROFILE);
  867. DefInt(SQLITE_TRACE_ROW);
  868. DefInt(SQLITE_TRACE_CLOSE);
  869. } _DefGroup;
  870. DefGroup(txnState){
  871. DefInt(SQLITE_TXN_NONE);
  872. DefInt(SQLITE_TXN_READ);
  873. DefInt(SQLITE_TXN_WRITE);
  874. } _DefGroup;
  875. DefGroup(udfFlags) {
  876. DefInt(SQLITE_DETERMINISTIC);
  877. DefInt(SQLITE_DIRECTONLY);
  878. DefInt(SQLITE_INNOCUOUS);
  879. DefInt(SQLITE_SUBTYPE);
  880. DefInt(SQLITE_RESULT_SUBTYPE);
  881. } _DefGroup;
  882. DefGroup(version) {
  883. DefInt(SQLITE_VERSION_NUMBER);
  884. DefStr(SQLITE_VERSION);
  885. DefStr(SQLITE_SOURCE_ID);
  886. } _DefGroup;
  887. DefGroup(vtab) {
  888. #if SQLITE_WASM_HAS_VTAB
  889. DefInt(SQLITE_INDEX_SCAN_UNIQUE);
  890. DefInt(SQLITE_INDEX_CONSTRAINT_EQ);
  891. DefInt(SQLITE_INDEX_CONSTRAINT_GT);
  892. DefInt(SQLITE_INDEX_CONSTRAINT_LE);
  893. DefInt(SQLITE_INDEX_CONSTRAINT_LT);
  894. DefInt(SQLITE_INDEX_CONSTRAINT_GE);
  895. DefInt(SQLITE_INDEX_CONSTRAINT_MATCH);
  896. DefInt(SQLITE_INDEX_CONSTRAINT_LIKE);
  897. DefInt(SQLITE_INDEX_CONSTRAINT_GLOB);
  898. DefInt(SQLITE_INDEX_CONSTRAINT_REGEXP);
  899. DefInt(SQLITE_INDEX_CONSTRAINT_NE);
  900. DefInt(SQLITE_INDEX_CONSTRAINT_ISNOT);
  901. DefInt(SQLITE_INDEX_CONSTRAINT_ISNOTNULL);
  902. DefInt(SQLITE_INDEX_CONSTRAINT_ISNULL);
  903. DefInt(SQLITE_INDEX_CONSTRAINT_IS);
  904. DefInt(SQLITE_INDEX_CONSTRAINT_LIMIT);
  905. DefInt(SQLITE_INDEX_CONSTRAINT_OFFSET);
  906. DefInt(SQLITE_INDEX_CONSTRAINT_FUNCTION);
  907. DefInt(SQLITE_VTAB_CONSTRAINT_SUPPORT);
  908. DefInt(SQLITE_VTAB_INNOCUOUS);
  909. DefInt(SQLITE_VTAB_DIRECTONLY);
  910. DefInt(SQLITE_VTAB_USES_ALL_SCHEMAS);
  911. DefInt(SQLITE_ROLLBACK);
  912. //DefInt(SQLITE_IGNORE); // Also used by sqlite3_authorizer() callback
  913. DefInt(SQLITE_FAIL);
  914. //DefInt(SQLITE_ABORT); // Also an error code
  915. DefInt(SQLITE_REPLACE);
  916. #endif /*SQLITE_WASM_HAS_VTAB*/
  917. } _DefGroup;
  918. #undef DefGroup
  919. #undef DefStr
  920. #undef DefInt
  921. #undef _DefGroup
  922. /*
  923. ** Emit an array of "StructBinder" struct descripions, which look
  924. ** like:
  925. **
  926. ** {
  927. ** "name": "MyStruct",
  928. ** "sizeof": 16,
  929. ** "members": {
  930. ** "member1": {"offset": 0,"sizeof": 4,"signature": "i"},
  931. ** "member2": {"offset": 4,"sizeof": 4,"signature": "p"},
  932. ** "member3": {"offset": 8,"sizeof": 8,"signature": "j"}
  933. ** }
  934. ** }
  935. **
  936. ** Detailed documentation for those bits are in the docs for the
  937. ** Jaccwabyt JS-side component.
  938. */
  939. /** Macros for emitting StructBinder description. */
  940. #define StructBinder__(TYPE) \
  941. n = 0; \
  942. outf("%s{", (nStruct++ ? ", " : "")); \
  943. out("\"name\": \"" # TYPE "\","); \
  944. outf("\"sizeof\": %d", (int)sizeof(TYPE)); \
  945. out(",\"members\": {");
  946. #define StructBinder_(T) StructBinder__(T)
  947. /** ^^^ indirection needed to expand CurrentStruct */
  948. #define StructBinder StructBinder_(CurrentStruct)
  949. #define _StructBinder CloseBrace(2)
  950. #define M(MEMBER,SIG) \
  951. outf("%s\"%s\": " \
  952. "{\"offset\":%d,\"sizeof\": %d,\"signature\":\"%s\"}", \
  953. (n++ ? ", " : ""), #MEMBER, \
  954. (int)offsetof(CurrentStruct,MEMBER), \
  955. (int)sizeof(((CurrentStruct*)0)->MEMBER), \
  956. SIG)
  957. nStruct = 0;
  958. out(", \"structs\": ["); {
  959. #define CurrentStruct sqlite3_vfs
  960. StructBinder {
  961. M(iVersion, "i");
  962. M(szOsFile, "i");
  963. M(mxPathname, "i");
  964. M(pNext, "p");
  965. M(zName, "s");
  966. M(pAppData, "p");
  967. M(xOpen, "i(pppip)");
  968. M(xDelete, "i(ppi)");
  969. M(xAccess, "i(ppip)");
  970. M(xFullPathname, "i(ppip)");
  971. M(xDlOpen, "p(pp)");
  972. M(xDlError, "p(pip)");
  973. M(xDlSym, "p()");
  974. M(xDlClose, "v(pp)");
  975. M(xRandomness, "i(pip)");
  976. M(xSleep, "i(pi)");
  977. M(xCurrentTime, "i(pp)");
  978. M(xGetLastError, "i(pip)");
  979. M(xCurrentTimeInt64, "i(pp)");
  980. M(xSetSystemCall, "i(ppp)");
  981. M(xGetSystemCall, "p(pp)");
  982. M(xNextSystemCall, "p(pp)");
  983. } _StructBinder;
  984. #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
  1008. #define CurrentStruct sqlite3_file
  1009. StructBinder {
  1010. M(pMethods, "p");
  1011. } _StructBinder;
  1012. #undef CurrentStruct
  1013. #define CurrentStruct sqlite3_kvvfs_methods
  1014. StructBinder {
  1015. M(xRead, "i(sspi)");
  1016. M(xWrite, "i(sss)");
  1017. M(xDelete, "i(ss)");
  1018. M(nKeySize, "i");
  1019. } _StructBinder;
  1020. #undef CurrentStruct
  1021. #if SQLITE_WASM_HAS_VTAB
  1022. #define CurrentStruct sqlite3_vtab
  1023. StructBinder {
  1024. M(pModule, "p");
  1025. M(nRef, "i");
  1026. M(zErrMsg, "p");
  1027. } _StructBinder;
  1028. #undef CurrentStruct
  1029. #define CurrentStruct sqlite3_vtab_cursor
  1030. StructBinder {
  1031. M(pVtab, "p");
  1032. } _StructBinder;
  1033. #undef CurrentStruct
  1034. #define CurrentStruct sqlite3_module
  1035. StructBinder {
  1036. M(iVersion, "i");
  1037. M(xCreate, "i(ppippp)");
  1038. M(xConnect, "i(ppippp)");
  1039. M(xBestIndex, "i(pp)");
  1040. M(xDisconnect, "i(p)");
  1041. M(xDestroy, "i(p)");
  1042. M(xOpen, "i(pp)");
  1043. M(xClose, "i(p)");
  1044. M(xFilter, "i(pisip)");
  1045. M(xNext, "i(p)");
  1046. M(xEof, "i(p)");
  1047. M(xColumn, "i(ppi)");
  1048. M(xRowid, "i(pp)");
  1049. M(xUpdate, "i(pipp)");
  1050. M(xBegin, "i(p)");
  1051. M(xSync, "i(p)");
  1052. M(xCommit, "i(p)");
  1053. M(xRollback, "i(p)");
  1054. M(xFindFunction, "i(pispp)");
  1055. M(xRename, "i(ps)");
  1056. // ^^^ v1. v2+ follows...
  1057. M(xSavepoint, "i(pi)");
  1058. M(xRelease, "i(pi)");
  1059. M(xRollbackTo, "i(pi)");
  1060. // ^^^ v2. v3+ follows...
  1061. M(xShadowName, "i(s)");
  1062. } _StructBinder;
  1063. #undef CurrentStruct
  1064. /**
  1065. ** Workaround: in order to map the various inner structs from
  1066. ** sqlite3_index_info, we have to uplift those into constructs we
  1067. ** can access by type name. These structs _must_ match their
  1068. ** in-sqlite3_index_info counterparts byte for byte.
  1069. */
  1070. typedef struct {
  1071. int iColumn;
  1072. unsigned char op;
  1073. unsigned char usable;
  1074. int iTermOffset;
  1075. } sqlite3_index_constraint;
  1076. typedef struct {
  1077. int iColumn;
  1078. unsigned char desc;
  1079. } sqlite3_index_orderby;
  1080. typedef struct {
  1081. int argvIndex;
  1082. unsigned char omit;
  1083. } sqlite3_index_constraint_usage;
  1084. { /* Validate that the above struct sizeof()s match
  1085. ** expectations. We could improve upon this by
  1086. ** checking the offsetof() for each member. */
  1087. const sqlite3_index_info siiCheck;
  1088. #define IndexSzCheck(T,M) \
  1089. (sizeof(T) == sizeof(*siiCheck.M))
  1090. if(!IndexSzCheck(sqlite3_index_constraint,aConstraint)
  1091. || !IndexSzCheck(sqlite3_index_orderby,aOrderBy)
  1092. || !IndexSzCheck(sqlite3_index_constraint_usage,aConstraintUsage)){
  1093. assert(!"sizeof mismatch in sqlite3_index_... struct(s)");
  1094. return 0;
  1095. }
  1096. #undef IndexSzCheck
  1097. }
  1098. #define CurrentStruct sqlite3_index_constraint
  1099. StructBinder {
  1100. M(iColumn, "i");
  1101. M(op, "C");
  1102. M(usable, "C");
  1103. M(iTermOffset, "i");
  1104. } _StructBinder;
  1105. #undef CurrentStruct
  1106. #define CurrentStruct sqlite3_index_orderby
  1107. StructBinder {
  1108. M(iColumn, "i");
  1109. M(desc, "C");
  1110. } _StructBinder;
  1111. #undef CurrentStruct
  1112. #define CurrentStruct sqlite3_index_constraint_usage
  1113. StructBinder {
  1114. M(argvIndex, "i");
  1115. M(omit, "C");
  1116. } _StructBinder;
  1117. #undef CurrentStruct
  1118. #define CurrentStruct sqlite3_index_info
  1119. StructBinder {
  1120. M(nConstraint, "i");
  1121. M(aConstraint, "p");
  1122. M(nOrderBy, "i");
  1123. M(aOrderBy, "p");
  1124. M(aConstraintUsage, "p");
  1125. M(idxNum, "i");
  1126. M(idxStr, "p");
  1127. M(needToFreeIdxStr, "i");
  1128. M(orderByConsumed, "i");
  1129. M(estimatedCost, "d");
  1130. M(estimatedRows, "j");
  1131. M(idxFlags, "i");
  1132. M(colUsed, "j");
  1133. } _StructBinder;
  1134. #undef CurrentStruct
  1135. #endif /*SQLITE_WASM_HAS_VTAB*/
  1136. #if SQLITE_WASM_ENABLE_C_TESTS
  1137. #define CurrentStruct WasmTestStruct
  1138. StructBinder {
  1139. M(v4, "i");
  1140. M(cstr, "s");
  1141. M(ppV, "p");
  1142. M(v8, "j");
  1143. M(xFunc, "v(p)");
  1144. } _StructBinder;
  1145. #undef CurrentStruct
  1146. #endif /*SQLITE_WASM_ENABLE_C_TESTS*/
  1147. } out( "]"/*structs*/);
  1148. out("}"/*top-level object*/);
  1149. *zPos = 0;
  1150. aBuffer[0] = '{'/*end of the race-condition workaround*/;
  1151. return aBuffer;
  1152. #undef StructBinder
  1153. #undef StructBinder_
  1154. #undef StructBinder__
  1155. #undef M
  1156. #undef _StructBinder
  1157. #undef CloseBrace
  1158. #undef out
  1159. #undef outf
  1160. #undef lenCheck
  1161. }
  1162. /*
  1163. ** This function is NOT part of the sqlite3 public API. It is strictly
  1164. ** for use by the sqlite project's own JS/WASM bindings.
  1165. **
  1166. ** This function invokes the xDelete method of the given VFS (or the
  1167. ** default VFS if pVfs is NULL), passing on the given filename. If
  1168. ** zName is NULL, no default VFS is found, or it has no xDelete
  1169. ** method, SQLITE_MISUSE is returned, else the result of the xDelete()
  1170. ** call is returned.
  1171. */
  1172. SQLITE_WASM_EXPORT
  1173. int sqlite3__wasm_vfs_unlink(sqlite3_vfs *pVfs, const char *zName){
  1174. int rc = SQLITE_MISUSE /* ??? */;
  1175. if( 0==pVfs && 0!=zName ) pVfs = sqlite3_vfs_find(0);
  1176. if( zName && pVfs && pVfs->xDelete ){
  1177. rc = pVfs->xDelete(pVfs, zName, 1);
  1178. }
  1179. return rc;
  1180. }
  1181. /*
  1182. ** This function is NOT part of the sqlite3 public API. It is strictly
  1183. ** for use by the sqlite project's own JS/WASM bindings.
  1184. **
  1185. ** Returns a pointer to the given DB's VFS for the given DB name,
  1186. ** defaulting to "main" if zDbName is 0. Returns 0 if no db with the
  1187. ** given name is open.
  1188. */
  1189. SQLITE_WASM_EXPORT
  1190. sqlite3_vfs * sqlite3__wasm_db_vfs(sqlite3 *pDb, const char *zDbName){
  1191. sqlite3_vfs * pVfs = 0;
  1192. sqlite3_file_control(pDb, zDbName ? zDbName : "main",
  1193. SQLITE_FCNTL_VFS_POINTER, &pVfs);
  1194. return pVfs;
  1195. }
  1196. /*
  1197. ** This function is NOT part of the sqlite3 public API. It is strictly
  1198. ** for use by the sqlite project's own JS/WASM bindings.
  1199. **
  1200. ** This function resets the given db pointer's database as described at
  1201. **
  1202. ** https://sqlite.org/c3ref/c_dbconfig_defensive.html#sqlitedbconfigresetdatabase
  1203. **
  1204. ** But beware: virtual tables destroyed that way do not have their
  1205. ** xDestroy() called, so will leak if they require that function for
  1206. ** proper cleanup.
  1207. **
  1208. ** Returns 0 on success, an SQLITE_xxx code on error. Returns
  1209. ** SQLITE_MISUSE if pDb is NULL.
  1210. */
  1211. SQLITE_WASM_EXPORT
  1212. int sqlite3__wasm_db_reset(sqlite3 *pDb){
  1213. int rc = SQLITE_MISUSE;
  1214. if( pDb ){
  1215. sqlite3_table_column_metadata(pDb, "main", 0, 0, 0, 0, 0, 0, 0);
  1216. rc = sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
  1217. if( 0==rc ){
  1218. rc = sqlite3_exec(pDb, "VACUUM", 0, 0, 0);
  1219. sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
  1220. }
  1221. }
  1222. return rc;
  1223. }
  1224. /*
  1225. ** This function is NOT part of the sqlite3 public API. It is strictly
  1226. ** for use by the sqlite project's own JS/WASM bindings.
  1227. **
  1228. ** Uses the given database's VFS xRead to stream the db file's
  1229. ** contents out to the given callback. The callback gets a single
  1230. ** chunk of size n (its 2nd argument) on each call and must return 0
  1231. ** on success, non-0 on error. This function returns 0 on success,
  1232. ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
  1233. ** code from the callback. Note that this is not thread-friendly: it
  1234. ** expects that it will be the only thread reading the db file and
  1235. ** takes no measures to ensure that is the case.
  1236. **
  1237. ** This implementation appears to work fine, but
  1238. ** sqlite3__wasm_db_serialize() is arguably the better way to achieve
  1239. ** this.
  1240. */
  1241. SQLITE_WASM_EXPORT
  1242. int sqlite3__wasm_db_export_chunked( sqlite3* pDb,
  1243. int (*xCallback)(unsigned const char *zOut, int n) ){
  1244. sqlite3_int64 nSize = 0;
  1245. sqlite3_int64 nPos = 0;
  1246. sqlite3_file * pFile = 0;
  1247. unsigned char buf[1024 * 8];
  1248. int nBuf = (int)sizeof(buf);
  1249. int rc = pDb
  1250. ? sqlite3_file_control(pDb, "main",
  1251. SQLITE_FCNTL_FILE_POINTER, &pFile)
  1252. : SQLITE_NOTFOUND;
  1253. if( rc ) return rc;
  1254. rc = pFile->pMethods->xFileSize(pFile, &nSize);
  1255. if( rc ) return rc;
  1256. if(nSize % nBuf){
  1257. /* DB size is not an even multiple of the buffer size. Reduce
  1258. ** buffer size so that we do not unduly inflate the db size
  1259. ** with zero-padding when exporting. */
  1260. if(0 == nSize % 4096) nBuf = 4096;
  1261. else if(0 == nSize % 2048) nBuf = 2048;
  1262. else if(0 == nSize % 1024) nBuf = 1024;
  1263. else nBuf = 512;
  1264. }
  1265. for( ; 0==rc && nPos<nSize; nPos += nBuf ){
  1266. rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
  1267. if( SQLITE_IOERR_SHORT_READ == rc ){
  1268. rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
  1269. }
  1270. if( 0==rc ) rc = xCallback(buf, nBuf);
  1271. }
  1272. return rc;
  1273. }
  1274. /*
  1275. ** This function is NOT part of the sqlite3 public API. It is strictly
  1276. ** for use by the sqlite project's own JS/WASM bindings.
  1277. **
  1278. ** A proxy for sqlite3_serialize() which serializes the schema zSchema
  1279. ** of pDb, placing the serialized output in pOut and nOut. nOut may be
  1280. ** NULL. If zSchema is NULL then "main" is assumed. If pDb or pOut are
  1281. ** NULL then SQLITE_MISUSE is returned. If allocation of the
  1282. ** serialized copy fails, SQLITE_NOMEM is returned. On success, 0 is
  1283. ** returned and `*pOut` will contain a pointer to the memory unless
  1284. ** mFlags includes SQLITE_SERIALIZE_NOCOPY and the database has no
  1285. ** contiguous memory representation, in which case `*pOut` will be
  1286. ** NULL but 0 will be returned.
  1287. **
  1288. ** If `*pOut` is not NULL, the caller is responsible for passing it to
  1289. ** sqlite3_free() to free it.
  1290. */
  1291. SQLITE_WASM_EXPORT
  1292. int sqlite3__wasm_db_serialize( sqlite3 *pDb, const char *zSchema,
  1293. unsigned char **pOut,
  1294. sqlite3_int64 *nOut, unsigned int mFlags ){
  1295. unsigned char * z;
  1296. if( !pDb || !pOut ) return SQLITE_MISUSE;
  1297. if( nOut ) *nOut = 0;
  1298. z = sqlite3_serialize(pDb, zSchema ? zSchema : "main", nOut, mFlags);
  1299. if( z || (SQLITE_SERIALIZE_NOCOPY & mFlags) ){
  1300. *pOut = z;
  1301. return 0;
  1302. }else{
  1303. return SQLITE_NOMEM;
  1304. }
  1305. }
  1306. /*
  1307. ** This function is NOT part of the sqlite3 public API. It is strictly
  1308. ** for use by the sqlite project's own JS/WASM bindings.
  1309. **
  1310. ** ACHTUNG: it was discovered on 2023-08-11 that, with SQLITE_DEBUG,
  1311. ** this function's out-of-scope use of the sqlite3_vfs/file/io_methods
  1312. ** APIs leads to triggering of assertions in the core library. Its use
  1313. ** is now deprecated and VFS-specific APIs for importing files need to
  1314. ** be found to replace it. sqlite3__wasm_posix_create_file() is
  1315. ** suitable for the "unix" family of VFSes.
  1316. **
  1317. ** Creates a new file using the I/O API of the given VFS, containing
  1318. ** the given number of bytes of the given data. If the file exists, it
  1319. ** is truncated to the given length and populated with the given
  1320. ** data.
  1321. **
  1322. ** This function exists so that we can implement the equivalent of
  1323. ** Emscripten's FS.createDataFile() in a VFS-agnostic way. This
  1324. ** functionality is intended for use in uploading database files.
  1325. **
  1326. ** Not all VFSes support this functionality, e.g. the "kvvfs" does
  1327. ** not.
  1328. **
  1329. ** If pVfs is NULL, sqlite3_vfs_find(0) is used.
  1330. **
  1331. ** If zFile is NULL, pVfs is NULL (and sqlite3_vfs_find(0) returns
  1332. ** NULL), or nData is negative, SQLITE_MISUSE are returned.
  1333. **
  1334. ** On success, it creates a new file with the given name, populated
  1335. ** with the fist nData bytes of pData. If pData is NULL, the file is
  1336. ** created and/or truncated to nData bytes.
  1337. **
  1338. ** Whether or not directory components of zFilename are created
  1339. ** automatically or not is unspecified: that detail is left to the
  1340. ** VFS. The "opfs" VFS, for example, creates them.
  1341. **
  1342. ** If an error happens while populating or truncating the file, the
  1343. ** target file will be deleted (if needed) if this function created
  1344. ** it. If this function did not create it, it is not deleted but may
  1345. ** be left in an undefined state.
  1346. **
  1347. ** Returns 0 on success. On error, it returns a code described above
  1348. ** or propagates a code from one of the I/O methods.
  1349. **
  1350. ** Design note: nData is an integer, instead of int64, for WASM
  1351. ** portability, so that the API can still work in builds where BigInt
  1352. ** support is disabled or unavailable.
  1353. */
  1354. SQLITE_WASM_EXPORT
  1355. int sqlite3__wasm_vfs_create_file( sqlite3_vfs *pVfs,
  1356. const char *zFilename,
  1357. const unsigned char * pData,
  1358. int nData ){
  1359. int rc;
  1360. sqlite3_file *pFile = 0;
  1361. sqlite3_io_methods const *pIo;
  1362. const int openFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
  1363. #if 0 && defined(SQLITE_DEBUG)
  1364. | SQLITE_OPEN_MAIN_JOURNAL
  1365. /* ^^^^ This is for testing a horrible workaround to avoid
  1366. triggering a specific assert() in os_unix.c:unixOpen(). Please
  1367. do not enable this in real builds. */
  1368. #endif
  1369. ;
  1370. int flagsOut = 0;
  1371. int fileExisted = 0;
  1372. int doUnlock = 0;
  1373. const unsigned char *pPos = pData;
  1374. const int blockSize = 512
  1375. /* Because we are using pFile->pMethods->xWrite() for writing, and
  1376. ** it may have a buffer limit related to sqlite3's pager size, we
  1377. ** conservatively write in 512-byte blocks (smallest page
  1378. ** size). */;
  1379. //fprintf(stderr, "pVfs=%p, zFilename=%s, nData=%d\n", pVfs, zFilename, nData);
  1380. if( !pVfs ) pVfs = sqlite3_vfs_find(0);
  1381. if( !pVfs || !zFilename || nData<0 ) return SQLITE_MISUSE;
  1382. pVfs->xAccess(pVfs, zFilename, SQLITE_ACCESS_EXISTS, &fileExisted);
  1383. rc = sqlite3OsOpenMalloc(pVfs, zFilename, &pFile, openFlags, &flagsOut);
  1384. #if 0
  1385. # define RC fprintf(stderr,"create_file(%s,%s) @%d rc=%d\n", \
  1386. pVfs->zName, zFilename, __LINE__, rc);
  1387. #else
  1388. # define RC
  1389. #endif
  1390. RC;
  1391. if(rc) return rc;
  1392. pIo = pFile->pMethods;
  1393. if( pIo->xLock ) {
  1394. /* We need xLock() in order to accommodate the OPFS VFS, as it
  1395. ** obtains a writeable handle via the lock operation and releases
  1396. ** it in xUnlock(). If we don't do those here, we have to add code
  1397. ** to the VFS to account check whether it was locked before
  1398. ** xFileSize(), xTruncate(), and the like, and release the lock
  1399. ** only if it was unlocked when the op was started. */
  1400. rc = pIo->xLock(pFile, SQLITE_LOCK_EXCLUSIVE);
  1401. RC;
  1402. doUnlock = 0==rc;
  1403. }
  1404. if( 0==rc ){
  1405. rc = pIo->xTruncate(pFile, nData);
  1406. RC;
  1407. }
  1408. if( 0==rc && 0!=pData && nData>0 ){
  1409. while( 0==rc && nData>0 ){
  1410. const int n = nData>=blockSize ? blockSize : nData;
  1411. rc = pIo->xWrite(pFile, pPos, n, (sqlite3_int64)(pPos - pData));
  1412. RC;
  1413. nData -= n;
  1414. pPos += n;
  1415. }
  1416. if( 0==rc && nData>0 ){
  1417. assert( nData<blockSize );
  1418. rc = pIo->xWrite(pFile, pPos, nData,
  1419. (sqlite3_int64)(pPos - pData));
  1420. RC;
  1421. }
  1422. }
  1423. if( pIo->xUnlock && doUnlock!=0 ){
  1424. pIo->xUnlock(pFile, SQLITE_LOCK_NONE);
  1425. }
  1426. pIo->xClose(pFile);
  1427. if( rc!=0 && 0==fileExisted ){
  1428. pVfs->xDelete(pVfs, zFilename, 1);
  1429. }
  1430. RC;
  1431. #undef RC
  1432. return rc;
  1433. }
  1434. /**
  1435. ** This function is NOT part of the sqlite3 public API. It is strictly
  1436. ** for use by the sqlite project's own JS/WASM bindings.
  1437. **
  1438. ** Creates or overwrites a file using the POSIX file API,
  1439. ** i.e. Emscripten's virtual filesystem. Creates or truncates
  1440. ** zFilename, appends pData bytes to it, and returns 0 on success or
  1441. ** SQLITE_IOERR on error.
  1442. */
  1443. SQLITE_WASM_EXPORT
  1444. int sqlite3__wasm_posix_create_file( const char *zFilename,
  1445. const unsigned char * pData,
  1446. int nData ){
  1447. int rc;
  1448. FILE * pFile = 0;
  1449. int fileExisted = 0;
  1450. size_t nWrote = 1;
  1451. if( !zFilename || nData<0 || (pData==0 && nData>0) ) return SQLITE_MISUSE;
  1452. pFile = fopen(zFilename, "w");
  1453. if( 0==pFile ) return SQLITE_IOERR;
  1454. if( nData>0 ){
  1455. nWrote = fwrite(pData, (size_t)nData, 1, pFile);
  1456. }
  1457. fclose(pFile);
  1458. return 1==nWrote ? 0 : SQLITE_IOERR;
  1459. }
  1460. /*
  1461. ** This function is NOT part of the sqlite3 public API. It is strictly
  1462. ** for use by the sqlite project's own JS/WASM bindings.
  1463. **
  1464. ** Allocates sqlite3KvvfsMethods.nKeySize bytes from
  1465. ** sqlite3__wasm_pstack_alloc() and returns 0 if that allocation fails,
  1466. ** else it passes that string to kvstorageMakeKey() and returns a
  1467. ** NUL-terminated pointer to that string. It is up to the caller to
  1468. ** use sqlite3__wasm_pstack_restore() to free the returned pointer.
  1469. */
  1470. SQLITE_WASM_EXPORT
  1471. char * sqlite3__wasm_kvvfsMakeKeyOnPstack(const char *zClass,
  1472. const char *zKeyIn){
  1473. assert(sqlite3KvvfsMethods.nKeySize>24);
  1474. char *zKeyOut =
  1475. (char *)sqlite3__wasm_pstack_alloc(sqlite3KvvfsMethods.nKeySize);
  1476. if(zKeyOut){
  1477. kvstorageMakeKey(zClass, zKeyIn, zKeyOut);
  1478. }
  1479. return zKeyOut;
  1480. }
  1481. /*
  1482. ** This function is NOT part of the sqlite3 public API. It is strictly
  1483. ** for use by the sqlite project's own JS/WASM bindings.
  1484. **
  1485. ** Returns the pointer to the singleton object which holds the kvvfs
  1486. ** I/O methods and associated state.
  1487. */
  1488. SQLITE_WASM_EXPORT
  1489. sqlite3_kvvfs_methods * sqlite3__wasm_kvvfs_methods(void){
  1490. return &sqlite3KvvfsMethods;
  1491. }
  1492. #if SQLITE_WASM_HAS_VTAB
  1493. /*
  1494. ** This function is NOT part of the sqlite3 public API. It is strictly
  1495. ** for use by the sqlite project's own JS/WASM bindings.
  1496. **
  1497. ** This is a proxy for the variadic sqlite3_vtab_config() which passes
  1498. ** its argument on, or not, to sqlite3_vtab_config(), depending on the
  1499. ** value of its 2nd argument. Returns the result of
  1500. ** sqlite3_vtab_config(), or SQLITE_MISUSE if the 2nd arg is not a
  1501. ** valid value.
  1502. */
  1503. SQLITE_WASM_EXPORT
  1504. int sqlite3__wasm_vtab_config(sqlite3 *pDb, int op, int arg){
  1505. switch(op){
  1506. case SQLITE_VTAB_DIRECTONLY:
  1507. case SQLITE_VTAB_INNOCUOUS:
  1508. return sqlite3_vtab_config(pDb, op);
  1509. case SQLITE_VTAB_CONSTRAINT_SUPPORT:
  1510. return sqlite3_vtab_config(pDb, op, arg);
  1511. default:
  1512. return SQLITE_MISUSE;
  1513. }
  1514. }
  1515. #endif /*SQLITE_WASM_HAS_VTAB*/
  1516. /*
  1517. ** This function is NOT part of the sqlite3 public API. It is strictly
  1518. ** for use by the sqlite project's own JS/WASM bindings.
  1519. **
  1520. ** Wrapper for the variants of sqlite3_db_config() which take
  1521. ** (int,int*) variadic args.
  1522. */
  1523. SQLITE_WASM_EXPORT
  1524. int sqlite3__wasm_db_config_ip(sqlite3 *pDb, int op, int arg1, int* pArg2){
  1525. switch(op){
  1526. case SQLITE_DBCONFIG_ENABLE_FKEY:
  1527. case SQLITE_DBCONFIG_ENABLE_TRIGGER:
  1528. case SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER:
  1529. case SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION:
  1530. case SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE:
  1531. case SQLITE_DBCONFIG_ENABLE_QPSG:
  1532. case SQLITE_DBCONFIG_TRIGGER_EQP:
  1533. case SQLITE_DBCONFIG_RESET_DATABASE:
  1534. case SQLITE_DBCONFIG_DEFENSIVE:
  1535. case SQLITE_DBCONFIG_WRITABLE_SCHEMA:
  1536. case SQLITE_DBCONFIG_LEGACY_ALTER_TABLE:
  1537. case SQLITE_DBCONFIG_DQS_DML:
  1538. case SQLITE_DBCONFIG_DQS_DDL:
  1539. case SQLITE_DBCONFIG_ENABLE_VIEW:
  1540. case SQLITE_DBCONFIG_LEGACY_FILE_FORMAT:
  1541. case SQLITE_DBCONFIG_TRUSTED_SCHEMA:
  1542. case SQLITE_DBCONFIG_STMT_SCANSTATUS:
  1543. case SQLITE_DBCONFIG_REVERSE_SCANORDER:
  1544. case SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE:
  1545. case SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE:
  1546. case SQLITE_DBCONFIG_ENABLE_COMMENTS:
  1547. return sqlite3_db_config(pDb, op, arg1, pArg2);
  1548. default: return SQLITE_MISUSE;
  1549. }
  1550. }
  1551. /*
  1552. ** This function is NOT part of the sqlite3 public API. It is strictly
  1553. ** for use by the sqlite project's own JS/WASM bindings.
  1554. **
  1555. ** Wrapper for the variants of sqlite3_db_config() which take
  1556. ** (void*,int,int) variadic args.
  1557. */
  1558. SQLITE_WASM_EXPORT
  1559. int sqlite3__wasm_db_config_pii(sqlite3 *pDb, int op, void * pArg1, int arg2, int arg3){
  1560. switch(op){
  1561. case SQLITE_DBCONFIG_LOOKASIDE:
  1562. return sqlite3_db_config(pDb, op, pArg1, arg2, arg3);
  1563. default: return SQLITE_MISUSE;
  1564. }
  1565. }
  1566. /*
  1567. ** This function is NOT part of the sqlite3 public API. It is strictly
  1568. ** for use by the sqlite project's own JS/WASM bindings.
  1569. **
  1570. ** Wrapper for the variants of sqlite3_db_config() which take
  1571. ** (const char *) variadic args.
  1572. */
  1573. SQLITE_WASM_EXPORT
  1574. int sqlite3__wasm_db_config_s(sqlite3 *pDb, int op, const char *zArg){
  1575. switch(op){
  1576. case SQLITE_DBCONFIG_MAINDBNAME:
  1577. return sqlite3_db_config(pDb, op, zArg);
  1578. default: return SQLITE_MISUSE;
  1579. }
  1580. }
  1581. /*
  1582. ** This function is NOT part of the sqlite3 public API. It is strictly
  1583. ** for use by the sqlite project's own JS/WASM bindings.
  1584. **
  1585. ** Binding for combinations of sqlite3_config() arguments which take
  1586. ** a single integer argument.
  1587. */
  1588. SQLITE_WASM_EXPORT
  1589. int sqlite3__wasm_config_i(int op, int arg){
  1590. return sqlite3_config(op, arg);
  1591. }
  1592. /*
  1593. ** This function is NOT part of the sqlite3 public API. It is strictly
  1594. ** for use by the sqlite project's own JS/WASM bindings.
  1595. **
  1596. ** Binding for combinations of sqlite3_config() arguments which take
  1597. ** two int arguments.
  1598. */
  1599. SQLITE_WASM_EXPORT
  1600. int sqlite3__wasm_config_ii(int op, int arg1, int arg2){
  1601. return sqlite3_config(op, arg1, arg2);
  1602. }
  1603. /*
  1604. ** This function is NOT part of the sqlite3 public API. It is strictly
  1605. ** for use by the sqlite project's own JS/WASM bindings.
  1606. **
  1607. ** Binding for combinations of sqlite3_config() arguments which take
  1608. ** a single i64 argument.
  1609. */
  1610. SQLITE_WASM_EXPORT
  1611. int sqlite3__wasm_config_j(int op, sqlite3_int64 arg){
  1612. return sqlite3_config(op, arg);
  1613. }
  1614. /*
  1615. ** This function is NOT part of the sqlite3 public API. It is strictly
  1616. ** for use by the sqlite project's own JS/WASM bindings.
  1617. **
  1618. ** If z is not NULL, returns the result of passing z to
  1619. ** sqlite3_mprintf()'s %Q modifier (if addQuotes is true) or %q (if
  1620. ** addQuotes is 0). Returns NULL if z is NULL or on OOM.
  1621. */
  1622. SQLITE_WASM_EXPORT
  1623. char * sqlite3__wasm_qfmt_token(char *z, int addQuotes){
  1624. char * rc = 0;
  1625. if( z ){
  1626. rc = addQuotes
  1627. ? sqlite3_mprintf("%Q", z)
  1628. : sqlite3_mprintf("%q", z);
  1629. }
  1630. return rc;
  1631. }
  1632. #if defined(__EMSCRIPTEN__) && defined(SQLITE_ENABLE_WASMFS)
  1633. #include <emscripten/console.h>
  1634. #include <emscripten/wasmfs.h>
  1635. /*
  1636. ** This function is NOT part of the sqlite3 public API. It is strictly
  1637. ** for use by the sqlite project's own JS/WASM bindings, specifically
  1638. ** only when building with Emscripten's WASMFS support.
  1639. **
  1640. ** This function should only be called if the JS side detects the
  1641. ** existence of the Origin-Private FileSystem (OPFS) APIs in the
  1642. ** client. The first time it is called, this function instantiates a
  1643. ** WASMFS backend impl for OPFS. On success, subsequent calls are
  1644. ** no-ops.
  1645. **
  1646. ** This function may be passed a "mount point" name, which must have a
  1647. ** leading "/" and is currently restricted to a single path component,
  1648. ** e.g. "/foo" is legal but "/foo/" and "/foo/bar" are not. If it is
  1649. ** NULL or empty, it defaults to "/opfs".
  1650. **
  1651. ** Returns 0 on success, SQLITE_NOMEM if instantiation of the backend
  1652. ** object fails, SQLITE_IOERR if mkdir() of the zMountPoint dir in
  1653. ** the virtual FS fails. In builds compiled without SQLITE_ENABLE_WASMFS
  1654. ** defined, SQLITE_NOTFOUND is returned without side effects.
  1655. */
  1656. SQLITE_WASM_EXPORT
  1657. int sqlite3__wasm_init_wasmfs(const char *zMountPoint){
  1658. static backend_t pOpfs = 0;
  1659. if( !zMountPoint || !*zMountPoint ) zMountPoint = "/opfs";
  1660. if( !pOpfs ){
  1661. pOpfs = wasmfs_create_opfs_backend();
  1662. }
  1663. /** It's not enough to instantiate the backend. We have to create a
  1664. mountpoint in the VFS and attach the backend to it. */
  1665. if( pOpfs && 0!=access(zMountPoint, F_OK) ){
  1666. /* Note that this check and is not robust but it will
  1667. hypothetically suffice for the transient wasm-based virtual
  1668. filesystem we're currently running in. */
  1669. const int rc = wasmfs_create_directory(zMountPoint, 0777, pOpfs);
  1670. /*emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc);*/
  1671. if(rc) return SQLITE_IOERR;
  1672. }
  1673. return pOpfs ? 0 : SQLITE_NOMEM;
  1674. }
  1675. #else
  1676. SQLITE_WASM_EXPORT
  1677. int sqlite3__wasm_init_wasmfs(const char *zUnused){
  1678. //emscripten_console_warn("WASMFS OPFS is not compiled in.");
  1679. if(zUnused){/*unused*/}
  1680. return SQLITE_NOTFOUND;
  1681. }
  1682. #endif /* __EMSCRIPTEN__ && SQLITE_ENABLE_WASMFS */
  1683. #if SQLITE_WASM_ENABLE_C_TESTS
  1684. SQLITE_WASM_EXPORT
  1685. int sqlite3__wasm_test_intptr(int * p){
  1686. return *p = *p * 2;
  1687. }
  1688. SQLITE_WASM_EXPORT
  1689. void * sqlite3__wasm_test_voidptr(void * p){
  1690. return p;
  1691. }
  1692. SQLITE_WASM_EXPORT
  1693. int64_t sqlite3__wasm_test_int64_max(void){
  1694. return (int64_t)0x7fffffffffffffff;
  1695. }
  1696. SQLITE_WASM_EXPORT
  1697. int64_t sqlite3__wasm_test_int64_min(void){
  1698. return ~sqlite3__wasm_test_int64_max();
  1699. }
  1700. SQLITE_WASM_EXPORT
  1701. int64_t sqlite3__wasm_test_int64_times2(int64_t x){
  1702. return x * 2;
  1703. }
  1704. SQLITE_WASM_EXPORT
  1705. void sqlite3__wasm_test_int64_minmax(int64_t * min, int64_t *max){
  1706. *max = sqlite3__wasm_test_int64_max();
  1707. *min = sqlite3__wasm_test_int64_min();
  1708. /*printf("minmax: min=%lld, max=%lld\n", *min, *max);*/
  1709. }
  1710. SQLITE_WASM_EXPORT
  1711. int64_t sqlite3__wasm_test_int64ptr(int64_t * p){
  1712. /*printf("sqlite3__wasm_test_int64ptr( @%lld = 0x%llx )\n", (int64_t)p, *p);*/
  1713. return *p = *p * 2;
  1714. }
  1715. SQLITE_WASM_EXPORT
  1716. void sqlite3__wasm_test_stack_overflow(int recurse){
  1717. if(recurse) sqlite3__wasm_test_stack_overflow(recurse);
  1718. }
  1719. /* For testing the 'string:dealloc' whwasmutil.xWrap() conversion. */
  1720. SQLITE_WASM_EXPORT
  1721. char * sqlite3__wasm_test_str_hello(int fail){
  1722. char * s = fail ? 0 : (char *)sqlite3_malloc(6);
  1723. if(s){
  1724. memcpy(s, "hello", 5);
  1725. s[5] = 0;
  1726. }
  1727. return s;
  1728. }
  1729. /*
  1730. ** For testing using SQLTester scripts.
  1731. **
  1732. ** Return non-zero if string z matches glob pattern zGlob and zero if the
  1733. ** pattern does not match.
  1734. **
  1735. ** To repeat:
  1736. **
  1737. ** zero == no match
  1738. ** non-zero == match
  1739. **
  1740. ** Globbing rules:
  1741. **
  1742. ** '*' Matches any sequence of zero or more characters.
  1743. **
  1744. ** '?' Matches exactly one character.
  1745. **
  1746. ** [...] Matches one character from the enclosed list of
  1747. ** characters.
  1748. **
  1749. ** [^...] Matches one character not in the enclosed list.
  1750. **
  1751. ** '#' Matches any sequence of one or more digits with an
  1752. ** optional + or - sign in front, or a hexadecimal
  1753. ** literal of the form 0x...
  1754. */
  1755. static int sqlite3__wasm_SQLTester_strnotglob(const char *zGlob, const char *z){
  1756. int c, c2;
  1757. int invert;
  1758. int seen;
  1759. typedef int (*recurse_f)(const char *,const char *);
  1760. static const recurse_f recurse = sqlite3__wasm_SQLTester_strnotglob;
  1761. while( (c = (*(zGlob++)))!=0 ){
  1762. if( c=='*' ){
  1763. while( (c=(*(zGlob++))) == '*' || c=='?' ){
  1764. if( c=='?' && (*(z++))==0 ) return 0;
  1765. }
  1766. if( c==0 ){
  1767. return 1;
  1768. }else if( c=='[' ){
  1769. while( *z && recurse(zGlob-1,z)==0 ){
  1770. z++;
  1771. }
  1772. return (*z)!=0;
  1773. }
  1774. while( (c2 = (*(z++)))!=0 ){
  1775. while( c2!=c ){
  1776. c2 = *(z++);
  1777. if( c2==0 ) return 0;
  1778. }
  1779. if( recurse(zGlob,z) ) return 1;
  1780. }
  1781. return 0;
  1782. }else if( c=='?' ){
  1783. if( (*(z++))==0 ) return 0;
  1784. }else if( c=='[' ){
  1785. int prior_c = 0;
  1786. seen = 0;
  1787. invert = 0;
  1788. c = *(z++);
  1789. if( c==0 ) return 0;
  1790. c2 = *(zGlob++);
  1791. if( c2=='^' ){
  1792. invert = 1;
  1793. c2 = *(zGlob++);
  1794. }
  1795. if( c2==']' ){
  1796. if( c==']' ) seen = 1;
  1797. c2 = *(zGlob++);
  1798. }
  1799. while( c2 && c2!=']' ){
  1800. if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
  1801. c2 = *(zGlob++);
  1802. if( c>=prior_c && c<=c2 ) seen = 1;
  1803. prior_c = 0;
  1804. }else{
  1805. if( c==c2 ){
  1806. seen = 1;
  1807. }
  1808. prior_c = c2;
  1809. }
  1810. c2 = *(zGlob++);
  1811. }
  1812. if( c2==0 || (seen ^ invert)==0 ) return 0;
  1813. }else if( c=='#' ){
  1814. if( z[0]=='0'
  1815. && (z[1]=='x' || z[1]=='X')
  1816. && sqlite3Isxdigit(z[2])
  1817. ){
  1818. z += 3;
  1819. while( sqlite3Isxdigit(z[0]) ){ z++; }
  1820. }else{
  1821. if( (z[0]=='-' || z[0]=='+') && sqlite3Isdigit(z[1]) ) z++;
  1822. if( !sqlite3Isdigit(z[0]) ) return 0;
  1823. z++;
  1824. while( sqlite3Isdigit(z[0]) ){ z++; }
  1825. }
  1826. }else{
  1827. if( c!=(*(z++)) ) return 0;
  1828. }
  1829. }
  1830. return *z==0;
  1831. }
  1832. SQLITE_WASM_EXPORT
  1833. int sqlite3__wasm_SQLTester_strglob(const char *zGlob, const char *z){
  1834. return !sqlite3__wasm_SQLTester_strnotglob(zGlob, z);
  1835. }
  1836. #endif /* SQLITE_WASM_ENABLE_C_TESTS */
  1837. #undef SQLITE_WASM_EXPORT
  1838. #undef SQLITE_WASM_HAS_VTAB
  1839. #undef SQLITE_WASM_BARE_BONES
  1840. #undef SQLITE_WASM_ENABLE_C_TESTS