JSDOMConvertUnion.h [plain text]
#pragma once
#include "IDLTypes.h"
#include "JSDOMBinding.h"
#include "JSDOMConvertBase.h"
#include "JSDOMConvertBufferSource.h"
#include "JSDOMConvertInterface.h"
#include "JSDOMConvertNull.h"
#include <JavaScriptCore/IteratorOperations.h>
#include <wtf/Variant.h>
namespace WebCore {
template<typename ReturnType, bool enabled>
struct ConditionalReturner;
template<typename ReturnType>
struct ConditionalReturner<ReturnType, true> {
template<typename T>
static Optional<ReturnType> get(T&& value)
{
return ReturnType(std::forward<T>(value));
}
};
template<typename ReturnType>
struct ConditionalReturner<ReturnType, false> {
template<typename T>
static Optional<ReturnType> get(T&&)
{
return WTF::nullopt;
}
};
template<typename ReturnType, typename T, bool enabled>
struct ConditionalConverter;
template<typename ReturnType, typename T>
struct ConditionalConverter<ReturnType, T, true> {
static Optional<ReturnType> convert(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSValue value)
{
return ReturnType(Converter<T>::convert(lexicalGlobalObject, value));
}
};
template<typename ReturnType, typename T>
struct ConditionalConverter<ReturnType, T, false> {
static Optional<ReturnType> convert(JSC::JSGlobalObject&, JSC::JSValue)
{
return WTF::nullopt;
}
};
template<typename ReturnType, typename T, bool enabled>
struct ConditionalSequenceConverter;
template<typename ReturnType, typename T>
struct ConditionalSequenceConverter<ReturnType, T, true> {
static Optional<ReturnType> convert(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSObject* object, JSC::JSValue method)
{
return ReturnType(Converter<T>::convert(lexicalGlobalObject, object, method));
}
};
template<typename ReturnType, typename T>
struct ConditionalSequenceConverter<ReturnType, T, false> {
static Optional<ReturnType> convert(JSC::JSGlobalObject&, JSC::JSObject*, JSC::JSValue)
{
return WTF::nullopt;
}
};
namespace Detail {
template<typename List, bool condition>
struct ConditionalFront;
template<typename List>
struct ConditionalFront<List, true> {
using type = brigand::front<List>;
};
template<typename List>
struct ConditionalFront<List, false> {
using type = void;
};
}
template<typename List, bool condition>
using ConditionalFront = typename Detail::ConditionalFront<List, condition>::type;
template<typename... T> struct Converter<IDLUnion<T...>> : DefaultConverter<IDLUnion<T...>> {
using Type = IDLUnion<T...>;
using TypeList = typename Type::TypeList;
using ReturnType = typename Type::ImplementationType;
using NumericTypeList = brigand::filter<TypeList, IsIDLNumber<brigand::_1>>;
static constexpr size_t numberOfNumericTypes = brigand::size<NumericTypeList>::value;
static_assert(numberOfNumericTypes == 0 || numberOfNumericTypes == 1, "There can be 0 or 1 numeric types in an IDLUnion.");
using NumericType = ConditionalFront<NumericTypeList, numberOfNumericTypes != 0>;
using StringTypeList = brigand::filter<TypeList, IsIDLStringOrEnumeration<brigand::_1>>;
static constexpr size_t numberOfStringTypes = brigand::size<StringTypeList>::value;
static_assert(numberOfStringTypes == 0 || numberOfStringTypes == 1, "There can be 0 or 1 string types in an IDLUnion.");
using StringType = ConditionalFront<StringTypeList, numberOfStringTypes != 0>;
using SequenceTypeList = brigand::filter<TypeList, IsIDLSequence<brigand::_1>>;
static constexpr size_t numberOfSequenceTypes = brigand::size<SequenceTypeList>::value;
static_assert(numberOfSequenceTypes == 0 || numberOfSequenceTypes == 1, "There can be 0 or 1 sequence types in an IDLUnion.");
using SequenceType = ConditionalFront<SequenceTypeList, numberOfSequenceTypes != 0>;
using FrozenArrayTypeList = brigand::filter<TypeList, IsIDLFrozenArray<brigand::_1>>;
static constexpr size_t numberOfFrozenArrayTypes = brigand::size<FrozenArrayTypeList>::value;
static_assert(numberOfFrozenArrayTypes == 0 || numberOfFrozenArrayTypes == 1, "There can be 0 or 1 FrozenArray types in an IDLUnion.");
using FrozenArrayType = ConditionalFront<FrozenArrayTypeList, numberOfFrozenArrayTypes != 0>;
using DictionaryTypeList = brigand::filter<TypeList, IsIDLDictionary<brigand::_1>>;
static constexpr size_t numberOfDictionaryTypes = brigand::size<DictionaryTypeList>::value;
static_assert(numberOfDictionaryTypes == 0 || numberOfDictionaryTypes == 1, "There can be 0 or 1 dictionary types in an IDLUnion.");
static constexpr bool hasDictionaryType = numberOfDictionaryTypes != 0;
using DictionaryType = ConditionalFront<DictionaryTypeList, hasDictionaryType>;
using RecordTypeList = brigand::filter<TypeList, IsIDLRecord<brigand::_1>>;
static constexpr size_t numberOfRecordTypes = brigand::size<RecordTypeList>::value;
static_assert(numberOfRecordTypes == 0 || numberOfRecordTypes == 1, "There can be 0 or 1 record types in an IDLUnion.");
static constexpr bool hasRecordType = numberOfRecordTypes != 0;
using RecordType = ConditionalFront<RecordTypeList, hasRecordType>;
using ObjectTypeList = brigand::filter<TypeList, std::is_same<IDLObject, brigand::_1>>;
static constexpr size_t numberOfObjectTypes = brigand::size<ObjectTypeList>::value;
static_assert(numberOfObjectTypes == 0 || numberOfObjectTypes == 1, "There can be 0 or 1 object types in an IDLUnion.");
static constexpr bool hasObjectType = numberOfObjectTypes != 0;
using ObjectType = ConditionalFront<ObjectTypeList, hasObjectType>;
static constexpr bool hasAnyObjectType = (numberOfSequenceTypes + numberOfFrozenArrayTypes + numberOfDictionaryTypes + numberOfRecordTypes + numberOfObjectTypes) > 0;
using InterfaceTypeList = brigand::filter<TypeList, IsIDLInterface<brigand::_1>>;
using TypedArrayTypeList = brigand::filter<TypeList, IsIDLTypedArray<brigand::_1>>;
static ReturnType convert(JSC::JSGlobalObject& lexicalGlobalObject, JSC::JSValue value)
{
JSC::VM& vm = JSC::getVM(&lexicalGlobalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
constexpr bool hasNullType = brigand::any<TypeList, std::is_same<IDLNull, brigand::_1>>::value;
if (hasNullType) {
if (value.isUndefinedOrNull())
return ConditionalConverter<ReturnType, IDLNull, hasNullType>::convert(lexicalGlobalObject, value).value();
}
if (hasDictionaryType) {
if (value.isUndefinedOrNull()) {
return ConditionalConverter<ReturnType, DictionaryType, hasDictionaryType>::convert(lexicalGlobalObject, value).value();
}
}
if (brigand::any<TypeList, IsIDLInterface<brigand::_1>>::value) {
Optional<ReturnType> returnValue;
brigand::for_each<InterfaceTypeList>([&](auto&& type) {
if (returnValue)
return;
using Type = typename WTF::RemoveCVAndReference<decltype(type)>::type::type;
using ImplementationType = typename Type::ImplementationType;
using RawType = typename Type::RawType;
auto castedValue = JSToWrappedOverloader<RawType>::toWrapped(lexicalGlobalObject, value);
if (!castedValue)
return;
returnValue = ReturnType(ImplementationType(castedValue));
});
if (returnValue)
return WTFMove(returnValue.value());
}
constexpr bool hasArrayBufferType = brigand::any<TypeList, std::is_same<IDLArrayBuffer, brigand::_1>>::value;
if (hasArrayBufferType || hasObjectType) {
auto arrayBuffer = JSC::JSArrayBuffer::toWrapped(vm, value);
if (arrayBuffer) {
if (hasArrayBufferType)
return ConditionalReturner<ReturnType, hasArrayBufferType>::get(WTFMove(arrayBuffer)).value();
return ConditionalConverter<ReturnType, ObjectType, hasObjectType>::convert(lexicalGlobalObject, value).value();
}
}
constexpr bool hasArrayBufferViewType = brigand::any<TypeList, std::is_same<IDLArrayBufferView, brigand::_1>>::value;
if (hasArrayBufferViewType || hasObjectType) {
auto arrayBufferView = JSC::JSArrayBufferView::toWrapped(vm, value);
if (arrayBufferView) {
if (hasArrayBufferViewType)
return ConditionalReturner<ReturnType, hasArrayBufferViewType>::get(WTFMove(arrayBufferView)).value();
return ConditionalConverter<ReturnType, ObjectType, hasObjectType>::convert(lexicalGlobalObject, value).value();
}
}
constexpr bool hasDataViewType = brigand::any<TypeList, std::is_same<IDLDataView, brigand::_1>>::value;
if (hasDataViewType || hasObjectType) {
auto dataView = JSC::JSDataView::toWrapped(vm, value);
if (dataView) {
if (hasDataViewType)
return ConditionalReturner<ReturnType, hasDataViewType>::get(WTFMove(dataView)).value();
return ConditionalConverter<ReturnType, ObjectType, hasObjectType>::convert(lexicalGlobalObject, value).value();
}
}
constexpr bool hasTypedArrayType = brigand::any<TypeList, IsIDLTypedArray<brigand::_1>>::value;
if (hasTypedArrayType) {
Optional<ReturnType> returnValue;
brigand::for_each<TypedArrayTypeList>([&](auto&& type) {
if (returnValue)
return;
using Type = typename WTF::RemoveCVAndReference<decltype(type)>::type::type;
using ImplementationType = typename Type::ImplementationType;
using WrapperType = typename Converter<Type>::WrapperType;
auto castedValue = WrapperType::toWrapped(vm, value);
if (!castedValue)
return;
returnValue = ReturnType(ImplementationType(castedValue));
});
if (returnValue)
return WTFMove(returnValue.value());
}
if (hasAnyObjectType) {
if (value.isCell()) {
JSC::JSCell* cell = value.asCell();
if (cell->isObject()) {
auto object = asObject(value);
constexpr bool hasSequenceType = numberOfSequenceTypes != 0;
if (hasSequenceType) {
auto method = JSC::iteratorMethod(&lexicalGlobalObject, object);
RETURN_IF_EXCEPTION(scope, ReturnType());
if (!method.isUndefined())
return ConditionalSequenceConverter<ReturnType, SequenceType, hasSequenceType>::convert(lexicalGlobalObject, object, method).value();
}
constexpr bool hasFrozenArrayType = numberOfFrozenArrayTypes != 0;
if (hasFrozenArrayType) {
auto method = JSC::iteratorMethod(&lexicalGlobalObject, object);
RETURN_IF_EXCEPTION(scope, ReturnType());
if (!method.isUndefined())
return ConditionalSequenceConverter<ReturnType, FrozenArrayType, hasFrozenArrayType>::convert(lexicalGlobalObject, object, method).value();
}
if (hasDictionaryType)
return ConditionalConverter<ReturnType, DictionaryType, hasDictionaryType>::convert(lexicalGlobalObject, value).value();
if (hasRecordType)
return ConditionalConverter<ReturnType, RecordType, hasRecordType>::convert(lexicalGlobalObject, value).value();
if (hasObjectType)
return ConditionalConverter<ReturnType, ObjectType, hasObjectType>::convert(lexicalGlobalObject, value).value();
}
}
}
constexpr bool hasBooleanType = brigand::any<TypeList, std::is_same<IDLBoolean, brigand::_1>>::value;
if (hasBooleanType) {
if (value.isBoolean())
return ConditionalConverter<ReturnType, IDLBoolean, hasBooleanType>::convert(lexicalGlobalObject, value).value();
}
constexpr bool hasNumericType = brigand::size<NumericTypeList>::value != 0;
if (hasNumericType) {
if (value.isNumber())
return ConditionalConverter<ReturnType, NumericType, hasNumericType>::convert(lexicalGlobalObject, value).value();
}
constexpr bool hasStringType = brigand::size<StringTypeList>::value != 0;
if (hasStringType)
return ConditionalConverter<ReturnType, StringType, hasStringType>::convert(lexicalGlobalObject, value).value();
if (hasNumericType)
return ConditionalConverter<ReturnType, NumericType, hasNumericType>::convert(lexicalGlobalObject, value).value();
if (hasBooleanType)
return ConditionalConverter<ReturnType, IDLBoolean, hasBooleanType>::convert(lexicalGlobalObject, value).value();
throwTypeError(&lexicalGlobalObject, scope);
return ReturnType();
}
};
template<typename... T> struct JSConverter<IDLUnion<T...>> {
using Type = IDLUnion<T...>;
using TypeList = typename Type::TypeList;
using ImplementationType = typename Type::ImplementationType;
static constexpr bool needsState = true;
static constexpr bool needsGlobalObject = true;
using Sequence = brigand::make_sequence<brigand::ptrdiff_t<0>, WTF::variant_size<ImplementationType>::value>;
static JSC::JSValue convert(JSC::JSGlobalObject& lexicalGlobalObject, JSDOMGlobalObject& globalObject, const ImplementationType& variant)
{
auto index = variant.index();
Optional<JSC::JSValue> returnValue;
brigand::for_each<Sequence>([&](auto&& type) {
using I = typename WTF::RemoveCVAndReference<decltype(type)>::type::type;
if (I::value == index) {
ASSERT(!returnValue);
returnValue = toJS<brigand::at<TypeList, I>>(lexicalGlobalObject, globalObject, WTF::get<I::value>(variant));
}
});
ASSERT(returnValue);
return returnValue.value();
}
};
}