• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 : public BaseObject {
45  public:
46   ContextifyContext(Environment* env,
47                     v8::Local<v8::Object> wrapper,
48                     v8::Local<v8::Context> v8_context,
49                     const ContextOptions& options);
50   ~ContextifyContext();
51 
52   void MemoryInfo(MemoryTracker* tracker) const override;
53   SET_MEMORY_INFO_NAME(ContextifyContext)
54   SET_SELF_SIZE(ContextifyContext)
55 
56   static v8::MaybeLocal<v8::Context> CreateV8Context(
57       v8::Isolate* isolate,
58       v8::Local<v8::ObjectTemplate> object_template,
59       const SnapshotData* snapshot_data,
60       v8::MicrotaskQueue* queue);
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 
context()68   inline v8::Local<v8::Context> context() const {
69     return PersistentToLocal::Default(env()->isolate(), context_);
70   }
71 
global_proxy()72   inline v8::Local<v8::Object> global_proxy() const {
73     return context()->Global();
74   }
75 
sandbox()76   inline v8::Local<v8::Object> sandbox() const {
77     return context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject)
78         .As<v8::Object>();
79   }
80 
microtask_queue()81   inline std::shared_ptr<v8::MicrotaskQueue> microtask_queue() const {
82     if (!microtask_queue_wrap_) return {};
83     return microtask_queue_wrap_->microtask_queue();
84   }
85 
86   template <typename T>
87   static ContextifyContext* Get(const v8::PropertyCallbackInfo<T>& args);
88   static ContextifyContext* Get(v8::Local<v8::Object> object);
89 
90   static void InitializeGlobalTemplates(IsolateData* isolate_data);
91 
92  private:
93   static BaseObjectPtr<ContextifyContext> New(Environment* env,
94                                               v8::Local<v8::Object> sandbox_obj,
95                                               const ContextOptions& options);
96   // Initialize a context created from CreateV8Context()
97   static BaseObjectPtr<ContextifyContext> New(v8::Local<v8::Context> ctx,
98                                               Environment* env,
99                                               v8::Local<v8::Object> sandbox_obj,
100                                               const ContextOptions& options);
101 
102   static bool IsStillInitializing(const ContextifyContext* ctx);
103   static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args);
104   static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
105   static void CompileFunction(
106       const v8::FunctionCallbackInfo<v8::Value>& args);
107   static void WeakCallback(
108       const v8::WeakCallbackInfo<ContextifyContext>& data);
109   static void PropertyGetterCallback(
110       v8::Local<v8::Name> property,
111       const v8::PropertyCallbackInfo<v8::Value>& args);
112   static void PropertySetterCallback(
113       v8::Local<v8::Name> property,
114       v8::Local<v8::Value> value,
115       const v8::PropertyCallbackInfo<v8::Value>& args);
116   static void PropertyDescriptorCallback(
117       v8::Local<v8::Name> property,
118       const v8::PropertyCallbackInfo<v8::Value>& args);
119   static void PropertyDefinerCallback(
120       v8::Local<v8::Name> property,
121       const v8::PropertyDescriptor& desc,
122       const v8::PropertyCallbackInfo<v8::Value>& args);
123   static void PropertyDeleterCallback(
124       v8::Local<v8::Name> property,
125       const v8::PropertyCallbackInfo<v8::Boolean>& args);
126   static void PropertyEnumeratorCallback(
127       const v8::PropertyCallbackInfo<v8::Array>& args);
128   static void IndexedPropertyGetterCallback(
129       uint32_t index,
130       const v8::PropertyCallbackInfo<v8::Value>& args);
131   static void IndexedPropertySetterCallback(
132       uint32_t index,
133       v8::Local<v8::Value> value,
134       const v8::PropertyCallbackInfo<v8::Value>& args);
135   static void IndexedPropertyDescriptorCallback(
136       uint32_t index,
137       const v8::PropertyCallbackInfo<v8::Value>& args);
138   static void IndexedPropertyDefinerCallback(
139       uint32_t index,
140       const v8::PropertyDescriptor& desc,
141       const v8::PropertyCallbackInfo<v8::Value>& args);
142   static void IndexedPropertyDeleterCallback(
143       uint32_t index,
144       const v8::PropertyCallbackInfo<v8::Boolean>& args);
145 
146   v8::Global<v8::Context> context_;
147   BaseObjectPtr<MicrotaskQueueWrap> microtask_queue_wrap_;
148 };
149 
150 class ContextifyScript : public BaseObject {
151  public:
152   SET_NO_MEMORY_INFO()
153   SET_MEMORY_INFO_NAME(ContextifyScript)
154   SET_SELF_SIZE(ContextifyScript)
155 
156   ContextifyScript(Environment* env, v8::Local<v8::Object> object);
157   ~ContextifyScript() override;
158 
159   static void Init(Environment* env, v8::Local<v8::Object> target);
160   static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
161   static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
162   static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
163   static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
164   static void RunInContext(const v8::FunctionCallbackInfo<v8::Value>& args);
165   static bool EvalMachine(v8::Local<v8::Context> context,
166                           Environment* env,
167                           const int64_t timeout,
168                           const bool display_errors,
169                           const bool break_on_sigint,
170                           const bool break_on_first_line,
171                           std::shared_ptr<v8::MicrotaskQueue> microtask_queue,
172                           const v8::FunctionCallbackInfo<v8::Value>& args);
173 
id()174   inline uint32_t id() { return id_; }
175 
176  private:
177   v8::Global<v8::UnboundScript> script_;
178   uint32_t id_;
179 };
180 
181 class CompiledFnEntry final : public BaseObject {
182  public:
183   SET_NO_MEMORY_INFO()
184   SET_MEMORY_INFO_NAME(CompiledFnEntry)
185   SET_SELF_SIZE(CompiledFnEntry)
186 
187   CompiledFnEntry(Environment* env,
188                   v8::Local<v8::Object> object,
189                   uint32_t id,
190                   v8::Local<v8::Function> fn);
191   ~CompiledFnEntry();
192 
IsNotIndicativeOfMemoryLeakAtExit()193   bool IsNotIndicativeOfMemoryLeakAtExit() const override { return true; }
194 
195  private:
196   uint32_t id_;
197   v8::Global<v8::Function> fn_;
198 
199   static void WeakCallback(const v8::WeakCallbackInfo<CompiledFnEntry>& data);
200 };
201 
202 v8::Maybe<bool> StoreCodeCacheResult(
203     Environment* env,
204     v8::Local<v8::Object> target,
205     v8::ScriptCompiler::CompileOptions compile_options,
206     const v8::ScriptCompiler::Source& source,
207     bool produce_cached_data,
208     std::unique_ptr<v8::ScriptCompiler::CachedData> new_cached_data);
209 
210 }  // namespace contextify
211 }  // namespace node
212 
213 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
214 
215 #endif  // SRC_NODE_CONTEXTIFY_H_
216