Skip to content

Commit c61f1d2

Browse files
committed
Allow time.time() to work on WASM
1 parent 9168455 commit c61f1d2

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ num_cpus = "1.0"
8383

8484
[target."cfg(windows)".dependencies]
8585
kernel32-sys = "0.2.2"
86+
87+
[target.'cfg(target_arch = "wasm32")'.dependencies]
88+
wasm-bindgen = "0.2"

vm/src/stdlib/time_module.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt;
55
use std::ops::Range;
66
use std::time::{Duration, SystemTime, UNIX_EPOCH};
77

8-
use crate::function::{OptionalArg, PyFuncArgs};
8+
use crate::function::OptionalArg;
99
use crate::obj::objfloat::PyFloatRef;
1010
use crate::obj::objint::PyInt;
1111
use crate::obj::objsequence::get_sequence_index;
@@ -44,38 +44,45 @@ fn time_sleep(seconds: PyFloatRef, vm: &VirtualMachine) -> PyResult<()> {
4444
}
4545

4646
#[cfg(not(unix))]
47-
fn time_sleep(seconds: PyFloatRef, vm: &VirtualMachine) -> PyResult<()> {
47+
fn time_sleep(seconds: PyFloatRef, _vm: &VirtualMachine) {
4848
let seconds = seconds.to_f64();
4949
let secs: u64 = seconds.trunc() as u64;
5050
let nanos: u32 = (seconds.fract() * 1e9) as u32;
5151
let duration = Duration::new(secs, nanos);
5252
std::thread::sleep(duration);
53-
Ok(())
5453
}
5554

5655
fn duration_to_f64(d: Duration) -> f64 {
5756
(d.as_secs() as f64) + (f64::from(d.subsec_nanos()) / 1e9)
5857
}
5958

60-
fn time_time(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
61-
arg_check!(vm, args);
62-
let x = match SystemTime::now().duration_since(UNIX_EPOCH) {
59+
#[cfg(not(target_arch = "wasm32"))]
60+
fn time_time(_vm: &VirtualMachine) -> f64 {
61+
match SystemTime::now().duration_since(UNIX_EPOCH) {
6362
Ok(v) => duration_to_f64(v),
64-
Err(err) => panic!("Error: {:?}", err),
65-
};
66-
let value = vm.ctx.new_float(x);
67-
Ok(value)
63+
Err(err) => panic!("Time error: {:?}", err),
64+
}
6865
}
6966

70-
fn time_monotonic(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
71-
arg_check!(vm, args);
67+
#[cfg(target_arch = "wasm32")]
68+
fn time_time(_vm: &VirtualMachine) -> f64 {
69+
use wasm_bindgen::prelude::*;
70+
#[wasm_bindgen]
71+
extern "C" {
72+
type Date;
73+
#[wasm_bindgen(static_method_of = Date)]
74+
fn now() -> f64;
75+
}
76+
// Date.now returns unix time in milliseconds, we want it in seconds
77+
Date::now() / 1000.0
78+
}
79+
80+
fn time_monotonic(_vm: &VirtualMachine) -> f64 {
7281
// TODO: implement proper monotonic time!
73-
let x = match SystemTime::now().duration_since(UNIX_EPOCH) {
82+
match SystemTime::now().duration_since(UNIX_EPOCH) {
7483
Ok(v) => duration_to_f64(v),
75-
Err(err) => panic!("Error: {:?}", err),
76-
};
77-
let value = vm.ctx.new_float(x);
78-
Ok(value)
84+
Err(err) => panic!("Time error: {:?}", err),
85+
}
7986
}
8087

8188
fn pyfloat_to_secs_and_nanos(seconds: &PyObjectRef) -> (i64, u32) {

0 commit comments

Comments
 (0)