Introduce safer layer of sugar for nsStyleUnion

This commit is contained in:
Manish Goregaokar 2016-07-20 16:32:21 +05:30
parent cb05c969c8
commit 6e9a68a0db
No known key found for this signature in database
GPG key ID: 3BBF4D3E2EF79F98
4 changed files with 365 additions and 341 deletions

View file

@ -6,291 +6,123 @@
use app_units::Au; use app_units::Au;
use cssparser::RGBA; use cssparser::RGBA;
use gecko_bindings::structs::{nsStyleCoord, nsStyleUnion, nsStyleUnit}; use gecko_bindings::structs::nsStyleCoord;
use gecko_bindings::sugar::ns_style_coord::{CoordData, CoordDataValues};
use std::cmp::max; use std::cmp::max;
use values::computed::Angle; use values::computed::Angle;
use values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto, LengthOrPercentageOrNone}; use values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto, LengthOrPercentageOrNone};
pub trait StyleCoordHelpers { pub trait StyleCoordHelpers {
fn copy_from(&mut self, other: &Self); fn copy_from(&mut self, other: &Self);
fn set<T: GeckoStyleCoordConvertible>(&mut self, val: T); fn set<T: GeckoStyleCoordConvertible>(&mut self, val: T);
fn set_auto(&mut self);
fn is_auto(&self) -> bool;
fn set_normal(&mut self);
fn is_normal(&self) -> bool;
fn set_coord(&mut self, val: Au);
fn is_coord(&self) -> bool;
fn get_coord(&self) -> Au;
fn set_int(&mut self, val: i32);
fn is_int(&self) -> bool;
fn get_int(&self) -> i32;
fn set_enum(&mut self, val: i32);
fn is_enum(&self) -> bool;
fn get_enum(&self) -> i32;
fn set_percent(&mut self, val: f32);
fn is_percent(&self) -> bool;
fn get_percent(&self) -> f32;
fn set_factor(&mut self, val: f32);
fn is_factor(&self) -> bool;
fn get_factor(&self) -> f32;
} }
impl StyleCoordHelpers for nsStyleCoord { impl StyleCoordHelpers for nsStyleCoord {
#[inline] #[inline]
fn copy_from(&mut self, other: &Self) { fn copy_from(&mut self, other: &Self) {
unsafe { self.mValue.reset(&mut self.mUnit) }; self.data().copy_from(&other.data())
self.mUnit = other.mUnit;
self.mValue = other.mValue;
unsafe { self.addref_if_calc(); }
} }
#[inline] #[inline]
fn set<T: GeckoStyleCoordConvertible>(&mut self, val: T) { fn set<T: GeckoStyleCoordConvertible>(&mut self, val: T) {
val.to_gecko_style_coord(&mut self.mUnit, &mut self.mValue); val.to_gecko_style_coord(&mut self.data());
}
#[inline]
fn set_auto(&mut self) {
unsafe { self.mValue.reset(&mut self.mUnit) };
self.mUnit = nsStyleUnit::eStyleUnit_Auto;
unsafe { *self.mValue.mInt.as_mut() = 0; }
}
#[inline]
fn is_auto(&self) -> bool {
self.mUnit == nsStyleUnit::eStyleUnit_Auto
}
#[inline]
fn set_normal(&mut self) {
unsafe { self.mValue.reset(&mut self.mUnit) };
self.mUnit = nsStyleUnit::eStyleUnit_Normal;
unsafe { *self.mValue.mInt.as_mut() = 0; }
}
#[inline]
fn is_normal(&self) -> bool {
self.mUnit == nsStyleUnit::eStyleUnit_Normal
}
#[inline]
fn set_coord(&mut self, val: Au) {
unsafe { self.mValue.reset(&mut self.mUnit) };
self.mUnit = nsStyleUnit::eStyleUnit_Coord;
unsafe { *self.mValue.mInt.as_mut() = val.0; }
}
#[inline]
fn is_coord(&self) -> bool {
self.mUnit == nsStyleUnit::eStyleUnit_Coord
}
#[inline]
fn get_coord(&self) -> Au {
debug_assert!(self.is_coord());
Au(unsafe { *self.mValue.mInt.as_ref() })
}
#[inline]
fn set_int(&mut self, val: i32) {
unsafe { self.mValue.reset(&mut self.mUnit) };
self.mUnit = nsStyleUnit::eStyleUnit_Integer;
unsafe { *self.mValue.mInt.as_mut() = val; }
}
#[inline]
fn is_int(&self) -> bool {
self.mUnit == nsStyleUnit::eStyleUnit_Integer
}
#[inline]
fn get_int(&self) -> i32 {
debug_assert!(self.is_int());
unsafe { *self.mValue.mInt.as_ref() }
}
#[inline]
fn set_enum(&mut self, val: i32) {
unsafe { self.mValue.reset(&mut self.mUnit) };
self.mUnit = nsStyleUnit::eStyleUnit_Enumerated;
unsafe { *self.mValue.mInt.as_mut() = val; }
}
#[inline]
fn is_enum(&self) -> bool {
self.mUnit == nsStyleUnit::eStyleUnit_Enumerated
}
#[inline]
fn get_enum(&self) -> i32 {
debug_assert!(self.is_enum());
unsafe { *self.mValue.mInt.as_ref() }
}
#[inline]
fn set_percent(&mut self, val: f32) {
unsafe { self.mValue.reset(&mut self.mUnit) };
self.mUnit = nsStyleUnit::eStyleUnit_Percent;
unsafe { *self.mValue.mFloat.as_mut() = val; }
}
#[inline]
fn is_percent(&self) -> bool {
self.mUnit == nsStyleUnit::eStyleUnit_Percent
}
#[inline]
fn get_percent(&self) -> f32 {
debug_assert!(self.is_percent());
unsafe { *self.mValue.mFloat.as_ref() }
}
#[inline]
fn set_factor(&mut self, val: f32) {
unsafe { self.mValue.reset(&mut self.mUnit) };
self.mUnit = nsStyleUnit::eStyleUnit_Factor;
unsafe { *self.mValue.mFloat.as_mut() = val; }
}
#[inline]
fn is_factor(&self) -> bool {
self.mUnit == nsStyleUnit::eStyleUnit_Factor
}
#[inline]
fn get_factor(&self) -> f32 {
debug_assert!(self.is_factor());
unsafe { *self.mValue.mFloat.as_ref() }
} }
} }
pub trait GeckoStyleCoordConvertible : Sized { pub trait GeckoStyleCoordConvertible : Sized {
fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion); fn to_gecko_style_coord(&self, coord: &mut CoordData);
fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self>; fn from_gecko_style_coord(coord: &CoordData) -> Option<Self>;
} }
impl GeckoStyleCoordConvertible for LengthOrPercentage { impl GeckoStyleCoordConvertible for LengthOrPercentage {
fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion) { fn to_gecko_style_coord(&self, coord: &mut CoordData) {
unsafe { union.reset(unit) }; let value = match *self {
match *self { LengthOrPercentage::Length(au) => CoordDataValues::Coord(au.0),
LengthOrPercentage::Length(au) => { LengthOrPercentage::Percentage(p) => CoordDataValues::Percent(p),
*unit = nsStyleUnit::eStyleUnit_Coord; LengthOrPercentage::Calc(calc) => CoordDataValues::Calc(calc.into()),
unsafe { *union.mInt.as_mut() = au.0; }
},
LengthOrPercentage::Percentage(p) => {
*unit = nsStyleUnit::eStyleUnit_Percent;
unsafe { *union.mFloat.as_mut() = p; }
},
LengthOrPercentage::Calc(calc) => unsafe { union.set_calc_value(unit, calc.into()) },
}; };
coord.set_enum(value);
} }
fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { fn from_gecko_style_coord(coord: &CoordData) -> Option<Self> {
match *unit { match coord.as_enum() {
nsStyleUnit::eStyleUnit_Coord CoordDataValues::Coord(coord) => Some(LengthOrPercentage::Length(Au(coord))),
=> Some(LengthOrPercentage::Length(Au(unsafe { *union.mInt.as_ref() }))), CoordDataValues::Percent(p) => Some(LengthOrPercentage::Percentage(p)),
nsStyleUnit::eStyleUnit_Percent CoordDataValues::Calc(calc) => Some(LengthOrPercentage::Calc(calc.into())),
=> Some(LengthOrPercentage::Percentage(unsafe { *union.mFloat.as_ref() })),
nsStyleUnit::eStyleUnit_Calc
=> Some(LengthOrPercentage::Calc(unsafe { union.get_calc().into() })),
_ => None, _ => None,
} }
} }
} }
impl GeckoStyleCoordConvertible for LengthOrPercentageOrAuto { impl GeckoStyleCoordConvertible for LengthOrPercentageOrAuto {
fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion) { fn to_gecko_style_coord(&self, coord: &mut CoordData) {
unsafe { union.reset(unit) }; let value = match *self {
match *self { LengthOrPercentageOrAuto::Length(au) => CoordDataValues::Coord(au.0),
LengthOrPercentageOrAuto::Length(au) => { LengthOrPercentageOrAuto::Percentage(p) => CoordDataValues::Percent(p),
*unit = nsStyleUnit::eStyleUnit_Coord; LengthOrPercentageOrAuto::Auto => CoordDataValues::Auto,
unsafe { *union.mInt.as_mut() = au.0; } LengthOrPercentageOrAuto::Calc(calc) => CoordDataValues::Calc(calc.into()),
},
LengthOrPercentageOrAuto::Percentage(p) => {
*unit = nsStyleUnit::eStyleUnit_Percent;
unsafe { *union.mFloat.as_mut() = p; }
},
LengthOrPercentageOrAuto::Auto => {
*unit = nsStyleUnit::eStyleUnit_Auto;
unsafe { *union.mInt.as_mut() = 0; }
},
LengthOrPercentageOrAuto::Calc(calc) => unsafe { union.set_calc_value(unit, calc.into()) },
}; };
coord.set_enum(value);
} }
fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { fn from_gecko_style_coord(coord: &CoordData) -> Option<Self> {
match *unit { match coord.as_enum() {
nsStyleUnit::eStyleUnit_Auto CoordDataValues::Coord(coord) => Some(LengthOrPercentageOrAuto::Length(Au(coord))),
=> Some(LengthOrPercentageOrAuto::Auto), CoordDataValues::Percent(p) => Some(LengthOrPercentageOrAuto::Percentage(p)),
nsStyleUnit::eStyleUnit_Coord CoordDataValues::Auto => Some(LengthOrPercentageOrAuto::Auto),
=> Some(LengthOrPercentageOrAuto::Length(Au(unsafe { *union.mInt.as_ref() }))), CoordDataValues::Calc(calc) => Some(LengthOrPercentageOrAuto::Calc(calc.into())),
nsStyleUnit::eStyleUnit_Percent
=> Some(LengthOrPercentageOrAuto::Percentage(unsafe { *union.mFloat.as_ref() })),
nsStyleUnit::eStyleUnit_Calc
=> Some(LengthOrPercentageOrAuto::Calc(unsafe { union.get_calc().into() })),
_ => None, _ => None,
} }
} }
} }
impl GeckoStyleCoordConvertible for LengthOrPercentageOrNone { impl GeckoStyleCoordConvertible for LengthOrPercentageOrNone {
fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion) { fn to_gecko_style_coord(&self, coord: &mut CoordData) {
unsafe { union.reset(unit) }; let value = match *self {
match *self { LengthOrPercentageOrNone::Length(au) => CoordDataValues::Coord(au.0),
LengthOrPercentageOrNone::Length(au) => { LengthOrPercentageOrNone::Percentage(p) => CoordDataValues::Percent(p),
*unit = nsStyleUnit::eStyleUnit_Coord; LengthOrPercentageOrNone::None => CoordDataValues::None,
unsafe { *union.mInt.as_mut() = au.0; } LengthOrPercentageOrNone::Calc(calc) => CoordDataValues::Calc(calc.into()),
},
LengthOrPercentageOrNone::Percentage(p) => {
*unit = nsStyleUnit::eStyleUnit_Percent;
unsafe { *union.mFloat.as_mut() = p; }
},
LengthOrPercentageOrNone::None => {
*unit = nsStyleUnit::eStyleUnit_None;
unsafe { *union.mInt.as_mut() = 0; }
},
LengthOrPercentageOrNone::Calc(calc) => unsafe { union.set_calc_value(unit, calc.into()) },
}; };
coord.set_enum(value);
} }
fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { fn from_gecko_style_coord(coord: &CoordData) -> Option<Self> {
match *unit { match coord.as_enum() {
nsStyleUnit::eStyleUnit_None CoordDataValues::Coord(coord) => Some(LengthOrPercentageOrNone::Length(Au(coord))),
=> Some(LengthOrPercentageOrNone::None), CoordDataValues::Percent(p) => Some(LengthOrPercentageOrNone::Percentage(p)),
nsStyleUnit::eStyleUnit_Coord CoordDataValues::None => Some(LengthOrPercentageOrNone::None),
=> Some(LengthOrPercentageOrNone::Length(Au(unsafe { *union.mInt.as_ref() }))), CoordDataValues::Calc(calc) => Some(LengthOrPercentageOrNone::Calc(calc.into())),
nsStyleUnit::eStyleUnit_Percent
=> Some(LengthOrPercentageOrNone::Percentage(unsafe { *union.mFloat.as_ref() })),
nsStyleUnit::eStyleUnit_Calc
=> Some(LengthOrPercentageOrNone::Calc(unsafe { union.get_calc().into() })),
_ => None, _ => None,
} }
} }
} }
impl<T: GeckoStyleCoordConvertible> GeckoStyleCoordConvertible for Option<T> { impl<T: GeckoStyleCoordConvertible> GeckoStyleCoordConvertible for Option<T> {
fn to_gecko_style_coord(&self, unit: &mut nsStyleUnit, union: &mut nsStyleUnion) { fn to_gecko_style_coord(&self, coord: &mut CoordData) {
unsafe { union.reset(unit) };
if let Some(ref me) = *self { if let Some(ref me) = *self {
me.to_gecko_style_coord(unit, union); me.to_gecko_style_coord(coord);
} else { } else {
*unit = nsStyleUnit::eStyleUnit_None; coord.set_enum(CoordDataValues::None);
unsafe { *union.mInt.as_mut() = 0; }
} }
} }
fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { fn from_gecko_style_coord(coord: &CoordData) -> Option<Self> {
Some(T::from_gecko_style_coord(unit, union)) Some(T::from_gecko_style_coord(coord))
} }
} }
impl GeckoStyleCoordConvertible for Angle { impl GeckoStyleCoordConvertible for Angle {
fn to_gecko_style_coord(&self, fn to_gecko_style_coord(&self, coord: &mut CoordData) {
unit: &mut nsStyleUnit, coord.set_enum(CoordDataValues::Radian(self.radians()))
union: &mut nsStyleUnion) {
unsafe { union.reset(unit) };
*unit = nsStyleUnit::eStyleUnit_Radian;
unsafe { *union.mFloat.as_mut() = self.radians() };
} }
fn from_gecko_style_coord(unit: &nsStyleUnit, union: &nsStyleUnion) -> Option<Self> { fn from_gecko_style_coord(coord: &CoordData) -> Option<Self> {
if *unit == nsStyleUnit::eStyleUnit_Radian { if let CoordDataValues::Radian(r) = coord.as_enum() {
Some(Angle::from_radians(unsafe { *union.mFloat.as_ref() })) Some(Angle::from_radians(r))
// XXXManishearth should this handle Degree too?
} else { } else {
None None
} }

View file

@ -26,6 +26,7 @@ use gecko_bindings::bindings::{Gecko_FontFamilyList_AppendGeneric, Gecko_FontFam
use gecko_bindings::bindings::{Gecko_FontFamilyList_Clear, Gecko_InitializeImageLayer}; use gecko_bindings::bindings::{Gecko_FontFamilyList_Clear, Gecko_InitializeImageLayer};
use gecko_bindings::bindings; use gecko_bindings::bindings;
use gecko_bindings::structs; use gecko_bindings::structs;
use gecko_bindings::sugar::ns_style_coord::CoordDataValues;
use gecko_glue::ArcHelpers; use gecko_glue::ArcHelpers;
use gecko_values::{StyleCoordHelpers, GeckoStyleCoordConvertible, convert_nscolor_to_rgba}; use gecko_values::{StyleCoordHelpers, GeckoStyleCoordConvertible, convert_nscolor_to_rgba};
use gecko_values::convert_rgba_to_nscolor; use gecko_values::convert_rgba_to_nscolor;
@ -325,67 +326,65 @@ def set_gecko_property(ffi_name, expr):
% endif % endif
</%def> </%def>
<%def name="impl_split_style_coord(ident, unit_ffi_name, union_ffi_name, need_clone=False)"> <%def name="impl_split_style_coord(ident, gecko_ffi_name, index, need_clone=False)">
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) { pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) {
v.to_gecko_style_coord(&mut self.gecko.${unit_ffi_name}, v.to_gecko_style_coord(&mut self.gecko.${gecko_ffi_name}.data_at(${index}));
&mut self.gecko.${union_ffi_name});
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn copy_${ident}_from(&mut self, other: &Self) { pub fn copy_${ident}_from(&mut self, other: &Self) {
unsafe { self.gecko.${union_ffi_name}.reset(&mut self.gecko.${unit_ffi_name}) }; self.gecko.${gecko_ffi_name}.data_at(${index}).copy_from(&other.gecko.${gecko_ffi_name}.data_at(${index}));
self.gecko.${unit_ffi_name} = other.gecko.${unit_ffi_name};
self.gecko.${union_ffi_name} = other.gecko.${union_ffi_name};
unsafe { self.gecko.${union_ffi_name}.addref_if_calc(&self.gecko.${unit_ffi_name}) };
} }
% if need_clone: % if need_clone:
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T {
use properties::longhands::${ident}::computed_value::T; use properties::longhands::${ident}::computed_value::T;
T::from_gecko_style_coord(&self.gecko.${unit_ffi_name}, T::from_gecko_style_coord(&self.gecko.${gecko_ffi_name}.data_at(${index}))
&self.gecko.${union_ffi_name})
.expect("clone for ${ident} failed") .expect("clone for ${ident} failed")
} }
% endif % endif
</%def> </%def>
<%def name="impl_style_coord(ident, gecko_ffi_name, need_clone=False)"> <%def name="impl_style_coord(ident, gecko_ffi_name, need_clone=False)">
${impl_split_style_coord(ident,
"%s.mUnit" % gecko_ffi_name,
"%s.mValue" % gecko_ffi_name,
need_clone=need_clone)}
</%def>
<%def name="impl_corner_style_coord(ident, x_unit_ffi_name, x_union_ffi_name, \
y_unit_ffi_name, y_union_ffi_name, need_clone=False)">
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) { pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) {
v.0.width.to_gecko_style_coord(&mut self.gecko.${x_unit_ffi_name}, v.to_gecko_style_coord(&mut self.gecko.${gecko_ffi_name}.data());
&mut self.gecko.${x_union_ffi_name});
v.0.height.to_gecko_style_coord(&mut self.gecko.${y_unit_ffi_name},
&mut self.gecko.${y_union_ffi_name});
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn copy_${ident}_from(&mut self, other: &Self) { pub fn copy_${ident}_from(&mut self, other: &Self) {
unsafe { self.gecko.${x_union_ffi_name}.reset(&mut self.gecko.${x_unit_ffi_name}) }; self.gecko.${gecko_ffi_name}.data().copy_from(&other.gecko.${gecko_ffi_name}.data());
unsafe { self.gecko.${y_union_ffi_name}.reset(&mut self.gecko.${y_unit_ffi_name}) }; }
self.gecko.${x_unit_ffi_name} = other.gecko.${x_unit_ffi_name}; % if need_clone:
self.gecko.${x_union_ffi_name} = other.gecko.${x_union_ffi_name}; #[allow(non_snake_case)]
self.gecko.${y_unit_ffi_name} = other.gecko.${y_unit_ffi_name}; pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T {
self.gecko.${y_union_ffi_name} = other.gecko.${y_union_ffi_name}; use properties::longhands::${ident}::computed_value::T;
unsafe { self.gecko.${x_union_ffi_name}.addref_if_calc(&self.gecko.${x_unit_ffi_name}) }; T::from_gecko_style_coord(&self.gecko.${gecko_ffi_name}.data())
unsafe { self.gecko.${y_union_ffi_name}.addref_if_calc(&self.gecko.${y_unit_ffi_name}) }; .expect("clone for ${ident} failed")
}
% endif
</%def>
<%def name="impl_corner_style_coord(ident, gecko_ffi_name, x_index, y_index, need_clone=False)">
#[allow(non_snake_case)]
pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) {
v.0.width.to_gecko_style_coord(&mut self.gecko.${gecko_ffi_name}.data_at(${x_index}));
v.0.width.to_gecko_style_coord(&mut self.gecko.${gecko_ffi_name}.data_at(${y_index}));
}
#[allow(non_snake_case)]
pub fn copy_${ident}_from(&mut self, other: &Self) {
self.gecko.${gecko_ffi_name}.data_at(${x_index}).copy_from(&other.gecko.${gecko_ffi_name}.data_at(${x_index}));
self.gecko.${gecko_ffi_name}.data_at(${x_index}).copy_from(&other.gecko.${gecko_ffi_name}.data_at(${y_index}));
} }
% if need_clone: % if need_clone:
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T { pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T {
use properties::longhands::${ident}::computed_value::T; use properties::longhands::${ident}::computed_value::T;
use euclid::Size2D; use euclid::Size2D;
let width = GeckoStyleCoordConvertible::from_gecko_style_coord(&self.gecko.${x_unit_ffi_name}, let width = GeckoStyleCoordConvertible::from_gecko_style_coord(
&self.gecko.${x_union_ffi_name}) &self.gecko.${gecko_ffi_name}.data_at(${x_index}))
.expect("Failed to clone ${ident}"); .expect("Failed to clone ${ident}");
let height = GeckoStyleCoordConvertible::from_gecko_style_coord(&self.gecko.${y_unit_ffi_name}, let height = GeckoStyleCoordConvertible::from_gecko_style_coord(
&self.gecko.${y_union_ffi_name}) &self.gecko.${gecko_ffi_name}.data_at(${y_index}))
.expect("Failed to clone ${ident}"); .expect("Failed to clone ${ident}");
T(Size2D::new(width, height)) T(Size2D::new(width, height))
} }
@ -602,10 +601,9 @@ fn static_assert() {
% for corner in CORNERS: % for corner in CORNERS:
<% impl_corner_style_coord("border_%s_radius" % corner.ident, <% impl_corner_style_coord("border_%s_radius" % corner.ident,
"mBorderRadius.mUnits[%s]" % corner.x_index, "mBorderRadius",
"mBorderRadius.mValues[%s]" % corner.x_index, corner.x_index,
"mBorderRadius.mUnits[%s]" % corner.y_index, corner.y_index,
"mBorderRadius.mValues[%s]" % corner.y_index,
need_clone=True) %> need_clone=True) %>
% endfor % endfor
</%self:impl_trait> </%self:impl_trait>
@ -616,8 +614,8 @@ fn static_assert() {
% for side in SIDES: % for side in SIDES:
<% impl_split_style_coord("margin_%s" % side.ident, <% impl_split_style_coord("margin_%s" % side.ident,
"mMargin.mUnits[%s]" % side.index, "mMargin",
"mMargin.mValues[%s]" % side.index, side.index,
need_clone=True) %> need_clone=True) %>
% endfor % endfor
</%self:impl_trait> </%self:impl_trait>
@ -628,8 +626,8 @@ fn static_assert() {
% for side in SIDES: % for side in SIDES:
<% impl_split_style_coord("padding_%s" % side.ident, <% impl_split_style_coord("padding_%s" % side.ident,
"mPadding.mUnits[%s]" % side.index, "mPadding",
"mPadding.mValues[%s]" % side.index, side.index,
need_clone=True) %> need_clone=True) %>
% endfor % endfor
</%self:impl_trait> </%self:impl_trait>
@ -640,16 +638,16 @@ fn static_assert() {
% for side in SIDES: % for side in SIDES:
<% impl_split_style_coord("%s" % side.ident, <% impl_split_style_coord("%s" % side.ident,
"mOffset.mUnits[%s]" % side.index, "mOffset",
"mOffset.mValues[%s]" % side.index, side.index,
need_clone=True) %> need_clone=True) %>
% endfor % endfor
pub fn set_z_index(&mut self, v: longhands::z_index::computed_value::T) { pub fn set_z_index(&mut self, v: longhands::z_index::computed_value::T) {
use properties::longhands::z_index::computed_value::T; use properties::longhands::z_index::computed_value::T;
match v { match v {
T::Auto => self.gecko.mZIndex.set_auto(), T::Auto => self.gecko.mZIndex.data().set_enum(CoordDataValues::Auto),
T::Number(n) => self.gecko.mZIndex.set_int(n), T::Number(n) => self.gecko.mZIndex.data().set_enum(CoordDataValues::Integer(n)),
} }
} }
@ -664,13 +662,14 @@ fn static_assert() {
pub fn clone_z_index(&self) -> longhands::z_index::computed_value::T { pub fn clone_z_index(&self) -> longhands::z_index::computed_value::T {
use properties::longhands::z_index::computed_value::T; use properties::longhands::z_index::computed_value::T;
return match self.gecko.mZIndex.data().as_enum() {
if self.gecko.mZIndex.is_auto() { CoordDataValues::Auto => T::Auto,
return T::Auto; CoordDataValues::Integer(n) => T::Number(n),
_ => {
debug_assert!(false);
T::Number(0)
}
} }
debug_assert!(self.gecko.mZIndex.is_int());
T::Number(self.gecko.mZIndex.get_int())
} }
pub fn set_box_sizing(&mut self, v: longhands::box_sizing::computed_value::T) { pub fn set_box_sizing(&mut self, v: longhands::box_sizing::computed_value::T) {
@ -702,10 +701,9 @@ fn static_assert() {
% for corner in CORNERS: % for corner in CORNERS:
<% impl_corner_style_coord("_moz_outline_radius_%s" % corner.ident.replace("_", ""), <% impl_corner_style_coord("_moz_outline_radius_%s" % corner.ident.replace("_", ""),
"mOutlineRadius.mUnits[%s]" % corner.x_index, "mOutlineRadius",
"mOutlineRadius.mValues[%s]" % corner.x_index, corner.x_index,
"mOutlineRadius.mUnits[%s]" % corner.y_index, corner.y_index) %>
"mOutlineRadius.mValues[%s]" % corner.y_index) %>
% endfor % endfor
pub fn outline_has_nonzero_width(&self) -> bool { pub fn outline_has_nonzero_width(&self) -> bool {
@ -826,7 +824,8 @@ fn static_assert() {
match v { match v {
% for value in keyword.values_for('gecko'): % for value in keyword.values_for('gecko'):
T::${to_rust_ident(value)} => T::${to_rust_ident(value)} =>
self.gecko.mVerticalAlign.set_enum(structs::${keyword.gecko_constant(value)} as i32), self.gecko.mVerticalAlign.data().set_enum(
CoordDataValues::Enumerated(structs::${keyword.gecko_constant(value)})),
% endfor % endfor
T::LengthOrPercentage(v) => self.gecko.mVerticalAlign.set(v), T::LengthOrPercentage(v) => self.gecko.mVerticalAlign.set(v),
} }
@ -836,19 +835,17 @@ fn static_assert() {
use properties::longhands::vertical_align::computed_value::T; use properties::longhands::vertical_align::computed_value::T;
use values::computed::LengthOrPercentage; use values::computed::LengthOrPercentage;
if self.gecko.mVerticalAlign.is_enum() { let data = self.gecko.mVerticalAlign.data();
match self.gecko.mVerticalAlign.get_enum() as u32 { match data.as_enum() {
% for value in keyword.values_for('gecko'): % for value in keyword.values_for('gecko'):
structs::${keyword.gecko_constant(value)} CoordDataValues::Enumerated(structs::${keyword.gecko_constant(value)}) => T::${to_rust_ident(value)},
=> T::${to_rust_ident(value)}, % endfor
% endfor CoordDataValues::Enumerated(_) => panic!("Unexpected enum variant for vertical-align"),
_ => panic!("Unexpected enum variant for vertical-align"), _ => {
} let v = LengthOrPercentage::from_gecko_style_coord(&data)
} else { .expect("Expected length or percentage for vertical-align");
let v = LengthOrPercentage::from_gecko_style_coord(&self.gecko.mVerticalAlign.mUnit, T::LengthOrPercentage(v)
&self.gecko.mVerticalAlign.mValue) }
.expect("Expected length or percentage for vertical-align");
T::LengthOrPercentage(v)
} }
} }
@ -1132,29 +1129,29 @@ fn static_assert() {
pub fn set_line_height(&mut self, v: longhands::line_height::computed_value::T) { pub fn set_line_height(&mut self, v: longhands::line_height::computed_value::T) {
use properties::longhands::line_height::computed_value::T; use properties::longhands::line_height::computed_value::T;
// FIXME: Align binary representations and ditch |match| for cast + static_asserts // FIXME: Align binary representations and ditch |match| for cast + static_asserts
match v { let en = match v {
T::Normal => self.gecko.mLineHeight.set_normal(), T::Normal => CoordDataValues::Normal,
T::Length(val) => self.gecko.mLineHeight.set_coord(val), T::Length(val) => CoordDataValues::Coord(val.0),
T::Number(val) => self.gecko.mLineHeight.set_factor(val), T::Number(val) => CoordDataValues::Factor(val),
T::MozBlockHeight => T::MozBlockHeight =>
self.gecko.mLineHeight.set_enum(structs::NS_STYLE_LINE_HEIGHT_BLOCK_HEIGHT as i32), CoordDataValues::Enumerated(structs::NS_STYLE_LINE_HEIGHT_BLOCK_HEIGHT),
} };
self.gecko.mLineHeight.data().set_enum(en);
} }
pub fn clone_line_height(&self) -> longhands::line_height::computed_value::T { pub fn clone_line_height(&self) -> longhands::line_height::computed_value::T {
use properties::longhands::line_height::computed_value::T; use properties::longhands::line_height::computed_value::T;
if self.gecko.mLineHeight.is_normal() { return match self.gecko.mLineHeight.data().as_enum() {
return T::Normal; CoordDataValues::Normal => T::Normal,
CoordDataValues::Coord(coord) => T::Length(Au(coord)),
CoordDataValues::Factor(n) => T::Number(n),
CoordDataValues::Enumerated(val) if val == structs::NS_STYLE_LINE_HEIGHT_BLOCK_HEIGHT =>
T::MozBlockHeight,
_ => {
debug_assert!(false);
T::MozBlockHeight
}
} }
if self.gecko.mLineHeight.is_coord() {
return T::Length(self.gecko.mLineHeight.get_coord());
}
if self.gecko.mLineHeight.is_factor() {
return T::Number(self.gecko.mLineHeight.get_factor());
}
debug_assert!(self.gecko.mLineHeight.get_enum() == structs::NS_STYLE_LINE_HEIGHT_BLOCK_HEIGHT as i32);
T::MozBlockHeight
} }
<%call expr="impl_coord_copy('line_height', 'mLineHeight')"></%call> <%call expr="impl_coord_copy('line_height', 'mLineHeight')"></%call>
@ -1283,8 +1280,8 @@ fn static_assert() {
pub fn set_column_width(&mut self, v: longhands::column_width::computed_value::T) { pub fn set_column_width(&mut self, v: longhands::column_width::computed_value::T) {
match v.0 { match v.0 {
Some(au) => self.gecko.mColumnWidth.set_coord(au), Some(au) => self.gecko.mColumnWidth.data().set_enum(CoordDataValues::Coord(au.0)),
None => self.gecko.mColumnWidth.set_auto(), None => self.gecko.mColumnWidth.data().set_enum(CoordDataValues::Auto),
} }
} }

View file

@ -3,5 +3,5 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
mod ns_style_auto_array; mod ns_style_auto_array;
mod ns_style_coord; pub mod ns_style_coord;
mod ns_t_array; mod ns_t_array;

View file

@ -4,51 +4,246 @@
use bindings::{Gecko_ResetStyleCoord, Gecko_SetStyleCoordCalcValue, Gecko_AddRefCalcArbitraryThread}; use bindings::{Gecko_ResetStyleCoord, Gecko_SetStyleCoordCalcValue, Gecko_AddRefCalcArbitraryThread};
use std::mem::transmute; use std::mem::transmute;
use structs::{nsStyleCoord_CalcValue, nsStyleCoord_Calc, nsStyleUnit, nsStyleUnion, nsStyleCoord}; use std::marker::PhantomData;
use structs::{nsStyleCoord_Calc, nsStyleUnit, nsStyleUnion, nsStyleCoord, nsStyleSides, nsStyleCorners};
use structs::{nsStyleCoord_CalcValue, nscoord};
// Functions here are unsafe because it is possible to use the wrong nsStyleUnit impl nsStyleCoord {
// FIXME we should be pairing up nsStyleUnion and nsStyleUnit somehow #[inline]
// nsStyleCoord is one way to do it, but there are other structs using pairs pub unsafe fn addref_if_calc(&mut self) {
// of union and unit too self.data().addref_if_calc();
}
impl nsStyleUnion { #[inline]
/// Clean up any resources used by an nsStyleUnit pub fn data(&self) -> CoordData {
CoordData {
union: &self.mValue as *const _ as *mut _,
unit: &self.mUnit as *const _ as *mut _,
_marker: PhantomData,
}
}
}
impl nsStyleSides {
#[inline]
pub fn data_at(&self, index: usize) -> CoordData {
CoordData {
union: &self.mValues[index] as *const _ as *mut _,
unit: &self.mUnits[index] as *const _ as *mut _,
_marker: PhantomData,
}
}
}
impl nsStyleCorners {
#[inline]
pub fn data_at(&self, index: usize) -> CoordData {
CoordData {
union: &self.mValues[index] as *const _ as *mut _,
unit: &self.mUnits[index] as *const _ as *mut _,
_marker: PhantomData,
}
}
}
#[derive(Copy, Clone)]
/// Enum representing the tagged union that is CoordData
/// In release mode this should never actually exist in the code,
/// and will be optimized out by threading matches and inlining
pub enum CoordDataValues {
Null,
Normal,
Auto,
None,
Percent(f32),
Factor(f32),
Degree(f32),
Grad(f32),
Radian(f32),
Turn(f32),
FlexFraction(f32),
Coord(nscoord),
Integer(i32),
Enumerated(u32),
Calc(nsStyleCoord_CalcValue),
}
/// XXXManishearth should this be using Cell/UnsafeCell?
pub struct CoordData<'a> {
union: *mut nsStyleUnion,
unit: *mut nsStyleUnit,
_marker: PhantomData<&'a mut ()>,
}
impl<'a> CoordData<'a> {
/// Clean up any resources used by the union
/// Currently, this only happens if the nsStyleUnit /// Currently, this only happens if the nsStyleUnit
/// is a Calc /// is a Calc
pub unsafe fn reset(&mut self, unit: &mut nsStyleUnit) { #[inline]
if *unit == nsStyleUnit::eStyleUnit_Calc { pub fn reset(&mut self) {
Gecko_ResetStyleCoord(unit, self); unsafe {
if *self.unit == nsStyleUnit::eStyleUnit_Calc {
Gecko_ResetStyleCoord(self.unit, self.union);
}
} }
} }
/// Set internal value to a calc() value #[inline]
/// reset() the union before calling this pub fn copy_from(&mut self, other: &CoordData) {
pub unsafe fn set_calc_value(&mut self, unit: &mut nsStyleUnit, v: nsStyleCoord_CalcValue) { self.reset();
// Calc should have been cleaned up self.unit = other.unit;
debug_assert!(*unit != nsStyleUnit::eStyleUnit_Calc); self.union = other.union;
Gecko_SetStyleCoordCalcValue(unit, self, v); self.addref_if_calc();
} }
#[inline(always)]
pub fn as_enum(&self) -> CoordDataValues {
use self::CoordDataValues::*;
use structs::nsStyleUnit::*;
unsafe {
match *self.unit {
eStyleUnit_Null => Null,
eStyleUnit_Normal => Normal,
eStyleUnit_Auto => Auto,
eStyleUnit_None => None,
eStyleUnit_Percent => Percent(self.get_float()),
eStyleUnit_Factor => Factor(self.get_float()),
eStyleUnit_Degree => Degree(self.get_float()),
eStyleUnit_Grad => Grad(self.get_float()),
eStyleUnit_Radian => Radian(self.get_float()),
eStyleUnit_Turn => Turn(self.get_float()),
eStyleUnit_FlexFraction => FlexFraction(self.get_float()),
eStyleUnit_Coord => Coord(self.get_integer()),
eStyleUnit_Integer => Integer(self.get_integer()),
eStyleUnit_Enumerated => Enumerated(self.get_integer() as u32),
eStyleUnit_Calc => Calc(self.get_calc()),
}
}
}
#[inline(always)]
pub fn set_enum(&mut self, value: CoordDataValues) {
use self::CoordDataValues::*;
use structs::nsStyleUnit::*;
self.reset();
unsafe {
match value {
Null => {
*self.unit = eStyleUnit_Null;
*(*self.union).mInt.as_mut() = 0;
}
Normal => {
*self.unit = eStyleUnit_Normal;
*(*self.union).mInt.as_mut() = 0;
}
Auto => {
*self.unit = eStyleUnit_Auto;
*(*self.union).mInt.as_mut() = 0;
}
None => {
*self.unit = eStyleUnit_None;
*(*self.union).mInt.as_mut() = 0;
}
Percent(f) => {
*self.unit = eStyleUnit_Percent;
*(*self.union).mFloat.as_mut() = f;
}
Factor(f) => {
*self.unit = eStyleUnit_Factor;
*(*self.union).mFloat.as_mut() = f;
}
Degree(f) => {
*self.unit = eStyleUnit_Degree;
*(*self.union).mFloat.as_mut() = f;
}
Grad(f) => {
*self.unit = eStyleUnit_Grad;
*(*self.union).mFloat.as_mut() = f;
}
Radian(f) => {
*self.unit = eStyleUnit_Radian;
*(*self.union).mFloat.as_mut() = f;
}
Turn(f) => {
*self.unit = eStyleUnit_Turn;
*(*self.union).mFloat.as_mut() = f;
}
FlexFraction(f) => {
*self.unit = eStyleUnit_FlexFraction;
*(*self.union).mFloat.as_mut() = f;
}
Coord(coord) => {
*self.unit = eStyleUnit_Coord;
*(*self.union).mInt.as_mut() = coord;
}
Integer(i) => {
*self.unit = eStyleUnit_Integer;
*(*self.union).mInt.as_mut() = i;
}
Enumerated(i) => {
*self.unit = eStyleUnit_Enumerated;
*(*self.union).mInt.as_mut() = i as i32;
}
Calc(calc) => {
*self.unit = eStyleUnit_Calc;
self.set_calc_value(calc);
}
}
}
}
#[inline]
/// Pretend inner value is a float; obtain it
/// While this should usually be called with the unit checked,
/// it is not an intrinsically unsafe operation to call this function
/// with the wrong unit
pub fn get_float(&self) -> f32 {
unsafe { *(*self.union).mFloat.as_ref() }
}
#[inline]
/// Pretend inner value is an int; obtain it
/// While this should usually be called with the unit checked,
/// it is not an intrinsically unsafe operation to call this function
/// with the wrong unit
pub fn get_integer(&self) -> i32 {
unsafe { *(*self.union).mInt.as_ref() }
}
#[inline]
/// Pretend inner value is a calc; obtain it
/// Ensure that the unit is Calc before calling this
pub unsafe fn get_calc(&self) -> nsStyleCoord_CalcValue { pub unsafe fn get_calc(&self) -> nsStyleCoord_CalcValue {
(*self.as_calc())._base (*self.as_calc())._base
} }
pub unsafe fn addref_if_calc(&mut self, unit: &nsStyleUnit) { /// Set internal value to a calc() value
if *unit == nsStyleUnit::eStyleUnit_Calc { /// reset() the union before calling this
Gecko_AddRefCalcArbitraryThread(self.as_calc_mut()); #[inline]
pub fn set_calc_value(&mut self, v: nsStyleCoord_CalcValue) {
unsafe {
// Calc should have been cleaned up
debug_assert!(*self.unit != nsStyleUnit::eStyleUnit_Calc);
Gecko_SetStyleCoordCalcValue(self.unit, self.union, v);
} }
} }
unsafe fn as_calc_mut(&mut self) -> &mut nsStyleCoord_Calc { #[inline]
transmute(*self.mPointer.as_mut() as *mut nsStyleCoord_Calc) pub fn addref_if_calc(&mut self) {
unsafe {
if *self.unit == nsStyleUnit::eStyleUnit_Calc {
Gecko_AddRefCalcArbitraryThread(self.as_calc_mut());
}
}
} }
unsafe fn as_calc(&self) -> &nsStyleCoord_Calc {
transmute(*self.mPointer.as_ref() as *const nsStyleCoord_Calc)
}
}
impl nsStyleCoord { #[inline]
pub unsafe fn addref_if_calc(&mut self) { unsafe fn as_calc_mut(&mut self) -> &mut nsStyleCoord_Calc {
self.mValue.addref_if_calc(&self.mUnit); transmute(*(*self.union).mPointer.as_mut() as *mut nsStyleCoord_Calc)
}
#[inline]
unsafe fn as_calc(&self) -> &nsStyleCoord_Calc {
transmute(*(*self.union).mPointer.as_ref() as *const nsStyleCoord_Calc)
} }
} }