Skip to content

Commit

Permalink
Do RestyleHint assertions at runtime so they use build-time bindgen.
Browse files Browse the repository at this point in the history
  • Loading branch information
heycam committed Feb 14, 2017
1 parent d44bf61 commit 229eb93
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
36 changes: 34 additions & 2 deletions components/style/restyle_hints.rs
Expand Up @@ -32,8 +32,7 @@ bitflags! {
///
/// Note that the bit values here must be kept in sync with the Gecko
/// nsRestyleHint values. If you add more bits with matching values,
/// please add assertions to assert_restyle_hints_match in
/// tests/unit/stylo/sanity_checks.rs.
/// please add assertions to assert_restyle_hints_match below.
pub flags RestyleHint: u32 {
/// Rerun selector matching on the element.
const RESTYLE_SELF = 0x01,
Expand All @@ -55,6 +54,39 @@ bitflags! {
}
}

/// Asserts that all RestyleHint flags have a matching nsRestyleHint value.
#[cfg(feature = "gecko")]
#[inline]
pub fn assert_restyle_hints_match() {
use gecko_bindings::structs;

macro_rules! check_restyle_hints {
( $( $a:ident => $b:ident ),*, ) => {
if cfg!(debug_assertions) {
let mut hints = RestyleHint::all();
$(
assert_eq!(structs::$a.0 as usize, $b.bits() as usize, stringify!($b));
hints.remove($b);
)*
assert_eq!(hints, RestyleHint::empty(), "all RestyleHint bits should have an assertion");
}
}
}

check_restyle_hints! {
nsRestyleHint_eRestyle_Self => RESTYLE_SELF,
// XXX This for Servo actually means something like an hypothetical
// Restyle_AllDescendants (but without running selector matching on the
// element). ServoRestyleManager interprets it like that, but in practice we
// should align the behavior with Gecko adding a new restyle hint, maybe?
//
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1291786
nsRestyleHint_eRestyle_SomeDescendants => RESTYLE_DESCENDANTS,
nsRestyleHint_eRestyle_LaterSiblings => RESTYLE_LATER_SIBLINGS,
nsRestyleHint_eRestyle_StyleAttribute => RESTYLE_STYLE_ATTRIBUTE,
}
}

impl RestyleHint {
/// The subset hints that affect the styling of a single element during the
/// traversal.
Expand Down
5 changes: 4 additions & 1 deletion ports/geckolib/glue.rs
Expand Up @@ -68,7 +68,7 @@ use style::properties::{ComputedValues, Importance, PropertyDeclaration};
use style::properties::{PropertyDeclarationParseResult, PropertyDeclarationBlock, PropertyId};
use style::properties::animated_properties::{AnimationValue, Interpolate, TransitionProperty};
use style::properties::parse_one_declaration;
use style::restyle_hints::RestyleHint;
use style::restyle_hints::{self, RestyleHint};
use style::selector_parser::PseudoElementCascadeType;
use style::sequential;
use style::string_cache::Atom;
Expand Down Expand Up @@ -98,6 +98,9 @@ pub extern "C" fn Servo_Initialize() -> () {

// Pretend that we're a Servo Layout thread, to make some assertions happy.
thread_state::initialize(thread_state::LAYOUT);

// Perform some debug-only runtime assertions.
restyle_hints::assert_restyle_hints_match();
}

#[no_mangle]
Expand Down
32 changes: 0 additions & 32 deletions tests/unit/stylo/sanity_checks.rs
Expand Up @@ -27,38 +27,6 @@ macro_rules! check_enum_value_non_static {
}
}

#[test]
fn assert_restyle_hints_match() {
use style::restyle_hints::*; // For flags
use style::gecko_bindings::structs;

macro_rules! check_restyle_hints {
( $( $a:ident => $b:ident ),*, ) => {
{
let mut hints = RestyleHint::all();
$(
check_enum_value_non_static!(structs::$a, $b.bits());
hints.remove($b);
)*
assert_eq!(hints, RestyleHint::empty(), "all RestyleHint bits should have an assertion");
}
}
}

check_restyle_hints! {
nsRestyleHint_eRestyle_Self => RESTYLE_SELF,
// XXX This for Servo actually means something like an hypothetical
// Restyle_AllDescendants (but without running selector matching on the
// element). ServoRestyleManager interprets it like that, but in practice we
// should align the behavior with Gecko adding a new restyle hint, maybe?
//
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1291786
nsRestyleHint_eRestyle_SomeDescendants => RESTYLE_DESCENDANTS,
nsRestyleHint_eRestyle_LaterSiblings => RESTYLE_LATER_SIBLINGS,
nsRestyleHint_eRestyle_StyleAttribute => RESTYLE_STYLE_ATTRIBUTE,
}
}

// Note that we can't call each_pseudo_element, parse_pseudo_element, or
// similar, because we'd need the foreign atom symbols to link.
#[test]
Expand Down

0 comments on commit 229eb93

Please sign in to comment.