Skip to content

Truly independent web browser

LibWeb: Take more pointer parameters as references

Andreas Kling31f2ae26307f
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs
index f285612aefe..7d06f3ef3f2 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs
@@ -429,26 +429,18 @@ pub unsafe extern "C" fn rust_html_tokenizer_append_ascii_input(
}
/// Get the tokenizer input that has not been consumed yet.
-///
-/// # Safety
-/// `handle`, `out_ptr`, and `out_len` must be valid pointers.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_html_tokenizer_unparsed_input(
+pub extern "C" fn rust_html_tokenizer_unparsed_input(
handle: Option<&mut RustFfiTokenizerHandle>,
- out_ptr: *mut *const u16,
- out_len: *mut usize,
+ out_ptr: Option<&mut *const u16>,
+ out_len: Option<&mut usize>,
) {
- let Some(handle) = handle else {
+ let (Some(handle), Some(out_ptr), Some(out_len)) = (handle, out_ptr, out_len) else {
return;
};
- if out_ptr.is_null() || out_len.is_null() {
- return;
- }
handle.last_unparsed_input = handle.tokenizer.unparsed_input().encode_utf16().collect();
- unsafe {
- *out_ptr = handle.last_unparsed_input.as_ptr();
- *out_len = handle.last_unparsed_input.len();
- }
+ *out_ptr = handle.last_unparsed_input.as_ptr();
+ *out_len = handle.last_unparsed_input.len();
}
/// Compact already-tokenized input after the parser has consumed a chunk.
diff --git a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
index 5fc05a4ba99..344c92aa58a 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
@@ -2911,20 +2911,19 @@ fn parse_sizes_attribute(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_parse_sizes_attribute(
source: FfiUtf16View,
- context: *const ParseContext,
- media_environment: *const FfiMediaEnvironment,
- img_auto_width: *const f64,
+ context: Option<&ParseContext>,
+ media_environment: Option<&FfiMediaEnvironment>,
+ img_auto_width: Option<&f64>,
) -> *const c_void {
crate::css::ffi_stats::bump(crate::css::ffi_stats::FfiOp::SizesAttributeParseEntry);
let Some(source) = source.units() else {
return std::ptr::null();
};
- let Some(context) = (unsafe { context.as_ref() }) else {
+ let Some(context) = context else {
return std::ptr::null();
};
- let media_environment =
- unsafe { media_environment.as_ref() }.and_then(|environment| unsafe { ffi_media_environment(environment) });
- let img_auto_width = unsafe { img_auto_width.as_ref() }.copied();
+ let media_environment = media_environment.and_then(|environment| unsafe { ffi_media_environment(environment) });
+ let img_auto_width = img_auto_width.copied();
Arc::into_raw(Arc::new(parse_sizes_attribute(
context,
source,
@@ -2954,12 +2953,12 @@ fn create_expression_handle(expression: Expression, kind: QueryKind) -> *const F
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_parse_supports_condition(
source: FfiUtf16View,
- context: *const ParseContext,
+ context: Option<&ParseContext>,
) -> *const FfiQueryHandle {
let Some(source) = source.units() else {
return std::ptr::null();
};
- let Some(context) = (unsafe { context.as_ref() }) else {
+ let Some(context) = context else {
return std::ptr::null();
};
let declared_namespaces = unsafe { declared_namespaces_from_context(context) };
@@ -3038,11 +3037,11 @@ pub(crate) unsafe fn evaluate_container_query(
/// retain a copy of the borrowed UTF-16 slice.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn css_query_serialize_condition(
- handle: *const FfiQueryHandle,
+ handle: Option<&FfiQueryHandle>,
context: *mut c_void,
visit: VisitQuerySerialization,
) -> bool {
- let Some(handle) = (unsafe { handle.as_ref() }) else {
+ let Some(handle) = handle else {
return false;
};
let QueryTree::Expression { expression, kind } = &handle.tree else {
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs
index b45f8477a4d..ff64e16b6cd 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs
@@ -483,11 +483,10 @@ fn worker_selector_normalization_survives_publication() {
use crate::css::selector_parser::*;
use crate::css::selector_serialization::serialize_selector_without_namespaces;
fn text(parsed: &RustParsedSelectorList) -> Vec<u16> {
- unsafe {
- assert_eq!(rust_parsed_selector_list_length(parsed), 1);
- let selector = Box::from_raw(rust_parsed_selector_list_selector(parsed, 0));
- serialize_selector_without_namespaces(&selector)
- }
+ assert_eq!(rust_parsed_selector_list_length(parsed), 1);
+ // SAFETY: The selector is a new one, which the test owns.
+ let selector = unsafe { Box::from_raw(rust_parsed_selector_list_selector(parsed, 0)) };
+ serialize_selector_without_namespaces(&selector)
}
fn visit(rule: &super::ParsedRule, selectors: &mut Vec<Vec<u16>>, scopes: &mut Vec<Vec<u16>>) {
for child in &rule.children {
@@ -806,25 +805,21 @@ fn independent_workers_read_the_same_immutable_graph() {
));
assert_eq!(serialize_style_value_to_utf16(&expected_value), utf16("13px"));
assert_eq!(sheet.rules.len(), 4);
- unsafe {
- assert_eq!(
- rust_parsed_selector_list_length(Arc::as_ptr(sheet.rules[0].selector_list.as_ref().unwrap())),
- 1
- );
- assert_eq!(
- css_query_evaluate_supports(sheet.rules[1].supports_condition.as_deref()),
- 1
- );
- }
+ assert_eq!(
+ rust_parsed_selector_list_length(sheet.rules[0].selector_list.as_ref().unwrap()),
+ 1
+ );
+ assert_eq!(
+ css_query_evaluate_supports(sheet.rules[1].supports_condition.as_deref()),
+ 1
+ );
let ParsedRuleChild::Rule(nested) = &sheet.rules[1].children[0] else {
panic!()
};
- unsafe {
- assert_eq!(
- rust_parsed_selector_list_length(Arc::as_ptr(nested.selector_list.as_ref().unwrap())),
- 1
- )
- };
+ assert_eq!(
+ rust_parsed_selector_list_length(nested.selector_list.as_ref().unwrap()),
+ 1
+ );
assert_eq!(
*sheet.rules[2].property_rule.as_ref().unwrap().syntax,
SyntaxNode::Type(SyntaxType::Length)
@@ -862,17 +857,15 @@ fn scope_presence_and_native_selector_names_survive_worker_exit() {
(scope.start.as_ref().unwrap(), "親"),
(scope.end.as_ref().unwrap(), "限界"),
] {
- unsafe {
- assert_eq!(rust_parsed_selector_list_length(Arc::as_ptr(selectors)), 1);
- let selector = &selectors.selectors()[0].compound_selectors[0].simple_selectors[0];
- let crate::css::selector::SimpleSelector::Class(name) = selector else {
- panic!("expected class selector")
- };
- assert_eq!(
- name.name,
- crate::css::retained_fly_string::RetainedUtf16FlyString::from_utf16(&utf16(expected))
- );
- }
+ assert_eq!(rust_parsed_selector_list_length(selectors), 1);
+ let selector = &selectors.selectors()[0].compound_selectors[0].simple_selectors[0];
+ let crate::css::selector::SimpleSelector::Class(name) = selector else {
+ panic!("expected class selector")
+ };
+ assert_eq!(
+ name.name,
+ crate::css::retained_fly_string::RetainedUtf16FlyString::from_utf16(&utf16(expected))
+ );
}
}
}
@@ -928,7 +921,7 @@ fn container_conditions_own_names_and_queries_after_the_sheet_is_dropped() {
assert!(entries[2].name.is_none());
assert!(entries[0].query.is_some());
unsafe {
- let query = Arc::as_ptr(entries[2].query.as_ref().unwrap());
+ let query = entries[2].query.as_deref();
let mut text = Vec::<u16>::new();
assert!(css_query_serialize_condition(
query,
diff --git a/Libraries/LibWeb/Rust/src/css/selector_parser.rs b/Libraries/LibWeb/Rust/src/css/selector_parser.rs
index 2b4a730e43a..4a446e4b905 100644
--- a/Libraries/LibWeb/Rust/src/css/selector_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/selector_parser.rs
@@ -1368,25 +1368,17 @@ pub unsafe extern "C" fn rust_parsed_selector_list_destroy(list: *mut RustParsed
}
}
-/// # Safety
-/// `list` must point to a live parsed selector list.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parsed_selector_list_length(list: *const RustParsedSelectorList) -> usize {
- unsafe { (&*list).selectors.len() }
+pub extern "C" fn rust_parsed_selector_list_length(list: &RustParsedSelectorList) -> usize {
+ list.selectors.len()
}
-/// # Safety
-/// `list` must point to a live selector list and `index` must be in bounds.
+/// The selector at `index`, which must be in bounds, as a new selector that C++ owns.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_parsed_selector_list_selector(
- list: *const RustParsedSelectorList,
- index: usize,
-) -> *mut RustSelector {
- unsafe {
- Box::into_raw(Box::new(RustSelector {
- selector: (&(*list).selectors)[index].clone(),
- }))
- }
+pub extern "C" fn rust_parsed_selector_list_selector(list: &RustParsedSelectorList, index: usize) -> *mut RustSelector {
+ Box::into_raw(Box::new(RustSelector {
+ selector: list.selectors[index].clone(),
+ }))
}
#[cfg(test)]