Skip to content

Truly independent web browser

LibWeb: Read and compare style values through their handles

Andreas Klingc564c41e4e01
diff --git a/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs b/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs
index 9dc0facd4a8..e4ce561f4df 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs
@@ -53,13 +53,8 @@ const fn mix_slot_hash(slot: usize, content: u64) -> u64 {
.rotate_left((slot as u32) & 63)
}
-pub(crate) fn longhand_slot_hash(slot: usize, value: *const c_void) -> u64 {
- if value.is_null() {
- return mix_slot_hash(slot, 0);
- }
- // SAFETY: A non-null slot value is live style value data.
- let content = crate::css::style_value::style_value_content_hash(unsafe { value.cast::<StyleValueData>().as_ref() });
- mix_slot_hash(slot, content)
+pub(crate) fn longhand_slot_hash(slot: usize, value: Option<&StyleValueData>) -> u64 {
+ mix_slot_hash(slot, crate::css::style_value::style_value_content_hash(value))
}
/// The memoized sum of a table's per-slot hashes.
@@ -423,19 +418,21 @@ impl SlotStorage {
/// base reduces the comparison to the union of the two change lists, and a delta agrees with
/// its own base everywhere but in its changes. Two tables sharing dense storage agree
/// everywhere.
- fn values_are_equal(&self, other: &Self, equal: impl Fn(*const c_void, *const c_void) -> bool) -> bool {
+ fn values_are_equal(
+ &self,
+ other: &Self,
+ equal: impl Fn(&RetainedStyleValueData, &RetainedStyleValueData) -> bool,
+ ) -> bool {
match (self, other) {
(Self::Dense(dense), Self::Delta { base, changes, .. })
| (Self::Delta { base, changes, .. }, Self::Dense(dense))
if Arc::ptr_eq(dense, base) =>
{
changes.iter().all(|change| {
- change.value.as_ref().is_none_or(|value| {
- equal(
- value.pointer().cast(),
- base.slots[usize::from(change.index)].pointer().cast(),
- )
- })
+ change
+ .value
+ .as_ref()
+ .is_none_or(|value| equal(value, &base.slots[usize::from(change.index)]))
})
}
(Self::Dense(first), Self::Dense(second)) => {
@@ -444,7 +441,7 @@ impl SlotStorage {
.slots
.iter()
.zip(second.slots.iter())
- .all(|(first, second)| equal(first.pointer().cast(), second.pointer().cast()))
+ .all(|(first, second)| equal(first, second))
}
(
Self::Delta {
@@ -475,7 +472,7 @@ impl SlotStorage {
second_value = second_changes[second].value.as_ref().unwrap_or(base);
second += 1;
}
- if !equal(first_value.pointer().cast(), second_value.pointer().cast()) {
+ if !equal(first_value, second_value) {
return false;
}
}
@@ -484,7 +481,7 @@ impl SlotStorage {
_ => self
.values()
.zip(other.values())
- .all(|(first, second)| equal(first.pointer().cast(), second.pointer().cast())),
+ .all(|(first, second)| equal(first, second)),
}
}
@@ -736,8 +733,7 @@ impl ComputedLonghandTable {
);
let index = Self::slot_index(property_id);
let existing = self.storage.slot(index).0;
- let equal_to_existing = !existing.pointer().is_null()
- && unsafe { crate::css::style_value::rust_style_value_equals(existing.pointer(), value.pointer()) };
+ let equal_to_existing = !existing.pointer().is_null() && *existing == value;
if !equal_to_existing {
self.replace_slot_value(index, value);
}
@@ -745,10 +741,9 @@ impl ComputedLonghandTable {
}
fn replace_slot_value(&mut self, index: usize, value: RetainedStyleValueData) {
- let pointer = value.pointer().cast();
- let previous = self.storage.slot(index).0.pointer().cast();
- if previous != pointer {
- self.adjust_slot_hash_sum(index, previous, pointer);
+ let previous = self.storage.slot(index).0;
+ if previous.pointer() != value.pointer() {
+ self.adjust_slot_hash_sum(index, previous.optional_data(), value.optional_data());
}
self.storage.set_value(index, value);
}
@@ -758,7 +753,12 @@ impl ComputedLonghandTable {
self.storage.set_source(index, i32::try_from(source_slot).unwrap_or(-1));
}
- fn adjust_slot_hash_sum(&mut self, index: usize, previous: *const c_void, replacement: *const c_void) {
+ fn adjust_slot_hash_sum(
+ &self,
+ index: usize,
+ previous: Option<&StyleValueData>,
+ replacement: Option<&StyleValueData>,
+ ) {
if let Some(sum) = self.slot_hash_sum.get() {
self.slot_hash_sum.set(Some(
sum.wrapping_sub(longhand_slot_hash(index, previous))
@@ -778,7 +778,7 @@ impl ComputedLonghandTable {
pub(crate) fn recomputed_slot_hash_sum(&self) -> u64 {
(0..LONGHAND_COUNT).fold(0_u64, |sum, slot| {
- sum.wrapping_add(longhand_slot_hash(slot, self.storage.slot(slot).0.pointer().cast()))
+ sum.wrapping_add(longhand_slot_hash(slot, self.storage.slot(slot).0.optional_data()))
})
}
@@ -946,13 +946,9 @@ impl ComputedLonghandTable {
}
pub(crate) fn raw_cascaded_font_size_value(&self) -> Option<&StyleValueData> {
- self.raw_cascaded_font_size.as_ref().map(RetainedStyleValueData::data)
- }
-
- pub(crate) fn raw_cascaded_font_size(&self) -> *const c_void {
self.raw_cascaded_font_size
.as_ref()
- .map_or(std::ptr::null(), |value| value.pointer().cast())
+ .and_then(RetainedStyleValueData::optional_data)
}
pub(crate) fn set_raw_cascaded_font_size(&mut self, value: Option<RetainedStyleValueData>) {
@@ -992,11 +988,12 @@ impl ComputedLonghandTable {
"the computed longhand table is immutable once its style is created"
);
let slot = Self::slot_index(property_id);
- let previous = self.storage.slot(slot).0.pointer().cast();
- let replacement = source.storage.slot(slot).0.pointer().cast();
- if previous != replacement {
- self.adjust_slot_hash_sum(slot, previous, replacement);
+ let previous = self.storage.slot(slot).0;
+ let replacement = source.storage.slot(slot).0;
+ if previous.pointer() != replacement.pointer() {
+ self.adjust_slot_hash_sum(slot, previous.optional_data(), replacement.optional_data());
}
+ let replacement = replacement.pointer().cast::<c_void>();
let source_slot = source.storage.slot(slot).1;
if let SlotStorage::Delta {
base,
@@ -1327,26 +1324,31 @@ impl ComputedLonghandTable {
}
pub(crate) fn publication_equals(&self, other: &Self) -> bool {
- let values_equal = |first: *const c_void, second: *const c_void| {
- first == second
- || (!first.is_null()
- && !second.is_null()
- && unsafe { crate::css::style_value::rust_style_value_equals(first.cast(), second.cast()) })
- };
+ use crate::css::style_value::{optional_style_values_equal, retained_style_values_equal};
self.important_bits == other.important_bits
&& self.inherited_bits == other.inherited_bits
&& self.publication_sidecars() == other.publication_sidecars()
&& self.publication_dependency_flags() == other.publication_dependency_flags()
&& self.pseudo_element_styles() == other.pseudo_element_styles()
&& self.inheritance_dependent.len() == other.inheritance_dependent.len()
- && self.storage.values_are_equal(&other.storage, values_equal)
- && values_equal(self.raw_cascaded_font_size(), other.raw_cascaded_font_size())
+ && self
+ .storage
+ .values_are_equal(&other.storage, retained_style_values_equal)
+ && optional_style_values_equal(
+ self.raw_cascaded_font_size
+ .as_ref()
+ .and_then(RetainedStyleValueData::optional_data),
+ other
+ .raw_cascaded_font_size
+ .as_ref()
+ .and_then(RetainedStyleValueData::optional_data),
+ )
&& self.inheritance_dependent.iter().all(|(property, value)| {
other
.inheritance_dependent
.iter()
.find(|(other_property, _)| other_property == property)
- .is_some_and(|(_, other_value)| values_equal(value.pointer().cast(), other_value.pointer().cast()))
+ .is_some_and(|(_, other_value)| retained_style_values_equal(value, other_value))
})
}
@@ -1748,12 +1750,7 @@ mod tests {
#[test]
fn slot_values_compare_over_a_shared_base_and_across_bases() {
- let equal = |first: *const c_void, second: *const c_void| {
- first == second
- || (!first.is_null()
- && !second.is_null()
- && unsafe { crate::css::style_value::rust_style_value_equals(first.cast(), second.cast()) })
- };
+ let equal = crate::css::style_value::retained_style_values_equal;
let mut base = ComputedLonghandTable::new();
base.set(property_id::OPACITY, retained_number(0.5), -1);
base.set(property_id::Z_INDEX, retained_number(7.0), -1);
diff --git a/Libraries/LibWeb/Rust/src/css/style/computed.rs b/Libraries/LibWeb/Rust/src/css/style/computed.rs
index 52e05fdc0a9..9d0463ebd07 100644
--- a/Libraries/LibWeb/Rust/src/css/style/computed.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/computed.rs
@@ -4090,9 +4090,9 @@ fn longhand_table_hash(table: &ComputedLonghandTable) -> u64 {
// NB: Inheritance-dependent values compare independently of insertion order. Sum the
// property-mixed hashes, as for longhand slots, without copying and sorting the values.
let mut inheritance_dependent_hash_sum = 0_u64;
- for (property, value) in table.inheritance_dependent_values() {
- inheritance_dependent_hash_sum =
- inheritance_dependent_hash_sum.wrapping_add(longhand_slot_hash(usize::from(property), value));
+ for (property, value) in table.retained_inheritance_dependent_values() {
+ inheritance_dependent_hash_sum = inheritance_dependent_hash_sum
+ .wrapping_add(longhand_slot_hash(usize::from(property), value.optional_data()));
}
inheritance_dependent_hash_sum.hash(&mut hasher);
hasher.finish()
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
index c07b6138769..86d760c86d7 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
@@ -256,8 +256,10 @@ impl RetainedState {
);
// The element's own color resolves first, since every other group resolves `currentcolor`
// against it.
- let color_value = table.effective_value(Some(overlay), prop::COLOR, true).value;
- let color_data = unsafe { color_value.cast::<StyleValueData>().as_ref() };
+ let color_data = table
+ .effective_value_handle(Some(overlay), prop::COLOR, true)
+ .0
+ .optional_data();
let color = color_data
.and_then(|color| {
to_color(
@@ -1339,13 +1341,7 @@ pub(super) fn effective_data<'a>(
overlay: Option<&'a AnimatedOverlay>,
property: u16,
) -> Option<&'a StyleValueData> {
- unsafe {
- table
- .effective_value(overlay, property, true)
- .value
- .cast::<StyleValueData>()
- .as_ref()
- }
+ table.effective_value_handle(overlay, property, true).0.optional_data()
}
/// What resolving an element's font asks the document's resolver, as the C++ font computer would
diff --git a/Libraries/LibWeb/Rust/src/css/style/environment_move.rs b/Libraries/LibWeb/Rust/src/css/style/environment_move.rs
index 94dc7c26f5f..66c947c99aa 100644
--- a/Libraries/LibWeb/Rust/src/css/style/environment_move.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/environment_move.rs
@@ -116,15 +116,14 @@ fn custom_property_value_moved(
let new = new.and_then(|store| store.get(name));
let (old, new) = match (old, new) {
(None, None) => return false,
- (Some(old), Some(new)) => (old.value.pointer(), new.value.pointer()),
+ (Some(old), Some(new)) => (&old.value, &new.value),
_ => return true,
};
- if std::ptr::eq(old, new) {
+ if std::ptr::eq(old.pointer(), new.pointer()) {
return false;
}
- // SAFETY: Both values are kept alive by the stores that hold them.
- matches!(unsafe { &*old }, StyleValueData::PendingSubstitution { .. })
- || !unsafe { crate::css::style_value::rust_style_value_equals(old, new) }
+ matches!(old.optional_data(), Some(StyleValueData::PendingSubstitution { .. }))
+ || !crate::css::style_value::retained_style_values_equal(old, new)
}
/// # Safety
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index 6ba659fa3bb..6bc433e14f1 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -1877,13 +1877,7 @@ impl RetainedState {
table: &ComputedLonghandTable,
) -> u64 {
use crate::css::property_metadata::property_id as prop;
- let value = |property| unsafe {
- table
- .effective_value(None, property, true)
- .value
- .cast::<StyleValueData>()
- .as_ref()
- };
+ let value = |property| table.effective_value_handle(None, property, true).0.optional_data();
if !crate::css::style_compute::style_reads_counter_style_environment(
value(prop::CONTENT),
value(prop::LIST_STYLE_TYPE),
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/drive.rs b/Libraries/LibWeb/Rust/src/css/style/publication/drive.rs
index 45188226226..b7ff245e9ef 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/drive.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/drive.rs
@@ -228,7 +228,7 @@ impl RetainedState {
.1
.as_ref()
.and_then(|view| view.longhand_table)
- .map_or(std::ptr::null(), ComputedLonghandTable::raw_cascaded_font_size)
+ .and_then(ComputedLonghandTable::raw_cascaded_font_size_value)
};
let default_size = CssPixels::from_integer(13).raw_value();
let document_inputs = FfiFontSizeRecascadeDocumentInputs::from_document(inputs);
@@ -240,7 +240,7 @@ impl RetainedState {
false,
default_size,
document_inputs,
- std::ptr::null(),
+ None,
);
while batch.status != FontSizeRecascadeStatus::Complete {
let index = batch.next_index;
@@ -329,7 +329,7 @@ impl RetainedState {
batch.depends_on_viewport_metrics,
default_size,
document_inputs,
- &raw const context,
+ Some(&context),
);
// The context resolves every length a cascaded font-size holds.
if batch.status == FontSizeRecascadeStatus::NeedsCppLengthResolution {
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 8e4f67e0a94..d3011ae3679 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -799,27 +799,26 @@ impl FfiFontSizeRecascadeDocumentInputs {
/// and viewport-relative lengths resolve from the size reached so far and the
/// document's inputs.
///
-/// A non-null `length_resolution_context` applies only to the value at
+/// A `length_resolution_context` applies only to the value at
/// `start_index`.
/// Building it involves font work, so the caller does so lazily after a batch
/// reports `NeedsLengthResolution` and resumes at the reported index.
///
#[allow(clippy::too_many_arguments)]
-pub(crate) fn recascade_font_size_batch(
+pub(crate) fn recascade_font_size_batch<'a>(
value_count: usize,
- mut value_at: impl FnMut(usize) -> *const c_void,
+ mut value_at: impl FnMut(usize) -> Option<&'a StyleValueData>,
start_index: usize,
current_size_raw: i32,
current_depends_on_viewport_metrics: bool,
default_size_raw: i32,
document_inputs: FfiFontSizeRecascadeDocumentInputs,
- length_resolution_context: *const FfiLengthResolutionContext,
+ supplied_length_resolution_context: Option<&FfiLengthResolutionContext>,
) -> FfiFontSizeRecascadeBatch {
assert!(start_index <= value_count);
let mut current_size = CssPixels::from_raw(current_size_raw);
let mut depends_on_viewport_metrics = current_depends_on_viewport_metrics;
let default_size = CssPixels::from_raw(default_size_raw);
- let supplied_length_resolution_context = unsafe { length_resolution_context.as_ref() };
let mut skipped_calculated_value = false;
let stopped =
|status, next_index, current_size: CssPixels, depends_on_viewport_metrics, skipped_calculated_value| {
@@ -833,11 +832,9 @@ pub(crate) fn recascade_font_size_batch(
};
for index in start_index..value_count {
- let value = value_at(index);
- if value.is_null() {
+ let Some(value) = value_at(index) else {
continue;
- }
- let value = unsafe { &*value.cast::<StyleValueData>() };
+ };
match value {
StyleValueData::Keyword { keyword } => {
if *keyword == keyword::INITIAL || *keyword == keyword::UNSET {
@@ -3060,9 +3057,7 @@ impl<'a> ParentSnapshot<'a> {
return Some(entry.value_handle());
}
for (property, value) in self.table.retained_inheritance_dependent_values() {
- if property == property_id
- && crate::css::style_value::style_value_dependency_flags(value.pointer()) & 1 != 0
- {
+ if property == property_id && crate::css::style_value::style_value_dependency_flags(value.data()) & 1 != 0 {
return Some(value);
}
}
@@ -4945,8 +4940,7 @@ fn effective_longhand_data<'a>(
overlay: Option<&'a AnimatedOverlay>,
property_id: u16,
) -> &'a StyleValueData {
- let effective = table.effective_value(overlay, property_id, true);
- unsafe { &*effective.value.cast::<StyleValueData>() }
+ table.effective_value_handle(overlay, property_id, true).0.data()
}
fn effective_keyword(table: &ComputedLonghandTable, overlay: Option<&AnimatedOverlay>, property_id: u16) -> u16 {
@@ -7060,8 +7054,8 @@ fn finalize_animated_box_type(
// A value the overlay animates keeps its animation's place; one it does not is adjusted over the base value.
let mut adjust = |property_id: u16, keyword_or_display: StyleValueData| {
let value = retained_new(keyword_or_display);
- let effective = longhand_table.effective_value(Some(&*overlay), property_id, true);
- if unsafe { &*effective.value.cast::<StyleValueData>() } == value.data() {
+ let (effective, _) = longhand_table.effective_value_handle(Some(&*overlay), property_id, true);
+ if effective.data() == value.data() {
return;
}
match overlay
@@ -7696,12 +7690,7 @@ mod tests {
unit: unit_code("em") as u8,
};
let final_percentage = StyleValueData::Percentage { value: 50.0 };
- let values: [*const std::ffi::c_void; 4] = [
- std::ptr::null(),
- (&percentage as *const StyleValueData).cast(),
- (&em as *const StyleValueData).cast(),
- (&final_percentage as *const StyleValueData).cast(),
- ];
+ let values = [None, Some(&percentage), Some(&em), Some(&final_percentage)];
let default_size = CssPixels::from_integer(13);
let batch = recascade_font_size_batch(
values.len(),
@@ -7716,7 +7705,7 @@ mod tests {
viewport_width: 800.0,
viewport_height: 600.0,
},
- std::ptr::null(),
+ None,
);
assert!(batch.status == FontSizeRecascadeStatus::Complete);
assert_eq!(batch.next_index, values.len());
@@ -7733,10 +7722,7 @@ mod tests {
value: 10.0,
unit: unit_code("vw") as u8,
};
- let values: [*const std::ffi::c_void; 2] = [
- (&rem as *const StyleValueData).cast(),
- (&vw as *const StyleValueData).cast(),
- ];
+ let values = [Some(&rem), Some(&vw)];
let batch = recascade_font_size_batch(
values.len(),
|index| values[index],
@@ -7750,7 +7736,7 @@ mod tests {
viewport_width: 800.0,
viewport_height: 600.0,
},
- std::ptr::null(),
+ None,
);
assert!(batch.status == FontSizeRecascadeStatus::Complete);
assert_eq!(batch.current_size_raw, CssPixels::from_integer(80).raw_value());
@@ -7763,7 +7749,7 @@ mod tests {
value: 2.0,
unit: unit_code("ex") as u8,
};
- let values: [*const std::ffi::c_void; 1] = [(&ex as *const StyleValueData).cast()];
+ let values = [Some(&ex)];
let batch = recascade_font_size_batch(
values.len(),
|index| values[index],
@@ -7777,7 +7763,7 @@ mod tests {
viewport_width: 800.0,
viewport_height: 600.0,
},
- std::ptr::null(),
+ None,
);
assert!(batch.status == FontSizeRecascadeStatus::NeedsLengthResolution);
assert_eq!(batch.next_index, 0);
diff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rs
index 30ed9fca81c..edb56abac72 100644
--- a/Libraries/LibWeb/Rust/src/css/style_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs
@@ -3289,7 +3289,7 @@ pub(crate) fn retained_style_values_equal(first: &RetainedStyleValueData, second
}
/// Two absent values are equal, and an absent value equals no present one.
-fn optional_style_values_equal(first: Option<&StyleValueData>, second: Option<&StyleValueData>) -> bool {
+pub(crate) fn optional_style_values_equal(first: Option<&StyleValueData>, second: Option<&StyleValueData>) -> bool {
match (first, second) {
(None, None) => true,
(Some(first), Some(second)) => style_values_equal(first, second),
@@ -3549,23 +3549,22 @@ pub(crate) fn value_may_depend_on_font_metrics(value: &StyleValueData) -> bool {
.expect("font dependency requested for an unsupported value")
}
-pub(crate) fn style_value_dependency_flags(value: *const StyleValueData) -> u8 {
- let value = unsafe { &*value };
+pub(crate) fn style_value_dependency_flags(value: &StyleValueData) -> u8 {
u8::from(value_depends_on_current_color(value))
| (u8::from(value_depends_on_color_scheme(value)) << 1)
| (u8::from(value_may_depend_on_font_metrics(value)) << 2)
}
pub(crate) fn retained_value_depends_on_current_color(value: &RetainedStyleValueData) -> bool {
- style_value_dependency_flags(value.pointer()) & 1 != 0
+ style_value_dependency_flags(value.data()) & 1 != 0
}
pub(crate) fn retained_value_depends_on_color_scheme(value: &RetainedStyleValueData) -> bool {
- style_value_dependency_flags(value.pointer()) & 2 != 0
+ style_value_dependency_flags(value.data()) & 2 != 0
}
pub(crate) fn retained_value_may_depend_on_font_metrics(value: &RetainedStyleValueData) -> bool {
- style_value_dependency_flags(value.pointer()) & 4 != 0
+ style_value_dependency_flags(value.data()) & 4 != 0
}
/// # Safety