1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-25 21:05:07 +00:00

LibJS: Return a bool from Object::put* to indicate success

This commit is contained in:
Linus Groh 2020-04-30 20:01:54 +01:00 committed by Andreas Kling
parent 6dbb5df81f
commit 4cdd802927
4 changed files with 24 additions and 21 deletions

View file

@ -177,7 +177,7 @@ void Object::set_shape(Shape& new_shape)
m_shape = &new_shape; m_shape = &new_shape;
} }
void Object::put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value value, PutOwnPropertyMode mode) bool Object::put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value value, PutOwnPropertyMode mode)
{ {
auto metadata = shape().lookup(property_name); auto metadata = shape().lookup(property_name);
bool new_property = !metadata.has_value(); bool new_property = !metadata.has_value();
@ -196,7 +196,7 @@ void Object::put_own_property(Object& this_object, const FlyString& property_nam
if (!new_property && 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"; dbg() << "Disallow reconfig of non-configurable property";
interpreter().throw_exception<TypeError>(String::format("Cannot redefine property '%s'", property_name.characters())); interpreter().throw_exception<TypeError>(String::format("Cannot redefine property '%s'", property_name.characters()));
return; return false;
} }
if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) { if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
@ -212,11 +212,11 @@ void Object::put_own_property(Object& this_object, const FlyString& property_nam
if (!new_property && 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"; dbg() << "Disallow write to non-writable property";
return; return false;
} }
if (value.is_empty()) if (value.is_empty())
return; return true;
auto value_here = m_storage[metadata.value().offset]; auto value_here = m_storage[metadata.value().offset];
if (value_here.is_object() && value_here.as_object().is_native_property()) { if (value_here.is_object() && value_here.as_object().is_native_property()) {
@ -229,6 +229,7 @@ void Object::put_own_property(Object& this_object, const FlyString& property_nam
} else { } else {
m_storage[metadata.value().offset] = value; m_storage[metadata.value().offset] = value;
} }
return true;
} }
Value Object::delete_property(PropertyName property_name) Value Object::delete_property(PropertyName property_name)
@ -306,7 +307,7 @@ Value Object::get(PropertyName property_name) const
return get(property_name.as_string()); return get(property_name.as_string());
} }
void Object::put_by_index(i32 property_index, Value value, u8 attributes) bool Object::put_by_index(i32 property_index, Value value, u8 attributes)
{ {
ASSERT(!value.is_empty()); ASSERT(!value.is_empty());
if (property_index < 0) if (property_index < 0)
@ -316,9 +317,10 @@ void Object::put_by_index(i32 property_index, Value value, u8 attributes)
if (static_cast<size_t>(property_index) >= m_elements.size()) if (static_cast<size_t>(property_index) >= m_elements.size())
m_elements.resize(property_index + 1); m_elements.resize(property_index + 1);
m_elements[property_index] = value; m_elements[property_index] = value;
return true;
} }
void Object::put(const FlyString& property_name, Value value, u8 attributes) bool Object::put(const FlyString& property_name, Value value, u8 attributes)
{ {
ASSERT(!value.is_empty()); ASSERT(!value.is_empty());
bool ok; bool ok;
@ -340,31 +342,31 @@ void Object::put(const FlyString& property_name, Value value, u8 attributes)
call_frame.this_value = this; call_frame.this_value = this;
native_property.set(interpreter, value); native_property.set(interpreter, value);
interpreter.pop_call_frame(); interpreter.pop_call_frame();
return; return true;
} }
} }
object = object->prototype(); object = object->prototype();
} }
put_own_property(*this, property_name, attributes, value, PutOwnPropertyMode::Put); return put_own_property(*this, property_name, attributes, value, PutOwnPropertyMode::Put);
} }
void Object::put(PropertyName property_name, Value value, u8 attributes) bool Object::put(PropertyName property_name, Value value, u8 attributes)
{ {
if (property_name.is_number()) if (property_name.is_number())
return put_by_index(property_name.as_number(), value, attributes); return put_by_index(property_name.as_number(), value, attributes);
return put(property_name.as_string(), 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, u8 attributes) bool 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)); auto* function = NativeFunction::create(interpreter(), interpreter().global_object(), property_name, move(native_function));
function->put("length", Value(length), Attribute::Configurable); function->put("length", Value(length), Attribute::Configurable);
put(property_name, function, attributes); return 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, u8 attributes) bool 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)), attributes); return put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)), attributes);
} }
void Object::visit_children(Cell::Visitor& visitor) void Object::visit_children(Cell::Visitor& visitor)

View file

@ -55,9 +55,9 @@ public:
Value get(const FlyString& property_name) const; Value get(const FlyString& property_name) const;
Value get(PropertyName) const; Value get(PropertyName) const;
virtual void put_by_index(i32 property_index, Value, u8 attributes = default_attributes); virtual bool put_by_index(i32 property_index, Value, u8 attributes = default_attributes);
void put(const FlyString& property_name, Value, u8 attributes = default_attributes); bool put(const FlyString& property_name, Value, u8 attributes = default_attributes);
void put(PropertyName, Value, u8 attributes = default_attributes); bool put(PropertyName, Value, u8 attributes = default_attributes);
Value get_own_property(const Object& this_object, const FlyString& property_name) const; Value get_own_property(const Object& this_object, const FlyString& property_name) const;
@ -73,10 +73,10 @@ public:
DefineProperty, DefineProperty,
}; };
void put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value, PutOwnPropertyMode); bool put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value, PutOwnPropertyMode);
void put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>, i32 length = 0, u8 attribute = default_attributes); bool put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>, i32 length = 0, u8 attribute = default_attributes);
void put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute = default_attributes); bool put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute = default_attributes);
virtual bool is_array() const { return false; } virtual bool is_array() const { return false; }
virtual bool is_boolean() const { return false; } virtual bool is_boolean() const { return false; }

View file

@ -65,12 +65,13 @@ Value Uint8ClampedArray::length_getter(Interpreter& interpreter)
return Value(static_cast<const Uint8ClampedArray*>(this_object)->length()); return Value(static_cast<const Uint8ClampedArray*>(this_object)->length());
} }
void Uint8ClampedArray::put_by_index(i32 property_index, Value value, u8) bool Uint8ClampedArray::put_by_index(i32 property_index, Value value, u8)
{ {
// FIXME: Use attributes // FIXME: Use attributes
ASSERT(property_index >= 0); ASSERT(property_index >= 0);
ASSERT(property_index < m_length); ASSERT(property_index < m_length);
m_data[property_index] = clamp(value.to_i32(), 0, 255); m_data[property_index] = clamp(value.to_i32(), 0, 255);
return true;
} }
Value Uint8ClampedArray::get_by_index(i32 property_index) const Value Uint8ClampedArray::get_by_index(i32 property_index) const

View file

@ -39,7 +39,7 @@ public:
i32 length() const { return m_length; } i32 length() const { return m_length; }
virtual void put_by_index(i32 property_index, Value value, u8 attribute = default_attributes) override; virtual bool put_by_index(i32 property_index, Value value, u8 attribute = default_attributes) override;
virtual Value get_by_index(i32 property_index) const override; virtual Value get_by_index(i32 property_index) const override;
u8* data() { return m_data; } u8* data() { return m_data; }