- // This helper is used in destroy() to be able to run destructors on
- // types like "int64_t" without a compiler error.
- struct Destroy {
- template<class T> static void destroy(T* t) { t->~T(); }
- };
-
- /*
- * The enable_if junk here is necessary to avoid ambiguous
- * conversions relating to bool and double when you implicitly
- * convert an int or long to a dynamic.
- */
- template<class T, class Enable = void> struct ConversionHelper;
- template<class T>
- struct ConversionHelper<
- T,
- typename std::enable_if<
- std::is_integral<T>::value && !std::is_same<T,bool>::value
- >::type
- > {
- typedef int64_t type;
- };
- template<class T>
- struct ConversionHelper<
- T,
- typename std::enable_if<
- (!std::is_integral<T>::value || std::is_same<T,bool>::value) &&
- !std::is_same<T,std::nullptr_t>::value
- >::type
- > {
- typedef T type;
- };
- template<class T>
- struct ConversionHelper<
- T,
- typename std::enable_if<
- std::is_same<T,std::nullptr_t>::value
- >::type
- > {
- typedef void* type;
- };
-
- /*
- * Helper for implementing numeric conversions in operators on
- * numbers. Just promotes to double when one of the arguments is
- * double, or throws if either is not a numeric type.
- */
- template<template<class> class Op>
- dynamic numericOp(dynamic const& a, dynamic const& b) {
- if (!a.isNumber() || !b.isNumber()) {
- throw TypeError("numeric", a.type(), b.type());
- }
- if (a.type() != b.type()) {
- auto& integ = a.isInt() ? a : b;
- auto& nonint = a.isInt() ? b : a;
- return Op<double>()(to<double>(integ.asInt()), nonint.asDouble());
- }
- if (a.isDouble()) {
- return Op<double>()(a.asDouble(), b.asDouble());
- }
- return Op<int64_t>()(a.asInt(), b.asInt());