GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_contextify.h Lines: 16 23 69.6 %
Date: 2022-08-14 04:19:53 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
  enum InternalFields { kSlot, kInternalFieldCount };
47
  ContextifyContext(Environment* env,
48
                    v8::Local<v8::Object> sandbox_obj,
49
                    const ContextOptions& options);
50
  ~ContextifyContext();
51
  static void CleanupHook(void* arg);
52
53
  v8::MaybeLocal<v8::Object> CreateDataWrapper(Environment* env);
54
  v8::MaybeLocal<v8::Context> CreateV8Context(Environment* env,
55
                                              v8::Local<v8::Object> sandbox_obj,
56
                                              const ContextOptions& options);
57
  static void Init(Environment* env, v8::Local<v8::Object> target);
58
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
59
60
  static ContextifyContext* ContextFromContextifiedSandbox(
61
      Environment* env,
62
      const v8::Local<v8::Object>& sandbox);
63
64
2145876
  inline Environment* env() const {
65
2145876
    return env_;
66
  }
67
68
2141501
  inline v8::Local<v8::Context> context() const {
69
2141501
    return PersistentToLocal::Default(env()->isolate(), context_);
70
  }
71
72
42295
  inline v8::Local<v8::Object> global_proxy() const {
73
84590
    return context()->Global();
74
  }
75
76
1046661
  inline v8::Local<v8::Object> sandbox() const {
77
4186644
    return context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject)
78
1046661
        .As<v8::Object>();
79
  }
80
81
3486
  inline std::shared_ptr<v8::MicrotaskQueue> microtask_queue() const {
82
3486
    if (!microtask_queue_wrap_) return {};
83
15
    return microtask_queue_wrap_->microtask_queue();
84
  }
85
86
87
  template <typename T>
88
  static ContextifyContext* Get(const v8::PropertyCallbackInfo<T>& args);
89
90
 private:
91
  static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args);
92
  static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
93
  static void CompileFunction(
94
      const v8::FunctionCallbackInfo<v8::Value>& args);
95
  static void WeakCallback(
96
      const v8::WeakCallbackInfo<ContextifyContext>& data);
97
  static void PropertyGetterCallback(
98
      v8::Local<v8::Name> property,
99
      const v8::PropertyCallbackInfo<v8::Value>& args);
100
  static void PropertySetterCallback(
101
      v8::Local<v8::Name> property,
102
      v8::Local<v8::Value> value,
103
      const v8::PropertyCallbackInfo<v8::Value>& args);
104
  static void PropertyDescriptorCallback(
105
      v8::Local<v8::Name> property,
106
      const v8::PropertyCallbackInfo<v8::Value>& args);
107
  static void PropertyDefinerCallback(
108
      v8::Local<v8::Name> property,
109
      const v8::PropertyDescriptor& desc,
110
      const v8::PropertyCallbackInfo<v8::Value>& args);
111
  static void PropertyDeleterCallback(
112
      v8::Local<v8::Name> property,
113
      const v8::PropertyCallbackInfo<v8::Boolean>& args);
114
  static void PropertyEnumeratorCallback(
115
      const v8::PropertyCallbackInfo<v8::Array>& args);
116
  static void IndexedPropertyGetterCallback(
117
      uint32_t index,
118
      const v8::PropertyCallbackInfo<v8::Value>& args);
119
  static void IndexedPropertySetterCallback(
120
      uint32_t index,
121
      v8::Local<v8::Value> value,
122
      const v8::PropertyCallbackInfo<v8::Value>& args);
123
  static void IndexedPropertyDescriptorCallback(
124
      uint32_t index,
125
      const v8::PropertyCallbackInfo<v8::Value>& args);
126
  static void IndexedPropertyDefinerCallback(
127
      uint32_t index,
128
      const v8::PropertyDescriptor& desc,
129
      const v8::PropertyCallbackInfo<v8::Value>& args);
130
  static void IndexedPropertyDeleterCallback(
131
      uint32_t index,
132
      const v8::PropertyCallbackInfo<v8::Boolean>& args);
133
  Environment* const env_;
134
  v8::Global<v8::Context> context_;
135
  BaseObjectPtr<MicrotaskQueueWrap> microtask_queue_wrap_;
136
};
137
138
class ContextifyScript : public BaseObject {
139
 public:
140
  SET_NO_MEMORY_INFO()
141
  SET_MEMORY_INFO_NAME(ContextifyScript)
142
  SET_SELF_SIZE(ContextifyScript)
143
144
  ContextifyScript(Environment* env, v8::Local<v8::Object> object);
145
  ~ContextifyScript() override;
146
147
  static void Init(Environment* env, v8::Local<v8::Object> target);
148
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
149
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
150
  static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
151
  static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
152
  static void RunInContext(const v8::FunctionCallbackInfo<v8::Value>& args);
153
  static bool EvalMachine(Environment* env,
154
                          const int64_t timeout,
155
                          const bool display_errors,
156
                          const bool break_on_sigint,
157
                          const bool break_on_first_line,
158
                          std::shared_ptr<v8::MicrotaskQueue> microtask_queue,
159
                          const v8::FunctionCallbackInfo<v8::Value>& args);
160
161
5533
  inline uint32_t id() { return id_; }
162
163
 private:
164
  v8::Global<v8::UnboundScript> script_;
165
  uint32_t id_;
166
};
167
168
class CompiledFnEntry final : public BaseObject {
169
 public:
170
68
  SET_NO_MEMORY_INFO()
171
68
  SET_MEMORY_INFO_NAME(CompiledFnEntry)
172
68
  SET_SELF_SIZE(CompiledFnEntry)
173
174
  CompiledFnEntry(Environment* env,
175
                  v8::Local<v8::Object> object,
176
                  uint32_t id,
177
                  v8::Local<v8::ScriptOrModule> script);
178
  ~CompiledFnEntry();
179
180
  bool IsNotIndicativeOfMemoryLeakAtExit() const override { return true; }
181
182
 private:
183
  uint32_t id_;
184
  v8::Global<v8::ScriptOrModule> script_;
185
186
  static void WeakCallback(const v8::WeakCallbackInfo<CompiledFnEntry>& data);
187
};
188
189
}  // namespace contextify
190
}  // namespace node
191
192
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
193
194
#endif  // SRC_NODE_CONTEXTIFY_H_