#ifndef SourceCodeKey_h
#define SourceCodeKey_h
#include "ParserModes.h"
#include "SourceCode.h"
#include <wtf/HashTraits.h>
namespace JSC {
enum class SourceCodeType { EvalType, ProgramType, FunctionType, ModuleType };
class SourceCodeFlags {
public:
SourceCodeFlags() = default;
SourceCodeFlags(SourceCodeType codeType, JSParserBuiltinMode builtinMode, JSParserStrictMode strictMode, DerivedContextType derivedContextType, EvalContextType evalContextType, bool isArrowFunctionContext)
: m_flags(
(static_cast<unsigned>(isArrowFunctionContext) << 7) |
(static_cast<unsigned>(evalContextType) << 6) |
(static_cast<unsigned>(derivedContextType) << 4) |
(static_cast<unsigned>(codeType) << 2) |
(static_cast<unsigned>(builtinMode) << 1) |
(static_cast<unsigned>(strictMode))
)
{
}
inline bool operator==(const SourceCodeFlags& rhs) const
{
return m_flags == rhs.m_flags;
}
private:
unsigned m_flags { 0 };
};
class SourceCodeKey {
public:
SourceCodeKey()
{
}
SourceCodeKey(const SourceCode& sourceCode, const String& name, SourceCodeType codeType, JSParserBuiltinMode builtinMode, JSParserStrictMode strictMode, DerivedContextType derivedContextType, EvalContextType evalContextType, bool isArrowFunctionContext)
: m_sourceCode(sourceCode)
, m_name(name)
, m_flags(codeType, builtinMode, strictMode, derivedContextType, evalContextType, isArrowFunctionContext)
, m_hash(sourceCode.hash())
{
}
SourceCodeKey(WTF::HashTableDeletedValueType)
: m_sourceCode(WTF::HashTableDeletedValue)
{
}
bool isHashTableDeletedValue() const { return m_sourceCode.isHashTableDeletedValue(); }
unsigned hash() const { return m_hash; }
size_t length() const { return m_sourceCode.length(); }
bool isNull() const { return m_sourceCode.isNull(); }
StringView string() const { return m_sourceCode.view(); }
bool operator==(const SourceCodeKey& other) const
{
return m_hash == other.m_hash
&& length() == other.length()
&& m_flags == other.m_flags
&& m_name == other.m_name
&& string() == other.string();
}
struct Hash {
static unsigned hash(const SourceCodeKey& key) { return key.hash(); }
static bool equal(const SourceCodeKey& a, const SourceCodeKey& b) { return a == b; }
static const bool safeToCompareToEmptyOrDeleted = false;
};
struct HashTraits : SimpleClassHashTraits<SourceCodeKey> {
static const bool hasIsEmptyValueFunction = true;
static bool isEmptyValue(const SourceCodeKey& key) { return key.isNull(); }
};
private:
SourceCode m_sourceCode;
String m_name;
SourceCodeFlags m_flags;
unsigned m_hash;
};
}
#endif // SourceCodeKey_h