Coverage Report

Created: 2025-09-05 10:05

/src/node/src/node_contextify.h
Line
Count
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
struct ContextOptions {
16
  v8::Local<v8::String> name;
17
  v8::Local<v8::String> origin;
18
  v8::Local<v8::Boolean> allow_code_gen_strings;
19
  v8::Local<v8::Boolean> allow_code_gen_wasm;
20
  std::unique_ptr<v8::MicrotaskQueue> own_microtask_queue;
21
  v8::Local<v8::Symbol> host_defined_options_id;
22
};
23
24
class ContextifyContext : public BaseObject {
25
 public:
26
  ContextifyContext(Environment* env,
27
                    v8::Local<v8::Object> wrapper,
28
                    v8::Local<v8::Context> v8_context,
29
                    ContextOptions* options);
30
  ~ContextifyContext();
31
32
  void MemoryInfo(MemoryTracker* tracker) const override;
33
  SET_MEMORY_INFO_NAME(ContextifyContext)
34
  SET_SELF_SIZE(ContextifyContext)
35
36
  static v8::MaybeLocal<v8::Context> CreateV8Context(
37
      v8::Isolate* isolate,
38
      v8::Local<v8::ObjectTemplate> object_template,
39
      const SnapshotData* snapshot_data,
40
      v8::MicrotaskQueue* queue);
41
  static void CreatePerIsolateProperties(IsolateData* isolate_data,
42
                                         v8::Local<v8::ObjectTemplate> target);
43
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
44
45
  static ContextifyContext* ContextFromContextifiedSandbox(
46
      Environment* env,
47
      const v8::Local<v8::Object>& sandbox);
48
49
16.5k
  inline v8::Local<v8::Context> context() const {
50
16.5k
    return PersistentToLocal::Default(env()->isolate(), context_);
51
16.5k
  }
52
53
3.81k
  inline v8::Local<v8::Object> global_proxy() const {
54
3.81k
    return context()->Global();
55
3.81k
  }
56
57
5.09k
  inline v8::Local<v8::Object> sandbox() const {
58
5.09k
    return context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject)
59
5.09k
        .As<v8::Object>();
60
5.09k
  }
61
62
1.27k
  inline v8::MicrotaskQueue* microtask_queue() const {
63
1.27k
    return microtask_queue_.get();
64
1.27k
  }
65
66
  template <typename T>
67
  static ContextifyContext* Get(const v8::PropertyCallbackInfo<T>& args);
68
  static ContextifyContext* Get(v8::Local<v8::Object> object);
69
70
  static void InitializeGlobalTemplates(IsolateData* isolate_data);
71
72
 private:
73
  static BaseObjectPtr<ContextifyContext> New(Environment* env,
74
                                              v8::Local<v8::Object> sandbox_obj,
75
                                              ContextOptions* options);
76
  // Initialize a context created from CreateV8Context()
77
  static BaseObjectPtr<ContextifyContext> New(v8::Local<v8::Context> ctx,
78
                                              Environment* env,
79
                                              v8::Local<v8::Object> sandbox_obj,
80
                                              ContextOptions* options);
81
82
  static bool IsStillInitializing(const ContextifyContext* ctx);
83
  static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args);
84
  static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
85
  static void CompileFunction(
86
      const v8::FunctionCallbackInfo<v8::Value>& args);
87
  static v8::Local<v8::Object> CompileFunctionAndCacheResult(
88
      Environment* env,
89
      v8::Local<v8::Context> parsing_context,
90
      v8::ScriptCompiler::Source* source,
91
      std::vector<v8::Local<v8::String>> params,
92
      std::vector<v8::Local<v8::Object>> context_extensions,
93
      v8::ScriptCompiler::CompileOptions options,
94
      bool produce_cached_data,
95
      v8::Local<v8::Symbol> id_symbol,
96
      const errors::TryCatchScope& try_catch);
97
  static v8::ScriptCompiler::Source GetCommonJSSourceInstance(
98
      v8::Isolate* isolate,
99
      v8::Local<v8::String> code,
100
      v8::Local<v8::String> filename,
101
      int line_offset,
102
      int column_offset,
103
      v8::Local<v8::PrimitiveArray> host_defined_options,
104
      v8::ScriptCompiler::CachedData* cached_data);
105
  static v8::ScriptCompiler::CompileOptions GetCompileOptions(
106
      const v8::ScriptCompiler::Source& source);
107
  static void ContainsModuleSyntax(
108
      const v8::FunctionCallbackInfo<v8::Value>& args);
