script/dom/bindings/
reflector.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//! The `Reflector` struct.
6
7use js::rust::HandleObject;
8use script_bindings::script_runtime::temp_cx;
9
10use crate::DomTypes;
11use crate::dom::bindings::conversions::DerivedFrom;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::globalscope::GlobalScope;
14use crate::realms::{InRealm, enter_realm};
15use crate::script_runtime::CanGc;
16
17/// Create the reflector for a new DOM object and yield ownership to the
18/// reflector.
19pub(crate) fn reflect_dom_object<D, T, U>(obj: Box<T>, global: &U, _can_gc: CanGc) -> DomRoot<T>
20where
21    D: DomTypes,
22    T: DomObject + DomObjectWrap<D>,
23    U: DerivedFrom<D::GlobalScope>,
24{
25    let global_scope = global.upcast();
26    let mut cx = unsafe { temp_cx() };
27    unsafe { T::WRAP(&mut cx, global_scope, None, obj) }
28}
29
30pub(crate) fn reflect_dom_object_with_proto<D, T, U>(
31    obj: Box<T>,
32    global: &U,
33    proto: Option<HandleObject>,
34    _can_gc: CanGc,
35) -> DomRoot<T>
36where
37    D: DomTypes,
38    T: DomObject + DomObjectWrap<D>,
39    U: DerivedFrom<D::GlobalScope>,
40{
41    let global_scope = global.upcast();
42    let mut cx = unsafe { temp_cx() };
43    unsafe { T::WRAP(&mut cx, global_scope, proto, obj) }
44}
45
46/// Create the reflector for a new DOM object and yield ownership to the
47/// reflector.
48pub(crate) fn reflect_dom_object_with_cx<D, T, U>(
49    obj: Box<T>,
50    global: &U,
51    cx: &mut js::context::JSContext,
52) -> DomRoot<T>
53where
54    D: DomTypes,
55    T: DomObject + DomObjectWrap<D>,
56    U: DerivedFrom<D::GlobalScope>,
57{
58    let global_scope = global.upcast();
59    unsafe { T::WRAP(cx, global_scope, None, obj) }
60}
61
62/// Create the reflector for a new DOM object and yield ownership to the
63/// reflector.
64pub(crate) fn reflect_dom_object_with_proto_and_cx<D, T, U>(
65    obj: Box<T>,
66    global: &U,
67    proto: Option<HandleObject>,
68    cx: &mut js::context::JSContext,
69) -> DomRoot<T>
70where
71    D: DomTypes,
72    T: DomObject + DomObjectWrap<D>,
73    U: DerivedFrom<D::GlobalScope>,
74{
75    let global_scope = global.upcast();
76    unsafe { T::WRAP(cx, global_scope, proto, obj) }
77}
78
79pub(crate) trait DomGlobal {
80    /// Returns the [relevant global] in whatever realm is currently active.
81    ///
82    /// [relevant global]: https://html.spec.whatwg.org/multipage/#concept-relevant-global
83    fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope>;
84
85    /// Returns the [relevant global] in the same realm as the callee object.
86    /// If you know the callee's realm is already the current realm, it is
87    /// more efficient to call [DomGlobal::global_] instead.
88    ///
89    /// [relevant global]: https://html.spec.whatwg.org/multipage/#concept-relevant-global
90    fn global(&self) -> DomRoot<GlobalScope>;
91}
92
93impl<T: DomGlobalGeneric<crate::DomTypeHolder>> DomGlobal for T {
94    fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope> {
95        <Self as DomGlobalGeneric<crate::DomTypeHolder>>::global_(self, realm)
96    }
97    fn global(&self) -> DomRoot<GlobalScope> {
98        let realm = enter_realm(self);
99        <Self as DomGlobalGeneric<crate::DomTypeHolder>>::global_(self, InRealm::entered(&realm))
100    }
101}
102
103pub(crate) use script_bindings::reflector::*;