From 531335f57c27021777482d9631825f7c9420bbf5 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 8 Apr 2020 18:56:12 +0100 Subject: LibJS: Handle empty values in Array.prototype.toString() --- Libraries/LibJS/Runtime/ArrayPrototype.cpp | 3 ++- Libraries/LibJS/Tests/Array.prototype.toString.js | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'Libraries/LibJS') 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); -- cgit v1.2.3