script_bindings/
interfaces.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 std::cell::RefCell;
6use std::thread::LocalKey;
7
8use js::glue::JSPrincipalsCallbacks;
9use js::jsapi::{CallArgs, HandleObject as RawHandleObject, JSContext as RawJSContext, JSObject};
10use js::realm::CurrentRealm;
11use js::rust::{HandleObject, MutableHandleObject};
12use servo_url::{MutableOrigin, ServoUrl};
13
14use crate::DomTypes;
15use crate::codegen::PrototypeList;
16use crate::conversions::DerivedFrom;
17use crate::error::Error;
18use crate::realms::InRealm;
19use crate::reflector::{DomObject, DomObjectWrap};
20use crate::root::DomRoot;
21use crate::script_runtime::{CanGc, JSContext};
22use crate::settings_stack::StackEntry;
23use crate::utils::ProtoOrIfaceArray;
24
25/// Operations that can be invoked for a WebIDL interface against
26/// a global object.
27///
28/// <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/bindings/WebIDLGlobalNameHash.h#L24>
29pub struct Interface {
30    /// Define the JS object for this interface on the given global.
31    pub define: fn(&mut js::context::JSContext, HandleObject),
32    /// Returns true if this interface's conditions are met for the given global.
33    pub enabled: fn(&mut js::context::JSContext, HandleObject) -> bool,
34}
35
36/// Operations that must be invoked from the generated bindings.
37pub trait DomHelpers<D: DomTypes> {
38    fn throw_dom_exception(cx: JSContext, global: &D::GlobalScope, result: Error, can_gc: CanGc);
39
40    fn call_html_constructor<T: DerivedFrom<D::Element> + DomObject>(
41        cx: &mut js::context::JSContext,
42        args: &CallArgs,
43        global: &D::GlobalScope,
44        proto_id: PrototypeList::ID,
45        creator: unsafe fn(JSContext, HandleObject, *mut ProtoOrIfaceArray),
46    ) -> bool;
47
48    fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<D>>>>;
49
50    fn principals_callbacks() -> &'static JSPrincipalsCallbacks;
51
52    fn is_platform_object_same_origin(cx: &CurrentRealm, obj: RawHandleObject) -> bool;
53
54    fn interface_map() -> &'static phf::Map<&'static [u8], Interface>;
55
56    fn push_new_element_queue();
57    fn pop_current_element_queue(can_gc: CanGc);
58
59    fn reflect_dom_object<T, U>(obj: Box<T>, global: &U, can_gc: CanGc) -> DomRoot<T>
60    where
61        T: DomObject + DomObjectWrap<D>,
62        U: DerivedFrom<D::GlobalScope>;
63
64    fn report_pending_exception(cx: JSContext, dispatch_event: bool, realm: InRealm, can_gc: CanGc);
65}
66
67/// Operations that must be invoked from the generated bindings.
68#[expect(unsafe_code)]
69pub trait GlobalScopeHelpers<D: DomTypes> {
70    fn from_current_realm(realm: &'_ CurrentRealm) -> DomRoot<D::GlobalScope>;
71    /// # Safety
72    /// `cx` must point to a valid, non-null RawJSContext.
73    unsafe fn from_context(cx: *mut RawJSContext, realm: InRealm) -> DomRoot<D::GlobalScope>;
74    fn get_cx() -> JSContext;
75    /// # Safety
76    /// `obj` must point to a valid, non-null JSObject.
77    unsafe fn from_object(obj: *mut JSObject) -> DomRoot<D::GlobalScope>;
78    fn from_reflector(reflector: &impl DomObject, realm: InRealm) -> DomRoot<D::GlobalScope>;
79
80    fn origin(&self) -> &MutableOrigin;
81
82    fn incumbent() -> Option<DomRoot<D::GlobalScope>>;
83
84    fn perform_a_microtask_checkpoint(&self, can_gc: CanGc);
85
86    fn get_url(&self) -> ServoUrl;
87
88    fn is_secure_context(&self) -> bool;
89}
90
91pub trait DocumentHelpers {
92    fn ensure_safe_to_run_script_or_layout(&self);
93}
94
95pub trait ServoInternalsHelpers {
96    fn is_servo_internal(cx: JSContext, global: HandleObject) -> bool;
97}
98
99pub trait TestBindingHelpers {
100    fn condition_satisfied(cx: JSContext, global: HandleObject) -> bool;
101    fn condition_unsatisfied(cx: JSContext, global: HandleObject) -> bool;
102}
103
104pub trait WebGL2RenderingContextHelpers {
105    fn is_webgl2_enabled(cx: JSContext, global: HandleObject) -> bool;
106}
107
108pub trait WindowHelpers {
109    fn create_named_properties_object(
110        cx: JSContext,
111        proto: HandleObject,
112        object: MutableHandleObject,
113    );
114}