summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-04-08 18:56:12 +0100
committerAndreas Kling <kling@serenityos.org>2020-04-08 20:01:42 +0200
commit531335f57c27021777482d9631825f7c9420bbf5 (patch)
treee50395754c3d6cf7889f3db3d13c1733e3b06b86
parent5c90cfa90f644be90e1dad8a48920f7b58aa742e (diff)
downloadserenity-531335f57c27021777482d9631825f7c9420bbf5.zip
LibJS: Handle empty values in Array.prototype.toString()
-rw-r--r--Libraries/LibJS/Runtime/ArrayPrototype.cpp3
-rw-r--r--Libraries/LibJS/Tests/Array.prototype.toString.js6
2 files changed, 8 insertions, 1 deletions
diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
index 8a295e96a3..56b007158c 100644
--- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp
+++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp
@@ -100,7 +100,8 @@ Value ArrayPrototype::to_string(Interpreter& interpreter)
for (size_t i = 0; i < array->elements().size(); ++i) {
if (i != 0)
builder.append(',');
- builder.append(array->elements()[i].to_string());
+ if (!array->elements()[i].is_empty())
+ builder.append(array->elements()[i].to_string());
}
return js_string(interpreter, builder.to_string());
}
diff --git a/Libraries/LibJS/Tests/Array.prototype.toString.js b/Libraries/LibJS/Tests/Array.prototype.toString.js
index 7b33f21183..afffee48ad 100644
--- a/Libraries/LibJS/Tests/Array.prototype.toString.js
+++ b/Libraries/LibJS/Tests/Array.prototype.toString.js
@@ -6,6 +6,12 @@ try {
assert("rgb(" + [10, 11, 12] + ")" === "rgb(10,11,12)");
+ a = new Array(5);
+ assert(a.toString() === ",,,,");
+ a[2] = "foo";
+ a[4] = "bar";
+ assert(a.toString() === ",,foo,,bar");
+
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);