Skip to content

Truly independent web browser

LibWeb: Resolve custom function visibility indices once per snapshot

Callum Law31a699dd1e9e
diff --git a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
index f95d27f943f..0044247d63c 100644
--- a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs
@@ -374,7 +374,7 @@ pub(crate) struct CascadeResolutionContext<'a> {
pub(crate) attributes: &'a [crate::css::custom_properties::SubstitutionAttribute<'a>],
pub(crate) custom_functions: &'a [crate::css::custom_properties::SubstitutionFunctionDefinition],
pub(crate) custom_function_scope_identity: usize,
- pub(crate) custom_function_visibilities: &'a [crate::css::custom_properties::FfiSubstitutionFunctionVisibility],
+ pub(crate) custom_function_visibilities: &'a [crate::css::custom_properties::CustomFunctionVisibility],
/// Where the custom properties and sibling position style queries read are kept, if anywhere.
pub(crate) style_query_dependencies:
Option<&'a mut Option<Box<crate::css::custom_properties::StyleQueryDependencies>>>,
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index f23ba688ae7..4a5db0a9f2a 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -163,7 +163,7 @@ struct CustomFunctionRegistry {
definitions: Vec<CustomFunctionDefinition>,
/// Which definition a call names from each scope that sees one: the caller scope and the
/// index of the definition its name resolves to there.
- visible: Vec<(usize, usize)>,
+ visible: Vec<CustomFunctionVisibility>,
}
#[derive(Clone)]
@@ -225,6 +225,13 @@ pub(crate) struct SubstitutionFunctionDefinition {
pub(crate) declarations: Vec<SubstitutionFunctionDeclaration>,
}
+/// A definition visible to a caller scope, indexed in the prepared definition list.
+#[derive(Clone, Copy)]
+pub(crate) struct CustomFunctionVisibility {
+ pub(crate) caller_scope_identity: usize,
+ pub(crate) definition_index: usize,
+}
+
/// That a call from a scope names a function: the definition its name dereferences to there, as
/// a tree-scoped reference.
#[repr(C)]
@@ -1138,7 +1145,7 @@ fn tokens_for_function_value(data: &StyleValueData) -> (Vec<ComponentValue>, boo
fn custom_function_registry(
definitions: &[SubstitutionFunctionDefinition],
caller_scope_identity: usize,
- visibilities: &[FfiSubstitutionFunctionVisibility],
+ visibilities: &[CustomFunctionVisibility],
) -> CustomFunctionRegistry {
let mut parsed_definitions = Vec::with_capacity(definitions.len());
for definition in definitions {
@@ -1165,20 +1172,10 @@ fn custom_function_registry(
declarations: parsed_declarations,
});
}
- let visible = visibilities
- .iter()
- .filter_map(|visibility| {
- let index = definitions
- .iter()
- .position(|definition| definition.identity == visibility.function_identity);
- debug_assert!(index.is_some(), "a visible function is published with its definition");
- Some((visibility.caller_scope_identity, index?))
- })
- .collect();
CustomFunctionRegistry {
caller_scope_identity,
definitions: parsed_definitions,
- visible,
+ visible: visibilities.to_vec(),
}
}
@@ -1187,7 +1184,7 @@ pub(crate) fn prepare_var_resolution_environment(
attributes: &[SubstitutionAttribute],
custom_functions: &[SubstitutionFunctionDefinition],
custom_function_scope_identity: usize,
- custom_function_visibilities: &[FfiSubstitutionFunctionVisibility],
+ custom_function_visibilities: &[CustomFunctionVisibility],
) -> VarResolutionEnvironment {
let attributes = attributes
.iter()
@@ -2809,8 +2806,8 @@ fn replace_a_dashed_function(
let definition = functions
.visible
.iter()
- .filter(|&&(scope, _)| scope == caller_scope_identity)
- .map(|&(_, index)| &functions.definitions[index])
+ .filter(|visibility| visibility.caller_scope_identity == caller_scope_identity)
+ .map(|visibility| &functions.definitions[visibility.definition_index])
.find(|definition| definition.signature.name.units() == name);
let Some(function) = definition else {
return TokenResolution::Invalid;
@@ -3624,7 +3621,10 @@ mod tests {
true,
)],
}],
- visible: vec![(1, 0)],
+ visible: vec![CustomFunctionVisibility {
+ caller_scope_identity: 1,
+ definition_index: 0,
+ }],
};
let mut context = ASFResolutionContext {
custom_functions: Some(&functions),
@@ -3665,7 +3665,24 @@ mod tests {
definition(11, 2, "--inner", b"2px"),
definition(12, 1, "--inner", b"1px"),
],
- visible: vec![(1, 0), (1, 2), (2, 0), (2, 1)],
+ visible: vec![
+ CustomFunctionVisibility {
+ caller_scope_identity: 1,
+ definition_index: 0,
+ },
+ CustomFunctionVisibility {
+ caller_scope_identity: 1,
+ definition_index: 2,
+ },
+ CustomFunctionVisibility {
+ caller_scope_identity: 2,
+ definition_index: 0,
+ },
+ CustomFunctionVisibility {
+ caller_scope_identity: 2,
+ definition_index: 1,
+ },
+ ],
};
let substitute = |source: &[u8]| {
let mut context = ASFResolutionContext {
diff --git a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
index 29631ce9748..a223a6bd6f5 100644
--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
@@ -23,9 +23,9 @@ use crate::css::cascaded_properties::{
CascadeResolutionContext, parse_substituted_value, resolve_declared_custom_properties,
};
use crate::css::custom_properties::{
- CustomPropertyFinalization, CustomPropertyStore, FfiSubstitutionFunctionVisibility, NativeVarResolution,
- SubstitutionAttribute, SubstitutionFunctionDeclaration, SubstitutionFunctionDefinition, VarResolutionInputs,
- prepare_var_resolution_environment,
+ CustomFunctionVisibility, CustomPropertyFinalization, CustomPropertyStore, FfiSubstitutionFunctionVisibility,
+ NativeVarResolution, SubstitutionAttribute, SubstitutionFunctionDeclaration, SubstitutionFunctionDefinition,
+ VarResolutionInputs, prepare_var_resolution_environment,
};
use crate::css::parser::component_value::{ComponentKind, ComponentValue};
use crate::css::rule::CompiledFunction;
@@ -115,7 +115,8 @@ pub(super) struct DocumentFunctionSnapshot {
pub(super) struct ScopeFunctions {
/// Each reachable definition, with the scope that defines it and its selected blocks.
definitions: Vec<(Arc<CompiledFunction>, usize, Box<[usize]>)>,
- visibilities: Vec<FfiSubstitutionFunctionVisibility>,
+ /// Caller scope identities paired with indices into `definitions`.
+ visibilities: Vec<CustomFunctionVisibility>,
}
/// What a call from one tree scope may reach: the definitions its scope sees, and those the
@@ -224,7 +225,7 @@ impl DocumentFunctionSnapshot {
continue;
}
let visible = self.visible_from_scope(caller_scope);
- let definitions = visible
+ let definitions: Vec<_> = visible
.definitions
.iter()
.map(|(function, scope)| {
@@ -245,9 +246,23 @@ impl DocumentFunctionSnapshot {
(function, *scope, selected)
})
.collect();
+ let visibilities = visible
+ .visibilities
+ .iter()
+ .filter_map(|visibility| {
+ let index = definitions
+ .iter()
+ .position(|(function, _, _)| function.identity == visibility.function_identity);
+ debug_assert!(index.is_some(), "a visible function is published with its definition");
+ Some(CustomFunctionVisibility {
+ caller_scope_identity: visibility.caller_scope_identity,
+ definition_index: index?,
+ })
+ })
+ .collect();
let scope_functions = ScopeFunctions {
definitions,
- visibilities: visible.visibilities,
+ visibilities,
};
self.scope_functions.insert(caller_scope, scope_functions);
}
@@ -312,7 +327,7 @@ impl DocumentFunctionSnapshot {
/// name.
pub(super) struct PreparedCustomFunctions {
definitions: Vec<SubstitutionFunctionDefinition>,
- visibilities: Vec<FfiSubstitutionFunctionVisibility>,
+ visibilities: Vec<CustomFunctionVisibility>,
caller_scope: usize,
/// Whether a selected declaration substitutes `attr()`, which reads the node's attributes.
pub(super) reads_attributes: bool,