Skip to main content

script/dom/webgpu/
gpucompilationinfo.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::MutableHandleValue;
8use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_cx};
9use webgpu_traits::ShaderCompilationInfo;
10
11use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPUCompilationInfoMethods;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::bindings::utils::to_frozen_array;
14use crate::dom::globalscope::GlobalScope;
15use crate::dom::types::GPUCompilationMessage;
16
17#[dom_struct]
18pub(crate) struct GPUCompilationInfo {
19    reflector_: Reflector,
20    // currently we only get one message from wgpu
21    msg: Vec<DomRoot<GPUCompilationMessage>>,
22}
23
24impl GPUCompilationInfo {
25    pub(crate) fn new_inherited(msg: Vec<DomRoot<GPUCompilationMessage>>) -> Self {
26        Self {
27            reflector_: Reflector::new(),
28            msg,
29        }
30    }
31
32    pub(crate) fn new(
33        cx: &mut JSContext,
34        global: &GlobalScope,
35        msg: Vec<DomRoot<GPUCompilationMessage>>,
36    ) -> DomRoot<Self> {
37        reflect_dom_object_with_proto_and_cx(Box::new(Self::new_inherited(msg)), global, None, cx)
38    }
39
40    pub(crate) fn from(
41        cx: &mut JSContext,
42        global: &GlobalScope,
43        error: Option<ShaderCompilationInfo>,
44    ) -> DomRoot<Self> {
45        let msg = error
46            .map(|error| vec![GPUCompilationMessage::from(cx, global, error)])
47            .unwrap_or_default();
48        Self::new(cx, global, msg)
49    }
50}
51
52impl GPUCompilationInfoMethods<crate::DomTypeHolder> for GPUCompilationInfo {
53    /// <https://gpuweb.github.io/gpuweb/#dom-gpucompilationinfo-messages>
54    fn Messages(&self, cx: &mut JSContext, retval: MutableHandleValue) {
55        to_frozen_array(cx, self.msg.as_slice(), retval)
56    }
57}