Skip to main content

script_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 malloc_size_of_derive::MallocSizeOf;
9use script_bindings::DomTypes;
10use script_bindings::codegen::GenericBindings::WebGPUBinding::{
11    GPUCompilationInfoMethods, GPUCompilationInfoWrap,
12};
13use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_wrap};
14use script_bindings::utils::to_frozen_array;
15use webgpu_traits::ShaderCompilationInfo;
16
17use crate::JSTraceable;
18use crate::dom::bindings::root::{Dom, DomRoot};
19use crate::gpucompilationmessage::GPUCompilationMessage;
20
21#[dom_struct]
22pub struct GPUCompilationInfo<D: DomTypes> {
23    reflector_: Reflector,
24    // currently we only get one message from wgpu
25    msg: Vec<Dom<GPUCompilationMessage<D>>>,
26}
27
28impl<D> GPUCompilationInfo<D>
29where
30    D: DomTypes<
31            GPUCompilationInfo = GPUCompilationInfo<D>,
32            GPUCompilationMessage = GPUCompilationMessage<D>,
33        >,
34{
35    pub(crate) fn new_inherited(msg: Vec<DomRoot<GPUCompilationMessage<D>>>) -> Self {
36        Self {
37            reflector_: Reflector::new(),
38            msg: msg.into_iter().map(|event| event.as_traced()).collect(),
39        }
40    }
41
42    pub(crate) fn new(
43        cx: &mut JSContext,
44        global: &D::GlobalScope,
45        msg: Vec<DomRoot<GPUCompilationMessage<D>>>,
46    ) -> DomRoot<Self> {
47        reflect_dom_object_with_proto_and_wrap::<D, _, _>(
48            Box::new(Self::new_inherited(msg)),
49            global,
50            None,
51            cx,
52            GPUCompilationInfoWrap::<D>,
53        )
54    }
55
56    pub fn from(
57        cx: &mut JSContext,
58        global: &D::GlobalScope,
59        error: Option<ShaderCompilationInfo>,
60    ) -> DomRoot<Self> {
61        let msg = error
62            .map(|error| vec![GPUCompilationMessage::from(cx, global, error)])
63            .unwrap_or_default();
64        Self::new(cx, global, msg)
65    }
66}
67
68impl<D: DomTypes> GPUCompilationInfoMethods<D> for GPUCompilationInfo<D> {
69    /// <https://gpuweb.github.io/gpuweb/#dom-gpucompilationinfo-messages>
70    fn Messages(&self, cx: &mut JSContext, retval: MutableHandleValue) {
71        let messages: Vec<DomRoot<GPUCompilationMessage<D>>> =
72            self.msg.iter().map(|msg| msg.as_rooted()).collect();
73        to_frozen_array(cx, messages.as_slice(), retval)
74    }
75}