ladybird/Libraries/LibJS/Bytecode/PropertyNameIterator.h
Andreas Kling 879ac36e45 LibJS: Cache stable for-in iteration at bytecode sites
Cache the flattened enumerable key snapshot for each `for..in` site and
reuse a `PropertyNameIterator` when the receiver shape, dictionary
generation, indexed storage kind and length, prototype chain
validity, and magical-length state still match.

Handle packed indexed receivers as well as plain named-property
objects. Teach `ObjectPropertyIteratorNext` in `asmint.asm` to return
cached property values directly and to fall back to the slow iterator
logic when any guard fails.

Treat arrays' hidden non-enumerable `length` property as a visited
name for for-in shadowing, and include the receiver's magical-length
state in the cache key so arrays and plain objects do not share
snapshots.

Add `test-js` and `test-js-bytecode` coverage for mixed numeric and
named keys, packed receiver transitions, re-entry, iterator reuse, GC
retention, array length shadowing, and same-site cache reuse.
2026-04-10 15:12:53 +02:00

61 lines
2.3 KiB
C++

/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Bytecode/Executable.h>
#include <LibJS/Export.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/Object.h>
namespace JS::Bytecode {
class JS_API PropertyNameIterator final
: public Object
, public BuiltinIterator {
JS_OBJECT(PropertyNameIterator, Object);
GC_DECLARE_ALLOCATOR(PropertyNameIterator);
public:
using FastPath = ObjectPropertyIteratorFastPath;
static GC::Ref<PropertyNameIterator> create(Realm&, GC::Ref<Object>, Vector<PropertyKey>, FastPath = FastPath::None, u32 indexed_property_count = 0, GC::Ptr<Shape> = nullptr, GC::Ptr<PrototypeChainValidity> = nullptr);
static GC::Ref<PropertyNameIterator> create(Realm&, GC::Ref<Object>, ObjectPropertyIteratorCacheData&, ObjectPropertyIteratorCache* = nullptr);
virtual ~PropertyNameIterator() override = default;
BuiltinIterator* as_builtin_iterator_if_next_is_not_redefined(Value) override { return this; }
ThrowCompletionOr<void> next(VM&, bool& done, Value& value) override;
void reset_with_cache_data(GC::Ref<Object>, ObjectPropertyIteratorCacheData&, ObjectPropertyIteratorCache*);
private:
PropertyNameIterator(Realm&, GC::Ref<Object>, Vector<PropertyKey>, FastPath, u32 indexed_property_count, GC::Ptr<Shape>, GC::Ptr<PrototypeChainValidity>);
PropertyNameIterator(Realm&, GC::Ref<Object>, ObjectPropertyIteratorCacheData&, ObjectPropertyIteratorCache*);
ReadonlySpan<PropertyKey> property_list() const;
ReadonlySpan<Value> property_value_list() const;
bool fast_path_still_valid() const;
void disable_fast_path();
virtual void visit_edges(Visitor&) override;
GC::Ptr<Object> m_object;
Vector<PropertyKey> m_owned_properties;
GC::Ptr<ObjectPropertyIteratorCacheData> m_property_cache;
GC::Ptr<Shape> m_shape;
GC::Ptr<PrototypeChainValidity> m_prototype_chain_validity;
ObjectPropertyIteratorCache* m_iterator_cache_slot { nullptr };
u32 m_indexed_property_count { 0 };
u32 m_next_indexed_property { 0 };
size_t m_next_property { 0 };
bool m_shape_is_dictionary { false };
u32 m_shape_dictionary_generation { 0 };
FastPath m_fast_path { FastPath::None };
};
}