xref: /sqlite-3.40.0/ext/wasm/api/sqlite3-wasm.c (revision 3961b263)
1 #include "sqlite3.c"
2 
3 /*
4 ** This function is NOT part of the sqlite3 public API. It is strictly
5 ** for use by the sqlite project's own JS/WASM bindings.
6 **
7 ** For purposes of certain hand-crafted C/Wasm function bindings, we
8 ** need a way of reporting errors which is consistent with the rest of
9 ** the C API. To that end, this internal-use-only function is a thin
10 ** proxy around sqlite3ErrorWithMessage(). The intent is that it only
11 ** be used from Wasm bindings such as sqlite3_prepare_v2/v3(), and
12 ** definitely not from client code.
13 **
14 ** Returns err_code.
15 */
16 int sqlite3_wasm_db_error(sqlite3*db, int err_code,
17                           const char *zMsg){
18   if(0!=zMsg){
19     const int nMsg = sqlite3Strlen30(zMsg);
20     sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg);
21   }else{
22     sqlite3ErrorWithMsg(db, err_code, NULL);
23   }
24   return err_code;
25 }
26 
27 /*
28 ** This function is NOT part of the sqlite3 public API. It is strictly
29 ** for use by the sqlite project's own JS/WASM bindings. Unlike the
30 ** rest of the sqlite3 API, this part requires C99 for snprintf() and
31 ** variadic macros.
32 **
33 ** Returns a string containing a JSON-format "enum" of C-level
34 ** constants intended to be imported into the JS environment. The JSON
35 ** is initialized the first time this function is called and that
36 ** result is reused for all future calls.
37 **
38 ** If this function returns NULL then it means that the internal
39 ** buffer is not large enough for the generated JSON. In debug builds
40 ** that will trigger an assert().
41 */
42 const char * sqlite3_wasm_enum_json(void){
43   static char strBuf[1024 * 8] = {0} /* where the JSON goes */;
44   int n = 0, childCount = 0, structCount = 0
45     /* output counters for figuring out where commas go */;
46   char * pos = &strBuf[1] /* skip first byte for now to help protect
47                           ** against a small race condition */;
48   char const * const zEnd = pos + sizeof(strBuf) /* one-past-the-end */;
49   if(strBuf[0]) return strBuf;
50   /* Leave strBuf[0] at 0 until the end to help guard against a tiny
51   ** race condition. If this is called twice concurrently, they might
52   ** end up both writing to strBuf, but they'll both write the same
53   ** thing, so that's okay. If we set byte 0 up front then the 2nd
54   ** instance might return and use the string before the 1st instance
55   ** is done filling it. */
56 
57 /* Core output macros... */
58 #define lenCheck assert(pos < zEnd - 128 \
59   && "sqlite3_wasm_enum_json() buffer is too small."); \
60   if(pos >= zEnd - 128) return 0
61 #define outf(format,...) \
62   pos += snprintf(pos, ((size_t)(zEnd - pos)), format, __VA_ARGS__); \
63   lenCheck
64 #define out(TXT) outf("%s",TXT)
65 #define CloseBrace(LEVEL) \
66   assert(LEVEL<5); memset(pos, '}', LEVEL); pos+=LEVEL; lenCheck
67 
68 /* Macros for emitting maps of integer- and string-type macros to
69 ** their values. */
70 #define DefGroup(KEY) n = 0; \
71   outf("%s\"" #KEY "\": {",(childCount++ ? "," : ""));
72 #define DefInt(KEY)                                     \
73   outf("%s\"%s\": %d", (n++ ? ", " : ""), #KEY, (int)KEY)
74 #define DefStr(KEY)                                     \
75   outf("%s\"%s\": \"%s\"", (n++ ? ", " : ""), #KEY, KEY)
76 #define _DefGroup CloseBrace(1)
77 
78   DefGroup(version) {
79     DefInt(SQLITE_VERSION_NUMBER);
80     DefStr(SQLITE_VERSION);
81     DefStr(SQLITE_SOURCE_ID);
82   } _DefGroup;
83 
84   DefGroup(resultCodes) {
85     DefInt(SQLITE_OK);
86     DefInt(SQLITE_ERROR);
87     DefInt(SQLITE_INTERNAL);
88     DefInt(SQLITE_PERM);
89     DefInt(SQLITE_ABORT);
90     DefInt(SQLITE_BUSY);
91     DefInt(SQLITE_LOCKED);
92     DefInt(SQLITE_NOMEM);
93     DefInt(SQLITE_READONLY);
94     DefInt(SQLITE_INTERRUPT);
95     DefInt(SQLITE_IOERR);
96     DefInt(SQLITE_CORRUPT);
97     DefInt(SQLITE_NOTFOUND);
98     DefInt(SQLITE_FULL);
99     DefInt(SQLITE_CANTOPEN);
100     DefInt(SQLITE_PROTOCOL);
101     DefInt(SQLITE_EMPTY);
102     DefInt(SQLITE_SCHEMA);
103     DefInt(SQLITE_TOOBIG);
104     DefInt(SQLITE_CONSTRAINT);
105     DefInt(SQLITE_MISMATCH);
106     DefInt(SQLITE_MISUSE);
107     DefInt(SQLITE_NOLFS);
108     DefInt(SQLITE_AUTH);
109     DefInt(SQLITE_FORMAT);
110     DefInt(SQLITE_RANGE);
111     DefInt(SQLITE_NOTADB);
112     DefInt(SQLITE_NOTICE);
113     DefInt(SQLITE_WARNING);
114     DefInt(SQLITE_ROW);
115     DefInt(SQLITE_DONE);
116 
117     // Extended Result Codes
118     DefInt(SQLITE_ERROR_MISSING_COLLSEQ);
119     DefInt(SQLITE_ERROR_RETRY);
120     DefInt(SQLITE_ERROR_SNAPSHOT);
121     DefInt(SQLITE_IOERR_READ);
122     DefInt(SQLITE_IOERR_SHORT_READ);
123     DefInt(SQLITE_IOERR_WRITE);
124     DefInt(SQLITE_IOERR_FSYNC);
125     DefInt(SQLITE_IOERR_DIR_FSYNC);
126     DefInt(SQLITE_IOERR_TRUNCATE);
127     DefInt(SQLITE_IOERR_FSTAT);
128     DefInt(SQLITE_IOERR_UNLOCK);
129     DefInt(SQLITE_IOERR_RDLOCK);
130     DefInt(SQLITE_IOERR_DELETE);
131     DefInt(SQLITE_IOERR_BLOCKED);
132     DefInt(SQLITE_IOERR_NOMEM);
133     DefInt(SQLITE_IOERR_ACCESS);
134     DefInt(SQLITE_IOERR_CHECKRESERVEDLOCK);
135     DefInt(SQLITE_IOERR_LOCK);
136     DefInt(SQLITE_IOERR_CLOSE);
137     DefInt(SQLITE_IOERR_DIR_CLOSE);
138     DefInt(SQLITE_IOERR_SHMOPEN);
139     DefInt(SQLITE_IOERR_SHMSIZE);
140     DefInt(SQLITE_IOERR_SHMLOCK);
141     DefInt(SQLITE_IOERR_SHMMAP);
142     DefInt(SQLITE_IOERR_SEEK);
143     DefInt(SQLITE_IOERR_DELETE_NOENT);
144     DefInt(SQLITE_IOERR_MMAP);
145     DefInt(SQLITE_IOERR_GETTEMPPATH);
146     DefInt(SQLITE_IOERR_CONVPATH);
147     DefInt(SQLITE_IOERR_VNODE);
148     DefInt(SQLITE_IOERR_AUTH);
149     DefInt(SQLITE_IOERR_BEGIN_ATOMIC);
150     DefInt(SQLITE_IOERR_COMMIT_ATOMIC);
151     DefInt(SQLITE_IOERR_ROLLBACK_ATOMIC);
152     DefInt(SQLITE_IOERR_DATA);
153     DefInt(SQLITE_IOERR_CORRUPTFS);
154     DefInt(SQLITE_LOCKED_SHAREDCACHE);
155     DefInt(SQLITE_LOCKED_VTAB);
156     DefInt(SQLITE_BUSY_RECOVERY);
157     DefInt(SQLITE_BUSY_SNAPSHOT);
158     DefInt(SQLITE_BUSY_TIMEOUT);
159     DefInt(SQLITE_CANTOPEN_NOTEMPDIR);
160     DefInt(SQLITE_CANTOPEN_ISDIR);
161     DefInt(SQLITE_CANTOPEN_FULLPATH);
162     DefInt(SQLITE_CANTOPEN_CONVPATH);
163     //DefInt(SQLITE_CANTOPEN_DIRTYWAL)/*docs say not used*/;
164     DefInt(SQLITE_CANTOPEN_SYMLINK);
165     DefInt(SQLITE_CORRUPT_VTAB);
166     DefInt(SQLITE_CORRUPT_SEQUENCE);
167     DefInt(SQLITE_CORRUPT_INDEX);
168     DefInt(SQLITE_READONLY_RECOVERY);
169     DefInt(SQLITE_READONLY_CANTLOCK);
170     DefInt(SQLITE_READONLY_ROLLBACK);
171     DefInt(SQLITE_READONLY_DBMOVED);
172     DefInt(SQLITE_READONLY_CANTINIT);
173     DefInt(SQLITE_READONLY_DIRECTORY);
174     DefInt(SQLITE_ABORT_ROLLBACK);
175     DefInt(SQLITE_CONSTRAINT_CHECK);
176     DefInt(SQLITE_CONSTRAINT_COMMITHOOK);
177     DefInt(SQLITE_CONSTRAINT_FOREIGNKEY);
178     DefInt(SQLITE_CONSTRAINT_FUNCTION);
179     DefInt(SQLITE_CONSTRAINT_NOTNULL);
180     DefInt(SQLITE_CONSTRAINT_PRIMARYKEY);
181     DefInt(SQLITE_CONSTRAINT_TRIGGER);
182     DefInt(SQLITE_CONSTRAINT_UNIQUE);
183     DefInt(SQLITE_CONSTRAINT_VTAB);
184     DefInt(SQLITE_CONSTRAINT_ROWID);
185     DefInt(SQLITE_CONSTRAINT_PINNED);
186     DefInt(SQLITE_CONSTRAINT_DATATYPE);
187     DefInt(SQLITE_NOTICE_RECOVER_WAL);
188     DefInt(SQLITE_NOTICE_RECOVER_ROLLBACK);
189     DefInt(SQLITE_WARNING_AUTOINDEX);
190     DefInt(SQLITE_AUTH_USER);
191     DefInt(SQLITE_OK_LOAD_PERMANENTLY);
192     //DefInt(SQLITE_OK_SYMLINK) /* internal use only */;
193   } _DefGroup;
194 
195   DefGroup(dataTypes) {
196     DefInt(SQLITE_INTEGER);
197     DefInt(SQLITE_FLOAT);
198     DefInt(SQLITE_TEXT);
199     DefInt(SQLITE_BLOB);
200     DefInt(SQLITE_NULL);
201   } _DefGroup;
202 
203   DefGroup(encodings) {
204     /* Noting that the wasm binding only aims to support UTF-8. */
205     DefInt(SQLITE_UTF8);
206     DefInt(SQLITE_UTF16LE);
207     DefInt(SQLITE_UTF16BE);
208     DefInt(SQLITE_UTF16);
209     /*deprecated DefInt(SQLITE_ANY); */
210     DefInt(SQLITE_UTF16_ALIGNED);
211   } _DefGroup;
212 
213   DefGroup(blobFinalizers) {
214     /* SQLITE_STATIC/TRANSIENT need to be handled explicitly as
215     ** integers to avoid casting-related warnings. */
216     out("\"SQLITE_STATIC\":0, "
217         "\"SQLITE_TRANSIENT\":-1");
218   } _DefGroup;
219 
220   DefGroup(udfFlags) {
221     DefInt(SQLITE_DETERMINISTIC);
222     DefInt(SQLITE_DIRECTONLY);
223     DefInt(SQLITE_INNOCUOUS);
224   } _DefGroup;
225 
226   DefGroup(openFlags) {
227     /* Noting that not all of these will have any effect in WASM-space. */
228     DefInt(SQLITE_OPEN_READONLY);
229     DefInt(SQLITE_OPEN_READWRITE);
230     DefInt(SQLITE_OPEN_CREATE);
231     DefInt(SQLITE_OPEN_URI);
232     DefInt(SQLITE_OPEN_MEMORY);
233     DefInt(SQLITE_OPEN_NOMUTEX);
234     DefInt(SQLITE_OPEN_FULLMUTEX);
235     DefInt(SQLITE_OPEN_SHAREDCACHE);
236     DefInt(SQLITE_OPEN_PRIVATECACHE);
237     DefInt(SQLITE_OPEN_EXRESCODE);
238     DefInt(SQLITE_OPEN_NOFOLLOW);
239     /* OPEN flags for use with VFSes... */
240     DefInt(SQLITE_OPEN_MAIN_DB);
241     DefInt(SQLITE_OPEN_MAIN_JOURNAL);
242     DefInt(SQLITE_OPEN_TEMP_DB);
243     DefInt(SQLITE_OPEN_TEMP_JOURNAL);
244     DefInt(SQLITE_OPEN_TRANSIENT_DB);
245     DefInt(SQLITE_OPEN_SUBJOURNAL);
246     DefInt(SQLITE_OPEN_SUPER_JOURNAL);
247     DefInt(SQLITE_OPEN_WAL);
248     DefInt(SQLITE_OPEN_DELETEONCLOSE);
249     DefInt(SQLITE_OPEN_EXCLUSIVE);
250   } _DefGroup;
251 
252   DefGroup(syncFlags) {
253     DefInt(SQLITE_SYNC_NORMAL);
254     DefInt(SQLITE_SYNC_FULL);
255     DefInt(SQLITE_SYNC_DATAONLY);
256   } _DefGroup;
257 
258   DefGroup(prepareFlags) {
259     DefInt(SQLITE_PREPARE_PERSISTENT);
260     DefInt(SQLITE_PREPARE_NORMALIZE);
261     DefInt(SQLITE_PREPARE_NO_VTAB);
262   } _DefGroup;
263 
264   DefGroup(flock) {
265     DefInt(SQLITE_LOCK_NONE);
266     DefInt(SQLITE_LOCK_SHARED);
267     DefInt(SQLITE_LOCK_RESERVED);
268     DefInt(SQLITE_LOCK_PENDING);
269     DefInt(SQLITE_LOCK_EXCLUSIVE);
270   } _DefGroup;
271 
272   DefGroup(ioCap) {
273     DefInt(SQLITE_IOCAP_ATOMIC);
274     DefInt(SQLITE_IOCAP_ATOMIC512);
275     DefInt(SQLITE_IOCAP_ATOMIC1K);
276     DefInt(SQLITE_IOCAP_ATOMIC2K);
277     DefInt(SQLITE_IOCAP_ATOMIC4K);
278     DefInt(SQLITE_IOCAP_ATOMIC8K);
279     DefInt(SQLITE_IOCAP_ATOMIC16K);
280     DefInt(SQLITE_IOCAP_ATOMIC32K);
281     DefInt(SQLITE_IOCAP_ATOMIC64K);
282     DefInt(SQLITE_IOCAP_SAFE_APPEND);
283     DefInt(SQLITE_IOCAP_SEQUENTIAL);
284     DefInt(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN);
285     DefInt(SQLITE_IOCAP_POWERSAFE_OVERWRITE);
286     DefInt(SQLITE_IOCAP_IMMUTABLE);
287     DefInt(SQLITE_IOCAP_BATCH_ATOMIC);
288   } _DefGroup;
289 
290   DefGroup(access){
291     DefInt(SQLITE_ACCESS_EXISTS);
292     DefInt(SQLITE_ACCESS_READWRITE);
293     DefInt(SQLITE_ACCESS_READ)/*docs say this is unused*/;
294   } _DefGroup;
295 
296 #undef DefGroup
297 #undef DefStr
298 #undef DefInt
299 #undef _DefGroup
300 
301   /*
302   ** Emit an array of "StructBinder" struct descripions, which look
303   ** like:
304   **
305   ** {
306   **   "name": "MyStruct",
307   **   "sizeof": 16,
308   **   "members": {
309   **     "member1": {"offset": 0,"sizeof": 4,"signature": "i"},
310   **     "member2": {"offset": 4,"sizeof": 4,"signature": "p"},
311   **     "member3": {"offset": 8,"sizeof": 8,"signature": "j"}
312   **   }
313   ** }
314   **
315   ** Detailed documentation for those bits are in an external
316   ** file (StackBinder.md, as of this writing).
317   */
318 
319   /** Macros for emitting StructBinder description. */
320 #define StructBinder__(TYPE)                 \
321   n = 0;                                     \
322   outf("%s{", (structCount++ ? ", " : ""));  \
323   out("\"name\": \"" # TYPE "\",");         \
324   outf("\"sizeof\": %d", (int)sizeof(TYPE)); \
325   out(",\"members\": {");
326 #define StructBinder_(T) StructBinder__(T)
327   /** ^^^ indirection needed to expand CurrentStruct */
328 #define StructBinder StructBinder_(CurrentStruct)
329 #define _StructBinder CloseBrace(2)
330 #define M(MEMBER,SIG)                                         \
331   outf("%s\"%s\": "                                           \
332        "{\"offset\":%d,\"sizeof\": %d,\"signature\":\"%s\"}", \
333        (n++ ? ", " : ""), #MEMBER,                            \
334        (int)offsetof(CurrentStruct,MEMBER),                   \
335        (int)sizeof(((CurrentStruct*)0)->MEMBER),              \
336        SIG)
337 
338   structCount = 0;
339   out(", \"structs\": ["); {
340 
341 #define CurrentStruct sqlite3_vfs
342     StructBinder {
343       M(iVersion,"i");
344       M(szOsFile,"i");
345       M(mxPathname,"i");
346       M(pNext,"p");
347       M(zName,"s");
348       M(pAppData,"p");
349       M(xOpen,"i(pppip)");
350       M(xDelete,"i(ppi)");
351       M(xAccess,"i(ppip)");
352       M(xFullPathname,"i(ppip)");
353       M(xDlOpen,"p(pp)");
354       M(xDlError,"p(pip)");
355       M(xDlSym,"p()");
356       M(xDlClose,"v(pp)");
357       M(xRandomness,"i(pip)");
358       M(xSleep,"i(pi)");
359       M(xCurrentTime,"i(pp)");
360       M(xGetLastError,"i(pip)");
361       M(xCurrentTimeInt64,"i(pp)");
362       M(xSetSystemCall,"i(ppp)");
363       M(xGetSystemCall,"p(pp)");
364       M(xNextSystemCall,"p(pp)");
365     } _StructBinder;
366 #undef CurrentStruct
367 
368 #define CurrentStruct sqlite3_io_methods
369     StructBinder {
370       M(iVersion,"i");
371       M(xClose,"i(p)");
372       M(xRead,"i(ppij)");
373       M(xWrite,"i(ppij)");
374       M(xTruncate,"i(pj)");
375       M(xSync,"i(pi)");
376       M(xFileSize,"i(pp)");
377       M(xLock,"i(pi)");
378       M(xUnlock,"i(pi)");
379       M(xCheckReservedLock,"i(pp)");
380       M(xFileControl,"i(pip)");
381       M(xSectorSize,"i(p)");
382       M(xDeviceCharacteristics,"i(p)");
383       M(xShmMap,"i(piiip)");
384       M(xShmLock,"i(piii)");
385       M(xShmBarrier,"v(p)");
386       M(xShmUnmap,"i(pi)");
387       M(xFetch,"i(pjip)");
388       M(xUnfetch,"i(pjp)");
389     } _StructBinder;
390 #undef CurrentStruct
391 
392 #define CurrentStruct sqlite3_file
393     StructBinder {
394       M(pMethods,"P");
395     } _StructBinder;
396 #undef CurrentStruct
397 
398   } out( "]"/*structs*/);
399 
400   out("}"/*top-level object*/);
401   *pos = 0;
402   strBuf[0] = '{'/*end of the race-condition workaround*/;
403   return strBuf;
404 #undef StructBinder
405 #undef StructBinder_
406 #undef StructBinder__
407 #undef M
408 #undef _StructBinder
409 #undef CloseBrace
410 #undef out
411 #undef outf
412 #undef lenCheck
413 }
414