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

LibJS: Convert enumerable_own_property_names() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-10-03 02:25:28 +01:00
parent 3af559ee8a
commit e5b8544762
6 changed files with 14 additions and 37 deletions

View file

@ -825,7 +825,7 @@ Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_obj
auto last_value = js_undefined(); auto last_value = js_undefined();
while (object) { while (object) {
auto property_names = object->enumerable_own_property_names(Object::PropertyKind::Key); auto property_names = TRY_OR_DISCARD(object->enumerable_own_property_names(Object::PropertyKind::Key));
for (auto& value : property_names) { for (auto& value : property_names) {
TRY_OR_DISCARD(for_in_head_state.execute_head(interpreter, global_object, value)); TRY_OR_DISCARD(for_in_head_state.execute_head(interpreter, global_object, value));
last_value = m_body->execute(interpreter, global_object).value_or(last_value); last_value = m_body->execute(interpreter, global_object).value_or(last_value);

View file

@ -238,9 +238,7 @@ String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyS
return {}; return {};
} }
} else { } else {
auto property_list = object.enumerable_own_property_names(PropertyKind::Key); auto property_list = TRY_OR_DISCARD(object.enumerable_own_property_names(PropertyKind::Key));
if (vm.exception())
return {};
for (auto& property : property_list) { for (auto& property : property_list) {
process_property(property.as_string().string()); process_property(property.as_string().string());
if (vm.exception()) if (vm.exception())
@ -480,9 +478,7 @@ Value JSONObject::internalize_json_property(GlobalObject& global_object, Object*
for (size_t i = 0; i < length; ++i) for (size_t i = 0; i < length; ++i)
TRY_OR_DISCARD(process_property(i)); TRY_OR_DISCARD(process_property(i));
} else { } else {
auto property_list = value_object.enumerable_own_property_names(Object::PropertyKind::Key); auto property_list = TRY_OR_DISCARD(value_object.enumerable_own_property_names(Object::PropertyKind::Key));
if (vm.exception())
return {};
for (auto& property_name : property_list) for (auto& property_name : property_list)
TRY_OR_DISCARD(process_property(property_name.as_string().string())); TRY_OR_DISCARD(process_property(property_name.as_string().string()));
} }

View file

@ -385,7 +385,7 @@ ThrowCompletionOr<bool> Object::test_integrity_level(IntegrityLevel level) const
} }
// 7.3.23 EnumerableOwnPropertyNames ( O, kind ), https://tc39.es/ecma262/#sec-enumerableownpropertynames // 7.3.23 EnumerableOwnPropertyNames ( O, kind ), https://tc39.es/ecma262/#sec-enumerableownpropertynames
MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const ThrowCompletionOr<MarkedValueList> Object::enumerable_own_property_names(PropertyKind kind) const
{ {
// NOTE: This has been flattened for readability, so some `else` branches in the // NOTE: This has been flattened for readability, so some `else` branches in the
// spec text have been replaced with `continue`s in the loop below. // spec text have been replaced with `continue`s in the loop below.
@ -395,10 +395,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
// 1. Assert: Type(O) is Object. // 1. Assert: Type(O) is Object.
// 2. Let ownKeys be ? O.[[OwnPropertyKeys]](). // 2. Let ownKeys be ? O.[[OwnPropertyKeys]]().
auto own_keys_or_error = internal_own_property_keys(); auto own_keys = TRY(internal_own_property_keys());
if (own_keys_or_error.is_error())
return MarkedValueList { heap() };
auto own_keys = own_keys_or_error.release_value();
// 3. Let properties be a new empty List. // 3. Let properties be a new empty List.
auto properties = MarkedValueList { heap() }; auto properties = MarkedValueList { heap() };
@ -411,10 +408,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
auto property_name = PropertyName::from_value(global_object, key); auto property_name = PropertyName::from_value(global_object, key);
// i. Let desc be ? O.[[GetOwnProperty]](key). // i. Let desc be ? O.[[GetOwnProperty]](key).
auto descriptor_or_error = internal_get_own_property(property_name); auto descriptor = TRY(internal_get_own_property(property_name));
if (descriptor_or_error.is_error())
return MarkedValueList { heap() };
auto descriptor = descriptor_or_error.release_value();
// ii. If desc is not undefined and desc.[[Enumerable]] is true, then // ii. If desc is not undefined and desc.[[Enumerable]] is true, then
if (descriptor.has_value() && *descriptor->enumerable) { if (descriptor.has_value() && *descriptor->enumerable) {
@ -426,10 +420,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
// 2. Else, // 2. Else,
// a. Let value be ? Get(O, key). // a. Let value be ? Get(O, key).
auto value_or_error = get(property_name); auto value = TRY(get(property_name));
if (value_or_error.is_error())
return MarkedValueList { heap() };
auto value = value_or_error.release_value();
// b. If kind is value, append value to properties. // b. If kind is value, append value to properties.
if (kind == PropertyKind::Value) { if (kind == PropertyKind::Value) {
@ -450,7 +441,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
} }
// 5. Return properties. // 5. Return properties.
return properties; return { move(properties) };
} }
// 7.3.25 CopyDataProperties ( target, source, excludedItems ), https://tc39.es/ecma262/#sec-copydataproperties // 7.3.25 CopyDataProperties ( target, source, excludedItems ), https://tc39.es/ecma262/#sec-copydataproperties

View file

@ -87,7 +87,7 @@ public:
ThrowCompletionOr<bool> has_own_property(PropertyName const&) const; ThrowCompletionOr<bool> has_own_property(PropertyName const&) const;
ThrowCompletionOr<bool> set_integrity_level(IntegrityLevel); ThrowCompletionOr<bool> set_integrity_level(IntegrityLevel);
ThrowCompletionOr<bool> test_integrity_level(IntegrityLevel) const; ThrowCompletionOr<bool> test_integrity_level(IntegrityLevel) const;
MarkedValueList enumerable_own_property_names(PropertyKind kind) const; ThrowCompletionOr<MarkedValueList> enumerable_own_property_names(PropertyKind kind) const;
ThrowCompletionOr<Object*> copy_data_properties(Value source, HashTable<PropertyName, PropertyNameTraits> const& seen_names, GlobalObject& global_object); ThrowCompletionOr<Object*> copy_data_properties(Value source, HashTable<PropertyName, PropertyNameTraits> const& seen_names, GlobalObject& global_object);
// 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots // 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots

View file

@ -369,9 +369,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
auto* object = vm.argument(0).to_object(global_object); auto* object = vm.argument(0).to_object(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
auto name_list = object->enumerable_own_property_names(PropertyKind::Key); auto name_list = TRY_OR_DISCARD(object->enumerable_own_property_names(PropertyKind::Key));
if (vm.exception())
return {};
return Array::create_from(global_object, name_list); return Array::create_from(global_object, name_list);
} }
@ -381,9 +379,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
auto* object = vm.argument(0).to_object(global_object); auto* object = vm.argument(0).to_object(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
auto name_list = object->enumerable_own_property_names(PropertyKind::Value); auto name_list = TRY_OR_DISCARD(object->enumerable_own_property_names(PropertyKind::Value));
if (vm.exception())
return {};
return Array::create_from(global_object, name_list); return Array::create_from(global_object, name_list);
} }
@ -393,9 +389,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
auto* object = vm.argument(0).to_object(global_object); auto* object = vm.argument(0).to_object(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
auto name_list = object->enumerable_own_property_names(PropertyKind::KeyAndValue); auto name_list = TRY_OR_DISCARD(object->enumerable_own_property_names(PropertyKind::KeyAndValue));
if (vm.exception())
return {};
return Array::create_from(global_object, name_list); return Array::create_from(global_object, name_list);
} }

