1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

LibJS: Implement correct attributes for (almost) all properties

Added the ability to include a u8 attributes parameter with all of the
various put methods in the Object class. They can be omitted, in which
case it defaults to "Writable | Enumerable | Configurable", just like
before this commit.

All of the attribute values for each property were gathered from
SpiderMonkey in the Firefox console. Some properties (e.g. all of the
canvas element properties) have undefined property descriptors... not
quite sure what that means. Those were left as the default specified
above.
This commit is contained in:
mattco98 2020-04-27 23:05:02 -07:00 committed by Andreas Kling
parent c4d05049c4
commit 23ec578a01
26 changed files with 189 additions and 169 deletions

View file

@ -116,7 +116,9 @@ void Object::set_shape(Shape& new_shape)
void Object::put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value value, PutOwnPropertyMode mode)
{
auto metadata = shape().lookup(property_name);
if (!metadata.has_value()) {
bool new_property = !metadata.has_value();
if (new_property) {
if (m_shape->is_unique()) {
m_shape->add_property_to_unique_shape(property_name, attributes);
m_storage.resize(m_shape->property_count());
@ -127,7 +129,7 @@ void Object::put_own_property(Object& this_object, const FlyString& property_nam
ASSERT(metadata.has_value());
}
if (mode == PutOwnPropertyMode::DefineProperty && !(metadata.value().attributes & Attribute::Configurable) && attributes != metadata.value().attributes) {
if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !(metadata.value().attributes & Attribute::Configurable) && attributes != metadata.value().attributes) {
dbg() << "Disallow reconfig of non-configurable property";
interpreter().throw_exception<TypeError>(String::format("Cannot redefine property '%s'", property_name.characters()));
return;
@ -144,7 +146,7 @@ void Object::put_own_property(Object& this_object, const FlyString& property_nam
dbg() << "Reconfigured property " << property_name << ", new shape says offset is " << metadata.value().offset << " and my storage capacity is " << m_storage.size();
}
if (mode == PutOwnPropertyMode::Put && !(metadata.value().attributes & Attribute::Writable)) {
if (!new_property && mode == PutOwnPropertyMode::Put && !(metadata.value().attributes & Attribute::Writable)) {
dbg() << "Disallow write to non-writable property";
return;
}
@ -240,24 +242,25 @@ Value Object::get(PropertyName property_name) const
return get(property_name.as_string());
}
void Object::put_by_index(i32 property_index, Value value)
void Object::put_by_index(i32 property_index, Value value, u8 attributes)
{
ASSERT(!value.is_empty());
if (property_index < 0)
return put(String::number(property_index), value);
return put(String::number(property_index), value, attributes);
// FIXME: Implement some kind of sparse storage for arrays with huge indices.
// Also: Take attributes into account here
if (static_cast<size_t>(property_index) >= m_elements.size())
m_elements.resize(property_index + 1);
m_elements[property_index] = value;
}
void Object::put(const FlyString& property_name, Value value)
void Object::put(const FlyString& property_name, Value value, u8 attributes)
{
ASSERT(!value.is_empty());
bool ok;
i32 property_index = property_name.to_int(ok);
if (ok && property_index >= 0)
return put_by_index(property_index, value);
return put_by_index(property_index, value, attributes);
// If there's a setter in the prototype chain, we go to the setter.
// Otherwise, it goes in the own property storage.
@ -278,26 +281,26 @@ void Object::put(const FlyString& property_name, Value value)
}
object = object->prototype();
}
put_own_property(*this, property_name, Attribute::Configurable | Attribute::Enumerable | Attribute::Writable, value, PutOwnPropertyMode::Put);
put_own_property(*this, property_name, attributes, value, PutOwnPropertyMode::Put);
}
void Object::put(PropertyName property_name, Value value)
void Object::put(PropertyName property_name, Value value, u8 attributes)
{
if (property_name.is_number())
return put_by_index(property_name.as_number(), value);
return put(property_name.as_string(), value);
return put_by_index(property_name.as_number(), value, attributes);
return put(property_name.as_string(), value, attributes);
}
void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length)
void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length, u8 attributes)
{
auto* function = NativeFunction::create(interpreter(), interpreter().global_object(), property_name, move(native_function));
function->put("length", Value(length));
put(property_name, function);
function->put("length", Value(length), Attribute::Configurable);
put(property_name, function, attributes);
}
void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter)
void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attributes)
{
put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)));
put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)), attributes);
}
void Object::visit_children(Cell::Visitor& visitor)