diff --git a/crates/js/src/es2015_tests.rs b/crates/js/src/es2015_tests.rs index 431eb785e2..034033ef23 100644 --- a/crates/js/src/es2015_tests.rs +++ b/crates/js/src/es2015_tests.rs @@ -416,9 +416,14 @@ fn spreading_a_rope_counts_its_flattened_length_first() { let build = "var s = 'x'; for (var i = 0; i < 18; i++) s = s + s; function f(c) { return s + c; } \ function mk() { var a = f('a'); a[0]; return a + 'b'; }"; let limits = Limits { heap_bytes: 4 * 262_146, fuel: 20_000, ..Limits::default() }; - for spread in ["[...mk()]", "Math.max(...mk())"] { + for spread in ["[...mk()]", "Math.max(...mk())", "var [c] = mk()", "for (var c of mk()) break"] { let e = Realm::with_options(es_options(), limits.clone()).run(&format!("{build} {spread}; 0")).unwrap_err(); assert_eq!(e.kind, RunErrorKind::Limit(LimitKind::Heap), "{spread}: {e:?}"); + // A realm without `Symbol.iterator` walks the string, and flattens it the same way. + let mut walking = Realm::with_options(Options::default(), limits.clone()); + walking.set_es2015(true); + let e = walking.run(&format!("{build} {spread}; 0")).unwrap_err(); + assert_eq!(e.kind, RunErrorKind::Limit(LimitKind::Heap), "walking {spread}: {e:?}"); } let made = Realm::with_options(es_options(), limits).run(&format!("{build} mk(); 0")); assert_eq!(made, Ok(JsValue::Number(0.0)), "the rope is made within the fuel"); diff --git a/crates/js/src/iteration.rs b/crates/js/src/iteration.rs index ce19c2be58..ca7e7f453c 100644 --- a/crates/js/src/iteration.rs +++ b/crates/js/src/iteration.rs @@ -77,10 +77,11 @@ impl Interp { } /// The record of a [`Walk`] over an array, an `arguments` object or a string, for a realm - /// without `Symbol.iterator`; any other value is not iterable there. + /// without `Symbol.iterator`; any other value is not iterable there. A concatenated string's + /// flattened copy is counted against the heap before it is made. fn walk_record(&mut self, v: &Value) -> JsResult { let walk = match v { - Value::String(s) => Walk::String(Str::from_rc(s.flat()), 0), + Value::String(s) => Walk::String(Str::from_rc(crate::string_builtins::flat_charged(self, s)?), 0), Value::Object(o) if matches!(self.object(*o)?.class, Class::Array { .. } | Class::Arguments(_)) => Walk::Array(*o, 0.0), _ => return Err(self.type_error("the value is not iterable")), }; diff --git a/crates/js/src/lib.rs b/crates/js/src/lib.rs index 077a0e3f63..7ce77a7a95 100644 --- a/crates/js/src/lib.rs +++ b/crates/js/src/lib.rs @@ -59,7 +59,8 @@ //! Spread, `for … of` and array patterns use the iterator protocol of the 11th edition //! (, section 7.4), and a loop or pattern left early //! closes its iterator. In a realm made without the option and later given it with -//! [`Realm::set_es2015`], which has no `Symbol.iterator`, they walk arrays, `arguments` objects and strings by index and throw a `TypeError` for other values. +//! [`Realm::set_es2015`], which has no `Symbol.iterator`, they walk arrays and `arguments` objects +//! by index and strings by code point, and throw a `TypeError` for other values. //! Object rest properties (`{ a, ...rest }`), classes, generators and modules stay syntax errors. //! //! With [`Options::es2015`], the realm also has `Symbol` (ECMA-262 11th edition, section 19.4,