mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 15:17:36 +00:00
LibJS: Add Array.prototype.toLocaleString()
This commit is contained in:
parent
70d2add22f
commit
1dd44210b7
3 changed files with 57 additions and 0 deletions
|
@ -53,6 +53,7 @@ ArrayPrototype::ArrayPrototype()
|
|||
define_native_function("push", push, 1, attr);
|
||||
define_native_function("shift", shift, 0, attr);
|
||||
define_native_function("toString", to_string, 0, attr);
|
||||
define_native_function("toLocaleString", to_locale_string, 0, attr);
|
||||
define_native_function("unshift", unshift, 1, attr);
|
||||
define_native_function("join", join, 1, attr);
|
||||
define_native_function("concat", concat, 1, attr);
|
||||
|
@ -264,6 +265,37 @@ Value ArrayPrototype::to_string(Interpreter& interpreter)
|
|||
return interpreter.call(join_function.as_function(), this_object);
|
||||
}
|
||||
|
||||
Value ArrayPrototype::to_locale_string(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter);
|
||||
if (!this_object)
|
||||
return {};
|
||||
String separator = ","; // NOTE: This is implementation-specific.
|
||||
auto length = get_length(interpreter, *this_object);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (i > 0)
|
||||
builder.append(separator);
|
||||
auto value = this_object->get(i).value_or(js_undefined());
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
if (value.is_undefined() || value.is_null())
|
||||
continue;
|
||||
auto* value_object = value.to_object(interpreter);
|
||||
ASSERT(value_object);
|
||||
auto locale_string_result = value_object->invoke("toLocaleString");
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
auto string = locale_string_result.to_string(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
builder.append(string);
|
||||
}
|
||||
return js_string(interpreter, builder.to_string());
|
||||
}
|
||||
|
||||
Value ArrayPrototype::join(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter);
|
||||
|
|
|
@ -46,6 +46,7 @@ private:
|
|||
static Value push(Interpreter&);
|
||||
static Value shift(Interpreter&);
|
||||
static Value to_string(Interpreter&);
|
||||
static Value to_locale_string(Interpreter&);
|
||||
static Value unshift(Interpreter&);
|
||||
static Value join(Interpreter&);
|
||||
static Value concat(Interpreter&);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue