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