From 4f80d7013ea035dd93fa88d0ef2102b41d6552c5 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Fri, 14 Apr 2023 14:42:18 -0600 Subject: [PATCH] add wasmbind feature --- vm/Cargo.toml | 2 ++ vm/src/stdlib/time.rs | 20 ++++++++++++-------- vm/src/vm/vm_object.rs | 19 +++++++++++-------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 251d482eb..33a1af586 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -23,6 +23,8 @@ ast = ["rustpython-ast"] codegen = ["rustpython-codegen", "ast"] parser = ["rustpython-parser", "ast"] serde = ["dep:serde"] +wasmbind = ["chrono/wasmbind", "getrandom/js"] + [dependencies] rustpython-compiler = { workspace = true, optional = true } diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index d026f2953..75434c2f2 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -104,15 +104,19 @@ mod decl { #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] fn _time(_vm: &VirtualMachine) -> PyResult { - use wasm_bindgen::prelude::*; - #[wasm_bindgen] - extern "C" { - type Date; - #[wasm_bindgen(static_method_of = Date)] - fn now() -> f64; + #[cfg(feature = "wasmbind")] + { + 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 + return Ok(Date::now() / 1000.0); } - // Date.now returns unix time in milliseconds, we want it in seconds - Ok(Date::now() / 1000.0) + panic!("Date not available") } #[pyfunction] diff --git a/vm/src/vm/vm_object.rs b/vm/src/vm/vm_object.rs index e3d143688..8003ac4ae 100644 --- a/vm/src/vm/vm_object.rs +++ b/vm/src/vm/vm_object.rs @@ -21,15 +21,18 @@ impl VirtualMachine { } #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] { - use wasm_bindgen::prelude::*; - #[wasm_bindgen] - extern "C" { - #[wasm_bindgen(js_namespace = console)] - fn error(s: &str); + #[cfg(feature = "wasmbind")] + { + use wasm_bindgen::prelude::*; + #[wasm_bindgen] + extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn error(s: &str); + } + let mut s = String::new(); + self.write_exception(&mut s, &exc).unwrap(); + error(&s); } - let mut s = String::new(); - self.write_exception(&mut s, &exc).unwrap(); - error(&s); panic!("{}; exception backtrace above", msg) } }