#pragma once
#if ENABLE(WEBASSEMBLY)
#if COMPILER(GCC) && ASSERT_DISABLED
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
#endif // COMPILER(GCC) && ASSERT_DISABLED
namespace JSC { namespace Wasm {
#define FOR_EACH_KNOWN_WASM_SECTION(macro) \
macro(Type, 1, "Function signature declarations") \
macro(Import, 2, "Import declarations") \
macro(Function, 3, "Function declarations") \
macro(Table, 4, "Indirect function table and other tables") \
macro(Memory, 5, "Memory attributes") \
macro(Global, 6, "Global declarations") \
macro(Export, 7, "Exports") \
macro(Start, 8, "Start function declaration") \
macro(Element, 9, "Elements section") \
macro(Code, 10, "Function bodies (code)") \
macro(Data, 11, "Data segments")
enum class Section : uint8_t {
Begin = 0,
#define DEFINE_WASM_SECTION_ENUM(NAME, ID, DESCRIPTION) NAME = ID,
FOR_EACH_KNOWN_WASM_SECTION(DEFINE_WASM_SECTION_ENUM)
#undef DEFINE_WASM_SECTION_ENUM
Custom
};
static_assert(static_cast<uint8_t>(Section::Begin) < static_cast<uint8_t>(Section::Type), "Begin should come before the first known section.");
template<typename Int>
inline bool isKnownSection(Int section)
{
switch (section) {
#define VALIDATE_SECTION(NAME, ID, DESCRIPTION) case static_cast<Int>(Section::NAME): return true;
FOR_EACH_KNOWN_WASM_SECTION(VALIDATE_SECTION)
#undef VALIDATE_SECTION
default:
return false;
}
}
inline bool decodeSection(uint8_t sectionByte, Section& section)
{
section = Section::Custom;
if (!sectionByte)
return true;
if (!isKnownSection(sectionByte))
return false;
section = static_cast<Section>(sectionByte);
return true;
}
inline bool validateOrder(Section previousKnown, Section next)
{
ASSERT(isKnownSection(previousKnown) || previousKnown == Section::Begin);
return static_cast<uint8_t>(previousKnown) < static_cast<uint8_t>(next);
}
inline const char* makeString(Section section)
{
switch (section) {
case Section::Begin:
return "Begin";
case Section::Custom:
return "Custom";
#define STRINGIFY_SECTION_NAME(NAME, ID, DESCRIPTION) case Section::NAME: return #NAME;
FOR_EACH_KNOWN_WASM_SECTION(STRINGIFY_SECTION_NAME)
#undef STRINGIFY_SECTION_NAME
}
ASSERT_NOT_REACHED();
}
} }
#if COMPILER(GCC) && ASSERT_DISABLED
#pragma GCC diagnostic pop
#endif // COMPILER(GCC) && ASSERT_DISABLED
#endif // ENABLE(WEBASSEMBLY)