script/
init.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 https://mozilla.org/MPL/2.0/. */
4
5use js::jsapi::JSObject;
6use servo_config::pref;
7
8use crate::dom::bindings::codegen::RegisterBindings;
9use crate::dom::bindings::conversions::is_dom_proxy;
10use crate::dom::bindings::proxyhandler;
11use crate::dom::bindings::utils::is_platform_object_static;
12use crate::script_runtime::JSEngineSetup;
13
14#[cfg(target_os = "linux")]
15#[allow(unsafe_code)]
16fn perform_platform_specific_initialization() {
17    // 4096 is default max on many linux systems
18    const MAX_FILE_LIMIT: libc::rlim_t = 4096;
19
20    // Bump up our number of file descriptors to save us from impending doom caused by an onslaught
21    // of iframes.
22    unsafe {
23        let mut rlim = libc::rlimit {
24            rlim_cur: 0,
25            rlim_max: 0,
26        };
27        match libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) {
28            0 => {
29                if rlim.rlim_cur >= MAX_FILE_LIMIT {
30                    // we have more than enough
31                    return;
32                }
33
34                rlim.rlim_cur = match rlim.rlim_max {
35                    libc::RLIM_INFINITY => MAX_FILE_LIMIT,
36                    _ => {
37                        if rlim.rlim_max < MAX_FILE_LIMIT {
38                            rlim.rlim_max
39                        } else {
40                            MAX_FILE_LIMIT
41                        }
42                    },
43                };
44                match libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) {
45                    0 => (),
46                    _ => warn!("Failed to set file count limit"),
47                };
48            },
49            _ => warn!("Failed to get file count limit"),
50        };
51    }
52}
53
54#[cfg(not(target_os = "linux"))]
55fn perform_platform_specific_initialization() {}
56
57#[allow(unsafe_code)]
58unsafe extern "C" fn is_dom_object(obj: *mut JSObject) -> bool {
59    !obj.is_null() && (is_platform_object_static(obj) || is_dom_proxy(obj))
60}
61
62#[allow(unsafe_code)]
63pub fn init() -> JSEngineSetup {
64    unsafe {
65        if pref!(js_disable_jit) {
66            js::jsapi::DisableJitBackend();
67        }
68        proxyhandler::init();
69
70        // Create the global vtables used by the (generated) DOM
71        // bindings to implement JS proxies.
72        RegisterBindings::RegisterProxyHandlers::<crate::DomTypeHolder>();
73        RegisterBindings::InitAllStatics::<crate::DomTypeHolder>();
74
75        js::glue::InitializeMemoryReporter(Some(is_dom_object));
76    }
77
78    perform_platform_specific_initialization();
79
80    JSEngineSetup::default()
81}