Files
servo/components/script/dom/webgpu/gpuuncapturederrorevent.rs
Martin Robinson 291e9e57cb script: Readily accept all event names as Atoms (#43066)
Accepting the name of the event as a `DOMString` means that when events
are constructed from within Servo (for instance via input event
handling), the static strings are converted to an owned `String`
wrapped in a `DOMString` and then finally converted to an `Atom` in
`Event`. This makes it so that all non-generated `Event` constructors
accept `Atom`, which can avoid a conversion entirely when the atom
already exists on the Stylo atoms list and eliminate one in-memory copy
in the case that it isn't on the atom list.

Eventually, all event names can be on the atom list and we can eliminate
all copies. This is a tiny optimization, but also makes the code much
friendlier everywhere.

Testing: This should not change behavior so should be covered by
existing test.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2026-03-06 19:25:33 +00:00

87 lines
2.7 KiB
Rust

/* 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/. */
use dom_struct::dom_struct;
use js::rust::HandleObject;
use stylo_atoms::Atom;
use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
GPUUncapturedErrorEventInit, GPUUncapturedErrorEventMethods,
};
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::event::Event;
use crate::dom::globalscope::GlobalScope;
use crate::dom::webgpu::gpuerror::GPUError;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct GPUUncapturedErrorEvent {
event: Event,
#[ignore_malloc_size_of = "Because it is non-owning"]
gpu_error: Dom<GPUError>,
}
impl GPUUncapturedErrorEvent {
fn new_inherited(init: &GPUUncapturedErrorEventInit) -> Self {
Self {
gpu_error: Dom::from_ref(&init.error),
event: Event::new_inherited(),
}
}
pub(crate) fn new(
global: &GlobalScope,
event_type: Atom,
init: &GPUUncapturedErrorEventInit,
can_gc: CanGc,
) -> DomRoot<Self> {
Self::new_with_proto(global, None, event_type, init, can_gc)
}
fn new_with_proto(
global: &GlobalScope,
proto: Option<HandleObject>,
event_type: Atom,
init: &GPUUncapturedErrorEventInit,
can_gc: CanGc,
) -> DomRoot<Self> {
let event = reflect_dom_object_with_proto(
Box::new(GPUUncapturedErrorEvent::new_inherited(init)),
global,
proto,
can_gc,
);
event
.event
.init_event(event_type, init.parent.bubbles, init.parent.cancelable);
event
}
}
impl GPUUncapturedErrorEventMethods<crate::DomTypeHolder> for GPUUncapturedErrorEvent {
/// <https://gpuweb.github.io/gpuweb/#dom-gpuuncapturederrorevent-gpuuncapturederrorevent>
fn Constructor(
global: &GlobalScope,
proto: Option<HandleObject>,
can_gc: CanGc,
event_type: DOMString,
init: &GPUUncapturedErrorEventInit,
) -> DomRoot<Self> {
GPUUncapturedErrorEvent::new_with_proto(global, proto, event_type.into(), init, can_gc)
}
/// <https://gpuweb.github.io/gpuweb/#dom-gpuuncapturederrorevent-error>
fn Error(&self) -> DomRoot<GPUError> {
DomRoot::from_ref(&self.gpu_error)
}
/// <https://dom.spec.whatwg.org/#dom-event-istrusted>
fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
}