From 3c3e4399da4fcddcb75255a1178ed71be7a5f46b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 18 Jul 2017 23:17:34 -0700 Subject: [PATCH] stylo: Remove usage of ServoComputedValues from binding functions --- components/servo_arc/lib.rs | 6 ++ components/style/gecko/arc_types.rs | 35 ++++++++++-- components/style/gecko/generated/bindings.rs | 14 ++--- .../style/gecko/generated/structs_debug.rs | 2 +- .../style/gecko/generated/structs_release.rs | 2 +- components/style/properties/gecko.mako.rs | 3 +- ports/geckolib/glue.rs | 55 +++++++++---------- 7 files changed, 71 insertions(+), 46 deletions(-) diff --git a/components/servo_arc/lib.rs b/components/servo_arc/lib.rs index b1f4f277a49f..d8a747582470 100644 --- a/components/servo_arc/lib.rs +++ b/components/servo_arc/lib.rs @@ -889,6 +889,12 @@ impl<'a, T> ArcBorrow<'a, T> { arc } + /// For constructing from a reference known to be Arc-backed, + /// e.g. if we obtain such a reference over FFI + pub unsafe fn from_ref(r: &'a T) -> Self { + ArcBorrow(r) + } + pub fn with_arc(&self, f: F) -> U where F: FnOnce(&Arc) -> U, T: 'static { // Synthesize transient Arc, which never touches the refcount. let transient = unsafe { NoDrop::new(Arc::from_raw(self.0)) }; diff --git a/components/style/gecko/arc_types.rs b/components/style/gecko/arc_types.rs index 704369e225c8..f25d5ef6f5f1 100644 --- a/components/style/gecko/arc_types.rs +++ b/components/style/gecko/arc_types.rs @@ -13,13 +13,14 @@ use gecko_bindings::bindings::{RawServoKeyframe, RawServoKeyframesRule}; use gecko_bindings::bindings::{RawServoMediaRule, RawServoNamespaceRule, RawServoPageRule}; use gecko_bindings::bindings::{RawServoRuleNode, RawServoRuleNodeStrong, RawServoDocumentRule}; use gecko_bindings::bindings::ServoCssRules; -use gecko_bindings::structs::{RawServoAnimationValue, RawServoDeclarationBlock, RawServoStyleRule, RawServoMediaList}; -use gecko_bindings::structs::{RawServoStyleSheetContents, ServoStyleContext}; -use gecko_bindings::sugar::ownership::{HasArcFFI, HasFFI}; +use gecko_bindings::structs::{RawServoAnimationValue, RawServoDeclarationBlock, RawServoStyleRule}; +use gecko_bindings::structs::{RawServoMediaList, RawServoStyleSheetContents}; +use gecko_bindings::sugar::ownership::{HasArcFFI, HasFFI, Strong}; use media_queries::MediaList; use properties::{ComputedValues, PropertyDeclarationBlock}; use properties::animated_properties::AnimationValue; use rule_tree::StrongRuleNode; +use servo_arc::{Arc, ArcBorrow}; use shared_lock::Locked; use std::{mem, ptr}; use stylesheets::{CssRules, StylesheetContents, StyleRule, ImportRule, KeyframesRule, MediaRule}; @@ -51,9 +52,6 @@ impl_arc_ffi!(Locked => ServoCssRules impl_arc_ffi!(StylesheetContents => RawServoStyleSheetContents [Servo_StyleSheetContents_AddRef, Servo_StyleSheetContents_Release]); -impl_arc_ffi!(ComputedValues => ServoStyleContext - [Servo_StyleContext_AddRef, Servo_StyleContext_Release]); - impl_arc_ffi!(Locked => RawServoDeclarationBlock [Servo_DeclarationBlock_AddRef, Servo_DeclarationBlock_Release]); @@ -114,3 +112,28 @@ pub unsafe extern "C" fn Servo_RuleNode_Release(obj: &RawServoRuleNode) { let ptr = StrongRuleNode::from_ffi(&obj); ptr::read(ptr as *const StrongRuleNode); } + +// ServoStyleContext is not an opaque type on any side of FFI. +// This means that doing the HasArcFFI type trick is actually unsound, +// since it gives us a way to construct an Arc from +// an &ServoStyleContext, which in general is not allowed. So we +// implement the restricted set of arc type functionality we need. + +#[no_mangle] +pub unsafe extern "C" fn Servo_StyleContext_AddRef(obj: &ComputedValues) { + mem::forget(ArcBorrow::from_ref(obj).clone_arc()); +} + +#[no_mangle] +pub unsafe extern "C" fn Servo_StyleContext_Release(obj: &ComputedValues) { + ArcBorrow::from_ref(obj).with_arc(|a: &Arc| { + let _: Arc = ptr::read(a); + }); +} + + +impl From> for Strong { + fn from(arc: Arc) -> Self { + unsafe { mem::transmute(Arc::into_raw_offset(arc)) } + } +} diff --git a/components/style/gecko/generated/bindings.rs b/components/style/gecko/generated/bindings.rs index 1f237d61566f..a31af059128e 100644 --- a/components/style/gecko/generated/bindings.rs +++ b/components/style/gecko/generated/bindings.rs @@ -5,7 +5,7 @@ use gecko_bindings::structs::nsStyleTransformMatrix; use gecko_bindings::structs::nsTArray; type nsACString_internal = nsACString; type nsAString_internal = nsAString; -pub type ServoStyleContextBorrowed<'a> = &'a ServoStyleContext; +pub type ServoStyleContextBorrowed<'a> = &'a ::properties::ComputedValues; pub type ServoStyleContextBorrowedOrNull<'a> = Option<&'a ::properties::ComputedValues>; pub type ServoComputedValuesBorrowed<'a> = &'a ServoComputedValues; pub type ServoComputedValuesBorrowedOrNull<'a> = Option<&'a ServoComputedValues>; @@ -2322,7 +2322,7 @@ extern "C" { } extern "C" { pub fn Servo_ComputedValues_SpecifiesAnimationsOrTransitions(computed_values: - ServoComputedValuesBorrowed) + ServoStyleContextBorrowed) -> bool; } extern "C" { @@ -2705,7 +2705,7 @@ extern "C" { } extern "C" { pub fn Servo_ComputedValues_GetStyleBits(values: - ServoComputedValuesBorrowed) + ServoStyleContextBorrowed) -> u64; } extern "C" { @@ -2717,7 +2717,7 @@ extern "C" { } extern "C" { pub fn Servo_ComputedValues_GetStyleRuleList(values: - ServoComputedValuesBorrowed, + ServoStyleContextBorrowed, rules: RawGeckoServoStyleRuleListBorrowedMut); } @@ -2804,17 +2804,17 @@ extern "C" { } extern "C" { pub fn Servo_GetCustomPropertyValue(computed_values: - ServoComputedValuesBorrowed, + ServoStyleContextBorrowed, name: *const nsAString, value: *mut nsAString) -> bool; } extern "C" { pub fn Servo_GetCustomPropertiesCount(computed_values: - ServoComputedValuesBorrowed) + ServoStyleContextBorrowed) -> u32; } extern "C" { - pub fn Servo_GetCustomPropertyNameAt(arg1: ServoComputedValuesBorrowed, + pub fn Servo_GetCustomPropertyNameAt(arg1: ServoStyleContextBorrowed, index: u32, name: *mut nsAString) -> bool; } diff --git a/components/style/gecko/generated/structs_debug.rs b/components/style/gecko/generated/structs_debug.rs index c54e2904f01a..22405e6d5704 100644 --- a/components/style/gecko/generated/structs_debug.rs +++ b/components/style/gecko/generated/structs_debug.rs @@ -16,7 +16,7 @@ pub type ServoRuleNode = Option<::rule_tree::StrongRuleNode>; pub type ServoVisitedStyle = Option<::servo_arc::RawOffsetArc<::properties::ComputedValues>>; pub type ServoComputedValueFlags = ::properties::computed_value_flags::ComputedValueFlags; pub type ServoRawOffsetArc = ::servo_arc::RawOffsetArc; -pub type ServoStyleContextStrong = ::gecko_bindings::sugar::ownership::Strong; +pub type ServoStyleContextStrong = ::gecko_bindings::sugar::ownership::Strong<::properties::ComputedValues>; #[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)] pub mod root { diff --git a/components/style/gecko/generated/structs_release.rs b/components/style/gecko/generated/structs_release.rs index cdcaf83b24c1..86ad173caf3b 100644 --- a/components/style/gecko/generated/structs_release.rs +++ b/components/style/gecko/generated/structs_release.rs @@ -16,7 +16,7 @@ pub type ServoRuleNode = Option<::rule_tree::StrongRuleNode>; pub type ServoVisitedStyle = Option<::servo_arc::RawOffsetArc<::properties::ComputedValues>>; pub type ServoComputedValueFlags = ::properties::computed_value_flags::ComputedValueFlags; pub type ServoRawOffsetArc = ::servo_arc::RawOffsetArc; -pub type ServoStyleContextStrong = ::gecko_bindings::sugar::ownership::Strong; +pub type ServoStyleContextStrong = ::gecko_bindings::sugar::ownership::Strong<::properties::ComputedValues>; #[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)] pub mod root { diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index a6859238d835..9523126f60e2 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -227,7 +227,8 @@ impl ComputedValuesInner { ) -> Arc { let arc = unsafe { let arc: Arc = Arc::new(uninitialized()); - bindings::Gecko_ServoStyleContext_Init(&arc.0 as *const _ as *mut _, parent, pres_context, + bindings::Gecko_ServoStyleContext_Init(&arc.0 as *const _ as *mut _, + parent, pres_context, &self, pseudo_ty, pseudo_tag); // We're simulating a move by having C++ do a memcpy and then forgetting // it on this end. diff --git a/ports/geckolib/glue.rs b/ports/geckolib/glue.rs index 6d71fdf7a725..52ac9419aa22 100644 --- a/ports/geckolib/glue.rs +++ b/ports/geckolib/glue.rs @@ -8,7 +8,7 @@ use cssparser::ToCss as ParserToCss; use env_logger::LogBuilder; use selectors::Element; use selectors::matching::{MatchingContext, MatchingMode, matches_selector}; -use servo_arc::{Arc, RawOffsetArc}; +use servo_arc::{Arc, ArcBorrow, RawOffsetArc}; use std::env; use std::fmt::Write; use std::ptr; @@ -666,10 +666,10 @@ pub extern "C" fn Servo_StyleSet_GetBaseComputedValuesForElement(raw_data: RawSe use style::style_resolver::StyleResolverForElement; debug_assert!(!snapshots.is_null()); - let computed_values = ComputedValues::as_arc(&computed_values); + let computed_values = unsafe { ArcBorrow::from_ref(computed_values) }; let rules = match computed_values.rules { - None => return computed_values.clone().into_strong(), + None => return computed_values.clone_arc().into(), Some(ref rules) => rules, }; @@ -678,14 +678,14 @@ pub extern "C" fn Servo_StyleSet_GetBaseComputedValuesForElement(raw_data: RawSe doc_data.stylist.rule_tree().remove_animation_rules(rules); if without_animations == *rules { - return computed_values.clone().into_strong(); + return computed_values.clone_arc().into(); } let element = GeckoElement(element); let element_data = match element.borrow_data() { Some(data) => data, - None => return computed_values.clone().into_strong(), + None => return computed_values.clone_arc().into(), }; let styles = &element_data.styles; @@ -695,7 +695,7 @@ pub extern "C" fn Servo_StyleSet_GetBaseComputedValuesForElement(raw_data: RawSe .pseudos .get(&pseudo) .expect("GetBaseComputedValuesForElement for an unexisting pseudo?") - .clone().into_strong(); + .clone().into(); } let global_style_data = &*GLOBAL_STYLE_DATA; @@ -721,7 +721,7 @@ pub extern "C" fn Servo_StyleSet_GetBaseComputedValuesForElement(raw_data: RawSe StyleResolverForElement::new(element, &mut context, RuleInclusion::All) .cascade_style_and_visited_with_default_parents(inputs) - .into_strong() + .into() } #[no_mangle] @@ -734,8 +734,7 @@ pub extern "C" fn Servo_ComputedValues_ExtractAnimationValue(computed_values: Se None => return Strong::null(), }; - let computed_values = ComputedValues::as_arc(&computed_values); - Arc::new(AnimationValue::from_computed_values(&property, computed_values)).into_strong() + Arc::new(AnimationValue::from_computed_values(&property, &computed_values)).into_strong() } #[no_mangle] @@ -1526,7 +1525,7 @@ pub extern "C" fn Servo_ComputedValues_GetForAnonymousBox(parent_style_or_null: &pseudo, parent_style_or_null.map(|x| &*x), cascade_flags, &metrics - ).into_strong() + ).into() } #[no_mangle] @@ -1550,7 +1549,7 @@ pub extern "C" fn Servo_ResolvePseudoStyle(element: RawGeckoElementBorrowed, return if is_probe { Strong::null() } else { - doc_data.default_computed_values().clone().into_strong() + doc_data.default_computed_values().clone().into() }; } @@ -1571,7 +1570,7 @@ pub extern "C" fn Servo_ResolvePseudoStyle(element: RawGeckoElementBorrowed, ); match style { - Some(s) => s.into_strong(), + Some(s) => s.into(), None => { debug_assert!(is_probe); Strong::null() @@ -1584,13 +1583,12 @@ pub extern "C" fn Servo_SetExplicitStyle(element: RawGeckoElementBorrowed, style: ServoStyleContextBorrowed) { let element = GeckoElement(element); - let style = ComputedValues::as_arc(&style); debug!("Servo_SetExplicitStyle: {:?}", element); // We only support this API for initial styling. There's no reason it couldn't // work for other things, we just haven't had a reason to do so. debug_assert!(element.get_data().is_none()); let mut data = unsafe { element.ensure_data() }; - data.styles.primary = Some(style.clone_arc()); + data.styles.primary = Some(unsafe { ArcBorrow::from_ref(style) }.clone_arc()); } #[no_mangle] @@ -1730,11 +1728,11 @@ pub extern "C" fn Servo_ComputedValues_Inherit( ).build() }; - style.into_strong() + style.into() } #[no_mangle] -pub extern "C" fn Servo_ComputedValues_GetStyleBits(values: ServoComputedValuesBorrowed) -> u64 { +pub extern "C" fn Servo_ComputedValues_GetStyleBits(values: ServoStyleContextBorrowed) -> u64 { use style::properties::computed_value_flags::*; let flags = values.flags; let mut result = 0; @@ -1748,7 +1746,7 @@ pub extern "C" fn Servo_ComputedValues_GetStyleBits(values: ServoComputedValuesB } #[no_mangle] -pub extern "C" fn Servo_ComputedValues_SpecifiesAnimationsOrTransitions(values: ServoComputedValuesBorrowed) +pub extern "C" fn Servo_ComputedValues_SpecifiesAnimationsOrTransitions(values: ServoStyleContextBorrowed) -> bool { let b = values.get_box(); b.specifies_animations() || b.specifies_transitions() @@ -1763,7 +1761,7 @@ pub extern "C" fn Servo_ComputedValues_EqualCustomProperties( } #[no_mangle] -pub extern "C" fn Servo_ComputedValues_GetStyleRuleList(values: ServoComputedValuesBorrowed, +pub extern "C" fn Servo_ComputedValues_GetStyleRuleList(values: ServoStyleContextBorrowed, rules: RawGeckoServoStyleRuleListBorrowedMut) { if let Some(ref rule_node) = values.rules { let mut result = vec![]; @@ -2804,7 +2802,7 @@ pub extern "C" fn Servo_ResolveStyle(element: RawGeckoElementBorrowed, }; debug_assert!(element.has_current_styles_for_traversal(&*data, flags), "Resolving style on element without current styles"); - data.styles.primary().clone().into_strong() + data.styles.primary().clone().into() } #[no_mangle] @@ -2853,7 +2851,7 @@ pub extern "C" fn Servo_ResolveStyleLazily(element: RawGeckoElementBorrowed, } }); if let Some(result) = styles { - return result.into_strong(); + return result.into(); } } @@ -2870,7 +2868,7 @@ pub extern "C" fn Servo_ResolveStyleLazily(element: RawGeckoElementBorrowed, }; let styles = resolve_style(&mut context, element, rule_inclusion); - finish(&styles).into_strong() + finish(&styles).into() } #[cfg(feature = "gecko_debug")] @@ -2919,7 +2917,6 @@ pub extern "C" fn Servo_GetComputedKeyframeValues(keyframes: RawGeckoKeyframeLis let data = PerDocumentStyleData::from_ffi(raw_data).borrow(); let metrics = get_metrics_provider_for_product(); - let style = ComputedValues::as_arc(&style); let element = GeckoElement(element); let parent_element = element.inheritance_parent(); @@ -2927,7 +2924,7 @@ pub extern "C" fn Servo_GetComputedKeyframeValues(keyframes: RawGeckoKeyframeLis let parent_style = parent_data.as_ref().map(|d| d.styles.primary()).map(|x| &**x); let pseudo = style.pseudo(); - let mut context = create_context(&data, &metrics, style, parent_style, pseudo.as_ref()); + let mut context = create_context(&data, &metrics, &style, parent_style, pseudo.as_ref()); let global_style_data = &*GLOBAL_STYLE_DATA; let guard = global_style_data.shared_lock.read(); @@ -3001,7 +2998,6 @@ pub extern "C" fn Servo_GetAnimationValues(declarations: RawServoDeclarationBloc animation_values: RawGeckoServoAnimationValueListBorrowedMut) { let data = PerDocumentStyleData::from_ffi(raw_data).borrow(); let metrics = get_metrics_provider_for_product(); - let style = ComputedValues::as_arc(&style); let element = GeckoElement(element); let parent_element = element.inheritance_parent(); @@ -3009,7 +3005,7 @@ pub extern "C" fn Servo_GetAnimationValues(declarations: RawServoDeclarationBloc let parent_style = parent_data.as_ref().map(|d| d.styles.primary()).map(|x| &**x); let pseudo = style.pseudo(); - let mut context = create_context(&data, &metrics, style, parent_style, pseudo.as_ref()); + let mut context = create_context(&data, &metrics, &style, parent_style, pseudo.as_ref()); let default_values = data.default_computed_values(); let global_style_data = &*GLOBAL_STYLE_DATA; @@ -3031,7 +3027,6 @@ pub extern "C" fn Servo_AnimationValue_Compute(element: RawGeckoElementBorrowed, -> RawServoAnimationValueStrong { let data = PerDocumentStyleData::from_ffi(raw_data).borrow(); let metrics = get_metrics_provider_for_product(); - let style = ComputedValues::as_arc(&style); let element = GeckoElement(element); let parent_element = element.inheritance_parent(); @@ -3293,7 +3288,7 @@ pub extern "C" fn Servo_StyleSet_ResolveForDeclarations( &guards, parent_style, declarations.clone_arc(), - ).into_strong() + ).into() } #[no_mangle] @@ -3350,7 +3345,7 @@ pub extern "C" fn Servo_StyleSet_HasStateDependency( } #[no_mangle] -pub extern "C" fn Servo_GetCustomPropertyValue(computed_values: ServoComputedValuesBorrowed, +pub extern "C" fn Servo_GetCustomPropertyValue(computed_values: ServoStyleContextBorrowed, name: *const nsAString, value: *mut nsAString) -> bool { let custom_properties = match computed_values.custom_properties() { @@ -3369,7 +3364,7 @@ pub extern "C" fn Servo_GetCustomPropertyValue(computed_values: ServoComputedVal } #[no_mangle] -pub extern "C" fn Servo_GetCustomPropertiesCount(computed_values: ServoComputedValuesBorrowed) -> u32 { +pub extern "C" fn Servo_GetCustomPropertiesCount(computed_values: ServoStyleContextBorrowed) -> u32 { match computed_values.custom_properties() { Some(p) => p.len() as u32, None => 0, @@ -3377,7 +3372,7 @@ pub extern "C" fn Servo_GetCustomPropertiesCount(computed_values: ServoComputedV } #[no_mangle] -pub extern "C" fn Servo_GetCustomPropertyNameAt(computed_values: ServoComputedValuesBorrowed, +pub extern "C" fn Servo_GetCustomPropertyNameAt(computed_values: ServoStyleContextBorrowed, index: u32, name: *mut nsAString) -> bool { let custom_properties = match computed_values.custom_properties() {