1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#![allow(unused_extern_crates)]

// These extern crates are needed for linking
extern crate encoding_c;
extern crate encoding_c_mem;
extern crate libz_sys;

// The jsimpls module just implements traits so can be private
mod jsimpls;

// Modules with public definitions
pub mod glue;
pub mod jsgc;
pub mod jsid;
pub mod jsval;

// Reexport the bindings in the jsapi module
pub use crate::generated::root as jsapi;

// The bindings generated by bindgen
#[doc(hidden)]
mod generated {
    include!(concat!(env!("OUT_DIR"), "/build/jsapi.rs"));
}

/*fn panic_hook(info: &std::panic::PanicInfo) {
    eprint!("Panic from mozjs: {}", info);
}*/

/// Configure a panic hook to redirect rust panics to MFBT's MOZ_Crash.
/// See <https://searchfox.org/mozilla-esr115/source/mozglue/static/rust/lib.rs#106>
#[no_mangle]
pub extern "C" fn install_rust_hooks() {
    //std::panic::set_hook(Box::new(panic_hook));
    #[cfg(feature = "oom_with_hook")]
    oom_hook::install();
}

#[cfg(feature = "oom_with_hook")]
mod oom_hook {
    use std::alloc::{set_alloc_error_hook, Layout};

    extern "C" {
        pub fn RustHandleOOM(size: usize) -> !;
    }

    pub fn hook(layout: Layout) {
        unsafe {
            RustHandleOOM(layout.size());
        }
    }

    pub fn install() {
        set_alloc_error_hook(hook);
    }
}