Skip to main content

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
5use script_bindings::realms::InRealm;
6use script_bindings::reflector::DomGlobalGeneric;
7use script_bindings::root::DomRoot;
8
9use crate::DomTypeHolder;
10use crate::dom::types::GlobalScope;
11use crate::realms::enter_realm;
12
13pub(crate) trait DomGlobal {
14    /// Returns the [relevant global] in whatever realm is currently active.
15    ///
16    /// [relevant global]: https://html.spec.whatwg.org/multipage/#concept-relevant-global
17    fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope>;
18
19    /// Returns the [relevant global] in the same realm as the callee object.
20    /// If you know the callee's realm is already the current realm, it is
21    /// more efficient to call [DomGlobal::global_] instead.
22    ///
23    /// [relevant global]: https://html.spec.whatwg.org/multipage/#concept-relevant-global
24    fn global(&self) -> DomRoot<GlobalScope>;
25}
26
27impl<T: DomGlobalGeneric<DomTypeHolder>> DomGlobal for T {
28    fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope> {
29        <Self as DomGlobalGeneric<DomTypeHolder>>::global_(self, realm)
30    }
31
32    fn global(&self) -> DomRoot<GlobalScope> {
33        let realm = enter_realm(self);
34        <Self as DomGlobalGeneric<DomTypeHolder>>::global_(self, InRealm::entered(&realm))
35    }
36}