Merge pull request #1311 from RustPython/coolreader18/syscall-settings

Allow time.time() to work on WASM
This commit is contained in:
Windel Bouwman
2019-08-27 08:01:08 +02:00
committed by GitHub
3 changed files with 28 additions and 17 deletions

1
Cargo.lock generated
View File

@@ -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)",
]

View File

@@ -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"

View File

@@ -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) {