/** * @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 #include #include #include //! 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; Boost::none if pointer == 0. */ inline Boost::optional 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(pointer); str.assign(reinterpret_cast(pData)); ::free(pData); return Boost::optional(CxxYZ::move(str)); } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- //! Get document title. inline Boost::optional GetDocumentTitle() { const emscripten::val document(emscripten::val::global("document")); if (document.as()) return document["title"].as(); return Boost::none; } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- // Forward reference for recursion. template emscripten::val ToVal(const T &object); // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- namespace Detail { //! Cast to base helper that can be made a friend. template BASE &GetBase(T &value) { return static_cast(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(T &); \ friend const BASE &Activ::Wasm::Detail::GetBase(const T &); // --------------------------------------------------------------------------------------------------------------------------------- // Special cases for overriding embind type mappings. template class TypeToEmbindType { public: using type = typename Boost::call_traits::param_type; }; // HACK support 64-bit integers as double for now... template<> class TypeToEmbindType { public: using type = double; }; template<> class TypeToEmbindType { public: using type = const double; }; template<> class TypeToEmbindType { public: using type = double; }; template<> class TypeToEmbindType { public: using type = const double; }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template class AccessorImpl { public: static auto Get(const T &object) { return static_cast::type>(GetBase(object).*POINTER); } static void Set(T &object, typename TypeToEmbindType::type value) { GetBase(object).*POINTER = static_cast(value); } static void NullSet(T &object, typename TypeToEmbindType::type value) { } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- // Tags for each category of supported type. class MapTag { }; class VectorTag { }; class ValTag { }; // --------------------------------------------------------------------------------------------------------------------------------- // Default to ValTag template class CategoryTag { public: using type = ValTag; }; // --------------------------------------------------------------------------------------------------------------------------------- template class CategoryTag::value>::type> { public: using type = MapTag; }; // --------------------------------------------------------------------------------------------------------------------------------- template class CategoryTag::value>::type> { public: using type = VectorTag; }; // --------------------------------------------------------------------------------------------------------------------------------- template 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 inline emscripten::val ToVal(const T &map, MapTag) { emscripten::val object(emscripten::val::object()); for (const auto &entry : map) { object.set(Boost::lexical_cast(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 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 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 class Accessor : public Detail::AccessorImpl { }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @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 class TypeSafeFlagsAccessor { public: static bool Get(const T &object) { return (Detail::GetBase(object).*POINTER).template IsSet(); } static void Set(T &object, bool value) { if (value) (Detail::GetBase(object).*POINTER) |= FLAG(); else (Detail::GetBase(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::Get, \ &Activ::Wasm::Accessor::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::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::Get, \ &Activ::Wasm::TypeSafeFlagsAccessor::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::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 inline emscripten::val ToVal(const T &object) { return Detail::ToVal(object, typename Detail::CategoryTag::type()); } } // namespace Wasm } // namespace Activ #endif // (!defined ACTIV_MIDDLEWARE_MISC_WEB_ASSEMBLY_UTILS_H)