Skip to content

Commit

Permalink
fix: RefFromWasmAbi should not drop JsType (#33)
Browse files Browse the repository at this point in the history
* fix: RefFromWasmAbi should not drop JsType

* test(RefFromWasmAbi): roundtrip

---------

Co-authored-by: Felix Hilgers <flyxi@posteo.de>
  • Loading branch information
fhilgers and Felix Hilgers authored Apr 23, 2024
1 parent cfc19b8 commit 9e2203b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
6 changes: 5 additions & 1 deletion tests/expand/borrow.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ const _: () = {
type Abi = <JsType as RefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
SelfOwner(Self::from_abi(js))
let result = Self::from_js(&*JsType::ref_from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
};
6 changes: 5 additions & 1 deletion tests/expand/generic_enum.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ const _: () = {
type Abi = <JsType as RefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
SelfOwner(Self::from_abi(js))
let result = Self::from_js(&*JsType::ref_from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
};
12 changes: 10 additions & 2 deletions tests/expand/generic_struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ const _: () = {
type Abi = <JsType as RefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
SelfOwner(Self::from_abi(js))
let result = Self::from_js(&*JsType::ref_from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
};
Expand Down Expand Up @@ -514,7 +518,11 @@ const _: () = {
type Abi = <JsType as RefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
SelfOwner(Self::from_abi(js))
let result = Self::from_js(&*JsType::ref_from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
};
50 changes: 50 additions & 0 deletions tests/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use tsify_next::Tsify;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::wasm_bindgen_test;

#[wasm_bindgen_test]
Expand All @@ -18,3 +19,52 @@ fn test_convert() {

assert_eq!(Unit::from_js(js).unwrap(), Unit);
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Tsify, Clone)]
#[tsify(into_wasm_abi, from_wasm_abi)]
struct SimpleData {
value: i32,
text: String,
}

#[wasm_bindgen(inline_js = r#"
function validate(value, validation) {
validation(value);
// Validate twice to make sure the value is not moved in any way to rust
validation(value);
}
module.exports = { validate };
"#)]
extern "C" {
#[wasm_bindgen(catch, js_name = "validate")]
pub fn validate_simple_data(
value: SimpleData,
validation: &dyn Fn(SimpleData),
) -> Result<(), JsValue>;

#[wasm_bindgen(catch, js_name = "validate")]
pub fn validate_simple_data_ref(
value: SimpleData,
validation: &dyn Fn(&SimpleData),
) -> Result<(), JsValue>;
}

#[wasm_bindgen_test]
fn test_convert_simple_value_type() {
let val = SimpleData {
value: 42,
text: "Hello".to_string(),
};

validate_simple_data(val.clone(), &|val_after| {
assert_eq!(val_after, val);
})
.unwrap_throw();

validate_simple_data_ref(val.clone(), &|val_after| {
assert_eq!(val_after, &val);
})
.unwrap_throw();
}
6 changes: 5 additions & 1 deletion tsify-next-macros/src/wasm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ fn expand_from_wasm_abi(cont: &Container) -> TokenStream {
type Anchor = SelfOwner<Self>;

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
SelfOwner(Self::from_abi(js))
let result = Self::from_js(&*JsType::ref_from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
}
Expand Down

0 comments on commit 9e2203b

Please sign in to comment.