1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
/*
** This file requires access to sqlite3.c static state in order to
** implement certain WASM-specific features, and thus directly
** includes that file. Unlike the rest of sqlite3.c, this file
** requires compiling with -std=c99 (or equivalent, or a later C
** version) because it makes use of features not available in C89.
**
** At it's simplest, to build sqlite3.wasm either place this file
** in the same directory as sqlite3.c/h before compilation or use the
** -I/path flag to tell the compiler where to find both of those
** files, then compile this file. For example:
**
** emcc -o sqlite3.wasm ... -I/path/to/sqlite3-c-and-h sqlite3-wasm.c
*/
/*
** Threading and file locking: JS is single-threaded. Each Worker
** thread is a separate instance of the JS engine so can never access
** the same db handle as another thread, thus multi-threading support
** is unnecessary in the library. Because the filesystems are virtual
** and local to a given wasm runtime instance, two Workers can never
** access the same db file at once, with the exception of OPFS. As of
** this writing (2022-09-30), OPFS exclusively locks a file when
** opening it, so two Workers can never open the same OPFS-backed file
** at once. That situation will change if and when lower-level locking
** features are added to OPFS (as is currently planned, per folks
** involved with its development).
**
** Summary: except for the case of future OPFS, which supports
** locking, and any similar future filesystems, threading and file
** locking support are unnecessary in the wasm build.
*/
#undef SQLITE_OMIT_DESERIALIZE
#ifndef SQLITE_DEFAULT_UNIX_VFS
# define SQLITE_DEFAULT_UNIX_VFS "unix-none"
#endif
#ifndef SQLITE_OMIT_DEPRECATED
# define SQLITE_OMIT_DEPRECATED
#endif
#ifndef SQLITE_OMIT_LOAD_EXTENSION
# define SQLITE_OMIT_LOAD_EXTENSION
#endif
#ifndef SQLITE_OMIT_SHARED_CACHE
# define SQLITE_OMIT_SHARED_CACHE
#endif
#ifndef SQLITE_OMIT_UTF16
# define SQLITE_OMIT_UTF16
#endif
#ifndef SQLITE_OS_KV_OPTIONAL
# define SQLITE_OS_KV_OPTIONAL 1
#endif
#ifndef SQLITE_TEMP_STORE
# define SQLITE_TEMP_STORE 3
#endif
#ifndef SQLITE_THREADSAFE
# define SQLITE_THREADSAFE 0
#endif
#include "sqlite3.c" /* yes, .c instead of .h. */
/*
** WASM_KEEP is identical to EMSCRIPTEN_KEEPALIVE but is not
** Emscripten-specific. It explicitly marks functions for export into
** the target wasm file without requiring explicit listing of those
** functions in Emscripten's -sEXPORTED_FUNCTIONS=... list (or
** equivalent in other build platforms). Any function with neither
** this attribute nor which is listed as an explicit export will not
** be exported from the wasm file (but may still be used internally
** within the wasm file).
**
** The functions in this file (sqlite3-wasm.c) which require exporting
** are marked with this flag. They may also be added to any explicit
** build-time export list but need not be. All of these APIs are
** intended for use only within the project's own JS/WASM code, and
** not by client code, so an argument can be made for reducing their
** visibility by not including them in any build-time export lists.
**
** 2022-09-11: it's not yet _proven_ that this approach works in
** non-Emscripten builds. If not, such builds will need to export
** those using the --export=... wasm-ld flag (or equivalent). As of
** this writing we are tied to Emscripten for various reasons
** and cannot test the library with other build environments.
*/
#define WASM_KEEP __attribute__((used,visibility("default")))
// See also:
//__attribute__((export_name("theExportedName"), used, visibility("default")))
/*
** This function is NOT part of the sqlite3 public API. It is strictly
** for use by the sqlite project's own JS/WASM bindings.
**
** For purposes of certain hand-crafted C/Wasm function bindings, we
** need a way of reporting errors which is consistent with the rest of
** the C API, as opposed to throwing JS exceptions. To that end, this
** internal-use-only function is a thin proxy around
** sqlite3ErrorWithMessage(). The intent is that it only be used from
** Wasm bindings such as sqlite3_prepare_v2/v3(), and definitely not
** from client code.
**
** Returns err_code.
*/
WASM_KEEP
int sqlite3_wasm_db_error(sqlite3*db, int err_code, const char *zMsg){
if(0!=zMsg){
const int nMsg = sqlite3Strlen30(zMsg);
sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg);
}else{
sqlite3ErrorWithMsg(db, err_code, NULL);
}
return err_code;
}
/*
** This function is NOT part of the sqlite3 public API. It is strictly
** for use by the sqlite project's own JS/WASM bindings. Unlike the
** rest of the sqlite3 API, this part requires C99 for snprintf() and
** variadic macros.
**
** Returns a string containing a JSON-format "enum" of C-level
** constants intended to be imported into the JS environment. The JSON
** is initialized the first time this function is called and that
** result is reused for all future calls.
**
** If this function returns NULL then it means that the internal
** buffer is not large enough for the generated JSON. In debug builds
** that will trigger an assert().
*/
WASM_KEEP
const char * sqlite3_wasm_enum_json(void){
static char strBuf[1024 * 12] = {0} /* where the JSON goes */;
int n = 0, childCount = 0, structCount = 0
/* output counters for figuring out where commas go */;
char * pos = &strBuf[1] /* skip first byte for now to help protect
** against a small race condition */;
char const * const zEnd = pos + sizeof(strBuf) /* one-past-the-end */;
if(strBuf[0]) return strBuf;
/* Leave strBuf[0] at 0 until the end to help guard against a tiny
** race condition. If this is called twice concurrently, they might
** end up both writing to strBuf, but they'll both write the same
** thing, so that's okay. If we set byte 0 up front then the 2nd
** instance might return and use the string before the 1st instance
** is done filling it. */
/* Core output macros... */
#define lenCheck assert(pos < zEnd - 128 \
&& "sqlite3_wasm_enum_json() buffer is too small."); \
if(pos >= zEnd - 128) return 0
#define outf(format,...) \
pos += snprintf(pos, ((size_t)(zEnd - pos)), format, __VA_ARGS__); \
lenCheck
#define out(TXT) outf("%s",TXT)
#define CloseBrace(LEVEL) \
assert(LEVEL<5); memset(pos, '}', LEVEL); pos+=LEVEL; lenCheck
/* Macros for emitting maps of integer- and string-type macros to
** their values. */
#define DefGroup(KEY) n = 0; \
outf("%s\"" #KEY "\": {",(childCount++ ? "," : ""));
#define DefInt(KEY) \
outf("%s\"%s\": %d", (n++ ? ", " : ""), #KEY, (int)KEY)
#define DefStr(KEY) \
outf("%s\"%s\": \"%s\"", (n++ ? ", " : ""), #KEY, KEY)
#define _DefGroup CloseBrace(1)
/* The following groups are sorted alphabetic by group name. */
DefGroup(access){
DefInt(SQLITE_ACCESS_EXISTS);
DefInt(SQLITE_ACCESS_READWRITE);
DefInt(SQLITE_ACCESS_READ)/*docs say this is unused*/;
} _DefGroup;
DefGroup(blobFinalizers) {
/* SQLITE_STATIC/TRANSIENT need to be handled explicitly as
** integers to avoid casting-related warnings. */
out("\"SQLITE_STATIC\":0, \"SQLITE_TRANSIENT\":-1");
} _DefGroup;
DefGroup(dataTypes) {
DefInt(SQLITE_INTEGER);
DefInt(SQLITE_FLOAT);
DefInt(SQLITE_TEXT);
DefInt(SQLITE_BLOB);
DefInt(SQLITE_NULL);
} _DefGroup;
DefGroup(encodings) {
/* Noting that the wasm binding only aims to support UTF-8. */
DefInt(SQLITE_UTF8);
DefInt(SQLITE_UTF16LE);
DefInt(SQLITE_UTF16BE);
DefInt(SQLITE_UTF16);
/*deprecated DefInt(SQLITE_ANY); */
DefInt(SQLITE_UTF16_ALIGNED);
} _DefGroup;
DefGroup(fcntl) {
DefInt(SQLITE_FCNTL_LOCKSTATE);
DefInt(SQLITE_FCNTL_GET_LOCKPROXYFILE);
DefInt(SQLITE_FCNTL_SET_LOCKPROXYFILE);
DefInt(SQLITE_FCNTL_LAST_ERRNO);
DefInt(SQLITE_FCNTL_SIZE_HINT);
DefInt(SQLITE_FCNTL_CHUNK_SIZE);
DefInt(SQLITE_FCNTL_FILE_POINTER);
DefInt(SQLITE_FCNTL_SYNC_OMITTED);
DefInt(SQLITE_FCNTL_WIN32_AV_RETRY);
DefInt(SQLITE_FCNTL_PERSIST_WAL);
DefInt(SQLITE_FCNTL_OVERWRITE);
DefInt(SQLITE_FCNTL_VFSNAME);
DefInt(SQLITE_FCNTL_POWERSAFE_OVERWRITE);
DefInt(SQLITE_FCNTL_PRAGMA);
DefInt(SQLITE_FCNTL_BUSYHANDLER);
DefInt(SQLITE_FCNTL_TEMPFILENAME);
DefInt(SQLITE_FCNTL_MMAP_SIZE);
DefInt(SQLITE_FCNTL_TRACE);
DefInt(SQLITE_FCNTL_HAS_MOVED);
DefInt(SQLITE_FCNTL_SYNC);
DefInt(SQLITE_FCNTL_COMMIT_PHASETWO);
DefInt(SQLITE_FCNTL_WIN32_SET_HANDLE);
DefInt(SQLITE_FCNTL_WAL_BLOCK);
DefInt(SQLITE_FCNTL_ZIPVFS);
DefInt(SQLITE_FCNTL_RBU);
DefInt(SQLITE_FCNTL_VFS_POINTER);
DefInt(SQLITE_FCNTL_JOURNAL_POINTER);
DefInt(SQLITE_FCNTL_WIN32_GET_HANDLE);
DefInt(SQLITE_FCNTL_PDB);
DefInt(SQLITE_FCNTL_BEGIN_ATOMIC_WRITE);
DefInt(SQLITE_FCNTL_COMMIT_ATOMIC_WRITE);
DefInt(SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE);
DefInt(SQLITE_FCNTL_LOCK_TIMEOUT);
DefInt(SQLITE_FCNTL_DATA_VERSION);
DefInt(SQLITE_FCNTL_SIZE_LIMIT);
DefInt(SQLITE_FCNTL_CKPT_DONE);
DefInt(SQLITE_FCNTL_RESERVE_BYTES);
DefInt(SQLITE_FCNTL_CKPT_START);
DefInt(SQLITE_FCNTL_EXTERNAL_READER);
DefInt(SQLITE_FCNTL_CKSM_FILE);
} _DefGroup;
DefGroup(flock) {
DefInt(SQLITE_LOCK_NONE);
DefInt(SQLITE_LOCK_SHARED);
DefInt(SQLITE_LOCK_RESERVED);
DefInt(SQLITE_LOCK_PENDING);
DefInt(SQLITE_LOCK_EXCLUSIVE);
} _DefGroup;
DefGroup(ioCap) {
DefInt(SQLITE_IOCAP_ATOMIC);
DefInt(SQLITE_IOCAP_ATOMIC512);
DefInt(SQLITE_IOCAP_ATOMIC1K);
DefInt(SQLITE_IOCAP_ATOMIC2K);
DefInt(SQLITE_IOCAP_ATOMIC4K);
DefInt(SQLITE_IOCAP_ATOMIC8K);
DefInt(SQLITE_IOCAP_ATOMIC16K);
DefInt(SQLITE_IOCAP_ATOMIC32K);
DefInt(SQLITE_IOCAP_ATOMIC64K);
DefInt(SQLITE_IOCAP_SAFE_APPEND);
DefInt(SQLITE_IOCAP_SEQUENTIAL);
DefInt(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN);
DefInt(SQLITE_IOCAP_POWERSAFE_OVERWRITE);
DefInt(SQLITE_IOCAP_IMMUTABLE);
DefInt(SQLITE_IOCAP_BATCH_ATOMIC);
} _DefGroup;
DefGroup(openFlags) {
/* Noting that not all of these will have any effect in
** WASM-space. */
DefInt(SQLITE_OPEN_READONLY);
DefInt(SQLITE_OPEN_READWRITE);
DefInt(SQLITE_OPEN_CREATE);
DefInt(SQLITE_OPEN_URI);
DefInt(SQLITE_OPEN_MEMORY);
DefInt(SQLITE_OPEN_NOMUTEX);
DefInt(SQLITE_OPEN_FULLMUTEX);
DefInt(SQLITE_OPEN_SHAREDCACHE);
DefInt(SQLITE_OPEN_PRIVATECACHE);
DefInt(SQLITE_OPEN_EXRESCODE);
DefInt(SQLITE_OPEN_NOFOLLOW);
/* OPEN flags for use with VFSes... */
DefInt(SQLITE_OPEN_MAIN_DB);
DefInt(SQLITE_OPEN_MAIN_JOURNAL);
DefInt(SQLITE_OPEN_TEMP_DB);
DefInt(SQLITE_OPEN_TEMP_JOURNAL);
DefInt(SQLITE_OPEN_TRANSIENT_DB);
DefInt(SQLITE_OPEN_SUBJOURNAL);
DefInt(SQLITE_OPEN_SUPER_JOURNAL);
DefInt(SQLITE_OPEN_WAL);
DefInt(SQLITE_OPEN_DELETEONCLOSE);
DefInt(SQLITE_OPEN_EXCLUSIVE);
} _DefGroup;
DefGroup(prepareFlags) {
DefInt(SQLITE_PREPARE_PERSISTENT);
DefInt(SQLITE_PREPARE_NORMALIZE);
DefInt(SQLITE_PREPARE_NO_VTAB);
} _DefGroup;
DefGroup(resultCodes) {
DefInt(SQLITE_OK);
DefInt(SQLITE_ERROR);
DefInt(SQLITE_INTERNAL);
DefInt(SQLITE_PERM);
DefInt(SQLITE_ABORT);
DefInt(SQLITE_BUSY);
DefInt(SQLITE_LOCKED);
DefInt(SQLITE_NOMEM);
DefInt(SQLITE_READONLY);
DefInt(SQLITE_INTERRUPT);
DefInt(SQLITE_IOERR);
DefInt(SQLITE_CORRUPT);
DefInt(SQLITE_NOTFOUND);
DefInt(SQLITE_FULL);
DefInt(SQLITE_CANTOPEN);
DefInt(SQLITE_PROTOCOL);
DefInt(SQLITE_EMPTY);
DefInt(SQLITE_SCHEMA);
DefInt(SQLITE_TOOBIG);
DefInt(SQLITE_CONSTRAINT);
DefInt(SQLITE_MISMATCH);
DefInt(SQLITE_MISUSE);
DefInt(SQLITE_NOLFS);
DefInt(SQLITE_AUTH);
DefInt(SQLITE_FORMAT);
DefInt(SQLITE_RANGE);
DefInt(SQLITE_NOTADB);
DefInt(SQLITE_NOTICE);
DefInt(SQLITE_WARNING);
DefInt(SQLITE_ROW);
DefInt(SQLITE_DONE);
// Extended Result Codes
DefInt(SQLITE_ERROR_MISSING_COLLSEQ);
DefInt(SQLITE_ERROR_RETRY);
DefInt(SQLITE_ERROR_SNAPSHOT);
DefInt(SQLITE_IOERR_READ);
DefInt(SQLITE_IOERR_SHORT_READ);
DefInt(SQLITE_IOERR_WRITE);
DefInt(SQLITE_IOERR_FSYNC);
DefInt(SQLITE_IOERR_DIR_FSYNC);
DefInt(SQLITE_IOERR_TRUNCATE);
DefInt(SQLITE_IOERR_FSTAT);
DefInt(SQLITE_IOERR_UNLOCK);
DefInt(SQLITE_IOERR_RDLOCK);
DefInt(SQLITE_IOERR_DELETE);
DefInt(SQLITE_IOERR_BLOCKED);
DefInt(SQLITE_IOERR_NOMEM);
DefInt(SQLITE_IOERR_ACCESS);
DefInt(SQLITE_IOERR_CHECKRESERVEDLOCK);
DefInt(SQLITE_IOERR_LOCK);
DefInt(SQLITE_IOERR_CLOSE);
DefInt(SQLITE_IOERR_DIR_CLOSE);
DefInt(SQLITE_IOERR_SHMOPEN);
DefInt(SQLITE_IOERR_SHMSIZE);
DefInt(SQLITE_IOERR_SHMLOCK);
DefInt(SQLITE_IOERR_SHMMAP);
DefInt(SQLITE_IOERR_SEEK);
DefInt(SQLITE_IOERR_DELETE_NOENT);
DefInt(SQLITE_IOERR_MMAP);
DefInt(SQLITE_IOERR_GETTEMPPATH);
DefInt(SQLITE_IOERR_CONVPATH);
DefInt(SQLITE_IOERR_VNODE);
DefInt(SQLITE_IOERR_AUTH);
DefInt(SQLITE_IOERR_BEGIN_ATOMIC);
DefInt(SQLITE_IOERR_COMMIT_ATOMIC);
DefInt(SQLITE_IOERR_ROLLBACK_ATOMIC);
DefInt(SQLITE_IOERR_DATA);
DefInt(SQLITE_IOERR_CORRUPTFS);
DefInt(SQLITE_LOCKED_SHAREDCACHE);
DefInt(SQLITE_LOCKED_VTAB);
DefInt(SQLITE_BUSY_RECOVERY);
DefInt(SQLITE_BUSY_SNAPSHOT);
DefInt(SQLITE_BUSY_TIMEOUT);
DefInt(SQLITE_CANTOPEN_NOTEMPDIR);
DefInt(SQLITE_CANTOPEN_ISDIR);
DefInt(SQLITE_CANTOPEN_FULLPATH);
DefInt(SQLITE_CANTOPEN_CONVPATH);
//DefInt(SQLITE_CANTOPEN_DIRTYWAL)/*docs say not used*/;
DefInt(SQLITE_CANTOPEN_SYMLINK);
DefInt(SQLITE_CORRUPT_VTAB);
DefInt(SQLITE_CORRUPT_SEQUENCE);
DefInt(SQLITE_CORRUPT_INDEX);
DefInt(SQLITE_READONLY_RECOVERY);
DefInt(SQLITE_READONLY_CANTLOCK);
DefInt(SQLITE_READONLY_ROLLBACK);
DefInt(SQLITE_READONLY_DBMOVED);
DefInt(SQLITE_READONLY_CANTINIT);
DefInt(SQLITE_READONLY_DIRECTORY);
DefInt(SQLITE_ABORT_ROLLBACK);
DefInt(SQLITE_CONSTRAINT_CHECK);
DefInt(SQLITE_CONSTRAINT_COMMITHOOK);
DefInt(SQLITE_CONSTRAINT_FOREIGNKEY);
DefInt(SQLITE_CONSTRAINT_FUNCTION);
DefInt(SQLITE_CONSTRAINT_NOTNULL);
DefInt(SQLITE_CONSTRAINT_PRIMARYKEY);
DefInt(SQLITE_CONSTRAINT_TRIGGER);
DefInt(SQLITE_CONSTRAINT_UNIQUE);
DefInt(SQLITE_CONSTRAINT_VTAB);
DefInt(SQLITE_CONSTRAINT_ROWID);
DefInt(SQLITE_CONSTRAINT_PINNED);
DefInt(SQLITE_CONSTRAINT_DATATYPE);
DefInt(SQLITE_NOTICE_RECOVER_WAL);
DefInt(SQLITE_NOTICE_RECOVER_ROLLBACK);
DefInt(SQLITE_WARNING_AUTOINDEX);
DefInt(SQLITE_AUTH_USER);
DefInt(SQLITE_OK_LOAD_PERMANENTLY);
//DefInt(SQLITE_OK_SYMLINK) /* internal use only */;
} _DefGroup;
DefGroup(serialize){
DefInt(SQLITE_DESERIALIZE_FREEONCLOSE);
DefInt(SQLITE_DESERIALIZE_READONLY);
DefInt(SQLITE_DESERIALIZE_RESIZEABLE);
} _DefGroup;
DefGroup(syncFlags) {
DefInt(SQLITE_SYNC_NORMAL);
DefInt(SQLITE_SYNC_FULL);
DefInt(SQLITE_SYNC_DATAONLY);
} _DefGroup;
DefGroup(udfFlags) {
DefInt(SQLITE_DETERMINISTIC);
DefInt(SQLITE_DIRECTONLY);
DefInt(SQLITE_INNOCUOUS);
} _DefGroup;
DefGroup(version) {
DefInt(SQLITE_VERSION_NUMBER);
DefStr(SQLITE_VERSION);
DefStr(SQLITE_SOURCE_ID);
} _DefGroup;
#undef DefGroup
#undef DefStr
#undef DefInt
#undef _DefGroup
/*
** Emit an array of "StructBinder" struct descripions, which look
** like:
**
** {
** "name": "MyStruct",
** "sizeof": 16,
** "members": {
** "member1": {"offset": 0,"sizeof": 4,"signature": "i"},
** "member2": {"offset": 4,"sizeof": 4,"signature": "p"},
** "member3": {"offset": 8,"sizeof": 8,"signature": "j"}
** }
** }
**
** Detailed documentation for those bits are in the docs for the
** Jaccwabyt JS-side component.
*/
/** Macros for emitting StructBinder description. */
#define StructBinder__(TYPE) \
n = 0; \
outf("%s{", (structCount++ ? ", " : "")); \
out("\"name\": \"" # TYPE "\","); \
outf("\"sizeof\": %d", (int)sizeof(TYPE)); \
out(",\"members\": {");
#define StructBinder_(T) StructBinder__(T)
/** ^^^ indirection needed to expand CurrentStruct */
#define StructBinder StructBinder_(CurrentStruct)
#define _StructBinder CloseBrace(2)
#define M(MEMBER,SIG) \
outf("%s\"%s\": " \
"{\"offset\":%d,\"sizeof\": %d,\"signature\":\"%s\"}", \
(n++ ? ", " : ""), #MEMBER, \
(int)offsetof(CurrentStruct,MEMBER), \
(int)sizeof(((CurrentStruct*)0)->MEMBER), \
SIG)
structCount = 0;
out(", \"structs\": ["); {
#define CurrentStruct sqlite3_vfs
StructBinder {
M(iVersion,"i");
M(szOsFile,"i");
M(mxPathname,"i");
M(pNext,"p");
M(zName,"s");
M(pAppData,"p");
M(xOpen,"i(pppip)");
M(xDelete,"i(ppi)");
M(xAccess,"i(ppip)");
M(xFullPathname,"i(ppip)");
M(xDlOpen,"p(pp)");
M(xDlError,"p(pip)");
M(xDlSym,"p()");
M(xDlClose,"v(pp)");
M(xRandomness,"i(pip)");
M(xSleep,"i(pi)");
M(xCurrentTime,"i(pp)");
M(xGetLastError,"i(pip)");
M(xCurrentTimeInt64,"i(pp)");
M(xSetSystemCall,"i(ppp)");
M(xGetSystemCall,"p(pp)");
M(xNextSystemCall,"p(pp)");
} _StructBinder;
#undef CurrentStruct
#define CurrentStruct sqlite3_io_methods
StructBinder {
M(iVersion,"i");
M(xClose,"i(p)");
M(xRead,"i(ppij)");
M(xWrite,"i(ppij)");
M(xTruncate,"i(pj)");
M(xSync,"i(pi)");
M(xFileSize,"i(pp)");
M(xLock,"i(pi)");
M(xUnlock,"i(pi)");
M(xCheckReservedLock,"i(pp)");
M(xFileControl,"i(pip)");
M(xSectorSize,"i(p)");
M(xDeviceCharacteristics,"i(p)");
M(xShmMap,"i(piiip)");
M(xShmLock,"i(piii)");
M(xShmBarrier,"v(p)");
M(xShmUnmap,"i(pi)");
M(xFetch,"i(pjip)");
M(xUnfetch,"i(pjp)");
} _StructBinder;
#undef CurrentStruct
#define CurrentStruct sqlite3_file
StructBinder {
M(pMethods,"p");
} _StructBinder;
#undef CurrentStruct
} out( "]"/*structs*/);
out("}"/*top-level object*/);
*pos = 0;
strBuf[0] = '{'/*end of the race-condition workaround*/;
return strBuf;
#undef StructBinder
#undef StructBinder_
#undef StructBinder__
#undef M
#undef _StructBinder
#undef CloseBrace
#undef out
#undef outf
#undef lenCheck
}
/*
** This function is NOT part of the sqlite3 public API. It is strictly
** for use by the sqlite project's own JS/WASM bindings.
**
** This function invokes the xDelete method of the default VFS,
** passing on the given filename. If zName is NULL, no default VFS is
** found, or it has no xDelete method, SQLITE_MISUSE is returned, else
** the result of the xDelete() call is returned.
*/
WASM_KEEP
int sqlite3_wasm_vfs_unlink(const char * zName){
int rc = SQLITE_MISUSE /* ??? */;
sqlite3_vfs * const pVfs = sqlite3_vfs_find(0);
if( zName && pVfs && pVfs->xDelete ){
rc = pVfs->xDelete(pVfs, zName, 1);
}
return rc;
}
#if defined(__EMSCRIPTEN__)
#include <emscripten/console.h>
#if defined(SQLITE_WASM_WASMFS)
#include <emscripten/wasmfs.h>
/*
** This function is NOT part of the sqlite3 public API. It is strictly
** for use by the sqlite project's own JS/WASM bindings, specifically
** only when building with Emscripten's WASMFS support.
**
** This function should only be called if the JS side detects the
** existence of the Origin-Private FileSystem (OPFS) APIs in the
** client. The first time it is called, this function instantiates a
** WASMFS backend impl for OPFS. On success, subsequent calls are
** no-ops.
**
** This function may be passed a "mount point" name, which must have a
** leading "/" and is currently restricted to a single path component,
** e.g. "/foo" is legal but "/foo/" and "/foo/bar" are not. If it is
** NULL or empty, it defaults to "/persistent".
**
** Returns 0 on success, SQLITE_NOMEM if instantiation of the backend
** object fails, SQLITE_IOERR if mkdir() of the zMountPoint dir in
** the virtual FS fails. In builds compiled without SQLITE_WASM_WASMFS
** defined, SQLITE_NOTFOUND is returned without side effects.
*/
WASM_KEEP
int sqlite3_wasm_init_wasmfs(const char *zMountPoint){
static backend_t pOpfs = 0;
if( !zMountPoint || !*zMountPoint ) zMountPoint = "/opfs";
if( !pOpfs ){
pOpfs = wasmfs_create_opfs_backend();
if( pOpfs ){
emscripten_console_log("Created WASMFS OPFS backend.");
}
}
/** It's not enough to instantiate the backend. We have to create a
mountpoint in the VFS and attach the backend to it. */
if( pOpfs && 0!=access(zMountPoint, F_OK) ){
/* mkdir() simply hangs when called from fiddle app. Cause is
not yet determined but the hypothesis is an init-order
issue. */
/* Note that this check and is not robust but it will
hypothetically suffice for the transient wasm-based virtual
filesystem we're currently running in. */
const int rc = wasmfs_create_directory(zMountPoint, 0777, pOpfs);
emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc);
if(rc) return SQLITE_IOERR;
}
return pOpfs ? 0 : SQLITE_NOMEM;
}
#else
WASM_KEEP
int sqlite3_wasm_init_wasmfs(const char *zUnused){
emscripten_console_warn("WASMFS OPFS is not compiled in.");
if(zUnused){/*unused*/}
return SQLITE_NOTFOUND;
}
#endif /* __EMSCRIPTEN__ && SQLITE_WASM_WASMFS */
#endif
#undef WASM_KEEP
|