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