• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_ALIASED_STRUCT_INL_H_
2 #define SRC_ALIASED_STRUCT_INL_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "aliased_struct.h"
7 #include "v8.h"
8 #include <memory>
9 
10 namespace node {
11 
12 template <typename T>
13 template <typename... Args>
AliasedStruct(v8::Isolate * isolate,Args &&...args)14 AliasedStruct<T>::AliasedStruct(v8::Isolate* isolate, Args&&... args)
15     : isolate_(isolate) {
16   const v8::HandleScope handle_scope(isolate);
17 
18   store_ = v8::ArrayBuffer::NewBackingStore(isolate, sizeof(T));
19   ptr_ = new (store_->Data()) T(std::forward<Args>(args)...);
20   DCHECK_NOT_NULL(ptr_);
21 
22   v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(isolate, store_);
23   buffer_ = v8::Global<v8::ArrayBuffer>(isolate, buffer);
24 }
25 
26 template <typename T>
AliasedStruct(const AliasedStruct & that)27 AliasedStruct<T>::AliasedStruct(const AliasedStruct& that)
28     : AliasedStruct(that.isolate_, *that) {}
29 
30 template <typename T>
31 AliasedStruct<T>& AliasedStruct<T>::operator=(
32     AliasedStruct<T>&& that) noexcept {
33   this->~AliasedStruct();
34   isolate_ = that.isolate_;
35   store_ = that.store_;
36   ptr_ = that.ptr_;
37 
38   buffer_ = std::move(that.buffer_);
39 
40   that.ptr_ = nullptr;
41   that.store_.reset();
42   return *this;
43 }
44 
45 template <typename T>
~AliasedStruct()46 AliasedStruct<T>::~AliasedStruct() {
47   if (ptr_ != nullptr) ptr_->~T();
48 }
49 
50 }  // namespace node
51 
52 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
53 
54 #endif  // SRC_ALIASED_STRUCT_INL_H_
55