66
77// sqlite3_io_methods javascript handlers
88// 64-bit integer parameters are passed by pointer.
9- extern int vfsClose (sqlite3_file * file );
10- extern int vfsRead (sqlite3_file * file , void * pData , int iAmt , sqlite3_int64 iOffset );
11- extern int vfsWrite (sqlite3_file * file , const void * pData , int iAmt , sqlite3_int64 iOffset );
12- extern int vfsTruncate (sqlite3_file * file , sqlite3_int64 size );
13- extern int vfsSync (sqlite3_file * file , int flags );
14- extern int vfsFileSize (sqlite3_file * file , sqlite3_int64 * pSize );
15- extern int vfsLock (sqlite3_file * file , int flags );
16- extern int vfsUnlock (sqlite3_file * file , int flags );
17- extern int vfsCheckReservedLock (sqlite3_file * file , int * pResOut );
18- extern int vfsFileControl (sqlite3_file * file , int flags , void * pOut );
19- extern int vfsSectorSize (sqlite3_file * file );
20- extern int vfsDeviceCharacteristics (sqlite3_file * file );
9+ extern int vfsClose (sqlite3_file * file );
10+ extern int vfsRead (sqlite3_file * file , void * pData , int iAmt , sqlite3_int64 iOffset );
11+ extern int vfsWrite (sqlite3_file * file , const void * pData , int iAmt , sqlite3_int64 iOffset );
12+ extern int vfsTruncate (sqlite3_file * file , sqlite3_int64 size );
13+ extern int vfsSync (sqlite3_file * file , int flags );
14+ extern int vfsFileSize (sqlite3_file * file , sqlite3_int64 * pSize );
15+ extern int vfsLock (sqlite3_file * file , int flags );
16+ extern int vfsUnlock (sqlite3_file * file , int flags );
17+ extern int vfsCheckReservedLock (sqlite3_file * file , int * pResOut );
18+ extern int vfsFileControl (sqlite3_file * file , int flags , void * pOut );
19+ extern int vfsSectorSize (sqlite3_file * file );
20+ extern int vfsDeviceCharacteristics (sqlite3_file * file );
2121
22- extern int vfsOpen (sqlite3_vfs * vfs , const char * zName , sqlite3_file * file , int flags , int * pOutFlags );
23- extern int vfsDelete (sqlite3_vfs * vfs , const char * zName , int syncDir );
24- extern int vfsAccess (sqlite3_vfs * vfs , const char * zName , int flags , int * pResOut );
22+ extern int vfsOpen (sqlite3_vfs * vfs , const char * zName , sqlite3_file * file , int flags , int * pOutFlags );
23+ extern int vfsDelete (sqlite3_vfs * vfs , const char * zName , int syncDir );
24+ extern int vfsAccess (sqlite3_vfs * vfs , const char * zName , int flags , int * pResOut );
2525
2626// This is undefined in the WASM linker step if not specified
2727extern int __rust_no_alloc_shim_is_unstable = 0 ;
2828extern int sqlite3_powersync_init (sqlite3 * db , char * * pzErrMsg ,
29- const sqlite3_api_routines * pApi );
29+ const sqlite3_api_routines * pApi );
3030
31- static int xOpen (sqlite3_vfs * vfs , const char * zName , sqlite3_file * file , int flags , int * pOutFlags ) {
31+ static int xOpen (sqlite3_vfs * vfs , const char * zName , sqlite3_file * file , int flags , int * pOutFlags )
32+ {
3233 static sqlite3_io_methods io_methods = {
33- 1 ,
34- vfsClose ,
35- vfsRead ,
36- vfsWrite ,
37- vfsTruncate ,
38- vfsSync ,
39- vfsFileSize ,
40- vfsLock ,
41- vfsUnlock ,
42- vfsCheckReservedLock ,
43- vfsFileControl ,
44- vfsSectorSize ,
45- vfsDeviceCharacteristics
46- };
34+ 1 ,
35+ vfsClose ,
36+ vfsRead ,
37+ vfsWrite ,
38+ vfsTruncate ,
39+ vfsSync ,
40+ vfsFileSize ,
41+ vfsLock ,
42+ vfsUnlock ,
43+ vfsCheckReservedLock ,
44+ vfsFileControl ,
45+ vfsSectorSize ,
46+ vfsDeviceCharacteristics };
4747 file -> pMethods = & io_methods ;
4848
4949 return vfsOpen (vfs , zName , file , flags , pOutFlags );
5050}
5151
52- static int xFullPathname (sqlite3_vfs * vfs , const char * zName , int nOut , char * zOut ) {
52+ static int xFullPathname (sqlite3_vfs * vfs , const char * zName , int nOut , char * zOut )
53+ {
5354 strncpy (zOut , zName , nOut );
5455 return SQLITE_OK ;
5556}
5657
57- static int xCurrentTime (sqlite3_vfs * vfs , double * pJulianDay ) {
58+ static int xCurrentTime (sqlite3_vfs * vfs , double * pJulianDay )
59+ {
5860 // UNIX epoch 1/1/1970 is Julian day 2440587.5
59- static const sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64 )8640000 ;
61+ static const sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64 )8640000 ;
6062 struct timeval sNow ;
6163 gettimeofday (& sNow , 0 );
62- sqlite3_int64 julianMillis = unixEpoch + 1000 * (sqlite3_int64 )sNow .tv_sec + sNow .tv_usec / 1000 ;
64+ sqlite3_int64 julianMillis = unixEpoch + 1000 * (sqlite3_int64 )sNow .tv_sec + sNow .tv_usec / 1000 ;
6365 * pJulianDay = julianMillis / 86400000.0 ;
6466 return SQLITE_OK ;
6567}
6668
6769const int EMSCRIPTEN_KEEPALIVE register_vfs (
68- const char * zName ,
69- int mxPathName ,
70- int makeDefault ,
71- sqlite3_vfs * * ppVFS ) {
72- sqlite3_vfs * vfs = * ppVFS = (sqlite3_vfs * )sqlite3_malloc (sizeof (sqlite3_vfs ));
73- if (!vfs ) {
70+ const char * zName ,
71+ int mxPathName ,
72+ int makeDefault ,
73+ sqlite3_vfs * * ppVFS )
74+ {
75+ sqlite3_vfs * vfs = * ppVFS = (sqlite3_vfs * )sqlite3_malloc (sizeof (sqlite3_vfs ));
76+ if (!vfs )
77+ {
7478 return SQLITE_NOMEM ;
7579 }
7680
@@ -85,9 +89,9 @@ const int EMSCRIPTEN_KEEPALIVE register_vfs(
8589 vfs -> xAccess = vfsAccess ;
8690 vfs -> xFullPathname = xFullPathname ;
8791 vfs -> xCurrentTime = xCurrentTime ;
88-
92+
8993 // Get remaining functionality from the default VFS.
90- sqlite3_vfs * defer = sqlite3_vfs_find (0 );
94+ sqlite3_vfs * defer = sqlite3_vfs_find (0 );
9195#define COPY_FIELD (NAME ) vfs->NAME = defer->NAME
9296 COPY_FIELD (xDlOpen );
9397 COPY_FIELD (xDlError );
@@ -99,22 +103,26 @@ const int EMSCRIPTEN_KEEPALIVE register_vfs(
99103#undef COPY_FIELD
100104
101105 const int result = sqlite3_vfs_register (vfs , makeDefault );
102- if (result != SQLITE_OK ) {
106+ if (result != SQLITE_OK )
107+ {
103108 * ppVFS = 0 ;
104109 sqlite3_free (vfs );
105110 }
106111 return result ;
107112}
108113
109- void * EMSCRIPTEN_KEEPALIVE getSqliteFree () {
114+ void * EMSCRIPTEN_KEEPALIVE getSqliteFree ()
115+ {
110116 return sqlite3_free ;
111117}
112118
113- int main () {
119+ int main ()
120+ {
114121 sqlite3_initialize ();
115122 return 0 ;
116123}
117124
118- int setup_powersync () {
119- return sqlite3_auto_extension ((void (* )(void )) & sqlite3_powersync_init );
125+ int setup_powersync ()
126+ {
127+ return sqlite3_auto_extension ((void (* )(void )) & sqlite3_powersync_init );
120128}
0 commit comments