2019-10-20 19:48:12 -04:00
|
|
|
#ifndef SUPPORT_H_
|
|
|
|
#define SUPPORT_H_
|
|
|
|
|
2019-12-21 12:17:03 -05:00
|
|
|
#include <algorithm>
|
2019-12-25 21:32:22 -05:00
|
|
|
#include <array>
|
2019-10-20 19:48:12 -04:00
|
|
|
#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-12-25 21:32:22 -05:00
|
|
|
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
|
|
|
}
|
2019-12-18 09:05:33 -05:00
|
|
|
|
2019-12-25 21:32:22 -05:00
|
|
|
template <class P>
|
|
|
|
struct make_pod {
|
|
|
|
template <class V>
|
2020-02-28 18:48:23 -05:00
|
|
|
inline make_pod(V&& value) : pod_(helper<V>(std::move(value))) {}
|
2019-12-25 21:32:22 -05:00
|
|
|
template <class V>
|
|
|
|
inline make_pod(const V& value) : pod_(helper<V>(value)) {}
|
|
|
|
inline operator P() { return pod_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
P pod_;
|
|
|
|
|
|
|
|
template <class V>
|
|
|
|
union helper {
|
|
|
|
static_assert(std::is_pod<P>::value, "type P must a pod type");
|
|
|
|
static_assert(sizeof(V) <= sizeof(P),
|
|
|
|
"type P must be at least as big as type V");
|
|
|
|
static_assert(alignof(V) <= alignof(P),
|
|
|
|
"alignment of type P must be compatible with that of type V");
|
|
|
|
|
2020-02-28 18:48:23 -05:00
|
|
|
inline helper(V&& value) : value_(std::move(value)), padding_() {}
|
2019-12-25 21:32:22 -05:00
|
|
|
inline helper(const V& value) : value_(value), padding_() {}
|
|
|
|
inline ~helper() {}
|
|
|
|
|
|
|
|
inline operator P() {
|
|
|
|
// Do a memcpy here avoid undefined behavior.
|
|
|
|
P result;
|
|
|
|
memcpy(&result, this, sizeof result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct {
|
|
|
|
V value_;
|
|
|
|
char padding_[sizeof(P) - sizeof(V)];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-12-18 09:05:33 -05:00
|
|
|
// The C-ABI compatible equivalent of V8's Maybe<bool>.
|
|
|
|
enum class MaybeBool { JustFalse = 0, JustTrue = 1, Nothing = 2 };
|
|
|
|
|
|
|
|
inline static MaybeBool maybe_to_maybe_bool(v8::Maybe<bool> maybe) {
|
|
|
|
if (maybe.IsNothing()) {
|
|
|
|
return MaybeBool::Nothing;
|
|
|
|
} else if (maybe.FromJust()) {
|
|
|
|
return MaybeBool::JustTrue;
|
|
|
|
} else {
|
|
|
|
return MaybeBool::JustFalse;
|
|
|
|
}
|
|
|
|
}
|
2019-10-20 19:48:12 -04:00
|
|
|
|
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
|
|
|
}
|
2019-12-21 12:17:03 -05:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline static v8::Global<T> ptr_to_global(T* ptr) {
|
|
|
|
v8::Global<T> global;
|
|
|
|
std::swap(ptr, *reinterpret_cast<T**>(&global));
|
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
2020-02-28 18:48:23 -05:00
|
|
|
// Because, for some reason, Clang complains that `std::array<void*, 2>` is an
|
2019-12-25 21:32:22 -05:00
|
|
|
// incomplete type, incompatible with C linkage.
|
|
|
|
struct two_pointers_t {
|
|
|
|
void* a;
|
|
|
|
void* b;
|
|
|
|
};
|
|
|
|
|
2019-12-18 12:13:59 -05:00
|
|
|
} // namespace support
|
2019-12-04 02:03:17 -05:00
|
|
|
|
|
|
|
#endif // SUPPORT_H_
|