LibJS: Make length_of_array_like() take an Object rather than Value

The pseudo-code from the spec says "Assert: Type(obj) is Object.", so we
can just enforce this at compile time rather than taking it literally
and doing "ASSERT(value.is_object())".

Also fix an issue where the absence of a "length" property on the object
would cause a crash (to_number() on empty value).
This commit is contained in:
Linus Groh 2021-01-10 14:08:27 +01:00 committed by Andreas Kling
parent a5e557472c
commit 9be0b664e3
Notes: sideshowbarker 2024-07-18 23:57:14 +09:00
3 changed files with 10 additions and 9 deletions

View File

@ -70,7 +70,7 @@ String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Valu
state.replacer_function = &replacer.as_function();
} else if (replacer.is_array()) {
auto& replacer_object = replacer.as_object();
auto replacer_length = length_of_array_like(global_object, replacer);
auto replacer_length = length_of_array_like(global_object, replacer_object);
if (vm.exception())
return {};
Vector<String> list;
@ -298,7 +298,7 @@ String JSONObject::serialize_json_array(GlobalObject& global_object, StringifySt
state.indent = String::formatted("{}{}", state.indent, state.gap);
Vector<String> property_strings;
auto length = length_of_array_like(global_object, Value(&object));
auto length = length_of_array_like(global_object, object);
if (vm.exception())
return {};
for (size_t i = 0; i < length; ++i) {
@ -476,7 +476,7 @@ Value JSONObject::internalize_json_property(GlobalObject& global_object, Object*
};
if (value_object.is_array()) {
auto length = length_of_array_like(global_object, value);
auto length = length_of_array_like(global_object, value_object);
for (size_t i = 0; i < length; ++i) {
process_property(i);
if (vm.exception())

View File

@ -1222,14 +1222,15 @@ TriState abstract_relation(GlobalObject& global_object, bool left_first, Value l
return TriState::False;
}
size_t length_of_array_like(GlobalObject& global_object, Value value)
size_t length_of_array_like(GlobalObject& global_object, const Object& object)
{
ASSERT(value.is_object());
// 7.3.18 LengthOfArrayLike, https://tc39.es/ecma262/#sec-lengthofarraylike
auto& vm = global_object.vm();
auto result = value.as_object().get(vm.names.length);
auto result = object.get(vm.names.length).value_or(js_undefined());
if (vm.exception())
return 0;
return result.to_size_t(global_object);
return INVALID;
return result.to_length(global_object);
}
}

View File

@ -341,7 +341,7 @@ bool same_value(Value lhs, Value rhs);
bool same_value_zero(Value lhs, Value rhs);
bool same_value_non_numeric(Value lhs, Value rhs);
TriState abstract_relation(GlobalObject&, bool left_first, Value lhs, Value rhs);
size_t length_of_array_like(GlobalObject&, Value);
size_t length_of_array_like(GlobalObject&, const Object&);
}