Преглед изворни кода

LibJS: Convert has_property() to ThrowCompletionOr

Linus Groh пре 3 година
родитељ
комит
f38a5957bf

+ 26 - 68
Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp

@@ -190,9 +190,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::filter)
         auto property_name = PropertyName { k };
 
         // b. Let kPresent be ? HasProperty(O, Pk).
-        auto k_present = object->has_property(property_name);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
         // c. If kPresent is true, then
         if (k_present) {
@@ -246,9 +244,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::for_each)
         auto property_name = PropertyName { k };
 
         // b. Let kPresent be ? HasProperty(O, Pk).
-        auto k_present = object->has_property(property_name);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
         // c. If kPresent is true, then
         if (k_present) {
@@ -300,9 +296,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::map)
         auto property_name = PropertyName { k };
 
         // b. Let kPresent be ? HasProperty(O, Pk).
-        auto k_present = object->has_property(property_name);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
         // c. If kPresent is true, then
         if (k_present) {
@@ -362,9 +356,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::unshift)
             auto from = k - 1;
             auto to = k + arg_count - 1;
 
-            bool from_present = this_object->has_property(from);
-            if (vm.exception())
-                return {};
+            bool from_present = TRY_OR_DISCARD(this_object->has_property(from));
             if (from_present) {
                 auto from_value = TRY_OR_DISCARD(this_object->get(from));
                 TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
@@ -414,9 +406,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::shift)
     for (size_t k = 1; k < length; ++k) {
         size_t from = k;
         size_t to = k - 1;
-        bool from_present = this_object->has_property(from);
-        if (vm.exception())
-            return {};
+        bool from_present = TRY_OR_DISCARD(this_object->has_property(from));
         if (from_present) {
             auto from_value = TRY_OR_DISCARD(this_object->get(from));
             TRY_OR_DISCARD(this_object->set(to, from_value, Object::ShouldThrowExceptions::Yes));
@@ -562,9 +552,10 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::concat)
                 return;
             }
             while (k < length) {
-                auto k_exists = obj.has_property(k);
-                if (vm.exception())
+                auto k_exists_or_error = obj.has_property(k);
+                if (k_exists_or_error.is_error())
                     return;
+                auto k_exists = k_exists_or_error.release_value();
                 if (k_exists) {
                     auto k_value_or_error = obj.get(k);
                     if (k_value_or_error.is_error())
@@ -655,10 +646,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::slice)
     size_t k = actual_start;
 
     while (k < final) {
-        bool present = this_object->has_property(k);
-        if (vm.exception())
-            return {};
-
+        bool present = TRY_OR_DISCARD(this_object->has_property(k));
         if (present) {
             auto value = TRY_OR_DISCARD(this_object->get(k));
             TRY_OR_DISCARD(new_array->create_data_property_or_throw(index, value));
@@ -726,9 +714,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::index_of)
         auto property_name = PropertyName { k };
 
         // a. Let kPresent be ? HasProperty(O, ! ToString(𝔽(k))).
-        auto k_present = object->has_property(property_name);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
         // b. If kPresent is true, then
         if (k_present) {
@@ -798,9 +784,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reduce)
             auto property_name = PropertyName { k };
 
             // ii. Set kPresent to ? HasProperty(O, Pk).
-            k_present = object->has_property(property_name);
-            if (vm.exception())
-                return {};
+            k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
             // iii. If kPresent is true, then
             if (k_present) {
@@ -824,7 +808,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reduce)
         auto property_name = PropertyName { k };
 
         // b. Let kPresent be ? HasProperty(O, Pk).
-        auto k_present = object->has_property(property_name);
+        auto k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
         // c. If kPresent is true, then
         if (k_present) {
@@ -890,9 +874,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reduce_right)
             auto property_name = PropertyName { k };
 
             // ii. Set kPresent to ? HasProperty(O, Pk).
-            k_present = object->has_property(property_name);
-            if (vm.exception())
-                return {};
+            k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
             // iii. If kPresent is true, then
             if (k_present) {
@@ -916,9 +898,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reduce_right)
         auto property_name = PropertyName { k };
 
         // b. Let kPresent be ? HasProperty(O, Pk).
-        auto k_present = object->has_property(property_name);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
         // c. If kPresent is true, then
         if (k_present) {
@@ -948,16 +928,12 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::reverse)
     for (size_t lower = 0; lower < middle; ++lower) {
         auto upper = length - lower - 1;
 
-        auto lower_exists = this_object->has_property(lower);
-        if (vm.exception())
-            return {};
+        auto lower_exists = TRY_OR_DISCARD(this_object->has_property(lower));
         Value lower_value;
         if (lower_exists)
             lower_value = TRY_OR_DISCARD(this_object->get(lower));
 
-        auto upper_exists = this_object->has_property(upper);
-        if (vm.exception())
-            return {};
+        auto upper_exists = TRY_OR_DISCARD(this_object->has_property(upper));
         Value upper_value;
         if (upper_exists)
             upper_value = TRY_OR_DISCARD(this_object->get(upper));
@@ -1103,9 +1079,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::sort)
 
     MarkedValueList items(vm.heap());
     for (size_t k = 0; k < length; ++k) {
-        auto k_present = object->has_property(k);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(object->has_property(k));
 
         if (k_present) {
             auto k_value = TRY_OR_DISCARD(object->get(k));
@@ -1185,9 +1159,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::last_index_of)
         auto property_name = PropertyName { k };
 
         // a. Let kPresent be ? HasProperty(O, ! ToString(𝔽(k))).
-        auto k_present = object->has_property(property_name);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
         // b. If kPresent is true, then
         if (k_present) {
@@ -1442,9 +1414,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::some)
         auto property_name = PropertyName { k };
 
         // b. Let kPresent be ? HasProperty(O, Pk).
-        auto k_present = object->has_property(property_name);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
         // c. If kPresent is true, then
         if (k_present) {
@@ -1493,9 +1463,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::every)
         auto property_name = PropertyName { k };
 
         // b. Let kPresent be ? HasProperty(O, Pk).
-        auto k_present = object->has_property(property_name);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(object->has_property(property_name));
 
         // c. If kPresent is true, then
         if (k_present) {
@@ -1567,9 +1535,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice)
 
     for (u64 i = 0; i < actual_delete_count; ++i) {
         auto from = actual_start + i;
-        bool from_present = this_object->has_property(from);
-        if (vm.exception())
-            return {};
+        bool from_present = TRY_OR_DISCARD(this_object->has_property(from));
 
         if (from_present) {
             auto from_value = TRY_OR_DISCARD(this_object->get(from));
@@ -1585,9 +1551,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice)
             auto to = i + insert_count;
             u64 from = i + actual_delete_count;
 
-            auto from_present = this_object->has_property(from);
-            if (vm.exception())
-                return {};
+            auto from_present = TRY_OR_DISCARD(this_object->has_property(from));
 
             if (from_present) {
                 auto from_value = TRY_OR_DISCARD(this_object->get(from));
@@ -1602,9 +1566,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice)
     } 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;
-            auto from_present = this_object->has_property(from_index);
-            if (vm.exception())
-                return {};
+            auto from_present = TRY_OR_DISCARD(this_object->has_property(from_index));
 
             auto to = i + insert_count - 1;
 
@@ -1645,7 +1607,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::fill)
             relative_start = 0;
     }
 
-    //If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToIntegerOrInfinity(end).
+    // If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToIntegerOrInfinity(end).
     if (vm.argument_count() >= 3 && !vm.argument(2).is_undefined()) {
         relative_end = vm.argument(2).to_integer_or_infinity(global_object);
         if (vm.exception())
@@ -1709,9 +1671,7 @@ static size_t flatten_into_array(GlobalObject& global_object, Object& new_array,
     auto& vm = global_object.vm();
 
     for (size_t j = 0; j < array_length; ++j) {
-        auto value_exists = array.has_property(j);
-        if (vm.exception())
-            return {};
+        auto value_exists = TRY_OR_DISCARD(array.has_property(j));
 
         if (!value_exists)
             continue;
@@ -1863,9 +1823,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::copy_within)
     size_t count_i = count;
 
     while (count_i > 0) {
-        auto from_present = this_object->has_property(from_i);
-        if (vm.exception())
-            return {};
+        auto from_present = TRY_OR_DISCARD(this_object->has_property(from_i));
 
         if (from_present) {
             auto from_value = TRY_OR_DISCARD(this_object->get(from_i));

+ 1 - 6
Userland/Libraries/LibJS/Runtime/Error.cpp

@@ -36,12 +36,7 @@ ThrowCompletionOr<void> Error::install_error_cause(Value options)
     auto& vm = this->vm();
 
     // 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then
-    if (!options.is_object())
-        return {};
-    auto has_property = options.as_object().has_property(vm.names.cause);
-    if (auto* exception = vm.exception())
-        return throw_completion(exception->value());
-    if (has_property) {
+    if (options.is_object() && TRY(options.as_object().has_property(vm.names.cause))) {
         // a. Let cause be ? Get(options, "cause").
         auto cause = TRY(options.as_object().get(vm.names.cause));
 

+ 1 - 3
Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp

@@ -225,9 +225,7 @@ ThrowCompletionOr<Vector<String>> canonicalize_locale_list(GlobalObject& global_
         auto property_key = PropertyName { k };
 
         // b. Let kPresent be ? HasProperty(O, Pk).
-        auto key_present = object->has_property(property_key);
-        if (auto* exception = vm.exception())
-            return throw_completion(exception->value());
+        auto key_present = TRY(object->has_property(property_key));
 
         // c. If kPresent is true, then
         if (key_present) {

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Object.cpp

@@ -242,7 +242,7 @@ ThrowCompletionOr<bool> Object::delete_property_or_throw(PropertyName const& pro
 }
 
 // 7.3.11 HasProperty ( O, P ), https://tc39.es/ecma262/#sec-hasproperty
-bool Object::has_property(PropertyName const& property_name) const
+ThrowCompletionOr<bool> Object::has_property(PropertyName const& property_name) const
 {
     // 1. Assert: Type(O) is Object.
 
@@ -250,7 +250,7 @@ bool Object::has_property(PropertyName const& property_name) const
     VERIFY(property_name.is_valid());
 
     // 3. Return ? O.[[HasProperty]](P).
-    return TRY_OR_DISCARD(internal_has_property(property_name));
+    return internal_has_property(property_name);
 }
 
 // 7.3.12 HasOwnProperty ( O, P ), https://tc39.es/ecma262/#sec-hasownproperty

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Object.h

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

+ 6 - 9
Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp

@@ -28,9 +28,7 @@ void ObjectEnvironment::visit_edges(Cell::Visitor& visitor)
 bool ObjectEnvironment::has_binding(FlyString const& name) const
 {
     auto& vm = this->vm();
-    bool found_binding = m_binding_object.has_property(name);
-    if (vm.exception())
-        return {};
+    bool found_binding = TRY_OR_DISCARD(m_binding_object.has_property(name));
     if (!found_binding)
         return false;
     if (!m_with_environment)
@@ -69,9 +67,10 @@ void ObjectEnvironment::initialize_binding(GlobalObject& global_object, FlyStrin
 void ObjectEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
 {
     auto& vm = this->vm();
-    bool still_exists = m_binding_object.has_property(name);
-    if (vm.exception())
+    auto still_exists_or_error = m_binding_object.has_property(name);
+    if (still_exists_or_error.is_error())
         return;
+    auto still_exists = still_exists_or_error.release_value();
     if (!still_exists && strict) {
         global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
         return;
@@ -96,14 +95,12 @@ void ObjectEnvironment::set_mutable_binding(GlobalObject& global_object, FlyStri
 Value ObjectEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
 {
     auto& vm = this->vm();
-    auto value = m_binding_object.has_property(name);
-    if (vm.exception())
-        return {};
+    auto value = TRY_OR_DISCARD(m_binding_object.has_property(name));
     if (!value) {
         if (!strict)
             return js_undefined();
 
-        global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
+        vm.throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
         return {};
     }
     return TRY_OR_DISCARD(m_binding_object.get(name));

+ 6 - 18
Userland/Libraries/LibJS/Runtime/PropertyDescriptor.cpp

@@ -84,37 +84,27 @@ PropertyDescriptor to_property_descriptor(GlobalObject& global_object, Value arg
     }
     auto& object = argument.as_object();
     PropertyDescriptor descriptor;
-    auto has_enumerable = object.has_property(vm.names.enumerable);
-    if (vm.exception())
-        return {};
+    auto has_enumerable = TRY_OR_DISCARD(object.has_property(vm.names.enumerable));
     if (has_enumerable) {
         auto enumerable = TRY_OR_DISCARD(object.get(vm.names.enumerable));
         descriptor.enumerable = enumerable.to_boolean();
     }
-    auto has_configurable = object.has_property(vm.names.configurable);
-    if (vm.exception())
-        return {};
+    auto has_configurable = TRY_OR_DISCARD(object.has_property(vm.names.configurable));
     if (has_configurable) {
         auto configurable = TRY_OR_DISCARD(object.get(vm.names.configurable));
         descriptor.configurable = configurable.to_boolean();
     }
-    auto has_value = object.has_property(vm.names.value);
-    if (vm.exception())
-        return {};
+    auto has_value = TRY_OR_DISCARD(object.has_property(vm.names.value));
     if (has_value) {
         auto value = TRY_OR_DISCARD(object.get(vm.names.value));
         descriptor.value = value;
     }
-    auto has_writable = object.has_property(vm.names.writable);
-    if (vm.exception())
-        return {};
+    auto has_writable = TRY_OR_DISCARD(object.has_property(vm.names.writable));
     if (has_writable) {
         auto writable = TRY_OR_DISCARD(object.get(vm.names.writable));
         descriptor.writable = writable.to_boolean();
     }
-    auto has_get = object.has_property(vm.names.get);
-    if (vm.exception())
-        return {};
+    auto has_get = TRY_OR_DISCARD(object.has_property(vm.names.get));
     if (has_get) {
         auto getter = TRY_OR_DISCARD(object.get(vm.names.get));
         if (!getter.is_function() && !getter.is_undefined()) {
@@ -123,9 +113,7 @@ PropertyDescriptor to_property_descriptor(GlobalObject& global_object, Value arg
         }
         descriptor.get = getter.is_function() ? &getter.as_function() : nullptr;
     }
-    auto has_set = object.has_property(vm.names.set);
-    if (vm.exception())
-        return {};
+    auto has_set = TRY_OR_DISCARD(object.has_property(vm.names.set));
     if (has_set) {
         auto setter = TRY_OR_DISCARD(object.get(vm.names.set));
         if (!setter.is_function() && !setter.is_undefined()) {

+ 3 - 11
Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp

@@ -325,23 +325,15 @@ ThrowCompletionOr<Object*> to_temporal_calendar(GlobalObject& global_object, Val
             return &static_cast<ZonedDateTime&>(temporal_calendar_like_object).calendar();
 
         // b. If ? HasProperty(temporalCalendarLike, "calendar") is false, return temporalCalendarLike.
-        auto has_property = temporal_calendar_like_object.has_property(vm.names.calendar);
-        if (auto* exception = vm.exception())
-            return throw_completion(exception->value());
-        if (!has_property)
+        if (!TRY(temporal_calendar_like_object.has_property(vm.names.calendar)))
             return &temporal_calendar_like_object;
 
         // c. Set temporalCalendarLike to ? Get(temporalCalendarLike, "calendar").
         temporal_calendar_like = TRY(temporal_calendar_like_object.get(vm.names.calendar));
 
         // d. If Type(temporalCalendarLike) is Object and ? HasProperty(temporalCalendarLike, "calendar") is false, return temporalCalendarLike.
-        if (temporal_calendar_like.is_object()) {
-            has_property = temporal_calendar_like.as_object().has_property(vm.names.calendar);
-            if (auto* exception = vm.exception())
-                return throw_completion(exception->value());
-            if (!has_property)
-                return &temporal_calendar_like.as_object();
-        }
+        if (temporal_calendar_like.is_object() && !TRY(temporal_calendar_like.as_object().has_property(vm.names.calendar)))
+            return &temporal_calendar_like.as_object();
     }
 
     // 2. Let identifier be ? ToString(temporalCalendarLike).

+ 3 - 11
Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp

@@ -326,23 +326,15 @@ ThrowCompletionOr<Object*> to_temporal_time_zone(GlobalObject& global_object, Va
         }
 
         // b. If ? HasProperty(temporalTimeZoneLike, "timeZone") is false, return temporalTimeZoneLike.
-        auto has_property = temporal_time_zone_like.as_object().has_property(vm.names.timeZone);
-        if (auto* exception = vm.exception())
-            return throw_completion(exception->value());
-        if (!has_property)
+        if (!TRY(temporal_time_zone_like.as_object().has_property(vm.names.timeZone)))
             return &temporal_time_zone_like.as_object();
 
         // c. Set temporalTimeZoneLike to ? Get(temporalTimeZoneLike, "timeZone").
         temporal_time_zone_like = TRY(temporal_time_zone_like.as_object().get(vm.names.timeZone));
 
         // d. If Type(temporalTimeZoneLike) is Object and ? HasProperty(temporalTimeZoneLike, "timeZone") is false, return temporalTimeZoneLike.
-        if (temporal_time_zone_like.is_object()) {
-            has_property = temporal_time_zone_like.as_object().has_property(vm.names.timeZone);
-            if (auto* exception = vm.exception())
-                return throw_completion(exception->value());
-            if (!has_property)
-                return &temporal_time_zone_like.as_object();
-        }
+        if (temporal_time_zone_like.is_object() && !TRY(temporal_time_zone_like.as_object().has_property(vm.names.timeZone)))
+            return &temporal_time_zone_like.as_object();
     }
 
     // 2. Let identifier be ? ToString(temporalTimeZoneLike).

+ 3 - 5
Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp

@@ -446,7 +446,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::index_of)
 
     auto search_element = vm.argument(0);
     for (; k < length; ++k) {
-        auto k_present = typed_array->has_property(k);
+        auto k_present = MUST(typed_array->has_property(k));
         if (k_present) {
             auto element_k = MUST(typed_array->get(k));
 
@@ -494,7 +494,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::last_index_of)
 
     auto search_element = vm.argument(0);
     for (; k >= 0; --k) {
-        auto k_present = typed_array->has_property(k);
+        auto k_present = MUST(typed_array->has_property(k));
         if (k_present) {
             auto element_k = MUST(typed_array->get(k));
 
@@ -1062,9 +1062,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::sort)
 
     MarkedValueList items(vm.heap());
     for (u32 k = 0; k < length; ++k) {
-        auto k_present = typed_array->has_property(k);
-        if (vm.exception())
-            return {};
+        auto k_present = TRY_OR_DISCARD(typed_array->has_property(k));
 
         if (k_present) {
             auto k_value = TRY_OR_DISCARD(typed_array->get(k));

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Value.cpp

@@ -1283,7 +1283,7 @@ Value in(GlobalObject& global_object, Value lhs, Value rhs)
     auto lhs_property_key = lhs.to_property_key(global_object);
     if (global_object.vm().exception())
         return {};
-    return Value(rhs.as_object().has_property(lhs_property_key));
+    return Value(TRY_OR_DISCARD(rhs.as_object().has_property(lhs_property_key)));
 }
 
 // 13.10.2 InstanceofOperator ( V, target ), https://tc39.es/ecma262/#sec-instanceofoperator