Skip to content

Commit 4f80d70

Browse files
bdemannyouknowone
authored andcommitted
add wasmbind feature
1 parent 2919df1 commit 4f80d70

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

vm/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ ast = ["rustpython-ast"]
2323
codegen = ["rustpython-codegen", "ast"]
2424
parser = ["rustpython-parser", "ast"]
2525
serde = ["dep:serde"]
26+
wasmbind = ["chrono/wasmbind", "getrandom/js"]
27+
2628

2729
[dependencies]
2830
rustpython-compiler = { workspace = true, optional = true }

vm/src/stdlib/time.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,19 @@ mod decl {
104104

105105
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
106106
fn _time(_vm: &VirtualMachine) -> PyResult<f64> {
107-
use wasm_bindgen::prelude::*;
108-
#[wasm_bindgen]
109-
extern "C" {
110-
type Date;
111-
#[wasm_bindgen(static_method_of = Date)]
112-
fn now() -> f64;
107+
#[cfg(feature = "wasmbind")]
108+
{
109+
use wasm_bindgen::prelude::*;
110+
#[wasm_bindgen]
111+
extern "C" {
112+
type Date;
113+
#[wasm_bindgen(static_method_of = Date)]
114+
fn now() -> f64;
115+
}
116+
// Date.now returns unix time in milliseconds, we want it in seconds
117+
return Ok(Date::now() / 1000.0);
113118
}
114-
// Date.now returns unix time in milliseconds, we want it in seconds
115-
Ok(Date::now() / 1000.0)
119+
panic!("Date not available")
116120
}
117121

118122
#[pyfunction]

vm/src/vm/vm_object.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ impl VirtualMachine {
2121
}
2222
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
2323
{
24-
use wasm_bindgen::prelude::*;
25-
#[wasm_bindgen]
26-
extern "C" {
27-
#[wasm_bindgen(js_namespace = console)]
28-
fn error(s: &str);
24+
#[cfg(feature = "wasmbind")]
25+
{
26+
use wasm_bindgen::prelude::*;
27+
#[wasm_bindgen]
28+
extern "C" {
29+
#[wasm_bindgen(js_namespace = console)]
30+
fn error(s: &str);
31+
}
32+
let mut s = String::new();
33+
self.write_exception(&mut s, &exc).unwrap();
34+
error(&s);
2935
}
30-
let mut s = String::new();
31-
self.write_exception(&mut s, &exc).unwrap();
32-
error(&s);
3336
panic!("{}; exception backtrace above", msg)
3437
}
3538
}

0 commit comments

Comments
 (0)