Skip to main content

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