Skip to content

Truly independent web browser

LibWeb: Hand style values to Rust as OwnedArc and ArcRef

Andreas Klinga3994eb7b585
diff --git a/Libraries/LibWeb/Rust/src/css/animated_overlay.rs b/Libraries/LibWeb/Rust/src/css/animated_overlay.rs
index df7e3073191..483c775f0ab 100644
--- a/Libraries/LibWeb/Rust/src/css/animated_overlay.rs
+++ b/Libraries/LibWeb/Rust/src/css/animated_overlay.rs
@@ -224,21 +224,15 @@ pub extern "C" fn rust_animated_overlay_free(overlay: OwnedBox<AnimatedOverlay>)
}
/// Stores or replaces the overlay entry for a longhand, retaining `value`.
-///
-/// # Safety
-/// `overlay` must be a valid overlay and `value` must point at live style
-/// value data.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_animated_overlay_set(
+pub extern "C" fn rust_animated_overlay_set(
overlay: &mut AnimatedOverlay,
property: u16,
- value: *const c_void,
+ value: crate::css::ffi_support::ArcRef<'_, StyleValueData>,
inherited: bool,
result_of_transition: bool,
) {
- let retained = unsafe {
- RetainedStyleValueData::from_retained_pointer(crate::css::style_value::retain_style_value(value.cast()))
- };
+ let retained = RetainedStyleValueData::from_arc(value.to_arc());
overlay.set_owned(property, retained, inherited, result_of_transition);
}
diff --git a/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs b/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs
index a922cc29a0b..fc9b13f4a27 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs
@@ -1452,21 +1452,18 @@ pub extern "C" fn rust_computed_longhand_table_create_with_inherited_values(
/// winning declaration's cascade source slot when its value carries style
/// sheet context, and -1 otherwise; storing without a slot clears any slot a
/// previous store recorded for the longhand.
-///
-/// # Safety
-/// `table` must be a valid, unfrozen, uniquely owned table and `data` must
-/// point at live `StyleValueData`.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_computed_longhand_table_set(
+pub extern "C" fn rust_computed_longhand_table_set(
table: &mut ComputedLonghandTable,
property_id: u16,
- data: *const c_void,
+ data: crate::css::ffi_support::ArcRef<'_, StyleValueData>,
source_slot: i64,
) {
- let value = unsafe {
- RetainedStyleValueData::from_retained_pointer(crate::css::style_value::retain_style_value(data.cast()))
- };
- table.set(property_id, value, source_slot);
+ table.set(
+ property_id,
+ RetainedStyleValueData::from_arc(data.to_arc()),
+ source_slot,
+ );
}
/// Replaces the table's contents with `source`'s, for the C++ builder that
diff --git a/Libraries/LibWeb/Rust/src/css/ffi_support.rs b/Libraries/LibWeb/Rust/src/css/ffi_support.rs
index 790c577cfb4..eebb90ff981 100644
--- a/Libraries/LibWeb/Rust/src/css/ffi_support.rs
+++ b/Libraries/LibWeb/Rust/src/css/ffi_support.rs
@@ -126,6 +126,11 @@ impl<T> OwnedArc<T> {
pub(crate) fn into_arc(mut self) -> std::sync::Arc<T> {
self.take().expect("C++ hands over a live object")
}
+
+ /// The reference, or none where C++ handed over null.
+ pub(crate) fn into_option(mut self) -> Option<std::sync::Arc<T>> {
+ self.take()
+ }
}
impl<T> Drop for OwnedArc<T> {
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 02d2db32ed8..40ab40c7412 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -7095,18 +7095,12 @@ fn expand_shorthands(property_id: u16, data: &RetainedStyleValueData) -> Shortha
/// Expands a declared property into one owned batch of longhand assignments, recursing through
/// shorthand and pending-substitution values.
-///
-/// # Safety
-/// `data` must point to live Rust-owned style value data.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_expand_property_shorthands(
+pub extern "C" fn rust_expand_property_shorthands(
property_id: u16,
- data: *const c_void,
+ data: crate::css::ffi_support::ArcRef<'_, StyleValueData>,
) -> FfiShorthandExpansion {
- // SAFETY: Guaranteed by the caller.
- let data = unsafe {
- RetainedStyleValueData::from_retained_pointer(crate::css::style_value::retain_style_value(data.cast()))
- };
+ let data = RetainedStyleValueData::from_arc(data.to_arc());
let expansion = Box::new(expand_shorthands(property_id, &data));
let properties = expansion.properties.as_ptr();
let count = expansion.properties.len();
diff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rs
index df9dabedd89..2488c29faa2 100644
--- a/Libraries/LibWeb/Rust/src/css/style_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs
@@ -11,6 +11,7 @@
//! [`StyleValueData`] is exposed to C++
//! through cbindgen so that hot accessors compile to inline field reads with no FFI call.
+use crate::css::ffi_support::OwnedArc;
use std::ffi::c_void;
use std::sync::Arc;
@@ -422,6 +423,10 @@ impl RetainedStyleValueData {
unsafe { Self::from_retained_pointer(Arc::into_raw(value)) }
}
+ pub(crate) fn from_optional_arc(value: Option<Arc<StyleValueData>>) -> Self {
+ value.map_or_else(Self::none, Self::from_arc)
+ }
+
/// Borrow the handles' pointer storage without copying or transferring ownership.
pub(crate) fn pointer_slice(values: &[Self]) -> &[*const c_void] {
const {
@@ -3110,13 +3115,13 @@ pub extern "C" fn rust_style_value_create_time(value: f64, unit: u8) -> *const S
/// Takes ownership of one strong reference to each of the numerator and denominator.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_create_ratio(
- numerator: *const StyleValueData,
- denominator: *const StyleValueData,
+pub extern "C" fn rust_style_value_create_ratio(
+ numerator: OwnedArc<StyleValueData>,
+ denominator: OwnedArc<StyleValueData>,
) -> *const StyleValueData {
Arc::into_raw(Arc::new(StyleValueData::Ratio {
- numerator: unsafe { RetainedStyleValueData::from_retained_pointer(numerator) },
- denominator: unsafe { RetainedStyleValueData::from_retained_pointer(denominator) },
+ numerator: RetainedStyleValueData::from_arc(numerator.into_arc()),
+ denominator: RetainedStyleValueData::from_arc(denominator.into_arc()),
}))
}
@@ -3186,25 +3191,25 @@ pub extern "C" fn rust_style_value_create_guaranteed_invalid() -> *const StyleVa
/// Takes ownership of one strong reference to the original shorthand value.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_create_pending_substitution(
- original_shorthand_value: *const StyleValueData,
+pub extern "C" fn rust_style_value_create_pending_substitution(
+ original_shorthand_value: OwnedArc<StyleValueData>,
) -> *const StyleValueData {
Arc::into_raw(Arc::new(StyleValueData::PendingSubstitution {
- original_shorthand_value: unsafe { RetainedStyleValueData::from_retained_pointer(original_shorthand_value) },
+ original_shorthand_value: RetainedStyleValueData::from_arc(original_shorthand_value.into_arc()),
}))
}
/// Takes ownership of one strong reference to the filter's value.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_create_filter(
+pub extern "C" fn rust_style_value_create_filter(
kind: u8,
color_operation: u8,
- value: *const StyleValueData,
+ value: OwnedArc<StyleValueData>,
) -> *const StyleValueData {
Arc::into_raw(Arc::new(StyleValueData::Filter {
kind,
color_operation,
- value: unsafe { RetainedStyleValueData::from_retained_pointer(value) },
+ value: RetainedStyleValueData::from_arc(value.into_arc()),
}))
}
@@ -3223,44 +3228,44 @@ pub unsafe extern "C" fn rust_style_value_create_custom_ident(custom_ident: usiz
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_style_value_create_function(
name: usize,
- value: *const StyleValueData,
+ value: OwnedArc<StyleValueData>,
) -> *const StyleValueData {
Arc::into_raw(Arc::new(StyleValueData::Function {
name: unsafe { CssString::from_leaked_raw(name) },
- value: unsafe { RetainedStyleValueData::from_retained_pointer(value) },
+ value: RetainedStyleValueData::from_arc(value.into_arc()),
}))
}
/// Takes ownership of one strong reference to the angle value data if it is non-null.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_create_font_style(
+pub extern "C" fn rust_style_value_create_font_style(
font_style: u8,
- angle_value: *const StyleValueData,
+ angle_value: OwnedArc<StyleValueData>,
) -> *const StyleValueData {
Arc::into_raw(Arc::new(StyleValueData::FontStyle {
font_style,
- angle_value: unsafe { RetainedStyleValueData::from_retained_optional_pointer(angle_value) },
+ angle_value: RetainedStyleValueData::from_optional_arc(angle_value.into_option()),
}))
}
/// Takes ownership of one strong reference to each non-null style value.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_value_create_shadow(
+pub extern "C" fn rust_style_value_create_shadow(
shadow_type: u8,
- color: *const StyleValueData,
- offset_x: *const StyleValueData,
- offset_y: *const StyleValueData,
- blur_radius: *const StyleValueData,
- spread_distance: *const StyleValueData,
+ color: OwnedArc<StyleValueData>,
+ offset_x: OwnedArc<StyleValueData>,
+ offset_y: OwnedArc<StyleValueData>,
+ blur_radius: OwnedArc<StyleValueData>,
+ spread_distance: OwnedArc<StyleValueData>,
placement: u8,
) -> *const StyleValueData {
Arc::into_raw(Arc::new(StyleValueData::Shadow {
shadow_type,
- color: unsafe { RetainedStyleValueData::from_retained_optional_pointer(color) },
- offset_x: unsafe { RetainedStyleValueData::from_retained_pointer(offset_x) },
- offset_y: unsafe { RetainedStyleValueData::from_retained_pointer(offset_y) },
- blur_radius: unsafe { RetainedStyleValueData::from_retained_optional_pointer(blur_radius) },
- spread_distance: unsafe { RetainedStyleValueData::from_retained_optional_pointer(spread_distance) },
+ color: RetainedStyleValueData::from_optional_arc(color.into_option()),
+ offset_x: RetainedStyleValueData::from_arc(offset_x.into_arc()),
+ offset_y: RetainedStyleValueData::from_arc(offset_y.into_arc()),
+ blur_radius: RetainedStyleValueData::from_optional_arc(blur_radius.into_option()),
+ spread_distance: RetainedStyleValueData::from_optional_arc(spread_distance.into_option()),
placement,
}))
}
@@ -3438,13 +3443,13 @@ pub unsafe extern "C" fn rust_style_value_create_color_function(
has_color_type: bool,
color_type: u8,
color_syntax: u8,
- channel_0: *const StyleValueData,
- channel_1: *const StyleValueData,
- channel_2: *const StyleValueData,
- alpha: *const StyleValueData,
+ channel_0: OwnedArc<StyleValueData>,
+ channel_1: OwnedArc<StyleValueData>,
+ channel_2: OwnedArc<StyleValueData>,
+ alpha: OwnedArc<StyleValueData>,
has_name: bool,
name: usize,
- origin_color: *const StyleValueData,
+ origin_color: OwnedArc<StyleValueData>,
) -> *const StyleValueData {
Arc::into_raw(Arc::new(StyleValueData::ColorFunction {
color_base: ColorBase {
@@ -3452,13 +3457,13 @@ pub unsafe extern "C" fn rust_style_value_create_color_function(
color_type,
color_syntax,
},
- channel_0: unsafe { RetainedStyleValueData::from_retained_pointer(channel_0) },
- channel_1: unsafe { RetainedStyleValueData::from_retained_pointer(channel_1) },
- channel_2: unsafe { RetainedStyleValueData::from_retained_pointer(channel_2) },
- alpha: unsafe { RetainedStyleValueData::from_retained_optional_pointer(alpha) },
+ channel_0: RetainedStyleValueData::from_arc(channel_0.into_arc()),
+ channel_1: RetainedStyleValueData::from_arc(channel_1.into_arc()),
+ channel_2: RetainedStyleValueData::from_arc(channel_2.into_arc()),
+ alpha: RetainedStyleValueData::from_optional_arc(alpha.into_option()),
has_name,
name: unsafe { CssString::from_leaked_raw(name) },
- origin_color: unsafe { RetainedStyleValueData::from_retained_optional_pointer(origin_color) },
+ origin_color: RetainedStyleValueData::from_optional_arc(origin_color.into_option()),
}))
}