JavaScript: spread reads a proxy of an array as an array, and Proxy ignores newTarget

- Spread treats a proxy whose target is an array as an array and reads its
  length again, through the traps, before each element after the first. A
  proxy that its length read revokes spreads as the array that length gives.
- Reflect.construct(Proxy, args, newTarget) does not read
  newTarget.prototype (ECMA-262 2020 section 26.2.1.1).
This commit is contained in:
dotCooCoo
2026-11-22 16:01:17 -08:00
parent 7285bd87a1
commit 6df45292e1
2 changed files with 38 additions and 8 deletions
+19 -7
View File
@@ -2473,14 +2473,16 @@ impl Interp {
}
Value::Object(o) if self.has_property(*o, &key("length"))? => {
let o = *o;
// Tested before any trap reads `length`, which may revoke the proxy.
let array = self.is_array(&Value::Object(o))?;
let len = self.get_from(o, &key("length"), &v)?;
// ToLength (ECMA-262 2015 section 7.1.15): a negative or NaN length reads nothing.
let n = crate::string_builtins::to_integer(self.to_number(&len)?).max(0.0);
// An array's iterator reads its length again before each element (ECMA-262 2015
// section 22.1.5.2.1), so a getter that shrinks or grows the array changes the count,
// and the room is checked as each element is read. Any other object, and an array
// and the room is checked as each element is read. A proxy of an array is read
// the same way, its `length` through its traps. Any other object, and an array
// whose elements no getter reads, is checked against the length it has now.
let array = matches!(self.object(o)?.class, Class::Array { .. });
if n >= f64::from(room) && !(array && self.has_index_getter(o)?) {
return Err(self.throw(ErrorType::Range, "array literal too long"));
}
@@ -2492,8 +2494,17 @@ impl Interp {
let mut read = || -> JsResult<()> {
let mut i = 0;
loop {
let end = match &self.object(o)?.class {
Class::Array { length, .. } if array => *length,
let stored = match &self.object(o)?.class {
Class::Array { length, .. } => Some(*length),
_ => None,
};
let end = match stored {
Some(length) if array => length,
None if array && i > 0 => {
let len = self.get_from(o, &key("length"), &v)?;
let len = crate::string_builtins::to_integer(self.to_number(&len)?).max(0.0);
if len >= f64::from(u32::MAX) { u32::MAX } else { len as u32 }
}
_ => n,
};
if i >= end {
@@ -3042,15 +3053,16 @@ impl Interp {
let obj = self.heap.alloc_object(Object::new(Class::Ordinary, Some(proto)));
return Ok(Prepared::Closure(Entry { callee, code, env, this: Value::Object(obj), args }));
}
Some(Func::Native { f: native, constructor: true, .. }) => {
Some(Func::Native { f: native, constructor: true, name, .. }) => {
// With a different `newTarget` (`Reflect.construct`), the new object takes
// its `prototype` when that is an object (ECMA-262 2020 section 9.1.14). An
// argument given back and a proxy keep their own. `Object` makes a new object
// instead of converting its argument (section 19.1.1.1). `Number`, `String`,
// `Date` and `Function` convert their arguments before `prototype` is read
// (sections 20.1.1.1, 21.1.1.1, 20.4.2 and 19.2.1.1.1), so a conversion that
// throws reads nothing from `newTarget`.
let other_target = !same_value(&f, &new_target);
// throws reads nothing from `newTarget`. `Proxy` does not read it (section
// 26.2.1.1).
let other_target = !same_value(&f, &new_target) && name != "Proxy";
let converts_first = other_target && {
let i = &self.intrinsics;
let own = [i.number_proto, i.string_proto, i.date_proto, i.function_proto];
+19 -1
View File
@@ -355,6 +355,12 @@ fn reflect_construct_reads_new_target_prototype_after_number_and_string_convert(
try { Reflect.construct(String, [bad], nt); } catch (e) { thrown++; }
thrown === 2 && log.length === 0",
);
// `Proxy` does not read `newTarget.prototype`.
truth(
"var nt = new Proxy(function () {}, { get: function (t, k) { if (k === 'prototype') throw new Error('read'); return t[k]; } });
var p = Reflect.construct(Proxy, [{ a: 1 }, {}], nt);
p.a === 1",
);
}
/// Runs `src` with a collection at every heap reservation, and checks that it gives `true`.
@@ -476,7 +482,7 @@ fn for_in_reads_ordinary_and_proxy_names_by_the_same_rules() {
}
#[test]
fn spread_lets_a_proxy_on_the_prototype_chain_shrink_a_long_array() {
fn spread_lets_a_proxy_shrink_the_array_it_reads() {
// A proxy's `get` trap answers for the indices the array does not hold, so the length is not
// refused before the elements are read.
truth(
@@ -485,6 +491,18 @@ fn spread_lets_a_proxy_on_the_prototype_chain_shrink_a_long_array() {
var r = [...a];
r.length === 1 && r[0] === 'v'",
);
// A proxy of an array is spread as an array: its length is read again before each element.
truth(
"var t = [1, 2, 3];
var p = new Proxy(t, { get: function (o, k) { if (k === '0') t.length = 1; return o[k]; } });
var r = [...p];
r.length === 1 && r[0] === 1",
);
// A proxy that its `length` read revokes spreads as the empty array that length gives.
truth(
"var made = Proxy.revocable([], { get: function (t, k) { if (k === 'length') { made.revoke(); return 0; } return t[k]; } });
[...made.proxy].length === 0",
);
// Without a proxy or a getter, the length is refused at once.
let src = "var a = []; a.length = 4294967295; [...a]";
match es().run(src) {