1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::utils::*;
use js_sys::{Array, Object, Reflect};
use wasm_bindgen::prelude::*;
pub trait ObjectTrait {
fn get(&self, prop: &str) -> Result<JsValue, JsValue>;
fn get_string(&self, prop: &str) -> Result<String, JsValue>;
fn get_u64(&self, prop: &str) -> Result<u64, JsValue>;
fn get_f64(&self, prop: &str) -> Result<f64, JsValue>;
fn get_bool(&self, prop: &str) -> Result<bool, JsValue>;
fn get_vec(&self, prop: &str) -> Result<Vec<JsValue>, JsValue>;
fn get_vec_u8(&self, prop: &str) -> Result<Vec<u8>, JsValue>;
fn set(&self, prop: &str, value: &JsValue) -> Result<bool, JsValue>;
fn set_vec(&self, prop: &str, values: &[JsValue]) -> Result<bool, JsValue>;
fn set_properties(&self, props: &[(&str, &JsValue)]) -> Result<(), JsValue>;
fn delete(&self, prop: &str) -> Result<bool, JsValue>;
}
impl ObjectTrait for Object {
fn get(&self, prop: &str) -> Result<JsValue, JsValue> {
Reflect::get(self, &JsValue::from(prop))
}
fn get_string(&self, prop: &str) -> Result<String, JsValue> {
try_get_string_from_prop(self, prop)
}
fn get_u64(&self, prop: &str) -> Result<u64, JsValue> {
try_get_u64_from_prop(self, prop)
}
fn get_bool(&self, prop: &str) -> Result<bool, JsValue> {
try_get_bool_from_prop(self, prop)
}
fn get_vec(&self, prop: &str) -> Result<Vec<JsValue>, JsValue> {
let v = Reflect::get(self, &JsValue::from(prop))?;
let array = v.dyn_into::<Array>()?;
Ok(array.to_vec())
}
fn get_vec_u8(&self, prop: &str) -> Result<Vec<u8>, JsValue> {
try_get_vec_u8_from_prop(self, prop)
}
fn get_f64(&self, prop: &str) -> Result<f64, JsValue> {
try_get_f64_from_prop(self, prop)
}
fn set(&self, prop: &str, value: &JsValue) -> Result<bool, JsValue> {
Reflect::set(self, &JsValue::from(prop), value)
}
fn set_vec(&self, prop: &str, values: &[JsValue]) -> Result<bool, JsValue> {
let array = js_sys::Array::new();
for v in values {
array.push(v);
}
Reflect::set(self, &JsValue::from(prop), &array)
}
fn set_properties(&self, props: &[(&str, &JsValue)]) -> Result<(), JsValue> {
for (k, v) in props.iter() {
Reflect::set(self, &JsValue::from(*k), v)?;
}
Ok(())
}
fn delete(&self, prop: &str) -> Result<bool, JsValue> {
Reflect::delete_property(self, &JsValue::from(prop))
}
}