#pragma once
#include "CodeLocation.h"
#include <wtf/Vector.h>
namespace JSC {
enum class HandlerType {
Catch = 0,
Finally = 1,
SynthesizedCatch = 2,
SynthesizedFinally = 3
};
enum class RequiredHandler {
CatchHandler,
AnyHandler
};
struct HandlerInfoBase {
HandlerType type() const { return static_cast<HandlerType>(typeBits); }
void setType(HandlerType type) { typeBits = static_cast<uint32_t>(type); }
const char* typeName() const
{
switch (type()) {
case HandlerType::Catch:
return "catch";
case HandlerType::Finally:
return "finally";
case HandlerType::SynthesizedCatch:
return "synthesized catch";
case HandlerType::SynthesizedFinally:
return "synthesized finally";
default:
ASSERT_NOT_REACHED();
}
return nullptr;
}
bool isCatchHandler() const { return type() == HandlerType::Catch; }
template<typename Handler>
static Handler* handlerForIndex(Vector<Handler>& exeptionHandlers, unsigned index, RequiredHandler requiredHandler)
{
for (Handler& handler : exeptionHandlers) {
if ((requiredHandler == RequiredHandler::CatchHandler) && !handler.isCatchHandler())
continue;
if (handler.start <= index && handler.end > index)
return &handler;
}
return nullptr;
}
uint32_t start;
uint32_t end;
uint32_t target;
uint32_t typeBits : 2; };
struct UnlinkedHandlerInfo : public HandlerInfoBase {
UnlinkedHandlerInfo(uint32_t start, uint32_t end, uint32_t target, HandlerType handlerType)
{
this->start = start;
this->end = end;
this->target = target;
setType(handlerType);
ASSERT(type() == handlerType);
}
};
struct HandlerInfo : public HandlerInfoBase {
void initialize(const UnlinkedHandlerInfo& unlinkedInfo)
{
start = unlinkedInfo.start;
end = unlinkedInfo.end;
target = unlinkedInfo.target;
typeBits = unlinkedInfo.typeBits;
}
#if ENABLE(JIT)
void initialize(const UnlinkedHandlerInfo& unlinkedInfo, CodeLocationLabel label)
{
initialize(unlinkedInfo);
nativeCode = label;
}
CodeLocationLabel nativeCode;
#endif
};
}