Skip to content

Truly independent web browser

LibWeb: Hand CSS strings to C++ as typed pointers

Andreas Klingbf35424e41a2
diff --git a/Libraries/LibWeb/Animations/NamedTimelines.cpp b/Libraries/LibWeb/Animations/NamedTimelines.cpp
index 12728565bce..f5e1f8e8768 100644
--- a/Libraries/LibWeb/Animations/NamedTimelines.cpp
+++ b/Libraries/LibWeb/Animations/NamedTimelines.cpp
@@ -110,7 +110,7 @@ void NamedTimelines::refresh_declarations()
.view_inset = view_timeline_inset_from_ffi(declaration->view_inset),
});
});
- auto scope = CSS::ComputedValuesFFI::rust_animation_values_timeline_scope(&values, &declaring_element, [](void* context, void const* name) {
+ auto scope = CSS::ComputedValuesFFI::rust_animation_values_timeline_scope(&values, &declaring_element, [](void* context, CSS::StyleValueFFI::CssString const* name) {
static_cast<DeclaringElement*>(context)->scoped_names.append(CSS::css_string_from_rust(name));
});
declaring_element.scopes_all_names = scope == CSS::ComputedValuesFFI::FfiTimelineScope::All;
diff --git a/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp
index 320e567af41..cc074e33a7c 100644
--- a/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp
+++ b/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp
@@ -76,9 +76,9 @@ static_assert(offsetof(Web::CSS::ComputedValuesFFI::RetainedUtf16FlyString, raw)
namespace Web::CSS {
-Utf16FlyString css_string_from_rust(void const* string)
+Utf16FlyString css_string_from_rust(StyleValueFFI::CssString const* string)
{
- auto view = StyleValueFFI::rust_css_string_view(static_cast<StyleValueFFI::CssString const*>(string));
+ auto view = StyleValueFFI::rust_css_string_view(string);
return Utf16FlyString::from_utf16(Utf16View { reinterpret_cast<char16_t const*>(view.data), view.length });
}
diff --git a/Libraries/LibWeb/CSS/StyleValues/StyleValue.h b/Libraries/LibWeb/CSS/StyleValues/StyleValue.h
index 6d04ed61d9b..3e76f832631 100644
--- a/Libraries/LibWeb/CSS/StyleValues/StyleValue.h
+++ b/Libraries/LibWeb/CSS/StyleValues/StyleValue.h
@@ -34,7 +34,7 @@
namespace Web::CSS {
-WEB_API Utf16FlyString css_string_from_rust(void const*);
+WEB_API Utf16FlyString css_string_from_rust(StyleValueFFI::CssString const*);
#define ENUMERATE_CSS_STYLE_VALUE_TYPES \
__ENUMERATE_CSS_STYLE_VALUE_TYPE(Anchor, anchor, StyleValue) \
diff --git a/Libraries/LibWeb/Rust/build.rs b/Libraries/LibWeb/Rust/build.rs
index 72df65e1e85..b27cb5b14ab 100644
--- a/Libraries/LibWeb/Rust/build.rs
+++ b/Libraries/LibWeb/Rust/build.rs
@@ -2812,6 +2812,15 @@ fn main() -> Result<(), Box<dyn Error>> {
.after_includes
.get_or_insert_with(String::new)
.push_str("\nnamespace Web::CSS::StyleValueFFI { union StyleValueData; }");
+ // So do the names it hands out as CSS strings.
+ computed_values_config.export.rename.insert(
+ "CssString".to_string(),
+ "Web::CSS::StyleValueFFI::CssString".to_string(),
+ );
+ computed_values_config
+ .after_includes
+ .get_or_insert_with(String::new)
+ .push_str("\nnamespace Web::CSS::StyleValueFFI { struct CssString; }");
// 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/css_string.rs b/Libraries/LibWeb/Rust/src/css/css_string.rs
index c0ec00f6372..1020e3a65de 100644
--- a/Libraries/LibWeb/Rust/src/css/css_string.rs
+++ b/Libraries/LibWeb/Rust/src/css/css_string.rs
@@ -60,12 +60,8 @@ impl CssString {
Self { raw: 0 }
}
- pub(crate) fn as_ptr(&self) -> *const std::ffi::c_void {
- if self.raw == 0 {
- std::ptr::null()
- } else {
- (self as *const Self).cast()
- }
+ pub(crate) fn as_ptr(&self) -> *const Self {
+ if self.raw == 0 { std::ptr::null() } else { self }
}
pub(crate) fn units(&self) -> &[u16] {
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 40ab40c7412..5002a28711a 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -15,6 +15,7 @@
//! Element-bound inputs, such as container sizes and tree positions, are
//! snapshotted by C++ before entering the Rust computation drive.
+use crate::css::css_string::CssString;
use crate::css::ffi_support::OwnedBox;
use std::borrow::Cow;
use std::ffi::c_void;
@@ -2889,14 +2890,14 @@ pub struct FfiComputedAnimation {
pub delay: f64,
pub fill_mode: u8,
pub composition: u8,
- pub name: *const c_void,
+ pub name: *const CssString,
pub timeline_kind: FfiAnimationTimelineKind,
pub scroll_scroller: u8,
pub scroll_axis: u8,
/// The insets of a view() timeline.
pub view_inset: FfiViewTimelineInset,
/// The name of a named timeline, as a CSS string, or null.
- pub timeline_name: *const c_void,
+ pub timeline_name: *const CssString,
/// The edges of the animation attachment range, each a `TimelineRangeName` in the host's numbering and an offset
/// from the start of that range: a length plus a percentage of the range.
pub range_start_name: u8,
@@ -5004,7 +5005,7 @@ pub extern "C" fn rust_view_timeline_inset(value: &StyleValueData) -> FfiViewTim
#[repr(C)]
pub struct FfiTimelineDeclaration {
/// The name, as a CSS string.
- pub name: *const c_void,
+ pub name: *const CssString,
pub is_view: bool,
pub axis: u8,
pub view_inset: FfiViewTimelineInset,
@@ -5075,7 +5076,7 @@ pub unsafe extern "C" fn rust_animation_values_timeline_declarations(
pub unsafe extern "C" fn rust_animation_values_timeline_scope(
values: &crate::css::computed_value_types::AnimationValues,
context: *mut c_void,
- scope: unsafe extern "C" fn(*mut c_void, *const c_void),
+ scope: unsafe extern "C" fn(*mut c_void, *const CssString),
) -> FfiTimelineScope {
// SAFETY: A computed style group's handles point at retained style values it owns.
let Some(value) = (unsafe { values.timeline_scope.pointer.cast::<StyleValueData>().as_ref() }) else {
@@ -5181,7 +5182,7 @@ struct AnimationTimelineDescriptor {
scroller: u8,
axis: u8,
view_inset: FfiViewTimelineInset,
- name: *const c_void,
+ name: *const CssString,
}
fn animation_timeline_descriptor(value: &StyleValueData) -> AnimationTimelineDescriptor {
diff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rs
index 2488c29faa2..330c11c00a1 100644
--- a/Libraries/LibWeb/Rust/src/css/style_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs
@@ -2251,7 +2251,7 @@ const _: () = assert!(size_of::<StyleValueData>() <= 64);
pub struct FfiComputedFontFamilyEntry {
pub kind: u8,
pub keyword: u16,
- pub string: *const c_void,
+ pub string: *const CssString,
}
pub const COMPUTED_FONT_FAMILY_GENERIC: u8 = 0;