diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 48d43e873a..bbbf43682d 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -22,6 +22,8 @@ ast = ["rustpython-ast"] codegen = ["rustpython-codegen", "ast"] parser = ["rustpython-parser", "ast"] serde = ["dep:serde"] +wasmbind = ["chrono/wasmbind", "getrandom/js"] + [dependencies] rustpython-compiler = { path = "../compiler", optional = true, version = "0.2.0" } @@ -64,7 +66,7 @@ thiserror = { workspace = true } thread_local = { workspace = true } caseless = "0.2.1" -getrandom = { version = "0.2.6", features = ["js"] } +getrandom = { version = "0.2.6", features = ["custom"] } flamer = { version = "0.4", optional = true } half = "1.8.2" is-macro = "0.2.0" diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 151b97acf3..8010357c98 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -84,12 +84,16 @@ mod time { _time(vm) } - #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] + #[cfg(any( + not(target_arch = "wasm32"), + target_os = "wasi", + not(feature = "wasmbind") + ))] fn _time(vm: &VirtualMachine) -> PyResult { Ok(duration_since_system_now(vm)?.as_secs_f64()) } - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] + #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] fn _time(_vm: &VirtualMachine) -> PyResult { use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -99,7 +103,7 @@ mod time { fn now() -> f64; } // Date.now returns unix time in milliseconds, we want it in seconds - Ok(Date::now() / 1000.0) + return Ok(Date::now() / 1000.0); } #[pyfunction] diff --git a/vm/src/vm/vm_object.rs b/vm/src/vm/vm_object.rs index 056ded9bfd..6d64fc694e 100644 --- a/vm/src/vm/vm_object.rs +++ b/vm/src/vm/vm_object.rs @@ -12,7 +12,7 @@ impl VirtualMachine { #[track_caller] #[cold] fn _py_panic_failed(&self, exc: PyBaseExceptionRef, msg: &str) -> ! { - #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))] + #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] { let show_backtrace = std::env::var_os("RUST_BACKTRACE").map_or(cfg!(target_os = "wasi"), |v| &v != "0"); @@ -24,7 +24,7 @@ impl VirtualMachine { }; panic!("{msg}; {after}") } - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] + #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] { use wasm_bindgen::prelude::*; #[wasm_bindgen]