use wasm_bindgen_test::*; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; #[wasm_bindgen(module = "tests/wasm/simple.js")] extern { fn test_add(); fn test_string_arguments(); fn test_return_a_string(); fn test_wrong_types(); fn test_other_exports_still_available(); fn test_jsvalue_typeof(); fn optional_str_none(a: Option<&str>); fn optional_str_some(a: Option<&str>); fn optional_slice_none(a: Option<&[u8]>); fn optional_slice_some(a: Option<&[u8]>); fn optional_string_none(a: Option); fn optional_string_some(a: Option); fn optional_string_some_empty(a: Option); fn return_string_none() -> Option; fn return_string_some() -> Option; fn test_rust_optional(); #[wasm_bindgen(js_name = RenamedInRust)] type Renamed; fn new_renamed() -> Renamed; } #[wasm_bindgen_test] fn add() { test_add(); } #[wasm_bindgen] pub fn simple_add(a: u32, b: u32) -> u32 { a + b } #[wasm_bindgen] pub fn simple_add3(a: u32) -> u32 { a + 3 } #[wasm_bindgen] pub fn simple_get2(_b: bool) -> u32 { 2 } #[wasm_bindgen] pub fn simple_return_and_take_bool(a: bool, b: bool) -> bool { a && b } #[wasm_bindgen] pub fn simple_raw_pointers_work(a: *mut u32, b: *const u8) -> *const u32 { unsafe { (*a) = (*b) as u32; return a } } #[wasm_bindgen_test] fn string_arguments() { test_string_arguments(); } #[wasm_bindgen] pub fn simple_assert_foo_and_bar(a: &str, b: &str) { assert_eq!(a, "foo2"); assert_eq!(b, "bar"); } #[wasm_bindgen] pub fn simple_assert_foo(a: &str) { assert_eq!(a, "foo"); } #[wasm_bindgen_test] fn return_a_string() { test_return_a_string(); } #[wasm_bindgen] pub fn simple_clone(a: &str) -> String { a.to_string() } #[wasm_bindgen] pub fn simple_concat(a: &str, b: &str, c: i8) -> String { format!("{} {} {}", a, b, c) } #[wasm_bindgen_test] fn wrong_types() { test_wrong_types(); } #[wasm_bindgen] pub fn simple_int(_a: u32) {} #[wasm_bindgen] pub fn simple_str(_a: &str) {} #[wasm_bindgen_test] fn other_exports() { test_other_exports_still_available(); } #[no_mangle] pub extern fn foo(_a: u32) { } #[wasm_bindgen_test] fn jsvalue_typeof() { test_jsvalue_typeof(); } #[wasm_bindgen] pub fn is_object(val: &JsValue) -> bool { val.is_object() } #[wasm_bindgen] pub fn is_function(val: &JsValue) -> bool { val.is_function() } #[wasm_bindgen] pub fn is_string(val: &JsValue) -> bool { val.is_string() } #[wasm_bindgen] extern { #[derive(Clone)] type Array; #[wasm_bindgen(constructor)] fn new() -> Array; #[wasm_bindgen(method, catch)] fn standardized_method_this_js_runtime_doesnt_implement_yet(this: &Array) -> Result<(), JsValue>; } #[wasm_bindgen_test] fn binding_to_unimplemented_apis_doesnt_break_everything() { let array = Array::new(); let res = array.standardized_method_this_js_runtime_doesnt_implement_yet(); assert!(res.is_err()); } #[wasm_bindgen_test] fn optional_slices() { optional_str_none(None); optional_str_some(Some("x")); optional_slice_none(None); optional_slice_some(Some(&[1, 2, 3])); optional_string_none(None); optional_string_some_empty(Some(String::new())); optional_string_some(Some("abcd".to_string())); assert_eq!(return_string_none(), None); assert_eq!(return_string_some(), Some("foo".to_string())); test_rust_optional(); } #[wasm_bindgen] pub fn take_optional_str_none(x: Option) { assert!(x.is_none()) } #[wasm_bindgen] pub fn take_optional_str_some(x: Option) { assert_eq!(x, Some(String::from("hello"))); } #[wasm_bindgen] pub fn return_optional_str_none() -> Option { None } #[wasm_bindgen] pub fn return_optional_str_some() -> Option { Some("world".to_string()) } #[wasm_bindgen_test] fn renaming_imports_and_instanceof() { let null = JsValue::NULL; assert!(!null.is_instance_of::()); let arr: JsValue = Array::new().into(); assert!(!arr.is_instance_of::()); let renamed: JsValue = new_renamed().into(); assert!(renamed.is_instance_of::()); }