From dec6c0a20746d7b0ec43c80755d3e77e7741f180 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 5 Nov 2020 18:39:02 +0000 Subject: [PATCH] LibJS: Use array-like size for IndexedProperties::is_empty() Some things, like (the non-generic version of) Array.prototype.pop(), check is_empty() to determine whether an action, like removing elements, can be performed. We need to know the array-like size for that, not the size of the underlying storage, which can be different - and is not something IndexedProperties should expose so I removed its size(). Fixes #3948. --- Libraries/LibJS/Runtime/IndexedProperties.h | 3 +-- Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Libraries/LibJS/Runtime/IndexedProperties.h b/Libraries/LibJS/Runtime/IndexedProperties.h index c0ce391c0e..cba20878d5 100644 --- a/Libraries/LibJS/Runtime/IndexedProperties.h +++ b/Libraries/LibJS/Runtime/IndexedProperties.h @@ -161,8 +161,7 @@ public: IndexedPropertyIterator begin(bool skip_empty = true) const { return IndexedPropertyIterator(*this, 0, skip_empty); }; IndexedPropertyIterator end() const { return IndexedPropertyIterator(*this, array_like_size(), false); }; - size_t size() const { return m_storage->size(); } - bool is_empty() const { return size() == 0; } + bool is_empty() const { return array_like_size() == 0; } size_t array_like_size() const { return m_storage->array_like_size(); } void set_array_like_size(size_t); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js index 2763159b3f..d1c34ad60e 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js @@ -7,6 +7,12 @@ describe("normal behavior", () => { var a = [1, 2, 3]; expect(a.pop()).toBe(3); expect(a).toEqual([1, 2]); + expect(a.pop()).toBe(2); + expect(a).toEqual([1]); + expect(a.pop()).toBe(1); + expect(a).toEqual([]); + expect(a.pop()).toBeUndefined(); + expect(a).toEqual([]); }); test("empty array", () => {