Auto merge of #23253 - BartGitHub:refactor-promise-compartment, r=jdm

Refactor promise compartment

<!-- Please describe your changes on the following line: -->
This PR adds a mechanism to verify that certain code is executed inside a ```JSAutoCompartment```, and applies this to the ```Promise::new_in_current_compartment``` constructor.

r? @jdm

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #23167

<!-- Either: -->
- [x] These changes do not require tests because they do not change existing functionality.

<!-- 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. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23253)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-04-29 13:59:04 -04:00 committed by GitHub
commit 852223b08a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 301 additions and 95 deletions

View file

@ -2,6 +2,7 @@
* 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::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::bindings::callback::{CallbackContainer, ExceptionHandling};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::CustomElementRegistryBinding;
@ -414,21 +415,28 @@ impl CustomElementRegistryMethods for CustomElementRegistry {
}
/// <https://html.spec.whatwg.org/multipage/#dom-customelementregistry-whendefined>
#[allow(unsafe_code)]
fn WhenDefined(&self, name: DOMString) -> Rc<Promise> {
let global_scope = self.window.upcast::<GlobalScope>();
let name = LocalName::from(&*name);
// Step 1
if !is_valid_custom_element_name(&name) {
let promise = unsafe { Promise::new_in_current_compartment(global_scope) };
promise.reject_native(&DOMException::new(global_scope, DOMErrorName::SyntaxError));
let in_compartment_proof = AlreadyInCompartment::assert(&global_scope);
let promise = Promise::new_in_current_compartment(
&global_scope,
InCompartment::Already(&in_compartment_proof),
);
promise.reject_native(&DOMException::new(&global_scope, DOMErrorName::SyntaxError));
return promise;
}
// Step 2
if self.definitions.borrow().contains_key(&name) {
let promise = unsafe { Promise::new_in_current_compartment(global_scope) };
let in_compartment_proof = AlreadyInCompartment::assert(&global_scope);
let promise = Promise::new_in_current_compartment(
&global_scope,
InCompartment::Already(&in_compartment_proof),
);
promise.resolve_native(&UndefinedValue());
return promise;
}
@ -438,7 +446,11 @@ impl CustomElementRegistryMethods for CustomElementRegistry {
// Steps 4, 5
let promise = map.get(&name).cloned().unwrap_or_else(|| {
let promise = unsafe { Promise::new_in_current_compartment(global_scope) };
let in_compartment_proof = AlreadyInCompartment::assert(&global_scope);
let promise = Promise::new_in_current_compartment(
&global_scope,
InCompartment::Already(&in_compartment_proof),
);
map.insert(name, promise.clone());
promise
});