mirror of
https://github.com/servo/servo.git
synced 2025-08-07 22:45:34 +01:00
bindings: Allow Guard to take multiple conditions, check for SecureContext in ConstructorEnabled (#33508)
* Update condition handling for exposing values - Let Guard take a list of conditions - Check for secure context condition when exposing constructor Signed-off-by: Daniel Adams <msub2official@gmail.com> * Update WPT expectations Signed-off-by: Daniel Adams <msub2official@gmail.com> * Python tidy Signed-off-by: Daniel Adams <msub2official@gmail.com> * Make interfaces test run in secure context Signed-off-by: Daniel Adams <msub2official@gmail.com> --------- Signed-off-by: Daniel Adams <msub2official@gmail.com>
This commit is contained in:
parent
24ad2a0526
commit
4e4b137eaa
8 changed files with 45 additions and 32 deletions
|
@ -13,23 +13,37 @@ use crate::dom::globalscope::GlobalScope;
|
|||
use crate::realms::{AlreadyInRealm, InRealm};
|
||||
use crate::script_runtime::JSContext;
|
||||
|
||||
/// A container with a condition.
|
||||
/// A container with a list of conditions.
|
||||
pub struct Guard<T: Clone + Copy> {
|
||||
condition: Condition,
|
||||
conditions: &'static [Condition],
|
||||
value: T,
|
||||
}
|
||||
|
||||
impl<T: Clone + Copy> Guard<T> {
|
||||
/// Construct a new guarded value.
|
||||
pub const fn new(condition: Condition, value: T) -> Self {
|
||||
Guard { condition, value }
|
||||
pub const fn new(conditions: &'static [Condition], value: T) -> Self {
|
||||
Guard { conditions, value }
|
||||
}
|
||||
|
||||
/// Expose the value if the condition is satisfied.
|
||||
/// Expose the value if the conditions are satisfied.
|
||||
///
|
||||
/// The passed handle is the object on which the value may be exposed.
|
||||
pub fn expose(&self, cx: JSContext, obj: HandleObject, global: HandleObject) -> Option<T> {
|
||||
if self.condition.is_satisfied(cx, obj, global) {
|
||||
let mut exposed_on_global = false;
|
||||
let conditions_satisfied = self.conditions.iter().all(|c| match c {
|
||||
Condition::Satisfied => {
|
||||
exposed_on_global = true;
|
||||
true
|
||||
},
|
||||
// If there are multiple Exposed conditions, we just need one of them to be true
|
||||
Condition::Exposed(globals) => {
|
||||
exposed_on_global |= is_exposed_in(global, *globals);
|
||||
true
|
||||
},
|
||||
_ => c.is_satisfied(cx, obj, global),
|
||||
});
|
||||
|
||||
if conditions_satisfied && exposed_on_global {
|
||||
Some(self.value)
|
||||
} else {
|
||||
None
|
||||
|
@ -38,6 +52,7 @@ impl<T: Clone + Copy> Guard<T> {
|
|||
}
|
||||
|
||||
/// A condition to expose things.
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Condition {
|
||||
/// The condition is satisfied if the function returns true.
|
||||
Func(fn(JSContext, HandleObject) -> bool),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue