From 48fa419b64d72446b065465cc88fecc9552790ff Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Fri, 14 Apr 2023 14:42:18 -0600 Subject: [PATCH 1/5] 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 48d43e873a..26f32c9084 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" } diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 151b97acf3..232483bdbd 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -91,15 +91,19 @@ mod time { #[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 056ded9bfd..39591df666 100644 --- a/vm/src/vm/vm_object.rs +++ b/vm/src/vm/vm_object.rs @@ -26,15 +26,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) } } From 5b89a3f5ffa2c85e2a16460227d0415fa31d29ca Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 19 Apr 2023 09:09:39 -0600 Subject: [PATCH 2/5] address pr notes --- vm/src/stdlib/time.rs | 22 +++++++++------------- vm/src/vm/vm_object.rs | 21 +++++++++------------ 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 232483bdbd..41fccb7057 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -89,21 +89,17 @@ mod time { 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 { - #[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); + use wasm_bindgen::prelude::*; + #[wasm_bindgen] + extern "C" { + type Date; + #[wasm_bindgen(static_method_of = Date)] + fn now() -> f64; } - panic!("Date not available") + // Date.now returns unix time in milliseconds, we want it in seconds + return Ok(Date::now() / 1000.0); } #[pyfunction] diff --git a/vm/src/vm/vm_object.rs b/vm/src/vm/vm_object.rs index 39591df666..5491f0034c 100644 --- a/vm/src/vm/vm_object.rs +++ b/vm/src/vm/vm_object.rs @@ -24,20 +24,17 @@ 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"))] { - #[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); + 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); panic!("{}; exception backtrace above", msg) } } From 578f206aaf1d944d3b5d5379c9c1b73b165dc327 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 19 Apr 2023 10:10:25 -0600 Subject: [PATCH 3/5] run the first block if wasmbind is not present --- vm/src/vm/vm_object.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/vm_object.rs b/vm/src/vm/vm_object.rs index 5491f0034c..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"); From 3f1261ce0f452469017671329cb4bda6592111dc Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 19 Apr 2023 10:21:15 -0600 Subject: [PATCH 4/5] add cfg for not wasmbind for time --- vm/src/stdlib/time.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 41fccb7057..8010357c98 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -84,7 +84,11 @@ 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()) } From e2c63972a2660f1fdcc8f470d2f41c6ae2017923 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 19 Apr 2023 11:08:43 -0600 Subject: [PATCH 5/5] enable js feature of getrandom only for wasmbind --- vm/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 26f32c9084..bbbf43682d 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -66,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"