mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 18:57:42 +00:00
LibJS: Object.preventExtensions should allow property modfication
Existing properties on a non-extensible object should be changable and deletable.
This commit is contained in:
parent
bfbd6df892
commit
93ebd320ef
3 changed files with 39 additions and 17 deletions
|
@ -462,7 +462,10 @@ bool Object::put_own_property(Object& this_object, const FlyString& property_nam
|
||||||
{
|
{
|
||||||
ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
|
ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
|
||||||
|
|
||||||
if (!is_extensible()) {
|
auto metadata = shape().lookup(property_name);
|
||||||
|
bool new_property = !metadata.has_value();
|
||||||
|
|
||||||
|
if (!is_extensible() && new_property) {
|
||||||
#ifdef OBJECT_DEBUG
|
#ifdef OBJECT_DEBUG
|
||||||
dbg() << "Disallow define_property of non-extensible object";
|
dbg() << "Disallow define_property of non-extensible object";
|
||||||
#endif
|
#endif
|
||||||
|
@ -479,8 +482,6 @@ bool Object::put_own_property(Object& this_object, const FlyString& property_nam
|
||||||
attributes.set_has_setter();
|
attributes.set_has_setter();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto metadata = shape().lookup(property_name);
|
|
||||||
bool new_property = !metadata.has_value();
|
|
||||||
|
|
||||||
if (new_property) {
|
if (new_property) {
|
||||||
if (!m_shape->is_unique() && shape().property_count() > 100) {
|
if (!m_shape->is_unique() && shape().property_count() > 100) {
|
||||||
|
@ -544,7 +545,10 @@ bool Object::put_own_property_by_index(Object& this_object, u32 property_index,
|
||||||
{
|
{
|
||||||
ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
|
ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
|
||||||
|
|
||||||
if (!is_extensible()) {
|
auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
|
||||||
|
auto new_property = !existing_property.has_value();
|
||||||
|
|
||||||
|
if (!is_extensible() && new_property) {
|
||||||
#ifdef OBJECT_DEBUG
|
#ifdef OBJECT_DEBUG
|
||||||
dbg() << "Disallow define_property of non-extensible object";
|
dbg() << "Disallow define_property of non-extensible object";
|
||||||
#endif
|
#endif
|
||||||
|
@ -561,8 +565,6 @@ bool Object::put_own_property_by_index(Object& this_object, u32 property_index,
|
||||||
attributes.set_has_setter();
|
attributes.set_has_setter();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
|
|
||||||
auto new_property = !existing_property.has_value();
|
|
||||||
PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;
|
PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;
|
||||||
|
|
||||||
if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
|
if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
|
||||||
|
|
|
@ -24,6 +24,20 @@ describe("correct behavior", () => {
|
||||||
o.baz = "baz";
|
o.baz = "baz";
|
||||||
expect(o.baz).toBeUndefined();
|
expect(o.baz).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("modifying existing properties", () => {
|
||||||
|
const o = { foo: "bar" };
|
||||||
|
Object.preventExtensions(o);
|
||||||
|
o.foo = "baz";
|
||||||
|
expect(o.foo).toBe("baz");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("deleting existing properties", () => {
|
||||||
|
const o = { foo: "bar" };
|
||||||
|
Object.preventExtensions(o);
|
||||||
|
delete o.foo;
|
||||||
|
expect(o).not.toHaveProperty("foo");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("errors", () => {
|
describe("errors", () => {
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
test("length is 1", () => {
|
|
||||||
expect(Reflect.preventExtensions).toHaveLength(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("errors", () => {
|
describe("errors", () => {
|
||||||
test("target must be an object", () => {
|
test("target must be an object", () => {
|
||||||
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
|
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
|
||||||
|
@ -16,6 +12,10 @@ describe("errors", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("normal behavior", () => {
|
describe("normal behavior", () => {
|
||||||
|
test("length is 1", () => {
|
||||||
|
expect(Reflect.preventExtensions).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
test("properties cannot be added", () => {
|
test("properties cannot be added", () => {
|
||||||
var o = {};
|
var o = {};
|
||||||
o.foo = "foo";
|
o.foo = "foo";
|
||||||
|
@ -25,12 +25,18 @@ describe("normal behavior", () => {
|
||||||
expect(o.bar).toBeUndefined();
|
expect(o.bar).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("property values can still be changed", () => {
|
test("modifying existing properties", () => {
|
||||||
// FIXME: This doesn't work even though it should (the value remains unchanged)
|
const o = {};
|
||||||
// var o = {};
|
o.foo = "foo";
|
||||||
// o.foo = "foo";
|
expect(Reflect.preventExtensions(o)).toBeTrue();
|
||||||
// expect(Reflect.preventExtensions(o)).toBeTrue();
|
o.foo = "bar";
|
||||||
// o.foo = "bar";
|
expect(o.foo).toBe("bar");
|
||||||
// expect(o.foo).toBe("bar");
|
});
|
||||||
|
|
||||||
|
test("deleting existing properties", () => {
|
||||||
|
const o = { foo: "bar" };
|
||||||
|
Reflect.preventExtensions(o);
|
||||||
|
delete o.foo;
|
||||||
|
expect(o).not.toHaveProperty("foo");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue