Skip to main content

script_webgpu/
gpuuncapturederrorevent.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 dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use malloc_size_of_derive::MallocSizeOf;
9use script_bindings::DomTypes;
10use script_bindings::codegen::GenericBindings::WebGPUBinding::{
11    GPUUncapturedErrorEventInit, GPUUncapturedErrorEventMethods, GPUUncapturedErrorEventWrap,
12};
13use script_bindings::reflector::reflect_dom_object_with_proto_and_wrap;
14use script_bindings::traits::DomEventTrait;
15use stylo_atoms::Atom;
16
17use crate::JSTraceable;
18use crate::dom::bindings::root::{Dom, DomRoot};
19use crate::dom::bindings::str::DOMString;
20use crate::gpuerror::GPUError;
21use crate::traits::Equivalence;
22
23#[dom_struct]
24pub struct GPUUncapturedErrorEvent<D: DomTypes> {
25    event: D::Event,
26    #[ignore_malloc_size_of = "Because it is non-owning"]
27    gpu_error: Dom<GPUError<D>>,
28}
29
30impl<D> GPUUncapturedErrorEvent<D>
31where
32    D: Equivalence,
33{
34    fn new_inherited(init: &GPUUncapturedErrorEventInit<D>) -> Self {
35        Self {
36            gpu_error: Dom::from_ref(&init.error),
37            event: D::Event::new_inherited(),
38        }
39    }
40
41    pub(crate) fn new(
42        cx: &mut JSContext,
43        global: &D::GlobalScope,
44        event_type: Atom,
45        init: &GPUUncapturedErrorEventInit<D>,
46    ) -> DomRoot<Self> {
47        Self::new_with_proto(cx, global, None, event_type, init)
48    }
49
50    fn new_with_proto(
51        cx: &mut JSContext,
52        global: &D::GlobalScope,
53        proto: Option<HandleObject>,
54        event_type: Atom,
55        init: &GPUUncapturedErrorEventInit<D>,
56    ) -> DomRoot<Self> {
57        let event = reflect_dom_object_with_proto_and_wrap::<D, _, _>(
58            Box::new(GPUUncapturedErrorEvent::new_inherited(init)),
59            global,
60            proto,
61            cx,
62            GPUUncapturedErrorEventWrap::<D>,
63        );
64        event
65            .event
66            .init_event(event_type, init.parent.bubbles, init.parent.cancelable);
67        event
68    }
69}
70
71impl<D> GPUUncapturedErrorEventMethods<D> for GPUUncapturedErrorEvent<D>
72where
73    D: Equivalence,
74    D::Event: DomEventTrait<D>,
75{
76    /// <https://gpuweb.github.io/gpuweb/#dom-gpuuncapturederrorevent-gpuuncapturederrorevent>
77    fn Constructor(
78        cx: &mut js::context::JSContext,
79        global: &D::GlobalScope,
80        proto: Option<HandleObject>,
81        event_type: DOMString,
82        init: &GPUUncapturedErrorEventInit<D>,
83    ) -> DomRoot<Self> {
84        GPUUncapturedErrorEvent::new_with_proto(cx, global, proto, event_type.into(), init)
85    }
86
87    /// <https://gpuweb.github.io/gpuweb/#dom-gpuuncapturederrorevent-error>
88    fn Error(&self) -> DomRoot<GPUError<D>> {
89        DomRoot::from_ref(&self.gpu_error)
90    }
91
92    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
93    fn IsTrusted(&self) -> bool {
94        self.event.IsTrusted()
95    }
96}