55#![ allow( non_upper_case_globals) ]
66#![ allow( non_camel_case_types) ]
77#![ allow( non_snake_case) ]
8- #![ cfg_attr( target_feature = "atomics" , feature( stdarch_wasm_atomic_wait) ) ]
98
109extern crate alloc;
1110
@@ -25,6 +24,7 @@ use alloc::string::String;
2524use alloc:: vec:: Vec ;
2625use alloc:: { boxed:: Box , ffi:: CString } ;
2726use alloc:: { format, vec} ;
27+ use core:: time:: Duration ;
2828use core:: { cell:: RefCell , ffi:: CStr , ops:: Deref } ;
2929
3030pub mod memvfs;
@@ -464,6 +464,7 @@ pub trait SQLiteVfs<IO: SQLiteIoMethods> {
464464 const VERSION : :: core:: ffi:: c_int ;
465465 const MAX_PATH_SIZE : :: core:: ffi:: c_int = 1024 ;
466466
467+ fn sleep ( dur : Duration ) ;
467468 fn random ( buf : & mut [ u8 ] ) ;
468469 fn epoch_timestamp_in_ms ( ) -> i64 ;
469470
@@ -487,7 +488,7 @@ pub trait SQLiteVfs<IO: SQLiteIoMethods> {
487488 xDlSym : None ,
488489 xDlClose : None ,
489490 xRandomness : Some ( Self :: xRandomness) ,
490- xSleep : Some ( x_methods_shim :: xSleep) ,
491+ xSleep : Some ( Self :: xSleep) ,
491492 xCurrentTime : Some ( Self :: xCurrentTime) ,
492493 xGetLastError : Some ( Self :: xGetLastError) ,
493494 xCurrentTimeInt64 : Some ( Self :: xCurrentTimeInt64) ,
@@ -635,32 +636,45 @@ pub trait SQLiteVfs<IO: SQLiteIoMethods> {
635636
636637 /// <https://github.com/sqlite/sqlite/blob/fb9e8e48fd70b463fb7ba6d99e00f2be54df749e/ext/wasm/api/sqlite3-vfs-opfs.c-pp.js#L951>
637638 unsafe extern "C" fn xRandomness (
638- _pVfs : * mut sqlite3_vfs ,
639+ pVfs : * mut sqlite3_vfs ,
639640 nByte : :: core:: ffi:: c_int ,
640641 zOut : * mut :: core:: ffi:: c_char ,
641642 ) -> :: core:: ffi:: c_int {
643+ unused ! ( pVfs) ;
642644 let slice = core:: slice:: from_raw_parts_mut ( zOut. cast ( ) , nByte as usize ) ;
643645 Self :: random ( slice) ;
644646 nByte
645647 }
646648
647649 /// <https://github.com/sqlite/sqlite/blob/fb9e8e48fd70b463fb7ba6d99e00f2be54df749e/ext/wasm/api/sqlite3-vfs-opfs.c-pp.js#L870>
648650 unsafe extern "C" fn xCurrentTime (
649- _pVfs : * mut sqlite3_vfs ,
651+ pVfs : * mut sqlite3_vfs ,
650652 pTimeOut : * mut f64 ,
651653 ) -> :: core:: ffi:: c_int {
654+ unused ! ( pVfs) ;
652655 * pTimeOut = 2440587.5 + ( Self :: epoch_timestamp_in_ms ( ) as f64 / 86400000.0 ) ;
653656 SQLITE_OK
654657 }
655658
656659 /// <https://github.com/sqlite/sqlite/blob/fb9e8e48fd70b463fb7ba6d99e00f2be54df749e/ext/wasm/api/sqlite3-vfs-opfs.c-pp.js#L877>
657660 unsafe extern "C" fn xCurrentTimeInt64 (
658- _pVfs : * mut sqlite3_vfs ,
661+ pVfs : * mut sqlite3_vfs ,
659662 pOut : * mut sqlite3_int64 ,
660663 ) -> :: core:: ffi:: c_int {
664+ unused ! ( pVfs) ;
661665 * pOut = ( ( 2440587.5 * 86400000.0 ) + Self :: epoch_timestamp_in_ms ( ) as f64 ) as sqlite3_int64 ;
662666 SQLITE_OK
663667 }
668+
669+ unsafe extern "C" fn xSleep (
670+ pVfs : * mut sqlite3_vfs ,
671+ microseconds : :: core:: ffi:: c_int ,
672+ ) -> :: core:: ffi:: c_int {
673+ unused ! ( pVfs) ;
674+ let dur = Duration :: from_micros ( microseconds as u64 ) ;
675+ Self :: sleep ( dur) ;
676+ SQLITE_OK
677+ }
664678}
665679
666680/// A trait that abstracts the `sqlite3_io_methods` struct, allowing for a more idiomatic Rust implementation.
@@ -865,45 +879,6 @@ pub trait SQLiteIoMethods {
865879 }
866880}
867881
868- /// A module containing shims for VFS methods that are implemented using JavaScript interoperability.
869- #[ allow( clippy:: missing_safety_doc) ]
870- pub mod x_methods_shim {
871- use super :: * ;
872-
873- /// thread::sleep is available when atomics is enabled
874- #[ cfg( target_feature = "atomics" ) ]
875- pub unsafe extern "C" fn xSleep (
876- _pVfs : * mut sqlite3_vfs ,
877- microseconds : :: core:: ffi:: c_int ,
878- ) -> :: core:: ffi:: c_int {
879- use core:: time:: Duration ;
880-
881- // Use an atomic wait to block the current thread artificially with a
882- // timeout listed. Note that we should never be notified (return value
883- // of 0) or our comparison should never fail (return value of 1) so we
884- // should always only resume execution through a timeout (return value
885- // 2).
886- let dur = Duration :: from_micros ( microseconds as u64 ) ;
887- let mut nanos = dur. as_nanos ( ) ;
888- while nanos > 0 {
889- let amt = core:: cmp:: min ( i64:: MAX as u128 , nanos) ;
890- let mut x = 0 ;
891- let val = unsafe { core:: arch:: wasm32:: memory_atomic_wait32 ( & mut x, 0 , amt as i64 ) } ;
892- debug_assert_eq ! ( val, 2 ) ;
893- nanos -= amt;
894- }
895- SQLITE_OK
896- }
897-
898- #[ cfg( not( target_feature = "atomics" ) ) ]
899- pub unsafe extern "C" fn xSleep (
900- _pVfs : * mut sqlite3_vfs ,
901- _microseconds : :: core:: ffi:: c_int ,
902- ) -> :: core:: ffi:: c_int {
903- SQLITE_OK
904- }
905- }
906-
907882#[ derive( thiserror:: Error , Debug ) ]
908883pub enum ImportDbError {
909884 #[ error( "Byte array size is invalid for an SQLite db." ) ]
0 commit comments