| 1 |
use std::ptr::{self, NonNull}; |
| 2 |
|
| 3 |
use wasm_bindgen::prelude::*; |
| 4 |
use wasm_bindgen::{intern, unintern}; |
| 5 |
use wasm_bindgen_test::*; |
| 6 |
|
| 7 |
#[wasm_bindgen(module = "tests/wasm/simple.js")] |
| 8 |
extern "C" { |
| 9 |
fn test_add(); |
| 10 |
fn test_string_arguments(); |
| 11 |
fn test_return_a_string(); |
| 12 |
fn test_wrong_types(); |
| 13 |
fn test_other_exports_still_available(); |
| 14 |
fn test_jsvalue_typeof(); |
| 15 |
|
| 16 |
fn optional_str_none(a: Option<&str>); |
| 17 |
fn optional_str_some(a: Option<&str>); |
| 18 |
fn optional_slice_none(a: Option<&[u8]>); |
| 19 |
fn optional_slice_some(a: Option<&[u8]>); |
| 20 |
fn optional_string_none(a: Option<String>); |
| 21 |
fn optional_string_some(a: Option<String>); |
| 22 |
fn optional_string_some_empty(a: Option<String>); |
| 23 |
fn return_string_none() -> Option<String>; |
| 24 |
fn return_string_some() -> Option<String>; |
| 25 |
fn test_rust_optional(); |
| 26 |
#[wasm_bindgen(js_name = import_export_same_name)] |
| 27 |
fn js_import_export_same_name(); |
| 28 |
|
| 29 |
#[wasm_bindgen(js_name = RenamedInRust)] |
| 30 |
type Renamed; |
| 31 |
fn new_renamed() -> Renamed; |
| 32 |
|
| 33 |
fn test_string_roundtrip(); |
| 34 |
|
| 35 |
fn test_raw_pointers(); |
| 36 |
fn test_non_null(); |
| 37 |
} |
| 38 |
|
| 39 |
#[wasm_bindgen_test] |
| 40 |
fn add() { |
| 41 |
test_add(); |
| 42 |
} |
| 43 |
|
| 44 |
#[wasm_bindgen] |
| 45 |
pub fn simple_add(a: u32, b: u32) -> u32 { |
| 46 |
a + b |
| 47 |
} |
| 48 |
|
| 49 |
#[wasm_bindgen] |
| 50 |
pub fn simple_add3(a: u32) -> u32 { |
| 51 |
a + 3 |
| 52 |
} |
| 53 |
|
| 54 |
#[wasm_bindgen] |
| 55 |
pub fn simple_get2(_b: bool) -> u32 { |
| 56 |
2 |
| 57 |
} |
| 58 |
|
| 59 |
#[wasm_bindgen] |
| 60 |
pub fn simple_return_and_take_bool(a: bool, b: bool) -> bool { |
| 61 |
a && b |
| 62 |
} |
| 63 |
|
| 64 |
#[wasm_bindgen] |
| 65 |
pub fn simple_return_raw_pointer_u32(value: u32) -> *mut u32 { |
| 66 |
Box::into_raw(Box::new(value)) |
| 67 |
} |
| 68 |
|
| 69 |
#[wasm_bindgen] |
| 70 |
pub fn simple_return_raw_pointer_u8(value: u8) -> *const u8 { |
| 71 |
Box::into_raw(Box::new(value)) |
| 72 |
} |
| 73 |
|
| 74 |
#[wasm_bindgen] |
| 75 |
pub unsafe fn simple_raw_pointers_work(a: *mut u32, b: *const u8) -> *const u32 { |
| 76 |
(*a) = (*b) as u32; |
| 77 |
a |
| 78 |
} |
| 79 |
|
| 80 |
#[wasm_bindgen] |
| 81 |
pub fn simple_return_option_null_pointer() -> Option<*const u32> { |
| 82 |
Some(ptr::null()) |
| 83 |
} |
| 84 |
|
| 85 |
#[wasm_bindgen] |
| 86 |
pub unsafe fn simple_option_raw_pointers_work( |
| 87 |
a: Option<*mut u32>, |
| 88 |
b: Option<*const u8>, |
| 89 |
) -> Option<*const u32> { |
| 90 |
let a = a.and_then(|ptr| ptr.as_mut()); |
| 91 |
let b = b.and_then(|ptr| ptr.as_ref()); |
| 92 |
|
| 93 |
if let (Some(a), Some(b)) = (a, b) { |
| 94 |
*a = *b as u32; |
| 95 |
Some(a) |
| 96 |
} else { |
| 97 |
None |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
#[wasm_bindgen_test] |
| 102 |
fn raw_pointers() { |
| 103 |
test_raw_pointers(); |
| 104 |
} |
| 105 |
|
| 106 |
#[wasm_bindgen] |
| 107 |
pub fn simple_return_non_null() -> NonNull<u32> { |
| 108 |
NonNull::from(Box::leak(Box::new(42))) |
| 109 |
} |
| 110 |
|
| 111 |
#[wasm_bindgen] |
| 112 |
pub fn simple_return_option_non_null(value: u32) -> Option<NonNull<u32>> { |
| 113 |
Some(NonNull::from(Box::leak(Box::new(value)))) |
| 114 |
} |
| 115 |
|
| 116 |
#[wasm_bindgen] |
| 117 |
pub unsafe fn simple_nonnull_work(a: NonNull<u32>) -> u32 { |
| 118 |
*Box::from_raw(a.as_ptr()) |
| 119 |
} |
| 120 |
|
| 121 |
#[wasm_bindgen] |
| 122 |
pub unsafe fn simple_option_nonnull_work(a: Option<NonNull<u32>>) -> Option<u32> { |
| 123 |
a.map(|ptr| *Box::from_raw(ptr.as_ptr())) |
| 124 |
} |
| 125 |
|
| 126 |
#[wasm_bindgen_test] |
| 127 |
fn non_null() { |
| 128 |
test_non_null(); |
| 129 |
} |
| 130 |
|
| 131 |
#[wasm_bindgen_test] |
| 132 |
fn string_arguments() { |
| 133 |
test_string_arguments(); |
| 134 |
} |
| 135 |
|
| 136 |
#[wasm_bindgen] |
| 137 |
pub fn simple_assert_foo_and_bar(a: &str, b: &str) { |
| 138 |
assert_eq!(a, "foo2"); |
| 139 |
assert_eq!(b, "bar"); |
| 140 |
} |
| 141 |
|
| 142 |
#[wasm_bindgen] |
| 143 |
pub fn simple_assert_foo(a: &str) { |
| 144 |
assert_eq!(a, "foo"); |
| 145 |
} |
| 146 |
|
| 147 |
#[wasm_bindgen_test] |
| 148 |
fn return_a_string() { |
| 149 |
test_return_a_string(); |
| 150 |
} |
| 151 |
|
| 152 |
#[wasm_bindgen] |
| 153 |
pub fn simple_clone(a: &str) -> String { |
| 154 |
a.to_string() |
| 155 |
} |
| 156 |
|
| 157 |
#[wasm_bindgen] |
| 158 |
pub fn simple_concat(a: &str, b: &str, c: i8) -> String { |
| 159 |
format!("{} {} {}", a, b, c) |
| 160 |
} |
| 161 |
|
| 162 |
#[wasm_bindgen_test] |
| 163 |
fn wrong_types() { |
| 164 |
test_wrong_types(); |
| 165 |
} |
| 166 |
|
| 167 |
#[wasm_bindgen] |
| 168 |
pub fn simple_int(_a: u32) {} |
| 169 |
|
| 170 |
#[wasm_bindgen] |
| 171 |
pub fn simple_bool(_a: bool) {} |
| 172 |
|
| 173 |
#[wasm_bindgen] |
| 174 |
pub fn simple_str(_a: &str) {} |
| 175 |
|
| 176 |
#[wasm_bindgen_test] |
| 177 |
fn other_exports() { |
| 178 |
test_other_exports_still_available(); |
| 179 |
} |
| 180 |
|
| 181 |
#[no_mangle] |
| 182 |
pub extern "C" fn foo(_a: u32) {} |
| 183 |
|
| 184 |
#[wasm_bindgen_test] |
| 185 |
fn jsvalue_typeof() { |
| 186 |
test_jsvalue_typeof(); |
| 187 |
} |
| 188 |
|
| 189 |
#[wasm_bindgen] |
| 190 |
pub fn is_object(val: &JsValue) -> bool { |
| 191 |
val.is_object() |
| 192 |
} |
| 193 |
|
| 194 |
#[wasm_bindgen] |
| 195 |
pub fn is_function(val: &JsValue) -> bool { |
| 196 |
val.is_function() |
| 197 |
} |
| 198 |
|
| 199 |
#[wasm_bindgen] |
| 200 |
pub fn is_string(val: &JsValue) -> bool { |
| 201 |
val.is_string() |
| 202 |
} |
| 203 |
|
| 204 |
#[wasm_bindgen] |
| 205 |
extern "C" { |
| 206 |
#[derive(Clone)] |
| 207 |
type Array; |
| 208 |
#[wasm_bindgen(constructor)] |
| 209 |
fn new() -> Array; |
| 210 |
#[wasm_bindgen(method, catch)] |
| 211 |
fn standardized_method_this_js_runtime_doesnt_implement_yet( |
| 212 |
this: &Array, |
| 213 |
) -> Result<(), JsValue>; |
| 214 |
} |
| 215 |
|
| 216 |
#[wasm_bindgen_test] |
| 217 |
fn binding_to_unimplemented_apis_doesnt_break_everything() { |
| 218 |
let array = Array::new(); |
| 219 |
let res = array.standardized_method_this_js_runtime_doesnt_implement_yet(); |
| 220 |
assert!(res.is_err()); |
| 221 |
} |
| 222 |
|
| 223 |
#[wasm_bindgen_test] |
| 224 |
fn optional_slices() { |
| 225 |
optional_str_none(None); |
| 226 |
optional_str_some(Some("x")); |
| 227 |
optional_str_some(Some(intern("x"))); |
| 228 |
unintern("x"); |
| 229 |
optional_str_some(Some("x")); |
| 230 |
optional_slice_none(None); |
| 231 |
optional_slice_some(Some(&[1, 2, 3])); |
| 232 |
optional_string_none(None); |
| 233 |
optional_string_some_empty(Some(String::new())); |
| 234 |
optional_string_some(Some("abcd".to_string())); |
| 235 |
|
| 236 |
assert_eq!(return_string_none(), None); |
| 237 |
assert_eq!(return_string_some(), Some("foo".to_string())); |
| 238 |
test_rust_optional(); |
| 239 |
} |
| 240 |
|
| 241 |
#[wasm_bindgen] |
| 242 |
pub fn take_optional_str_none(x: Option<String>) { |
| 243 |
assert!(x.is_none()) |
| 244 |
} |
| 245 |
#[wasm_bindgen] |
| 246 |
pub fn take_optional_str_some(x: Option<String>) { |
| 247 |
assert_eq!(x, Some(String::from("hello"))); |
| 248 |
} |
| 249 |
|
| 250 |
#[wasm_bindgen] |
| 251 |
pub fn return_optional_str_none() -> Option<String> { |
| 252 |
None |
| 253 |
} |
| 254 |
|
| 255 |
#[wasm_bindgen] |
| 256 |
pub fn return_optional_str_some() -> Option<String> { |
| 257 |
Some("world".to_string()) |
| 258 |
} |
| 259 |
|
| 260 |
#[wasm_bindgen_test] |
| 261 |
fn renaming_imports_and_instanceof() { |
| 262 |
let null = JsValue::NULL; |
| 263 |
assert!(!null.is_instance_of::<Renamed>()); |
| 264 |
|
| 265 |
let arr: JsValue = Array::new().into(); |
| 266 |
assert!(!arr.is_instance_of::<Renamed>()); |
| 267 |
|
| 268 |
let renamed: JsValue = new_renamed().into(); |
| 269 |
assert!(renamed.is_instance_of::<Renamed>()); |
| 270 |
} |
| 271 |
|
| 272 |
#[wasm_bindgen] |
| 273 |
pub fn import_export_same_name() { |
| 274 |
js_import_export_same_name(); |
| 275 |
} |
| 276 |
|
| 277 |
#[wasm_bindgen_test] |
| 278 |
fn string_roundtrip() { |
| 279 |
test_string_roundtrip(); |
| 280 |
} |
| 281 |
|
| 282 |
#[wasm_bindgen] |
| 283 |
pub fn do_string_roundtrip(s: String) -> String { |
| 284 |
s |
| 285 |
} |
| 286 |
|
| 287 |
#[wasm_bindgen_test] |
| 288 |
#[allow(clippy::redundant_clone)] // clone to increase heap live count |
| 289 |
fn externref_heap_live_count() { |
| 290 |
let x = wasm_bindgen::externref_heap_live_count(); |
| 291 |
let y = JsValue::null().clone(); |
| 292 |
assert!(wasm_bindgen::externref_heap_live_count() > x); |
| 293 |
drop(y); |
| 294 |
assert_eq!(x, wasm_bindgen::externref_heap_live_count()); |
| 295 |
} |