summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-10-03 01:47:21 +0100
committerLinus Groh <mail@linusgroh.de>2021-10-03 20:14:03 +0100
commita29b7a3ec779f86158cd7ba55f25ce73a27ba207 (patch)
tree84c31f0c1dd8cbd86ff5b4cc7b124906baf3d4ae /Userland
parentfe86b04b42f58f71e1f46f7a119a994897b4fdd8 (diff)
downloadserenity-a29b7a3ec779f86158cd7ba55f25ce73a27ba207.zip
LibJS: Convert delete_property_or_throw() to ThrowCompletionOr
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp51
-rw-r--r--Userland/Libraries/LibJS/Runtime/Object.cpp7
-rw-r--r--Userland/Libraries/LibJS/Runtime/Object.h2
-rw-r--r--Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp7
4 files changed, 19 insertions, 48 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index acca92a240..2612a0b819 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -369,9 +369,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::unshift)
auto from_value = TRY_OR_DISCARD(this_object->get(from));
TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
} else {
- this_object->delete_property_or_throw(to);
- if (vm.exception())
- return {};
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(to));
}
}
@@ -396,9 +394,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::pop)
}
auto index = length - 1;
auto element = TRY_OR_DISCARD(this_object->get(index));
- this_object->delete_property_or_throw(index);
- if (vm.exception())
- return {};
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(index));
TRY_OR_DISCARD(this_object->set(vm.names.length, Value(index), Object::ShouldThrowExceptions::Yes));
return element;
}
@@ -425,16 +421,11 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::shift)
auto from_value = TRY_OR_DISCARD(this_object->get(from));
TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
} else {
- this_object->delete_property_or_throw(to);
- if (vm.exception())
- return {};
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(to));
}
}
- this_object->delete_property_or_throw(length - 1);
- if (vm.exception())
- return {};
-
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(length - 1));
TRY_OR_DISCARD(this_object->set(vm.names.length, Value(length - 1), Object::ShouldThrowExceptions::Yes));
return first;
}
@@ -976,13 +967,9 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reverse)
TRY_OR_DISCARD(this_object->set(upper, lower_value, Object::ShouldThrowExceptions::Yes));
} else if (!lower_exists && upper_exists) {
TRY_OR_DISCARD(this_object->set(lower, upper_value, Object::ShouldThrowExceptions::Yes));
- this_object->delete_property_or_throw(upper);
- if (vm.exception())
- return {};
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(upper));
} else if (lower_exists && !upper_exists) {
- this_object->delete_property_or_throw(lower);
- if (vm.exception())
- return {};
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(lower));
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
// compare function. FIXME: For performance, a similar process could be used
// for undefined, which are sorted to right before the empty values.
- for (size_t j = items.size(); j < length; ++j) {
- object->delete_property_or_throw(j);
- if (vm.exception())
- return {};
- }
+ for (size_t j = items.size(); j < length; ++j)
+ TRY_OR_DISCARD(object->delete_property_or_throw(j));
return object;
}
@@ -1609,17 +1593,12 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice)
auto from_value = TRY_OR_DISCARD(this_object->get(from));
TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
} else {
- this_object->delete_property_or_throw(to);
- if (vm.exception())
- return {};
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(to));
}
}
- for (u64 i = initial_length; i > new_length; --i) {
- this_object->delete_property_or_throw(i - 1);
- if (vm.exception())
- return {};
- }
+ for (u64 i = initial_length; i > new_length; --i)
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(i - 1));
} else if (insert_count > actual_delete_count) {
for (u64 i = initial_length - actual_delete_count; i > actual_start; --i) {
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));
TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
} else {
- this_object->delete_property_or_throw(to);
- if (vm.exception())
- return {};
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(to));
}
}
}
@@ -1894,9 +1871,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::copy_within)
auto from_value = TRY_OR_DISCARD(this_object->get(from_i));
TRY_OR_DISCARD(this_object->set(to_i, from_value, Object::ShouldThrowExceptions::Yes));
} else {
- this_object->delete_property_or_throw(to_i);
- if (vm.exception())
- return {};
+ TRY_OR_DISCARD(this_object->delete_property_or_throw(to_i));
}
from_i += direction;
diff --git a/Userland/Libraries/LibJS/Runtime/Object.cpp b/Userland/Libraries/LibJS/Runtime/Object.cpp
index 5f527ea04c..f86dfe6089 100644
--- a/Userland/Libraries/LibJS/Runtime/Object.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Object.cpp
@@ -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
-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();
@@ -229,13 +229,12 @@ bool Object::delete_property_or_throw(PropertyName const& property_name)
VERIFY(property_name.is_valid());
// 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.
if (!success) {
// FIXME: Improve/contextualize error message
- vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectDeleteReturnedFalse);
- return {};
+ return vm.throw_completion<TypeError>(global_object(), ErrorType::ObjectDeleteReturnedFalse);
}
// 5. Return success.
diff --git a/Userland/Libraries/LibJS/Runtime/Object.h b/Userland/Libraries/LibJS/Runtime/Object.h
index 275a9e57d6..76fa2b02d3 100644
--- a/Userland/Libraries/LibJS/Runtime/Object.h
+++ b/Userland/Libraries/LibJS/Runtime/Object.h
@@ -82,7 +82,7 @@ public:
ThrowCompletionOr<bool> create_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&);
- bool delete_property_or_throw(PropertyName const&);
+ ThrowCompletionOr<bool> delete_property_or_throw(PropertyName const&);
bool has_property(PropertyName const&) const;
bool has_own_property(PropertyName const&) const;
bool set_integrity_level(IntegrityLevel);
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
index 2d20792d1f..825d73bd16 100644
--- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp
@@ -1080,11 +1080,8 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::sort)
for (j = 0; j < items.size(); ++j)
TRY_OR_DISCARD(typed_array->set(j, items[j], Object::ShouldThrowExceptions::Yes));
- for (; j < length; ++j) {
- typed_array->delete_property_or_throw(j);
- if (vm.exception())
- return {};
- }
+ for (; j < length; ++j)
+ TRY_OR_DISCARD(typed_array->delete_property_or_throw(j));
return typed_array;
}