1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-17 07:17:35 +00:00

LibJS: Convert CanonicalNumericIndexString to use NumberToString

This commit is contained in:
Timothy Flynn 2023-02-12 21:38:17 -05:00 committed by Linus Groh
parent 9a5a4302d9
commit 36d72a7f4c
5 changed files with 14 additions and 13 deletions

View file

@ -1169,7 +1169,7 @@ Object* create_mapped_arguments_object(VM& vm, FunctionObject& function, Vector<
}
// 7.1.21 CanonicalNumericIndexString ( argument ), https://tc39.es/ecma262/#sec-canonicalnumericindexstring
CanonicalIndex canonical_numeric_index_string(PropertyKey const& property_key, CanonicalIndexMode mode)
ThrowCompletionOr<CanonicalIndex> canonical_numeric_index_string(VM& vm, PropertyKey const& property_key, CanonicalIndexMode mode)
{
// NOTE: If the property name is a number type (An implementation-defined optimized
// property key type), it can be treated as a string property that has already been
@ -1219,11 +1219,10 @@ CanonicalIndex canonical_numeric_index_string(PropertyKey const& property_key, C
auto maybe_double = argument.to_double(AK::TrimWhitespace::No);
if (!maybe_double.has_value())
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
auto double_value = Value(maybe_double.value());
// FIXME: We return 0 instead of n but it might not observable?
// 3. If SameValue(! ToString(n), argument) is true, return n.
if (double_value.to_deprecated_string_without_side_effects() == argument)
if (TRY_OR_THROW_OOM(vm, number_to_string(*maybe_double)) == argument.view())
return CanonicalIndex(CanonicalIndex::Type::Numeric, 0);
// 4. Return undefined.

View file

@ -57,7 +57,7 @@ enum class CanonicalIndexMode {
DetectNumericRoundtrip,
IgnoreNumericRoundtrip,
};
CanonicalIndex canonical_numeric_index_string(PropertyKey const&, CanonicalIndexMode needs_numeric);
ThrowCompletionOr<CanonicalIndex> canonical_numeric_index_string(VM&, PropertyKey const&, CanonicalIndexMode needs_numeric);
ThrowCompletionOr<String> get_substitution(VM&, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement);
enum class CallerMode {

View file

@ -144,7 +144,7 @@ ThrowCompletionOr<Optional<Value>> PrimitiveString::get(VM& vm, PropertyKey cons
return Value(static_cast<double>(length));
}
}
auto index = canonical_numeric_index_string(property_key, CanonicalIndexMode::IgnoreNumericRoundtrip);
auto index = MUST_OR_THROW_OOM(canonical_numeric_index_string(vm, property_key, CanonicalIndexMode::IgnoreNumericRoundtrip));
if (!index.is_index())
return Optional<Value> {};
auto str = TRY(utf16_string_view());

View file

@ -47,6 +47,8 @@ static ThrowCompletionOr<Optional<PropertyDescriptor>> string_get_own_property(S
{
VERIFY(property_key.is_valid());
auto& vm = string.vm();
// 1. If Type(P) is not String, return undefined.
// NOTE: The spec only uses string and symbol keys, and later coerces to numbers -
// this is not the case for PropertyKey, so '!property_key.is_string()' would be wrong.
@ -54,7 +56,7 @@ static ThrowCompletionOr<Optional<PropertyDescriptor>> string_get_own_property(S
return Optional<PropertyDescriptor> {};
// 2. Let index be CanonicalNumericIndexString(P).
auto index = canonical_numeric_index_string(property_key, CanonicalIndexMode::IgnoreNumericRoundtrip);
auto index = MUST_OR_THROW_OOM(canonical_numeric_index_string(vm, property_key, CanonicalIndexMode::IgnoreNumericRoundtrip));
// 3. If index is undefined, return undefined.
// 4. If IsIntegralNumber(index) is false, return undefined.
@ -74,7 +76,7 @@ static ThrowCompletionOr<Optional<PropertyDescriptor>> string_get_own_property(S
return Optional<PropertyDescriptor> {};
// 10. Let resultStr be the String value of length 1, containing one code unit from str, specifically the code unit at index (index).
auto result_str = PrimitiveString::create(string.vm(), TRY(Utf16String::create(string.vm(), str.substring_view(index.as_index(), 1))));
auto result_str = PrimitiveString::create(vm, TRY(Utf16String::create(vm, str.substring_view(index.as_index(), 1))));
// 11. Return the PropertyDescriptor { [[Value]]: resultStr, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false }.
return PropertyDescriptor {

View file

@ -191,7 +191,7 @@ public:
// NOTE: This includes an implementation-defined optimization, see note above!
if (property_key.is_string() || property_key.is_number()) {
// a. Let numericIndex be CanonicalNumericIndexString(P).
auto numeric_index = canonical_numeric_index_string(property_key, CanonicalIndexMode::DetectNumericRoundtrip);
auto numeric_index = MUST_OR_THROW_OOM(canonical_numeric_index_string(vm(), property_key, CanonicalIndexMode::DetectNumericRoundtrip));
// b. If numericIndex is not undefined, then
if (!numeric_index.is_undefined()) {
// i. Let value be IntegerIndexedElementGet(O, numericIndex).
@ -228,7 +228,7 @@ public:
// NOTE: This includes an implementation-defined optimization, see note above!
if (property_key.is_string() || property_key.is_number()) {
// a. Let numericIndex be CanonicalNumericIndexString(P).
auto numeric_index = canonical_numeric_index_string(property_key, CanonicalIndexMode::DetectNumericRoundtrip);
auto numeric_index = MUST_OR_THROW_OOM(canonical_numeric_index_string(vm(), property_key, CanonicalIndexMode::DetectNumericRoundtrip));
// b. If numericIndex is not undefined, return IsValidIntegerIndex(O, numericIndex).
if (!numeric_index.is_undefined())
return is_valid_integer_index(*this, numeric_index);
@ -251,7 +251,7 @@ public:
// NOTE: This includes an implementation-defined optimization, see note above!
if (property_key.is_string() || property_key.is_number()) {
// a. Let numericIndex be CanonicalNumericIndexString(P).
auto numeric_index = canonical_numeric_index_string(property_key, CanonicalIndexMode::DetectNumericRoundtrip);
auto numeric_index = MUST_OR_THROW_OOM(canonical_numeric_index_string(vm(), property_key, CanonicalIndexMode::DetectNumericRoundtrip));
// b. If numericIndex is not undefined, then
if (!numeric_index.is_undefined()) {
// i. If IsValidIntegerIndex(O, numericIndex) is false, return false.
@ -301,7 +301,7 @@ public:
// NOTE: This includes an implementation-defined optimization, see note above!
if (property_key.is_string() || property_key.is_number()) {
// a. Let numericIndex be CanonicalNumericIndexString(P).
auto numeric_index = canonical_numeric_index_string(property_key, CanonicalIndexMode::DetectNumericRoundtrip);
auto numeric_index = MUST_OR_THROW_OOM(canonical_numeric_index_string(vm(), property_key, CanonicalIndexMode::DetectNumericRoundtrip));
// b. If numericIndex is not undefined, then
if (!numeric_index.is_undefined()) {
// i. Return IntegerIndexedElementGet(O, numericIndex).
@ -328,7 +328,7 @@ public:
// NOTE: This includes an implementation-defined optimization, see note above!
if (property_key.is_string() || property_key.is_number()) {
// a. Let numericIndex be CanonicalNumericIndexString(P).
auto numeric_index = canonical_numeric_index_string(property_key, CanonicalIndexMode::DetectNumericRoundtrip);
auto numeric_index = MUST_OR_THROW_OOM(canonical_numeric_index_string(vm(), property_key, CanonicalIndexMode::DetectNumericRoundtrip));
// b. If numericIndex is not undefined, then
if (!numeric_index.is_undefined()) {
// i. If SameValue(O, Receiver) is true, then
@ -363,7 +363,7 @@ public:
// NOTE: This includes an implementation-defined optimization, see note above!
if (property_key.is_string() || property_key.is_number()) {
// a. Let numericIndex be CanonicalNumericIndexString(P).
auto numeric_index = canonical_numeric_index_string(property_key, CanonicalIndexMode::DetectNumericRoundtrip);
auto numeric_index = MUST_OR_THROW_OOM(canonical_numeric_index_string(vm(), property_key, CanonicalIndexMode::DetectNumericRoundtrip));
// b. If numericIndex is not undefined, then
if (!numeric_index.is_undefined()) {
// i. If IsValidIntegerIndex(O, numericIndex) is false, return true; else return false.