| 1 |
use js_sys::Number; |
| 2 |
use std::cell::{Cell, RefCell}; |
| 3 |
use std::rc::Rc; |
| 4 |
use wasm_bindgen::prelude::*; |
| 5 |
use wasm_bindgen_test::*; |
| 6 |
|
| 7 |
#[wasm_bindgen(module = "tests/wasm/closures.js")] |
| 8 |
extern "C" { |
| 9 |
fn works_call(a: &dyn Fn()); |
| 10 |
fn works_thread(a: &dyn Fn(u32) -> u32) -> u32; |
| 11 |
|
| 12 |
fn cannot_reuse_call(a: &dyn Fn()); |
| 13 |
#[wasm_bindgen(catch)] |
| 14 |
fn cannot_reuse_call_again() -> Result<(), JsValue>; |
| 15 |
|
| 16 |
fn long_lived_call1(a: &Closure<dyn Fn()>); |
| 17 |
fn long_lived_call2(a: &Closure<dyn FnMut(u32) -> u32>) -> u32; |
| 18 |
|
| 19 |
fn many_arity_call1(a: &Closure<dyn Fn()>); |
| 20 |
fn many_arity_call2(a: &Closure<dyn Fn(u32)>); |
| 21 |
fn many_arity_call3(a: &Closure<dyn Fn(u32, u32)>); |
| 22 |
fn many_arity_call4(a: &Closure<dyn Fn(u32, u32, u32)>); |
| 23 |
fn many_arity_call5(a: &Closure<dyn Fn(u32, u32, u32, u32)>); |
| 24 |
fn many_arity_call6(a: &Closure<dyn Fn(u32, u32, u32, u32, u32)>); |
| 25 |
fn many_arity_call7(a: &Closure<dyn Fn(u32, u32, u32, u32, u32, u32)>); |
| 26 |
fn many_arity_call8(a: &Closure<dyn Fn(u32, u32, u32, u32, u32, u32, u32)>); |
| 27 |
fn many_arity_call9(a: &Closure<dyn Fn(u32, u32, u32, u32, u32, u32, u32, u32)>); |
| 28 |
|
| 29 |
#[wasm_bindgen(js_name = many_arity_call1)] |
| 30 |
fn many_arity_call_mut1(a: &Closure<dyn FnMut()>); |
| 31 |
#[wasm_bindgen(js_name = many_arity_call2)] |
| 32 |
fn many_arity_call_mut2(a: &Closure<dyn FnMut(u32)>); |
| 33 |
#[wasm_bindgen(js_name = many_arity_call3)] |
| 34 |
fn many_arity_call_mut3(a: &Closure<dyn FnMut(u32, u32)>); |
| 35 |
#[wasm_bindgen(js_name = many_arity_call4)] |
| 36 |
fn many_arity_call_mut4(a: &Closure<dyn FnMut(u32, u32, u32)>); |
| 37 |
#[wasm_bindgen(js_name = many_arity_call5)] |
| 38 |
fn many_arity_call_mut5(a: &Closure<dyn FnMut(u32, u32, u32, u32)>); |
| 39 |
#[wasm_bindgen(js_name = many_arity_call6)] |
| 40 |
fn many_arity_call_mut6(a: &Closure<dyn FnMut(u32, u32, u32, u32, u32)>); |
| 41 |
#[wasm_bindgen(js_name = many_arity_call7)] |
| 42 |
fn many_arity_call_mut7(a: &Closure<dyn FnMut(u32, u32, u32, u32, u32, u32)>); |
| 43 |
#[wasm_bindgen(js_name = many_arity_call8)] |
| 44 |
fn many_arity_call_mut8(a: &Closure<dyn FnMut(u32, u32, u32, u32, u32, u32, u32)>); |
| 45 |
#[wasm_bindgen(js_name = many_arity_call9)] |
| 46 |
fn many_arity_call_mut9(a: &Closure<dyn FnMut(u32, u32, u32, u32, u32, u32, u32, u32)>); |
| 47 |
|
| 48 |
fn option_call1(a: Option<&Closure<dyn Fn()>>); |
| 49 |
fn option_call2(a: Option<&Closure<dyn FnMut(u32) -> u32>>) -> u32; |
| 50 |
fn option_call3(a: Option<&Closure<dyn Fn()>>) -> bool; |
| 51 |
|
| 52 |
#[wasm_bindgen(js_name = many_arity_call1)] |
| 53 |
fn many_arity_stack1(a: &dyn Fn()); |
| 54 |
#[wasm_bindgen(js_name = many_arity_call2)] |
| 55 |
fn many_arity_stack2(a: &dyn Fn(u32)); |
| 56 |
#[wasm_bindgen(js_name = many_arity_call3)] |
| 57 |
fn many_arity_stack3(a: &dyn Fn(u32, u32)); |
| 58 |
#[wasm_bindgen(js_name = many_arity_call4)] |
| 59 |
fn many_arity_stack4(a: &dyn Fn(u32, u32, u32)); |
| 60 |
#[wasm_bindgen(js_name = many_arity_call5)] |
| 61 |
fn many_arity_stack5(a: &dyn Fn(u32, u32, u32, u32)); |
| 62 |
#[wasm_bindgen(js_name = many_arity_call6)] |
| 63 |
fn many_arity_stack6(a: &dyn Fn(u32, u32, u32, u32, u32)); |
| 64 |
#[wasm_bindgen(js_name = many_arity_call7)] |
| 65 |
fn many_arity_stack7(a: &dyn Fn(u32, u32, u32, u32, u32, u32)); |
| 66 |
#[wasm_bindgen(js_name = many_arity_call8)] |
| 67 |
fn many_arity_stack8(a: &dyn Fn(u32, u32, u32, u32, u32, u32, u32)); |
| 68 |
#[wasm_bindgen(js_name = many_arity_call9)] |
| 69 |
fn many_arity_stack9(a: &dyn Fn(u32, u32, u32, u32, u32, u32, u32, u32)); |
| 70 |
|
| 71 |
fn long_lived_dropping_cache(a: &Closure<dyn Fn()>); |
| 72 |
#[wasm_bindgen(catch)] |
| 73 |
fn long_lived_dropping_call() -> Result<(), JsValue>; |
| 74 |
|
| 75 |
fn long_lived_option_dropping_cache(a: Option<&Closure<dyn Fn()>>) -> bool; |
| 76 |
#[wasm_bindgen(catch)] |
| 77 |
fn long_lived_option_dropping_call() -> Result<(), JsValue>; |
| 78 |
|
| 79 |
fn long_fnmut_recursive_cache(a: &Closure<dyn FnMut()>); |
| 80 |
#[wasm_bindgen(catch)] |
| 81 |
fn long_fnmut_recursive_call() -> Result<(), JsValue>; |
| 82 |
|
| 83 |
fn fnmut_call(a: &mut dyn FnMut()); |
| 84 |
fn fnmut_thread(a: &mut dyn FnMut(u32) -> u32) -> u32; |
| 85 |
|
| 86 |
fn fnmut_bad_call(a: &mut dyn FnMut()); |
| 87 |
#[wasm_bindgen(catch)] |
| 88 |
fn fnmut_bad_again(a: bool) -> Result<(), JsValue>; |
| 89 |
|
| 90 |
fn string_arguments_call(a: &mut dyn FnMut(String)); |
| 91 |
|
| 92 |
fn string_ret_call(a: &mut dyn FnMut(String) -> String); |
| 93 |
|
| 94 |
fn drop_during_call_save(a: &Closure<dyn Fn()>); |
| 95 |
fn drop_during_call_call(); |
| 96 |
|
| 97 |
fn js_test_closure_returner(); |
| 98 |
|
| 99 |
fn calling_it_throws(a: &Closure<dyn FnMut()>) -> bool; |
| 100 |
|
| 101 |
fn call_val(f: &JsValue); |
| 102 |
|
| 103 |
#[wasm_bindgen(js_name = calling_it_throws)] |
| 104 |
fn call_val_throws(f: &JsValue) -> bool; |
| 105 |
|
| 106 |
fn pass_reference_first_arg_twice( |
| 107 |
a: RefFirstArgument, |
| 108 |
b: &Closure<dyn FnMut(&RefFirstArgument)>, |
| 109 |
c: &Closure<dyn FnMut(&RefFirstArgument)>, |
| 110 |
); |
| 111 |
#[wasm_bindgen(js_name = pass_reference_first_arg_twice)] |
| 112 |
fn pass_reference_first_arg_twice2( |
| 113 |
a: RefFirstArgument, |
| 114 |
b: &mut dyn FnMut(&RefFirstArgument), |
| 115 |
c: &mut dyn FnMut(&RefFirstArgument), |
| 116 |
); |
| 117 |
fn call_destroyed(a: &JsValue); |
| 118 |
|
| 119 |
fn js_store_forgotten_closure(closure: &Closure<dyn Fn()>); |
| 120 |
fn js_call_forgotten_closure(); |
| 121 |
|
| 122 |
#[wasm_bindgen(js_name = many_arity_call2)] |
| 123 |
fn externref_call(a: &Closure<dyn Fn(JsValue)>); |
| 124 |
#[wasm_bindgen(js_name = many_arity_call2)] |
| 125 |
fn named_externref_call(a: &Closure<dyn Fn(Number)>); |
| 126 |
} |
| 127 |
|
| 128 |
#[wasm_bindgen_test] |
| 129 |
fn works() { |
| 130 |
let a = Cell::new(false); |
| 131 |
works_call(&|| a.set(true)); |
| 132 |
assert!(a.get()); |
| 133 |
|
| 134 |
assert_eq!(works_thread(&|a| a + 1), 3); |
| 135 |
} |
| 136 |
|
| 137 |
#[wasm_bindgen_test] |
| 138 |
fn cannot_reuse() { |
| 139 |
cannot_reuse_call(&|| {}); |
| 140 |
assert!(cannot_reuse_call_again().is_err()); |
| 141 |
} |
| 142 |
|
| 143 |
#[wasm_bindgen_test] |
| 144 |
fn debug() { |
| 145 |
let closure = Closure::wrap(Box::new(|| {}) as Box<dyn FnMut()>); |
| 146 |
assert_eq!(&format!("{:?}", closure), "Closure { ... }"); |
| 147 |
} |
| 148 |
|
| 149 |
#[wasm_bindgen_test] |
| 150 |
fn long_lived() { |
| 151 |
let hit = Rc::new(Cell::new(false)); |
| 152 |
let hit2 = hit.clone(); |
| 153 |
let a = Closure::new(move || hit2.set(true)); |
| 154 |
assert!(!hit.get()); |
| 155 |
long_lived_call1(&a); |
| 156 |
assert!(hit.get()); |
| 157 |
|
| 158 |
let hit = Rc::new(Cell::new(false)); |
| 159 |
{ |
| 160 |
let hit = hit.clone(); |
| 161 |
let a = Closure::new(move |x| { |
| 162 |
hit.set(true); |
| 163 |
x + 3 |
| 164 |
}); |
| 165 |
assert_eq!(long_lived_call2(&a), 5); |
| 166 |
} |
| 167 |
assert!(hit.get()); |
| 168 |
} |
| 169 |
|
| 170 |
#[wasm_bindgen_test] |
| 171 |
fn many_arity() { |
| 172 |
many_arity_call1(&Closure::new(|| {})); |
| 173 |
many_arity_call2(&Closure::new(|a| assert_eq!(a, 1))); |
| 174 |
many_arity_call3(&Closure::new(|a, b| assert_eq!((a, b), (1, 2)))); |
| 175 |
many_arity_call4(&Closure::new(|a, b, c| assert_eq!((a, b, c), (1, 2, 3)))); |
| 176 |
many_arity_call5(&Closure::new(|a, b, c, d| { |
| 177 |
assert_eq!((a, b, c, d), (1, 2, 3, 4)) |
| 178 |
})); |
| 179 |
many_arity_call6(&Closure::new(|a, b, c, d, e| { |
| 180 |
assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5)) |
| 181 |
})); |
| 182 |
many_arity_call7(&Closure::new(|a, b, c, d, e, f| { |
| 183 |
assert_eq!((a, b, c, d, e, f), (1, 2, 3, 4, 5, 6)) |
| 184 |
})); |
| 185 |
many_arity_call8(&Closure::new(|a, b, c, d, e, f, g| { |
| 186 |
assert_eq!((a, b, c, d, e, f, g), (1, 2, 3, 4, 5, 6, 7)) |
| 187 |
})); |
| 188 |
many_arity_call9(&Closure::new(|a, b, c, d, e, f, g, h| { |
| 189 |
assert_eq!((a, b, c, d, e, f, g, h), (1, 2, 3, 4, 5, 6, 7, 8)) |
| 190 |
})); |
| 191 |
|
| 192 |
let s = String::new(); |
| 193 |
many_arity_call_mut1(&Closure::once(move || drop(s))); |
| 194 |
let s = String::new(); |
| 195 |
many_arity_call_mut2(&Closure::once(move |a| { |
| 196 |
drop(s); |
| 197 |
assert_eq!(a, 1); |
| 198 |
})); |
| 199 |
let s = String::new(); |
| 200 |
many_arity_call_mut3(&Closure::once(move |a, b| { |
| 201 |
drop(s); |
| 202 |
assert_eq!((a, b), (1, 2)); |
| 203 |
})); |
| 204 |
let s = String::new(); |
| 205 |
many_arity_call_mut4(&Closure::once(move |a, b, c| { |
| 206 |
drop(s); |
| 207 |
assert_eq!((a, b, c), (1, 2, 3)); |
| 208 |
})); |
| 209 |
let s = String::new(); |
| 210 |
many_arity_call_mut5(&Closure::once(move |a, b, c, d| { |
| 211 |
drop(s); |
| 212 |
assert_eq!((a, b, c, d), (1, 2, 3, 4)); |
| 213 |
})); |
| 214 |
let s = String::new(); |
| 215 |
many_arity_call_mut6(&Closure::once(move |a, b, c, d, e| { |
| 216 |
drop(s); |
| 217 |
assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5)); |
| 218 |
})); |
| 219 |
let s = String::new(); |
| 220 |
many_arity_call_mut7(&Closure::once(move |a, b, c, d, e, f| { |
| 221 |
drop(s); |
| 222 |
assert_eq!((a, b, c, d, e, f), (1, 2, 3, 4, 5, 6)); |
| 223 |
})); |
| 224 |
let s = String::new(); |
| 225 |
many_arity_call_mut8(&Closure::once(move |a, b, c, d, e, f, g| { |
| 226 |
drop(s); |
| 227 |
assert_eq!((a, b, c, d, e, f, g), (1, 2, 3, 4, 5, 6, 7)); |
| 228 |
})); |
| 229 |
let s = String::new(); |
| 230 |
many_arity_call_mut9(&Closure::once(move |a, b, c, d, e, f, g, h| { |
| 231 |
drop(s); |
| 232 |
assert_eq!((a, b, c, d, e, f, g, h), (1, 2, 3, 4, 5, 6, 7, 8)); |
| 233 |
})); |
| 234 |
|
| 235 |
many_arity_stack1(&(|| {})); |
| 236 |
many_arity_stack2(&(|a| assert_eq!(a, 1))); |
| 237 |
many_arity_stack3(&(|a, b| assert_eq!((a, b), (1, 2)))); |
| 238 |
many_arity_stack4(&(|a, b, c| assert_eq!((a, b, c), (1, 2, 3)))); |
| 239 |
many_arity_stack5(&(|a, b, c, d| assert_eq!((a, b, c, d), (1, 2, 3, 4)))); |
| 240 |
many_arity_stack6(&(|a, b, c, d, e| assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5)))); |
| 241 |
many_arity_stack7(&(|a, b, c, d, e, f| assert_eq!((a, b, c, d, e, f), (1, 2, 3, 4, 5, 6)))); |
| 242 |
many_arity_stack8( |
| 243 |
&(|a, b, c, d, e, f, g| assert_eq!((a, b, c, d, e, f, g), (1, 2, 3, 4, 5, 6, 7))), |
| 244 |
); |
| 245 |
many_arity_stack9( |
| 246 |
&(|a, b, c, d, e, f, g, h| assert_eq!((a, b, c, d, e, f, g, h), (1, 2, 3, 4, 5, 6, 7, 8))), |
| 247 |
); |
| 248 |
} |
| 249 |
|
| 250 |
#[wasm_bindgen_test] |
| 251 |
fn option() { |
| 252 |
let hit = Rc::new(Cell::new(false)); |
| 253 |
let hit2 = hit.clone(); |
| 254 |
let a = Closure::new(move || hit2.set(true)); |
| 255 |
assert!(!hit.get()); |
| 256 |
option_call1(Some(&a)); |
| 257 |
assert!(hit.get()); |
| 258 |
|
| 259 |
let hit = Rc::new(Cell::new(false)); |
| 260 |
{ |
| 261 |
let hit = hit.clone(); |
| 262 |
let a = Closure::new(move |x| { |
| 263 |
hit.set(true); |
| 264 |
x + 3 |
| 265 |
}); |
| 266 |
assert_eq!(option_call2(Some(&a)), 5); |
| 267 |
} |
| 268 |
assert!(hit.get()); |
| 269 |
|
| 270 |
assert!(option_call3(None)); |
| 271 |
} |
| 272 |
|
| 273 |
struct Dropper(Rc<Cell<bool>>); |
| 274 |
impl Drop for Dropper { |
| 275 |
fn drop(&mut self) { |
| 276 |
assert!(!self.0.get()); |
| 277 |
self.0.set(true); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
#[wasm_bindgen_test] |
| 282 |
fn call_fn_once_twice() { |
| 283 |
let dropped = Rc::new(Cell::new(false)); |
| 284 |
let dropper = Dropper(dropped.clone()); |
| 285 |
let called = Rc::new(Cell::new(false)); |
| 286 |
|
| 287 |
let c = Closure::once({ |
| 288 |
let called = called.clone(); |
| 289 |
move || { |
| 290 |
assert!(!called.get()); |
| 291 |
called.set(true); |
| 292 |
drop(dropper); |
| 293 |
} |
| 294 |
}); |
| 295 |
|
| 296 |
many_arity_call_mut1(&c); |
| 297 |
assert!(called.get()); |
| 298 |
assert!(dropped.get()); |
| 299 |
|
| 300 |
assert!(calling_it_throws(&c)); |
| 301 |
} |
| 302 |
|
| 303 |
#[wasm_bindgen_test] |
| 304 |
fn once_into_js() { |
| 305 |
let dropped = Rc::new(Cell::new(false)); |
| 306 |
let dropper = Dropper(dropped.clone()); |
| 307 |
let called = Rc::new(Cell::new(false)); |
| 308 |
|
| 309 |
let f = Closure::once_into_js({ |
| 310 |
let called = called.clone(); |
| 311 |
move || { |
| 312 |
assert!(!called.get()); |
| 313 |
called.set(true); |
| 314 |
drop(dropper); |
| 315 |
} |
| 316 |
}); |
| 317 |
|
| 318 |
call_val(&f); |
| 319 |
assert!(called.get()); |
| 320 |
assert!(dropped.get()); |
| 321 |
|
| 322 |
assert!(call_val_throws(&f)); |
| 323 |
} |
| 324 |
|
| 325 |
#[wasm_bindgen_test] |
| 326 |
fn long_lived_dropping() { |
| 327 |
let hit = Rc::new(Cell::new(false)); |
| 328 |
let hit2 = hit.clone(); |
| 329 |
let a = Closure::new(move || hit2.set(true)); |
| 330 |
long_lived_dropping_cache(&a); |
| 331 |
assert!(!hit.get()); |
| 332 |
assert!(long_lived_dropping_call().is_ok()); |
| 333 |
assert!(hit.get()); |
| 334 |
drop(a); |
| 335 |
assert!(long_lived_dropping_call().is_err()); |
| 336 |
} |
| 337 |
|
| 338 |
#[wasm_bindgen_test] |
| 339 |
fn long_lived_option_dropping() { |
| 340 |
let hit = Rc::new(Cell::new(false)); |
| 341 |
let hit2 = hit.clone(); |
| 342 |
|
| 343 |
let a = Closure::new(move || hit2.set(true)); |
| 344 |
|
| 345 |
assert!(!long_lived_option_dropping_cache(None)); |
| 346 |
assert!(long_lived_option_dropping_cache(Some(&a))); |
| 347 |
|
| 348 |
assert!(!hit.get()); |
| 349 |
assert!(long_lived_option_dropping_call().is_ok()); |
| 350 |
assert!(hit.get()); |
| 351 |
|
| 352 |
drop(a); |
| 353 |
assert!(long_lived_option_dropping_call().is_err()); |
| 354 |
} |
| 355 |
|
| 356 |
#[wasm_bindgen_test] |
| 357 |
fn long_fnmut_recursive() { |
| 358 |
let a = Closure::new(|| { |
| 359 |
assert!(long_fnmut_recursive_call().is_err()); |
| 360 |
}); |
| 361 |
long_fnmut_recursive_cache(&a); |
| 362 |
assert!(long_fnmut_recursive_call().is_ok()); |
| 363 |
} |
| 364 |
|
| 365 |
#[wasm_bindgen_test] |
| 366 |
fn fnmut() { |
| 367 |
let mut a = false; |
| 368 |
fnmut_call(&mut || a = true); |
| 369 |
assert!(a); |
| 370 |
|
| 371 |
let mut x = false; |
| 372 |
assert_eq!( |
| 373 |
fnmut_thread(&mut |a| { |
| 374 |
x = true; |
| 375 |
a + 1 |
| 376 |
}), |
| 377 |
3 |
| 378 |
); |
| 379 |
assert!(x); |
| 380 |
} |
| 381 |
|
| 382 |
#[wasm_bindgen_test] |
| 383 |
fn fnmut_bad() { |
| 384 |
let mut x = true; |
| 385 |
let mut hits = 0; |
| 386 |
fnmut_bad_call(&mut || { |
| 387 |
hits += 1; |
| 388 |
if fnmut_bad_again(hits == 1).is_err() { |
| 389 |
return; |
| 390 |
} |
| 391 |
x = false; |
| 392 |
}); |
| 393 |
assert_eq!(hits, 1); |
| 394 |
assert!(x); |
| 395 |
|
| 396 |
assert!(fnmut_bad_again(true).is_err()); |
| 397 |
} |
| 398 |
|
| 399 |
#[wasm_bindgen_test] |
| 400 |
fn string_arguments() { |
| 401 |
let mut x = false; |
| 402 |
string_arguments_call(&mut |s| { |
| 403 |
assert_eq!(s, "foo"); |
| 404 |
x = true; |
| 405 |
}); |
| 406 |
assert!(x); |
| 407 |
} |
| 408 |
|
| 409 |
#[wasm_bindgen_test] |
| 410 |
fn string_ret() { |
| 411 |
let mut x = false; |
| 412 |
string_ret_call(&mut |mut s| { |
| 413 |
assert_eq!(s, "foo"); |
| 414 |
s.push_str("bar"); |
| 415 |
x = true; |
| 416 |
s |
| 417 |
}); |
| 418 |
assert!(x); |
| 419 |
} |
| 420 |
|
| 421 |
#[wasm_bindgen_test] |
| 422 |
fn drop_drops() { |
| 423 |
static mut HIT: bool = false; |
| 424 |
|
| 425 |
struct A; |
| 426 |
|
| 427 |
impl Drop for A { |
| 428 |
fn drop(&mut self) { |
| 429 |
unsafe { |
| 430 |
HIT = true; |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
let a = A; |
| 435 |
let x: Closure<dyn Fn()> = Closure::new(move || { |
| 436 |
let _ = a; |
| 437 |
}); |
| 438 |
drop(x); |
| 439 |
unsafe { |
| 440 |
assert!(HIT); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
#[wasm_bindgen_test] |
| 445 |
fn drop_during_call_ok() { |
| 446 |
static mut HIT: bool = false; |
| 447 |
struct A; |
| 448 |
impl Drop for A { |
| 449 |
fn drop(&mut self) { |
| 450 |
unsafe { |
| 451 |
HIT = true; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
let rc = Rc::new(RefCell::new(None)); |
| 457 |
let rc2 = rc.clone(); |
| 458 |
let x = 3; |
| 459 |
let a = A; |
| 460 |
let x: Closure<dyn Fn()> = Closure::new(move || { |
| 461 |
// "drop ourselves" |
| 462 |
drop(rc2.borrow_mut().take().unwrap()); |
| 463 |
|
| 464 |
// `A` should not have been destroyed as a result |
| 465 |
unsafe { |
| 466 |
assert!(!HIT); |
| 467 |
} |
| 468 |
|
| 469 |
// allocate some heap memory to try to paper over our `3` |
| 470 |
drop(String::from("1234567890")); |
| 471 |
|
| 472 |
// make sure our closure memory is still valid |
| 473 |
assert_eq!(x, 3); |
| 474 |
|
| 475 |
// make sure `A` is bound to our closure environment. |
| 476 |
drop(&a); |
| 477 |
unsafe { |
| 478 |
assert!(!HIT); |
| 479 |
} |
| 480 |
}); |
| 481 |
drop_during_call_save(&x); |
| 482 |
*rc.borrow_mut() = Some(x); |
| 483 |
drop(rc); |
| 484 |
unsafe { |
| 485 |
assert!(!HIT); |
| 486 |
} |
| 487 |
drop_during_call_call(); |
| 488 |
unsafe { |
| 489 |
assert!(HIT); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
#[wasm_bindgen_test] |
| 494 |
fn test_closure_returner() { |
| 495 |
type ClosureType = dyn FnMut() -> BadStruct; |
| 496 |
|
| 497 |
use js_sys::{Object, Reflect}; |
| 498 |
|
| 499 |
js_test_closure_returner(); |
| 500 |
|
| 501 |
#[wasm_bindgen] |
| 502 |
pub struct ClosureHandle(Closure<ClosureType>); |
| 503 |
|
| 504 |
#[wasm_bindgen] |
| 505 |
pub struct BadStruct {} |
| 506 |
|
| 507 |
#[wasm_bindgen] |
| 508 |
pub fn closure_returner() -> Result<Object, JsValue> { |
| 509 |
let o = Object::new(); |
| 510 |
|
| 511 |
let some_fn = Closure::wrap(Box::new(move || BadStruct {}) as Box<ClosureType>); |
| 512 |
Reflect::set( |
| 513 |
&o, |
| 514 |
&JsValue::from("someKey"), |
| 515 |
some_fn.as_ref().unchecked_ref(), |
| 516 |
) |
| 517 |
.unwrap(); |
| 518 |
Reflect::set( |
| 519 |
&o, |
| 520 |
&JsValue::from("handle"), |
| 521 |
&JsValue::from(ClosureHandle(some_fn)), |
| 522 |
) |
| 523 |
.unwrap(); |
| 524 |
|
| 525 |
Ok(o) |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
#[wasm_bindgen] |
| 530 |
pub struct RefFirstArgument { |
| 531 |
contents: u32, |
| 532 |
} |
| 533 |
|
| 534 |
#[wasm_bindgen_test] |
| 535 |
fn reference_as_first_argument_builds_at_all() { |
| 536 |
#[wasm_bindgen] |
| 537 |
extern "C" { |
| 538 |
fn ref_first_arg1(a: &dyn Fn(&JsValue)); |
| 539 |
fn ref_first_arg2(a: &mut dyn FnMut(&JsValue)); |
| 540 |
fn ref_first_arg3(a: &Closure<dyn Fn(&JsValue)>); |
| 541 |
fn ref_first_arg4(a: &Closure<dyn FnMut(&JsValue)>); |
| 542 |
fn ref_first_custom1(a: &dyn Fn(&RefFirstArgument)); |
| 543 |
fn ref_first_custom2(a: &mut dyn FnMut(&RefFirstArgument)); |
| 544 |
fn ref_first_custom3(a: &Closure<dyn Fn(&RefFirstArgument)>); |
| 545 |
fn ref_first_custom4(a: &Closure<dyn FnMut(&RefFirstArgument)>); |
| 546 |
} |
| 547 |
|
| 548 |
Closure::wrap(Box::new(|_: &JsValue| ()) as Box<dyn Fn(&JsValue)>); |
| 549 |
Closure::wrap(Box::new(|_: &JsValue| ()) as Box<dyn FnMut(&JsValue)>); |
| 550 |
Closure::once(|_: &JsValue| ()); |
| 551 |
Closure::once_into_js(|_: &JsValue| ()); |
| 552 |
Closure::wrap(Box::new(|_: &RefFirstArgument| ()) as Box<dyn Fn(&RefFirstArgument)>); |
| 553 |
Closure::wrap(Box::new(|_: &RefFirstArgument| ()) as Box<dyn FnMut(&RefFirstArgument)>); |
| 554 |
Closure::once(|_: &RefFirstArgument| ()); |
| 555 |
Closure::once_into_js(|_: &RefFirstArgument| ()); |
| 556 |
} |
| 557 |
|
| 558 |
#[wasm_bindgen_test] |
| 559 |
fn reference_as_first_argument_works() { |
| 560 |
let a = Rc::new(Cell::new(0)); |
| 561 |
let b = { |
| 562 |
let a = a.clone(); |
| 563 |
Closure::once(move |x: &RefFirstArgument| { |
| 564 |
assert_eq!(a.get(), 0); |
| 565 |
assert_eq!(x.contents, 3); |
| 566 |
a.set(a.get() + 1); |
| 567 |
}) |
| 568 |
}; |
| 569 |
let c = { |
| 570 |
let a = a.clone(); |
| 571 |
Closure::once(move |x: &RefFirstArgument| { |
| 572 |
assert_eq!(a.get(), 1); |
| 573 |
assert_eq!(x.contents, 3); |
| 574 |
a.set(a.get() + 1); |
| 575 |
}) |
| 576 |
}; |
| 577 |
pass_reference_first_arg_twice(RefFirstArgument { contents: 3 }, &b, &c); |
| 578 |
assert_eq!(a.get(), 2); |
| 579 |
} |
| 580 |
|
| 581 |
#[wasm_bindgen_test] |
| 582 |
fn reference_as_first_argument_works2() { |
| 583 |
let a = Cell::new(0); |
| 584 |
pass_reference_first_arg_twice2( |
| 585 |
RefFirstArgument { contents: 3 }, |
| 586 |
&mut |x: &RefFirstArgument| { |
| 587 |
assert_eq!(a.get(), 0); |
| 588 |
assert_eq!(x.contents, 3); |
| 589 |
a.set(a.get() + 1); |
| 590 |
}, |
| 591 |
&mut |x: &RefFirstArgument| { |
| 592 |
assert_eq!(a.get(), 1); |
| 593 |
assert_eq!(x.contents, 3); |
| 594 |
a.set(a.get() + 1); |
| 595 |
}, |
| 596 |
); |
| 597 |
assert_eq!(a.get(), 2); |
| 598 |
} |
| 599 |
|
| 600 |
#[wasm_bindgen_test] |
| 601 |
fn call_destroyed_doesnt_segfault() { |
| 602 |
struct A(i32, i32); |
| 603 |
impl Drop for A { |
| 604 |
fn drop(&mut self) { |
| 605 |
assert_eq!(self.0, self.1); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
let a = A(1, 1); |
| 610 |
let a = Closure::wrap(Box::new(move || { |
| 611 |
let _ = a; |
| 612 |
}) as Box<dyn Fn()>); |
| 613 |
let b = a.as_ref().clone(); |
| 614 |
drop(a); |
| 615 |
call_destroyed(&b); |
| 616 |
|
| 617 |
let a = A(2, 2); |
| 618 |
let a = Closure::wrap(Box::new(move || { |
| 619 |
let _ = a; |
| 620 |
}) as Box<dyn FnMut()>); |
| 621 |
let b = a.as_ref().clone(); |
| 622 |
drop(a); |
| 623 |
call_destroyed(&b); |
| 624 |
|
| 625 |
let a = A(1, 1); |
| 626 |
let a = Closure::wrap(Box::new(move |_: &JsValue| { |
| 627 |
let _ = a; |
| 628 |
}) as Box<dyn Fn(&JsValue)>); |
| 629 |
let b = a.as_ref().clone(); |
| 630 |
drop(a); |
| 631 |
call_destroyed(&b); |
| 632 |
|
| 633 |
let a = A(2, 2); |
| 634 |
let a = Closure::wrap(Box::new(move |_: &JsValue| { |
| 635 |
let _ = a; |
| 636 |
}) as Box<dyn FnMut(&JsValue)>); |
| 637 |
let b = a.as_ref().clone(); |
| 638 |
drop(a); |
| 639 |
call_destroyed(&b); |
| 640 |
} |
| 641 |
|
| 642 |
#[wasm_bindgen_test] |
| 643 |
fn forget_works() { |
| 644 |
let a = Closure::wrap(Box::new(|| {}) as Box<dyn Fn()>); |
| 645 |
js_store_forgotten_closure(&a); |
| 646 |
a.forget(); |
| 647 |
js_call_forgotten_closure(); |
| 648 |
} |
| 649 |
|
| 650 |
#[wasm_bindgen_test] |
| 651 |
fn named_externref_no_duplicate_adapter() { |
| 652 |
externref_call(&Closure::new(|a| assert_eq!(a, 1))); |
| 653 |
named_externref_call(&Closure::new(|a| assert_eq!(a, 1))); |
| 654 |
} |