GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_contextify.h Lines: 16 23 69.6 %
Date: 2022-09-25 04:23:55 Branches: 2 2 100.0 %

Line Branch Exec Source
1
#ifndef SRC_NODE_CONTEXTIFY_H_
2
#define SRC_NODE_CONTEXTIFY_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "base_object-inl.h"
7
#include "node_context_data.h"
8
#include "node_errors.h"
9
10
namespace node {
11
class ExternalReferenceRegistry;
12
13
namespace contextify {
14
15
class MicrotaskQueueWrap : public BaseObject {
16
 public:
17
  MicrotaskQueueWrap(Environment* env, v8::Local<v8::Object> obj);
18
19
  const std::shared_ptr<v8::MicrotaskQueue>& microtask_queue() const;
20
21
  static void Init(Environment* env, v8::Local<v8::Object> target);
22
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
23
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
24
25
  // This could have methods for running the microtask queue, if we ever decide
26
  // to make that fully customizable from userland.
27
28
  SET_NO_MEMORY_INFO()
29
  SET_MEMORY_INFO_NAME(MicrotaskQueueWrap)
30
  SET_SELF_SIZE(MicrotaskQueueWrap)
31
32
 private:
33
  std::shared_ptr<v8::MicrotaskQueue> microtask_queue_;
34
};
35
36
struct ContextOptions {
37
  v8::Local<v8::String> name;
38
  v8::Local<v8::String> origin;
39
  v8::Local<v8::Boolean> allow_code_gen_strings;
40
  v8::Local<v8::Boolean> allow_code_gen_wasm;
41
  BaseObjectPtr<MicrotaskQueueWrap> microtask_queue_wrap;
42
};
43
44
class ContextifyContext {
45
 public:
46
  ContextifyContext(Environment* env,
47
                    v8::Local<v8::Object> sandbox_obj,
48
                    const ContextOptions& options);
49
  ~ContextifyContext();
50
  static void CleanupHook(void* arg);
51
52
  static v8::MaybeLocal<v8::Context> CreateV8Context(
53
      v8::Isolate* isolate,
54
      v8::Local<v8::ObjectTemplate> object_template,
55
      const SnapshotData* snapshot_data,
56
      v8::MicrotaskQueue* queue);
57
  bool InitializeContext(v8::Local<v8::Context> ctx,
58
                         Environment* env,
59
                         v8::Local<v8::Object> sandbox_obj,
60
                         const ContextOptions& options);
61
  static void Init(Environment* env, v8::Local<v8::Object> target);
62
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
63
64
  static ContextifyContext* ContextFromContextifiedSandbox(
65
      Environment* env,
66
      const v8::Local<v8::Object>& sandbox);
67
68
2146753
  inline Environment* env() const {
69
2146753
    return env_;
70
  }
71
72
2142347
  inline v8::Local<v8::Context> context() const {
73
2142347
    return PersistentToLocal::Default(env()->isolate(), context_);
74
  }
75
76
42534
  inline v8::Local<v8::Object> global_proxy() const {
77
85068
    return context()->Global();
78
  }
79
80
1046953
  inline v8::Local<v8::Object> sandbox() const {
81
4187812
    return context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject)
82
1046953
        .As<v8::Object>();
83
  }
84
85
3502
  inline std::shared_ptr<v8::MicrotaskQueue> microtask_queue() const {
86
3502
    if (!microtask_queue_wrap_) return {};
87
15
    return microtask_queue_wrap_->microtask_queue();
88
  }
89
90
  template <typename T>
91
  static ContextifyContext* Get(const v8::PropertyCallbackInfo<T>& args);
92
93
  static v8::Local<v8::ObjectTemplate> CreateGlobalTemplate(
94
      v8::Isolate* isolate);
95
96
 private:
97
  static bool IsStillInitializing(const ContextifyContext* ctx);
98
  static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args);
99
  static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
100
  static void CompileFunction(
101
      const v8::FunctionCallbackInfo<v8::Value>& args);
102
  static void WeakCallback(
103
      const v8::WeakCallbackInfo<ContextifyContext>& data);
104
  static void PropertyGetterCallback(
105
      v8::Local<v8::Name> property,
106
      const v8::PropertyCallbackInfo<v8::Value>& args);
107
  static void PropertySetterCallback(
108
      v8::Local<v8::Name> property,
109
      v8::Local<v8::Value> value,
110
      const v8::PropertyCallbackInfo<v8::Value>& args);
111
  static void PropertyDescriptorCallback(
112
      v8::Local<v8::Name> property,
113
      const v8::PropertyCallbackInfo<v8::Value>& args);
114
  static void PropertyDefinerCallback(
115
      v8::Local<v8::Name> property,
116
      const v8::PropertyDescriptor& desc,
117
      const v8::PropertyCallbackInfo<v8::Value>& args);
118
  static void PropertyDeleterCallback(
119
      v8::Local<v8::Name> property,
120
      const v8::PropertyCallbackInfo<v8::Boolean>& args);
121
  static void PropertyEnumeratorCallback(
122
      const v8::PropertyCallbackInfo<v8::Array>& args);
123
  static void IndexedPropertyGetterCallback(
124
      uint32_t index,
125
      const v8::PropertyCallbackInfo<v8::Value>& args);
126
  static void IndexedPropertySetterCallback(
127
      uint32_t index,
128
      v8::Local<v8::Value> value,
129
      const v8::PropertyCallbackInfo<v8::Value>& args);
130
  static void IndexedPropertyDescriptorCallback(
131
      uint32_t index,
132
      const v8::PropertyCallbackInfo<v8::Value>& args);
133
  static void IndexedPropertyDefinerCallback(
134
      uint32_t index,
135
      const v8::PropertyDescriptor& desc,
136
      const v8::PropertyCallbackInfo<v8::Value>& args);
137
  static void IndexedPropertyDeleterCallback(
138
      uint32_t index,
139
      const v8::PropertyCallbackInfo<v8::Boolean>& args);
140
  Environment* const env_;
141
  v8::Global<v8::Context> context_;
142
  BaseObjectPtr<MicrotaskQueueWrap> microtask_queue_wrap_;
143
};
144
145
class ContextifyScript : public BaseObject {
146
 public:
147
  SET_NO_MEMORY_INFO()
148
  SET_MEMORY_INFO_NAME(ContextifyScript)
149
  SET_SELF_SIZE(ContextifyScript)
150
151
  ContextifyScript(Environment* env, v8::Local<v8::Object> object);
152
  ~ContextifyScript() override;
153
154
  static void Init(Environment* env, v8::Local<v8::Object> target);
155
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
156
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
157
  static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
158
  static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
159
  static void RunInContext(const v8::FunctionCallbackInfo<v8::Value>& args);
160
  static bool EvalMachine(v8::Local<v8::Context> context,
161
                          Environment* env,
162
                          const int64_t timeout,
163
                          const bool display_errors,
164
                          const bool break_on_sigint,
165
                          const bool break_on_first_line,
166
                          std::shared_ptr<v8::MicrotaskQueue> microtask_queue,
167
                          const v8::FunctionCallbackInfo<v8::Value>& args);
168
169
6931
  inline uint32_t id() { return id_; }
170
171
 private:
172
  v8::Global<v8::UnboundScript> script_;
173
  uint32_t id_;
174
};
175
176
class CompiledFnEntry final : public BaseObject {
177
 public:
178
70
  SET_NO_MEMORY_INFO()
179
70
  SET_MEMORY_INFO_NAME(CompiledFnEntry)
180
70
  SET_SELF_SIZE(CompiledFnEntry)
181
182
  CompiledFnEntry(Environment* env,
183
                  v8::Local<v8::Object> object,
184
                  uint32_t id,
185
                  v8::Local<v8::Function> fn);
186
  ~CompiledFnEntry();
187
188
  bool IsNotIndicativeOfMemoryLeakAtExit() const override { return true; }
189
190
 private:
191
  uint32_t id_;
192
  v8::Global<v8::Function> fn_;
193
194
  static void WeakCallback(const v8::WeakCallbackInfo<CompiledFnEntry>& data);
195
};
196
197
}  // namespace contextify
198
}  // namespace node
199
200
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
201
202
#endif  // SRC_NODE_CONTEXTIFY_H_