mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:37:36 +00:00
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:
parent
f4867572b7
commit
a59ba0e21f
2 changed files with 13 additions and 8 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue