From d4ec38097f833229f021deadc35d75c433a5231b Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 29 Apr 2020 00:22:47 +0100 Subject: [PATCH] LibJS: Return undefined in Array.prototype.{pop,shift} for empty values --- Libraries/LibJS/Runtime/ArrayPrototype.cpp | 4 ++-- Libraries/LibJS/Tests/Array.prototype.pop.js | 2 ++ Libraries/LibJS/Tests/Array.prototype.shift.js | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index eb40a51f47..29505a270c 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -207,7 +207,7 @@ Value ArrayPrototype::pop(Interpreter& interpreter) return {}; if (array->elements().is_empty()) return js_undefined(); - return array->elements().take_last(); + return array->elements().take_last().value_or(js_undefined()); } Value ArrayPrototype::shift(Interpreter& interpreter) @@ -217,7 +217,7 @@ Value ArrayPrototype::shift(Interpreter& interpreter) return {}; if (array->elements().is_empty()) return js_undefined(); - return array->elements().take_first(); + return array->elements().take_first().value_or(js_undefined()); } static Value join_array_with_separator(Interpreter& interpreter, const Array& array, StringView separator) diff --git a/Libraries/LibJS/Tests/Array.prototype.pop.js b/Libraries/LibJS/Tests/Array.prototype.pop.js index 9dce8d4c29..a4d09a2311 100644 --- a/Libraries/LibJS/Tests/Array.prototype.pop.js +++ b/Libraries/LibJS/Tests/Array.prototype.pop.js @@ -13,6 +13,8 @@ try { assert(value === undefined); assert(a.length === 0); + assert([,].pop() === undefined); + console.log("PASS"); } catch (e) { console.log("FAIL: " + e); diff --git a/Libraries/LibJS/Tests/Array.prototype.shift.js b/Libraries/LibJS/Tests/Array.prototype.shift.js index 44cb316203..2b48e7bf20 100644 --- a/Libraries/LibJS/Tests/Array.prototype.shift.js +++ b/Libraries/LibJS/Tests/Array.prototype.shift.js @@ -13,6 +13,8 @@ try { assert(value === undefined); assert(a.length === 0); + assert([,].shift() === undefined); + console.log("PASS"); } catch (e) { console.log("FAIL: " + e);