mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
JavaScript: a walked string counts its flattened copy against the heap
In a realm made without the ECMAScript 2015 built-ins and later switched to ECMAScript 2015 code, `for`-`of`, spread and array patterns walk a string by code point. The walk flattened a concatenated string before any limit was checked; it now flattens through the counted helper that `String.prototype[Symbol.iterator]` uses, so the run ends with the heap limit before the copy is made. The rope test also covers array patterns and `for`-`of`, in both kinds of realm. The crate docs now say that such a realm walks arrays and `arguments` objects by index and strings by code point.
This commit is contained in:
1 parent
51565e30cc
commit
f9d7fd23b4
3 files changed
+11
-4
No files matched your search
@@ -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");
|
||||
|
||||
@@ -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<ObjId> {
|
||||
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")),
|
||||
};
|
||||
|
||||
@@ -59,7 +59,8 @@
|
||||
//! Spread, `for … of` and array patterns use the iterator protocol of the 11th edition
|
||||
//! (<https://262.ecma-international.org/11.0/>, 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,
|
||||
|
||||
Reference in new issue
Block a user