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 enum InternalFields { 153 kUnboundScriptSlot = BaseObject::kInternalFieldCount, 154 kInternalFieldCount 155 }; 156 157 SET_NO_MEMORY_INFO() 158 SET_MEMORY_INFO_NAME(ContextifyScript) 159 SET_SELF_SIZE(ContextifyScript) 160 161 ContextifyScript(Environment* env, v8::Local<v8::Object> object); 162 ~ContextifyScript() override; 163 164 static void Init(Environment* env, v8::Local<v8::Object> target); 165 static void RegisterExternalReferences(ExternalReferenceRegistry* registry); 166 static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 167 static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args); 168 static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args); 169 static void RunInContext(const v8::FunctionCallbackInfo<v8::Value>& args); 170 static bool EvalMachine(v8::Local<v8::Context> context, 171 Environment* env, 172 const int64_t timeout, 173 const bool display_errors, 174 const bool break_on_sigint, 175 const bool break_on_first_line, 176 std::shared_ptr<v8::MicrotaskQueue> microtask_queue, 177 const v8::FunctionCallbackInfo<v8::Value>& args); 178 179 private: 180 v8::Global<v8::UnboundScript> script_; 181 }; 182 183 v8::Maybe<bool> StoreCodeCacheResult( 184 Environment* env, 185 v8::Local<v8::Object> target, 186 v8::ScriptCompiler::CompileOptions compile_options, 187 const v8::ScriptCompiler::Source& source, 188 bool produce_cached_data, 189 std::unique_ptr<v8::ScriptCompiler::CachedData> new_cached_data); 190 191 } // namespace contextify 192 } // namespace node 193 194 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 195 196 #endif // SRC_NODE_CONTEXTIFY_H_ 197