style: The counters code should use atoms rather than strings.

Servo already atomizes the counter names, it makes no sense to copy the string
rather than bumping the refcount.

Differential Revision: https://phabricator.services.mozilla.com/D27061
This commit is contained in:
Emilio Cobos Álvarez 2019-04-15 20:11:45 +00:00
parent a9e473c6e8
commit 498a163cdf
2 changed files with 31 additions and 19 deletions

View file

@ -4455,7 +4455,7 @@ clip-path
fn set_counter_function( fn set_counter_function(
data: &mut nsStyleContentData, data: &mut nsStyleContentData,
content_type: StyleContentType, content_type: StyleContentType,
name: &CustomIdent, name: CustomIdent,
sep: &str, sep: &str,
style: CounterStyleOrNone, style: CounterStyleOrNone,
) { ) {
@ -4464,7 +4464,9 @@ clip-path
let counter_func = unsafe { let counter_func = unsafe {
bindings::Gecko_SetCounterFunction(data, content_type).as_mut().unwrap() bindings::Gecko_SetCounterFunction(data, content_type).as_mut().unwrap()
}; };
counter_func.mIdent.assign(name.0.as_slice()); counter_func.mIdent.set_move(unsafe {
RefPtr::from_addrefed(name.0.into_addrefed())
});
if content_type == StyleContentType::Counters { if content_type == StyleContentType::Counters {
counter_func.mSeparator.assign_str(sep); counter_func.mSeparator.assign_str(sep);
} }
@ -4493,14 +4495,14 @@ clip-path
Gecko_ClearAndResizeStyleContents(&mut self.gecko, Gecko_ClearAndResizeStyleContents(&mut self.gecko,
items.len() as u32); items.len() as u32);
} }
for (i, item) in items.into_iter().enumerate() { for (i, item) in items.into_vec().into_iter().enumerate() {
// NB: Gecko compares the mString value if type is not image // NB: Gecko compares the mString value if type is not image
// or URI independently of whatever gets there. In the quote // or URI independently of whatever gets there. In the quote
// cases, they set it to null, so do the same here. // cases, they set it to null, so do the same here.
unsafe { unsafe {
*self.gecko.mContents[i].mContent.mString.as_mut() = ptr::null_mut(); *self.gecko.mContents[i].mContent.mString.as_mut() = ptr::null_mut();
} }
match *item { match item {
ContentItem::String(ref value) => { ContentItem::String(ref value) => {
self.gecko.mContents[i].mType = StyleContentType::String; self.gecko.mContents[i].mType = StyleContentType::String;
unsafe { unsafe {
@ -4536,22 +4538,22 @@ clip-path
=> self.gecko.mContents[i].mType = StyleContentType::NoOpenQuote, => self.gecko.mContents[i].mType = StyleContentType::NoOpenQuote,
ContentItem::NoCloseQuote ContentItem::NoCloseQuote
=> self.gecko.mContents[i].mType = StyleContentType::NoCloseQuote, => self.gecko.mContents[i].mType = StyleContentType::NoCloseQuote,
ContentItem::Counter(ref name, ref style) => { ContentItem::Counter(name, style) => {
set_counter_function( set_counter_function(
&mut self.gecko.mContents[i], &mut self.gecko.mContents[i],
StyleContentType::Counter, StyleContentType::Counter,
&name, name,
"", "",
style.clone(), style,
); );
} }
ContentItem::Counters(ref name, ref sep, ref style) => { ContentItem::Counters(name, sep, style) => {
set_counter_function( set_counter_function(
&mut self.gecko.mContents[i], &mut self.gecko.mContents[i],
StyleContentType::Counters, StyleContentType::Counters,
&name, name,
&sep, &sep,
style.clone(), style,
); );
} }
ContentItem::Url(ref url) => { ContentItem::Url(ref url) => {
@ -4627,7 +4629,9 @@ clip-path
StyleContentType::Counter | StyleContentType::Counters => { StyleContentType::Counter | StyleContentType::Counters => {
let gecko_function = let gecko_function =
unsafe { &**gecko_content.mContent.mCounters.as_ref() }; unsafe { &**gecko_content.mContent.mCounters.as_ref() };
let ident = CustomIdent(Atom::from(&*gecko_function.mIdent)); let ident = CustomIdent(unsafe {
Atom::from_raw(gecko_function.mIdent.mRawPtr)
});
let style = let style =
CounterStyleOrNone::from_gecko_value(&gecko_function.mCounterStyle); CounterStyleOrNone::from_gecko_value(&gecko_function.mCounterStyle);
let style = match style { let style = match style {
@ -4664,8 +4668,10 @@ clip-path
) { ) {
unsafe { unsafe {
bindings::Gecko_ClearAndResizeCounter${counter_property}s(&mut self.gecko, v.len() as u32); bindings::Gecko_ClearAndResizeCounter${counter_property}s(&mut self.gecko, v.len() as u32);
for (i, ref pair) in v.iter().enumerate() { for (i, pair) in v.0.into_vec().into_iter().enumerate() {
self.gecko.m${counter_property}s[i].mCounter.assign(pair.name.0.as_slice()); self.gecko.m${counter_property}s[i].mCounter.set_move(
RefPtr::from_addrefed(pair.name.0.into_addrefed())
);
self.gecko.m${counter_property}s[i].mValue = pair.value; self.gecko.m${counter_property}s[i].mValue = pair.value;
} }
} }
@ -4690,7 +4696,9 @@ clip-path
longhands::counter_${counter_property.lower()}::computed_value::T::new( longhands::counter_${counter_property.lower()}::computed_value::T::new(
self.gecko.m${counter_property}s.iter().map(|ref gecko_counter| { self.gecko.m${counter_property}s.iter().map(|ref gecko_counter| {
CounterPair { CounterPair {
name: CustomIdent(Atom::from(gecko_counter.mCounter.to_string())), name: CustomIdent(unsafe {
Atom::from_raw(gecko_counter.mCounter.mRawPtr)
}),
value: gecko_counter.mValue, value: gecko_counter.mValue,
} }
}).collect() }).collect()

View file

@ -45,7 +45,7 @@ pub struct CounterPair<Integer> {
ToResolvedValue, ToResolvedValue,
ToShmem, ToShmem,
)] )]
pub struct CounterIncrement<I>(Counters<I>); pub struct CounterIncrement<I>(pub Counters<I>);
impl<I> CounterIncrement<I> { impl<I> CounterIncrement<I> {
/// Returns a new value for `counter-increment`. /// Returns a new value for `counter-increment`.
@ -77,7 +77,7 @@ impl<I> Deref for CounterIncrement<I> {
ToResolvedValue, ToResolvedValue,
ToShmem, ToShmem,
)] )]
pub struct CounterSetOrReset<I>(Counters<I>); pub struct CounterSetOrReset<I>(pub Counters<I>);
impl<I> CounterSetOrReset<I> { impl<I> CounterSetOrReset<I> {
/// Returns a new value for `counter-set` / `counter-reset`. /// Returns a new value for `counter-set` / `counter-reset`.
@ -102,6 +102,7 @@ impl<I> Deref for CounterSetOrReset<I> {
#[derive( #[derive(
Clone, Clone,
Debug, Debug,
Default,
MallocSizeOf, MallocSizeOf,
PartialEq, PartialEq,
SpecifiedValueInfo, SpecifiedValueInfo,
@ -112,10 +113,13 @@ impl<I> Deref for CounterSetOrReset<I> {
)] )]
pub struct Counters<I>(#[css(iterable, if_empty = "none")] Box<[CounterPair<I>]>); pub struct Counters<I>(#[css(iterable, if_empty = "none")] Box<[CounterPair<I>]>);
impl<I> Default for Counters<I> { impl<I> Counters<I> {
/// Move out the Box into a vector. This could just return the Box<>, but
/// Vec<> is a bit more convenient because Box<[T]> doesn't implement
/// IntoIter: https://github.com/rust-lang/rust/issues/59878
#[inline] #[inline]
fn default() -> Self { pub fn into_vec(self) -> Vec<CounterPair<I>> {
Counters(vec![].into_boxed_slice()) self.0.into_vec()
} }
} }