0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-23 15:50:11 -05:00

Use make_pod to convert Global<T> to T* (#297)

This commit is contained in:
Bert Belder 2020-02-28 15:48:23 -08:00
parent 1f0d6e8f68
commit 5e99489c3b
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
2 changed files with 8 additions and 14 deletions

View file

@ -258,21 +258,23 @@ v8::Value* v8__Local__New(v8::Isolate* isolate, v8::Value* other) {
}
v8::Value* v8__Global__New(v8::Isolate* isolate, v8::Value* other) {
// We have to use `std::move()` here because v8 disables the copy constructor
// for class `v8::Global`.
auto global = v8::Global<v8::Value>(isolate, ptr_to_local(other));
return global_to_ptr(global);
return make_pod<v8::Value*>(std::move(global));
}
void v8__Global__Reset__0(v8::Value*& self) {
auto global = ptr_to_global(self);
global.Reset();
self = global_to_ptr(global);
self = make_pod<v8::Value*>(std::move(global));
}
void v8__Global__Reset__2(v8::Value*& self, v8::Isolate* isolate,
v8::Value* const& other) {
auto global = ptr_to_global(self);
global.Reset(isolate, ptr_to_local(other));
self = global_to_ptr(global);
self = make_pod<v8::Value*>(std::move(global));
}
void v8__ScriptCompiler__Source__CONSTRUCT(

View file

@ -38,7 +38,7 @@ void construct_in_place(uninit_t<T>& buf, Args... args) {
template <class P>
struct make_pod {
template <class V>
inline make_pod(V&& value) : pod_(helper<V>(value)) {}
inline make_pod(V&& value) : pod_(helper<V>(std::move(value))) {}
template <class V>
inline make_pod(const V& value) : pod_(helper<V>(value)) {}
inline operator P() { return pod_; }
@ -54,7 +54,7 @@ struct make_pod {
static_assert(alignof(V) <= alignof(P),
"alignment of type P must be compatible with that of type V");
inline helper(V&& value) : value_(value), padding_() {}
inline helper(V&& value) : value_(std::move(value)), padding_() {}
inline helper(const V& value) : value_(value), padding_() {}
inline ~helper() {}
@ -110,14 +110,6 @@ inline static v8::MaybeLocal<T> ptr_to_maybe_local(T* ptr) {
return *reinterpret_cast<v8::MaybeLocal<T>*>(&ptr);
}
template <class T>
inline static T* global_to_ptr(v8::Global<T>& global) {
static_assert(sizeof(v8::Global<T>) == sizeof(T*), "");
T* ptr = nullptr;
std::swap(ptr, reinterpret_cast<T*&>(global));
return ptr;
}
template <class T>
inline static v8::Global<T> ptr_to_global(T* ptr) {
v8::Global<T> global;
@ -125,7 +117,7 @@ inline static v8::Global<T> ptr_to_global(T* ptr) {
return global;
}
// Because, for some reason, Clang complains that `std::aray<void*, 2` is an
// Because, for some reason, Clang complains that `std::array<void*, 2>` is an
// incomplete type, incompatible with C linkage.
struct two_pointers_t {
void* a;