Skip to content

Commit

Permalink
LibJS: Convert enumerable_own_property_names() to ThrowCompletionOr
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg committed Oct 3, 2021
1 parent 3af559e commit e5b8544
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_obj

auto last_value = js_undefined();
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) {
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);
Expand Down
8 changes: 2 additions & 6 deletions Userland/Libraries/LibJS/Runtime/JSONObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyS
return {};
}
} else {
auto property_list = object.enumerable_own_property_names(PropertyKind::Key);
if (vm.exception())
return {};
auto property_list = TRY_OR_DISCARD(object.enumerable_own_property_names(PropertyKind::Key));
for (auto& property : property_list) {
process_property(property.as_string().string());
if (vm.exception())
Expand Down Expand Up @@ -480,9 +478,7 @@ Value JSONObject::internalize_json_property(GlobalObject& global_object, Object*
for (size_t i = 0; i < length; ++i)
TRY_OR_DISCARD(process_property(i));
} else {
auto property_list = value_object.enumerable_own_property_names(Object::PropertyKind::Key);
if (vm.exception())
return {};
auto property_list = TRY_OR_DISCARD(value_object.enumerable_own_property_names(Object::PropertyKind::Key));
for (auto& property_name : property_list)
TRY_OR_DISCARD(process_property(property_name.as_string().string()));
}
Expand Down
19 changes: 5 additions & 14 deletions Userland/Libraries/LibJS/Runtime/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
// spec text have been replaced with `continue`s in the loop below.
Expand All @@ -395,10 +395,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
// 1. Assert: Type(O) is Object.

// 2. Let ownKeys be ? O.[[OwnPropertyKeys]]().
auto own_keys_or_error = internal_own_property_keys();
if (own_keys_or_error.is_error())
return MarkedValueList { heap() };
auto own_keys = own_keys_or_error.release_value();
auto own_keys = TRY(internal_own_property_keys());

// 3. Let properties be a new empty List.
auto properties = MarkedValueList { heap() };
Expand All @@ -411,10 +408,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
auto property_name = PropertyName::from_value(global_object, key);

// i. Let desc be ? O.[[GetOwnProperty]](key).
auto descriptor_or_error = internal_get_own_property(property_name);
if (descriptor_or_error.is_error())
return MarkedValueList { heap() };
auto descriptor = descriptor_or_error.release_value();
auto descriptor = TRY(internal_get_own_property(property_name));

// ii. If desc is not undefined and desc.[[Enumerable]] is true, then
if (descriptor.has_value() && *descriptor->enumerable) {
Expand All @@ -426,10 +420,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
// 2. Else,

// a. Let value be ? Get(O, key).
auto value_or_error = get(property_name);
if (value_or_error.is_error())
return MarkedValueList { heap() };
auto value = value_or_error.release_value();
auto value = TRY(get(property_name));

// b. If kind is value, append value to properties.
if (kind == PropertyKind::Value) {
Expand All @@ -450,7 +441,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
}

// 5. Return properties.
return properties;
return { move(properties) };
}

// 7.3.25 CopyDataProperties ( target, source, excludedItems ), https://tc39.es/ecma262/#sec-copydataproperties
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Object : public Cell {
ThrowCompletionOr<bool> has_own_property(PropertyName const&) const;
ThrowCompletionOr<bool> set_integrity_level(IntegrityLevel);
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);

// 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
Expand Down
12 changes: 3 additions & 9 deletions Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
auto* object = vm.argument(0).to_object(global_object);
if (vm.exception())
return {};
auto name_list = object->enumerable_own_property_names(PropertyKind::Key);
if (vm.exception())
return {};
auto name_list = TRY_OR_DISCARD(object->enumerable_own_property_names(PropertyKind::Key));
return Array::create_from(global_object, name_list);
}

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

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

Expand Down
8 changes: 2 additions & 6 deletions Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,9 +933,7 @@ ThrowCompletionOr<Object*> default_merge_fields(GlobalObject& global_object, Obj
auto* merged = Object::create(global_object, global_object.object_prototype());

// 2. Let originalKeys be ? EnumerableOwnPropertyNames(fields, key).
auto original_keys = fields.enumerable_own_property_names(Object::PropertyKind::Key);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto original_keys = TRY(fields.enumerable_own_property_names(Object::PropertyKind::Key));

// 3. For each element nextKey of originalKeys, do
for (auto& next_key : original_keys) {
Expand All @@ -955,9 +953,7 @@ ThrowCompletionOr<Object*> default_merge_fields(GlobalObject& global_object, Obj
}

// 4. Let newKeys be ? EnumerableOwnPropertyNames(additionalFields, key).
auto new_keys = additional_fields.enumerable_own_property_names(Object::PropertyKind::Key);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto new_keys = TRY(additional_fields.enumerable_own_property_names(Object::PropertyKind::Key));

// 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;
Expand Down

0 comments on commit e5b8544

Please sign in to comment.