#ifndef V8_OBJECTS_LOOKUP_H_
#define V8_OBJECTS_LOOKUP_H_
#include <optional>
#include "src/common/globals.h"
#include "src/execution/isolate.h"
#include "src/heap/factory.h"
#include "src/objects/descriptor-array.h"
#include "src/objects/js-objects.h"
#include "src/objects/map.h"
#include "src/objects/objects.h"
namespace v8::internal {
class PropertyKey {
public:
inline PropertyKey(Isolate* isolate, double index);
template <template <typename> typename HandleType>
requires(std::is_convertible_v<HandleType<Name>, DirectHandle<Name>>)
inline PropertyKey(Isolate* isolate, HandleType<Name> name);
template <typename T, template <typename> typename HandleType>
requires(std::is_convertible_v<HandleType<T>, DirectHandle<T>>)
inline PropertyKey(Isolate* isolate, HandleType<T> valid_key);
template <typename T, template <typename> typename HandleType>
requires(std::is_convertible_v<HandleType<T>, DirectHandle<T>>)
PropertyKey(Isolate* isolate, HandleType<T> key, bool* success);
inline bool is_element() const;
DirectHandle<Name> name() const { return name_; }
size_t index() const { return index_; }
inline DirectHandle<Name> GetName(Isolate* isolate);
private:
friend LookupIterator;
template <template <typename> typename HandleType>
requires(std::is_convertible_v<HandleType<Name>, DirectHandle<Name>>)
inline PropertyKey(Isolate* isolate, HandleType<Name> name, size_t index);
DirectHandle<Name> name_;
size_t index_;
};
class V8_EXPORT_PRIVATE LookupIterator final {
public:
enum Configuration {
kInterceptor = 1 << 0,
kPrototypeChain = 1 << 1,
OWN_SKIP_INTERCEPTOR = 0,
OWN = kInterceptor,
PROTOTYPE_CHAIN_SKIP_INTERCEPTOR = kPrototypeChain,
PROTOTYPE_CHAIN = kPrototypeChain | kInterceptor,
DEFAULT = PROTOTYPE_CHAIN
};
enum State {
NOT_FOUND,
STRING_LOOKUP_START_OBJECT,
TYPED_ARRAY_INDEX_NOT_FOUND,
ACCESS_CHECK,
INTERCEPTOR,
JSPROXY,
ACCESSOR,
DATA,
WASM_OBJECT,
TRANSITION,
BEFORE_PROPERTY = INTERCEPTOR
};
inline LookupIterator(Isolate* isolate, DirectHandle<JSAny> receiver,
DirectHandle<Name> name,
Configuration configuration = DEFAULT);
inline LookupIterator(Isolate* isolate, DirectHandle<JSAny> receiver,
DirectHandle<Name> name,
DirectHandle<JSAny> lookup_start_object,
Configuration configuration = DEFAULT);
inline LookupIterator(Isolate* isolate, DirectHandle<JSAny> receiver,
size_t index, Configuration configuration = DEFAULT);
inline LookupIterator(Isolate* isolate, DirectHandle<JSAny> receiver,
size_t index, DirectHandle<JSAny> lookup_start_object,
Configuration configuration = DEFAULT);
inline LookupIterator(Isolate* isolate, DirectHandle<JSAny> receiver,
const PropertyKey& key,
Configuration configuration = DEFAULT);
inline LookupIterator(Isolate* isolate, DirectHandle<JSAny> receiver,
const PropertyKey& key,
DirectHandle<JSAny> lookup_start_object,
Configuration configuration = DEFAULT);
inline LookupIterator(Isolate* isolate, Configuration configuration,
DirectHandle<JSAny> receiver,
DirectHandle<Symbol> name);
void Restart() {
InterceptorState state = InterceptorState::kUninitialized;
IsElement() ? RestartInternal<true>(state) : RestartInternal<false>(state);
}
void RecheckTypedArrayBounds();
Isolate* isolate() const { return isolate_; }
State state() const { return state_; }
inline DirectHandle<Name> name() const;
inline DirectHandle<Name> GetName();
size_t index() const { return index_; }
uint32_t array_index() const {
DCHECK_LE(index_, JSArray::kMaxArrayIndex);
return static_cast<uint32_t>(index_);
}
inline PropertyKey GetKey() const;
bool IsElement() const { return index_ != kInvalidIndex; }
inline bool IsElement(Tagged<JSReceiver> object) const;
inline bool IsPrivateName() const;
bool IsFound() const { return state_ != NOT_FOUND; }
void Next();
void NotFound() {
has_property_ = false;
state_ = NOT_FOUND;
}
Heap* heap() const { return isolate_->heap(); }
Factory* factory() const { return isolate_->factory(); }
DirectHandle<JSAny> GetReceiver() const { return receiver_; }
template <class T>
inline DirectHandle<T> GetStoreTarget() const;
inline bool is_dictionary_holder() const;
inline DirectHandle<Map> transition_map() const;
inline DirectHandle<PropertyCell> transition_cell() const;
template <class T>
inline DirectHandle<T> GetHolder() const;
Tagged<JSObject> GetHolderForApi() const;
DirectHandle<JSAny> lookup_start_object() const {
return lookup_start_object_;
}
bool HolderIsReceiver() const;
bool HolderIsReceiverOrHiddenPrototype() const;
bool check_prototype_chain() const {
return (configuration_ & kPrototypeChain) != 0;
}
bool HasAccess() const;
inline bool ExtendingNonExtensible(DirectHandle<JSReceiver> receiver);
void PrepareForDataProperty(DirectHandle<Object> value);
void PrepareTransitionToDataProperty(DirectHandle<JSReceiver> receiver,
DirectHandle<Object> value,
PropertyAttributes attributes,
StoreOrigin store_origin);
inline bool IsCacheableTransition();
V8_WARN_UNUSED_RESULT Maybe<bool> ApplyTransitionToDataProperty(
DirectHandle<JSReceiver> receiver, Maybe<ShouldThrow> should_throw);
void ReconfigureDataProperty(DirectHandle<Object> value,
PropertyAttributes attributes);
void Delete();
void TransitionToAccessorProperty(DirectHandle<Object> getter,
DirectHandle<Object> setter,
PropertyAttributes attributes);
void TransitionToAccessorPair(DirectHandle<Object> pair,
PropertyAttributes attributes);
PropertyDetails property_details() const {
DCHECK(has_property_);
return property_details_;
}
PropertyAttributes property_attributes() const {
return property_details().attributes();
}
bool IsConfigurable() const { return property_details().IsConfigurable(); }
bool IsReadOnly() const { return property_details().IsReadOnly(); }
bool IsEnumerable() const { return property_details().IsEnumerable(); }
Representation representation() const {
return property_details().representation();
}
PropertyLocation location() const { return property_details().location(); }
PropertyConstness constness() const { return property_details().constness(); }
FieldIndex GetFieldIndex() const;
int GetFieldDescriptorIndex() const;
int GetAccessorIndex() const;
DirectHandle<PropertyCell> GetPropertyCell() const;
DirectHandle<Object> GetAccessors() const;
inline DirectHandle<InterceptorInfo> GetInterceptor() const;
DirectHandle<InterceptorInfo> GetInterceptorForFailedAccessCheck() const;
Handle<Object> GetStringPropertyValue(
AllocationPolicy allocation_policy =
AllocationPolicy::kAllocationAllowed) const;
Handle<Object> GetDataValue(AllocationPolicy allocation_policy =
AllocationPolicy::kAllocationAllowed) const;
void WriteDataValue(DirectHandle<Object> value, bool initializing_store);
DirectHandle<Object> GetDataValue(SeqCstAccessTag tag) const;
void WriteDataValue(DirectHandle<Object> value, SeqCstAccessTag tag);
DirectHandle<Object> SwapDataValue(DirectHandle<Object> value,
SeqCstAccessTag tag);
DirectHandle<Object> CompareAndSwapDataValue(DirectHandle<Object> expected,
DirectHandle<Object> value,
SeqCstAccessTag tag);
inline void UpdateProtector(MaybeDirectHandle<Object> value = {},
MaybeDirectHandle<Object> old_value = {});
static inline void UpdateProtector(Isolate* isolate,
DirectHandle<JSAny> receiver,
DirectHandle<Name> name,
MaybeDirectHandle<Object> value = {},
MaybeDirectHandle<Object> old_value = {});
bool TryLookupCachedProperty(DirectHandle<AccessorPair> accessor);
bool TryLookupCachedProperty();
static bool HasInternalMarkerProperty(Isolate* isolate,
Tagged<JSReceiver> object,
DirectHandle<Symbol> marker);
private:
friend PropertyKey;
static const size_t kInvalidIndex = std::numeric_limits<size_t>::max();
constexpr size_t AssumeValidIndex(size_t index) {
V8_ASSUME(index != kInvalidIndex);
return index;
}
bool LookupCachedProperty(DirectHandle<AccessorPair> accessor);
inline LookupIterator(Isolate* isolate, DirectHandle<JSAny> receiver,
DirectHandle<Name> name, size_t index,
DirectHandle<JSAny> lookup_start_object,
Configuration configuration);
inline LookupIterator(Isolate* isolate, Configuration configuration,
DirectHandle<JSAny> receiver, DirectHandle<Symbol> name,
DirectHandle<JSAny> lookup_start_object);
static void InternalUpdateProtector(Isolate* isolate,
DirectHandle<JSAny> receiver,
DirectHandle<Name> name,
MaybeDirectHandle<Object> value = {},
MaybeDirectHandle<Object> old_value = {});
enum class InterceptorState {
kUninitialized,
kSkipNonMasking,
kProcessNonMasking
};
DirectHandle<Map> GetReceiverMap() const;
V8_WARN_UNUSED_RESULT inline Tagged<JSReceiver> NextHolder(Tagged<Map> map);
bool is_js_array_element(bool is_element) const {
return is_element && index_ <= JSArray::kMaxArrayIndex;
}
template <bool is_element>
V8_EXPORT_PRIVATE void Start();
template <bool is_element>
void NextInternal(Tagged<Map> map, Tagged<JSReceiver> holder);
template <bool is_element>
inline State LookupInHolder(Tagged<Map> map, Tagged<JSReceiver> holder) {
return IsSpecialReceiverMap(map)
? LookupInSpecialHolder<is_element>(map, holder)
: LookupInRegularHolder<is_element>(map, holder);
}
template <bool is_element>
State LookupInRegularHolder(Tagged<Map> map, Tagged<JSReceiver> holder);
template <bool is_element>
State LookupInSpecialHolder(Tagged<Map> map, Tagged<JSReceiver> holder);
template <bool is_element>
void RestartLookupForNonMaskingInterceptors() {
RestartInternal<is_element>(InterceptorState::kProcessNonMasking);
}
template <bool is_element>
void RestartInternal(InterceptorState interceptor_state);
DirectHandle<Object> FetchValue(
AllocationPolicy allocation_policy =
AllocationPolicy::kAllocationAllowed) const;
bool CanStayConst(Tagged<Object> value) const;
bool DictCanStayConst(Tagged<Object> value) const;
template <bool is_element>
void ReloadPropertyInformation();
template <bool is_element>
bool SkipInterceptor(Tagged<JSObject> holder);
template <bool is_element>
inline Tagged<InterceptorInfo> GetInterceptor(Tagged<JSObject> holder) const;
bool check_interceptor() const {
return (configuration_ & kInterceptor) != 0;
}
inline InternalIndex descriptor_number() const;
inline InternalIndex dictionary_entry() const;
static inline Configuration ComputeConfiguration(Isolate* isolate,
Configuration configuration,
size_t index,
DirectHandle<Name> name);
Tagged<JSReceiver> GetRootForNonJSReceiver() const;
State NotFound(Tagged<JSReceiver> const holder) const;
const Configuration configuration_;
State state_ = NOT_FOUND;
bool has_property_ = false;
InterceptorState interceptor_state_ = InterceptorState::kUninitialized;
PropertyDetails property_details_ = PropertyDetails::Empty();
Isolate* const isolate_;
DirectHandle<Name> name_;
DirectHandle<UnionOf<Map, PropertyCell>> transition_;
const DirectHandle<JSAny> receiver_;
DirectHandle<JSReceiver> holder_;
const DirectHandle<JSAny> lookup_start_object_;
const size_t index_;
InternalIndex number_ = InternalIndex::NotFound();
};
class ConcurrentLookupIterator final : public AllStatic {
public:
enum Result {
kPresent,
kNotPresent,
kGaveUp,
};
V8_EXPORT_PRIVATE static std::optional<Tagged<Object>> TryGetOwnCowElement(
Isolate* isolate, Tagged<FixedArray> array_elements,
ElementsKind elements_kind, int array_length, size_t index);
V8_EXPORT_PRIVATE static Result TryGetOwnConstantElement(
Tagged<Object>* result_out, Isolate* isolate, LocalIsolate* local_isolate,
Tagged<JSObject> holder, Tagged<FixedArrayBase> elements,
ElementsKind elements_kind, size_t index);
V8_EXPORT_PRIVATE static Result TryGetOwnChar(Tagged<String>* result_out,
Isolate* isolate,
LocalIsolate* local_isolate,
Tagged<String> string,
size_t index);
V8_EXPORT_PRIVATE static std::optional<Tagged<PropertyCell>>
TryGetPropertyCell(Isolate* isolate, LocalIsolate* local_isolate,
DirectHandle<JSGlobalObject> holder,
DirectHandle<Name> name);
};
}
#endif