mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-21 18:37:58 +03: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
Notes:
sideshowbarker
2024-07-18 11:30:29 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/a59ba0e21fb Pull-request: https://github.com/SerenityOS/serenity/pull/8247 Reviewed-by: https://github.com/IdanHo ✅ Reviewed-by: https://github.com/alimpfard ✅ Reviewed-by: https://github.com/awesomekling ✅
@ -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 {};
|
||||
|
||||
|
@ -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…
Reference in New Issue
Block a user