fix: avoid bundler change the classname (#301)

This commit is contained in:
Zixuan Chen 2024-03-31 01:11:55 +08:00 committed by GitHub
parent 422e442012
commit 3fe9cc1969
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -22,36 +22,45 @@ pub(crate) fn js_to_container(js: JsContainer) -> Result<Container, JsValue> {
js js
))); )));
} }
let ctor_name = Object::get_prototype_of(&js).constructor().name();
let kind_method = Reflect::get(&js, &JsValue::from_str("kind"));
let kind = match kind_method {
Ok(kind_method) if kind_method.is_function() => {
let kind_string = js_sys::Function::from(kind_method).call0(&js);
match kind_string {
Ok(kind_string) if kind_string.is_string() => kind_string.as_string().unwrap(),
_ => return Err(JsValue::from_str("kind() did not return a string")),
}
}
_ => return Err(JsValue::from_str("No kind method found or not a function")),
};
let Ok(ptr) = Reflect::get(&js, &JsValue::from_str("__wbg_ptr")) else { let Ok(ptr) = Reflect::get(&js, &JsValue::from_str("__wbg_ptr")) else {
return Err(JsValue::from_str("Cannot find pointer field")); return Err(JsValue::from_str("Cannot find pointer field"));
}; };
let ptr_u32: u32 = ptr.as_f64().unwrap() as u32; let ptr_u32: u32 = ptr.as_f64().unwrap() as u32;
let ctor_name = ctor_name let container = match kind.as_str() {
.as_string() "Text" => {
.ok_or(JsValue::from_str("Constructor name is not a string"))?;
let container = match ctor_name.as_str() {
"LoroText" => {
let obj = unsafe { LoroText::ref_from_abi(ptr_u32) }; let obj = unsafe { LoroText::ref_from_abi(ptr_u32) };
Container::Text(obj.clone()) Container::Text(obj.clone())
} }
"LoroMap" => { "Map" => {
let obj = unsafe { LoroMap::ref_from_abi(ptr_u32) }; let obj = unsafe { LoroMap::ref_from_abi(ptr_u32) };
Container::Map(obj.clone()) Container::Map(obj.clone())
} }
"LoroList" => { "List" => {
let obj = unsafe { LoroList::ref_from_abi(ptr_u32) }; let obj = unsafe { LoroList::ref_from_abi(ptr_u32) };
Container::List(obj.clone()) Container::List(obj.clone())
} }
"LoroTree" => { "Tree" => {
let obj = unsafe { LoroTree::ref_from_abi(ptr_u32) }; let obj = unsafe { LoroTree::ref_from_abi(ptr_u32) };
Container::Tree(obj.clone()) Container::Tree(obj.clone())
} }
_ => { _ => {
return Err(JsValue::from_str( return Err(JsValue::from_str(
format!( format!(
"Value ctor_name is {} but the valid container name is LoroMap, LoroList, LoroText or LoroTree", "Value kind is {} but the valid container name is Map, List, Text or Tree",
ctor_name kind
) )
.as_str(), .as_str(),
)); ));