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::ops::Deref;
7use std::rc::Rc;
8use std::thread::LocalKey;
9
10use js::context::JSContext;
11use js::conversions::{FromJSValConvertible, ToJSValConvertible};
12use js::gc::Traceable;
13use js::glue::JSPrincipalsCallbacks;
14use js::jsapi::{CallArgs, JSObject};
15use js::realm::CurrentRealm;
16use js::rust::{HandleObject, MutableHandleObject};
17use malloc_size_of::MallocSizeOf;
18use servo_base::id::PipelineId;
19use servo_constellation_traits::ScriptToConstellationChan;
20use servo_url::{MutableOrigin, ServoUrl};
21
22use crate::DomTypes;
23use crate::codegen::PrototypeList;
24use crate::conversions::DerivedFrom;
25use crate::error::Error;
26use crate::reflector::{DomObject, DomObjectWrap};
27use crate::root::DomRoot;
28use crate::settings_stack::StackEntry;
29use crate::utils::ProtoOrIfaceArray;
30
31/// Operations that can be invoked for a WebIDL interface against
32/// a global object.
33///
34/// <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/bindings/WebIDLGlobalNameHash.h#L24>
35pub struct Interface {
36    /// Define the JS object for this interface on the given global.
37    pub define: fn(&mut JSContext, HandleObject),
38    /// Returns true if this interface's conditions are met for the given global.
39    pub enabled: fn(&mut JSContext, HandleObject) -> bool,
40}
41
42/// Operations that must be invoked from the generated bindings.
43pub trait DomHelpers<D: DomTypes> {
44    fn throw_dom_exception(cx: &mut JSContext, global: &D::GlobalScope, result: Error);
45
46    fn call_html_constructor<T: DerivedFrom<D::Element> + DomObject>(
47        cx: &mut JSContext,
48        args: &CallArgs,
49        global: &D::GlobalScope,
50        proto_id: PrototypeList::ID,
51        creator: unsafe fn(&mut JSContext, HandleObject, *mut ProtoOrIfaceArray),
52    ) -> bool;
53
54    fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<D>>>>;
55
56    fn principals_callbacks() -> &'static JSPrincipalsCallbacks;
57
58    fn interface_map() -> &'static phf::Map<&'static [u8], Interface>;
59
60    fn push_new_element_queue();
61    fn pop_current_element_queue(cx: &mut JSContext);
62
63    fn reflect_dom_object_with_cx<T, U>(cx: &mut JSContext, obj: Box<T>, global: &U) -> DomRoot<T>
64    where
65        T: DomObject + DomObjectWrap<D>,
66        U: DerivedFrom<D::GlobalScope>;
67
68    fn report_pending_exception(cx: &mut CurrentRealm);
69}
70
71/// Operations that must be invoked from the generated bindings.
72#[expect(unsafe_code)]
73pub trait GlobalScopeHelpers<D: DomTypes> {
74    fn from_current_realm(realm: &'_ mut CurrentRealm) -> DomRoot<D::GlobalScope>;
75
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) -> 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, cx: &mut JSContext);
86
87    fn get_url(&self) -> ServoUrl;
88
89    fn is_secure_context(&self) -> bool;
90
91    fn pipeline_id(&self) -> PipelineId;
92
93    fn script_to_constellation_chan(&self) -> ScriptToConstellationChan;
94}
95
96pub trait HeapTracedPromiseHelpers<D: DomTypes> {
97    type StackRoot;
98    fn root(&self) -> Self::StackRoot;
99}
100
101pub trait StackRootPromiseHelpers<D: DomTypes> {
102    type HeapTraced;
103    fn to_traced(&self) -> Self::HeapTraced;
104}
105
106pub trait PromiseHelpers<D: DomTypes> {
107    type StackRoot: FromJSValConvertible<Config = ()>
108        + ToJSValConvertible
109        + Deref<Target = D::Promise>
110        + StackRootPromiseHelpers<D, HeapTraced = Self::HeapTraced>;
111    type HeapTraced: Traceable
112        + MallocSizeOf
113        + Deref<Target = D::Promise>
114        + HeapTracedPromiseHelpers<D, StackRoot = Self::StackRoot>;
115    fn new_in_realm(cx: &mut CurrentRealm) -> Rc<D::Promise>;
116    fn new_in_realm_rooted(cx: &mut CurrentRealm) -> Self::StackRoot;
117    fn reject_error(&self, cx: &mut JSContext, error: Error);
118    fn is_rejected(&self) -> bool;
119    fn is_pending(&self) -> bool;
120    fn resolve_native<T: ToJSValConvertible>(&self, cx: &mut JSContext, val: &T);
121}
122
123pub trait DocumentHelpers {
124    fn ensure_safe_to_run_script_or_layout(&self);
125}
126
127pub trait ServoInternalsHelpers {
128    fn is_servo_internal(cx: &mut JSContext, global: HandleObject) -> bool;
129}
130
131pub trait TestBindingHelpers {
132    fn condition_satisfied(cx: &mut JSContext, global: HandleObject) -> bool;
133    fn condition_unsatisfied(cx: &mut JSContext, global: HandleObject) -> bool;
134}
135
136pub trait WebGL2RenderingContextHelpers {
137    fn is_webgl2_enabled(cx: &mut JSContext, global: HandleObject) -> bool;
138}
139
140pub trait WindowHelpers {
141    fn create_named_properties_object(
142        cx: &mut JSContext,
143        proto: HandleObject,
144        object: MutableHandleObject,
145    );
146}
147
148pub trait HasOrigin {
149    fn origin(&self) -> MutableOrigin;
150}