Skip to content

Commit

Permalink
style: Generate StyleDisplay using cbindgen.
Browse files Browse the repository at this point in the history
We use the same setup WR uses, which is checking-in the files.

But I think it's much better than keeping the two things in sync manually :)

When you add a new value, you need to add it to the rust source, then run the
command, but since it doesn't need to build the style crate (it uses the Rust
AST, doesn't build) there's no problem.

Differential Revision: https://phabricator.services.mozilla.com/D2354
  • Loading branch information
emilio committed Aug 7, 2018
1 parent c63a9a3 commit 798d45f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
26 changes: 26 additions & 0 deletions components/style/cbindgen.toml
@@ -0,0 +1,26 @@
header = """/* 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 http://mozilla.org/MPL/2.0/. */"""
autogen_warning = """/* DO NOT MODIFY THIS MANUALLY! This file was generated using cbindgen.
* To generate this file:
* 1. Get the latest cbindgen using `cargo install --force cbindgen`
* a. Alternatively, you can clone `https://github.com/eqrion/cbindgen` and use a tagged release
* 2. Run `rustup run nightly cbindgen toolkit/library/rust/ --lockfile Cargo.lock --crate style -o layout/style/ServoStyleConsts.h`
*/"""
include_version = true
braces = "SameLine"
line_length = 80
tab_width = 2
language = "C++"
namespaces = ["mozilla"]

[struct]
derive_eq = true

[enum]
derive_helper_methods = true

[export]
prefix = "Style"
include = ["StyleDisplay"]
item_types = ["enums"]
46 changes: 14 additions & 32 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -3031,57 +3031,39 @@ fn static_assert() {
shape-outside contain touch-action translate
scale""" %>
<%self:impl_trait style_struct_name="Box" skip_longhands="${skip_box_longhands}">

// We manually-implement the |display| property until we get general
// infrastructure for preffing certain values.
<% display_keyword = Keyword("display", "inline block inline-block table inline-table table-row-group " +
"table-header-group table-footer-group table-row table-column-group " +
"table-column table-cell table-caption list-item flex none " +
"inline-flex grid inline-grid ruby ruby-base ruby-base-container " +
"ruby-text ruby-text-container contents flow-root -webkit-box " +
"-webkit-inline-box -moz-box -moz-inline-box -moz-grid -moz-inline-grid " +
"-moz-grid-group -moz-grid-line -moz-stack -moz-inline-stack -moz-deck " +
"-moz-popup -moz-groupbox",
gecko_enum_prefix="StyleDisplay",
gecko_strip_moz_prefix=False) %>

fn match_display_keyword(
v: longhands::display::computed_value::T
) -> structs::root::mozilla::StyleDisplay {
use properties::longhands::display::computed_value::T as Keyword;
// FIXME(bholley): Align binary representations and ditch |match| for cast + static_asserts
match v {
% for value in display_keyword.values_for('gecko'):
Keyword::${to_camel_case(value)} =>
structs::${display_keyword.gecko_constant(value)},
% endfor
}
}

#[inline]
pub fn set_display(&mut self, v: longhands::display::computed_value::T) {
let result = Self::match_display_keyword(v);
self.gecko.mDisplay = result;
self.gecko.mOriginalDisplay = result;
// unsafe: cbindgen ensures the representation is the same.
self.gecko.mDisplay = unsafe { transmute(v) };
self.gecko.mOriginalDisplay = unsafe { transmute(v) };
}

#[inline]
pub fn copy_display_from(&mut self, other: &Self) {
self.gecko.mDisplay = other.gecko.mDisplay;
self.gecko.mOriginalDisplay = other.gecko.mDisplay;
}

#[inline]
pub fn reset_display(&mut self, other: &Self) {
self.copy_display_from(other)
}

#[inline]
pub fn set_adjusted_display(
&mut self,
v: longhands::display::computed_value::T,
_is_item_or_root: bool
) {
self.gecko.mDisplay = Self::match_display_keyword(v);
// unsafe: cbindgen ensures the representation is the same.
self.gecko.mDisplay = unsafe { transmute(v) };
}

<%call expr="impl_keyword_clone('display', 'mDisplay', display_keyword)"></%call>
#[inline]
pub fn clone_display(&self) -> longhands::display::computed_value::T {
// unsafe: cbindgen ensures the representation is the same.
unsafe { transmute(self.gecko.mDisplay) }
}

<% float_keyword = Keyword("float", "Left Right None", gecko_enum_prefix="StyleFloat") %>
${impl_keyword('float', 'mFloat', float_keyword)}
Expand Down
35 changes: 23 additions & 12 deletions components/style/values/specified/box.rs
Expand Up @@ -41,29 +41,42 @@ fn moz_box_display_values_enabled(context: &ParserContext) -> bool {
}
}

/// Defines an element’s display type, which consists of
/// the two basic qualities of how an element generates boxes
/// <https://drafts.csswg.org/css-display/#propdef-display>
///
///
/// NOTE(emilio): Order is important in Gecko!
///
/// If you change it, make sure to take a look at the
/// FrameConstructionDataByDisplay stuff (both the XUL and non-XUL version), and
/// ensure it's still correct!
///
/// Also, when you change this from Gecko you may need to regenerate the
/// C++-side bindings (see components/style/cbindgen.toml).
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, Parse, PartialEq,
SpecifiedValueInfo, ToComputedValue, ToCss)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
/// Defines an element’s display type, which consists of
/// the two basic qualities of how an element generates boxes
/// <https://drafts.csswg.org/css-display/#propdef-display>
#[repr(u8)]
pub enum Display {
Inline,
None = 0,
Block,
#[cfg(feature = "gecko")]
FlowRoot,
Inline,
InlineBlock,
ListItem,
Table,
InlineTable,
TableRowGroup,
TableColumn,
TableColumnGroup,
TableHeaderGroup,
TableFooterGroup,
TableRow,
TableColumnGroup,
TableColumn,
TableCell,
TableCaption,
ListItem,
None,
#[parse(aliases = "-webkit-flex")]
Flex,
#[parse(aliases = "-webkit-inline-flex")]
Expand All @@ -85,8 +98,6 @@ pub enum Display {
#[cfg(feature = "gecko")]
Contents,
#[cfg(feature = "gecko")]
FlowRoot,
#[cfg(feature = "gecko")]
WebkitBox,
#[cfg(feature = "gecko")]
WebkitInlineBox,
Expand Down Expand Up @@ -119,10 +130,10 @@ pub enum Display {
MozDeck,
#[cfg(feature = "gecko")]
#[parse(condition = "moz_display_values_enabled")]
MozPopup,
MozGroupbox,
#[cfg(feature = "gecko")]
#[parse(condition = "moz_display_values_enabled")]
MozGroupbox,
MozPopup,
}

impl Display {
Expand Down

0 comments on commit 798d45f

Please sign in to comment.