0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-27 01:29:19 -05:00
denoland-rusty-v8/src/support.h

64 lines
1.5 KiB
C
Raw Normal View History

2019-10-20 19:48:12 -04:00
#ifndef SUPPORT_H_
#define SUPPORT_H_
#include <cassert>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
2019-12-04 02:03:17 -05:00
#include "v8/include/v8.h"
2019-10-20 19:48:12 -04:00
// Check assumptions made in binding code.
2019-10-31 12:03:44 -04:00
// TODO(ry) re-enable the following
// static_assert(sizeof(bool) == sizeof(uint8_t));
// static_assert(sizeof(std::unique_ptr<void>) == sizeof(void*));
2019-10-20 19:48:12 -04:00
namespace support {
template <class T>
using uninit_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
2019-11-27 10:14:39 -05:00
template <class T, class... Args>
class construct_in_place_helper {
public:
construct_in_place_helper(uninit_t<T>& buf, Args... args)
: inner_(std::forward<Args>(args)...) {}
private:
T inner_;
};
2019-10-20 19:48:12 -04:00
template <class T, class... Args>
void construct_in_place(uninit_t<T>& buf, Args... args) {
2019-11-27 10:14:39 -05:00
new (&buf)
construct_in_place_helper<T, Args...>(buf, std::forward<Args>(args)...);
2019-10-20 19:48:12 -04:00
}
} // namespace support
2019-12-04 08:12:27 -05:00
template <class T>
inline static T* local_to_ptr(v8::Local<T> local) {
return *local;
}
template <class T>
inline static v8::Local<T> ptr_to_local(T* ptr) {
static_assert(sizeof(v8::Local<T>) == sizeof(T*), "");
auto local = *reinterpret_cast<v8::Local<T>*>(&ptr);
assert(*local == ptr);
return local;
}
template <class T>
inline static T* maybe_local_to_ptr(v8::MaybeLocal<T> local) {
return *local.FromMaybe(v8::Local<T>());
}
2019-12-04 02:03:17 -05:00
template <class T>
2019-12-04 08:12:27 -05:00
inline static v8::MaybeLocal<T> ptr_to_maybe_local(T* ptr) {
static_assert(sizeof(v8::MaybeLocal<T>) == sizeof(T*), "");
return *reinterpret_cast<v8::MaybeLocal<T>*>(&ptr);
2019-12-04 02:03:17 -05:00
}
#endif // SUPPORT_H_