Introduce DynamicModuleOwner dom struct

This commit is contained in:
CYBAI 2020-07-04 17:55:30 +09:00
parent f221b00007
commit 0c7f08f743
4 changed files with 72 additions and 0 deletions

View file

@ -47,6 +47,10 @@ DOMInterfaces = {
'inRealms': ['PromiseAttribute', 'PromiseNativeHandler'],
},
'DynamicModuleOwner': {
'inRealms': ['PromiseAttribute'],
},
'URL': {
'weakReferenceable': True,
},

View file

@ -0,0 +1,54 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::dom::bindings::codegen::Bindings::DynamicModuleOwnerBinding::DynamicModuleOwnerMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use dom_struct::dom_struct;
use std::rc::Rc;
use uuid::Uuid;
/// An unique id for dynamic module
#[derive(Clone, Copy, Debug, Eq, Hash, JSTraceable, PartialEq)]
pub struct DynamicModuleId(pub Uuid);
#[dom_struct]
pub struct DynamicModuleOwner {
reflector_: Reflector,
#[ignore_malloc_size_of = "Rc"]
promise: Rc<Promise>,
/// Unique id for each dynamic module
#[ignore_malloc_size_of = "Defined in uuid"]
id: DynamicModuleId,
}
impl DynamicModuleOwner {
#[allow(unrooted_must_root)]
fn new_inherited(promise: Rc<Promise>, id: DynamicModuleId) -> Self {
DynamicModuleOwner {
reflector_: Reflector::new(),
promise,
id,
}
}
#[allow(unrooted_must_root)]
pub fn new(global: &GlobalScope, promise: Rc<Promise>, id: DynamicModuleId) -> DomRoot<Self> {
reflect_dom_object(
Box::new(DynamicModuleOwner::new_inherited(promise, id)),
global,
)
}
}
impl DynamicModuleOwnerMethods for DynamicModuleOwner {
// https://html.spec.whatwg.org/multipage/#integration-with-the-javascript-module-system:import()
fn Promise(&self) -> Rc<Promise> {
self.promise.clone()
}
}

View file

@ -295,6 +295,7 @@ pub mod domrectreadonly;
pub mod domstringlist;
pub mod domstringmap;
pub mod domtokenlist;
pub mod dynamicmoduleowner;
pub mod element;
pub mod errorevent;
pub mod event;

View file

@ -0,0 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/**
* This is defined for [`Dynamic Module`](https://html.spec.whatwg.org/multipage/#fetch-an-import()-module-script-graph)
* so that we can hold a traceable owner for those dynamic modules which don't hold a owner.
*/
[NoInterfaceObject, Exposed=Window]
interface DynamicModuleOwner {
readonly attribute Promise<any> promise;
};