11cb0ef41Sopenharmony_ci#ifndef SRC_ALIASED_STRUCT_INL_H_
21cb0ef41Sopenharmony_ci#define SRC_ALIASED_STRUCT_INL_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include "aliased_struct.h"
71cb0ef41Sopenharmony_ci#include "v8.h"
81cb0ef41Sopenharmony_ci#include <memory>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace node {
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_citemplate <typename T>
131cb0ef41Sopenharmony_citemplate <typename... Args>
141cb0ef41Sopenharmony_ciAliasedStruct<T>::AliasedStruct(v8::Isolate* isolate, Args&&... args)
151cb0ef41Sopenharmony_ci    : isolate_(isolate) {
161cb0ef41Sopenharmony_ci  const v8::HandleScope handle_scope(isolate);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  store_ = v8::ArrayBuffer::NewBackingStore(isolate, sizeof(T));
191cb0ef41Sopenharmony_ci  ptr_ = new (store_->Data()) T(std::forward<Args>(args)...);
201cb0ef41Sopenharmony_ci  DCHECK_NOT_NULL(ptr_);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(isolate, store_);
231cb0ef41Sopenharmony_ci  buffer_ = v8::Global<v8::ArrayBuffer>(isolate, buffer);
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_citemplate <typename T>
271cb0ef41Sopenharmony_ciAliasedStruct<T>::AliasedStruct(const AliasedStruct& that)
281cb0ef41Sopenharmony_ci    : AliasedStruct(that.isolate_, *that) {}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_citemplate <typename T>
311cb0ef41Sopenharmony_ciAliasedStruct<T>& AliasedStruct<T>::operator=(
321cb0ef41Sopenharmony_ci    AliasedStruct<T>&& that) noexcept {
331cb0ef41Sopenharmony_ci  this->~AliasedStruct();
341cb0ef41Sopenharmony_ci  isolate_ = that.isolate_;
351cb0ef41Sopenharmony_ci  store_ = that.store_;
361cb0ef41Sopenharmony_ci  ptr_ = that.ptr_;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  buffer_ = std::move(that.buffer_);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  that.ptr_ = nullptr;
411cb0ef41Sopenharmony_ci  that.store_.reset();
421cb0ef41Sopenharmony_ci  return *this;
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_citemplate <typename T>
461cb0ef41Sopenharmony_ciAliasedStruct<T>::~AliasedStruct() {
471cb0ef41Sopenharmony_ci  if (ptr_ != nullptr) ptr_->~T();
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci}  // namespace node
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci#endif  // SRC_ALIASED_STRUCT_INL_H_
55