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> |
14 |
|
1108 |
AliasedStruct<T>::AliasedStruct(v8::Isolate* isolate, Args&&... args) |
15 |
|
1108 |
: isolate_(isolate) { |
16 |
|
1108 |
const v8::HandleScope handle_scope(isolate); |
17 |
|
|
|
18 |
|
1108 |
store_ = v8::ArrayBuffer::NewBackingStore(isolate, sizeof(T)); |
19 |
|
1108 |
ptr_ = new (store_->Data()) T(std::forward<Args>(args)...); |
20 |
|
|
DCHECK_NOT_NULL(ptr_); |
21 |
|
|
|
22 |
|
1108 |
v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(isolate, store_); |
23 |
✓✗ |
2216 |
buffer_ = v8::Global<v8::ArrayBuffer>(isolate, buffer); |
24 |
|
1108 |
} |
25 |
|
|
|
26 |
|
|
template <typename T> |
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> |
46 |
|
1108 |
AliasedStruct<T>::~AliasedStruct() { |
47 |
|
1108 |
if (ptr_ != nullptr) ptr_->~T(); |
48 |
|
2216 |
} |
49 |
|
|
|
50 |
|
|
} // namespace node |
51 |
|
|
|
52 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
53 |
|
|
|
54 |
|
|
#endif // SRC_ALIASED_STRUCT_INL_H_ |