Skip to content

Commit cc2a9e7

Browse files
committed
Introduce DynamicModuleOwner dom struct
1 parent c50a7af commit cc2a9e7

4 files changed

Lines changed: 71 additions & 0 deletions

File tree

components/script/dom/bindings/codegen/Bindings.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ DOMInterfaces = {
4747
'inRealms': ['PromiseAttribute', 'PromiseNativeHandler'],
4848
},
4949

50+
'DynamicModuleOwner': {
51+
'inRealms': ['PromiseAttribute'],
52+
},
53+
5054
'URL': {
5155
'weakReferenceable': True,
5256
},
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
5+
use crate::dom::bindings::codegen::Bindings::DynamicModuleOwnerBinding::DynamicModuleOwnerMethods;
6+
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
7+
use crate::dom::bindings::root::DomRoot;
8+
use crate::dom::globalscope::GlobalScope;
9+
use crate::dom::promise::Promise;
10+
use dom_struct::dom_struct;
11+
use std::rc::Rc;
12+
use uuid::Uuid;
13+
14+
/// An unique id for dynamic module
15+
#[derive(Clone, Copy, Debug, Eq, Hash, JSTraceable, PartialEq)]
16+
pub struct DynamicModuleId(pub Uuid);
17+
18+
#[dom_struct]
19+
pub struct DynamicModuleOwner {
20+
reflector_: Reflector,
21+
22+
#[ignore_malloc_size_of = "Rc"]
23+
promise: Rc<Promise>,
24+
25+
/// Unique id for each dynamic module
26+
#[ignore_malloc_size_of = "Defined in uuid"]
27+
id: DynamicModuleId,
28+
}
29+
30+
impl DynamicModuleOwner {
31+
#[allow(unrooted_must_root)]
32+
fn new_inherited(promise: Rc<Promise>, id: DynamicModuleId) -> Self {
33+
DynamicModuleOwner {
34+
reflector_: Reflector::new(),
35+
promise,
36+
id,
37+
}
38+
}
39+
40+
#[allow(unrooted_must_root)]
41+
pub fn new(global: &GlobalScope, promise: Rc<Promise>, id: DynamicModuleId) -> DomRoot<Self> {
42+
reflect_dom_object(
43+
Box::new(DynamicModuleOwner::new_inherited(promise, id)),
44+
global,
45+
)
46+
}
47+
}
48+
49+
impl DynamicModuleOwnerMethods for DynamicModuleOwner {
50+
fn Promise(&self) -> Rc<Promise> {
51+
self.promise.clone()
52+
}
53+
}

components/script/dom/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ pub mod domrectreadonly;
295295
pub mod domstringlist;
296296
pub mod domstringmap;
297297
pub mod domtokenlist;
298+
pub mod dynamicmoduleowner;
298299
pub mod element;
299300
pub mod errorevent;
300301
pub mod event;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
5+
/**
6+
* This is defined for [`Dynamic Module`](https://html.spec.whatwg.org/multipage/#fetch-an-import()-module-script-graph)
7+
* so that we can hold a traceable owner for those dynamic modules which don't hold a owner.
8+
*/
9+
10+
[Exposed=(Window)]
11+
interface DynamicModuleOwner {
12+
readonly attribute Promise<any> promise;
13+
};

0 commit comments

Comments
 (0)