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
89
90
91
92
93
94
95
96
97
98
use wasm_bindgen::convert::RefFromWasmAbi;
use wasm_bindgen::prelude::*;
pub use workflow_wasm_macros::{ref_from_abi, ref_from_abi_as_option, TryFromJsValue};
pub unsafe fn ref_from_abi<T>(js: &JsValue) -> std::result::Result<T, JsValue>
where
T: RefFromWasmAbi<Abi = u32> + Clone,
{
if !js.is_object() {
return Err(JsValue::from(
"ref_from_abi_safe(): supplied argument is not a class object",
));
}
let ptr = ::js_sys::Reflect::get(js, &JsValue::from_str("ptr"))?;
if ptr.is_falsy() {
return Err(JsValue::from(
"ref_from_abi(): WASM API received an invalid JavaScript object (the object is not a WASM ABI pointer)",
));
}
let ptr_u32: u32 = ptr.as_f64().ok_or(JsValue::from(
"reg_from_abi(): WASM API received an invalid JavaScript object (the object is not a WASM ABI pointer)",
))? as u32;
let instance_ref = unsafe { T::ref_from_abi(ptr_u32) };
Ok(instance_ref.clone())
}
pub fn ref_from_abi_safe<T>(class: &str, js: &JsValue) -> std::result::Result<T, JsValue>
where
T: RefFromWasmAbi<Abi = u32> + Clone,
{
if !js.is_object() {
return Err(JsValue::from(format!(
"ref_from_abi_safe(): supplied argument is not of expected class type `{class}`"
)));
}
let ctor = ::js_sys::Reflect::get(js, &JsValue::from_str("constructor"))?;
if ctor.is_falsy() {
return Err(JsValue::from(format!("ref_from_abi_safe(): unable to obtain object constructor (for expected class `{class}`)")));
} else {
let name = ::js_sys::Reflect::get(&ctor, &JsValue::from_str("name"))?;
if name.is_falsy() {
return Err(JsValue::from(
format!("ref_from_abi_safe(): unable to obtain object constructor name (for expected class `{class}`)")
));
} else {
let name = name.as_string().ok_or(JsValue::from(format!("ref_from_abi_safe(): unable to obtain object constructor name (for expected class `{class}`)")))?;
if name != class {
return Err(JsValue::from(format!(
"ref_from_abi_safe(): object constructor `{name}` does not match expected class `{class}`"
)));
}
}
}
let ptr = ::js_sys::Reflect::get(js, &::wasm_bindgen::JsValue::from_str("ptr"))?;
if ptr.is_falsy() {
return Err(JsValue::from(
"ref_from_abi(): WASM API received an invalid JavaScript object (the object is not a WASM ABI pointer)",
));
}
let ptr_u32: u32 = ptr.as_f64().ok_or(::wasm_bindgen::JsValue::from(
"reg_from_abi(): WASM API received an invalid JavaScript object (the object is not a WASM ABI pointer)",
))? as u32;
let instance_ref = unsafe { T::ref_from_abi(ptr_u32) };
Ok(instance_ref.clone())
}
pub fn ref_from_abi_safe_as_option<T>(
class: &str,
js: &JsValue,
) -> std::result::Result<Option<T>, JsValue>
where
T: RefFromWasmAbi<Abi = u32> + Clone,
{
if !js.is_undefined() && !js.is_null() {
Ok(Some(ref_from_abi_safe::<T>(class, js)?))
} else {
Ok(None)
}
}