diff --git a/Cargo.lock b/Cargo.lock index d23ccdd223c..f5504e8c14d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2492,6 +2492,7 @@ dependencies = [ "wasmer-dev-utils 0.4.2", "wasmer-runtime-core 0.4.2", "wasmer-singlepass-backend 0.4.2", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/lib/wasi/Cargo.toml b/lib/wasi/Cargo.toml index efd64270e99..e59b45f96b2 100644 --- a/lib/wasi/Cargo.toml +++ b/lib/wasi/Cargo.toml @@ -19,6 +19,7 @@ log = "0.4.6" byteorder = "1.3.1" # hack to get tests to work wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.4.2", optional = true } +winapi = "0.3" [build-dependencies] glob = "0.2.11" diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index 8621dc55e8b..2ebf294b31e 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -2,6 +2,8 @@ #[macro_use] extern crate log; +#[cfg(target = "windows")] +extern crate winapi; #[macro_use] mod macros; diff --git a/lib/wasi/src/syscalls/windows.rs b/lib/wasi/src/syscalls/windows.rs index 6273695d322..f6559290bbc 100644 --- a/lib/wasi/src/syscalls/windows.rs +++ b/lib/wasi/src/syscalls/windows.rs @@ -5,7 +5,22 @@ pub fn platform_clock_res_get( clock_id: __wasi_clockid_t, resolution: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { - __WASI_EINVAL + let resolution_val = match clock_id { + // resolution of monotonic clock at 10ms, from: + // https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-gettickcount64 + __WASI_CLOCK_MONOTONIC => 10_000_000, + // TODO: verify or compute this + __WASI_CLOCK_REALTIME => 1, + __WASI_CLOCK_PROCESS_CPUTIME_ID => { + return __WASI_EINVAL; + } + __WASI_CLOCK_THREAD_CPUTIME_ID => { + return __WASI_EINVAL; + } + _ => return __WASI_EINVAL, + }; + resolution.set(resolution_val); + __WASI_ESUCCESS } pub fn platform_clock_time_get( @@ -13,5 +28,28 @@ pub fn platform_clock_time_get( precision: __wasi_timestamp_t, time: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { - unimplemented!() + let nanos = match clock_id { + __WASI_CLOCK_MONOTONIC => { + let tick_ms = unsafe { winapi::um::sysinfoapi::GetTickCount64() }; + tick_ms * 1_000_000 + } + __WASI_CLOCK_REALTIME => { + let duration = wasi_try!(std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_err(|e| { + debug!("Error in wasi::platform_clock_time_get: {:?}", e); + __WASI_EIO + })); + duration.as_nanos() as u64 + } + __WASI_CLOCK_PROCESS_CPUTIME_ID => { + unimplemented!("wasi::platform_clock_time_get(__WASI_CLOCK_PROCESS_CPUTIME_ID, ..)") + } + __WASI_CLOCK_THREAD_CPUTIME_ID => { + unimplemented!("wasi::platform_clock_time_get(__WASI_CLOCK_THREAD_CPUTIME_ID, ..)") + } + _ => return __WASI_EINVAL, + }; + time.set(nanos); + __WASI_ESUCCESS }