Skip to main content

script_webgpu/
gpuerror.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 js::rust::HandleObject;
10use malloc_size_of_derive::MallocSizeOf;
11use script_bindings::DomTypes;
12use script_bindings::codegen::GenericBindings::WebGPUBinding::{
13    GPUErrorFilter, GPUErrorMethods, GPUErrorWrap,
14};
15use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_wrap};
16use webgpu_traits::{Error, ErrorFilter};
17
18use crate::JSTraceable;
19use crate::dom::bindings::root::DomRoot;
20use crate::dom::bindings::str::DOMString;
21use crate::gpuconvert::WebGPUConvert;
22use crate::gpuinternalerror::GPUInternalError;
23use crate::gpuoutofmemoryerror::GPUOutOfMemoryError;
24use crate::gpuvalidationerror::GPUValidationError;
25use crate::traits::Equivalence;
26
27#[dom_struct]
28pub struct GPUError<D: DomTypes> {
29    reflector_: Reflector,
30    message: DOMString,
31    #[no_trace = "PhantomData does not exist"]
32    phantom: PhantomData<D>,
33}
34
35impl<D> GPUError<D>
36where
37    D: Equivalence,
38{
39    pub(crate) fn new_inherited(message: DOMString) -> Self {
40        Self {
41            reflector_: Reflector::new(),
42            message,
43            phantom: PhantomData,
44        }
45    }
46
47    #[expect(dead_code)]
48    pub(crate) fn new(
49        cx: &mut JSContext,
50        global: &D::GlobalScope,
51        message: DOMString,
52    ) -> DomRoot<Self> {
53        Self::new_with_proto(cx, global, None, message)
54    }
55
56    pub(crate) fn new_with_proto(
57        cx: &mut JSContext,
58        global: &D::GlobalScope,
59        proto: Option<HandleObject>,
60        message: DOMString,
61    ) -> DomRoot<Self> {
62        reflect_dom_object_with_proto_and_wrap::<D, _, _>(
63            Box::new(GPUError::new_inherited(message)),
64            global,
65            proto,
66            cx,
67            GPUErrorWrap::<D>,
68        )
69    }
70
71    pub fn from_error(cx: &mut JSContext, global: &D::GlobalScope, error: Error) -> DomRoot<Self> {
72        match error {
73            Error::Validation(msg) => DomRoot::upcast(GPUValidationError::new_with_proto(
74                cx,
75                global,
76                None,
77                msg.into(),
78            )),
79            Error::OutOfMemory(msg) => DomRoot::upcast(GPUOutOfMemoryError::new_with_proto(
80                cx,
81                global,
82                None,
83                msg.into(),
84            )),
85            Error::Internal(msg) => DomRoot::upcast(GPUInternalError::new_with_proto(
86                cx,
87                global,
88                None,
89                msg.into(),
90            )),
91        }
92    }
93}
94
95impl<D: Equivalence> GPUErrorMethods<D> for GPUError<D> {
96    /// <https://gpuweb.github.io/gpuweb/#dom-gpuerror-message>
97    fn Message(&self) -> DOMString {
98        self.message.clone()
99    }
100}
101
102impl WebGPUConvert<GPUErrorFilter> for ErrorFilter {
103    fn convert(self) -> GPUErrorFilter {
104        match self {
105            ErrorFilter::Validation => GPUErrorFilter::Validation,
106            ErrorFilter::OutOfMemory => GPUErrorFilter::Out_of_memory,
107            ErrorFilter::Internal => GPUErrorFilter::Internal,
108        }
109    }
110}
111
112pub(crate) trait AsWebGpu {
113    fn as_webgpu(&self) -> ErrorFilter;
114}
115
116impl AsWebGpu for GPUErrorFilter {
117    fn as_webgpu(&self) -> ErrorFilter {
118        match self {
119            GPUErrorFilter::Validation => ErrorFilter::Validation,
120            GPUErrorFilter::Out_of_memory => ErrorFilter::OutOfMemory,
121            GPUErrorFilter::Internal => ErrorFilter::Internal,
122        }
123    }
124}