GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_blob.h Lines: 10 21 47.6 %
Date: 2022-08-29 04:21:03 Branches: 0 0 - %

Line Branch Exec Source
1
#ifndef SRC_NODE_BLOB_H_
2
#define SRC_NODE_BLOB_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "async_wrap.h"
7
#include "base_object.h"
8
#include "env.h"
9
#include "memory_tracker.h"
10
#include "node_internals.h"
11
#include "node_snapshotable.h"
12
#include "node_worker.h"
13
#include "v8.h"
14
15
#include <string>
16
#include <unordered_map>
17
#include <vector>
18
19
namespace node {
20
21
struct BlobEntry {
22
  std::shared_ptr<v8::BackingStore> store;
23
  size_t length;
24
  size_t offset;
25
};
26
27
class Blob : public BaseObject {
28
 public:
29
  static void RegisterExternalReferences(
30
      ExternalReferenceRegistry* registry);
31
32
  static void Initialize(
33
      v8::Local<v8::Object> target,
34
      v8::Local<v8::Value> unused,
35
      v8::Local<v8::Context> context,
36
      void* priv);
37
38
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
39
  static void ToArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
40
  static void ToSlice(const v8::FunctionCallbackInfo<v8::Value>& args);
41
  static void StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
42
  static void GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
43
  static void RevokeDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
44
45
  static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
46
      Environment* env);
47
48
  static BaseObjectPtr<Blob> Create(
49
      Environment* env,
50
      const std::vector<BlobEntry> store,
51
      size_t length);
52
53
  static bool HasInstance(Environment* env, v8::Local<v8::Value> object);
54
55
112
  const std::vector<BlobEntry> entries() const {
56
112
    return store_;
57
  }
58
59
  void MemoryInfo(MemoryTracker* tracker) const override;
60
  SET_MEMORY_INFO_NAME(Blob)
61
  SET_SELF_SIZE(Blob)
62
63
  // Copies the contents of the Blob into an ArrayBuffer.
64
  v8::MaybeLocal<v8::Value> GetArrayBuffer(Environment* env);
65
66
  BaseObjectPtr<Blob> Slice(Environment* env, size_t start, size_t end);
67
68
130
  inline size_t length() const { return length_; }
69
70
  class BlobTransferData : public worker::TransferData {
71
   public:
72
1
    explicit BlobTransferData(
73
        const std::vector<BlobEntry>& store,
74
      size_t length)
75
1
        : store_(store),
76
1
          length_(length) {}
77
78
    BaseObjectPtr<BaseObject> Deserialize(
79
        Environment* env,
80
        v8::Local<v8::Context> context,
81
        std::unique_ptr<worker::TransferData> self) override;
82
83
    SET_MEMORY_INFO_NAME(BlobTransferData)
84
    SET_SELF_SIZE(BlobTransferData)
85
    SET_NO_MEMORY_INFO()
86
87
   private:
88
    std::vector<BlobEntry> store_;
89
    size_t length_ = 0;
90
  };
91
92
  BaseObject::TransferMode GetTransferMode() const override;
93
  std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
94
95
  Blob(
96
      Environment* env,
97
      v8::Local<v8::Object> obj,
98
      const std::vector<BlobEntry>& store,
99
      size_t length);
100
101
 private:
102
  std::vector<BlobEntry> store_;
103
  size_t length_ = 0;
104
};
105
106
class FixedSizeBlobCopyJob : public AsyncWrap, public ThreadPoolWork {
107
 public:
108
  enum class Mode {
109
    SYNC,
110
    ASYNC
111
  };
112
113
  static void RegisterExternalReferences(
114
      ExternalReferenceRegistry* registry);
115
  static void Initialize(Environment* env, v8::Local<v8::Object> target);
116
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
117
  static void Run(const v8::FunctionCallbackInfo<v8::Value>& args);
118
119
  bool IsNotIndicativeOfMemoryLeakAtExit() const override {
120
    return true;
121
  }
122
123
  void DoThreadPoolWork() override;
124
  void AfterThreadPoolWork(int status) override;
125
126
50
  Mode mode() const { return mode_; }
127
128
  void MemoryInfo(MemoryTracker* tracker) const override;
129
  SET_MEMORY_INFO_NAME(FixedSizeBlobCopyJob)
130
  SET_SELF_SIZE(FixedSizeBlobCopyJob)
131
132
 private:
133
  FixedSizeBlobCopyJob(
134
    Environment* env,
135
    v8::Local<v8::Object> object,
136
    Blob* blob,
137
    Mode mode = Mode::ASYNC);
138
139
  Mode mode_;
140
  std::vector<BlobEntry> source_;
141
  std::shared_ptr<v8::BackingStore> destination_;
142
  size_t length_ = 0;
143
};
144
145
class BlobBindingData : public SnapshotableObject {
146
 public:
147
  explicit BlobBindingData(Environment* env, v8::Local<v8::Object> wrap);
148
149
  using InternalFieldInfo = InternalFieldInfoBase;
150
151
  SERIALIZABLE_OBJECT_METHODS()
152
153
  static constexpr FastStringKey type_name{"node::BlobBindingData"};
154
  static constexpr EmbedderObjectType type_int =
155
      EmbedderObjectType::k_blob_binding_data;
156
157
  void MemoryInfo(MemoryTracker* tracker) const override;
158
24
  SET_SELF_SIZE(BlobBindingData)
159
24
  SET_MEMORY_INFO_NAME(BlobBindingData)
160
161
  struct StoredDataObject : public MemoryRetainer {
162
    BaseObjectPtr<Blob> blob;
163
    size_t length;
164
    std::string type;
165
166
3
    StoredDataObject() = default;
167
168
    StoredDataObject(
169
        const BaseObjectPtr<Blob>& blob_,
170
        size_t length_,
171
        const std::string& type_);
172
173
    void MemoryInfo(MemoryTracker* tracker) const override;
174
    SET_SELF_SIZE(StoredDataObject)
175
    SET_MEMORY_INFO_NAME(StoredDataObject)
176
  };
177
178
  void store_data_object(
179
      const std::string& uuid,
180
      const StoredDataObject& object);
181
182
  void revoke_data_object(const std::string& uuid);
183
184
  StoredDataObject get_data_object(const std::string& uuid);
185
186
 private:
187
  std::unordered_map<std::string, StoredDataObject> data_objects_;
188
};
189
190
}  // namespace node
191
192
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
193
#endif  // SRC_NODE_BLOB_H_