2018-11-26 21:15:49 +00:00
|
|
|
use libc::{
|
|
|
|
gettimeofday,
|
|
|
|
timeval,
|
|
|
|
c_int,
|
2018-11-26 21:30:55 +00:00
|
|
|
clock_gettime,
|
2018-11-26 21:20:10 +00:00
|
|
|
clock_gettime as libc_clock_gettime,
|
2018-11-26 21:15:49 +00:00
|
|
|
clockid_t,
|
|
|
|
timespec,
|
|
|
|
tm,
|
|
|
|
localtime,
|
|
|
|
time_t,
|
|
|
|
time
|
|
|
|
};
|
|
|
|
use std::{ptr, slice, mem};
|
2018-11-26 21:30:55 +00:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
2018-11-26 20:28:20 +00:00
|
|
|
|
|
|
|
use crate::webassembly::Instance;
|
|
|
|
|
|
|
|
/// emscripten: _gettimeofday
|
2018-11-26 21:45:38 +00:00
|
|
|
pub extern fn _gettimeofday(tp: c_int, tz: c_int, instance: &mut Instance) -> c_int {
|
|
|
|
#[repr(C)]
|
|
|
|
struct GuestTimeVal {
|
|
|
|
tv_sec: i32,
|
|
|
|
tv_usec: i32,
|
|
|
|
}
|
2018-11-26 20:28:20 +00:00
|
|
|
|
2018-11-26 21:45:38 +00:00
|
|
|
assert!(tz == 0, "the timezone argument of `_gettimeofday` must be null");
|
2018-11-26 20:28:20 +00:00
|
|
|
unsafe {
|
2018-11-26 21:45:38 +00:00
|
|
|
let now = SystemTime::now();
|
|
|
|
let since_epoch = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
|
|
|
|
let timeval_struct_ptr = instance.memory_offset_addr(0, tp as _) as *mut GuestTimeVal;
|
2018-11-26 20:28:20 +00:00
|
|
|
|
2018-11-26 21:45:38 +00:00
|
|
|
(*timeval_struct_ptr).tv_sec = since_epoch.as_secs() as _;
|
|
|
|
(*timeval_struct_ptr).tv_usec = since_epoch.subsec_nanos() as _;
|
2018-11-26 20:28:20 +00:00
|
|
|
}
|
2018-11-26 21:45:38 +00:00
|
|
|
0
|
2018-11-26 20:28:20 +00:00
|
|
|
}
|
2018-11-26 20:42:47 +00:00
|
|
|
|
2018-11-26 20:28:20 +00:00
|
|
|
|
|
|
|
/// emscripten: _clock_gettime
|
2018-11-26 21:45:38 +00:00
|
|
|
pub extern fn _clock_gettime(clk_id: c_int, tp: c_int, instance: &mut Instance) -> c_int {
|
|
|
|
#[repr(C)]
|
|
|
|
struct GuestTimeSpec {
|
|
|
|
tv_sec: i32,
|
|
|
|
tv_nsec: i32,
|
|
|
|
}
|
2018-11-26 20:28:20 +00:00
|
|
|
|
|
|
|
unsafe {
|
2018-11-26 21:45:38 +00:00
|
|
|
let mut timespec = timespec {
|
|
|
|
tv_sec: 0,
|
|
|
|
tv_nsec: 0,
|
|
|
|
};
|
|
|
|
let ret = libc_clock_gettime(clk_id as _, &mut timespec);
|
|
|
|
if ret != 0 {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
let timespec_struct_ptr = instance.memory_offset_addr(0, tp as _) as *mut GuestTimeSpec;
|
|
|
|
(*timespec_struct_ptr).tv_sec = timespec.tv_sec as _;
|
|
|
|
(*timespec_struct_ptr).tv_nsec = timespec.tv_nsec as _;
|
2018-11-26 20:28:20 +00:00
|
|
|
}
|
2018-11-26 21:45:38 +00:00
|
|
|
0
|
2018-11-26 20:28:20 +00:00
|
|
|
}
|
2018-11-26 21:15:49 +00:00
|
|
|
|
|
|
|
/// emscripten: _localtime
|
|
|
|
pub extern "C" fn _localtime(time_p: u32, instance: &mut Instance) -> *mut tm {
|
|
|
|
debug!("emscripten::_localtime {}", time_p);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let time_p_addr = instance.memory_offset_addr(0, time_p as _) as *mut i64;
|
|
|
|
localtime(time_p_addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// emscripten: _time
|
|
|
|
pub extern "C" fn _time(time_p: u32, instance: &mut Instance) -> time_t {
|
|
|
|
debug!("emscripten::_time {}", time_p);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let time_p_addr = instance.memory_offset_addr(0, time_p as _) as *mut i64;
|
|
|
|
time(time_p_addr)
|
|
|
|
}
|
|
|
|
}
|