Truly independent web browser
LibWeb: Reuse compiled custom function data during substitution
Callum Lawc9aa55807933
diff --git a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rsindex 0044247d63c..44af370d00c 100644--- a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs+++ b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs@@ -372,9 +372,7 @@ pub(crate) fn parse_substituted_value(pub(crate) struct CascadeResolutionContext<'a> {pub(crate) inputs: crate::css::custom_properties::VarResolutionInputs<'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::CustomFunctionVisibility],+ pub(crate) custom_functions: &'a crate::css::custom_properties::CustomFunctionRegistry<'a>,/// 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>>>,@@ -422,8 +420,6 @@ pub(crate) fn resolve_declared_custom_properties(let mut environment = crate::css::custom_properties::prepare_var_resolution_environment(resolution_context.attributes,resolution_context.custom_functions,- resolution_context.custom_function_scope_identity,- resolution_context.custom_function_visibilities,);let (outputs, reads) = crate::css::custom_properties::resolve_element_custom_properties(&resolution_context.inputs,diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rsindex 4a5db0a9f2a..245bec4c17b 100644--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs@@ -149,21 +149,30 @@ struct RegisteredCustomProperty {type CustomFunctionIdentity = u64;-#[derive(Clone)]-struct CustomFunctionDefinition {- identity: CustomFunctionIdentity,- scope_identity: usize,- signature: Arc<FunctionSignature>,- parameter_defaults: Vec<Option<Vec<ComponentValue>>>,- declarations: Vec<(Vec<u16>, Vec<ComponentValue>, bool)>,+pub(crate) struct PreparedCustomFunction<'a> {+ pub(crate) identity: CustomFunctionIdentity,+ pub(crate) scope_identity: usize,+ pub(crate) signature: &'a FunctionSignature,+ pub(crate) parameter_defaults: &'a [Option<Vec<ComponentValue>>],+ pub(crate) declarations: Vec<&'a SubstitutionFunctionDeclaration>,}-struct CustomFunctionRegistry {- caller_scope_identity: usize,- definitions: Vec<CustomFunctionDefinition>,+pub(crate) struct CustomFunctionRegistry<'a> {+ pub(crate) caller_scope_identity: usize,+ pub(crate) definitions: Vec<PreparedCustomFunction<'a>>,/// 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<CustomFunctionVisibility>,+ pub(crate) visible: &'a [CustomFunctionVisibility],+}++impl CustomFunctionRegistry<'_> {+ pub(crate) const fn empty() -> Self {+ Self {+ caller_scope_identity: 0,+ definitions: Vec::new(),+ visible: &[],+ }+ }}#[derive(Clone)]@@ -213,16 +222,9 @@ pub(crate) struct SubstitutionAttribute<'a> {/// One declaration in the body of a custom function, as its conditions select it.pub(crate) struct SubstitutionFunctionDeclaration {- pub(crate) name: crate::css::style_value::CssString,- pub(crate) value: Arc<StyleValueData>,-}--/// A custom function a call may reach, with the declarations its conditions select.-pub(crate) struct SubstitutionFunctionDefinition {- pub(crate) identity: u64,- pub(crate) scope_identity: usize,- pub(crate) signature: Arc<FunctionSignature>,- pub(crate) declarations: Vec<SubstitutionFunctionDeclaration>,+ name: Vec<u16>,+ tokens: Vec<ComponentValue>,+ includes_substitution: bool,}/// A definition visible to a caller scope, indexed in the prepared definition list.@@ -938,7 +940,7 @@ struct ASFResolutionContext<'a> {in_url_function: bool,/// Whether an attr-tainted value was substituted into a URL, or substituted one.uses_attr_tainted_url: bool,- custom_functions: Option<&'a CustomFunctionRegistry>,+ custom_functions: Option<&'a CustomFunctionRegistry<'a>>,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.@@ -1025,9 +1027,9 @@ pub unsafe extern "C" fn rust_style_query_dependencies_take(type CustomPropertyTokenCache = HashMap<*const StyleValueData, Arc<[ComponentValue]>>;-pub(crate) struct VarResolutionEnvironment {+pub(crate) struct VarResolutionEnvironment<'a> {attributes: HashMap<Vec<u16>, Vec<u16>>,- custom_functions: CustomFunctionRegistry,+ custom_functions: &'a CustomFunctionRegistry<'a>,token_cache: CustomPropertyTokenCache,}@@ -1113,7 +1115,8 @@ fn resolve_typed_custom_property_value(TokenResolution::Resolved(tokens.to_vec())}-fn tokens_for_function_value(data: &StyleValueData) -> (Vec<ComponentValue>, bool) {+/// Tokens, substitution-function presence, and attribute reads for a function value.+fn tokens_for_function_value(data: &StyleValueData) -> (Vec<ComponentValue>, bool, bool) {if let StyleValueData::Unresolved {components,presence_attr,@@ -1133,68 +1136,64 @@ fn tokens_for_function_value(data: &StyleValueData) -> (Vec<ComponentValue>, boo|| *presence_env|| *presence_if|| *presence_inherit,+ *presence_attr,);}let source = crate::css::serialize::serialize_style_value_to_utf16(data);(parse_substitution_values(&source).expect("serialized style values form valid component values"),false,+ false,)}-fn custom_function_registry(- definitions: &[SubstitutionFunctionDefinition],- caller_scope_identity: usize,- visibilities: &[CustomFunctionVisibility],-) -> CustomFunctionRegistry {- let mut parsed_definitions = Vec::with_capacity(definitions.len());- for definition in definitions {- let signature = definition.signature.clone();- let mut parameter_defaults = Vec::with_capacity(signature.parameters.len());- for parameter in &signature.parameters {- parameter_defaults.push(- parameter- .default_value- .as_ref()- .map(|data| tokens_for_function_value(data).0),- );- }- let mut parsed_declarations = Vec::with_capacity(definition.declarations.len());- for declaration in &definition.declarations {- let (tokens, includes_substitution) = tokens_for_function_value(&declaration.value);- parsed_declarations.push((declaration.name.units().to_vec(), tokens, includes_substitution));- }- parsed_definitions.push(CustomFunctionDefinition {- identity: definition.identity,- scope_identity: definition.scope_identity,- signature,- parameter_defaults,- declarations: parsed_declarations,- });+pub(crate) fn prepare_function_parameter_defaults(+ signature: &FunctionSignature,+) -> (Vec<Option<Vec<ComponentValue>>>, bool) {+ let mut parameter_defaults = Vec::with_capacity(signature.parameters.len());+ let mut reads_attributes = false;++ for parameter in &signature.parameters {+ parameter_defaults.push(parameter.default_value.as_ref().map(|data| {+ let (tokens, _, value_reads_attributes) = tokens_for_function_value(data);+ reads_attributes |= value_reads_attributes;+ tokens+ }));}- CustomFunctionRegistry {- caller_scope_identity,- definitions: parsed_definitions,- visible: visibilities.to_vec(),++ (parameter_defaults, reads_attributes)+}++pub(crate) fn prepare_function_declarations(+ descriptor_block: &crate::css::descriptor_block::DescriptorBlockData,+) -> (Vec<SubstitutionFunctionDeclaration>, bool) {+ let mut declarations = Vec::with_capacity(descriptor_block.descriptors.len());+ let mut reads_attributes = false;++ for declaration in &descriptor_block.descriptors {+ let (tokens, includes_substitution, value_reads_attributes) = tokens_for_function_value(&declaration.value);++ reads_attributes |= value_reads_attributes;++ declarations.push(SubstitutionFunctionDeclaration {+ name: declaration.name.units().to_vec(),+ tokens,+ includes_substitution,+ });}++ (declarations, reads_attributes)}/// Builds the element-wide immutable substitution inputs once for all declarations in a cascade.-pub(crate) fn prepare_var_resolution_environment(+pub(crate) fn prepare_var_resolution_environment<'a>(attributes: &[SubstitutionAttribute],- custom_functions: &[SubstitutionFunctionDefinition],- custom_function_scope_identity: usize,- custom_function_visibilities: &[CustomFunctionVisibility],-) -> VarResolutionEnvironment {+ custom_functions: &'a CustomFunctionRegistry<'a>,+) -> VarResolutionEnvironment<'a> {let attributes = attributes.iter().map(|attribute| (attribute.name.to_vec(), attribute.value.to_vec())).collect();- let custom_functions = custom_function_registry(- custom_functions,- custom_function_scope_identity,- custom_function_visibilities,- );VarResolutionEnvironment {attributes,custom_functions,@@ -1326,7 +1325,7 @@ fn compute_element_custom_property(/// Computes the element's declared custom properties, reusing completed values across declarations.pub(crate) fn resolve_element_custom_properties(inputs: &VarResolutionInputs,- environment: &mut VarResolutionEnvironment,+ environment: &mut VarResolutionEnvironment<'_>,style_query_dependencies: Option<&mut Option<Box<StyleQueryDependencies>>>,finalization: CustomPropertyFinalization,) -> (Vec<RetainedStyleValueData>, RegisteredValueReads) {@@ -1338,7 +1337,7 @@ pub(crate) fn resolve_element_custom_properties(attributes: Some(&environment.attributes),inheritance_store: inputs.inheritance_store,attribute_names_are_ascii_case_insensitive: inputs.attribute_names_are_ascii_case_insensitive,- custom_functions: Some(&environment.custom_functions),+ custom_functions: Some(environment.custom_functions),parse_context: inputs.parse_context,media_environment: inputs.media_environment,style_query_length_resolution_context: inputs.style_query_inputs.map(|inputs| &inputs.length),@@ -2575,7 +2574,7 @@ fn resolve_function_styles<'a, 'names>(fn evaluate_a_custom_function<'a>(store: Option<&CustomPropertyStore>,registry: Option<&CustomPropertyRegistry>,- custom_function: &'a CustomFunctionDefinition,+ custom_function: &'a PreparedCustomFunction<'a>,arguments: Vec<TokenResolution>,context: &mut ASFResolutionContext<'a>,recursion_depth: u32,@@ -2732,12 +2731,12 @@ fn evaluate_a_custom_function<'a>(// NB: Function declarations are inserted after the parameter values to emulate prepending those parameter values// to the body rule.- for (name, tokens, includes_substitution) in &custom_function.declarations {+ for declaration in &custom_function.declarations {body_rule.insert(- name.clone(),+ declaration.name.clone(),FunctionLocalValue {- tokens: Cow::Borrowed(tokens),- includes_substitution: *includes_substitution,+ tokens: Cow::Borrowed(&declaration.tokens),+ includes_substitution: declaration.includes_substitution,},);}@@ -2756,7 +2755,7 @@ fn evaluate_a_custom_function<'a>(custom_function.declarations.iter()- .map(|(name, _, _)| name.as_slice())+ .map(|declaration| declaration.name.as_slice()).filter(|name| *name != result_name.as_slice()).chain(std::iter::once(result_name.as_slice())),context,@@ -3322,7 +3321,7 @@ pub(crate) fn resolve_vars(property_id: u16,root_custom_property_name: &[u16],value_data: &StyleValueData,- environment: &mut VarResolutionEnvironment,+ environment: &mut VarResolutionEnvironment<'_>,style_query_dependencies: Option<&mut Option<Box<StyleQueryDependencies>>>,) -> NativeVarResolution {let VarResolutionInputs {@@ -3599,29 +3598,19 @@ mod tests {#[test]fn substitutes_custom_functions_from_a_snapshot() {+ let definitions =+ crate::css::rule::compile_functions_for_testing("@function --echo(--value) { result: var(--value); }");+ let function = &definitions[0];let functions = CustomFunctionRegistry {caller_scope_identity: 1,- definitions: vec![CustomFunctionDefinition {- identity: 2,+ definitions: vec![PreparedCustomFunction {+ identity: function.identity,scope_identity: 1,- signature: Arc::new(FunctionSignature {- name: crate::css::css_string::CssString::from_utf16(&utf16("--echo")),- parameters: vec![crate::css::function_signature::FunctionParameterData {- name: crate::css::css_string::CssString::from_utf16(&utf16("--value")),- syntax: Arc::new(SyntaxNode::Universal),- default_value: None,- }]- .into_boxed_slice(),- return_type: Arc::new(SyntaxNode::Universal),- }),- parameter_defaults: vec![None],- declarations: vec![(- utf16("result"),- parse_substitution_values(b"var(--value)").unwrap(),- true,- )],+ signature: &function.signature,+ parameter_defaults: &function.parameter_defaults,+ declarations: function.inputs.iter().flat_map(|input| &input.declarations).collect(),}],- visible: vec![CustomFunctionVisibility {+ visible: &[CustomFunctionVisibility {caller_scope_identity: 1,definition_index: 0,}],@@ -3644,28 +3633,28 @@ mod tests {#[test]fn a_nested_call_names_the_function_its_caller_s_scope_sees() {- let definition = |identity, scope_identity, name: &str, result: &[u8]| CustomFunctionDefinition {- identity,- scope_identity,- signature: Arc::new(FunctionSignature {- name: crate::css::css_string::CssString::from_utf16(&utf16(name)),- parameters: Box::default(),- return_type: Arc::new(SyntaxNode::Universal),- }),- parameter_defaults: Vec::new(),- declarations: vec![(utf16("result"), parse_substitution_values(result).unwrap(), true)],- };// The document (scope 1) defines --inner, a shadow tree (scope 2) defines --outer and an// --inner of its own. A call from the document reaches --outer, whose body calls --inner// as the shadow tree names it.+ let definitions = crate::css::rule::compile_functions_for_testing(+ "@function --outer() { result: --inner(); }+ @function --inner() { result: 2px; }+ @function --inner() { result: 1px; }",+ );let functions = CustomFunctionRegistry {caller_scope_identity: 1,- definitions: vec![- definition(10, 2, "--outer", b"--inner()"),- definition(11, 2, "--inner", b"2px"),- definition(12, 1, "--inner", b"1px"),- ],- visible: vec![+ definitions: definitions+ .iter()+ .zip([2, 2, 1])+ .map(|(function, scope_identity)| PreparedCustomFunction {+ identity: function.identity,+ scope_identity,+ signature: &function.signature,+ parameter_defaults: &function.parameter_defaults,+ declarations: function.inputs.iter().flat_map(|input| &input.declarations).collect(),+ })+ .collect(),+ visible: &[CustomFunctionVisibility {caller_scope_identity: 1,definition_index: 0,@@ -3852,7 +3841,8 @@ mod tests {style_query_inputs: None,};- let mut environment = prepare_var_resolution_environment(&[], &[], 0, &[]);+ let functions = CustomFunctionRegistry::empty();+ let mut environment = prepare_var_resolution_environment(&[], &functions);CUSTOM_PROPERTY_COMPUTATIONS.with(|count| count.set(0));diff --git a/Libraries/LibWeb/Rust/src/css/rule/function.rs b/Libraries/LibWeb/Rust/src/css/rule/function.rsindex 388ecde886a..f28fd2164d2 100644--- a/Libraries/LibWeb/Rust/src/css/rule/function.rs+++ b/Libraries/LibWeb/Rust/src/css/rule/function.rs@@ -7,15 +7,19 @@use super::NativeRuleType;use super::read::{NativeRuleView, RuleRef};use crate::css::container_conditions::ContainerConditionsData;-use crate::css::descriptor_block::DescriptorBlockData;+use crate::css::custom_properties::{+ SubstitutionFunctionDeclaration, prepare_function_declarations, prepare_function_parameter_defaults,+};use crate::css::function_signature::FunctionSignature;use crate::css::media_list::MediaListData;+use crate::css::parser::component_value::ComponentValue;use std::ops::ControlFlow;use std::sync::Arc;/// One block of a function's declarations, with the conditions that gate it.pub(crate) struct FunctionDeclarationInput {- pub(crate) declarations: Arc<DescriptorBlockData>,+ pub(crate) declarations: Vec<SubstitutionFunctionDeclaration>,+ pub(crate) reads_attributes: bool,/// The container conditions around the block, nearest first, which hold for an element or not.pub(crate) containers: Vec<Arc<ContainerConditionsData>>,/// The `@media` lists around the block, which hold for the document or not.@@ -27,6 +31,8 @@ pub(crate) struct FunctionDeclarationInput {pub struct CompiledFunction {pub(crate) identity: u64,pub(crate) signature: Arc<FunctionSignature>,+ pub(crate) parameter_defaults: Vec<Option<Vec<ComponentValue>>>,+ pub(crate) parameter_defaults_read_attributes: bool,pub(crate) inputs: Vec<FunctionDeclarationInput>,}@@ -53,8 +59,11 @@ impl CompiledFunction {.cloned().collect()};+ let (parsed_declarations, reads_attributes) =+ prepare_function_declarations(&rule.descriptors().unwrap());inputs.push(FunctionDeclarationInput {- declarations: rule.descriptors().unwrap(),+ declarations: parsed_declarations,+ reads_attributes,containers: conditions,media: media.clone(),});@@ -79,9 +88,13 @@ impl CompiledFunction {_ => {}}}+ let signature = view.rule.function_signature().unwrap();+ let (parameter_defaults, parameter_defaults_read_attributes) = prepare_function_parameter_defaults(&signature);let mut result = Self {identity: view.rule.identity(),- signature: view.rule.function_signature().unwrap(),+ signature,+ parameter_defaults,+ parameter_defaults_read_attributes,inputs: Vec::new(),};let _ = view.rule.visit_children(&mut |child| {diff --git a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rsindex a223a6bd6f5..ed0cf153aeb 100644--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs@@ -23,8 +23,8 @@ use crate::css::cascaded_properties::{CascadeResolutionContext, parse_substituted_value, resolve_declared_custom_properties,};use crate::css::custom_properties::{- CustomFunctionVisibility, CustomPropertyFinalization, CustomPropertyStore, FfiSubstitutionFunctionVisibility,- NativeVarResolution, SubstitutionAttribute, SubstitutionFunctionDeclaration, SubstitutionFunctionDefinition,+ CustomFunctionRegistry, CustomFunctionVisibility, CustomPropertyFinalization, CustomPropertyStore,+ FfiSubstitutionFunctionVisibility, NativeVarResolution, PreparedCustomFunction, SubstitutionAttribute,VarResolutionInputs, prepare_var_resolution_environment,};use crate::css::parser::component_value::{ComponentKind, ComponentValue};@@ -34,6 +34,8 @@ use crate::css::style_value::{};use custom_property_environments::{CascadedCustomProperty, CustomPropertyName};+static EMPTY_CUSTOM_FUNCTIONS: CustomFunctionRegistry<'static> = CustomFunctionRegistry::empty();+/// The document's media features as the style update a transaction belongs to saw them, which/// `media()` conditions in `if()` read. Copied rather than borrowed: the host's snapshot ends with/// the style update, and a record demanded after it still resolves against these.@@ -325,11 +327,10 @@ impl DocumentFunctionSnapshot {/// The custom functions a node's calls may reach, as the resolver takes them: each definition with/// the declarations its conditions select for the node, and which definition each scope's calls/// name.-pub(super) struct PreparedCustomFunctions {- definitions: Vec<SubstitutionFunctionDefinition>,- visibilities: Vec<CustomFunctionVisibility>,- caller_scope: usize,- /// Whether a selected declaration substitutes `attr()`, which reads the node's attributes.+pub(super) struct PreparedCustomFunctions<'a> {+ registry: CustomFunctionRegistry<'a>,+ /// Whether a parameter default or selected declaration substitutes `attr()`, which reads the+ /// node's attributes.pub(super) reads_attributes: bool,/// What the container conditions read of the node's containers, for the host to record with/// its record; `None` when no condition was evaluated.@@ -360,7 +361,7 @@ fn engine_resolution_context<'a>(media_environment: &'a crate::css::parser::query_parser::FfiMediaEnvironment<'a>,style_query: Option<&'a crate::css::cascaded_properties::FfiStyleQueryInputs>,style_query_references: Option<&'a mut Option<Box<crate::css::custom_properties::StyleQueryDependencies>>>,- functions: Option<&'a PreparedCustomFunctions>,+ functions: Option<&'a PreparedCustomFunctions<'a>>,) -> CascadeResolutionContext<'a> {CascadeResolutionContext {inputs: VarResolutionInputs {@@ -374,9 +375,7 @@ fn engine_resolution_context<'a>(style_query_inputs: style_query,},attributes: attributes.map_or(&[][..], |attributes| attributes.attributes.as_slice()),- custom_functions: functions.map_or(&[][..], |functions| functions.definitions.as_slice()),- custom_function_scope_identity: functions.map_or(0, |functions| functions.caller_scope),- custom_function_visibilities: functions.map_or(&[][..], |functions| functions.visibilities.as_slice()),+ custom_functions: functions.map_or(&EMPTY_CUSTOM_FUNCTIONS, |functions| &functions.registry),style_query_dependencies: style_query_references,}}@@ -643,7 +642,7 @@ pub(super) struct SubstitutionInputs<'a> {/// Where the custom properties a `style()` query reads are noted.pub style_query_references:Option<&'a std::cell::RefCell<Option<Box<crate::css::custom_properties::StyleQueryDependencies>>>>,- pub functions: Option<&'a PreparedCustomFunctions>,+ pub functions: Option<&'a PreparedCustomFunctions<'a>>,}/// What an `attr()` reads of an element, as the resolution takes it: each of the element's@@ -1374,23 +1373,19 @@ impl RetainedState {/// resolve against: the functions its tree scope's calls reach, each with the blocks of/// declarations whose `@media` conditions hold for the transaction and whose container/// conditions hold for the node. `None` when the engine cannot decide a container condition.- pub(super) fn prepare_custom_functions(+ pub(super) fn prepare_custom_functions<'a>(&self,+ snapshot: &'a DocumentFunctionSnapshot,node: StyleNodeID,pseudo: Option<u8>,- ) -> Option<PreparedCustomFunctions> {- let (caller_scope, visible) = self.document_functions.scope_functions(self.tree.tree_scope(node));+ ) -> Option<PreparedCustomFunctions<'a>> {+ let (caller_scope, visible) = snapshot.scope_functions(self.tree.tree_scope(node));let visible_definitions = visible.map_or(&[][..], |visible| visible.definitions.as_slice());let mut container_effects: Option<super::container_queries::ContainerVerdict> = None;let mut reads_attributes = false;- let mut declarations = Vec::with_capacity(visible_definitions.len());- for (function, _, selected_inputs) in visible_definitions {- reads_attributes |= function- .signature- .parameters- .iter()- .filter_map(|parameter| parameter.default_value.as_deref())- .any(value_reads_attributes);+ let mut definitions = Vec::with_capacity(visible_definitions.len());+ for (function, scope, selected_inputs) in visible_definitions {+ reads_attributes |= function.parameter_defaults_read_attributes;let mut selected = Vec::new();for input in selected_inputs.iter().map(|&index| &function.inputs[index]) {if !input.containers.is_empty() {@@ -1401,30 +1396,23 @@ impl RetainedState {continue;}}- for descriptor in &input.declarations.descriptors {- reads_attributes |= value_reads_attributes(&descriptor.value);- selected.push(SubstitutionFunctionDeclaration {- name: descriptor.name.clone(),- value: descriptor.value.clone(),- });- }+ reads_attributes |= input.reads_attributes;+ selected.extend(&input.declarations);}- declarations.push(selected);- }- let definitions = visible_definitions- .iter()- .zip(declarations)- .map(|((function, scope, _), declarations)| SubstitutionFunctionDefinition {+ definitions.push(PreparedCustomFunction {identity: function.identity,scope_identity: *scope,- signature: function.signature.clone(),- declarations,- })- .collect();+ signature: &function.signature,+ parameter_defaults: &function.parameter_defaults,+ declarations: selected,+ });+ }Some(PreparedCustomFunctions {- definitions,- visibilities: visible.map_or_else(Vec::new, |visible| visible.visibilities.clone()),- caller_scope,+ registry: CustomFunctionRegistry {+ definitions,+ visible: visible.map_or(&[][..], |visible| visible.visibilities.as_slice()),+ caller_scope_identity: caller_scope,+ },reads_attributes,container_effects,})@@ -1709,7 +1697,7 @@ impl RetainedState {{// A container condition the engine cannot decide asks about an ancestor the host styles// in this update: the row waits for it.- let Some(mut functions) = self.prepare_custom_functions(node, pseudo) else {+ let Some(mut functions) = self.prepare_custom_functions(&self.document_functions, node, pseudo) else {self.counters.bump(Counter::EngineComputedRecordBailRecordParent);return Err(Unanswered::AwaitsParent);};@@ -2031,7 +2019,7 @@ impl RetainedState {.any(|(_, _, value)| value_calls_custom_functions(value.data())){// A container condition the engine cannot decide leaves the declarations as they resolved.- let Some(functions) = self.prepare_custom_functions(node, pseudo) else {+ let Some(functions) = self.prepare_custom_functions(&self.document_functions, node, pseudo) else {return Vec::new();};Some(functions)@@ -2153,7 +2141,7 @@ impl RetainedState {) -> RetainedStyleValueData {let calls_functions = value_calls_custom_functions(written.data());let functions = calls_functions- .then(|| self.prepare_custom_functions(node, pseudo_kind))+ .then(|| self.prepare_custom_functions(&self.document_functions, node, pseudo_kind)).flatten();let reads_attributes = value_reads_attributes(written.data())|| functions.as_ref().is_some_and(|functions| functions.reads_attributes);@@ -2283,9 +2271,7 @@ fn substitute_against_stores(let media_environment = inputs.media.environment();let mut resolution_environment = prepare_var_resolution_environment(attributes.map_or(&[][..], |attributes| attributes.attributes.as_slice()),- functions.map_or(&[][..], |functions| functions.definitions.as_slice()),- functions.map_or(0, |functions| functions.caller_scope),- functions.map_or(&[][..], |functions| functions.visibilities.as_slice()),+ functions.map_or(&EMPTY_CUSTOM_FUNCTIONS, |functions| &functions.registry),);let mut style_query_references = inputs.style_query_references.map(std::cell::RefCell::borrow_mut);let resolution_inputs = VarResolutionInputs {diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rsindex d1fe5afa8d7..9cf8567df3d 100644--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs@@ -3458,7 +3458,7 @@ impl RetainedState {// reaches, likewise.let mut style_query = None;let style_query_references = std::cell::RefCell::new(None);- let mut functions: Option<custom_property_cascade::PreparedCustomFunctions> = None;+ let mut functions: Option<custom_property_cascade::PreparedCustomFunctions<'_>> = None;// Whether a property rolled back below a substituted revert keyword: what it rolled back to// is decided by declarations the winners do not name.let mut rolled_back = false;@@ -3551,7 +3551,8 @@ impl RetainedState {if calls_functions && functions.is_none() {// A container condition the engine cannot decide asks about an ancestor the// host styles in this update: the row waits for it.- let Some(prepared) = self.prepare_custom_functions(node, pseudo_kind) else {+ let Some(prepared) = self.prepare_custom_functions(&self.document_functions, node, pseudo_kind)+ else {self.counters.bump(Counter::EngineComputedRecordBailRecordParent);return Err(Unanswered::AwaitsParent);};diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs b/Libraries/LibWeb/Rust/src/css/style/publication/demand.rsindex f5447f44e6e..443f4101349 100644--- a/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs+++ b/Libraries/LibWeb/Rust/src/css/style/publication/demand.rs@@ -678,7 +678,10 @@ impl RetainedState {Some(substituted_property) => {let calls_functions = custom_property_cascade::value_calls_custom_functions(written.data());if calls_functions && functions.is_none() {- functions = Some(self.prepare_custom_functions(subject, None).or_refused()?);+ functions = Some(+ self.prepare_custom_functions(&self.document_functions, subject, None)+ .or_refused()?,+ );}if style_query.is_none()&& (calls_functions || custom_property_cascade::value_reads_conditions(written.data()))