#pragma once
#include "DOMAnnotation.h"
#include "JSCJSValue.h"
#include "PropertyName.h"
#include "PropertyOffset.h"
#include "ScopeOffset.h"
#include <wtf/Assertions.h>
#include <wtf/ForbidHeapAllocation.h>
namespace JSC {
class ExecState;
class GetterSetter;
class JSObject;
class JSModuleEnvironment;
enum class PropertyAttribute : unsigned {
None = 0,
ReadOnly = 1 << 1, DontEnum = 1 << 2, DontDelete = 1 << 3, Accessor = 1 << 4, CustomAccessor = 1 << 5,
Function = 1 << 8, Builtin = 1 << 9, ConstantInteger = 1 << 10, CellProperty = 1 << 11, ClassStructure = 1 << 12, PropertyCallback = 1 << 13, DOMAttribute = 1 << 14, DOMJITAttribute = 1 << 15, DOMJITFunction = 1 << 16, BuiltinOrFunction = Builtin | Function, BuiltinOrFunctionOrLazyProperty = Builtin | Function | CellProperty | ClassStructure | PropertyCallback, BuiltinOrFunctionOrAccessorOrLazyProperty = Builtin | Function | Accessor | CellProperty | ClassStructure | PropertyCallback, BuiltinOrFunctionOrAccessorOrLazyPropertyOrConstant = Builtin | Function | Accessor | CellProperty | ClassStructure | PropertyCallback | ConstantInteger };
static constexpr unsigned operator| (PropertyAttribute a, PropertyAttribute b) { return static_cast<unsigned>(a) | static_cast<unsigned>(b); }
static constexpr unsigned operator| (unsigned a, PropertyAttribute b) { return a | static_cast<unsigned>(b); }
static constexpr unsigned operator| (PropertyAttribute a, unsigned b) { return static_cast<unsigned>(a) | b; }
static constexpr unsigned operator&(unsigned a, PropertyAttribute b) { return a & static_cast<unsigned>(b); }
static constexpr bool operator<(PropertyAttribute a, PropertyAttribute b) { return static_cast<unsigned>(a) < static_cast<unsigned>(b); }
static constexpr unsigned operator~(PropertyAttribute a) { return ~static_cast<unsigned>(a); }
static constexpr bool operator<(PropertyAttribute a, unsigned b) { return static_cast<unsigned>(a) < b; }
static inline unsigned& operator|=(unsigned& a, PropertyAttribute b) { return a |= static_cast<unsigned>(b); }
enum CacheabilityType : uint8_t {
CachingDisallowed,
CachingAllowed
};
inline unsigned attributesForStructure(unsigned attributes)
{
return static_cast<uint8_t>(attributes);
}
class PropertySlot {
WTF_FORBID_HEAP_ALLOCATION;
enum PropertyType : uint8_t {
TypeUnset,
TypeValue,
TypeGetter,
TypeCustom,
TypeCustomAccessor,
};
public:
enum class InternalMethodType : uint8_t {
Get, HasProperty, GetOwnProperty, VMInquiry, };
enum class AdditionalDataType : uint8_t {
None,
DOMAttribute, ModuleNamespace, };
explicit PropertySlot(const JSValue thisValue, InternalMethodType internalMethodType)
: m_offset(invalidOffset)
, m_thisValue(thisValue)
, m_slotBase(nullptr)
, m_watchpointSet(nullptr)
, m_cacheability(CachingAllowed)
, m_propertyType(TypeUnset)
, m_internalMethodType(internalMethodType)
, m_additionalDataType(AdditionalDataType::None)
, m_isTaintedByOpaqueObject(false)
{
}
typedef EncodedJSValue (*GetValueFunc)(ExecState*, EncodedJSValue thisValue, PropertyName);
JSValue getValue(ExecState*, PropertyName) const;
JSValue getValue(ExecState*, unsigned propertyName) const;
JSValue getPureResult() const;
bool isCacheable() const { return m_cacheability == CachingAllowed && m_offset != invalidOffset; }
bool isUnset() const { return m_propertyType == TypeUnset; }
bool isValue() const { return m_propertyType == TypeValue; }
bool isAccessor() const { return m_propertyType == TypeGetter; }
bool isCustom() const { return m_propertyType == TypeCustom; }
bool isCustomAccessor() const { return m_propertyType == TypeCustomAccessor; }
bool isCacheableValue() const { return isCacheable() && isValue(); }
bool isCacheableGetter() const { return isCacheable() && isAccessor(); }
bool isCacheableCustom() const { return isCacheable() && isCustom(); }
void setIsTaintedByOpaqueObject() { m_isTaintedByOpaqueObject = true; }
bool isTaintedByOpaqueObject() const { return m_isTaintedByOpaqueObject; }
InternalMethodType internalMethodType() const { return m_internalMethodType; }
void disableCaching()
{
m_cacheability = CachingDisallowed;
}
unsigned attributes() const { return m_attributes; }
PropertyOffset cachedOffset() const
{
ASSERT(isCacheable());
return m_offset;
}
GetterSetter* getterSetter() const
{
ASSERT(isAccessor());
return m_data.getter.getterSetter;
}
GetValueFunc customGetter() const
{
ASSERT(isCacheableCustom());
return m_data.custom.getValue;
}
CustomGetterSetter* customGetterSetter() const
{
ASSERT(isCustomAccessor());
return m_data.customAccessor.getterSetter;
}
JSObject* slotBase() const
{
return m_slotBase;
}
WatchpointSet* watchpointSet() const
{
return m_watchpointSet;
}
std::optional<DOMAttributeAnnotation> domAttribute() const
{
if (m_additionalDataType == AdditionalDataType::DOMAttribute)
return m_additionalData.domAttribute;
return std::nullopt;
}
struct ModuleNamespaceSlot {
JSModuleEnvironment* environment;
unsigned scopeOffset;
};
std::optional<ModuleNamespaceSlot> moduleNamespaceSlot() const
{
if (m_additionalDataType == AdditionalDataType::ModuleNamespace)
return m_additionalData.moduleNamespaceSlot;
return std::nullopt;
}
void setValue(JSObject* slotBase, unsigned attributes, JSValue value)
{
ASSERT(attributes == attributesForStructure(attributes));
m_data.value = JSValue::encode(value);
m_attributes = attributes;
ASSERT(slotBase);
m_slotBase = slotBase;
m_propertyType = TypeValue;
m_offset = invalidOffset;
}
void setValue(JSObject* slotBase, unsigned attributes, JSValue value, PropertyOffset offset)
{
ASSERT(attributes == attributesForStructure(attributes));
ASSERT(value);
m_data.value = JSValue::encode(value);
m_attributes = attributes;
ASSERT(slotBase);
m_slotBase = slotBase;
m_propertyType = TypeValue;
m_offset = offset;
}
void setValue(JSString*, unsigned attributes, JSValue value)
{
ASSERT(attributes == attributesForStructure(attributes));
ASSERT(value);
m_data.value = JSValue::encode(value);
m_attributes = attributes;
m_slotBase = 0;
m_propertyType = TypeValue;
m_offset = invalidOffset;
}
void setValueModuleNamespace(JSObject* slotBase, unsigned attributes, JSValue value, JSModuleEnvironment* environment, ScopeOffset scopeOffset)
{
setValue(slotBase, attributes, value);
m_additionalDataType = AdditionalDataType::ModuleNamespace;
m_additionalData.moduleNamespaceSlot.environment = environment;
m_additionalData.moduleNamespaceSlot.scopeOffset = scopeOffset.offset();
}
void setCustom(JSObject* slotBase, unsigned attributes, GetValueFunc getValue)
{
ASSERT(attributes == attributesForStructure(attributes));
ASSERT(getValue);
assertIsCFunctionPtr(getValue);
m_data.custom.getValue = getValue;
m_attributes = attributes;
ASSERT(slotBase);
m_slotBase = slotBase;
m_propertyType = TypeCustom;
m_offset = invalidOffset;
}
void setCustom(JSObject* slotBase, unsigned attributes, GetValueFunc getValue, DOMAttributeAnnotation domAttribute)
{
setCustom(slotBase, attributes, getValue);
m_additionalDataType = AdditionalDataType::DOMAttribute;
m_additionalData.domAttribute = domAttribute;
}
void setCacheableCustom(JSObject* slotBase, unsigned attributes, GetValueFunc getValue)
{
ASSERT(attributes == attributesForStructure(attributes));
ASSERT(getValue);
assertIsCFunctionPtr(getValue);
m_data.custom.getValue = getValue;
m_attributes = attributes;
ASSERT(slotBase);
m_slotBase = slotBase;
m_propertyType = TypeCustom;
m_offset = !invalidOffset;
}
void setCacheableCustom(JSObject* slotBase, unsigned attributes, GetValueFunc getValue, DOMAttributeAnnotation domAttribute)
{
setCacheableCustom(slotBase, attributes, getValue);
m_additionalDataType = AdditionalDataType::DOMAttribute;
m_additionalData.domAttribute = domAttribute;
}
void setCustomGetterSetter(JSObject* slotBase, unsigned attributes, CustomGetterSetter* getterSetter)
{
ASSERT(attributes == attributesForStructure(attributes));
disableCaching();
ASSERT(getterSetter);
m_data.customAccessor.getterSetter = getterSetter;
m_attributes = attributes;
ASSERT(slotBase);
m_slotBase = slotBase;
m_propertyType = TypeCustomAccessor;
m_offset = invalidOffset;
}
void setGetterSlot(JSObject* slotBase, unsigned attributes, GetterSetter* getterSetter)
{
ASSERT(attributes == attributesForStructure(attributes));
ASSERT(getterSetter);
m_data.getter.getterSetter = getterSetter;
m_attributes = attributes;
ASSERT(slotBase);
m_slotBase = slotBase;
m_propertyType = TypeGetter;
m_offset = invalidOffset;
}
void setCacheableGetterSlot(JSObject* slotBase, unsigned attributes, GetterSetter* getterSetter, PropertyOffset offset)
{
ASSERT(attributes == attributesForStructure(attributes));
ASSERT(getterSetter);
m_data.getter.getterSetter = getterSetter;
m_attributes = attributes;
ASSERT(slotBase);
m_slotBase = slotBase;
m_propertyType = TypeGetter;
m_offset = offset;
}
JSValue thisValue() const
{
return m_thisValue;
}
void setThisValue(JSValue thisValue)
{
m_thisValue = thisValue;
}
void setUndefined()
{
m_data.value = JSValue::encode(jsUndefined());
m_attributes = PropertyAttribute::ReadOnly | PropertyAttribute::DontDelete | PropertyAttribute::DontEnum;
m_slotBase = 0;
m_propertyType = TypeValue;
m_offset = invalidOffset;
}
void setWatchpointSet(WatchpointSet& set)
{
m_watchpointSet = &set;
}
private:
JS_EXPORT_PRIVATE JSValue functionGetter(ExecState*) const;
JS_EXPORT_PRIVATE JSValue customGetter(ExecState*, PropertyName) const;
JS_EXPORT_PRIVATE JSValue customAccessorGetter(ExecState*, PropertyName) const;
unsigned m_attributes;
union {
EncodedJSValue value;
struct {
GetterSetter* getterSetter;
} getter;
struct {
GetValueFunc getValue;
} custom;
struct {
CustomGetterSetter* getterSetter;
} customAccessor;
} m_data;
PropertyOffset m_offset;
JSValue m_thisValue;
JSObject* m_slotBase;
WatchpointSet* m_watchpointSet;
CacheabilityType m_cacheability;
PropertyType m_propertyType;
InternalMethodType m_internalMethodType;
AdditionalDataType m_additionalDataType;
union {
DOMAttributeAnnotation domAttribute;
ModuleNamespaceSlot moduleNamespaceSlot;
} m_additionalData;
bool m_isTaintedByOpaqueObject;
};
ALWAYS_INLINE JSValue PropertySlot::getValue(ExecState* exec, PropertyName propertyName) const
{
if (m_propertyType == TypeValue)
return JSValue::decode(m_data.value);
if (m_propertyType == TypeGetter)
return functionGetter(exec);
if (m_propertyType == TypeCustomAccessor)
return customAccessorGetter(exec, propertyName);
return customGetter(exec, propertyName);
}
ALWAYS_INLINE JSValue PropertySlot::getValue(ExecState* exec, unsigned propertyName) const
{
if (m_propertyType == TypeValue)
return JSValue::decode(m_data.value);
if (m_propertyType == TypeGetter)
return functionGetter(exec);
if (m_propertyType == TypeCustomAccessor)
return customAccessorGetter(exec, Identifier::from(exec, propertyName));
return customGetter(exec, Identifier::from(exec, propertyName));
}
}