PropertyCondition.h [plain text]
#ifndef PropertyCondition_h
#define PropertyCondition_h
#include "JSObject.h"
#include <wtf/HashMap.h>
namespace JSC {
class TrackedReferences;
class PropertyCondition {
public:
enum Kind {
Presence,
Absence,
AbsenceOfSetter,
Equivalence };
PropertyCondition()
: m_uid(nullptr)
, m_kind(Presence)
{
memset(&u, 0, sizeof(u));
}
PropertyCondition(WTF::HashTableDeletedValueType)
: m_uid(nullptr)
, m_kind(Absence)
{
memset(&u, 0, sizeof(u));
}
static PropertyCondition presenceWithoutBarrier(UniquedStringImpl* uid, PropertyOffset offset, unsigned attributes)
{
PropertyCondition result;
result.m_uid = uid;
result.m_kind = Presence;
result.u.presence.offset = offset;
result.u.presence.attributes = attributes;
return result;
}
static PropertyCondition presence(
VM&, JSCell*, UniquedStringImpl* uid, PropertyOffset offset, unsigned attributes)
{
return presenceWithoutBarrier(uid, offset, attributes);
}
static PropertyCondition absenceWithoutBarrier(UniquedStringImpl* uid, JSObject* prototype)
{
PropertyCondition result;
result.m_uid = uid;
result.m_kind = Absence;
result.u.absence.prototype = prototype;
return result;
}
static PropertyCondition absence(
VM& vm, JSCell* owner, UniquedStringImpl* uid, JSObject* prototype)
{
if (owner)
vm.heap.writeBarrier(owner);
return absenceWithoutBarrier(uid, prototype);
}
static PropertyCondition absenceOfSetterWithoutBarrier(
UniquedStringImpl* uid, JSObject* prototype)
{
PropertyCondition result;
result.m_uid = uid;
result.m_kind = AbsenceOfSetter;
result.u.absence.prototype = prototype;
return result;
}
static PropertyCondition absenceOfSetter(
VM& vm, JSCell* owner, UniquedStringImpl* uid, JSObject* prototype)
{
if (owner)
vm.heap.writeBarrier(owner);
return absenceOfSetterWithoutBarrier(uid, prototype);
}
static PropertyCondition equivalenceWithoutBarrier(
UniquedStringImpl* uid, JSValue value)
{
PropertyCondition result;
result.m_uid = uid;
result.m_kind = Equivalence;
result.u.equivalence.value = JSValue::encode(value);
return result;
}
static PropertyCondition equivalence(
VM& vm, JSCell* owner, UniquedStringImpl* uid, JSValue value)
{
if (value.isCell() && owner)
vm.heap.writeBarrier(owner);
return equivalenceWithoutBarrier(uid, value);
}
explicit operator bool() const { return m_uid || m_kind != Presence; }
Kind kind() const { return m_kind; }
UniquedStringImpl* uid() const { return m_uid; }
bool hasOffset() const { return !!*this && m_kind == Presence; };
PropertyOffset offset() const
{
ASSERT(hasOffset());
return u.presence.offset;
}
bool hasAttributes() const { return !!*this && m_kind == Presence; };
unsigned attributes() const
{
ASSERT(hasAttributes());
return u.presence.attributes;
}
bool hasPrototype() const { return !!*this && (m_kind == Absence || m_kind == AbsenceOfSetter); }
JSObject* prototype() const
{
ASSERT(hasPrototype());
return u.absence.prototype;
}
bool hasRequiredValue() const { return !!*this && m_kind == Equivalence; }
JSValue requiredValue() const
{
ASSERT(hasRequiredValue());
return JSValue::decode(u.equivalence.value);
}
void dumpInContext(PrintStream&, DumpContext*) const;
void dump(PrintStream&) const;
unsigned hash() const
{
unsigned result = WTF::PtrHash<UniquedStringImpl*>::hash(m_uid) + static_cast<unsigned>(m_kind);
switch (m_kind) {
case Presence:
result ^= u.presence.offset;
result ^= u.presence.attributes;
break;
case Absence:
case AbsenceOfSetter:
result ^= WTF::PtrHash<JSObject*>::hash(u.absence.prototype);
break;
case Equivalence:
result ^= EncodedJSValueHash::hash(u.equivalence.value);
break;
}
return result;
}
bool operator==(const PropertyCondition& other) const
{
if (m_uid != other.m_uid)
return false;
if (m_kind != other.m_kind)
return false;
switch (m_kind) {
case Presence:
return u.presence.offset == other.u.presence.offset
&& u.presence.attributes == other.u.presence.attributes;
case Absence:
case AbsenceOfSetter:
return u.absence.prototype == other.u.absence.prototype;
case Equivalence:
return u.equivalence.value == other.u.equivalence.value;
}
RELEASE_ASSERT_NOT_REACHED();
return false;
}
bool isHashTableDeletedValue() const
{
return !m_uid && m_kind == Absence;
}
bool isCompatibleWith(const PropertyCondition& other) const
{
if (!*this || !other)
return false;
return *this == other || uid() != other.uid();
}
bool isStillValidAssumingImpurePropertyWatchpoint(Structure*, JSObject* base = nullptr) const;
bool validityRequiresImpurePropertyWatchpoint(Structure*) const;
bool isStillValid(Structure*, JSObject* base = nullptr) const;
enum WatchabilityEffort {
MakeNoChanges,
EnsureWatchability
};
bool isWatchableAssumingImpurePropertyWatchpoint(
Structure*, JSObject* base = nullptr, WatchabilityEffort = MakeNoChanges) const;
bool isWatchable(
Structure*, JSObject* base = nullptr, WatchabilityEffort = MakeNoChanges) const;
bool watchingRequiresStructureTransitionWatchpoint() const
{
return !!*this;
}
bool watchingRequiresReplacementWatchpoint() const
{
return !!*this && m_kind == Equivalence;
}
bool isStillLive() const;
void validateReferences(const TrackedReferences&) const;
static bool isValidValueForAttributes(JSValue value, unsigned attributes);
bool isValidValueForPresence(JSValue) const;
PropertyCondition attemptToMakeEquivalenceWithoutBarrier(JSObject* base) const;
private:
bool isWatchableWhenValid(Structure*, WatchabilityEffort) const;
UniquedStringImpl* m_uid;
Kind m_kind;
union {
struct {
PropertyOffset offset;
unsigned attributes;
} presence;
struct {
JSObject* prototype;
} absence;
struct {
EncodedJSValue value;
} equivalence;
} u;
};
struct PropertyConditionHash {
static unsigned hash(const PropertyCondition& key) { return key.hash(); }
static bool equal(
const PropertyCondition& a, const PropertyCondition& b)
{
return a == b;
}
static const bool safeToCompareToEmptyOrDeleted = true;
};
}
namespace WTF {
void printInternal(PrintStream&, JSC::PropertyCondition::Kind);
template<typename T> struct DefaultHash;
template<> struct DefaultHash<JSC::PropertyCondition> {
typedef JSC::PropertyConditionHash Hash;
};
template<typename T> struct HashTraits;
template<> struct HashTraits<JSC::PropertyCondition> : SimpleClassHashTraits<JSC::PropertyCondition> { };
}
#endif // PropertyCondition_h