Skip to content

Truly independent web browser

LibWeb: Pass cascaded values to the longhand drive by reference

Andreas Klingc75bc2ed49e0
diff --git a/Libraries/LibWeb/Rust/src/css/animation.rs b/Libraries/LibWeb/Rust/src/css/animation.rs
index 2c0e78b6be6..ebfb76a80e7 100644
--- a/Libraries/LibWeb/Rust/src/css/animation.rs
+++ b/Libraries/LibWeb/Rust/src/css/animation.rs
@@ -583,7 +583,7 @@ fn resolve_animation_declarations(
}
crate::css::style_compute::expand_shorthands_with(
declaration.property_id,
- declaration.value.pointer().cast(),
+ &declaration.value,
false,
&mut |longhand_id, data, _| {
let physical_property_id =
@@ -595,11 +595,7 @@ fn resolve_animation_declarations(
custom_is_inherited: false,
source_property_id: declaration.property_id,
source_longhand_id: longhand_id,
- value: unsafe {
- RetainedStyleValueData::from_retained_pointer(crate::css::style_value::retain_style_value(
- data.cast(),
- ))
- },
+ value: data.clone(),
style_sheet_resource_context: declaration.style_sheet_resource_context,
use_initial: declaration.use_initial,
is_transition: declaration.is_transition,
diff --git a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
index e1da8f5a840..195243acee0 100644
--- a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
@@ -113,8 +113,8 @@ impl Hasher for PropertyIdHasher {
}
}
-pub(crate) type WinningDeclaration = (
- *const c_void,
+pub(crate) type WinningDeclaration<'a> = (
+ &'a RetainedStyleValueData,
bool,
u32,
bool,
@@ -123,13 +123,13 @@ pub(crate) type WinningDeclaration = (
/// The immutable winner inputs used by longhand computation, without cascade history.
pub(crate) trait CascadedValues {
- fn winning_declaration(&self, property: u16) -> Option<WinningDeclaration>;
+ fn winning_declaration(&self, property: u16) -> Option<WinningDeclaration<'_>>;
fn winning_origin(&self, property: u16) -> Option<CascadeOrigin>;
fn property_with_higher_priority(&self, first: u16, second: u16) -> u16;
}
impl CascadedValues for CascadedPropertyStore {
- fn winning_declaration(&self, property: u16) -> Option<WinningDeclaration> {
+ fn winning_declaration(&self, property: u16) -> Option<WinningDeclaration<'_>> {
self.winning_declaration(property)
}
fn winning_origin(&self, property: u16) -> Option<CascadeOrigin> {
@@ -304,19 +304,10 @@ impl CascadedPropertyStore {
/// The winning declaration for a property: its Rust-owned data, importance,
/// and C++ declaration-source slot.
- pub(crate) fn winning_declaration(
- &self,
- property_id: u16,
- ) -> Option<(
- *const c_void,
- bool,
- u32,
- bool,
- crate::css::style_compute::ExternalValueDependencies,
- )> {
+ pub(crate) fn winning_declaration(&self, property_id: u16) -> Option<WinningDeclaration<'_>> {
self.last_entry(property_id).map(|entry| {
(
- entry.value.pointer().cast(),
+ &entry.value,
entry.important,
entry.source_slot,
entry.has_style_sheet_context,
diff --git a/Libraries/LibWeb/Rust/src/css/declaration_block.rs b/Libraries/LibWeb/Rust/src/css/declaration_block.rs
index a1a71e09272..15355b8a802 100644
--- a/Libraries/LibWeb/Rust/src/css/declaration_block.rs
+++ b/Libraries/LibWeb/Rust/src/css/declaration_block.rs
@@ -144,13 +144,13 @@ impl DeclarationBlockData {
// the one with greatest cascading order must be represented, at the same relative position as it was specified.
expand_shorthands_with(
declaration.property_id,
- Arc::as_ptr(&declaration.value).cast(),
+ &crate::css::style_value::RetainedStyleValueData::from_arc(declaration.value),
false,
&mut |property_id, value, _| {
self.append_normalized_property(DeclaredProperty {
property_id,
important: declaration.important,
- value: unsafe { retain_value(value) },
+ value: value.clone().into_arc(),
});
},
);
diff --git a/Libraries/LibWeb/Rust/src/css/serialize.rs b/Libraries/LibWeb/Rust/src/css/serialize.rs
index 16430b5670b..f5aa7cecfab 100644
--- a/Libraries/LibWeb/Rust/src/css/serialize.rs
+++ b/Libraries/LibWeb/Rust/src/css/serialize.rs
@@ -3258,28 +3258,26 @@ fn serialize_shorthand(
// If all the longhands are the same CSS-wide keyword, just return that once.
let mut built_in_keyword: Option<u16> = None;
let mut all_same_keyword = true;
- expand_shorthands_with(
- shorthand_property,
- (whole_value as *const StyleValueData).cast(),
- false,
- &mut |_, data, _| {
- // SAFETY: The expansion hands back live Rust-owned style value data.
- let value = unsafe { &*data.cast::<StyleValueData>() };
- if !value_is_css_wide_keyword(value) {
- all_same_keyword = false;
- return;
- }
- let StyleValueData::Keyword { keyword: code } = value else {
- all_same_keyword = false;
- return;
- };
- match built_in_keyword {
- None => built_in_keyword = Some(*code),
- Some(existing) if existing != *code => all_same_keyword = false,
- _ => {}
- }
- },
- );
+ // The expansion of a shorthand value is that of each of its sub-values.
+ let mut check = |_, data: &crate::css::style_value::RetainedStyleValueData, _| {
+ let value = data.data();
+ if !value_is_css_wide_keyword(value) {
+ all_same_keyword = false;
+ return;
+ }
+ let StyleValueData::Keyword { keyword: code } = value else {
+ all_same_keyword = false;
+ return;
+ };
+ match built_in_keyword {
+ None => built_in_keyword = Some(*code),
+ Some(existing) if existing != *code => all_same_keyword = false,
+ _ => {}
+ }
+ };
+ for (&sub_property, sub_value) in sub_properties.iter().zip(values.as_slice()) {
+ expand_shorthands_with(sub_property, sub_value, false, &mut check);
+ }
if let Some(code) = built_in_keyword {
if all_same_keyword {
sink.push_ascii(keyword::NAMES[code as usize]);
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index 229f6a761ff..57e1bfba557 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -3528,7 +3528,7 @@ impl RetainedState {
)?;
continue 'winners;
};
- (location, Some(value))
+ (location, Some(value.data()))
}
// A value with var() references substitutes under the node's environment, as
// the C++ cascade substitutes it; a value invalid at computed-value time is
@@ -4978,20 +4978,11 @@ fn expanded_longhand_value(
value: &crate::css::style_value::RetainedStyleValueData,
) -> Option<crate::css::style_value::RetainedStyleValueData> {
let mut found = None;
- crate::css::style_compute::expand_shorthands_with(
- shorthand,
- value.pointer().cast(),
- false,
- &mut |longhand, data, _| {
- if longhand == property && found.is_none() {
- found = Some(unsafe {
- crate::css::style_value::RetainedStyleValueData::from_retained_pointer(
- crate::css::style_value::retain_style_value(data.cast()),
- )
- });
- }
- },
- );
+ crate::css::style_compute::expand_shorthands_with(shorthand, value, false, &mut |longhand, data, _| {
+ if longhand == property && found.is_none() {
+ found = Some(data.clone());
+ }
+ });
found
}
@@ -5006,15 +4997,10 @@ fn shorthand_longhand_value(
return None;
};
for (&sub_property, sub_value) in sub_properties.as_slice().iter().zip(values.as_slice()) {
- let sub_data = unsafe { &*sub_value.pointer().cast::<crate::css::style_value::StyleValueData>() };
if sub_property == property {
- return Some(unsafe {
- crate::css::style_value::RetainedStyleValueData::from_retained_pointer(
- crate::css::style_value::retain_style_value(sub_data),
- )
- });
+ return Some(sub_value.clone());
}
- if let Some(found) = shorthand_longhand_value(property, sub_data) {
+ if let Some(found) = shorthand_longhand_value(property, sub_value.data()) {
return Some(found);
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs b/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs
index e314cc94052..205eb1a96d1 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs
@@ -654,7 +654,7 @@ impl RetainedState {
// substituted shorthand, which the same declarations hold.
let (substituted_property, written) = match declaration.value.as_ref() {
data @ StyleValueData::Shorthand { .. } => match shorthand_longhand_data(property, data) {
- Some(longhand) => (None, retained(longhand)),
+ Some(longhand) => (None, longhand.clone()),
None => continue,
},
data @ StyleValueData::Unresolved { .. } => (Some(property), retained(data)),
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/drive.rs b/Libraries/LibWeb/Rust/src/css/style/publication/drive.rs
index 4d78a909e08..45188226226 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/drive.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/drive.rs
@@ -708,7 +708,7 @@ impl RetainedState {
progress.recascaded_font_size
} else if store
.winning_declaration(prop::FONT_FAMILY)
- .is_some_and(|(value, ..)| font_family_is_monospace(unsafe { &*value.cast::<StyleValueData>() }))
+ .is_some_and(|(value, ..)| font_family_is_monospace(value.data()))
{
match self.monospace_recascaded_font_size(target, inputs) {
Some(MonospaceRecascade::Size(recascaded, reads_viewport)) => {
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs b/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
index 93917a03bb2..bc5c3d303bb 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
@@ -1305,10 +1305,7 @@ fn pseudo_content_generates_nothing(store: &impl crate::css::cascaded_properties
use crate::css::property_metadata::property_id as prop;
use crate::css::style_compute::keyword;
let generated = matches!(kind, pseudo_kind::BEFORE | pseudo_kind::AFTER);
- match store
- .winning_declaration(prop::CONTENT)
- .map(|(value, ..)| unsafe { &*value.cast::<StyleValueData>() })
- {
+ match store.winning_declaration(prop::CONTENT).map(|(value, ..)| value.data()) {
None => generated,
Some(StyleValueData::Keyword { keyword }) => {
*keyword == keyword::NONE || (*keyword == keyword::NORMAL && generated)
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs b/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs
index d3373a3fb6c..710f1a59780 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/winner_store.rs
@@ -199,7 +199,7 @@ pub(super) struct WinnerView<'a> {
engine: &'a RetainedState,
}
-pub(super) fn shorthand_longhand_data(property: u16, data: &StyleValueData) -> Option<&StyleValueData> {
+pub(super) fn shorthand_longhand_data(property: u16, data: &StyleValueData) -> Option<&RetainedStyleValueData> {
let StyleValueData::Shorthand {
sub_properties, values, ..
} = data
@@ -207,12 +207,10 @@ pub(super) fn shorthand_longhand_data(property: u16, data: &StyleValueData) -> O
return None;
};
for (&sub_property, sub_value) in sub_properties.as_slice().iter().zip(values.as_slice()) {
- // SAFETY: The shorthand owns every nested value for this borrow.
- let sub_data = unsafe { &*sub_value.pointer().cast::<StyleValueData>() };
if sub_property == property {
- return Some(sub_data);
+ return Some(sub_value);
}
- if let Some(found) = shorthand_longhand_data(property, sub_data) {
+ if let Some(found) = shorthand_longhand_data(property, sub_value.data()) {
return Some(found);
}
}
@@ -220,9 +218,9 @@ pub(super) fn shorthand_longhand_data(property: u16, data: &StyleValueData) -> O
}
impl WinnerView<'_> {
- fn value<'a>(&'a self, declaration: &'a WinnerDeclaration) -> &'a StyleValueData {
+ fn value<'a>(&'a self, declaration: &'a WinnerDeclaration) -> &'a RetainedStyleValueData {
let written = match &declaration.value {
- WinnerValue::Substituted { value, .. } => return value.data(),
+ WinnerValue::Substituted { value, .. } => return value,
WinnerValue::Written { node, source, index } => match source {
WinnerSource::Rule(rule) => &self.engine.program.written_values_of(*rule)[*index],
WinnerSource::Element(kind) => self
@@ -238,7 +236,7 @@ impl WinnerView<'_> {
data @ StyleValueData::Shorthand { .. } => {
shorthand_longhand_data(declaration.property, data).expect("validated shorthand winner")
}
- data => data,
+ _ => written,
}
}
}
@@ -275,7 +273,7 @@ impl WinnerStore {
for declaration in &self.declarations {
if view.dependencies(declaration).has_unfixed_random_sharing {
crate::css::style_compute::collect_unfixed_random_sharings_in_value(
- view.value(declaration),
+ view.value(declaration).data(),
&mut sharings,
);
}
@@ -321,7 +319,7 @@ impl<'a> WinnerView<'a> {
if memoized & DEPENDENCIES_COMPUTED != 0 {
return unpack_dependencies(memoized);
}
- let dependencies = external_value_dependencies(self.value(declaration));
+ let dependencies = external_value_dependencies(self.value(declaration).data());
declaration
.dependencies
.store(pack_dependencies(dependencies), Ordering::Relaxed);
@@ -354,10 +352,10 @@ impl<'a> WinnerView<'a> {
}
impl CascadedValues for WinnerView<'_> {
- fn winning_declaration(&self, property: u16) -> Option<WinningDeclaration> {
+ fn winning_declaration(&self, property: u16) -> Option<WinningDeclaration<'_>> {
let (index, declaration) = self.store.declaration(property)?;
Some((
- std::ptr::from_ref(self.value(declaration)).cast(),
+ self.value(declaration),
declaration.important,
u32::try_from(index).unwrap(),
self.resource_context(declaration).is_some(),
@@ -480,8 +478,8 @@ mod tests {
let actual = view.winning_declaration(property).unwrap();
let expected = owned.winning_declaration(property).unwrap();
assert_eq!(
- (actual.0, actual.1, actual.2, actual.3),
- (expected.0, expected.1, expected.2, expected.3)
+ (actual.0.pointer(), actual.1, actual.2, actual.3),
+ (expected.0.pointer(), expected.1, expected.2, expected.3)
);
assert!(matches!(view.winning_origin(property), Some(CascadeOrigin::Author)));
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/tests.rs b/Libraries/LibWeb/Rust/src/css/style/tests.rs
index 7c14b04e979..d691c46e297 100644
--- a/Libraries/LibWeb/Rust/src/css/style/tests.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/tests.rs
@@ -834,7 +834,7 @@ fn retained_winners_construct_a_source_free_cascade_store() {
.unwrap();
let (stored, important, _, has_style_sheet_context, _) =
store.winning_declaration(property_id::BACKGROUND_COLOR).unwrap();
- assert_eq!(stored, value.pointer().cast());
+ assert_eq!(stored.pointer(), value.pointer());
assert!(important);
assert!(!has_style_sheet_context);
}
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index b5be196d67f..8ec16fd04a6 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -2692,20 +2692,19 @@ pub fn map_physical_to_logical_alias(property_id: u16, writing_mode: u8, directi
}
}
-struct ComputedStoreEntry {
+struct ComputedStoreEntry<'a> {
property_id: u16,
/// The selected specified value; also the stored value when
/// `computed_kind` is `COMPUTED_KIND_UNCHANGED`.
- data: *const c_void,
+ data: &'a RetainedStyleValueData,
/// The C++ declaration-source slot for a cascaded value, or -1.
source_slot: i64,
/// Whether the selected cascaded facade carried stylesheet context.
has_style_sheet_context: bool,
inheritance_dependent: bool,
- /// An owned replacement style value when `computed_kind` is
- /// `COMPUTED_KIND_STYLE_VALUE`. The Rust table adopts this reference when
- /// storing the entry.
- computed_data: *const c_void,
+ /// The replacement style value when `computed_kind` is
+ /// `COMPUTED_KIND_STYLE_VALUE`.
+ computed_data: Option<RetainedStyleValueData>,
/// How Rust constructs the table value: with COMPUTED_KIND_UNCHANGED the
/// stored value is `data` itself; the other kinds carry a replacement in
/// `value` while `data` remains the specified value for the
@@ -3204,11 +3203,8 @@ pub(crate) fn table_row_bits(property_id: u16) -> u8 {
}
}
-fn value_is_initial_or_unset(value: *const c_void) -> bool {
- if value.is_null() {
- return false;
- }
- match unsafe { &*(value as *const StyleValueData) } {
+fn value_is_initial_or_unset(value: &StyleValueData) -> bool {
+ match value {
StyleValueData::Keyword { keyword } => *keyword == keyword::INITIAL || *keyword == keyword::UNSET,
_ => false,
}
@@ -3231,40 +3227,33 @@ fn retained_new(value: StyleValueData) -> RetainedStyleValueData {
RetainedStyleValueData::from_arc(Arc::new(value))
}
-fn needs_computed_style_sheet_context(value: *const StyleValueData) -> bool {
- if value.is_null() {
- return false;
- }
+fn needs_computed_style_sheet_context(value: &RetainedStyleValueData) -> bool {
// NB: Keep this traversal aligned with StyleValue::set_style_sheet(). Only image
// wrappers consume the context; container wrappers forward it to their children.
- match unsafe { &*value } {
- StyleValueData::Image { .. } | StyleValueData::ImageSet { .. } => true,
- StyleValueData::Content { content, alt_text } => {
- needs_computed_style_sheet_context(content.pointer())
- || needs_computed_style_sheet_context(alt_text.pointer())
+ match value.optional_data() {
+ Some(StyleValueData::Image { .. } | StyleValueData::ImageSet { .. }) => true,
+ Some(StyleValueData::Content { content, alt_text }) => {
+ needs_computed_style_sheet_context(content) || needs_computed_style_sheet_context(alt_text)
+ }
+ Some(StyleValueData::Shorthand { values, .. } | StyleValueData::ValueList { values, .. }) => {
+ values.as_slice().iter().any(needs_computed_style_sheet_context)
}
- StyleValueData::Shorthand { values, .. } | StyleValueData::ValueList { values, .. } => values
- .as_slice()
- .iter()
- .any(|value| needs_computed_style_sheet_context(value.pointer())),
_ => false,
}
}
/// Store what a drive found for one longhand: its computed value, and the specified value its
/// inheritance depends on, if it does.
-fn store_driven_value(longhand_table: &mut ComputedLonghandTable, entry: &ComputedStoreEntry) {
- let specified_value = entry.inheritance_dependent.then(|| unsafe {
- RetainedStyleValueData::from_retained_pointer(crate::css::style_value::retain_style_value(entry.data.cast()))
- });
+fn store_driven_value(longhand_table: &mut ComputedLonghandTable, entry: ComputedStoreEntry) {
+ let specified_value = entry.inheritance_dependent.then(|| entry.data.clone());
longhand_table.set_drive_inheritance_dependent_value(entry.property_id, specified_value);
store_computed_value(longhand_table, entry);
}
-fn store_computed_value(longhand_table: &mut ComputedLonghandTable, entry: &ComputedStoreEntry) {
+fn store_computed_value(longhand_table: &mut ComputedLonghandTable, entry: ComputedStoreEntry) {
let source_slot = if entry.has_style_sheet_context
&& entry.computed_kind == COMPUTED_KIND_UNCHANGED
- && needs_computed_style_sheet_context(entry.data.cast())
+ && needs_computed_style_sheet_context(entry.data)
{
entry.source_slot
} else {
@@ -3272,17 +3261,14 @@ fn store_computed_value(longhand_table: &mut ComputedLonghandTable, entry: &Comp
};
let computed = match entry.computed_kind {
COMPUTED_KIND_UNCHANGED => {
- let retained = unsafe {
- RetainedStyleValueData::from_retained_pointer(crate::css::style_value::retain_style_value(
- entry.data.cast(),
- ))
- };
- longhand_table.set_or_keep_equal(entry.property_id, retained, source_slot);
+ longhand_table.set_or_keep_equal(entry.property_id, entry.data.clone(), source_slot);
return;
}
COMPUTED_KIND_STYLE_VALUE => {
- let retained = unsafe { RetainedStyleValueData::from_retained_pointer(entry.computed_data.cast()) };
- longhand_table.set_or_keep_equal(entry.property_id, retained, source_slot);
+ let computed = entry
+ .computed_data
+ .expect("a replaced computed value carries its replacement");
+ longhand_table.set_or_keep_equal(entry.property_id, computed, source_slot);
return;
}
COMPUTED_KIND_PX_LENGTH => StyleValueData::Length {
@@ -3454,7 +3440,7 @@ pub(crate) unsafe fn drive_property_computation(
cascaded_property_id = store.property_with_higher_priority(property_id, counterpart_property_id);
}
- let mut value = std::ptr::null();
+ let mut value: Option<&RetainedStyleValueData> = None;
let mut source_slot = -1;
let mut has_style_sheet_context = false;
let mut external_dependencies = None;
@@ -3466,7 +3452,8 @@ pub(crate) unsafe fn drive_property_computation(
cascaded_external_dependencies,
)) = store.winning_declaration(cascaded_property_id)
{
- value = value_data;
+ // An empty winning value cascades nothing, as no declaration would.
+ value = Some(value_data).filter(|value| value.optional_data().is_some());
source_slot = i64::from(cascaded_source_slot);
has_style_sheet_context = cascaded_has_style_sheet_context;
external_dependencies = Some(cascaded_external_dependencies);
@@ -3474,11 +3461,7 @@ pub(crate) unsafe fn drive_property_computation(
set_longhand_bit(&mut important_words, property_id);
}
if property_id == crate::css::property_metadata::property_id::FONT_SIZE {
- longhand_table.set_raw_cascaded_font_size(Some(unsafe {
- RetainedStyleValueData::from_retained_pointer(crate::css::style_value::retain_style_value(
- value_data.cast(),
- ))
- }));
+ longhand_table.set_raw_cascaded_font_size(Some(value_data.clone()));
}
} else if property_id == crate::css::property_metadata::property_id::FONT_SIZE && has_new_font_size {
// NOTE: The recascaded font-size has already been stored before the loop.
@@ -3487,11 +3470,7 @@ pub(crate) unsafe fn drive_property_computation(
longhand_table.set_raw_cascaded_font_size(None);
}
- let cascaded_value = if value.is_null() {
- None
- } else {
- Some(unsafe { &*(value as *const StyleValueData) })
- };
+ let cascaded_value = value.map(RetainedStyleValueData::data);
let decision = longhand_decision(cascaded_value, property_id);
// The computation-need level to compare against depends on which source wins;
@@ -3551,8 +3530,8 @@ pub(crate) unsafe fn drive_property_computation(
// non-inherited property take the parent's stored computed value for
// `inherited_property_id` straight from the snapshot's table span.
value = snapshot
- .value(inherited_property_id)
- .map_or(std::ptr::null(), |data| (data as *const StyleValueData).cast());
+ .value_handle(inherited_property_id)
+ .filter(|value| value.optional_data().is_some());
if property_affects_font_metrics(inherited_property_id) && snapshot.font_metrics_depend_on_viewport_metrics
{
results.font_metrics_depend_on_viewport_metrics = true;
@@ -3561,7 +3540,7 @@ pub(crate) unsafe fn drive_property_computation(
}
let use_initial = if inherit_fetch_attempted {
- value.is_null() || value_is_initial_or_unset(value)
+ value.is_none_or(|value| value_is_initial_or_unset(value.data()))
} else {
decision.use_initial_without_inherit
};
@@ -3569,7 +3548,7 @@ pub(crate) unsafe fn drive_property_computation(
source_slot = -1;
has_style_sheet_context = false;
external_dependencies = Some(initial_value_dependencies(property_id));
- value = initial_value_data(property_id).cast();
+ value = Some(initial_value_handle(property_id));
required_level = REQUIRES_COMPUTATION_NON_INHERITED;
}
@@ -3577,7 +3556,8 @@ pub(crate) unsafe fn drive_property_computation(
// Whether the computed value depends on inherited information, so the specified
// value must be kept for re-resolution when an ancestor changes.
- let value_data = unsafe { &*(value as *const StyleValueData) };
+ let value = value.expect("a longhand has a cascaded, inherited or initial value");
+ let value_data = value.data();
let external_dependencies = external_dependencies.unwrap_or_else(|| external_value_dependencies(value_data));
if tree_counting_context.is_some() && external_dependencies.uses_tree_counting_function {
@@ -4262,16 +4242,20 @@ pub(crate) unsafe fn drive_property_computation(
(native, _) => native,
};
let (computed_kind, computed_value, computed_data) = match native {
- NativeValue::Px(px) => (COMPUTED_KIND_PX_LENGTH, px, std::ptr::null()),
- NativeValue::Integer(integer) => (COMPUTED_KIND_INTEGER, integer as f64, std::ptr::null()),
- NativeValue::Superellipse(parameter) => (COMPUTED_KIND_SUPERELLIPSE, parameter, std::ptr::null()),
- NativeValue::Number(number) => (COMPUTED_KIND_NUMBER, number, std::ptr::null()),
- NativeValue::Percentage(percentage) => (COMPUTED_KIND_PERCENTAGE, percentage, std::ptr::null()),
+ NativeValue::Px(px) => (COMPUTED_KIND_PX_LENGTH, px, None),
+ NativeValue::Integer(integer) => (COMPUTED_KIND_INTEGER, integer as f64, None),
+ NativeValue::Superellipse(parameter) => (COMPUTED_KIND_SUPERELLIPSE, parameter, None),
+ NativeValue::Number(number) => (COMPUTED_KIND_NUMBER, number, None),
+ NativeValue::Percentage(percentage) => (COMPUTED_KIND_PERCENTAGE, percentage, None),
NativeValue::FontStyle(font_style_keyword) => {
- (COMPUTED_KIND_FONT_STYLE, font_style_keyword as f64, std::ptr::null())
+ (COMPUTED_KIND_FONT_STYLE, font_style_keyword as f64, None)
}
- NativeValue::StyleValue(value) => (COMPUTED_KIND_STYLE_VALUE, 0.0, Arc::into_raw(value).cast()),
- NativeValue::Unchanged => (COMPUTED_KIND_UNCHANGED, 0.0, std::ptr::null()),
+ NativeValue::StyleValue(value) => (
+ COMPUTED_KIND_STYLE_VALUE,
+ 0.0,
+ Some(RetainedStyleValueData::from_arc(value)),
+ ),
+ NativeValue::Unchanged => (COMPUTED_KIND_UNCHANGED, 0.0, None),
NativeValue::Unsupported => {
unreachable!("unsupported native computation for longhand property {inherited_property_id}")
}
@@ -4293,7 +4277,7 @@ pub(crate) unsafe fn drive_property_computation(
source_slot,
has_style_sheet_context,
inheritance_dependent,
- computed_data: std::ptr::null(),
+ computed_data: None,
computed_kind: COMPUTED_KIND_UNCHANGED,
value: 0.0,
}
@@ -4395,7 +4379,7 @@ pub(crate) unsafe fn drive_property_computation(
}
let mut store = |entry: ComputedStoreEntry| {
- store_driven_value(longhand_table, &entry);
+ store_driven_value(longhand_table, entry);
};
if property_id == prop::OVERFLOW_X {
pending_overflow_x_store = Some(entry);
@@ -6300,18 +6284,18 @@ fn apply_post_compute_adjustments(
};
let adjusted_entry = |property_id, computed_kind, value| ComputedStoreEntry {
property_id,
- data: initial_value_data(property_id).cast(),
+ data: initial_value_handle(property_id),
source_slot: -1,
has_style_sheet_context: false,
inheritance_dependent: false,
- computed_data: std::ptr::null(),
+ computed_data: None,
computed_kind,
value,
};
// An adjustment moves only a computed value. Whether the value depends on the parent stays
// what the drive recorded for the specified value, as for a match-parent text-align a table
// adjusts.
- let mut store = |entry: ComputedStoreEntry| store_computed_value(longhand_table, &entry);
+ let mut store = |entry: ComputedStoreEntry| store_computed_value(longhand_table, entry);
let mut post_adjusted_longhands = 0;
if transformation.set_float_none {
post_adjusted_longhands |= POST_ADJUSTED_FLOAT;
@@ -6401,13 +6385,13 @@ pub(crate) fn value_is_css_wide_keyword(value: &StyleValueData) -> bool {
/// values have no facade-local resource context.
pub(crate) fn expand_shorthands_with<Sink>(
property_id: u16,
- data: *const c_void,
+ data: &RetainedStyleValueData,
has_style_sheet_context: bool,
sink: &mut Sink,
) where
- Sink: FnMut(u16, *const c_void, bool),
+ Sink: FnMut(u16, &RetainedStyleValueData, bool),
{
- let value = unsafe { &*(data as *const StyleValueData) };
+ let value = data.data();
let is_shorthand = property_is_shorthand(property_id);
if is_shorthand
@@ -6424,12 +6408,11 @@ pub(crate) fn expand_shorthands_with<Sink>(
// https://drafts.csswg.org/css-values-5/#pending-substitution-value
// Ensure we keep the longhand around until it can be resolved.
sink(property_id, data, has_style_sheet_context);
- let retained_data = unsafe { crate::css::style_value::retain_style_value(data.cast::<StyleValueData>()) };
- let pending_data =
- unsafe { crate::css::style_value::rust_style_value_create_pending_substitution(retained_data) };
- let pending = unsafe { RetainedStyleValueData::from_retained_pointer(pending_data) };
+ let pending = RetainedStyleValueData::from_arc(std::sync::Arc::new(StyleValueData::PendingSubstitution {
+ original_shorthand_value: data.clone(),
+ }));
for &longhand in longhands_for_shorthand(property_id) {
- expand_shorthands_with(longhand, pending.pointer().cast(), has_style_sheet_context, sink);
+ expand_shorthands_with(longhand, &pending, has_style_sheet_context, sink);
}
return;
}
@@ -6439,8 +6422,7 @@ pub(crate) fn expand_shorthands_with<Sink>(
} = value
{
for (&sub_property, sub_value) in sub_properties.as_slice().iter().zip(values.as_slice()) {
- let sub_data = sub_value.pointer().cast();
- expand_shorthands_with(sub_property, sub_data, false, sink);
+ expand_shorthands_with(sub_property, sub_value, false, sink);
}
return;
}
@@ -6465,15 +6447,11 @@ struct ShorthandExpansion {
_values: Vec<RetainedStyleValueData>,
}
-fn expand_shorthands(property_id: u16, data: *const c_void) -> ShorthandExpansion {
+fn expand_shorthands(property_id: u16, data: &RetainedStyleValueData) -> ShorthandExpansion {
let mut properties = Vec::new();
let mut values = Vec::new();
expand_shorthands_with(property_id, data, false, &mut |longhand_id, longhand_data, _| {
- let value = unsafe {
- RetainedStyleValueData::from_retained_pointer(crate::css::style_value::retain_style_value(
- longhand_data.cast(),
- ))
- };
+ let value = longhand_data.clone();
properties.push(FfiExpandedProperty {
property_id: longhand_id,
data: value.pointer().cast(),
@@ -6496,7 +6474,11 @@ pub unsafe extern "C" fn rust_expand_property_shorthands(
property_id: u16,
data: *const c_void,
) -> FfiShorthandExpansion {
- let expansion = Box::new(expand_shorthands(property_id, data));
+ // SAFETY: Guaranteed by the caller.
+ let data = unsafe {
+ RetainedStyleValueData::from_retained_pointer(crate::css::style_value::retain_style_value(data.cast()))
+ };
+ let expansion = Box::new(expand_shorthands(property_id, &data));
let properties = expansion.properties.as_ptr();
let count = expansion.properties.len();
FfiShorthandExpansion {