add wasmbind feature

This commit is contained in:
Benjamin DeMann authored and Jeong YunWon committed 2024-08-09 00:29:25 +09:00
1 parent 2919df1df5
commit 4f80d7013e
3 files changed
+25 -16

No files matched your search

+2
View File
@@ -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 }
+12 -8
View File
@@ -104,15 +104,19 @@ mod decl {
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
fn _time(_vm: &VirtualMachine) -> PyResult<f64> {
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]
+11 -8
View File
@@ -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)
}
}