Skip to content

Truly independent web browser

LibWeb: Take style values by reference in the FFI computations

Andreas Klingb6af8a9aa878
diff --git a/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp b/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp
index b57259c4c23..e52cbe2065a 100644
--- a/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp
+++ b/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp
@@ -423,7 +423,7 @@ StyleValue const& ComputedStyleWorkingSet::property(PropertyID property_id, With
return *wrapper;
}
-void const* ComputedStyleWorkingSet::effective_property_data(PropertyID property_id, WithAnimationsApplied return_animated_value) const
+StyleValueFFI::StyleValueData const* ComputedStyleWorkingSet::effective_property_data(PropertyID property_id, WithAnimationsApplied return_animated_value) const
{
VERIFY(property_id >= first_longhand_property_id && property_id <= last_longhand_property_id);
auto effective = ComputedValuesFFI::rust_computed_longhand_table_effective_value(
@@ -432,7 +432,7 @@ void const* ComputedStyleWorkingSet::effective_property_data(PropertyID property
to_underlying(property_id),
return_animated_value == WithAnimationsApplied::Yes);
VERIFY(effective.value);
- return effective.value;
+ return static_cast<StyleValueFFI::StyleValueData const*>(effective.value);
}
// https://drafts.csswg.org/css-color-adjust-1/#determine-the-used-color-scheme
diff --git a/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.h b/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.h
index f92efb737ed..7a854bcf1ed 100644
--- a/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.h
+++ b/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.h
@@ -99,7 +99,7 @@ public:
void clear_animated_properties(Badge<StyleComputer>);
OrderedHashMap<Utf16FlyString, NonnullRefPtr<StyleValue const>> const& animated_custom_properties() const { return m_animated_custom_properties; }
StyleValue const& property(PropertyID, WithAnimationsApplied = WithAnimationsApplied::Yes) const;
- void const* effective_property_data(PropertyID, WithAnimationsApplied = WithAnimationsApplied::Yes) const;
+ StyleValueFFI::StyleValueData const* effective_property_data(PropertyID, WithAnimationsApplied = WithAnimationsApplied::Yes) const;
PreferredColorScheme color_scheme(PreferredColorScheme, Optional<Vector<Utf16FlyString> const&> document_supported_schemes) const;
FontVariantEmoji font_variant_emoji() const;
diff --git a/Libraries/LibWeb/CSS/ComputedValues.h b/Libraries/LibWeb/CSS/ComputedValues.h
index d67458044c2..592336865ea 100644
--- a/Libraries/LibWeb/CSS/ComputedValues.h
+++ b/Libraries/LibWeb/CSS/ComputedValues.h
@@ -591,7 +591,7 @@ public:
RefPtr<AbstractImageStyleValue const> list_style_image() const { return m_inherited.list->list_style_image_value(); }
// Whether this style reads its tree scope's counter-style registry, which a pseudo-element's does for any named
// counter style.
- bool reads_counter_style_environment(bool is_pseudo) const { return ComputedValuesFFI::rust_style_reads_counter_style_environment(m_noninherited.content_data->content.pointer, list_style_type_data(), is_pseudo); }
+ bool reads_counter_style_environment(bool is_pseudo) const { return ComputedValuesFFI::rust_style_reads_counter_style_environment(static_cast<StyleValueFFI::StyleValueData const*>(m_noninherited.content_data->content.pointer), static_cast<StyleValueFFI::StyleValueData const*>(list_style_type_data()), is_pseudo); }
Optional<URL> clip_path() const { return m_noninherited.mask_data->clip_path_value(); }
Color flood_color() const { return Gfx::Color::from_bgra(m_noninherited.svg_reset->flood_color); }
diff --git a/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp
index decdd83e0da..422f5727d9f 100644
--- a/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp
+++ b/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp
@@ -289,7 +289,7 @@ bool CalculatedStyleValue::contains_percentage() const
}
// https://drafts.css-houdini.org/css-typed-om-1/#reify-a-math-expression
-static GC::Ptr<CSSNumericValue> reify_rust_calculation(void const* calculated_data)
+static GC::Ptr<CSSNumericValue> reify_rust_calculation(StyleValueFFI::StyleValueData const* calculated_data)
{
struct Reification {
StyleValueFFI::FfiCalcReification description;
diff --git a/Libraries/LibWeb/Rust/build.rs b/Libraries/LibWeb/Rust/build.rs
index 6c5ef0902ce..d5145128613 100644
--- a/Libraries/LibWeb/Rust/build.rs
+++ b/Libraries/LibWeb/Rust/build.rs
@@ -2718,6 +2718,15 @@ fn main() -> Result<(), Box<dyn Error>> {
.after_includes
.get_or_insert_with(String::new)
.push_str("\nnamespace Web::CSS::Parser::ValueParserFFI { struct DeclarationBlockData; }");
+ // The computations C++ hands a style value take the value as the style value header declares it.
+ computed_values_config.export.rename.insert(
+ "StyleValueData".to_string(),
+ "Web::CSS::StyleValueFFI::StyleValueData".to_string(),
+ );
+ computed_values_config
+ .after_includes
+ .get_or_insert_with(String::new)
+ .push_str("\nnamespace Web::CSS::StyleValueFFI { union StyleValueData; }");
// An animation sample and plan read the document's style engine, which the document host reaches.
declare_document_host_of_layout_header(&mut computed_values_config);
diff --git a/Libraries/LibWeb/Rust/src/css/calc.rs b/Libraries/LibWeb/Rust/src/css/calc.rs
index b87984c8369..0d62db95fe3 100644
--- a/Libraries/LibWeb/Rust/src/css/calc.rs
+++ b/Libraries/LibWeb/Rust/src/css/calc.rs
@@ -2667,18 +2667,21 @@ pub(crate) fn collect_unfixed_random_sharings(node: &CalcNode, sharings: &mut Ve
node.for_each_child(&mut |child| collect_unfixed_random_sharings(child, sharings));
}
+/// Returns the borrowed calculation-tree root stored in calculated style value data.
+pub(crate) fn calc_root(calculated: &crate::css::style_value::StyleValueData) -> &CalcNode {
+ let crate::css::style_value::StyleValueData::Calculated { rust_calculation, .. } = calculated else {
+ unreachable!("calc_root requires calculated value data");
+ };
+ rust_calculation.node()
+}
+
/// Returns the borrowed calculation-tree root stored in calculated style value
/// data.
///
/// # Safety
/// `calculated` must point at live Calculated style value data.
pub(crate) unsafe fn calc_root_from_calculated(calculated: *const std::ffi::c_void) -> *const CalcNode {
- let crate::css::style_value::StyleValueData::Calculated { rust_calculation, .. } =
- (unsafe { &*(calculated as *const crate::css::style_value::StyleValueData) })
- else {
- unreachable!("calc_root_from_calculated requires calculated value data");
- };
- rust_calculation.node()
+ calc_root(unsafe { &*calculated.cast::<crate::css::style_value::StyleValueData>() })
}
/// The resolve-as target from a calculated value's creation-time fields.
@@ -3363,15 +3366,11 @@ pub(crate) fn resolve_calculated_line_height_without_context(
/// valid resolution context.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_calc_resolve(
- calculated: *const std::ffi::c_void,
+ calculated: &crate::css::style_value::StyleValueData,
context: &FfiCalcResolutionContext,
apply_censoring_and_clamping: bool,
) -> FfiResolvedCalc {
- let calculated = unsafe { &*calculated.cast::<crate::css::style_value::StyleValueData>() };
- let externals = prepare_external_resolutions(
- unsafe { &*calc_root_from_calculated(std::ptr::from_ref(calculated).cast()) },
- context,
- );
+ let externals = prepare_external_resolutions(calc_root(calculated), context);
resolve_calculated(
calculated,
context,
@@ -4420,14 +4419,14 @@ impl CalcNode {
}
/// Structural equality of two calculated style values' trees.
-///
-/// # Safety
-/// Both pointers must reference Calculated style value data.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_calc_equals(first: *const std::ffi::c_void, second: *const std::ffi::c_void) -> bool {
+pub extern "C" fn rust_calc_equals(
+ first: &crate::css::style_value::StyleValueData,
+ second: &crate::css::style_value::StyleValueData,
+) -> bool {
use crate::css::style_value::StyleValueData;
- let tree_of = |data: *const std::ffi::c_void| {
- let StyleValueData::Calculated { rust_calculation, .. } = (unsafe { &*(data as *const StyleValueData) }) else {
+ let tree_of = |data: &StyleValueData| {
+ let StyleValueData::Calculated { rust_calculation, .. } = data else {
unreachable!("rust_calc_equals requires calculated value data");
};
rust_calculation.node_arc()
@@ -4552,18 +4551,17 @@ fn append_reification_node(
/// https://drafts.css-houdini.org/css-typed-om-1/#reify-a-math-expression
/// Describe a calculation tree in postorder so C++ can create its GC-owned Typed OM objects.
/// Unsupported math functions return an empty description.
-///
-/// # Safety
-/// `calculated` must point at Calculated style value data.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_calc_describe_for_typed_om(calculated: *const std::ffi::c_void) -> FfiCalcReification {
+pub extern "C" fn rust_calc_describe_for_typed_om(
+ calculated: &crate::css::style_value::StyleValueData,
+) -> FfiCalcReification {
let crate::css::style_value::StyleValueData::Calculated {
rust_calculation,
has_percentages_resolve_as,
resolve_as_is_number,
resolve_as_base,
..
- } = (unsafe { &*(calculated as *const crate::css::style_value::StyleValueData) })
+ } = calculated
else {
unreachable!("rust_calc_describe_for_typed_om requires calculated value data");
};
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index d3011ae3679..de487600cf6 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -584,13 +584,9 @@ fn compute_font_width(value: &StyleValueData) -> FfiComputedNumber {
}
/// Computes the font-width property from its absolutized value.
-///
-/// # Safety
-/// `absolutized_value` must point at a valid StyleValueData.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_compute_font_width(absolutized_value: *const c_void) -> FfiComputedNumber {
- let value = unsafe { &*(absolutized_value as *const StyleValueData) };
- compute_font_width(value)
+pub extern "C" fn rust_compute_font_width(absolutized_value: &StyleValueData) -> FfiComputedNumber {
+ compute_font_width(absolutized_value)
}
/// An <absolute-size> keyword refers to an entry in a table of font sizes computed and kept by
@@ -727,20 +723,16 @@ fn compute_font_size(
/// Computes the font-size property from its absolutized value. The font size
/// inputs are raw CSSPixels values.
-///
-/// # Safety
-/// `absolutized_value` must point at a valid StyleValueData.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_compute_font_size(
- absolutized_value: *const c_void,
+pub extern "C" fn rust_compute_font_size(
+ absolutized_value: &StyleValueData,
computed_math_depth: i32,
inherited_font_size_raw: i32,
inherited_math_depth: i32,
default_font_size_raw: i32,
) -> FfiComputedNumber {
- let value = unsafe { &*(absolutized_value as *const StyleValueData) };
compute_font_size(
- value,
+ absolutized_value,
computed_math_depth,
CssPixels::from_raw(inherited_font_size_raw),
inherited_math_depth,
@@ -969,11 +961,9 @@ pub(crate) fn content_reads_counter_style_environment(value: &StyleValueData) ->
}
}
-/// # Safety
-/// `value` must point to a live style value.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_content_reads_counter_style_environment(value: *const c_void) -> bool {
- content_reads_counter_style_environment(unsafe { &*value.cast::<StyleValueData>() })
+pub extern "C" fn rust_content_reads_counter_style_environment(value: &StyleValueData) -> bool {
+ content_reads_counter_style_environment(value)
}
/// The counter-style names no @counter-style rule overrides: decimal, disc, square, circle,
@@ -1016,22 +1006,13 @@ pub(crate) fn style_reads_counter_style_environment(
}
}
-/// # Safety
-/// `content` and `list_style_type` must point to live style values.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_reads_counter_style_environment(
- content: *const c_void,
- list_style_type: *const c_void,
+pub extern "C" fn rust_style_reads_counter_style_environment(
+ content: Option<&StyleValueData>,
+ list_style_type: Option<&StyleValueData>,
is_pseudo: bool,
) -> bool {
- // SAFETY: Guaranteed by the caller.
- unsafe {
- style_reads_counter_style_environment(
- content.cast::<StyleValueData>().as_ref(),
- list_style_type.cast::<StyleValueData>().as_ref(),
- is_pseudo,
- )
- }
+ style_reads_counter_style_environment(content, list_style_type, is_pseudo)
}
/// https://drafts.css-houdini.org/css-properties-values-api/#computationally-independent
@@ -1988,12 +1969,9 @@ pub(crate) fn collect_unfixed_random_sharings_in_value(
}
}
-/// # Safety
-/// `data` must point at a valid StyleValueData.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_is_computationally_independent(data: *const c_void) -> bool {
- value_is_computationally_independent(unsafe { &*(data as *const StyleValueData) })
- .expect("computational independence requested for an unsupported value")
+pub extern "C" fn rust_style_value_is_computationally_independent(data: &StyleValueData) -> bool {
+ value_is_computationally_independent(data).expect("computational independence requested for an unsupported value")
}
// The computed value of the math-depth value is determined as follows:
@@ -2308,11 +2286,9 @@ pub struct FfiFontStyleComputation {
/// this keyword arm is reached when StylePropertyMap sets a keyword directly.
/// https://drafts.csswg.org/css-fonts-4/#font-style-prop
///
-/// # Safety
-/// `absolutized_value` must point at a valid StyleValueData.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_compute_font_style(absolutized_value: *const c_void) -> FfiFontStyleComputation {
- if let StyleValueData::Keyword { keyword } = (unsafe { &*(absolutized_value as *const StyleValueData) })
+pub extern "C" fn rust_compute_font_style(absolutized_value: &StyleValueData) -> FfiFontStyleComputation {
+ if let StyleValueData::Keyword { keyword } = absolutized_value
&& let Some(font_style_keyword) = keyword_to_font_style_keyword(*keyword)
{
return FfiFontStyleComputation {
@@ -6940,14 +6916,13 @@ fn resolve_effective_color_scheme(
/// Resolves the used color scheme for a value that required C++ computation.
///
/// # Safety
-/// `value` must point at valid ColorScheme StyleValueData and `input` at valid
-/// document inputs for the duration of the call.
+/// `input` must point at valid document inputs for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_resolve_effective_color_scheme(
- value: *const c_void,
+ value: &StyleValueData,
input: &FfiEffectiveColorSchemeInput,
) -> u8 {
- let StyleValueData::ColorScheme { scheme_codes, .. } = (unsafe { &*(value as *const StyleValueData) }) else {
+ let StyleValueData::ColorScheme { scheme_codes, .. } = value else {
unreachable!("computed color-scheme must have color-scheme data");
};
resolve_effective_color_scheme(scheme_codes.as_slice(), input.preferred_color_scheme, unsafe {
@@ -7118,16 +7093,12 @@ pub extern "C" fn rust_finalize_animated_box_type(
}
/// Computes the font-weight property from its absolutized value.
-///
-/// # Safety
-/// `absolutized_value` must point at a valid StyleValueData.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_compute_font_weight(
- absolutized_value: *const c_void,
+pub extern "C" fn rust_compute_font_weight(
+ absolutized_value: &StyleValueData,
inherited_font_weight: f64,
) -> FfiComputedNumber {
- let value = unsafe { &*(absolutized_value as *const StyleValueData) };
- compute_font_weight(value, inherited_font_weight)
+ compute_font_weight(absolutized_value, inherited_font_weight)
}
// The standalone cargo test binary has no C++ side, so release callbacks are
diff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rs
index edb56abac72..6833174d841 100644
--- a/Libraries/LibWeb/Rust/src/css/style_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs
@@ -2167,36 +2167,27 @@ pub unsafe extern "C" fn rust_style_value_copy_computed_font_families(
}
/// Reads the primitive payload of a computed absolute length.
-///
-/// # Safety
-/// `value` must point at live Length StyleValueData.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_computed_length_value(value: *const c_void) -> f64 {
- match unsafe { &*(value as *const StyleValueData) } {
+pub extern "C" fn rust_style_value_computed_length_value(value: &StyleValueData) -> f64 {
+ match value {
StyleValueData::Length { value, .. } => *value,
_ => unreachable!("computed value must be a length"),
}
}
/// Reads the primitive payload of a computed number.
-///
-/// # Safety
-/// `value` must point at live Number StyleValueData.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_computed_number(value: *const c_void) -> f64 {
- match unsafe { &*(value as *const StyleValueData) } {
+pub extern "C" fn rust_style_value_computed_number(value: &StyleValueData) -> f64 {
+ match value {
StyleValueData::Number { value } => *value,
_ => unreachable!("computed value must be a number"),
}
}
/// Reads the primitive payload of a computed percentage.
-///
-/// # Safety
-/// `value` must point at live Percentage StyleValueData.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_computed_percentage(value: *const c_void) -> f64 {
- match unsafe { &*(value as *const StyleValueData) } {
+pub extern "C" fn rust_style_value_computed_percentage(value: &StyleValueData) -> f64 {
+ match value {
StyleValueData::Percentage { value } => *value,
_ => unreachable!("computed value must be a percentage"),
}
@@ -3567,9 +3558,7 @@ pub(crate) fn retained_value_may_depend_on_font_metrics(value: &RetainedStyleVal
style_value_dependency_flags(value.data()) & 4 != 0
}
-/// # Safety
-/// `data` must point at a valid StyleValueData.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_depends_on_current_color(data: *const c_void) -> bool {
- value_depends_on_current_color(unsafe { &*(data as *const StyleValueData) })
+pub extern "C" fn rust_style_value_depends_on_current_color(data: &StyleValueData) -> bool {
+ value_depends_on_current_color(data)
}