Skip to content

add wasmbind feature #4875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 7 additions & 3 deletions vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
))]
Comment on lines +87 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "wasi",
not(feature = "wasmbind")
))]
#[cfg(not(all(
target_arch = "wasm32",
feature = "wasmbind",
not(target_os = "wasi")
)))]

The condition is looking confusing, which actually is negation of the below

fn _time(vm: &VirtualMachine) -> PyResult<f64> {
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<f64> {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Ok(Date::now() / 1000.0);
Ok(Date::now() / 1000.0)

}

#[pyfunction]
Expand Down
4 changes: 2 additions & 2 deletions vm/src/vm/vm_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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]
Expand Down