• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_ALIASED_STRUCT_H_
2 #define SRC_ALIASED_STRUCT_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "node_internals.h"
7 #include "v8.h"
8 #include <memory>
9 
10 namespace node {
11 
12 // AliasedStruct is a utility that allows uses a V8 Backing Store
13 // to be exposed to the C++/C side as a struct and to the
14 // JavaScript side as an ArrayBuffer to efficiently share
15 // data without marshalling. It is similar in nature to
16 // AliasedBuffer.
17 //
18 //   struct Foo { int x; }
19 //
20 //   AliasedStruct<Foo> foo;
21 //   foo->x = 1;
22 //
23 //   Local<ArrayBuffer> ab = foo.GetArrayBuffer();
24 template <typename T>
25 class AliasedStruct final {
26  public:
27   template <typename... Args>
28   explicit AliasedStruct(v8::Isolate* isolate, Args&&... args);
29 
30   inline AliasedStruct(const AliasedStruct& that);
31 
32   inline ~AliasedStruct();
33 
34   inline AliasedStruct& operator=(AliasedStruct&& that) noexcept;
35 
GetArrayBuffer()36   v8::Local<v8::ArrayBuffer> GetArrayBuffer() const {
37     return buffer_.Get(isolate_);
38   }
39 
Data()40   const T* Data() const { return ptr_; }
41 
Data()42   T* Data() { return ptr_; }
43 
44   const T& operator*() const { return *ptr_; }
45 
46   T& operator*()  { return *ptr_; }
47 
48   const T* operator->() const { return ptr_; }
49 
50   T* operator->() { return ptr_; }
51 
52  private:
53   v8::Isolate* isolate_;
54   std::shared_ptr<v8::BackingStore> store_;
55   T* ptr_;
56   v8::Global<v8::ArrayBuffer> buffer_;
57 };
58 
59 }  // namespace node
60 
61 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
62 
63 #endif  // SRC_ALIASED_STRUCT_H_
64