Skip to content

Commit c4ecc0d

Browse files
committed
add wasmbind feature
1 parent 7483ff7 commit c4ecc0d

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

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

2628
[dependencies]
2729
rustpython-compiler = { path = "../compiler", optional = true, version = "0.2.0" }

vm/src/stdlib/time.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,19 @@ mod time {
9191

9292
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
9393
fn _time(_vm: &VirtualMachine) -> PyResult<f64> {
94-
// use wasm_bindgen::prelude::*;
95-
// #[wasm_bindgen]
96-
// extern "C" {
97-
// type Date;
98-
// #[wasm_bindgen(static_method_of = Date)]
99-
// fn now() -> f64;
100-
// }
101-
// Date.now returns unix time in milliseconds, we want it in seconds
102-
Ok(ic_cdk::api::time() as f64 / 1_000_000_000 as f64)
94+
#[cfg(feature = "wasmbind")]
95+
{
96+
use wasm_bindgen::prelude::*;
97+
#[wasm_bindgen]
98+
extern "C" {
99+
type Date;
100+
#[wasm_bindgen(static_method_of = Date)]
101+
fn now() -> f64;
102+
}
103+
// Date.now returns unix time in milliseconds, we want it in seconds
104+
return Ok(Date::now() / 1000.0);
105+
}
106+
panic!{"No date available"}
103107
}
104108

105109
#[pyfunction]

vm/src/vm/vm_object.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ impl VirtualMachine {
2626
}
2727
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
2828
{
29-
// use wasm_bindgen::prelude::*;
30-
// #[wasm_bindgen]
31-
// extern "C" {
32-
// #[wasm_bindgen(js_namespace = console)]
33-
// fn error(s: &str);
34-
// }
35-
// let mut s = String::new();
36-
// self.write_exception(&mut s, &exc).unwrap();
37-
// error(&s);
29+
#[cfg(feature = "wasmbind")]
30+
{
31+
use wasm_bindgen::prelude::*;
32+
#[wasm_bindgen]
33+
extern "C" {
34+
#[wasm_bindgen(js_namespace = console)]
35+
fn error(s: &str);
36+
}
37+
let mut s = String::new();
38+
self.write_exception(&mut s, &exc).unwrap();
39+
error(&s);
40+
}
3841
panic!("{}; exception backtrace above", msg)
3942
}
4043
}

0 commit comments

Comments
 (0)