sqlite3-wasm.c 64 KB

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