JSDOMConvertNumbers.h [plain text]
#pragma once
#include "IDLTypes.h"
#include "JSDOMConvertBase.h"
#include "JSDOMExceptionHandling.h"
#include <JavaScriptCore/JSCJSValueInlines.h>
#include <JavaScriptCore/PureNaN.h>
namespace WebCore {
template<typename T> T convertToInteger(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int8_t convertToInteger<int8_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint8_t convertToInteger<uint8_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int16_t convertToInteger<int16_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint16_t convertToInteger<uint16_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int32_t convertToInteger<int32_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint32_t convertToInteger<uint32_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int64_t convertToInteger<int64_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint64_t convertToInteger<uint64_t>(JSC::ExecState&, JSC::JSValue);
template<typename T> T convertToIntegerEnforceRange(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int8_t convertToIntegerEnforceRange<int8_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint8_t convertToIntegerEnforceRange<uint8_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int16_t convertToIntegerEnforceRange<int16_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint16_t convertToIntegerEnforceRange<uint16_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int32_t convertToIntegerEnforceRange<int32_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint32_t convertToIntegerEnforceRange<uint32_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int64_t convertToIntegerEnforceRange<int64_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint64_t convertToIntegerEnforceRange<uint64_t>(JSC::ExecState&, JSC::JSValue);
template<typename T> T convertToIntegerClamp(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int8_t convertToIntegerClamp<int8_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint8_t convertToIntegerClamp<uint8_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int16_t convertToIntegerClamp<int16_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint16_t convertToIntegerClamp<uint16_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int32_t convertToIntegerClamp<int32_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint32_t convertToIntegerClamp<uint32_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT int64_t convertToIntegerClamp<int64_t>(JSC::ExecState&, JSC::JSValue);
template<> WEBCORE_EXPORT uint64_t convertToIntegerClamp<uint64_t>(JSC::ExecState&, JSC::JSValue);
template<> struct Converter<IDLByte> : DefaultConverter<IDLByte> {
static int8_t convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToInteger<int8_t>(state, value);
}
};
template<> struct JSConverter<IDLByte> {
using Type = typename IDLByte::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLOctet> : DefaultConverter<IDLOctet> {
static uint8_t convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToInteger<uint8_t>(state, value);
}
};
template<> struct JSConverter<IDLOctet> {
using Type = typename IDLOctet::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLShort> : DefaultConverter<IDLShort> {
static int16_t convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToInteger<int16_t>(state, value);
}
};
template<> struct JSConverter<IDLShort> {
using Type = typename IDLShort::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLUnsignedShort> : DefaultConverter<IDLUnsignedShort> {
static uint16_t convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToInteger<uint16_t>(state, value);
}
};
template<> struct JSConverter<IDLUnsignedShort> {
using Type = typename IDLUnsignedShort::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLLong> : DefaultConverter<IDLLong> {
static inline int32_t convert(JSC::ExecState&, JSC::ThrowScope&, double number)
{
return JSC::toInt32(number);
}
static int32_t convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToInteger<int32_t>(state, value);
}
};
template<> struct JSConverter<IDLLong> {
using Type = typename IDLLong::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLUnsignedLong> : DefaultConverter<IDLUnsignedLong> {
static uint32_t convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToInteger<uint32_t>(state, value);
}
};
template<> struct JSConverter<IDLUnsignedLong> {
using Type = typename IDLUnsignedLong::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLLongLong> : DefaultConverter<IDLLongLong> {
static int64_t convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToInteger<int64_t>(state, value);
}
};
template<> struct JSConverter<IDLLongLong> {
using Type = typename IDLLongLong::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLUnsignedLongLong> : DefaultConverter<IDLUnsignedLongLong> {
static uint64_t convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToInteger<uint64_t>(state, value);
}
};
template<> struct JSConverter<IDLUnsignedLongLong> {
using Type = typename IDLUnsignedLongLong::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<typename T> struct Converter<IDLClampAdaptor<T>> : DefaultConverter<IDLClampAdaptor<T>> {
using ReturnType = typename IDLClampAdaptor<T>::ImplementationType;
static ReturnType convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToIntegerClamp<ReturnType>(state, value);
}
};
template<typename T> struct JSConverter<IDLClampAdaptor<T>> {
using Type = typename IDLClampAdaptor<T>::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSConverter<T>::convert(value);
}
};
template<typename T> struct Converter<IDLEnforceRangeAdaptor<T>> : DefaultConverter<IDLEnforceRangeAdaptor<T>> {
using ReturnType = typename IDLEnforceRangeAdaptor<T>::ImplementationType;
static ReturnType convert(JSC::ExecState& state, JSC::JSValue value)
{
return convertToIntegerEnforceRange<ReturnType>(state, value);
}
};
template<typename T> struct JSConverter<IDLEnforceRangeAdaptor<T>> {
using Type = typename IDLEnforceRangeAdaptor<T>::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSConverter<T>::convert(value);
}
};
template<> struct Converter<IDLFloat> : DefaultConverter<IDLFloat> {
static inline float convert(JSC::ExecState& state, JSC::ThrowScope& scope, double number)
{
if (UNLIKELY(!std::isfinite(number)))
throwNonFiniteTypeError(state, scope);
return static_cast<float>(number);
}
static float convert(JSC::ExecState& state, JSC::JSValue value)
{
JSC::VM& vm = state.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
double number = value.toNumber(&state);
if (UNLIKELY(!std::isfinite(number)))
throwNonFiniteTypeError(state, scope);
return static_cast<float>(number);
}
};
template<> struct JSConverter<IDLFloat> {
using Type = typename IDLFloat::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLUnrestrictedFloat> : DefaultConverter<IDLUnrestrictedFloat> {
static inline float convert(JSC::ExecState&, JSC::ThrowScope&, double number)
{
return static_cast<float>(number);
}
static float convert(JSC::ExecState& state, JSC::JSValue value)
{
return static_cast<float>(value.toNumber(&state));
}
};
template<> struct JSConverter<IDLUnrestrictedFloat> {
using Type = typename IDLUnrestrictedFloat::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLDouble> : DefaultConverter<IDLDouble> {
static inline double convert(JSC::ExecState& state, JSC::ThrowScope& scope, double number)
{
if (UNLIKELY(!std::isfinite(number)))
throwNonFiniteTypeError(state, scope);
return number;
}
static double convert(JSC::ExecState& state, JSC::JSValue value)
{
JSC::VM& vm = state.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
double number = value.toNumber(&state);
if (UNLIKELY(!std::isfinite(number)))
throwNonFiniteTypeError(state, scope);
return number;
}
};
template<> struct JSConverter<IDLDouble> {
using Type = typename IDLDouble::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
ASSERT(!std::isnan(value));
return JSC::jsNumber(value);
}
};
template<> struct Converter<IDLUnrestrictedDouble> : DefaultConverter<IDLUnrestrictedDouble> {
static inline double convert(JSC::ExecState&, JSC::ThrowScope&, double number)
{
return number;
}
static double convert(JSC::ExecState& state, JSC::JSValue value)
{
return value.toNumber(&state);
}
};
template<> struct JSConverter<IDLUnrestrictedDouble> {
using Type = typename IDLUnrestrictedDouble::ImplementationType;
static constexpr bool needsState = false;
static constexpr bool needsGlobalObject = false;
static JSC::JSValue convert(Type value)
{
return JSC::jsNumber(JSC::purifyNaN(value));
}
static JSC::JSValue convert(MediaTime value)
{
return JSC::jsNumber(JSC::purifyNaN(value.toDouble()));
}
};
}