436 lines
14 KiB
C++
436 lines
14 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Misc/WebAssemblyUtils.h
|
|
*
|
|
* @brief WebAssembly utilities.
|
|
*/
|
|
|
|
#if (!defined ACTIV_MIDDLEWARE_MISC_WEB_ASSEMBLY_UTILS_H)
|
|
#define ACTIV_MIDDLEWARE_MISC_WEB_ASSEMBLY_UTILS_H
|
|
|
|
#include "ActivMiddleware/ActivMiddleware.h"
|
|
#include "ActivMiddleware/CxxYZ/utility.h"
|
|
#include "ActivMiddleware/Misc/ContainerTraits.h"
|
|
#include "ActivMiddleware/Misc/Optional.h"
|
|
|
|
#include <emscripten/emscripten.h>
|
|
#include <emscripten/bind.h>
|
|
#include <emscripten/val.h>
|
|
|
|
#include <memory>
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! Wasm namespace.
|
|
namespace Wasm
|
|
{
|
|
|
|
/**
|
|
* @brief Macro containing JS code to mangle a JS string to a c-string.
|
|
*
|
|
* @param __str a JS string. Should be a variable not a function call.
|
|
*
|
|
* @return An (integral) pointer to a c-string. It must be reinterpret_cast<>ed to void * and free()d! (StringPointerToString).
|
|
*/
|
|
#define ACTIV_WASM_RETURN_STRING_POINTER(__str) \
|
|
if (__str !== null) \
|
|
{ \
|
|
const len = lengthBytesUTF8(__str); \
|
|
const ptr = Module._malloc(len + 1); \
|
|
stringToUTF8(__str, ptr, len + 1); \
|
|
return ptr; \
|
|
} \
|
|
else \
|
|
{ \
|
|
return 0; \
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Convert an integral c-string "pointer" to a String.
|
|
*
|
|
* @param pointer pointer value from an EM_ASM_INT() call (etc.). This pointer is free()d.
|
|
*
|
|
* @return Boost::optional<String>; Boost::none if pointer == 0.
|
|
*/
|
|
inline Boost::optional<String> StringPointerToString(const int pointer)
|
|
{
|
|
if (0 == pointer)
|
|
return Boost::none;
|
|
|
|
String str;
|
|
|
|
// Shame we can't re-use the memory...
|
|
void * const pData = reinterpret_cast<void *>(pointer);
|
|
str.assign(reinterpret_cast<char *>(pData));
|
|
::free(pData);
|
|
|
|
return Boost::optional<String>(CxxYZ::move(str));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Get document title.
|
|
inline Boost::optional<String> GetDocumentTitle()
|
|
{
|
|
const emscripten::val document(emscripten::val::global("document"));
|
|
if (document.as<bool>())
|
|
return document["title"].as<String>();
|
|
|
|
return Boost::none;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Forward reference for recursion.
|
|
template<typename T>
|
|
emscripten::val ToVal(const T &object);
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace Detail
|
|
{
|
|
|
|
//! Cast to base helper that can be made a friend.
|
|
template<typename BASE, typename T>
|
|
BASE &GetBase(T &value)
|
|
{
|
|
return static_cast<BASE &>(value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Helper macro to allow GetBase() access to a private or protected base of a class.
|
|
#define ACTIV_WASM_ADD_GET_BASE_FRIEND(T, BASE) \
|
|
friend BASE &Activ::Wasm::Detail::GetBase<BASE, T>(T &); \
|
|
friend const BASE &Activ::Wasm::Detail::GetBase<const BASE, const T>(const T &);
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Special cases for overriding embind type mappings.
|
|
template<typename T>
|
|
class TypeToEmbindType
|
|
{
|
|
public:
|
|
using type = typename Boost::call_traits<T>::param_type;
|
|
};
|
|
|
|
// HACK support 64-bit integers as double for now...
|
|
template<>
|
|
class TypeToEmbindType<uint64_t>
|
|
{
|
|
public:
|
|
using type = double;
|
|
};
|
|
|
|
template<>
|
|
class TypeToEmbindType<const uint64_t>
|
|
{
|
|
public:
|
|
using type = const double;
|
|
};
|
|
|
|
template<>
|
|
class TypeToEmbindType<int64_t>
|
|
{
|
|
public:
|
|
using type = double;
|
|
};
|
|
|
|
template<>
|
|
class TypeToEmbindType<const int64_t>
|
|
{
|
|
public:
|
|
using type = const double;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T, typename BASE, typename V, V BASE::*POINTER>
|
|
class AccessorImpl
|
|
{
|
|
public:
|
|
static auto Get(const T &object)
|
|
{
|
|
return static_cast<typename TypeToEmbindType<V>::type>(GetBase<const BASE>(object).*POINTER);
|
|
}
|
|
|
|
static void Set(T &object, typename TypeToEmbindType<V>::type value)
|
|
{
|
|
GetBase<BASE>(object).*POINTER = static_cast<V>(value);
|
|
}
|
|
|
|
static void NullSet(T &object, typename TypeToEmbindType<V>::type value)
|
|
{
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Tags for each category of supported type.
|
|
class MapTag { };
|
|
class VectorTag { };
|
|
class ValTag { };
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Default to ValTag
|
|
template<typename T, typename ENABLE = void>
|
|
class CategoryTag
|
|
{
|
|
public:
|
|
using type = ValTag;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
class CategoryTag<T, typename std::enable_if<ContainerTraits::IsMapLike<T>::value>::type>
|
|
{
|
|
public:
|
|
using type = MapTag;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
class CategoryTag<T, typename std::enable_if<ContainerTraits::IsSequence<T>::value>::type>
|
|
{
|
|
public:
|
|
using type = VectorTag;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
inline emscripten::val ToVal(const T &object, ValTag)
|
|
{
|
|
return emscripten::val(object);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Convert a map to emscripten::object. embind's support for maps is a bit lacking.
|
|
*
|
|
* @param map
|
|
*
|
|
* @return emscripten::val
|
|
*/
|
|
template<typename T>
|
|
inline emscripten::val ToVal(const T &map, MapTag)
|
|
{
|
|
emscripten::val object(emscripten::val::object());
|
|
|
|
for (const auto &entry : map)
|
|
{
|
|
object.set(Boost::lexical_cast<String>(entry.first), Wasm::ToVal(entry.second));
|
|
}
|
|
|
|
return object;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Convert a vector to emscripten::array. embind's support for vectors is a bit lacking.
|
|
*
|
|
* @param vector
|
|
*
|
|
* @return emscripten::val
|
|
*/
|
|
template<typename T>
|
|
inline emscripten::val ToVal(const T &vector, VectorTag)
|
|
{
|
|
emscripten::val array(emscripten::val::array());
|
|
size_t index = 0;
|
|
|
|
for (const auto &entry : vector)
|
|
{
|
|
array.set(index, Wasm::ToVal(entry));
|
|
++index;
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
} // namespace Detail
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Function to pass to embind for a no-op set on a property/field.
|
|
template<typename T, typename V>
|
|
void NullSetValue(T &object, const V &value)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Helper to access an object member.
|
|
*
|
|
* @tparam T being wrapped.
|
|
* @tparam BASE type containing the member, must be (a base of) T.
|
|
* @tparam V type of the member.
|
|
* @tparam POINTER member pointer.
|
|
*/
|
|
template<typename T, typename BASE, typename V, V BASE::*POINTER>
|
|
class Accessor : public Detail::AccessorImpl<T, BASE, V, POINTER>
|
|
{
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Helper to access TypeSafeFlags value.
|
|
*
|
|
* @tparam T being wrapped.
|
|
* @tparam BASE type containing a TypeSafeFlags member, must be (a base of) T.
|
|
* @tparam V TypeSafeFlags type.
|
|
* @tparam POINTER member pointer.
|
|
* @tparam FLAG individual flag within @a V.
|
|
*/
|
|
template<typename T, typename BASE, typename V, V BASE::*POINTER, typename FLAG>
|
|
class TypeSafeFlagsAccessor
|
|
{
|
|
public:
|
|
static bool Get(const T &object)
|
|
{
|
|
return (Detail::GetBase<const BASE>(object).*POINTER).template IsSet<FLAG>();
|
|
}
|
|
|
|
static void Set(T &object, bool value)
|
|
{
|
|
if (value)
|
|
(Detail::GetBase<BASE>(object).*POINTER) |= FLAG();
|
|
else
|
|
(Detail::GetBase<BASE>(object).*POINTER) &= ~FLAG();
|
|
}
|
|
|
|
static void NullSet(T &object, bool value)
|
|
{
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Read-write base field accessor.
|
|
*
|
|
* @tparam type derived type.
|
|
* @tparam base base of @a type that contains @a field.
|
|
* @tparam field member to access.
|
|
*/
|
|
#define ACTIV_WASM_BASE_ACCESSOR(type, base, field) \
|
|
&Activ::Wasm::Accessor<type, base, decltype(base::field), &base::field>::Get, \
|
|
&Activ::Wasm::Accessor<type, base, decltype(base::field), &base::field>::Set
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Read-only base field accessor.
|
|
*
|
|
* @tparam type derived type.
|
|
* @tparam base base of @a type that contains @a field.
|
|
* @tparam field member to access.
|
|
*/
|
|
#define ACTIV_WASM_BASE_ACCESSOR_RO(type, base, field) \
|
|
&Activ::Wasm::Accessor<type, base, decltype(base::field), &base::field>::Get
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Read-write field accessor.
|
|
*
|
|
* @tparam type derived type.
|
|
* @tparam field member to access.
|
|
*/
|
|
#define ACTIV_WASM_ACCESSOR(type, field) ACTIV_WASM_BASE_ACCESSOR(type, type, field)
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Read-only field accessor.
|
|
*
|
|
* @tparam type derived type.
|
|
* @tparam field member to access.
|
|
*/
|
|
#define ACTIV_WASM_ACCESSOR_RO(type, field) ACTIV_WASM_BASE_ACCESSOR_RO(type, type, field)
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Read-write base TypeSafeFlags accessor.
|
|
*
|
|
* @tparam type derived type.
|
|
* @tparam base base of @a type that contains @a field.
|
|
* @tparam field flags member to access.
|
|
* @tparam flag flag name of @a field to check.
|
|
*/
|
|
#define ACTIV_WASM_BASE_FLAG_ACCESSOR(type, base, field, flag) \
|
|
&Activ::Wasm::TypeSafeFlagsAccessor<type, base, decltype(base::field), &base::field, \
|
|
decltype(base::field)::ParentType::flag>::Get, \
|
|
&Activ::Wasm::TypeSafeFlagsAccessor<type, base, decltype(base::field), &base::field, \
|
|
decltype(base::field)::ParentType::flag>::Set
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Read-only base TypeSafeFlags accessor.
|
|
*
|
|
* @tparam type derived type.
|
|
* @tparam base base of @a type that contains @a field.
|
|
* @tparam field flags member to access.
|
|
* @tparam flag flag name of @a field to check.
|
|
*/
|
|
#define ACTIV_WASM_BASE_FLAG_ACCESSOR_RO(type, base, field, flag) \
|
|
&Activ::Wasm::TypeSafeFlagsAccessor<type, base, decltype(base::field), &base::field, \
|
|
decltype(base::field)::ParentType::flag>::Get
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Read-write TypeSafeFlags accessor.
|
|
*
|
|
* @tparam type derived type.
|
|
* @tparam field flags member to access.
|
|
* @tparam flag flag name of @a field to check.
|
|
*/
|
|
#define ACTIV_WASM_FLAG_ACCESSOR(type, field, flag) ACTIV_WASM_BASE_FLAG_ACCESSOR(type, type, field, flag)
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Read-only TypeSafeFlags accessor.
|
|
*
|
|
* @tparam type derived type.
|
|
* @tparam field flags member to access.
|
|
* @tparam flag flag name of @a field to check.
|
|
*/
|
|
#define ACTIV_WASM_FLAG_ACCESSOR_RO(type, field, flag) ACTIV_WASM_BASE_FLAG_ACCESSOR_RO(type, type, field, flag)
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Convert an object to an emscripten::val. Copes with sequences.
|
|
template<typename T>
|
|
inline emscripten::val ToVal(const T &object)
|
|
{
|
|
return Detail::ToVal(object, typename Detail::CategoryTag<T>::type());
|
|
}
|
|
|
|
} // namespace Wasm
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_MIDDLEWARE_MISC_WEB_ASSEMBLY_UTILS_H)
|