script/dom/
dynamicmoduleowner.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 std::rc::Rc;
6
7use dom_struct::dom_struct;
8use uuid::Uuid;
9
10use crate::dom::bindings::codegen::Bindings::DynamicModuleOwnerBinding::DynamicModuleOwnerMethods;
11use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::globalscope::GlobalScope;
14use crate::dom::promise::Promise;
15use crate::script_runtime::CanGc;
16
17/// An unique id for dynamic module
18#[derive(Clone, Copy, Debug, Eq, Hash, JSTraceable, PartialEq)]
19pub(crate) struct DynamicModuleId(#[no_trace] pub(crate) Uuid);
20
21#[dom_struct]
22pub(crate) struct DynamicModuleOwner {
23    reflector_: Reflector,
24
25    #[ignore_malloc_size_of = "Rc"]
26    promise: Rc<Promise>,
27
28    /// Unique id for each dynamic module
29    #[ignore_malloc_size_of = "Defined in uuid"]
30    id: DynamicModuleId,
31}
32
33impl DynamicModuleOwner {
34    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
35    fn new_inherited(promise: Rc<Promise>, id: DynamicModuleId) -> Self {
36        DynamicModuleOwner {
37            reflector_: Reflector::new(),
38            promise,
39            id,
40        }
41    }
42
43    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
44    pub(crate) fn new(
45        global: &GlobalScope,
46        promise: Rc<Promise>,
47        id: DynamicModuleId,
48        can_gc: CanGc,
49    ) -> DomRoot<Self> {
50        reflect_dom_object(
51            Box::new(DynamicModuleOwner::new_inherited(promise, id)),
52            global,
53            can_gc,
54        )
55    }
56}
57
58impl DynamicModuleOwnerMethods<crate::DomTypeHolder> for DynamicModuleOwner {
59    // https://html.spec.whatwg.org/multipage/#integration-with-the-javascript-module-system:import()
60    fn Promise(&self) -> Rc<Promise> {
61        self.promise.clone()
62    }
63}