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 namespace contextify { 12 13 struct ContextOptions { 14 v8::Local<v8::String> name; 15 v8::Local<v8::String> origin; 16 v8::Local<v8::Boolean> allow_code_gen_strings; 17 v8::Local<v8::Boolean> allow_code_gen_wasm; 18 }; 19 20 class ContextifyContext { 21 public: 22 enum InternalFields { kSlot, kInternalFieldCount }; 23 ContextifyContext(Environment* env, 24 v8::Local<v8::Object> sandbox_obj, 25 const ContextOptions& options); 26 ~ContextifyContext(); 27 static void CleanupHook(void* arg); 28 29 v8::MaybeLocal<v8::Object> CreateDataWrapper(Environment* env); 30 v8::MaybeLocal<v8::Context> CreateV8Context(Environment* env, 31 v8::Local<v8::Object> sandbox_obj, 32 const ContextOptions& options); 33 static void Init(Environment* env, v8::Local<v8::Object> target); 34 35 static ContextifyContext* ContextFromContextifiedSandbox( 36 Environment* env, 37 const v8::Local<v8::Object>& sandbox); 38 env()39 inline Environment* env() const { 40 return env_; 41 } 42 context()43 inline v8::Local<v8::Context> context() const { 44 return PersistentToLocal::Default(env()->isolate(), context_); 45 } 46 global_proxy()47 inline v8::Local<v8::Object> global_proxy() const { 48 return context()->Global(); 49 } 50 sandbox()51 inline v8::Local<v8::Object> sandbox() const { 52 return v8::Local<v8::Object>::Cast( 53 context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject)); 54 } 55 56 57 template <typename T> 58 static ContextifyContext* Get(const v8::PropertyCallbackInfo<T>& args); 59 60 private: 61 static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args); 62 static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args); 63 static void CompileFunction( 64 const v8::FunctionCallbackInfo<v8::Value>& args); 65 static void WeakCallback( 66 const v8::WeakCallbackInfo<ContextifyContext>& data); 67 static void PropertyGetterCallback( 68 v8::Local<v8::Name> property, 69 const v8::PropertyCallbackInfo<v8::Value>& args); 70 static void PropertySetterCallback( 71 v8::Local<v8::Name> property, 72 v8::Local<v8::Value> value, 73 const v8::PropertyCallbackInfo<v8::Value>& args); 74 static void PropertyDescriptorCallback( 75 v8::Local<v8::Name> property, 76 const v8::PropertyCallbackInfo<v8::Value>& args); 77 static void PropertyDefinerCallback( 78 v8::Local<v8::Name> property, 79 const v8::PropertyDescriptor& desc, 80 const v8::PropertyCallbackInfo<v8::Value>& args); 81 static void PropertyDeleterCallback( 82 v8::Local<v8::Name> property, 83 const v8::PropertyCallbackInfo<v8::Boolean>& args); 84 static void PropertyEnumeratorCallback( 85 const v8::PropertyCallbackInfo<v8::Array>& args); 86 static void IndexedPropertyGetterCallback( 87 uint32_t index, 88 const v8::PropertyCallbackInfo<v8::Value>& args); 89 static void IndexedPropertySetterCallback( 90 uint32_t index, 91 v8::Local<v8::Value> value, 92 const v8::PropertyCallbackInfo<v8::Value>& args); 93 static void IndexedPropertyDescriptorCallback( 94 uint32_t index, 95 const v8::PropertyCallbackInfo<v8::Value>& args); 96 static void IndexedPropertyDefinerCallback( 97 uint32_t index, 98 const v8::PropertyDescriptor& desc, 99 const v8::PropertyCallbackInfo<v8::Value>& args); 100 static void IndexedPropertyDeleterCallback( 101 uint32_t index, 102 const v8::PropertyCallbackInfo<v8::Boolean>& args); 103 Environment* const env_; 104 v8::Global<v8::Context> context_; 105 }; 106 107 class ContextifyScript : public BaseObject { 108 public: 109 SET_NO_MEMORY_INFO() 110 SET_MEMORY_INFO_NAME(ContextifyScript) 111 SET_SELF_SIZE(ContextifyScript) 112 113 ContextifyScript(Environment* env, v8::Local<v8::Object> object); 114 ~ContextifyScript() override; 115 116 static void Init(Environment* env, v8::Local<v8::Object> target); 117 static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 118 static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args); 119 static void CreateCachedData( 120 const v8::FunctionCallbackInfo<v8::Value>& args); 121 static void RunInThisContext(const v8::FunctionCallbackInfo<v8::Value>& args); 122 static void RunInContext(const v8::FunctionCallbackInfo<v8::Value>& args); 123 static bool EvalMachine(Environment* env, 124 const int64_t timeout, 125 const bool display_errors, 126 const bool break_on_sigint, 127 const bool break_on_first_line, 128 const v8::FunctionCallbackInfo<v8::Value>& args); 129 id()130 inline uint32_t id() { return id_; } 131 132 private: 133 v8::Global<v8::UnboundScript> script_; 134 uint32_t id_; 135 }; 136 137 class CompiledFnEntry final : public BaseObject { 138 public: 139 SET_NO_MEMORY_INFO() 140 SET_MEMORY_INFO_NAME(CompiledFnEntry) 141 SET_SELF_SIZE(CompiledFnEntry) 142 143 CompiledFnEntry(Environment* env, 144 v8::Local<v8::Object> object, 145 uint32_t id, 146 v8::Local<v8::ScriptOrModule> script); 147 ~CompiledFnEntry(); 148 149 private: 150 uint32_t id_; 151 v8::Global<v8::ScriptOrModule> script_; 152 153 static void WeakCallback(const v8::WeakCallbackInfo<CompiledFnEntry>& data); 154 }; 155 156 } // namespace contextify 157 } // namespace node 158 159 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 160 161 #endif // SRC_NODE_CONTEXTIFY_H_ 162