@@ -5,7 +5,7 @@ use std::fmt;
5
5
use std:: ops:: Range ;
6
6
use std:: time:: { Duration , SystemTime , UNIX_EPOCH } ;
7
7
8
- use crate :: function:: { OptionalArg , PyFuncArgs } ;
8
+ use crate :: function:: OptionalArg ;
9
9
use crate :: obj:: objfloat:: PyFloatRef ;
10
10
use crate :: obj:: objint:: PyInt ;
11
11
use crate :: obj:: objsequence:: get_sequence_index;
@@ -44,38 +44,45 @@ fn time_sleep(seconds: PyFloatRef, vm: &VirtualMachine) -> PyResult<()> {
44
44
}
45
45
46
46
#[ cfg( not( unix) ) ]
47
- fn time_sleep ( seconds : PyFloatRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
47
+ fn time_sleep ( seconds : PyFloatRef , _vm : & VirtualMachine ) {
48
48
let seconds = seconds. to_f64 ( ) ;
49
49
let secs: u64 = seconds. trunc ( ) as u64 ;
50
50
let nanos: u32 = ( seconds. fract ( ) * 1e9 ) as u32 ;
51
51
let duration = Duration :: new ( secs, nanos) ;
52
52
std:: thread:: sleep ( duration) ;
53
- Ok ( ( ) )
54
53
}
55
54
56
55
fn duration_to_f64 ( d : Duration ) -> f64 {
57
56
( d. as_secs ( ) as f64 ) + ( f64:: from ( d. subsec_nanos ( ) ) / 1e9 )
58
57
}
59
58
60
- fn time_time ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
61
- arg_check ! ( vm , args ) ;
62
- let x = match SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) {
59
+ # [ cfg ( not ( target_arch = "wasm32" ) ) ]
60
+ fn time_time ( _vm : & VirtualMachine ) -> f64 {
61
+ match SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) {
63
62
Ok ( v) => duration_to_f64 ( v) ,
64
- Err ( err) => panic ! ( "Error: {:?}" , err) ,
65
- } ;
66
- let value = vm. ctx . new_float ( x) ;
67
- Ok ( value)
63
+ Err ( err) => panic ! ( "Time error: {:?}" , err) ,
64
+ }
68
65
}
69
66
70
- fn time_monotonic ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
71
- arg_check ! ( vm, args) ;
67
+ #[ cfg( target_arch = "wasm32" ) ]
68
+ fn time_time ( _vm : & VirtualMachine ) -> f64 {
69
+ use wasm_bindgen:: prelude:: * ;
70
+ #[ wasm_bindgen]
71
+ extern "C" {
72
+ type Date ;
73
+ #[ wasm_bindgen( static_method_of = Date ) ]
74
+ fn now ( ) -> f64 ;
75
+ }
76
+ // Date.now returns unix time in milliseconds, we want it in seconds
77
+ Date :: now ( ) / 1000.0
78
+ }
79
+
80
+ fn time_monotonic ( _vm : & VirtualMachine ) -> f64 {
72
81
// TODO: implement proper monotonic time!
73
- let x = match SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) {
82
+ match SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) {
74
83
Ok ( v) => duration_to_f64 ( v) ,
75
- Err ( err) => panic ! ( "Error: {:?}" , err) ,
76
- } ;
77
- let value = vm. ctx . new_float ( x) ;
78
- Ok ( value)
84
+ Err ( err) => panic ! ( "Time error: {:?}" , err) ,
85
+ }
79
86
}
80
87
81
88
fn pyfloat_to_secs_and_nanos ( seconds : & PyObjectRef ) -> ( i64 , u32 ) {
0 commit comments