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::conversions::ToJSValConvertible;
11use js::glue::{IsWrapper, JSPrincipalsCallbacks, UnwrapObjectDynamic, UnwrapObjectStatic};
12use js::jsapi::{
13    CallArgs, DOMCallbacks, HandleObject as RawHandleObject, JS_FreezeObject, JSContext, JSObject,
14};
15use js::rust::{HandleObject, MutableHandleValue, get_object_class, is_dom_class};
16use script_bindings::conversions::SafeToJSValConvertible;
17use script_bindings::interfaces::{DomHelpers, Interface};
18use script_bindings::settings_stack::StackEntry;
19
20use crate::DomTypes;
21use crate::dom::bindings::codegen::{InterfaceObjectMap, PrototypeList};
22use crate::dom::bindings::constructor::call_html_constructor;
23use crate::dom::bindings::conversions::DerivedFrom;
24use crate::dom::bindings::error::{Error, report_pending_exception, throw_dom_exception};
25use crate::dom::bindings::principals::PRINCIPALS_CALLBACKS;
26use crate::dom::bindings::proxyhandler::is_platform_object_same_origin;
27use crate::dom::bindings::reflector::{DomObject, DomObjectWrap, reflect_dom_object};
28use crate::dom::bindings::root::DomRoot;
29use crate::dom::bindings::settings_stack;
30use crate::dom::globalscope::GlobalScope;
31use crate::dom::windowproxy::WindowProxyHandler;
32use crate::realms::InRealm;
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    convertibles.safe_to_jsval(cx, rval.reborrow());
63
64    rooted!(in(*cx) let obj = rval.to_object());
65    unsafe { JS_FreezeObject(*cx, RawHandleObject::from(obj.handle())) };
66}
67
68/// Returns wether `obj` is a platform object using dynamic unwrap
69/// <https://heycam.github.io/webidl/#dfn-platform-object>
70#[allow(dead_code)]
71pub(crate) fn is_platform_object_dynamic(obj: *mut JSObject, cx: *mut JSContext) -> bool {
72    is_platform_object(obj, &|o| unsafe {
73        UnwrapObjectDynamic(o, cx, /* stopAtWindowProxy = */ false)
74    })
75}
76
77/// Returns wether `obj` is a platform object using static unwrap
78/// <https://heycam.github.io/webidl/#dfn-platform-object>
79pub(crate) fn is_platform_object_static(obj: *mut JSObject) -> bool {
80    is_platform_object(obj, &|o| unsafe { UnwrapObjectStatic(o) })
81}
82
83fn is_platform_object(
84    obj: *mut JSObject,
85    unwrap_obj: &dyn Fn(*mut JSObject) -> *mut JSObject,
86) -> bool {
87    unsafe {
88        // Fast-path the common case
89        let mut clasp = get_object_class(obj);
90        if is_dom_class(&*clasp) {
91            return true;
92        }
93        // Now for simplicity check for security wrappers before anything else
94        if IsWrapper(obj) {
95            let unwrapped_obj = unwrap_obj(obj);
96            if unwrapped_obj.is_null() {
97                return false;
98            }
99            clasp = get_object_class(obj);
100        }
101        // TODO also check if JS_IsArrayBufferObject
102        is_dom_class(&*clasp)
103    }
104}
105
106unsafe extern "C" fn instance_class_has_proto_at_depth(
107    clasp: *const js::jsapi::JSClass,
108    proto_id: u32,
109    depth: u32,
110) -> bool {
111    let domclass: *const DOMJSClass = clasp as *const _;
112    let domclass = &*domclass;
113    domclass.dom_class.interface_chain[depth as usize] as u32 == proto_id
114}
115
116/// <https://searchfox.org/mozilla-central/rev/c18faaae88b30182e487fa3341bc7d923e22f23a/xpcom/base/CycleCollectedJSRuntime.cpp#792>
117unsafe extern "C" fn instance_class_is_error(clasp: *const js::jsapi::JSClass) -> bool {
118    if !is_dom_class(&*clasp) {
119        return false;
120    }
121    let domclass: *const DOMJSClass = clasp as *const _;
122    let domclass = &*domclass;
123    let root_interface = domclass.dom_class.interface_chain[0] as u32;
124    // TODO: support checking bare Exception prototype as well.
125    root_interface == PrototypeList::ID::DOMException as u32
126}
127
128#[allow(missing_docs)] // FIXME
129pub(crate) const DOM_CALLBACKS: DOMCallbacks = DOMCallbacks {
130    instanceClassMatchesProto: Some(instance_class_has_proto_at_depth),
131    instanceClassIsError: Some(instance_class_is_error),
132};
133
134/// Eagerly define all relevant WebIDL interface constructors on the
135/// provided global object.
136pub(crate) fn define_all_exposed_interfaces(
137    global: &GlobalScope,
138    _in_realm: InRealm,
139    _can_gc: CanGc,
140) {
141    let cx = GlobalScope::get_cx();
142    for (_, interface) in &InterfaceObjectMap::MAP {
143        (interface.define)(cx, global.reflector().get_jsobject());
144    }
145}
146
147impl DomHelpers<crate::DomTypeHolder> for crate::DomTypeHolder {
148    fn throw_dom_exception(
149        cx: SafeJSContext,
150        global: &<crate::DomTypeHolder as DomTypes>::GlobalScope,
151        result: Error,
152        can_gc: CanGc,
153    ) {
154        throw_dom_exception(cx, global, result, can_gc)
155    }
156
157    fn call_html_constructor<
158        T: DerivedFrom<<crate::DomTypeHolder as DomTypes>::Element> + DomObject,
159    >(
160        cx: SafeJSContext,
161        args: &CallArgs,
162        global: &<crate::DomTypeHolder as DomTypes>::GlobalScope,
163        proto_id: PrototypeList::ID,
164        creator: unsafe fn(SafeJSContext, HandleObject, *mut ProtoOrIfaceArray),
165        can_gc: CanGc,
166    ) -> bool {
167        call_html_constructor::<T>(cx, args, global, proto_id, creator, can_gc)
168    }
169
170    fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<crate::DomTypeHolder>>>> {
171        &settings_stack::STACK
172    }
173
174    fn principals_callbacks() -> &'static JSPrincipalsCallbacks {
175        &PRINCIPALS_CALLBACKS
176    }
177
178    fn is_platform_object_same_origin(cx: SafeJSContext, obj: RawHandleObject) -> bool {
179        unsafe { is_platform_object_same_origin(cx, obj) }
180    }
181
182    fn interface_map() -> &'static phf::Map<&'static [u8], Interface> {
183        &InterfaceObjectMap::MAP
184    }
185
186    fn push_new_element_queue() {
187        ScriptThread::custom_element_reaction_stack().push_new_element_queue()
188    }
189    fn pop_current_element_queue(can_gc: CanGc) {
190        ScriptThread::custom_element_reaction_stack().pop_current_element_queue(can_gc)
191    }
192
193    fn reflect_dom_object<T, U>(obj: Box<T>, global: &U, can_gc: CanGc) -> DomRoot<T>
194    where
195        T: DomObject + DomObjectWrap<crate::DomTypeHolder>,
196        U: DerivedFrom<GlobalScope>,
197    {
198        reflect_dom_object(obj, global, can_gc)
199    }
200
201    fn report_pending_exception(
202        cx: SafeJSContext,
203        dispatch_event: bool,
204        realm: InRealm,
205        can_gc: CanGc,
206    ) {
207        report_pending_exception(cx, dispatch_event, realm, can_gc)
208    }
209}