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    #[allow(dead_code)]
33    pub(crate) fn new(
34        global: &GlobalScope,
35        msg: Vec<DomRoot<GPUCompilationMessage>>,
36        can_gc: CanGc,
37    ) -> DomRoot<Self> {
38        reflect_dom_object_with_proto(Box::new(Self::new_inherited(msg)), global, None, can_gc)
39    }
40
41    pub(crate) fn from(
42        global: &GlobalScope,
43        error: Option<ShaderCompilationInfo>,
44        can_gc: CanGc,
45    ) -> DomRoot<Self> {
46        Self::new(
47            global,
48            if let Some(error) = error {
49                vec![GPUCompilationMessage::from(global, error, can_gc)]
50            } else {
51                Vec::new()
52            },
53            can_gc,
54        )
55    }
56}
57
58impl GPUCompilationInfoMethods<crate::DomTypeHolder> for GPUCompilationInfo {
59    /// <https://gpuweb.github.io/gpuweb/#dom-gpucompilationinfo-messages>
60    fn Messages(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
61        to_frozen_array(self.msg.as_slice(), cx, retval, can_gc)
62    }
63}