Skip to main content

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