View file

@ -933,9 +933,7 @@ ThrowCompletionOr<Object*> default_merge_fields(GlobalObject& global_object, Obj
auto* merged = Object::create(global_object, global_object.object_prototype()); auto* merged = Object::create(global_object, global_object.object_prototype());
// 2. Let originalKeys be ? EnumerableOwnPropertyNames(fields, key). // 2. Let originalKeys be ? EnumerableOwnPropertyNames(fields, key).
auto original_keys = fields.enumerable_own_property_names(Object::PropertyKind::Key); auto original_keys = TRY(fields.enumerable_own_property_names(Object::PropertyKind::Key));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 3. For each element nextKey of originalKeys, do // 3. For each element nextKey of originalKeys, do
for (auto& next_key : original_keys) { for (auto& next_key : original_keys) {
@ -955,9 +953,7 @@ ThrowCompletionOr<Object*> default_merge_fields(GlobalObject& global_object, Obj
} }
// 4. Let newKeys be ? EnumerableOwnPropertyNames(additionalFields, key). // 4. Let newKeys be ? EnumerableOwnPropertyNames(additionalFields, key).
auto new_keys = additional_fields.enumerable_own_property_names(Object::PropertyKind::Key); auto new_keys = TRY(additional_fields.enumerable_own_property_names(Object::PropertyKind::Key));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// IMPLEMENTATION DEFINED: This is an optimization, so we don't have to iterate new_keys three times (worst case), but only once. // IMPLEMENTATION DEFINED: This is an optimization, so we don't have to iterate new_keys three times (worst case), but only once.
bool new_keys_contains_month_or_month_code_property = false; bool new_keys_contains_month_or_month_code_property = false;