Skip to main content

script/dom/bindings/
utils.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
5//! Various utilities to glue JavaScript and the DOM implementation together.
6
7use std::cell::RefCell;
8use std::thread::LocalKey;
9
10use js::context::JSContext;
11use js::conversions::ToJSValConvertible;
12use js::glue::{IsWrapper, JSPrincipalsCallbacks, UnwrapObjectStatic};
13use js::jsapi::{CallArgs, DOMCallbacks, JSObject};
14use js::realm::CurrentRealm;
15use js::rust::wrappers2::JS_FreezeObject;
16use js::rust::{HandleObject, MutableHandleValue, get_object_class, is_dom_class};
17use script_bindings::interfaces::{DomHelpers, Interface};
18use script_bindings::reflector::{DomObject, DomObjectWrap, reflect_dom_object_with_cx};
19use script_bindings::settings_stack::StackEntry;
20
21use crate::DomTypes;
22use crate::dom::bindings::codegen::{InterfaceObjectMap, PrototypeList};
23use crate::dom::bindings::constructor::call_html_constructor;
24use crate::dom::bindings::conversions::DerivedFrom;
25use crate::dom::bindings::error::{Error, report_pending_exception, throw_dom_exception};
26use crate::dom::bindings::principals::PRINCIPALS_CALLBACKS;
27use crate::dom::bindings::root::DomRoot;
28use crate::dom::bindings::settings_stack;
29use crate::dom::globalscope::GlobalScope;
30use crate::dom::windowproxy::WindowProxyHandler;
31use crate::script_thread::ScriptThread;
32
33#[derive(JSTraceable, MallocSizeOf)]
34/// Static data associated with a global object.
35pub(crate) struct GlobalStaticData {
36    #[ignore_malloc_size_of = "WindowProxyHandler does not properly implement it anyway"]
37    /// The WindowProxy proxy handler for this global.
38    pub(crate) windowproxy_handler: &'static WindowProxyHandler,
39}
40
41impl GlobalStaticData {
42    /// Creates a new GlobalStaticData.
43    pub(crate) fn new() -> GlobalStaticData {
44        GlobalStaticData {
45            windowproxy_handler: WindowProxyHandler::proxy_handler(),
46        }
47    }
48}
49
50pub(crate) use script_bindings::utils::*;
51
52/// Returns a JSVal representing the frozen JavaScript array
53pub(crate) fn to_frozen_array<T: ToJSValConvertible>(
54    cx: &mut JSContext,
55    convertibles: &[T],
56    mut rval: MutableHandleValue,
57) {
58    script_bindings::conversions::SafeToJSValConvertible::safe_to_jsval(
59        convertibles,
60        cx,
61        rval.reborrow(),
62    );
63
64    rooted!(&in(cx) let obj = rval.to_object());
65    unsafe { JS_FreezeObject(cx, obj.handle()) };
66}
67
68/// Returns wether `obj` is a platform object using static unwrap
69/// <https://heycam.github.io/webidl/#dfn-platform-object>
70pub(crate) fn is_platform_object_static(obj: *mut JSObject) -> bool {
71    is_platform_object(obj, &|o| unsafe { UnwrapObjectStatic(o) })
72}
73
74fn is_platform_object(
75    obj: *mut JSObject,
76    unwrap_obj: &dyn Fn(*mut JSObject) -> *mut JSObject,
77) -> bool {
78    unsafe {
79        // Fast-path the common case
80        let mut clasp = get_object_class(obj);
81        if is_dom_class(&*clasp) {
82            return true;
83        }
84        // Now for simplicity check for security wrappers before anything else
85        if IsWrapper(obj) {
86            let unwrapped_obj = unwrap_obj(obj);
87            if unwrapped_obj.is_null() {
88                return false;
89            }
90            clasp = get_object_class(obj);
91        }
92        // TODO also check if JS_IsArrayBufferObject
93        is_dom_class(&*clasp)
94    }
95}
96
97unsafe extern "C" fn instance_class_has_proto_at_depth(
98    clasp: *const js::jsapi::JSClass,
99    proto_id: u32,
100    depth: u32,
101) -> bool {
102    let domclass: *const DOMJSClass = clasp as *const _;
103    let domclass = unsafe { &*domclass };
104    domclass.dom_class.interface_chain[depth as usize] as u32 == proto_id
105}
106
107/// <https://searchfox.org/mozilla-central/rev/c18faaae88b30182e487fa3341bc7d923e22f23a/xpcom/base/CycleCollectedJSRuntime.cpp#792>
108unsafe extern "C" fn instance_class_is_error(clasp: *const js::jsapi::JSClass) -> bool {
109    if !is_dom_class(unsafe { &*clasp }) {
110        return false;
111    }
112    let domclass: *const DOMJSClass = clasp as *const _;
113    let domclass = unsafe { &*domclass };
114    let root_interface = domclass.dom_class.interface_chain[0] as u32;
115    // TODO: support checking bare Exception prototype as well.
116    root_interface == PrototypeList::ID::DOMException as u32
117}
118
119pub(crate) const DOM_CALLBACKS: DOMCallbacks = DOMCallbacks {
120    instanceClassMatchesProto: Some(instance_class_has_proto_at_depth),
121    instanceClassIsError: Some(instance_class_is_error),
122};
123
124/// Eagerly define all relevant WebIDL interface constructors on the
125/// provided global object.
126pub(crate) fn define_all_exposed_interfaces(cx: &mut CurrentRealm, global: &GlobalScope) {
127    for (_, interface) in &InterfaceObjectMap::MAP {
128        (interface.define)(cx, global.reflector().get_jsobject());
129    }
130}
131
132impl DomHelpers<crate::DomTypeHolder> for crate::DomTypeHolder {
133    fn throw_dom_exception(
134        cx: &mut JSContext,
135        global: &<crate::DomTypeHolder as DomTypes>::GlobalScope,
136        result: Error,
137    ) {
138        throw_dom_exception(cx, global, result)
139    }
140
141    fn call_html_constructor<
142        T: DerivedFrom<<crate::DomTypeHolder as DomTypes>::Element> + DomObject,
143    >(
144        cx: &mut JSContext,
145        args: &CallArgs,
146        global: &<crate::DomTypeHolder as DomTypes>::GlobalScope,
147        proto_id: PrototypeList::ID,
148        creator: unsafe fn(&mut JSContext, HandleObject, *mut ProtoOrIfaceArray),
149    ) -> bool {
150        call_html_constructor::<T>(cx, args, global, proto_id, creator)
151    }
152
153    fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<crate::DomTypeHolder>>>> {
154        &settings_stack::STACK
155    }
156
157    fn principals_callbacks() -> &'static JSPrincipalsCallbacks {
158        &PRINCIPALS_CALLBACKS
159    }
160
161    fn interface_map() -> &'static phf::Map<&'static [u8], Interface> {
162        &InterfaceObjectMap::MAP
163    }
164
165    fn push_new_element_queue() {
166        ScriptThread::custom_element_reaction_stack().push_new_element_queue()
167    }
168    fn pop_current_element_queue(cx: &mut JSContext) {
169        ScriptThread::custom_element_reaction_stack().pop_current_element_queue(cx)
170    }
171
172    fn reflect_dom_object_with_cx<T, U>(cx: &mut JSContext, obj: Box<T>, global: &U) -> DomRoot<T>
173    where
174        T: DomObject + DomObjectWrap<crate::DomTypeHolder>,
175        U: DerivedFrom<GlobalScope>,
176    {
177        reflect_dom_object_with_cx(obj, global, cx)
178    }
179
180    fn report_pending_exception(cx: &mut CurrentRealm) {
181        report_pending_exception(cx)
182    }
183}