summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/IndexedProperties.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-07-04 18:14:16 +0100
committerLinus Groh <mail@linusgroh.de>2021-07-04 22:07:36 +0100
commit09bd5f8772156f0031a3fa914bed2371429d09a0 (patch)
treeb92031ab80db7b45e23f761066b065b35b087046 /Userland/Libraries/LibJS/Runtime/IndexedProperties.cpp
parent4e5362b7cb4160205b0b4ab2adaab73f20346b7a (diff)
downloadserenity-09bd5f8772156f0031a3fa914bed2371429d09a0.zip
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been done slightly more incremental, but I started and then fixed everything until it worked, and here we are. I tried splitting of some completely unrelated changes into separate commits, however. Anyway. This is a rewrite of most of Object, and by extension large parts of Array, Proxy, Reflect, String, TypedArray, and some other things. What we already had worked fine for about 90% of things, but getting the last 10% right proved to be increasingly difficult with the current code that sort of grew organically and is only very loosely based on the spec - this became especially obvious when we started fixing a large number of test262 failures. Key changes include: - 1:1 matching function names and parameters of all object-related functions, to avoid ambiguity. Previously we had things like put(), which the spec doesn't have - as a result it wasn't always clear which need to be used. - Better separation between object abstract operations and internal methods - the former are always the same, the latter can be overridden (and are therefore virtual). The internal methods (i.e. [[Foo]] in the spec) are now prefixed with 'internal_' for clarity - again, it was previously not always clear which AO a certain method represents, get() could've been both Get and [[Get]] (I don't know which one it was closer to right now). Note that some of the old names have been kept until all code relying on them is updated, but they are now simple wrappers around the closest matching standard abstract operation. - Simplifications of the storage layer: functions that write values to storage are now prefixed with 'storage_' to make their purpose clear, and as they are not part of the spec they should not contain any steps specified by it. Much functionality is now covered by the layers above it and was removed (e.g. handling of accessors, attribute checks). - PropertyAttributes has been greatly simplified, and is being replaced by PropertyDescriptor - a concept similar to the current implementation, but more aligned with the actual spec. See the commit message of the previous commit where it was introduced for details. - As a bonus, and since I had to look at the spec a whole lot anyway, I introduced more inline comments with the exact steps from the spec - this makes it super easy to verify correctness. - East-const all the things. As a result of all of this, things are much more correct but a bit slower now. Retaining speed wasn't a consideration at all, I have done no profiling of the new code - there might be low hanging fruits, which we can then harvest separately. Special thanks to Idan for helping me with this by tracking down bugs, updating everything outside of LibJS to work with these changes (LibWeb, Spreadsheet, HackStudio), as well as providing countless patches to fix regressions I introduced - there still are very few (we got it down to 5), but we also get many new passing test262 tests in return. :^) Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/IndexedProperties.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/IndexedProperties.cpp95
1 files changed, 14 insertions, 81 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/IndexedProperties.cpp b/Userland/Libraries/LibJS/Runtime/IndexedProperties.cpp
index ae492d600c..56ca07ec40 100644
--- a/Userland/Libraries/LibJS/Runtime/IndexedProperties.cpp
+++ b/Userland/Libraries/LibJS/Runtime/IndexedProperties.cpp
@@ -52,15 +52,8 @@ void SimpleIndexedPropertyStorage::put(u32 index, Value value, PropertyAttribute
void SimpleIndexedPropertyStorage::remove(u32 index)
{
- if (index < m_array_size)
- m_packed_elements[index] = {};
-}
-
-void SimpleIndexedPropertyStorage::insert(u32 index, Value value, PropertyAttributes attributes)
-{
- VERIFY(attributes == default_attributes);
- m_array_size++;
- m_packed_elements.insert(index, value);
+ VERIFY(index < m_array_size);
+ m_packed_elements[index] = {};
}
ValueAndAttributes SimpleIndexedPropertyStorage::take_first()
@@ -87,7 +80,9 @@ GenericIndexedPropertyStorage::GenericIndexedPropertyStorage(SimpleIndexedProper
{
m_array_size = storage.array_like_size();
for (size_t i = 0; i < storage.m_packed_elements.size(); ++i) {
- m_sparse_elements.set(i, { storage.m_packed_elements[i], default_attributes });
+ auto value = storage.m_packed_elements[i];
+ if (!value.is_empty())
+ m_sparse_elements.set(i, { value, default_attributes });
}
}
@@ -112,34 +107,10 @@ void GenericIndexedPropertyStorage::put(u32 index, Value value, PropertyAttribut
void GenericIndexedPropertyStorage::remove(u32 index)
{
- if (index >= m_array_size)
- return;
- if (index + 1 == m_array_size) {
- take_last();
- return;
- }
+ VERIFY(index < m_array_size);
m_sparse_elements.remove(index);
}
-void GenericIndexedPropertyStorage::insert(u32 index, Value value, PropertyAttributes attributes)
-{
- if (index >= m_array_size) {
- put(index, value, attributes);
- return;
- }
-
- m_array_size++;
-
- if (!m_sparse_elements.is_empty()) {
- HashMap<u32, ValueAndAttributes> new_sparse_elements;
- for (auto& entry : m_sparse_elements)
- new_sparse_elements.set(entry.key >= index ? entry.key + 1 : entry.key, entry.value);
- m_sparse_elements = move(new_sparse_elements);
- }
-
- m_sparse_elements.set(index, { value, attributes });
-}
-
ValueAndAttributes GenericIndexedPropertyStorage::take_first()
{
VERIFY(m_array_size > 0);
@@ -226,10 +197,10 @@ bool IndexedPropertyIterator::operator!=(const IndexedPropertyIterator& other) c
return m_index != other.m_index;
}
-ValueAndAttributes IndexedPropertyIterator::value_and_attributes(Object* this_object, AllowSideEffects allow_side_effects)
+ValueAndAttributes IndexedPropertyIterator::value_and_attributes()
{
if (m_index < m_indexed_properties.array_like_size())
- return m_indexed_properties.get(this_object, m_index, allow_side_effects).value_or({});
+ return m_indexed_properties.get(m_index).value_or({});
return {};
}
@@ -245,62 +216,24 @@ void IndexedPropertyIterator::skip_empty_indices()
m_index = m_indexed_properties.array_like_size();
}
-Optional<ValueAndAttributes> IndexedProperties::get(Object* this_object, u32 index, AllowSideEffects allow_side_effects) const
+Optional<ValueAndAttributes> IndexedProperties::get(u32 index) const
{
- auto result = m_storage->get(index);
- if (allow_side_effects == AllowSideEffects::No)
- return result;
- if (!result.has_value())
- return {};
- auto& value = result.value();
- if (value.value.is_accessor()) {
- VERIFY(this_object);
- auto& accessor = value.value.as_accessor();
- return ValueAndAttributes { accessor.call_getter(this_object), value.attributes };
- }
- return result;
+ return m_storage->get(index);
}
-void IndexedProperties::put(Object* this_object, u32 index, Value value, PropertyAttributes attributes, AllowSideEffects allow_side_effects)
+void IndexedProperties::put(u32 index, Value value, PropertyAttributes attributes)
{
if (m_storage->is_simple_storage() && (attributes != default_attributes || index > (array_like_size() + SPARSE_ARRAY_HOLE_THRESHOLD))) {
switch_to_generic_storage();
}
- if (m_storage->is_simple_storage() || allow_side_effects == AllowSideEffects::No) {
- m_storage->put(index, value, attributes);
- return;
- }
-
- auto value_here = m_storage->get(index);
- if (value_here.has_value() && value_here.value().value.is_accessor()) {
- VERIFY(this_object);
- value_here.value().value.as_accessor().call_setter(this_object, value);
- } else {
- m_storage->put(index, value, attributes);
- }
+ m_storage->put(index, value, attributes);
}
-bool IndexedProperties::remove(u32 index)
+void IndexedProperties::remove(u32 index)
{
- auto result = m_storage->get(index);
- if (!result.has_value())
- return true;
- if (!result.value().attributes.is_configurable())
- return false;
+ VERIFY(m_storage->has_index(index));
m_storage->remove(index);
- return true;
-}
-
-void IndexedProperties::insert(u32 index, Value value, PropertyAttributes attributes)
-{
- if (m_storage->is_simple_storage()) {
- if (attributes != default_attributes
- || index > (array_like_size() + SPARSE_ARRAY_HOLE_THRESHOLD)) {
- switch_to_generic_storage();
- }
- }
- m_storage->insert(index, value, attributes);
}
ValueAndAttributes IndexedProperties::take_first(Object* this_object)