#ifndef SUPPORT_H_ #define SUPPORT_H_ #include #include #include #include #include #include "v8/include/v8.h" // Check assumptions made in binding code. // TODO(ry) re-enable the following // static_assert(sizeof(bool) == sizeof(uint8_t)); // static_assert(sizeof(std::unique_ptr) == sizeof(void*)); namespace support { template using uninit_t = typename std::aligned_storage::type; template class construct_in_place_helper { public: construct_in_place_helper(uninit_t& buf, Args... args) : inner_(std::forward(args)...) {} private: T inner_; }; template void construct_in_place(uninit_t& buf, Args... args) { new (&buf) construct_in_place_helper(buf, std::forward(args)...); } } // namespace support template inline static T* local_to_ptr(v8::Local local) { return *local; } template inline static v8::Local ptr_to_local(T* ptr) { static_assert(sizeof(v8::Local) == sizeof(T*), ""); auto local = *reinterpret_cast*>(&ptr); assert(*local == ptr); return local; } template inline static T* maybe_local_to_ptr(v8::MaybeLocal local) { return *local.FromMaybe(v8::Local()); } template inline static v8::MaybeLocal ptr_to_maybe_local(T* ptr) { static_assert(sizeof(v8::MaybeLocal) == sizeof(T*), ""); return *reinterpret_cast*>(&ptr); } #endif // SUPPORT_H_