summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-05-28 19:42:21 +0100
committerAndreas Kling <kling@serenityos.org>2020-05-29 08:00:02 +0200
commit1dd44210b7887373d606863a93ed5e9ede370feb (patch)
tree46a722b52c26a066c356df967166de0e6b0fe22e /Libraries
parent70d2add22f7ac66a2dbce65613af094bbe72a834 (diff)
downloadserenity-1dd44210b7887373d606863a93ed5e9ede370feb.zip
LibJS: Add Array.prototype.toLocaleString()
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp32
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.h1
-rw-r--r--Libraries/LibJS/Tests/Array.prototype.toLocaleString.js24
3 files changed, 57 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index bbe6a929aa..f151ac45d3 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -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);
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h
index 81750751da..90d03cbc0d 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.h
+++ b/Libraries/LibJS/Runtime/ArrayPrototype.h
@@ -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&);
diff --git a/Libraries/LibJS/Tests/Array.prototype.toLocaleString.js b/Libraries/LibJS/Tests/Array.prototype.toLocaleString.js
new file mode 100644
index 0000000000..c0a8932e6e
--- /dev/null
+++ b/Libraries/LibJS/Tests/Array.prototype.toLocaleString.js
@@ -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);
+}