Skip to main content

script/dom/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 dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_cx};
9use webgpu_traits::{Error, ErrorFilter};
10
11use crate::conversions::Convert;
12use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{GPUErrorFilter, GPUErrorMethods};
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::bindings::str::DOMString;
15use crate::dom::globalscope::GlobalScope;
16use crate::dom::types::{GPUInternalError, GPUOutOfMemoryError, GPUValidationError};
17
18#[dom_struct]
19pub(crate) struct GPUError {
20    reflector_: Reflector,
21    message: DOMString,
22}
23
24impl GPUError {
25    pub(crate) fn new_inherited(message: DOMString) -> Self {
26        Self {
27            reflector_: Reflector::new(),
28            message,
29        }
30    }
31
32    #[expect(dead_code)]
33    pub(crate) fn new(
34        cx: &mut JSContext,
35        global: &GlobalScope,
36        message: DOMString,
37    ) -> DomRoot<Self> {
38        Self::new_with_proto(cx, global, None, message)
39    }
40
41    pub(crate) fn new_with_proto(
42        cx: &mut JSContext,
43        global: &GlobalScope,
44        proto: Option<HandleObject>,
45        message: DOMString,
46    ) -> DomRoot<Self> {
47        reflect_dom_object_with_proto_and_cx(
48            Box::new(GPUError::new_inherited(message)),
49            global,
50            proto,
51            cx,
52        )
53    }
54
55    pub(crate) fn from_error(
56        cx: &mut JSContext,
57        global: &GlobalScope,
58        error: Error,
59    ) -> DomRoot<Self> {
60        match error {
61            Error::Validation(msg) => DomRoot::upcast(GPUValidationError::new_with_proto(
62                cx,
63                global,
64                None,
65                msg.into(),
66            )),
67            Error::OutOfMemory(msg) => DomRoot::upcast(GPUOutOfMemoryError::new_with_proto(
68                cx,
69                global,
70                None,
71                msg.into(),
72            )),
73            Error::Internal(msg) => DomRoot::upcast(GPUInternalError::new_with_proto(
74                cx,
75                global,
76                None,
77                msg.into(),
78            )),
79        }
80    }
81}
82
83impl GPUErrorMethods<crate::DomTypeHolder> for GPUError {
84    /// <https://gpuweb.github.io/gpuweb/#dom-gpuerror-message>
85    fn Message(&self) -> DOMString {
86        self.message.clone()
87    }
88}
89
90impl Convert<GPUErrorFilter> for ErrorFilter {
91    fn convert(self) -> GPUErrorFilter {
92        match self {
93            ErrorFilter::Validation => GPUErrorFilter::Validation,
94            ErrorFilter::OutOfMemory => GPUErrorFilter::Out_of_memory,
95            ErrorFilter::Internal => GPUErrorFilter::Internal,
96        }
97    }
98}
99
100pub(crate) trait AsWebGpu {
101    fn as_webgpu(&self) -> ErrorFilter;
102}
103
104impl AsWebGpu for GPUErrorFilter {
105    fn as_webgpu(&self) -> ErrorFilter {
106        match self {
107            GPUErrorFilter::Validation => ErrorFilter::Validation,
108            GPUErrorFilter::Out_of_memory => ErrorFilter::OutOfMemory,
109            GPUErrorFilter::Internal => ErrorFilter::Internal,
110        }
111    }
112}