JSTestStandaloneEnumeration.cpp [plain text]
#include "config.h"
#if ENABLE(CONDITION)
#include "JSTestStandaloneEnumeration.h"
#include <runtime/JSString.h>
#include <wtf/NeverDestroyed.h>
using namespace JSC;
namespace WebCore {
template<> JSString* convertEnumerationToJS(ExecState& state, TestStandaloneEnumeration enumerationValue)
{
static NeverDestroyed<const String> values[] = {
ASCIILiteral("enumValue1"),
ASCIILiteral("enumValue2"),
};
static_assert(static_cast<size_t>(TestStandaloneEnumeration::EnumValue1) == 0, "TestStandaloneEnumeration::EnumValue1 is not 0 as expected");
static_assert(static_cast<size_t>(TestStandaloneEnumeration::EnumValue2) == 1, "TestStandaloneEnumeration::EnumValue2 is not 1 as expected");
ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values));
return jsStringWithCache(&state, values[static_cast<size_t>(enumerationValue)]);
}
template<> std::optional<TestStandaloneEnumeration> parseEnumeration<TestStandaloneEnumeration>(ExecState& state, JSValue value)
{
auto stringValue = value.toWTFString(&state);
if (stringValue == "enumValue1")
return TestStandaloneEnumeration::EnumValue1;
if (stringValue == "enumValue2")
return TestStandaloneEnumeration::EnumValue2;
return std::nullopt;
}
template<> TestStandaloneEnumeration convertEnumeration<TestStandaloneEnumeration>(ExecState& state, JSValue value)
{
VM& vm = state.vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
auto result = parseEnumeration<TestStandaloneEnumeration>(state, value);
if (UNLIKELY(!result)) {
throwTypeError(&state, throwScope);
return { };
}
return result.value();
}
template<> const char* expectedEnumerationValues<TestStandaloneEnumeration>()
{
return "\"enumValue1\", \"enumValue2\"";
}
}
#endif // ENABLE(CONDITION)