mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 10:12:25 -05:00
LibJS: Add Array.prototype.toLocaleString()
This commit is contained in:
parent
70d2add22f
commit
1dd44210b7
Notes:
sideshowbarker
2024-07-19 05:59:49 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/1dd44210b78 Pull-request: https://github.com/SerenityOS/serenity/pull/2431 Reviewed-by: https://github.com/awesomekling ✅
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&);
|
||||
|
|
24
Libraries/LibJS/Tests/Array.prototype.toLocaleString.js
Normal file
24
Libraries/LibJS/Tests/Array.prototype.toLocaleString.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.toLocaleString.length === 0);
|
||||
|
||||
assert([].toLocaleString() === "");
|
||||
assert(["foo"].toLocaleString() === "foo");
|
||||
assert(["foo", "bar"].toLocaleString() === "foo,bar");
|
||||
assert(["foo", undefined, "bar", null, "baz"].toLocaleString() === "foo,,bar,,baz");
|
||||
|
||||
var toStringCalled = 0;
|
||||
var o = {
|
||||
toString: () => {
|
||||
toStringCalled++;
|
||||
return "o";
|
||||
}
|
||||
};
|
||||
assert([o, undefined, o, null, o].toLocaleString() === "o,,o,,o");
|
||||
assert(toStringCalled === 3);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Reference in a new issue