/rust/git/checkouts/mozjs-fa11ffc7d4f1cc2d/d90edd1/mozjs/src/panic.rs
Line | Count | Source (jump to first uncovered line) |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | use std::any::Any; |
6 | | use std::cell::RefCell; |
7 | | use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe}; |
8 | | |
9 | | thread_local!(static PANIC_PAYLOAD: RefCell<Option<Box<dyn Any + Send>>> = RefCell::new(None)); |
10 | | |
11 | | /// If there is a pending panic, resume unwinding. |
12 | 0 | pub fn maybe_resume_unwind() { |
13 | 0 | if let Some(error) = PANIC_PAYLOAD.with(|result| result.borrow_mut().take()) { |
14 | 0 | resume_unwind(error); |
15 | 0 | } |
16 | 0 | } |
17 | | |
18 | | /// Generic wrapper for JS engine callbacks panic-catching |
19 | | // https://github.com/servo/servo/issues/26585 |
20 | | #[inline(never)] |
21 | 0 | pub fn wrap_panic(function: &mut dyn FnMut()) { |
22 | 0 | match catch_unwind(AssertUnwindSafe(function)) { |
23 | 0 | Ok(()) => {} |
24 | 0 | Err(payload) => { |
25 | 0 | PANIC_PAYLOAD.with(|opt_payload| { |
26 | 0 | let mut opt_payload = opt_payload.borrow_mut(); |
27 | 0 | assert!(opt_payload.is_none()); |
28 | 0 | *opt_payload = Some(payload); |
29 | 0 | }); |
30 | 0 | } |
31 | | } |
32 | 0 | } |