From a3545036834833840c525fae9031fff37a4bdf32 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 11 Nov 2026 14:54:26 -0800 Subject: [PATCH] JavaScript: XML namespace lookups take fuel for each ancestor they climb `inScopeNamespaces()`, `namespaceDeclarations()` and `namespace()` walk from the node up to its root. The walk cloned each ancestor's whole node, children and attributes included, to read its namespaces, and charged fuel only for the namespaces it found, so a deep tree with no declared namespaces cost no fuel. It now takes one unit of fuel per ancestor, copies only the namespace list and parent link, and checks prefixes already seen with a set. `namespaceDeclarations()` compares the node's declarations with the ancestors' through a map instead of a scan. --- crates/js/src/xml_builtins.rs | 24 +++++++++++++++--------- crates/js/src/xml_tests.rs | 10 ++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/crates/js/src/xml_builtins.rs b/crates/js/src/xml_builtins.rs index 07e2a1d12e..c74b9fac2e 100644 --- a/crates/js/src/xml_builtins.rs +++ b/crates/js/src/xml_builtins.rs @@ -1,6 +1,7 @@ //! The E4X built-ins (ECMA-357 sections 13.1 to 13.5): `XML`, `XMLList`, `Namespace`, `QName`, //! `isXMLName` and `setDefaultXMLNamespace`. A realm has them only with [`crate::Options::e4x`]. +use std::collections::{HashMap, HashSet}; use std::rc::Rc; use crate::builtins::{arg, constructor, data, method, native}; @@ -662,19 +663,25 @@ fn has_simple_content(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> } } -/// The namespaces in scope at `x`, nearest declarations first, one for each prefix. +/// The namespaces in scope at `x`, nearest declarations first, one for each prefix. Takes one unit +/// of fuel for each node it climbs through and for each declaration it reads. fn scope_of(interp: &mut Interp, x: Option) -> JsResult> { let mut out: Vec = Vec::new(); + let mut seen: HashSet> = HashSet::new(); let mut cur = x; while let Some(y) = cur { - let node = interp.xml_node(y)?.clone(); - for ns in &node.ns { + interp.use_fuel()?; + let (list, parent) = { + let node = interp.xml_node(y)?; + (node.ns.clone(), node.parent) + }; + for ns in list { interp.use_fuel()?; - if !out.iter().any(|n| n.prefix == ns.prefix) { - out.push(ns.clone()); + if seen.insert(ns.prefix.clone()) { + out.push(ns); } } - cur = node.parent; + cur = parent; } Ok(out) } @@ -702,9 +709,8 @@ fn namespace_declarations(interp: &mut Interp, this: Value, _: &[Value], _: bool return namespaces_array(interp, Vec::new()); } let parent = interp.xml_node(x)?.parent; - let ancestors = scope_of(interp, parent)?; - let declared: Vec = - interp.xml_node(x)?.ns.iter().filter(|ns| !ancestors.iter().any(|a| a.prefix == ns.prefix && a.uri == ns.uri)).cloned().collect(); + let ancestors: HashMap, Key> = scope_of(interp, parent)?.into_iter().map(|a| (a.prefix, a.uri)).collect(); + let declared: Vec = interp.xml_node(x)?.ns.iter().filter(|ns| ancestors.get(&ns.prefix) != Some(&ns.uri)).cloned().collect(); namespaces_array(interp, declared) } diff --git a/crates/js/src/xml_tests.rs b/crates/js/src/xml_tests.rs index dce24d9d95..cfe508a483 100644 --- a/crates/js/src/xml_tests.rs +++ b/crates/js/src/xml_tests.rs @@ -740,6 +740,16 @@ fn insert_child_before_and_after_take_fuel_for_each_child_they_scan() { } } +#[test] +fn namespace_lookups_take_fuel_for_each_ancestor_they_climb() { + let shallow = "var x = ; var leaf = x.children()[0];"; + let deep = "var x = ; var leaf = x; for (var i = 0; i < 150; i++) { var c = ; leaf.appendChild(c); leaf = c; }"; + for action in ["leaf.inScopeNamespaces()", "leaf.namespaceDeclarations()", "leaf.namespace()", "leaf.namespace('p')"] { + let (s, d) = (fuel_of(shallow, action), fuel_of(deep, action)); + assert!(d >= s + 149, "{action}: {s} one level down, {d} 150 levels down"); + } +} + #[test] fn limits_cannot_be_caught_by_the_script() { assert_eq!(kind_of(TIGHT, "try { for (;;) ().toXMLString(); } catch (e) { 1 }"), RunErrorKind::Limit(LimitKind::Fuel));