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

LibJS: Implement String.prototype.split

This adds a String.prototype.split implementation modelled after 
ECMA262 specification. 

Additionally, `Value::to_u32` was added as an implementation of
the standard `ToUint32` abstract operation.

There is a tiny kludge for when the separator is an empty string. 
Basic tests and visiting google.com prove that this is working.
This commit is contained in:
Marcin Gasperowicz 2021-01-09 21:52:47 +01:00 committed by Andreas Kling
parent b53664a8ef
commit b24ce0b5ee
5 changed files with 140 additions and 0 deletions

View file

@ -470,6 +470,12 @@ i32 Value::as_i32() const
return static_cast<i32>(as_double());
}
u32 Value::as_u32() const
{
ASSERT(as_double() >= 0);
return min((double)as_i32(), MAX_U32);
}
size_t Value::as_size_t() const
{
ASSERT(as_double() >= 0);
@ -494,6 +500,19 @@ i32 Value::to_i32(GlobalObject& global_object) const
return number.as_i32();
}
u32 Value::to_u32(GlobalObject& global_object) const
{
// 7.1.7 ToUint32, https://tc39.es/ecma262/#sec-touint32
auto number = to_number(global_object);
if (global_object.vm().exception())
return INVALID;
if (number.is_nan() || number.is_infinity())
return 0;
if (number.as_double() <= 0)
return 0;
return number.as_u32();
}
size_t Value::to_size_t(GlobalObject& global_object) const
{
// FIXME: Replace uses of this function with to_length/to_index for correct behaviour and remove this eventually.