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