LibJS: Change PropertyName(i32) => template<Integral T> PropertyName(T)

Negative numeric properties are not a thing (and we even VERIFY()'d this
in the constructor). It still allows using types with a negative range
for now as we have various places using int for example (without
actually needing the negative range, but that's a different story).

u32 is the internal type of `m_number` already, so this now allows us to
leverage the full u32 range for numeric properties.
This commit is contained in:
Linus Groh 2021-06-25 18:41:10 +01:00
parent f4867572b7
commit a59ba0e21f
Notes: sideshowbarker 2024-07-18 11:30:29 +09:00
2 changed files with 13 additions and 8 deletions

View File

@ -646,12 +646,12 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::slice)
size_t index = 0;
while (actual_start < final) {
bool present = this_object->has_property(actual_start);
bool present = this_object->has_property((u32)actual_start);
if (vm.exception())
return {};
if (present) {
auto value = this_object->get(actual_start).value_or(js_undefined());
auto value = this_object->get((u32)actual_start).value_or(js_undefined());
if (vm.exception())
return {};

View File

@ -31,8 +31,8 @@ public:
return {};
if (value.is_symbol())
return value.as_symbol();
if (value.is_integral_number() && value.as_i32() >= 0)
return value.as_i32();
if (value.is_integral_number() && value.as_double() >= 0 && value.as_double() <= NumericLimits<u32>::max())
return value.as_u32();
auto string = value.to_string(global_object);
if (string.is_null())
return {};
@ -41,11 +41,16 @@ public:
PropertyName() { }
PropertyName(i32 index)
template<Integral T>
PropertyName(T index)
: m_type(Type::Number)
, m_number(index)
{
// FIXME: Replace this with requires(IsUnsigned<T>)?
// Needs changes in various places using `int` (but not actually being in the negative range)
VERIFY(index >= 0);
if constexpr (NumericLimits<T>::max() > NumericLimits<u32>::max())
VERIFY(index <= NumericLimits<u32>::max());
}
PropertyName(char const* chars)
@ -125,13 +130,13 @@ public:
return false;
}
i32 property_index = m_string.to_int(TrimWhitespace::No).value_or(-1);
if (property_index < 0) {
auto property_index = m_string.to_uint(TrimWhitespace::No);
if (!property_index.has_value()) {
m_string_may_be_number = false;
return false;
}
m_type = Type::Number;
m_number = property_index;
m_number = *property_index;
return true;
}