Skip to content

Truly independent web browser

LibWeb: Borrow what a ParseContext names through typed fields

Andreas Kling409d7e447efd
diff --git a/Libraries/LibWeb/CSS/Parser/RustValueParsing.cpp b/Libraries/LibWeb/CSS/Parser/RustValueParsing.cpp
index b7a9b0fc649..cb1c2968dc9 100644
--- a/Libraries/LibWeb/CSS/Parser/RustValueParsing.cpp
+++ b/Libraries/LibWeb/CSS/Parser/RustValueParsing.cpp
@@ -35,8 +35,7 @@ Parser::ParseContextStorage::ParseContextStorage(Parser& parser, ParseContextMod
}
if (direct_property_context.has_value())
value_contexts.append({ .kind = ValueParserFFI::FfiValueParsingContextKind::Property, .value = to_underlying(*direct_property_context), .secondary_value = 0, .name = {} });
- context.value_contexts = value_contexts.data();
- context.value_context_count = value_contexts.size();
+ context.value_contexts = value_contexts.span();
}
if (parser.m_document) {
@@ -56,12 +55,9 @@ Parser::ParseContextStorage::ParseContextStorage(Parser& parser, ParseContextMod
.contains_attr_tainted_values = false,
.is_ua_style_sheet = mode == ParseContextMode::Syntax && parser.m_is_ua_style_sheet == IsUAStyleSheet::Yes,
.value_contexts = context.value_contexts,
- .value_context_count = context.value_context_count,
.declared_namespaces = mode == ParseContextMode::Syntax ? parser.m_declared_namespaces.handle() : nullptr,
- .document_url = document_url.bytes().data(),
- .document_url_length = document_url.bytes().size(),
- .document_base_url = document_base_url.bytes().data(),
- .document_base_url_length = document_base_url.bytes().size(),
+ .document_url = document_url.bytes(),
+ .document_base_url = document_base_url.bytes(),
.length_resolution_context = length_resolution_context.has_value() ? &*length_resolution_context : nullptr,
.random_function_index = &parser.m_random_function_index,
};
diff --git a/Libraries/LibWeb/Rust/build.rs b/Libraries/LibWeb/Rust/build.rs
index 26515de3254..9cce7b54447 100644
--- a/Libraries/LibWeb/Rust/build.rs
+++ b/Libraries/LibWeb/Rust/build.rs
@@ -2522,6 +2522,15 @@ fn main() -> Result<(), Box<dyn Error>> {
];
// A tree scope's counter styles are queued on the document host, which the layout header declares.
declare_document_host_of_layout_header(&mut value_parser_config);
+ // A parse context names the lengths it resolves against as the computed values header declares them.
+ value_parser_config.export.rename.insert(
+ "FfiLengthResolutionContext".to_string(),
+ "Web::CSS::ComputedValuesFFI::FfiLengthResolutionContext".to_string(),
+ );
+ value_parser_config
+ .after_includes
+ .get_or_insert_with(String::new)
+ .push_str("\nnamespace Web::CSS::ComputedValuesFFI { struct FfiLengthResolutionContext; }");
generate_ffi_header(
value_parser_config,
diff --git a/Libraries/LibWeb/Rust/cbindgen.toml b/Libraries/LibWeb/Rust/cbindgen.toml
index 7890cd522b1..5543488de86 100644
--- a/Libraries/LibWeb/Rust/cbindgen.toml
+++ b/Libraries/LibWeb/Rust/cbindgen.toml
@@ -10,6 +10,7 @@ line_length = 120
tab_width = 4
no_includes = true
sys_includes = ["stdint.h", "stddef.h"]
+includes = ["AK/Span.h"]
usize_is_size_t = true
[parse]
@@ -26,6 +27,8 @@ exclude = ["SyntaxNode"]
rename_types = "PascalCase"
[export.rename]
+# An ak::Span is laid out as an AK ReadonlySpan, which C++ passes it as.
+"Span" = "AK::ReadonlySpan"
# A style engine handle is opaque to C++.
"StyleEngineHandle" = "const void*"
# So is a parsed syntax, which C++ holds by pointer.
diff --git a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
index bf43739aea4..261feeb6363 100644
--- a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
@@ -358,8 +358,7 @@ pub(crate) fn parse_substituted_value(
let mut context = *base_context;
context.is_substituted_value = true;
context.contains_attr_tainted_values = contains_attr_tainted_values;
- context.value_contexts = &raw const value_context;
- context.value_context_count = 1;
+ context.value_contexts = ak::Span::new(std::slice::from_ref(&value_context));
context.random_function_index = &raw mut random_function_index;
match parse_css_value_from_source(&context, property_id, source) {
ParseOutcome::Parsed(value) => RetainedStyleValueData::from_arc(value),
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index dc874fa5f99..c71a23c2dcd 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -312,21 +312,18 @@ impl CustomPropertyRegistry {
)
}
- pub(crate) fn parse_context(&self, random_function_index: &mut usize) -> ParseContext {
+ pub(crate) fn parse_context(&self, random_function_index: &mut usize) -> ParseContext<'_> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: self.document_url.as_ptr(),
- document_url_length: self.document_url.len(),
- document_base_url: self.document_base_url.as_ptr(),
- document_base_url_length: self.document_base_url.len(),
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::new(&self.document_url),
+ document_base_url: ak::Span::new(&self.document_base_url),
+ length_resolution_context: None,
random_function_index,
}
}
@@ -385,8 +382,7 @@ impl CustomPropertyRegistry {
name: Default::default(),
};
let mut context = self.parse_context(&mut random_function_index);
- context.value_contexts = &raw const value_context;
- context.value_context_count = 1;
+ context.value_contexts = ak::Span::new(std::slice::from_ref(&value_context));
parse_with_syntax(&context, source, &registration.syntax)
}
@@ -928,7 +924,7 @@ struct ASFResolutionContext<'a> {
/// Whether an attr-tainted value was substituted into a URL, or substituted one.
uses_attr_tainted_url: bool,
custom_functions: Option<&'a CustomFunctionRegistry>,
- parse_context: Option<&'a ParseContext>,
+ parse_context: Option<&'a ParseContext<'a>>,
media_environment: Option<&'a FfiMediaEnvironment<'a>>,
/// What lengths in a style query resolve against: the element's last computed style.
style_query_length_resolution_context: Option<&'a crate::css::style_compute::FfiLengthResolutionContext>,
@@ -2514,7 +2510,7 @@ fn evaluate_if_condition(
return ConditionEvaluation::NotHandled;
};
let source = serialize_tokens(&test[1..close]);
- return match unsafe { parse_and_evaluate_supports_if_condition(&source, parse_context) } {
+ return match parse_and_evaluate_supports_if_condition(&source, parse_context) {
Some(MatchResult::True) => ConditionEvaluation::Match(true),
Some(MatchResult::False | MatchResult::Unknown) => ConditionEvaluation::Match(false),
None => ConditionEvaluation::Invalid,
@@ -3355,7 +3351,7 @@ pub(crate) struct VarResolutionInputs<'a> {
pub(crate) store: Option<&'a CustomPropertyStore>,
pub(crate) inheritance_store: Option<&'a CustomPropertyStore>,
pub(crate) registry: Option<&'a CustomPropertyRegistry>,
- pub(crate) parse_context: Option<&'a ParseContext>,
+ pub(crate) parse_context: Option<&'a ParseContext<'a>>,
pub(crate) media_environment: Option<&'a FfiMediaEnvironment<'a>>,
pub(crate) attribute_names_are_ascii_case_insensitive: bool,
pub(crate) style_query_inputs: Option<&'a crate::css::cascaded_properties::FfiStyleQueryInputs>,
diff --git a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs
index 2ad013363f5..1448a59110b 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs
@@ -44,7 +44,7 @@ pub(crate) struct CalcParserContext {
pub random_function_index: *mut usize,
pub allowed_color_channels: u64,
pub allow_random_functions: bool,
- pub parse_context: *const ParseContext,
+ pub parse_context: *const ParseContext<'static>,
}
impl CalcParserContext {
diff --git a/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs
index ae46711f7b3..09fa0f155c4 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs
@@ -374,7 +374,7 @@ fn parse_calculated_numeric(
random_function_index: context.random_function_index,
allowed_color_channels,
allow_random_functions: context_allows_random_functions(context),
- parse_context: context,
+ parse_context: std::ptr::from_ref(context).cast(),
},
) {
Ok(root) => root,
@@ -1084,21 +1084,18 @@ mod tests {
use crate::css::css_tokenizer::tokenize_for_parser;
use crate::css::parser::component_value::consume_a_list_of_component_values;
- fn context() -> ParseContext {
+ fn context() -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs
index 04e20a80dc1..ab82f094bd5 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs
@@ -111,12 +111,7 @@ fn parse_integer_component(
if matches!(parsed, StyleValueData::Calculated { .. }) {
super::stylesheet_cache::record_length_resolution_dependency();
}
- let length_context = unsafe {
- context
- .length_resolution_context
- .cast::<FfiLengthResolutionContext>()
- .as_ref()
- };
+ let length_context = context.length_resolution_context;
Some((resolve_descriptor_integer(&parsed, length_context)?, parsed))
}
@@ -473,13 +468,7 @@ pub(crate) fn parse_descriptor(
source: &[u16],
) -> Option<ParsedDescriptor> {
let metadata = descriptor_metadata(at_rule, name)?;
- let inherited_contexts = if context.value_context_count == 0 {
- &[]
- } else if context.value_contexts.is_null() {
- return None;
- } else {
- unsafe { ak::slice_from_raw_parts(context.value_contexts, context.value_context_count) }
- };
+ let inherited_contexts = context.value_contexts.as_slice();
let mut value_contexts = Vec::with_capacity(inherited_contexts.len() + 1);
value_contexts.extend_from_slice(inherited_contexts);
value_contexts.push(FfiValueParsingContext {
@@ -489,8 +478,7 @@ pub(crate) fn parse_descriptor(
name: FfiUtf16View::default(),
});
let mut descriptor_context = *context;
- descriptor_context.value_contexts = value_contexts.as_ptr();
- descriptor_context.value_context_count = value_contexts.len();
+ descriptor_context.value_contexts = ak::Span::new(&value_contexts);
let mut presence = SubstitutionFunctionsPresence::default();
collect_arbitrary_substitution_function_presence(values, &mut presence).ok()?;
let value = if presence.has_any() {
@@ -546,21 +534,18 @@ mod tests {
use crate::css::style_value::StyleValueData;
use std::sync::Arc;
- fn context() -> ParseContext {
+ fn context() -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/fonts_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/fonts_parser.rs
index ad729a3d011..b294476a172 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/fonts_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/fonts_parser.rs
@@ -686,21 +686,18 @@ mod tests {
use crate::css::parser::value_parser::{ParseContext, ParseOutcome};
use crate::css::property_metadata::property_id;
- fn context() -> ParseContext {
+ fn context() -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/grid_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/grid_parser.rs
index 3d1a69eb1c2..5c721c1db9d 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/grid_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/grid_parser.rs
@@ -1442,21 +1442,18 @@ mod tests {
use crate::css::css_tokenizer::tokenize_for_parser;
use crate::css::parser::component_value::consume_a_list_of_component_values;
- fn context() -> ParseContext {
+ fn context() -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/images_gradients_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/images_gradients_parser.rs
index cc8416f2181..1ca2b06c8a2 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/images_gradients_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/images_gradients_parser.rs
@@ -722,21 +722,18 @@ mod tests {
use crate::css::css_tokenizer::tokenize_for_parser;
use crate::css::parser::component_value::consume_a_list_of_component_values;
- fn context() -> ParseContext {
+ fn context() -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/positions_shapes_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/positions_shapes_parser.rs
index 880ee0a8da2..bc889d6e671 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/positions_shapes_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/positions_shapes_parser.rs
@@ -1624,21 +1624,18 @@ mod tests {
use crate::css::css_tokenizer::tokenize_for_parser;
use crate::css::parser::component_value::consume_a_list_of_component_values;
- fn context() -> ParseContext {
+ fn context() -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
index 9d665e0d53f..020b850ebf8 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
@@ -1992,21 +1992,18 @@ enum ResolvedFeatureValue {
Unknown,
}
-fn media_value_parse_context(value_context: &FfiValueParsingContext<'static>) -> ParseContext {
+fn media_value_parse_context<'a>(value_context: &'a FfiValueParsingContext<'a>) -> ParseContext<'a> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: value_context,
- value_context_count: 1,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::new(std::slice::from_ref(value_context)),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
@@ -2458,11 +2455,11 @@ pub(crate) fn parse_and_evaluate_media_if_condition(
Some(evaluate_media_expression(&expression, values, length_context))
}
-pub(crate) unsafe fn parse_and_evaluate_supports_if_condition(
+pub(crate) fn parse_and_evaluate_supports_if_condition(
source: &[u16],
- context: &ParseContext,
+ context: &ParseContext<'_>,
) -> Option<MatchResult> {
- let declared_namespaces = unsafe { declared_namespaces_from_context(context) };
+ let declared_namespaces = declared_namespaces_from_context(context);
let evaluate_feature =
|kind: SupportsFeatureKind, value: &[u16]| supports_feature_matches(context, &declared_namespaces, kind, value);
// NB: Mirrors the supports arm of evaluate_condition_for_substitution, which this series
@@ -2942,9 +2939,9 @@ pub unsafe extern "C" fn rust_parse_sizes_attribute(
.cast()
}
-pub(crate) unsafe fn declared_namespaces_from_context(context: &ParseContext) -> Vec<TokenizerInput<'_>> {
- let namespaces = unsafe { context.declared_namespaces.as_ref() };
- namespaces
+pub(crate) fn declared_namespaces_from_context<'a>(context: &ParseContext<'a>) -> Vec<TokenizerInput<'a>> {
+ context
+ .declared_namespaces
.into_iter()
.flat_map(|namespaces| namespaces.prefixes.iter())
.map(|namespace| TokenizerInput::Utf16(namespace.units()))
@@ -2970,7 +2967,7 @@ pub unsafe extern "C" fn rust_parse_supports_condition(
let Some(context) = context else {
return std::ptr::null();
};
- let declared_namespaces = unsafe { declared_namespaces_from_context(context) };
+ let declared_namespaces = declared_namespaces_from_context(context);
let Some(expression) = parse_supports_condition(source, &|kind, value| {
supports_feature_matches(context, &declared_namespaces, kind, value)
}) else {
diff --git a/Libraries/LibWeb/Rust/src/css/parser/stylesheet_cache.rs b/Libraries/LibWeb/Rust/src/css/parser/stylesheet_cache.rs
index df73cd6c99d..58fbcaec003 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/stylesheet_cache.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/stylesheet_cache.rs
@@ -165,16 +165,15 @@ fn own_text(source: TokenizerInput<'_>) -> Box<[u16]> {
text.into_boxed_slice()
}
-unsafe fn own_view(view: FfiUtf16View) -> Option<Box<[u16]>> {
+fn own_view(view: FfiUtf16View) -> Option<Box<[u16]>> {
Some(own_text(view.units()?))
}
-unsafe fn own_url(pointer: *const u8, length: usize) -> Option<Box<[u16]>> {
- if length == 0 {
+fn own_url(bytes: &[u8]) -> Option<Box<[u16]>> {
+ if bytes.is_empty() {
return Some(Box::default());
}
// The legacy URL context is UTF-8. Keep the cache's owned strings in native UTF-16.
- let bytes = unsafe { ak::slice_from_raw_parts(pointer, length) };
Some(std::str::from_utf8(bytes).ok()?.encode_utf16().collect())
}
@@ -188,38 +187,34 @@ impl ContextKey {
contains_attr_tainted_values,
is_ua_style_sheet,
value_contexts: borrowed_value_contexts,
- value_context_count,
- declared_namespaces: borrowed_declared_namespaces,
+ declared_namespaces,
// Parsed values do not use the document URL. Image values capture the base URL.
document_url: _,
- document_url_length: _,
document_base_url,
- document_base_url_length,
length_resolution_context,
random_function_index,
} = *context;
- let mut value_contexts = Vec::with_capacity(value_context_count);
- for index in 0..value_context_count {
- let FfiValueParsingContext {
- kind,
- value,
- secondary_value,
- name,
- } = unsafe { *borrowed_value_contexts.add(index) };
+ let mut value_contexts = Vec::with_capacity(borrowed_value_contexts.as_slice().len());
+ for &FfiValueParsingContext {
+ kind,
+ value,
+ secondary_value,
+ name,
+ } in borrowed_value_contexts.as_slice()
+ {
value_contexts.push(ValueContextKey {
kind: kind as u8,
value,
secondary_value,
- name: unsafe { own_view(name) }?,
+ name: own_view(name)?,
});
}
- let declared_namespaces = unsafe { borrowed_declared_namespaces.as_ref() }
+ let declared_namespaces = declared_namespaces
.into_iter()
.flat_map(|namespaces| namespaces.prefixes.iter())
.map(|namespace| namespace.units().into())
.collect();
- let length_resolution = match unsafe { length_resolution_context.cast::<FfiLengthResolutionContext>().as_ref() }
- {
+ let length_resolution = match length_resolution_context {
Some(context) => Some(LengthResolutionKey::new(context)?),
None => None,
};
@@ -236,7 +231,7 @@ impl ContextKey {
declared_namespaces,
random_function_index: unsafe { random_function_index.as_ref() }.copied(),
},
- document_base_url: unsafe { own_url(document_base_url, document_base_url_length) }?,
+ document_base_url: own_url(document_base_url.as_slice())?,
length_resolution,
})
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax.rs
index 3ec477f2c5a..ca7ddb8a1c3 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax.rs
@@ -512,21 +512,18 @@ pub unsafe extern "C" fn rust_parse_with_syntax(
mod tests {
use super::*;
- fn context(random_function_index: &mut usize) -> ParseContext {
+ fn context(random_function_index: &mut usize) -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index,
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
index 8b957fd6cd8..5004195b9a3 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
@@ -1948,9 +1948,8 @@ pub(crate) fn supports_declaration_matches(context: &ParseContext, source: &[u16
};
let mut value_context = *context;
value_context.is_ua_style_sheet = false;
- value_context.value_contexts = &raw const property_context;
- value_context.value_context_count = 1;
- value_context.length_resolution_context = std::ptr::null();
+ value_context.value_contexts = ak::Span::new(std::slice::from_ref(&property_context));
+ value_context.length_resolution_context = None;
let outcome = parse_css_value_with_utf16_source(&value_context, property_id, &declaration.value, source.as_ref());
if let Some(random_function_index) = unsafe { context.random_function_index.as_mut() } {
*random_function_index = 0;
@@ -2142,7 +2141,7 @@ struct SyntaxParseBuilder {
// Whether a top-level @import or @namespace rule appended next is still in its ordering window.
import_rules_valid: bool,
namespace_rules_valid: bool,
- parse_context: *const ParseContext,
+ parse_context: *const ParseContext<'static>,
preserve_property_source_text: bool,
dedup: DeclarationDedup,
}
@@ -2839,7 +2838,7 @@ impl SyntaxParseBuilder {
pub(crate) fn new(parse_context: *const ParseContext, preserve_property_source_text: bool) -> Self {
let declared_namespaces = (unsafe { parse_context.as_ref() })
- .map(|context| unsafe { declared_namespaces_from_context(context) })
+ .map(declared_namespaces_from_context)
.unwrap_or_default()
.into_iter()
.map(|namespace| {
@@ -2861,7 +2860,7 @@ impl SyntaxParseBuilder {
declared_namespaces,
import_rules_valid: true,
namespace_rules_valid: true,
- parse_context,
+ parse_context: parse_context.cast(),
preserve_property_source_text,
dedup: DeclarationDedup::default(),
}
@@ -3044,13 +3043,7 @@ impl SyntaxParseBuilder {
};
let parse_context = unsafe { &*self.parse_context };
- let mut value_contexts = if parse_context.value_context_count == 0 {
- Vec::new()
- } else {
- // SAFETY: C++ keeps the context storage live for the complete syntax parse call.
- unsafe { ak::slice_from_raw_parts(parse_context.value_contexts, parse_context.value_context_count) }
- .to_vec()
- };
+ let mut value_contexts = parse_context.value_contexts.as_slice().to_vec();
value_contexts.push(FfiValueParsingContext {
kind: FfiValueParsingContextKind::Property,
value: property_id,
@@ -3058,8 +3051,7 @@ impl SyntaxParseBuilder {
name: Default::default(),
});
let mut context = *parse_context;
- context.value_contexts = value_contexts.as_ptr();
- context.value_context_count = value_contexts.len();
+ context.value_contexts = ak::Span::new(&value_contexts);
let outcome = parse_css_value_with_utf16_source(&context, property_id, &declaration.value, source_utf16);
if let Some(random_function_index) = unsafe { parse_context.random_function_index.as_mut() } {
@@ -3983,21 +3975,18 @@ mod tests {
parse_rule_prelude(rule, rule_kind(rule), None, &[], &|_, _| None)
}
- pub(super) fn parse_context() -> ParseContext {
+ pub(super) fn parse_context() -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
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 08eb0d47f38..dddf39effc4 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
@@ -653,16 +653,12 @@ fn namespace_rules_own_native_names_after_the_sheet_is_dropped() {
fn cached_sheets_share_across_urls_without_capturing_image_resource_contexts() {
let mut first_context = super::tests::parse_context();
let first_url = b"https://first.example/path/";
- first_context.document_url = first_url.as_ptr();
- first_context.document_url_length = first_url.len();
- first_context.document_base_url = first_url.as_ptr();
- first_context.document_base_url_length = first_url.len();
+ first_context.document_url = ak::Span::new(first_url);
+ first_context.document_base_url = ak::Span::new(first_url);
let mut second_context = first_context;
let second_url = b"https://second.example/other/";
- second_context.document_url = second_url.as_ptr();
- second_context.document_url_length = second_url.len();
- second_context.document_base_url = second_url.as_ptr();
- second_context.document_base_url_length = second_url.len();
+ second_context.document_url = ak::Span::new(second_url);
+ second_context.document_base_url = ak::Span::new(second_url);
let source = utf16(".shared-without-images { display: none !important; --image: url(later.png) }");
let first = parse(&source, &first_context);
@@ -687,11 +683,12 @@ fn cached_calculated_descriptors_keep_their_length_resolution_context() {
let source =
utf16("@counter-style contextual { system: numeric; symbols: 'a' 'b'; pad: calc(sign(1em - 1px) + 1) '0'; }");
let mut context = super::tests::parse_context();
- let mut lengths = crate::css::style_compute::FfiLengthResolutionContext::default();
- context.length_resolution_context = (&raw const lengths).cast();
+ let lengths = crate::css::style_compute::FfiLengthResolutionContext::default();
+ context.length_resolution_context = Some(&lengths);
let first = parse(&source, &context);
- lengths.font_metrics.font_size = 16.0;
- context.length_resolution_context = (&raw const lengths).cast();
+ let mut larger_lengths = lengths;
+ larger_lengths.font_metrics.font_size = 16.0;
+ context.length_resolution_context = Some(&larger_lengths);
let second = parse(&source, &context);
assert!(!Arc::ptr_eq(&first, &second));
let repeated = parse(&source, &context);
@@ -710,28 +707,28 @@ fn cache_keys_include_parsing_context_and_replay_side_effects() {
assert!(!Arc::ptr_eq(&first, &quirks));
context.in_quirks_mode = false;
let base_url = b"https://example.com/directory/";
- context.document_base_url = base_url.as_ptr();
- context.document_base_url_length = base_url.len();
+ context.document_base_url = ak::Span::new(base_url);
let with_base_url = parse(&source, &context);
assert!(Arc::ptr_eq(&first, &with_base_url));
- context.document_base_url = std::ptr::null();
- context.document_base_url_length = 0;
+ context.document_base_url = ak::Span::default();
- let mut lengths = crate::css::style_compute::FfiLengthResolutionContext::default();
- context.length_resolution_context = (&raw const lengths).cast();
+ let lengths = crate::css::style_compute::FfiLengthResolutionContext::default();
+ context.length_resolution_context = Some(&lengths);
let with_lengths = parse(&source, &context);
- lengths.viewport_width = 800.0;
- context.length_resolution_context = (&raw const lengths).cast();
+ let mut resized_lengths = lengths;
+ resized_lengths.viewport_width = 800.0;
+ context.length_resolution_context = Some(&resized_lengths);
let resized = parse(&source, &context);
assert!(Arc::ptr_eq(&first, &with_lengths));
assert!(Arc::ptr_eq(&with_lengths, &resized));
let mut resolved_viewport_length = false;
- lengths.resolved_viewport_relative_length = &raw mut resolved_viewport_length;
- context.length_resolution_context = (&raw const lengths).cast();
+ let mut tracking_lengths = resized_lengths;
+ tracking_lengths.resolved_viewport_relative_length = &raw mut resolved_viewport_length;
+ context.length_resolution_context = Some(&tracking_lengths);
let tracked = parse(&source, &context);
let tracked_again = parse(&source, &context);
assert!(!Arc::ptr_eq(&tracked, &tracked_again));
- context.length_resolution_context = std::ptr::null();
+ context.length_resolution_context = None;
let mut random_index = 7;
context.random_function_index = &raw mut random_index;
diff --git a/Libraries/LibWeb/Rust/src/css/parser/transforms_effects_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/transforms_effects_parser.rs
index 319c93b1440..99d95704d05 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/transforms_effects_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/transforms_effects_parser.rs
@@ -821,21 +821,18 @@ mod tests {
use crate::css::css_tokenizer::tokenize_for_parser;
use crate::css::parser::component_value::consume_a_list_of_component_values;
- fn context() -> ParseContext {
+ fn context() -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
index b8e70d1dad4..82ca780cdeb 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
@@ -116,29 +116,25 @@ pub struct FfiValueParsingContext<'a> {
pub name: FfiUtf16View<'a>,
}
-/// Parser state required by CSS value parsing.
+/// Parser state required by CSS value parsing, borrowing what it names for `'a`.
#[repr(C)]
#[derive(Clone, Copy)]
-pub struct ParseContext {
+pub struct ParseContext<'a> {
pub in_quirks_mode: bool,
pub is_svg_presentation_attribute: bool,
pub is_substituted_value: bool,
pub contains_attr_tainted_values: bool,
pub is_ua_style_sheet: bool,
- /// Borrowed for the parse.
- pub value_contexts: *const FfiValueParsingContext<'static>,
- pub value_context_count: usize,
- pub declared_namespaces: *const crate::css::rule::NativeNamespaceContext,
- pub document_url: *const u8,
- pub document_url_length: usize,
- pub document_base_url: *const u8,
- pub document_base_url_length: usize,
- pub length_resolution_context: *const c_void,
+ pub value_contexts: ak::Span<'a, FfiValueParsingContext<'a>>,
+ pub declared_namespaces: Option<&'a crate::css::rule::NativeNamespaceContext>,
+ pub document_url: ak::Span<'a, u8>,
+ pub document_base_url: ak::Span<'a, u8>,
+ pub length_resolution_context: Option<&'a crate::css::style_compute::FfiLengthResolutionContext>,
pub random_function_index: *mut usize,
}
/// An empty context: no value contexts, namespaces or URLs, outside quirks mode.
-impl Default for ParseContext {
+impl Default for ParseContext<'_> {
fn default() -> Self {
Self {
in_quirks_mode: false,
@@ -146,14 +142,11 @@ impl Default for ParseContext {
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
@@ -1133,13 +1126,7 @@ pub(crate) fn parse_tree_counting_value(
}
pub(crate) fn context_allows_tree_counting_functions(context: &ParseContext) -> bool {
- let contexts = if context.value_context_count == 0 {
- &[]
- } else if context.value_contexts.is_null() {
- return false;
- } else {
- unsafe { ak::slice_from_raw_parts(context.value_contexts, context.value_context_count) }
- };
+ let contexts = context.value_contexts.as_slice();
!contexts.iter().any(|value_context| {
value_context.kind == FfiValueParsingContextKind::Descriptor
|| (value_context.kind == FfiValueParsingContextKind::Special && matches!(value_context.value, 0..=2))
@@ -1147,10 +1134,10 @@ pub(crate) fn context_allows_tree_counting_functions(context: &ParseContext) ->
}
fn substitution_function_context_names(context: &ParseContext) -> Option<Vec<Vec<u16>>> {
- if context.value_context_count == 0 || context.value_contexts.is_null() {
+ let contexts = context.value_contexts.as_slice();
+ if contexts.is_empty() {
return None;
}
- let contexts = unsafe { ak::slice_from_raw_parts(context.value_contexts, context.value_context_count) };
let names = contexts
.iter()
.filter(|value_context| value_context.kind == FfiValueParsingContextKind::Function)
@@ -1222,7 +1209,7 @@ pub(crate) fn parse_calculated_numeric_value_with_ranges(
random_function_index: context.random_function_index,
allowed_color_channels: 0,
allow_random_functions: context_allows_random_functions(context),
- parse_context: context,
+ parse_context: std::ptr::from_ref(context).cast(),
},
) {
Ok(root) => root,
@@ -3669,13 +3656,7 @@ fn context_allows_quirky_color(context: &ParseContext, property: u16) -> bool {
if !context.in_quirks_mode || !property_has_hashless_hex_color_quirk(property) {
return false;
}
- let value_contexts = if context.value_context_count == 0 {
- &[]
- } else if context.value_contexts.is_null() {
- return false;
- } else {
- unsafe { ak::slice_from_raw_parts(context.value_contexts, context.value_context_count) }
- };
+ let value_contexts = context.value_contexts.as_slice();
value_contexts.iter().all(|value_context| {
value_context.kind == FfiValueParsingContextKind::Property
&& property_has_hashless_hex_color_quirk(value_context.value)
@@ -3683,13 +3664,7 @@ fn context_allows_quirky_color(context: &ParseContext, property: u16) -> bool {
}
pub(crate) fn context_allows_random_functions(context: &ParseContext) -> bool {
- let value_contexts = if context.value_context_count == 0 {
- &[]
- } else if context.value_contexts.is_null() {
- return false;
- } else {
- unsafe { ak::slice_from_raw_parts(context.value_contexts, context.value_context_count) }
- };
+ let value_contexts = context.value_contexts.as_slice();
if value_contexts.first().is_some_and(|value_context| {
value_context.kind == FfiValueParsingContextKind::Special && matches!(value_context.value, 0 | 3)
@@ -3793,14 +3768,11 @@ pub(crate) fn parse_initial_value(property: u16) -> Option<Arc<StyleValueData>>
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: &raw const property_context,
- value_context_count: 1,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::new(std::slice::from_ref(&property_context)),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
};
let values = component_values_from_source(property_initial_value(property).as_bytes()).ok()?;
@@ -6125,16 +6097,14 @@ fn parse_css_primitive_values(
) -> Option<(StyleValueData, usize)> {
let mut stream = TokenStream::new(values);
let range = NumericRange::new(range_min, range_max);
- let property = if context.value_context_count == 0 || context.value_contexts.is_null() {
- property_id::WIDTH
- } else {
- unsafe { ak::slice_from_raw_parts(context.value_contexts, context.value_context_count) }
- .iter()
- .find(|value_context| {
- value_context.kind == FfiValueParsingContextKind::Property && value_context.value != property_id::CUSTOM
- })
- .map_or(property_id::WIDTH, |value_context| value_context.value)
- };
+ let property = context
+ .value_contexts
+ .as_slice()
+ .iter()
+ .find(|value_context| {
+ value_context.kind == FfiValueParsingContextKind::Property && value_context.value != property_id::CUSTOM
+ })
+ .map_or(property_id::WIDTH, |value_context| value_context.value);
let parsed = match value_type {
VALUE_TYPE_ANGLE => parse_angle_from_stream(context, property, &mut stream, range),
VALUE_TYPE_INTEGER => parse_integer_from_stream(context, property, &mut stream, range),
@@ -6305,21 +6275,18 @@ mod tests {
source.units().to_vec()
}
- fn context() -> ParseContext {
+ fn context() -> ParseContext<'static> {
ParseContext {
in_quirks_mode: false,
is_svg_presentation_attribute: false,
is_substituted_value: false,
contains_attr_tainted_values: false,
is_ua_style_sheet: false,
- value_contexts: std::ptr::null(),
- value_context_count: 0,
- declared_namespaces: std::ptr::null(),
- document_url: std::ptr::null(),
- document_url_length: 0,
- document_base_url: std::ptr::null(),
- document_base_url_length: 0,
- length_resolution_context: std::ptr::null(),
+ value_contexts: ak::Span::default(),
+ declared_namespaces: None,
+ document_url: ak::Span::default(),
+ document_base_url: ak::Span::default(),
+ length_resolution_context: None,
random_function_index: std::ptr::null_mut(),
}
}
@@ -7027,8 +6994,7 @@ mod tests {
secondary_value: 0,
name: FfiUtf16View::default(),
};
- random_context.value_contexts = &raw const value_context;
- random_context.value_context_count = 1;
+ random_context.value_contexts = ak::Span::new(std::slice::from_ref(&value_context));
random_context.random_function_index = &raw mut random_function_index;
assert!(matches!(
parse_with_context(
@@ -7365,8 +7331,7 @@ mod tests {
secondary_value: 0,
name: FfiUtf16View::default(),
};
- random_context.value_contexts = &raw const property_context;
- random_context.value_context_count = 1;
+ random_context.value_contexts = ak::Span::new(std::slice::from_ref(&property_context));
random_context.random_function_index = &raw mut random_function_index;
let ParseOutcome::Parsed(value) = parse_with_context(&random_context, property_id::OPACITY, "random(0, 1)")
else {
@@ -7431,8 +7396,7 @@ mod tests {
},
];
let mut canvas_context = context();
- canvas_context.value_contexts = value_contexts.as_ptr();
- canvas_context.value_context_count = value_contexts.len();
+ canvas_context.value_contexts = ak::Span::new(&value_contexts);
canvas_context.random_function_index = &raw mut random_function_index;
assert!(matches!(
@@ -7591,8 +7555,7 @@ mod tests {
name: FfiUtf16View::from_ascii(function_name),
}));
let mut parse_context = context();
- parse_context.value_contexts = function_contexts.as_ptr();
- parse_context.value_context_count = function_contexts.len();
+ parse_context.value_contexts = ak::Span::new(&function_contexts);
assert!(
matches!(
parse_with_context(&parse_context, property, source),
diff --git a/Libraries/LibWeb/Rust/src/css/rule/compilation.rs b/Libraries/LibWeb/Rust/src/css/rule/compilation.rs
index 453d282c43b..d88373569d4 100644
--- a/Libraries/LibWeb/Rust/src/css/rule/compilation.rs
+++ b/Libraries/LibWeb/Rust/src/css/rule/compilation.rs
@@ -1565,7 +1565,7 @@ mod tests {
drop(sheet);
std::thread::spawn(move || {
let context = ParseContext {
- declared_namespaces: Arc::as_ptr(&namespaces),
+ declared_namespaces: Some(&namespaces),
..ParseContext::default()
};
for (source, expected) in [("色|item { color: red; }", 1), ("未知|item { color: red; }", 0)] {