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::rust::MutableHandleValue;
7use webgpu_traits::ShaderCompilationInfo;
8
9use crate::dom::bindings::codegen::Bindings::WebGPUBinding::GPUCompilationInfoMethods;
10use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
11use crate::dom::bindings::root::DomRoot;
12use crate::dom::bindings::utils::to_frozen_array;
13use crate::dom::globalscope::GlobalScope;
14use crate::dom::types::GPUCompilationMessage;
15use crate::script_runtime::{CanGc, JSContext};
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        global: &GlobalScope,
34        msg: Vec<DomRoot<GPUCompilationMessage>>,
35        can_gc: CanGc,
36    ) -> DomRoot<Self> {
37        reflect_dom_object_with_proto(Box::new(Self::new_inherited(msg)), global, None, can_gc)
38    }
39
40    pub(crate) fn from(
41        global: &GlobalScope,
42        error: Option<ShaderCompilationInfo>,
43        can_gc: CanGc,
44    ) -> DomRoot<Self> {
45        Self::new(
46            global,
47            if let Some(error) = error {
48                vec![GPUCompilationMessage::from(global, error, can_gc)]
49            } else {
50                Vec::new()
51            },
52            can_gc,
53        )
54    }
55}
56
57impl GPUCompilationInfoMethods<crate::DomTypeHolder> for GPUCompilationInfo {
58    /// <https://gpuweb.github.io/gpuweb/#dom-gpucompilationinfo-messages>
59    fn Messages(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
60        to_frozen_array(self.msg.as_slice(), cx, retval, can_gc)
61    }
62}