109
  static void ShouldRetryAsESM(const v8::FunctionCallbackInfo<v8::Value>& args);
110
  static bool ShouldRetryAsESMInternal(Environment* env,
111
                                       v8::Local<v8::String> code);
112
  static void WeakCallback(
113
      const v8::WeakCallbackInfo<ContextifyContext>& data);
114
  static void PropertyGetterCallback(
115
      v8::Local<v8::Name> property,
116
      const v8::PropertyCallbackInfo<v8::Value>& args);
117
  static void PropertySetterCallback(
118
      v8::Local<v8::Name> property,
119
      v8::Local<v8::Value> value,
120
      const v8::PropertyCallbackInfo<v8::Value>& args);
121
  static void PropertyDescriptorCallback(
122
      v8::Local<v8::Name> property,
123
      const v8::PropertyCallbackInfo<v8::Value>& args);
124
  static void PropertyDefinerCallback(
125
      v8::Local<v8::Name> property,
126
      const v8::PropertyDescriptor& desc,
127
      const v8::PropertyCallbackInfo<v8::Value>& args);
128
  static void PropertyDeleterCallback(
129
      v8::Local<v8::Name> property,
130
      const v8::PropertyCallbackInfo<v8::Boolean>& args);
131
  static void PropertyEnumeratorCallback(
132
      const v8::PropertyCallbackInfo<v8::Array>& args);
133
  static void IndexedPropertyGetterCallback(
134
      uint32_t index,
135
      const v8::PropertyCallbackInfo<v8::Value>& args);
136
  static void IndexedPropertySetterCallback(
137
      uint32_t index,
138
      v8::Local<v8::Value> value,
139
      const v8::PropertyCallbackInfo<v8::Value>& args);
140
  static void IndexedPropertyDescriptorCallback(
141
      uint32_t index,
142
      const v8::PropertyCallbackInfo<v8::Value>& args);
143
  static void IndexedPropertyDefinerCallback(
144
      uint32_t index,
145
      const v8::PropertyDescriptor& desc,
146
      const v8::PropertyCallbackInfo<v8::Value>& args);
147
  static void IndexedPropertyDeleterCallback(
148
      uint32_t index,
149
      const v8::PropertyCallbackInfo<v8::Boolean>& args);
150
151
  v8::Global<v8::Context> context_;
152
  std::unique_ptr<v8::MicrotaskQueue> microtask_queue_;
153
};
154
155
class ContextifyScript : public BaseObject {
156
 public:
157
  enum InternalFields {
158
    kUnboundScriptSlot = BaseObject::kInternalFieldCount,
159
    kInternalFieldCount
160
  };
161
162
  SET_NO_MEMORY_INFO()
163
  SET_MEMORY_INFO_NAME(ContextifyScript)
164
  SET_SELF_SIZE(ContextifyScript)
165
166
  ContextifyScript(Environment* env, v8::Local<v8::Object> object);
167
  ~ContextifyScript() override;
168
169
  static void CreatePerIsolateProperties(IsolateData* isolate_data,
170
                                         v8::Local<v8::ObjectTemplate> target);
171
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
172
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
173
  static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
174
  static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
175
  static void RunInContext(const v8::FunctionCallbackInfo<v8::Value>& args);
176
  static bool EvalMachine(v8::Local<v8::Context> context,
177
                          Environment* env,
178
                          const int64_t timeout,
179
                          const bool display_errors,
180
                          const bool break_on_sigint,
181
                          const bool break_on_first_line,
182
                          v8::MicrotaskQueue* microtask_queue,
183
                          const v8::FunctionCallbackInfo<v8::Value>& args);
184
185
 private:
186
  v8::Global<v8::UnboundScript> script_;
187
};
188
189
v8::Maybe<bool> StoreCodeCacheResult(
190
    Environment* env,
191
    v8::Local<v8::Object> target,
192
    v8::ScriptCompiler::CompileOptions compile_options,
193
    const v8::ScriptCompiler::Source& source,
194
    bool produce_cached_data,
195
    std::unique_ptr<v8::ScriptCompiler::CachedData> new_cached_data);
196
197
v8::MaybeLocal<v8::Function> CompileFunction(
198
    v8::Local<v8::Context> context,
199
    v8::Local<v8::String> filename,
200
    v8::Local<v8::String> content,
201
    std::vector<v8::Local<v8::String>>* parameters);
202
203
}  // namespace contextify
204
}  // namespace node
205
206
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
207
208
#endif  // SRC_NODE_CONTEXTIFY_H_