1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:27:35 +00:00

LibJS: Remove Object::is_array() in favor of Value::is_array() and RTTI

It's way too easy to get this wrong: for the IsArray abstract operation,
Value::is_array() needs to be called. Since we have RTTI, the virtual
Object::is_array() method is not needed anymore - if we need to know
whether something is *actually* a JS::Array (we currently check in more
cases than we should, I think) and not a Proxy with an Array target, we
should do that in a way that doesn't look like an abstract operation.
This commit is contained in:
Linus Groh 2021-07-05 18:58:51 +01:00
parent 06ffc0c4db
commit 0ba81dc0b7
8 changed files with 8 additions and 11 deletions

View file

@ -68,7 +68,7 @@ Array* Array::typed_this(VM& vm, GlobalObject& global_object)
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return {};
if (!this_object->is_array()) {
if (!is<Array>(this_object)) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAn, "Array");
return nullptr;
}

View file

@ -24,8 +24,6 @@ public:
static Array* typed_this(VM&, GlobalObject&);
private:
virtual bool is_array() const override { return true; }
JS_DECLARE_NATIVE_GETTER(length_getter);
JS_DECLARE_NATIVE_SETTER(length_setter);
};

View file

@ -348,7 +348,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::push)
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return {};
if (this_object->is_array()) {
if (is<Array>(this_object)) {
auto* array = static_cast<Array*>(this_object);
for (size_t i = 0; i < vm.argument_count(); ++i)
array->indexed_properties().append(vm.argument(i));
@ -432,7 +432,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::pop)
auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return {};
if (this_object->is_array()) {
if (is<Array>(this_object)) {
auto* array = static_cast<Array*>(this_object);
if (array->indexed_properties().is_empty())
return js_undefined();

View file

@ -132,7 +132,6 @@ public:
void define_native_property(PropertyName const&, Function<Value(VM&, GlobalObject&)> getter, Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attributes);
void define_native_accessor(PropertyName const&, Function<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attributes);
virtual bool is_array() const { return false; }
virtual bool is_function() const { return false; }
virtual bool is_typed_array() const { return false; }
virtual bool is_string_object() const { return false; }

View file

@ -203,7 +203,7 @@ bool Value::is_array(GlobalObject& global_object) const
if (!is_object())
return false;
auto& object = as_object();
if (object.is_array())
if (is<Array>(object))
return true;
if (is<ProxyObject>(object)) {
auto& proxy = static_cast<ProxyObject const&>(object);
@ -219,7 +219,7 @@ bool Value::is_array(GlobalObject& global_object) const
Array& Value::as_array()
{
VERIFY(is_object() && as_object().is_array());
VERIFY(is_object() && is<Array>(as_object()));
return static_cast<Array&>(*m_value.as_object);
}