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::rust::HandleObject;
7use webgpu_traits::{Error, ErrorFilter};
8
9use crate::conversions::Convert;
10use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{GPUErrorFilter, GPUErrorMethods};
11use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::bindings::str::DOMString;
14use crate::dom::globalscope::GlobalScope;
15use crate::dom::types::{GPUInternalError, GPUOutOfMemoryError, GPUValidationError};
16use crate::script_runtime::CanGc;
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(global: &GlobalScope, message: DOMString, can_gc: CanGc) -> DomRoot<Self> {
34        Self::new_with_proto(global, None, message, can_gc)
35    }
36
37    pub(crate) fn new_with_proto(
38        global: &GlobalScope,
39        proto: Option<HandleObject>,
40        message: DOMString,
41        can_gc: CanGc,
42    ) -> DomRoot<Self> {
43        reflect_dom_object_with_proto(
44            Box::new(GPUError::new_inherited(message)),
45            global,
46            proto,
47            can_gc,
48        )
49    }
50
51    pub(crate) fn from_error(global: &GlobalScope, error: Error, can_gc: CanGc) -> DomRoot<Self> {
52        match error {
53            Error::Validation(msg) => DomRoot::upcast(GPUValidationError::new_with_proto(
54                global,
55                None,
56                DOMString::from_string(msg),
57                can_gc,
58            )),
59            Error::OutOfMemory(msg) => DomRoot::upcast(GPUOutOfMemoryError::new_with_proto(
60                global,
61                None,
62                DOMString::from_string(msg),
63                can_gc,
64            )),
65            Error::Internal(msg) => DomRoot::upcast(GPUInternalError::new_with_proto(
66                global,
67                None,
68                DOMString::from_string(msg),
69                can_gc,
70            )),
71        }
72    }
73}
74
75impl GPUErrorMethods<crate::DomTypeHolder> for GPUError {
76    /// <https://gpuweb.github.io/gpuweb/#dom-gpuerror-message>
77    fn Message(&self) -> DOMString {
78        self.message.clone()
79    }
80}
81
82impl Convert<GPUErrorFilter> for ErrorFilter {
83    fn convert(self) -> GPUErrorFilter {
84        match self {
85            ErrorFilter::Validation => GPUErrorFilter::Validation,
86            ErrorFilter::OutOfMemory => GPUErrorFilter::Out_of_memory,
87            ErrorFilter::Internal => GPUErrorFilter::Internal,
88        }
89    }
90}
91
92pub(crate) trait AsWebGpu {
93    fn as_webgpu(&self) -> ErrorFilter;
94}
95
96impl AsWebGpu for GPUErrorFilter {
97    fn as_webgpu(&self) -> ErrorFilter {
98        match self {
99            GPUErrorFilter::Validation => ErrorFilter::Validation,
100            GPUErrorFilter::Out_of_memory => ErrorFilter::OutOfMemory,
101            GPUErrorFilter::Internal => ErrorFilter::Internal,
102        }
103    }
104}