From 8c9fb2c7754e182f79e69ee09e2284b16d472dc9 Mon Sep 17 00:00:00 2001 From: Noa <33094578+coolreader18@users.noreply.github.com> Date: Thu, 6 Jan 2022 16:27:07 -0600 Subject: [PATCH] Fix unsatisfied times import on wasi --- Cargo.lock | 4 ++-- vm/Cargo.toml | 2 +- vm/src/stdlib/time.rs | 21 +++++++++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ee04733e..1d1fd5fb2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -993,9 +993,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.107" +version = "0.2.112" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbe5e23404da5b4f555ef85ebed98fb4083e55a00c317800bc2a50ede9f3d219" +checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" [[package]] name = "libffi" diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 3989b5648..22a6ad985 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -48,7 +48,7 @@ hexf-parse = "0.2.1" indexmap = "1.0.2" ahash = "0.7.2" bitflags = "1.3" -libc = "0.2.99" +libc = "0.2.107" nix = "0.23" paste = "1.0.5" is-macro = "0.1" diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 0af5d219f..940f2197c 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -248,7 +248,7 @@ mod time { Ok(get_thread_time(vm)?.as_nanos() as u64) } - #[cfg(any(windows, all(target_arch = "wasm32", not(target_os = "unknown"))))] + #[cfg(any(windows, all(target_arch = "wasm32", target_arch = "emscripten")))] pub(super) fn time_muldiv(ticks: i64, mul: i64, div: i64) -> u64 { let intpart = ticks / div; let ticks = ticks % div; @@ -256,7 +256,7 @@ mod time { (intpart * mul + remaining) as u64 } - #[cfg(all(target_arch = "wasm32", not(target_os = "unknown")))] + #[cfg(all(target_arch = "wasm32", target_os = "emscripten"))] fn get_process_time(vm: &VirtualMachine) -> PyResult { let t: libc::tms = unsafe { let mut t = std::mem::MaybeUninit::uninit(); @@ -265,10 +265,6 @@ mod time { } t.assume_init() }; - - #[cfg(target_os = "wasi")] - let freq = 60; - #[cfg(not(target_os = "wasi"))] let freq = unsafe { libc::sysconf(libc::_SC_CLK_TCK) }; Ok(Duration::from_nanos( @@ -276,6 +272,19 @@ mod time { )) } + // same as the get_process_time impl for most unixes + #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] + pub(super) fn get_process_time(vm: &VirtualMachine) -> PyResult { + let time: libc::timespec = unsafe { + let mut time = std::mem::MaybeUninit::uninit(); + if libc::clock_gettime(libc::CLOCK_PROCESS_CPUTIME_ID, time.as_mut_ptr()) == -1 { + return Err(vm.new_os_error("Failed to get clock time".to_owned())); + } + time.assume_init() + }; + Ok(Duration::new(time.tv_sec as u64, time.tv_nsec as u32)) + } + #[cfg(not(any( windows, target_os = "macos",