script/dom/bindings/
utils.rs1use std::cell::RefCell;
8use std::thread::LocalKey;
9
10use js::context::JSContext;
11use js::glue::{IsWrapper, JSPrincipalsCallbacks, UnwrapObjectStatic};
12use js::jsapi::{CallArgs, DOMCallbacks, JSObject};
13use js::realm::CurrentRealm;
14use js::rust::{HandleObject, get_object_class, is_dom_class};
15use script_bindings::interfaces::{DomHelpers, Interface};
16use script_bindings::reflector::{DomObject, DomObjectWrap, reflect_dom_object_with_cx};
17use script_bindings::settings_stack::StackEntry;
18
19use crate::DomTypes;
20use crate::dom::bindings::codegen::{InterfaceObjectMap, PrototypeList};
21use crate::dom::bindings::constructor::call_html_constructor;
22use crate::dom::bindings::conversions::DerivedFrom;
23use crate::dom::bindings::error::{Error, report_pending_exception, throw_dom_exception};
24use crate::dom::bindings::principals::PRINCIPALS_CALLBACKS;
25use crate::dom::bindings::root::DomRoot;
26use crate::dom::bindings::settings_stack;
27use crate::dom::globalscope::GlobalScope;
28use crate::dom::windowproxy::WindowProxyHandler;
29use crate::script_thread::ScriptThread;
30
31#[derive(JSTraceable, MallocSizeOf)]
32pub(crate) struct GlobalStaticData {
34 #[ignore_malloc_size_of = "WindowProxyHandler does not properly implement it anyway"]
35 pub(crate) windowproxy_handler: &'static WindowProxyHandler,
37}
38
39impl GlobalStaticData {
40 pub(crate) fn new() -> GlobalStaticData {
42 GlobalStaticData {
43 windowproxy_handler: WindowProxyHandler::proxy_handler(),
44 }
45 }
46}
47
48pub(crate) use script_bindings::utils::*;
49
50pub(crate) fn is_platform_object_static(obj: *mut JSObject) -> bool {
53 is_platform_object(obj, &|o| unsafe { UnwrapObjectStatic(o) })
54}
55
56fn is_platform_object(
57 obj: *mut JSObject,
58 unwrap_obj: &dyn Fn(*mut JSObject) -> *mut JSObject,
59) -> bool {
60 unsafe {
61 let mut clasp = get_object_class(obj);
63 if is_dom_class(&*clasp) {
64 return true;
65 }
66 if IsWrapper(obj) {
68 let unwrapped_obj = unwrap_obj(obj);
69 if unwrapped_obj.is_null() {
70 return false;
71 }
72 clasp = get_object_class(obj);
73 }
74 is_dom_class(&*clasp)
76 }
77}
78
79unsafe extern "C" fn instance_class_has_proto_at_depth(
80 clasp: *const js::jsapi::JSClass,
81 proto_id: u32,
82 depth: u32,
83) -> bool {
84 let domclass: *const DOMJSClass = clasp as *const _;
85 let domclass = unsafe { &*domclass };
86 domclass.dom_class.interface_chain[depth as usize] as u32 == proto_id
87}
88
89unsafe extern "C" fn instance_class_is_error(clasp: *const js::jsapi::JSClass) -> bool {
91 if !is_dom_class(unsafe { &*clasp }) {
92 return false;
93 }
94 let domclass: *const DOMJSClass = clasp as *const _;
95 let domclass = unsafe { &*domclass };
96 let root_interface = domclass.dom_class.interface_chain[0] as u32;
97 root_interface == PrototypeList::ID::DOMException as u32
99}
100
101pub(crate) const DOM_CALLBACKS: DOMCallbacks = DOMCallbacks {
102 instanceClassMatchesProto: Some(instance_class_has_proto_at_depth),
103 instanceClassIsError: Some(instance_class_is_error),
104};
105
106pub(crate) fn define_all_exposed_interfaces(cx: &mut CurrentRealm, global: &GlobalScope) {
109 for (_, interface) in &InterfaceObjectMap::MAP {
110 (interface.define)(cx, global.reflector().get_jsobject());
111 }
112}
113
114impl DomHelpers<crate::DomTypeHolder> for crate::DomTypeHolder {
115 fn throw_dom_exception(
116 cx: &mut JSContext,
117 global: &<crate::DomTypeHolder as DomTypes>::GlobalScope,
118 result: Error,
119 ) {
120 throw_dom_exception(cx, global, result)
121 }
122
123 fn call_html_constructor<
124 T: DerivedFrom<<crate::DomTypeHolder as DomTypes>::Element> + DomObject,
125 >(
126 cx: &mut JSContext,
127 args: &CallArgs,
128 global: &<crate::DomTypeHolder as DomTypes>::GlobalScope,
129 proto_id: PrototypeList::ID,
130 creator: unsafe fn(&mut JSContext, HandleObject, *mut ProtoOrIfaceArray),
131 ) -> bool {
132 call_html_constructor::<T>(cx, args, global, proto_id, creator)
133 }
134
135 fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<crate::DomTypeHolder>>>> {
136 &settings_stack::STACK
137 }
138
139 fn principals_callbacks() -> &'static JSPrincipalsCallbacks {
140 &PRINCIPALS_CALLBACKS
141 }
142
143 fn interface_map() -> &'static phf::Map<&'static [u8], Interface> {
144 &InterfaceObjectMap::MAP
145 }
146
147 fn push_new_element_queue() {
148 ScriptThread::custom_element_reaction_stack().push_new_element_queue()
149 }
150 fn pop_current_element_queue(cx: &mut JSContext) {
151 ScriptThread::custom_element_reaction_stack().pop_current_element_queue(cx)
152 }
153
154 fn reflect_dom_object_with_cx<T, U>(cx: &mut JSContext, obj: Box<T>, global: &U) -> DomRoot<T>
155 where
156 T: DomObject + DomObjectWrap<crate::DomTypeHolder>,
157 U: DerivedFrom<GlobalScope>,
158 {
159 reflect_dom_object_with_cx(obj, global, cx)
160 }
161
162 fn report_pending_exception(cx: &mut CurrentRealm) {
163 report_pending_exception(cx)
164 }
165}