Skip to content

Truly independent web browser

LibWeb: Read SVG parser input as FfiUtf16View, and type parsed paths

Andreas Klinga14a2a4f7367
diff --git a/Libraries/LibWeb/Rust/build.rs b/Libraries/LibWeb/Rust/build.rs
index d5145128613..be6b9c33a97 100644
--- a/Libraries/LibWeb/Rust/build.rs
+++ b/Libraries/LibWeb/Rust/build.rs
@@ -2391,7 +2391,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut svg_config = base_config.clone();
svg_config.namespaces = Some(vec!["Web".to_string(), "SVG".to_string(), "RustFFI".to_string()]);
svg_config.export.include = vec![
- "FfiSvgInput".to_string(),
+ "FfiUtf16View".to_string(),
"FfiSvgPoint".to_string(),
"FfiSvgTransform".to_string(),
"TRANSFORM_MATRIX".to_string(),
@@ -2401,10 +2401,17 @@ fn main() -> Result<(), Box<dyn Error>> {
"TRANSFORM_SKEW_Y".to_string(),
"TRANSFORM_TRANSLATE".to_string(),
];
+ // A parsed path is opaque to C++, which holds it by pointer.
+ svg_config
+ .export
+ .rename
+ .insert("ParsedPath".to_string(), "void".to_string());
+ svg_config.export.exclude.push("ParsedPath".to_string());
generate_ffi_header_strict(
svg_config,
&[
+ manifest_dir.join("src/css/ffi_support.rs"),
manifest_dir.join("src/svg/attribute_parser.rs"),
manifest_dir.join("src/svg/path_parser.rs"),
],
diff --git a/Libraries/LibWeb/Rust/src/svg/attribute_parser.rs b/Libraries/LibWeb/Rust/src/svg/attribute_parser.rs
index 9b87e945258..e7d0074987e 100644
--- a/Libraries/LibWeb/Rust/src/svg/attribute_parser.rs
+++ b/Libraries/LibWeb/Rust/src/svg/attribute_parser.rs
@@ -5,7 +5,7 @@
*/
use super::parser::{Input, Parser};
-use super::path_parser::FfiSvgInput;
+use crate::css::ffi_support::FfiUtf16View;
use std::ffi::c_void;
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -350,46 +350,38 @@ pub struct FfiSvgTransform {
pub values: [f32; 6],
}
-fn with_input<T>(input: FfiSvgInput, parse: impl FnOnce(Input<'_>) -> T) -> Option<T> {
- // SAFETY: FFI callers satisfy FfiSvgInput's pointer contract for the duration of the call.
- Some(parse(unsafe { input.input()? }))
+fn with_input<T>(input: FfiUtf16View<'_>, parse: impl FnOnce(Input<'_>) -> T) -> Option<T> {
+ Some(parse(super::path_parser::svg_input(input)?))
}
-/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and `value` must be writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parse_svg_integer(input: FfiSvgInput, value: *mut i32) -> bool {
+pub extern "C" fn rust_parse_svg_integer(input: FfiUtf16View<'_>, value: &mut i32) -> bool {
let Some(parsed_value) = with_input(input, parse_integer).flatten() else {
return false;
};
- unsafe { value.write(parsed_value) };
+ *value = parsed_value;
true
}
-/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and the output pointers must be writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parse_svg_number_percentage(
- input: FfiSvgInput,
- value: *mut f32,
- is_percentage: *mut bool,
+pub extern "C" fn rust_parse_svg_number_percentage(
+ input: FfiUtf16View<'_>,
+ value: &mut f32,
+ is_percentage: &mut bool,
) -> bool {
let Some(parsed_value) = with_input(input, parse_number_percentage).flatten() else {
return false;
};
- unsafe {
- value.write(parsed_value.value);
- is_percentage.write(parsed_value.is_percentage);
- }
+ *value = parsed_value.value;
+ *is_percentage = parsed_value.is_percentage;
true
}
/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and `context` must be valid for
-/// `set_points`.
+/// `context` must be valid for `set_points`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_parse_svg_points(
- input: FfiSvgInput,
+ input: FfiUtf16View<'_>,
context: *mut c_void,
set_points: unsafe extern "C" fn(*mut c_void, *const FfiSvgPoint, usize),
) {
@@ -405,11 +397,10 @@ pub unsafe extern "C" fn rust_parse_svg_points(
}
/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and `context` must be valid for
-/// `set_transforms`.
+/// `context` must be valid for `set_transforms`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_parse_svg_transform(
- input: FfiSvgInput,
+ input: FfiUtf16View<'_>,
context: *mut c_void,
set_transforms: unsafe extern "C" fn(*mut c_void, *const FfiSvgTransform, usize),
) -> bool {
@@ -427,52 +418,43 @@ pub unsafe extern "C" fn rust_parse_svg_transform(
true
}
-/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and the output pointers must be writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parse_svg_preserve_aspect_ratio(
- input: FfiSvgInput,
- align: *mut u8,
- meet_or_slice: *mut u8,
+pub extern "C" fn rust_parse_svg_preserve_aspect_ratio(
+ input: FfiUtf16View<'_>,
+ align: &mut u8,
+ meet_or_slice: &mut u8,
) -> bool {
let Some((parsed_align, parsed_meet_or_slice)) = with_input(input, parse_preserve_aspect_ratio).flatten() else {
return false;
};
- unsafe {
- align.write(parsed_align);
- meet_or_slice.write(parsed_meet_or_slice);
- }
+ *align = parsed_align;
+ *meet_or_slice = parsed_meet_or_slice;
true
}
-/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and `value` must be writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parse_svg_units(input: FfiSvgInput, value: *mut u8) -> bool {
+pub extern "C" fn rust_parse_svg_units(input: FfiUtf16View<'_>, value: &mut u8) -> bool {
let Some(parsed_value) = with_input(input, parse_units).flatten() else {
return false;
};
- unsafe { value.write(parsed_value) };
+ *value = parsed_value;
true
}
-/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and `value` must be writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parse_svg_spread_method(input: FfiSvgInput, value: *mut u8) -> bool {
+pub extern "C" fn rust_parse_svg_spread_method(input: FfiUtf16View<'_>, value: &mut u8) -> bool {
let Some(parsed_value) = with_input(input, parse_spread_method).flatten() else {
return false;
};
- unsafe { value.write(parsed_value) };
+ *value = parsed_value;
true
}
/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and `context` must be valid for
-/// `set_values`.
+/// `context` must be valid for `set_values`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_parse_svg_table_values(
- input: FfiSvgInput,
+ input: FfiUtf16View<'_>,
context: *mut c_void,
set_values: unsafe extern "C" fn(*mut c_void, *const f32, usize),
) {
@@ -480,36 +462,30 @@ pub unsafe extern "C" fn rust_parse_svg_table_values(
unsafe { set_values(context, values.as_ptr(), values.len()) };
}
-/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and the output pointers must be writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parse_svg_view_box(
- input: FfiSvgInput,
- min_x: *mut f64,
- min_y: *mut f64,
- width: *mut f64,
- height: *mut f64,
+pub extern "C" fn rust_parse_svg_view_box(
+ input: FfiUtf16View<'_>,
+ min_x: &mut f64,
+ min_y: &mut f64,
+ width: &mut f64,
+ height: &mut f64,
) -> bool {
let Some(values) = with_input(input, parse_view_box).flatten() else {
return false;
};
- unsafe {
- min_x.write(values[0]);
- min_y.write(values[1]);
- width.write(values[2]);
- height.write(values[3]);
- }
+ *min_x = values[0];
+ *min_y = values[1];
+ *width = values[2];
+ *height = values[3];
true
}
-/// # Safety
-/// `input` must satisfy [`FfiSvgInput::input`]'s requirements and `value` must be writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parse_svg_clock_value_in_milliseconds(input: FfiSvgInput, value: *mut f64) -> bool {
+pub extern "C" fn rust_parse_svg_clock_value_in_milliseconds(input: FfiUtf16View<'_>, value: &mut f64) -> bool {
let Some(parsed_value) = with_input(input, parse_clock_value_in_milliseconds).flatten() else {
return false;
};
- unsafe { value.write(parsed_value) };
+ *value = parsed_value;
true
}
diff --git a/Libraries/LibWeb/Rust/src/svg/path_parser.rs b/Libraries/LibWeb/Rust/src/svg/path_parser.rs
index f242f4c86d4..0a2480836c7 100644
--- a/Libraries/LibWeb/Rust/src/svg/path_parser.rs
+++ b/Libraries/LibWeb/Rust/src/svg/path_parser.rs
@@ -5,6 +5,8 @@
*/
use super::parser::{Input, Parser as Lexer};
+use crate::css::css_tokenizer::TokenizerInput;
+use crate::css::ffi_support::FfiUtf16View;
use libgfx_rust::path::{OwnedPath, PathBuilder};
use std::ffi::c_void;
use std::fmt::Write;
@@ -124,7 +126,7 @@ enum PathInstruction {
}
#[derive(Clone, Debug, Default, PartialEq)]
-pub(crate) struct ParsedPath {
+pub struct ParsedPath {
instructions: Vec<PathInstruction>,
}
@@ -845,86 +847,59 @@ impl<'a> Parser<'a> {
}
}
-#[derive(Clone, Copy)]
-#[repr(C)]
-pub struct FfiSvgInput {
- pub ascii: *const u8,
- pub utf16: *const u16,
- pub length: usize,
+/// The units of `view`, or none for a view that names both or neither kind of storage.
+pub(super) fn svg_input(view: FfiUtf16View<'_>) -> Option<Input<'_>> {
+ Some(match view.units()? {
+ TokenizerInput::Ascii(units) => Input::Ascii(units),
+ TokenizerInput::Utf16(units) => Input::Utf16(units),
+ })
}
-impl FfiSvgInput {
- /// # Safety
- /// For non-empty input, exactly one pointer must identify `length` readable units.
- pub(super) unsafe fn input<'a>(self) -> Option<Input<'a>> {
- if self.length == 0 {
- return Some(Input::Ascii(&[]));
- }
- match (self.ascii.is_null(), self.utf16.is_null()) {
- (false, true) => Some(Input::Ascii(unsafe {
- ak::slice_from_raw_parts(self.ascii, self.length)
- })),
- (true, false) => Some(Input::Utf16(unsafe {
- ak::slice_from_raw_parts(self.utf16, self.length)
- })),
- _ => None,
- }
- }
-}
-
-/// # Safety
-/// - `input` must satisfy [`FfiSvgInput::input`]'s requirements.
+/// The path `input` describes, as a new path C++ owns, or null where it describes none.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parse_svg_path_data(input: FfiSvgInput) -> *mut c_void {
- let Some(input) = (unsafe { input.input() }) else {
+pub extern "C" fn rust_parse_svg_path_data(input: FfiUtf16View<'_>) -> *mut ParsedPath {
+ let Some(input) = svg_input(input) else {
return std::ptr::null_mut();
};
let Some(instructions) = Parser::new(input).parse(true) else {
return std::ptr::null_mut();
};
- Box::into_raw(Box::new(ParsedPath { instructions })).cast()
+ Box::into_raw(Box::new(ParsedPath { instructions }))
}
-/// # Safety
-/// `path` must point to a live `ParsedPath`.
+/// A copy of `path`, as a new path C++ owns.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_svg_path_clone(path: *const c_void) -> *mut c_void {
- let path = unsafe { &*path.cast::<ParsedPath>() };
- Box::into_raw(Box::new(path.clone())).cast()
+pub extern "C" fn rust_svg_path_clone(path: &ParsedPath) -> *mut ParsedPath {
+ Box::into_raw(Box::new(path.clone()))
}
/// # Safety
/// `path` must be a `ParsedPath` allocation returned by this module that has not yet been destroyed.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_svg_path_destroy(path: *mut c_void) {
- drop(unsafe { Box::from_raw(path.cast::<ParsedPath>()) });
+pub unsafe extern "C" fn rust_svg_path_destroy(path: *mut ParsedPath) {
+ drop(unsafe { Box::from_raw(path) });
}
-/// # Safety
-/// `a` and `b` must point to live `ParsedPath` values.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_svg_path_equals(a: *const c_void, b: *const c_void) -> bool {
- unsafe { *a.cast::<ParsedPath>() == *b.cast::<ParsedPath>() }
+pub extern "C" fn rust_svg_path_equals(a: &ParsedPath, b: &ParsedPath) -> bool {
+ a == b
}
/// # Safety
-/// - `path` must point to a live `ParsedPath`.
-/// - `context` must be valid for `append` for the duration of this call.
+/// `context` must be valid for `append` for the duration of this call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_svg_path_serialize(
- path: *const c_void,
+ path: &ParsedPath,
context: *mut c_void,
append: unsafe extern "C" fn(*mut c_void, *const u8, usize),
) {
- let serialized = unsafe { &*path.cast::<ParsedPath>() }.serialize();
+ let serialized = path.serialize();
unsafe { append(context, serialized.as_ptr(), serialized.len()) };
}
-/// # Safety
-/// `path` must point to a live `ParsedPath`.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_svg_path_to_gfx_path(path: *const c_void) -> *mut c_void {
- unsafe { &*path.cast::<ParsedPath>() }.to_gfx_path().into_raw()
+pub extern "C" fn rust_svg_path_to_gfx_path(path: &ParsedPath) -> *mut c_void {
+ path.to_gfx_path().into_raw()
}
#[cfg(test)]
diff --git a/Libraries/LibWeb/SVG/AttributeParsing.cpp b/Libraries/LibWeb/SVG/AttributeParsing.cpp
index ef4a1f2c90b..18b132e7c6f 100644
--- a/Libraries/LibWeb/SVG/AttributeParsing.cpp
+++ b/Libraries/LibWeb/SVG/AttributeParsing.cpp
@@ -14,7 +14,7 @@
namespace Web::SVG {
-static RustFFI::FfiSvgInput ffi_svg_input(Utf16View input)
+static RustFFI::FfiUtf16View ffi_svg_input(Utf16View input)
{
return {
.ascii = input.has_ascii_storage() ? reinterpret_cast<u8 const*>(input.ascii_span().data()) : nullptr,
diff --git a/Libraries/LibWeb/SVG/Path.cpp b/Libraries/LibWeb/SVG/Path.cpp
index 4ad57ec1ea9..bb4170682a0 100644
--- a/Libraries/LibWeb/SVG/Path.cpp
+++ b/Libraries/LibWeb/SVG/Path.cpp
@@ -14,7 +14,7 @@
namespace Web::SVG {
-static RustFFI::FfiSvgInput ffi_svg_input(Utf16View input)
+static RustFFI::FfiUtf16View ffi_svg_input(Utf16View input)
{
return {
.ascii = input.has_ascii_storage() ? reinterpret_cast<u8 const*>(input.ascii_span().data()) : nullptr,