1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

LibJS: Convert delete_property_or_throw() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-10-03 01:47:21 +01:00
parent fe86b04b42
commit a29b7a3ec7
4 changed files with 19 additions and 48 deletions

View file

@ -369,9 +369,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::unshift)
auto from_value = TRY_OR_DISCARD(this_object->get(from)); auto from_value = TRY_OR_DISCARD(this_object->get(from));
TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
} else { } else {
this_object->delete_property_or_throw(to); TRY_OR_DISCARD(this_object->delete_property_or_throw(to));
if (vm.exception())
return {};
} }
} }
@ -396,9 +394,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::pop)
} }
auto index = length - 1; auto index = length - 1;
auto element = TRY_OR_DISCARD(this_object->get(index)); auto element = TRY_OR_DISCARD(this_object->get(index));
this_object->delete_property_or_throw(index); TRY_OR_DISCARD(this_object->delete_property_or_throw(index));
if (vm.exception())
return {};
TRY_OR_DISCARD(this_object->set(vm.names.length, Value(index), Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(vm.names.length, Value(index), Object::ShouldThrowExceptions::Yes));
return element; return element;
} }
@ -425,16 +421,11 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::shift)
auto from_value = TRY_OR_DISCARD(this_object->get(from)); auto from_value = TRY_OR_DISCARD(this_object->get(from));
TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
} else { } else {
this_object->delete_property_or_throw(to); TRY_OR_DISCARD(this_object->delete_property_or_throw(to));
if (vm.exception())
return {};
} }
} }
this_object->delete_property_or_throw(length - 1); TRY_OR_DISCARD(this_object->delete_property_or_throw(length - 1));
if (vm.exception())
return {};
TRY_OR_DISCARD(this_object->set(vm.names.length, Value(length - 1), Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(vm.names.length, Value(length - 1), Object::ShouldThrowExceptions::Yes));
return first; return first;
} }
@ -976,13 +967,9 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reverse)
TRY_OR_DISCARD(this_object->set(upper, lower_value, Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(upper, lower_value, Object::ShouldThrowExceptions::Yes));
} else if (!lower_exists && upper_exists) { } else if (!lower_exists && upper_exists) {
TRY_OR_DISCARD(this_object->set(lower, upper_value, Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(lower, upper_value, Object::ShouldThrowExceptions::Yes));
this_object->delete_property_or_throw(upper); TRY_OR_DISCARD(this_object->delete_property_or_throw(upper));
if (vm.exception())
return {};
} else if (lower_exists && !upper_exists) { } else if (lower_exists && !upper_exists) {
this_object->delete_property_or_throw(lower); TRY_OR_DISCARD(this_object->delete_property_or_throw(lower));
if (vm.exception())
return {};
TRY_OR_DISCARD(this_object->set(upper, lower_value, Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(upper, lower_value, Object::ShouldThrowExceptions::Yes));
} }
} }
@ -1141,11 +1128,8 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::sort)
// The empty parts of the array are always sorted to the end, regardless of the // The empty parts of the array are always sorted to the end, regardless of the
// compare function. FIXME: For performance, a similar process could be used // compare function. FIXME: For performance, a similar process could be used
// for undefined, which are sorted to right before the empty values. // for undefined, which are sorted to right before the empty values.
for (size_t j = items.size(); j < length; ++j) { for (size_t j = items.size(); j < length; ++j)
object->delete_property_or_throw(j); TRY_OR_DISCARD(object->delete_property_or_throw(j));
if (vm.exception())
return {};
}
return object; return object;
} }
@ -1609,17 +1593,12 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice)
auto from_value = TRY_OR_DISCARD(this_object->get(from)); auto from_value = TRY_OR_DISCARD(this_object->get(from));
TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
} else { } else {
this_object->delete_property_or_throw(to); TRY_OR_DISCARD(this_object->delete_property_or_throw(to));
if (vm.exception())
return {};
} }
} }
for (u64 i = initial_length; i > new_length; --i) { for (u64 i = initial_length; i > new_length; --i)
this_object->delete_property_or_throw(i - 1); TRY_OR_DISCARD(this_object->delete_property_or_throw(i - 1));
if (vm.exception())
return {};
}
} else if (insert_count > actual_delete_count) { } else if (insert_count > actual_delete_count) {
for (u64 i = initial_length - actual_delete_count; i > actual_start; --i) { for (u64 i = initial_length - actual_delete_count; i > actual_start; --i) {
u64 from_index = i + actual_delete_count - 1; u64 from_index = i + actual_delete_count - 1;
@ -1633,9 +1612,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice)
auto from_value = TRY_OR_DISCARD(this_object->get(from_index)); auto from_value = TRY_OR_DISCARD(this_object->get(from_index));
TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
} else { } else {
this_object->delete_property_or_throw(to); TRY_OR_DISCARD(this_object->delete_property_or_throw(to));
if (vm.exception())
return {};
} }
} }
} }
@ -1894,9 +1871,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::copy_within)
auto from_value = TRY_OR_DISCARD(this_object->get(from_i)); auto from_value = TRY_OR_DISCARD(this_object->get(from_i));
TRY_OR_DISCARD(this_object->set(to_i, from_value, Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(this_object->set(to_i, from_value, Object::ShouldThrowExceptions::Yes));
} else { } else {
this_object->delete_property_or_throw(to_i); TRY_OR_DISCARD(this_object->delete_property_or_throw(to_i));
if (vm.exception())
return {};
} }
from_i += direction; from_i += direction;

