Skip to main content

script_webgpu/
gpudevicelostinfo.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::marker::PhantomData;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use malloc_size_of_derive::MallocSizeOf;
10use script_bindings::DomTypes;
11use script_bindings::codegen::GenericBindings::WebGPUBinding::{
12    GPUDeviceLostInfoMethods, GPUDeviceLostInfoWrap, GPUDeviceLostReason,
13};
14use script_bindings::domstring::TracedDOMString;
15use script_bindings::reflector::{Reflector, reflect_dom_object_with_wrap};
16
17use crate::JSTraceable;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::bindings::str::DOMString;
20use crate::traits::Equivalence;
21
22#[dom_struct]
23pub struct GPUDeviceLostInfo<D: DomTypes> {
24    reflector_: Reflector,
25    message: TracedDOMString,
26    reason: GPUDeviceLostReason,
27    #[no_trace = "PhantomData does not exist"]
28    phantom: PhantomData<D>,
29}
30
31impl<D: Equivalence> GPUDeviceLostInfo<D> {
32    fn new_inherited(message: DOMString, reason: GPUDeviceLostReason) -> Self {
33        Self {
34            reflector_: Reflector::new(),
35            message: message.traced(),
36            reason,
37            phantom: PhantomData,
38        }
39    }
40
41    pub(crate) fn new(
42        cx: &mut JSContext,
43        global: &D::GlobalScope,
44        message: DOMString,
45        reason: GPUDeviceLostReason,
46    ) -> DomRoot<Self> {
47        reflect_dom_object_with_wrap::<D, _, _>(
48            cx,
49            Box::new(GPUDeviceLostInfo::new_inherited(message, reason)),
50            global,
51            GPUDeviceLostInfoWrap::<D>,
52        )
53    }
54}
55
56impl<D: DomTypes> GPUDeviceLostInfoMethods<D> for GPUDeviceLostInfo<D> {
57    /// <https://gpuweb.github.io/gpuweb/#dom-gpudevicelostinfo-message>
58    fn Message(&self) -> DOMString {
59        self.message.clone().root()
60    }
61
62    /// <https://gpuweb.github.io/gpuweb/#dom-gpudevicelostinfo-reason>
63    fn Reason(&self) -> GPUDeviceLostReason {
64        self.reason
65    }
66}