From a8174f2ae5cfd1dbe30f7a8867fa3922f6ac59da Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 11:33:43 -0800 Subject: [PATCH] JavaScript: spreading a string counts the strings it makes for its characters Spreading a string makes a new one- or two-unit string for each code point, but the spread held only the bytes of each element's property, so a long string could make the host build every character string past the heap limit in one instruction. Each element now holds the bytes of its string as well, and the strings stay in the heap estimate once the array holds them. --- crates/js/src/es2015_tests.rs | 40 +++++++++++++++++++++++++++++++++++ crates/js/src/interp.rs | 12 ++++++++--- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/crates/js/src/es2015_tests.rs b/crates/js/src/es2015_tests.rs index d717ba2090..f7b704b210 100644 --- a/crates/js/src/es2015_tests.rs +++ b/crates/js/src/es2015_tests.rs @@ -513,6 +513,46 @@ fn spreading_a_rope_counts_its_flattened_length_first() { assert_eq!(made, Ok(JsValue::Number(0.0)), "the rope is made within the fuel"); } +#[test] +fn spreading_a_string_counts_the_string_it_makes_for_each_character() { + // Each of the 16,384 elements is a property and a one-character string of its own. The + // smaller heap has room for the properties alone but not for the strings as well, so the + // spread stops with the heap limit after about 83% of the characters, before the fuel for + // 90% of them runs out. The larger heap holds the array, its keys and the strings. + const N: usize = 16_384; + let property = crate::heap::PROP_BYTES; + let string = crate::heap::string_bytes(1); + let setup = "var s = 'x'; for (var i = 0; i < 14; i++) s = s + s; s[0]; 0"; + let cases = [(property + string / 2, N * 9 / 10, Err(LimitKind::Heap)), (2 * (property + string) + 8, N * 2, Ok(N as f64))]; + for (per_element, fuel, expected) in cases { + let mut r = es(); + assert_eq!(r.run(setup), Ok(JsValue::Number(0.0))); + r.interp().collect_garbage(); + let heap_bytes = r.interp().heap.bytes + N * per_element; + r.set_limits(Limits { heap_bytes, fuel: fuel as u64, ..Limits::default() }); + match (r.run("var a = [...s]; a.length"), expected) { + (Ok(JsValue::Number(n)), Ok(m)) if n == m => {} + (Err(e), Err(kind)) if e.kind == RunErrorKind::Limit(kind) => {} + (result, expected) => panic!("{per_element} bytes for each element: {result:?}, expected {expected:?}"), + } + } +} + +#[test] +fn the_strings_a_string_spread_makes_stay_counted_while_the_array_holds_them() { + const N: u32 = 4096; + let mut r = es(); + assert_eq!(r.run("var s = 'x'; for (var i = 0; i < 12; i++) s = s + s; s[0]; 0"), Ok(JsValue::Number(0.0))); + r.interp().collect_garbage(); + let before = r.interp().heap.bytes; + assert_eq!(r.run("var a = [...s]; a.length"), Ok(JsValue::Number(f64::from(N)))); + // Takes the bytes the spread held while it ran off the estimate. + r.interp().reserve_heap(0).unwrap(); + let element = |i: u32| crate::heap::PROP_BYTES + crate::heap::string_bytes(i.to_string().len()) + crate::heap::string_bytes(1); + let least = before + (0..N).map(element).sum::(); + assert!(r.interp().heap.bytes >= least, "{} < {least}", r.interp().heap.bytes); +} + #[test] fn a_rest_parameter_checks_the_heap_before_its_array_is_made() { // The 20,000 arguments fit in the room left over, and the rest array of them does not, so diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index 0b75dab588..286302d021 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -2404,8 +2404,9 @@ impl Interp { /// index (holes read as `undefined`), with its length read again before each one as its /// iterator reads it, a string's or a `String` object's characters by code point, and an /// object with a `length` by index. Any other value is a `TypeError`. Each element takes a - /// unit of fuel, and a concatenated string's flattened copy is counted against the heap before - /// it is made. + /// unit of fuel, a concatenated string's flattened copy is counted against the heap before + /// it is made, and so is each string a string's spread makes for its characters, while the + /// spread runs and after the array holds it. fn op_array_spread(&mut self) -> JsResult<()> { let v = self.peek(0)?; let Value::Object(a) = self.peek(1)? else { return internal("spread into something that is not an array") }; @@ -2417,6 +2418,8 @@ impl Interp { }, _ => None, }; + // The bytes of the strings a string's spread makes, which the array keeps once it holds them. + let mut made = 0usize; let items: Vec = match boxed.as_ref().unwrap_or(&v) { Value::String(s) => { let units = crate::string_builtins::flat_charged(self, s)?; @@ -2425,9 +2428,11 @@ impl Interp { let mut i = 0; while let Some(&u) = units.get(i) { self.use_fuel()?; - held.add(self, PROP_BYTES)?; let pair = (0xD800..0xDC00).contains(&u) && units.get(i + 1).is_some_and(|n| (0xDC00..0xE000).contains(n)); let len = if pair { 2 } else { 1 }; + // Each element is a new string of its own as well as a property. + held.add(self, PROP_BYTES + string_bytes(len))?; + made = made.saturating_add(string_bytes(len)); out.push(Value::String(Str::from_units(units.get(i..i + len).unwrap_or_default().to_vec()))); i += len; } @@ -2490,6 +2495,7 @@ impl Interp { for (i, item) in (start..end).zip(items) { self.define(a, index_key(i), Prop::Data { value: item, flags: OPEN })?; } + self.heap.bytes = self.heap.bytes.saturating_add(made); self.pop().map(drop) }