Auto merge of #27629 - CYBAI:whendefined-constructor, r=jdm

Resolve `whenDefined` with the class constructor

I still don't have time to find the root cause of why nightly sync failed :( so we don't have the tests in our tree now.

But I tried to verify it with `wpt.live` that this is correct

<img width="1136" alt="image" src="https://user-images.githubusercontent.com/6782666/92739547-f7ca0c80-f3b7-11ea-966e-fd7ecd2638cf.png">

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #27626
- [x] There are tests for these changes

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
bors-servo 2020-09-22 20:25:49 -04:00 committed by GitHub
commit 3d057c8eec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 22 deletions

View file

@ -395,7 +395,13 @@ impl CustomElementRegistryMethods for CustomElementRegistry {
// Step 16, 16.3 // Step 16, 16.3
if let Some(promise) = self.when_defined.borrow_mut().remove(&name) { if let Some(promise) = self.when_defined.borrow_mut().remove(&name) {
promise.resolve_native(&UndefinedValue()); unsafe {
rooted!(in(*cx) let mut constructor = UndefinedValue());
definition
.constructor
.to_jsval(*cx, constructor.handle_mut());
promise.resolve_native(&constructor.get());
}
} }
Ok(()) Ok(())
} }
@ -416,6 +422,7 @@ impl CustomElementRegistryMethods for CustomElementRegistry {
} }
/// <https://html.spec.whatwg.org/multipage/#dom-customelementregistry-whendefined> /// <https://html.spec.whatwg.org/multipage/#dom-customelementregistry-whendefined>
#[allow(unsafe_code)]
fn WhenDefined(&self, name: DOMString, comp: InRealm) -> Rc<Promise> { fn WhenDefined(&self, name: DOMString, comp: InRealm) -> Rc<Promise> {
let global_scope = self.window.upcast::<GlobalScope>(); let global_scope = self.window.upcast::<GlobalScope>();
let name = LocalName::from(&*name); let name = LocalName::from(&*name);
@ -428,10 +435,17 @@ impl CustomElementRegistryMethods for CustomElementRegistry {
} }
// Step 2 // Step 2
if self.definitions.borrow().contains_key(&name) { if let Some(definition) = self.definitions.borrow().get(&LocalName::from(&*name)) {
let promise = Promise::new_in_current_realm(&global_scope, comp); unsafe {
promise.resolve_native(&UndefinedValue()); let cx = global_scope.get_cx();
return promise; rooted!(in(*cx) let mut constructor = UndefinedValue());
definition
.constructor
.to_jsval(*cx, constructor.handle_mut());
let promise = Promise::new_in_current_realm(&global_scope, comp);
promise.resolve_native(&constructor.get());
return promise;
}
} }
// Step 3 // Step 3

View file

@ -10,7 +10,7 @@ interface CustomElementRegistry {
any get(DOMString name); any get(DOMString name);
Promise<void> whenDefined(DOMString name); Promise<CustomElementConstructor> whenDefined(DOMString name);
[CEReactions] void upgrade(Node root); [CEReactions] void upgrade(Node root);
}; };

View file

@ -32,19 +32,3 @@
[customElements.define must not throw when defining another custom element in a different global object during Get(constructor, "prototype")] [customElements.define must not throw when defining another custom element in a different global object during Get(constructor, "prototype")]
expected: FAIL expected: FAIL
[A promise returned by customElements.whenDefined must be resolved by "define"]
expected: FAIL
[customElements.whenDefined must return a new resolved promise each time invoked when the registry contains the entry with the given name]
expected: FAIL
[A promise returned by customElements.whenDefined must be resolved with the defined class once such class is defined]
expected: FAIL
[customElements.whenDefined must return a resolved promise when the registry contains the entry with the given name]
expected: FAIL
[A promise returned by customElements.whenDefined must be resolved with the defined class]
expected: FAIL