script/dom/bindings/
reflector.rs1use 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
17pub(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
46pub(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
62pub(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 fn global_(&self, realm: InRealm) -> DomRoot<GlobalScope>;
84
85 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::*;