mozjs_sys/
lib.rs

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#![allow(unused_extern_crates)]
6#![cfg_attr(feature = "crown", feature(register_tool))]
7#![cfg_attr(feature = "crown", register_tool(crown))]
8#![cfg_attr(feature = "oom_with_hook", feature(alloc_error_hook))]
9
10// These extern crates are needed for linking
11extern crate encoding_c;
12extern crate encoding_c_mem;
13extern crate icu_capi;
14#[cfg(feature = "libz-rs")]
15extern crate libz_rs_sys;
16#[cfg(feature = "libz-sys")]
17extern crate libz_sys;
18
19// The jsimpls module just implements traits so can be private
20mod jsimpls;
21
22// Modules with public definitions
23pub mod glue;
24pub mod jsgc;
25pub mod jsid;
26pub mod jsval;
27pub mod trace;
28
29// Reexport the bindings in the jsapi module
30pub use crate::generated::root as jsapi;
31
32// The bindings generated by bindgen
33#[doc(hidden)]
34#[allow(dead_code)]
35mod generated {
36    include!(concat!(env!("OUT_DIR"), "/build/jsapi.rs"));
37}
38
39/*fn panic_hook(info: &std::panic::PanicInfo) {
40    eprint!("Panic from mozjs: {}", info);
41}*/
42
43/// Configure a panic hook to redirect rust panics to MFBT's MOZ_Crash.
44/// See <https://searchfox.org/mozilla-esr115/source/mozglue/static/rust/lib.rs#106>
45#[no_mangle]
46pub extern "C" fn install_rust_hooks() {
47    //std::panic::set_hook(Box::new(panic_hook));
48    #[cfg(feature = "oom_with_hook")]
49    oom_hook::install();
50}
51
52#[cfg(feature = "oom_with_hook")]
53mod oom_hook {
54    use std::alloc::{set_alloc_error_hook, Layout};
55
56    extern "C" {
57        pub fn RustHandleOOM(size: usize) -> !;
58    }
59
60    pub fn hook(layout: Layout) {
61        unsafe {
62            RustHandleOOM(layout.size());
63        }
64    }
65
66    pub fn install() {
67        set_alloc_error_hook(hook);
68    }
69}