/
githubmirror
/
servo
Обзор
Документация
Войти
/
githubmirror
/
servo
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
components/script/dom/bindings/utils.rs
165 строк
6 KB
Narfinger
script: Move more into script_webgpu crate (#46534)
17 июл 2026, 12:20
Не верифицирован
17 июл 2026, 12:20
1d37477
Код
Авторство
О чём код?
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! Various utilities to glue JavaScript and the DOM implementation together. use std::cell::RefCell; use std::thread::LocalKey; use js::context::JSContext; use js::glue::{IsWrapper, JSPrincipalsCallbacks, UnwrapObjectStatic}; use js::jsapi::{CallArgs, DOMCallbacks, JSObject}; use js::realm::CurrentRealm; use js::rust::{HandleObject, get_object_class, is_dom_class}; use script_bindings::interfaces::{DomHelpers, Interface}; use script_bindings::reflector::{DomObject, DomObjectWrap, reflect_dom_object_with_cx}; use script_bindings::settings_stack::StackEntry; use crate::DomTypes; use crate::dom::bindings::codegen::{InterfaceObjectMap, PrototypeList}; use crate::dom::bindings::constructor::call_html_constructor; use crate::dom::bindings::conversions::DerivedFrom; use crate::dom::bindings::error::{Error, report_pending_exception, throw_dom_exception}; use crate::dom::bindings::principals::PRINCIPALS_CALLBACKS; use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::settings_stack; use crate::dom::globalscope::GlobalScope; use crate::dom::windowproxy::WindowProxyHandler; use crate::script_thread::ScriptThread; #[derive(JSTraceable, MallocSizeOf)] /// Static data associated with a global object. pub(crate) struct GlobalStaticData { #[ignore_malloc_size_of = "WindowProxyHandler does not properly implement it anyway"] /// The WindowProxy proxy handler for this global. pub(crate) windowproxy_handler: &'static WindowProxyHandler, } impl GlobalStaticData { /// Creates a new GlobalStaticData. pub(crate) fn new() -> GlobalStaticData { GlobalStaticData { windowproxy_handler: WindowProxyHandler::proxy_handler(), } } } pub(crate) use script_bindings::utils::*; /// Returns wether `obj` is a platform object using static unwrap /// <https://heycam.github.io/webidl/#dfn-platform-object> pub(crate) fn is_platform_object_static(obj: *mut JSObject) -> bool { is_platform_object(obj, &|o| unsafe { UnwrapObjectStatic(o) }) } fn is_platform_object( obj: *mut JSObject, unwrap_obj: &dyn Fn(*mut JSObject) -> *mut JSObject, ) -> bool { unsafe { // Fast-path the common case let mut clasp = get_object_class(obj); if is_dom_class(&*clasp) { return true; } // Now for simplicity check for security wrappers before anything else if IsWrapper(obj) { let unwrapped_obj = unwrap_obj(obj); if unwrapped_obj.is_null() { return false; } clasp = get_object_class(obj); } // TODO also check if JS_IsArrayBufferObject is_dom_class(&*clasp) } } unsafe extern "C" fn instance_class_has_proto_at_depth( clasp: *const js::jsapi::JSClass, proto_id: u32, depth: u32, ) -> bool { let domclass: *const DOMJSClass = clasp as *const _; let domclass = unsafe { &*domclass }; domclass.dom_class.interface_chain[depth as usize] as u32 == proto_id } /// <https://searchfox.org/mozilla-central/rev/c18faaae88b30182e487fa3341bc7d923e22f23a/xpcom/base/CycleCollectedJSRuntime.cpp#792> unsafe extern "C" fn instance_class_is_error(clasp: *const js::jsapi::JSClass) -> bool { if !is_dom_class(unsafe { &*clasp }) { return false; } let domclass: *const DOMJSClass = clasp as *const _; let domclass = unsafe { &*domclass }; let root_interface = domclass.dom_class.interface_chain[0] as u32; // TODO: support checking bare Exception prototype as well. root_interface == PrototypeList::ID::DOMException as u32 } pub(crate) const DOM_CALLBACKS: DOMCallbacks = DOMCallbacks { instanceClassMatchesProto: Some(instance_class_has_proto_at_depth), instanceClassIsError: Some(instance_class_is_error), }; /// Eagerly define all relevant WebIDL interface constructors on the /// provided global object. pub(crate) fn define_all_exposed_interfaces(cx: &mut CurrentRealm, global: &GlobalScope) { for (_, interface) in &InterfaceObjectMap::MAP { (interface.define)(cx, global.reflector().get_jsobject()); } } impl DomHelpers<crate::DomTypeHolder> for crate::DomTypeHolder { fn throw_dom_exception( cx: &mut JSContext, global: &<crate::DomTypeHolder as DomTypes>::GlobalScope, result: Error, ) { throw_dom_exception(cx, global, result) } fn call_html_constructor< T: DerivedFrom<<crate::DomTypeHolder as DomTypes>::Element> + DomObject, >( cx: &mut JSContext, args: &CallArgs, global: &<crate::DomTypeHolder as DomTypes>::GlobalScope, proto_id: PrototypeList::ID, creator: unsafe fn(&mut JSContext, HandleObject, *mut ProtoOrIfaceArray), ) -> bool { call_html_constructor::<T>(cx, args, global, proto_id, creator) } fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<crate::DomTypeHolder>>>> { &settings_stack::STACK } fn principals_callbacks() -> &'static JSPrincipalsCallbacks { &PRINCIPALS_CALLBACKS } fn interface_map() -> &'static phf::Map<&'static [u8], Interface> { &InterfaceObjectMap::MAP } fn push_new_element_queue() { ScriptThread::custom_element_reaction_stack().push_new_element_queue() } fn pop_current_element_queue(cx: &mut JSContext) { ScriptThread::custom_element_reaction_stack().pop_current_element_queue(cx) } fn reflect_dom_object_with_cx<T, U>(cx: &mut JSContext, obj: Box<T>, global: &U) -> DomRoot<T> where T: DomObject + DomObjectWrap<crate::DomTypeHolder>, U: DerivedFrom<GlobalScope>, { reflect_dom_object_with_cx(obj, global, cx) } fn report_pending_exception(cx: &mut CurrentRealm) { report_pending_exception(cx) } }