From c61f1d20ea14c3e5f9b280b5983612d55eab1504 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Mon, 26 Aug 2019 21:21:31 +0000 Subject: [PATCH] Allow time.time() to work on WASM --- Cargo.lock | 1 + vm/Cargo.toml | 3 +++ vm/src/stdlib/time_module.rs | 41 +++++++++++++++++++++--------------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1fa7a64e2..9af305125 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1210,6 +1210,7 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicode_categories 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "unicode_names2 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "wtf8 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/vm/Cargo.toml b/vm/Cargo.toml index b11abadb7..9b702e4d4 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -83,3 +83,6 @@ num_cpus = "1.0" [target."cfg(windows)".dependencies] kernel32-sys = "0.2.2" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen = "0.2" diff --git a/vm/src/stdlib/time_module.rs b/vm/src/stdlib/time_module.rs index 5d7205489..f5d196b84 100644 --- a/vm/src/stdlib/time_module.rs +++ b/vm/src/stdlib/time_module.rs @@ -5,7 +5,7 @@ use std::fmt; use std::ops::Range; use std::time::{Duration, SystemTime, UNIX_EPOCH}; -use crate::function::{OptionalArg, PyFuncArgs}; +use crate::function::OptionalArg; use crate::obj::objfloat::PyFloatRef; use crate::obj::objint::PyInt; use crate::obj::objsequence::get_sequence_index; @@ -44,38 +44,45 @@ fn time_sleep(seconds: PyFloatRef, vm: &VirtualMachine) -> PyResult<()> { } #[cfg(not(unix))] -fn time_sleep(seconds: PyFloatRef, vm: &VirtualMachine) -> PyResult<()> { +fn time_sleep(seconds: PyFloatRef, _vm: &VirtualMachine) { let seconds = seconds.to_f64(); let secs: u64 = seconds.trunc() as u64; let nanos: u32 = (seconds.fract() * 1e9) as u32; let duration = Duration::new(secs, nanos); std::thread::sleep(duration); - Ok(()) } fn duration_to_f64(d: Duration) -> f64 { (d.as_secs() as f64) + (f64::from(d.subsec_nanos()) / 1e9) } -fn time_time(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!(vm, args); - let x = match SystemTime::now().duration_since(UNIX_EPOCH) { +#[cfg(not(target_arch = "wasm32"))] +fn time_time(_vm: &VirtualMachine) -> f64 { + match SystemTime::now().duration_since(UNIX_EPOCH) { Ok(v) => duration_to_f64(v), - Err(err) => panic!("Error: {:?}", err), - }; - let value = vm.ctx.new_float(x); - Ok(value) + Err(err) => panic!("Time error: {:?}", err), + } } -fn time_monotonic(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!(vm, args); +#[cfg(target_arch = "wasm32")] +fn time_time(_vm: &VirtualMachine) -> f64 { + use wasm_bindgen::prelude::*; + #[wasm_bindgen] + extern "C" { + type Date; + #[wasm_bindgen(static_method_of = Date)] + fn now() -> f64; + } + // Date.now returns unix time in milliseconds, we want it in seconds + Date::now() / 1000.0 +} + +fn time_monotonic(_vm: &VirtualMachine) -> f64 { // TODO: implement proper monotonic time! - let x = match SystemTime::now().duration_since(UNIX_EPOCH) { + match SystemTime::now().duration_since(UNIX_EPOCH) { Ok(v) => duration_to_f64(v), - Err(err) => panic!("Error: {:?}", err), - }; - let value = vm.ctx.new_float(x); - Ok(value) + Err(err) => panic!("Time error: {:?}", err), + } } fn pyfloat_to_secs_and_nanos(seconds: &PyObjectRef) -> (i64, u32) {