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