Skip to content

Truly independent web browser

LibJS: Recheck enumerability per key in Object.entries and values

Shannon Booth85de8d709187
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index a7fa92a8d77..f0df84048e3 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -52,7 +52,7 @@ use crate::runtime::property_attributes::{Attribute, DEFAULT_ATTRIBUTES, Propert
use crate::runtime::property_descriptor::{PropertyDescriptor, to_property_descriptor};
use crate::runtime::property_key::{NonSymbolPropertyKey, PropertyKey};
use crate::runtime::realm::Realm;
-use crate::runtime::shape::{AssignCopy, Shape, ShapeKey};
+use crate::runtime::shape::{AssignCopy, Shape};
use crate::runtime::special_property_cache::{self, SpecialPropertyKey};
use crate::runtime::symbol::Symbol;
use crate::runtime::value::{PreferredType, is_strictly_equal, same_value};
@@ -1317,15 +1317,14 @@ impl Object {
// 2. Let properties be a new empty List.
let properties = MarkedVec::with_capacity(vm, self.own_properties_count());
- let pre_iteration_shape = ShapeKey::of(self.shape());
+ let enumerability_is_snapshot = self.eligible_for_own_property_enumeration_fast_path();
self.for_each_own_property_with_enumerability(vm, |property_key, enumerable| {
// a. If Type(key) is String, then
// i. Let desc be ? O.[[GetOwnProperty]](key).
// ii. If desc is not undefined and desc.[[Enumerable]] is true, then
- // NOTE: If the object's shape has been mutated during iteration through own properties
- // by executing a getter, we can no longer assume that subsequent properties
- // are still present and enumerable.
- if ShapeKey::of(self.shape()) == pre_iteration_shape {
+ // NB: On the fast path, `enumerable` was read for every property before the loop, and a getter run by Get
+ // can make it stale. Otherwise it was read for this property just now.
+ if kind == PropertyKind::Key || !enumerability_is_snapshot {
if !enumerable {
return Ok(());
}
diff --git a/Tests/LibJS/Runtime/builtins/Object/Object.entries.js b/Tests/LibJS/Runtime/builtins/Object/Object.entries.js
index 89fc34d5019..86c0319d235 100644
--- a/Tests/LibJS/Runtime/builtins/Object/Object.entries.js
+++ b/Tests/LibJS/Runtime/builtins/Object/Object.entries.js
@@ -77,6 +77,32 @@ describe("basic functionality", () => {
expect(Object.entries(obj)).toEqual([["a", 1]]);
});
+
+ test("indexed property changed by a getter", () => {
+ const deleted = {};
+ Object.defineProperty(deleted, 0, {
+ get() {
+ delete deleted[1];
+ return "a";
+ },
+ enumerable: true,
+ configurable: true,
+ });
+ deleted[1] = "b";
+ expect(Object.entries(deleted)).toEqual([["0", "a"]]);
+
+ const hidden = {};
+ Object.defineProperty(hidden, 0, {
+ get() {
+ Object.defineProperty(hidden, 1, { enumerable: false });
+ return "a";
+ },
+ enumerable: true,
+ configurable: true,
+ });
+ hidden[1] = "b";
+ expect(Object.entries(hidden)).toEqual([["0", "a"]]);
+ });
});
describe("errors", () => {
diff --git a/Tests/LibJS/Runtime/builtins/Object/Object.values.js b/Tests/LibJS/Runtime/builtins/Object/Object.values.js
index d4c45f5e2ce..8521924da95 100644
--- a/Tests/LibJS/Runtime/builtins/Object/Object.values.js
+++ b/Tests/LibJS/Runtime/builtins/Object/Object.values.js
@@ -53,6 +53,21 @@ describe("correct behavior", () => {
expect(exposesProtectedValue(63)).toBeFalse();
expect(exposesProtectedValue(62)).toBeFalse();
});
+
+ test("rechecks indexed properties after getter side effects", () => {
+ const array = [];
+ Object.defineProperty(array, 0, {
+ get() {
+ Object.defineProperty(array, 1, { enumerable: false });
+ return "a";
+ },
+ enumerable: true,
+ configurable: true,
+ });
+ array[1] = "b";
+ array[2] = "c";
+ expect(Object.values(array)).toEqual(["a", "c"]);
+ });
});
describe("errors", () => {