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