Skip to content

Commit aaaa398

Browse files
author
Felix Hilgers
committed
test(RefFromWasmAbi): roundtrip
1 parent 67e7ca0 commit aaaa398

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/wasm.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use serde::{Deserialize, Serialize};
22
use tsify_next::Tsify;
3+
use wasm_bindgen::prelude::*;
34
use wasm_bindgen_test::wasm_bindgen_test;
45

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

1920
assert_eq!(Unit::from_js(js).unwrap(), Unit);
2021
}
22+
23+
#[derive(Debug, PartialEq, Serialize, Deserialize, Tsify, Clone)]
24+
#[tsify(into_wasm_abi, from_wasm_abi)]
25+
struct SimpleData {
26+
value: i32,
27+
text: String,
28+
}
29+
30+
#[wasm_bindgen(inline_js = r#"
31+
function validate(value, validation) {
32+
validation(value);
33+
34+
// Validate twice to make sure the value is not moved in any way to rust
35+
validation(value);
36+
}
37+
38+
module.exports = { validate };
39+
"#)]
40+
extern "C" {
41+
#[wasm_bindgen(catch, js_name = "validate")]
42+
pub fn validate_simple_data(
43+
value: SimpleData,
44+
validation: &dyn Fn(SimpleData),
45+
) -> Result<(), JsValue>;
46+
47+
#[wasm_bindgen(catch, js_name = "validate")]
48+
pub fn validate_simple_data_ref(
49+
value: SimpleData,
50+
validation: &dyn Fn(&SimpleData),
51+
) -> Result<(), JsValue>;
52+
}
53+
54+
#[wasm_bindgen_test]
55+
fn test_convert_simple_value_type() {
56+
let val = SimpleData {
57+
value: 42,
58+
text: "Hello".to_string(),
59+
};
60+
61+
validate_simple_data(val.clone(), &|val_after| {
62+
assert_eq!(val_after, val);
63+
})
64+
.unwrap_throw();
65+
66+
validate_simple_data_ref(val.clone(), &|val_after| {
67+
assert_eq!(val_after, &val);
68+
})
69+
.unwrap_throw();
70+
}

0 commit comments

Comments
 (0)