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(JSContext, HandleObject),
32    /// Returns true if this interface's conditions are met for the given global.
33    pub enabled: fn(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: JSContext,
42        args: &CallArgs,
43        global: &D::GlobalScope,
44        proto_id: PrototypeList::ID,
45        creator: unsafe fn(JSContext, HandleObject, *mut ProtoOrIfaceArray),
46        can_gc: CanGc,
47    ) -> bool;
48
49    fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<D>>>>;
50
51    fn principals_callbacks() -> &'static JSPrincipalsCallbacks;
52
53    fn is_platform_object_same_origin(cx: &CurrentRealm, obj: RawHandleObject) -> bool;
54
55    fn interface_map() -> &'static phf::Map<&'static [u8], Interface>;
56
57    fn push_new_element_queue();
58    fn pop_current_element_queue(can_gc: CanGc);
59
60    fn reflect_dom_object<T, U>(obj: Box<T>, global: &U, can_gc: CanGc) -> DomRoot<T>
61    where
62        T: DomObject + DomObjectWrap<D>,
63        U: DerivedFrom<D::GlobalScope>;
64
65    fn report_pending_exception(cx: JSContext, dispatch_event: bool, realm: InRealm, can_gc: CanGc);
66}
67
68/// Operations that must be invoked from the generated bindings.
69#[expect(unsafe_code)]
70pub trait GlobalScopeHelpers<D: DomTypes> {
71    fn from_current_realm(realm: &'_ CurrentRealm) -> DomRoot<D::GlobalScope>;
72    /// # Safety
73    /// `cx` must point to a valid, non-null RawJSContext.
74    unsafe fn from_context(cx: *mut RawJSContext, realm: InRealm) -> DomRoot<D::GlobalScope>;
75    fn get_cx() -> JSContext;
76    /// # Safety
77    /// `obj` must point to a valid, non-null JSObject.
78    unsafe fn from_object(obj: *mut JSObject) -> DomRoot<D::GlobalScope>;
79    fn from_reflector(reflector: &impl DomObject, realm: InRealm) -> DomRoot<D::GlobalScope>;
80
81    fn origin(&self) -> &MutableOrigin;
82
83    fn incumbent() -> Option<DomRoot<D::GlobalScope>>;
84
85    fn perform_a_microtask_checkpoint(&self, can_gc: CanGc);
86
87    fn get_url(&self) -> ServoUrl;
88
89    fn is_secure_context(&self) -> bool;
90}
91
92pub trait DocumentHelpers {
93    fn ensure_safe_to_run_script_or_layout(&self);
94}
95
96pub trait ServoInternalsHelpers {
97    fn is_servo_internal(cx: JSContext, global: HandleObject) -> bool;
98}
99
100pub trait TestBindingHelpers {
101    fn condition_satisfied(cx: JSContext, global: HandleObject) -> bool;
102    fn condition_unsatisfied(cx: JSContext, global: HandleObject) -> bool;
103}
104
105pub trait WebGL2RenderingContextHelpers {
106    fn is_webgl2_enabled(cx: JSContext, global: HandleObject) -> bool;
107}
108
109pub trait WindowHelpers {
110    fn create_named_properties_object(
111        cx: JSContext,
112        proto: HandleObject,
113        object: MutableHandleObject,
114    );
115}