View file

@ -219,7 +219,7 @@ ThrowCompletionOr<bool> Object::define_property_or_throw(PropertyName const& pro
} }
// 7.3.9 DeletePropertyOrThrow ( O, P ), https://tc39.es/ecma262/#sec-deletepropertyorthrow // 7.3.9 DeletePropertyOrThrow ( O, P ), https://tc39.es/ecma262/#sec-deletepropertyorthrow
bool Object::delete_property_or_throw(PropertyName const& property_name) ThrowCompletionOr<bool> Object::delete_property_or_throw(PropertyName const& property_name)
{ {
auto& vm = this->vm(); auto& vm = this->vm();
@ -229,13 +229,12 @@ bool Object::delete_property_or_throw(PropertyName const& property_name)
VERIFY(property_name.is_valid()); VERIFY(property_name.is_valid());
// 3. Let success be ? O.[[Delete]](P). // 3. Let success be ? O.[[Delete]](P).
auto success = TRY_OR_DISCARD(internal_delete(property_name)); auto success = TRY(internal_delete(property_name));
// 4. If success is false, throw a TypeError exception. // 4. If success is false, throw a TypeError exception.
if (!success) { if (!success) {
// FIXME: Improve/contextualize error message // FIXME: Improve/contextualize error message
vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectDeleteReturnedFalse); return vm.throw_completion<TypeError>(global_object(), ErrorType::ObjectDeleteReturnedFalse);
return {};
} }
// 5. Return success. // 5. Return success.

View file

@ -82,7 +82,7 @@ public:
ThrowCompletionOr<bool> create_data_property_or_throw(PropertyName const&, Value); ThrowCompletionOr<bool> create_data_property_or_throw(PropertyName const&, Value);
ThrowCompletionOr<bool> create_non_enumerable_data_property_or_throw(PropertyName const&, Value); ThrowCompletionOr<bool> create_non_enumerable_data_property_or_throw(PropertyName const&, Value);
ThrowCompletionOr<bool> define_property_or_throw(PropertyName const&, PropertyDescriptor const&); ThrowCompletionOr<bool> define_property_or_throw(PropertyName const&, PropertyDescriptor const&);
bool delete_property_or_throw(PropertyName const&); ThrowCompletionOr<bool> delete_property_or_throw(PropertyName const&);
bool has_property(PropertyName const&) const; bool has_property(PropertyName const&) const;
bool has_own_property(PropertyName const&) const; bool has_own_property(PropertyName const&) const;
bool set_integrity_level(IntegrityLevel); bool set_integrity_level(IntegrityLevel);

View file

@ -1080,11 +1080,8 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::sort)
for (j = 0; j < items.size(); ++j) for (j = 0; j < items.size(); ++j)
TRY_OR_DISCARD(typed_array->set(j, items[j], Object::ShouldThrowExceptions::Yes)); TRY_OR_DISCARD(typed_array->set(j, items[j], Object::ShouldThrowExceptions::Yes));
for (; j < length; ++j) { for (; j < length; ++j)
typed_array->delete_property_or_throw(j); TRY_OR_DISCARD(typed_array->delete_property_or_throw(j));
if (vm.exception())
return {};
}
return typed_array; return typed_array;
} }