11cb0ef41Sopenharmony_ci#ifndef SRC_NODE_CONTEXTIFY_H_
21cb0ef41Sopenharmony_ci#define SRC_NODE_CONTEXTIFY_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include "base_object-inl.h"
71cb0ef41Sopenharmony_ci#include "node_context_data.h"
81cb0ef41Sopenharmony_ci#include "node_errors.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace node {
111cb0ef41Sopenharmony_ciclass ExternalReferenceRegistry;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace contextify {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciclass MicrotaskQueueWrap : public BaseObject {
161cb0ef41Sopenharmony_ci public:
171cb0ef41Sopenharmony_ci  MicrotaskQueueWrap(Environment* env, v8::Local<v8::Object> obj);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  const std::shared_ptr<v8::MicrotaskQueue>& microtask_queue() const;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  static void Init(Environment* env, v8::Local<v8::Object> target);
221cb0ef41Sopenharmony_ci  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
231cb0ef41Sopenharmony_ci  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  // This could have methods for running the microtask queue, if we ever decide
261cb0ef41Sopenharmony_ci  // to make that fully customizable from userland.
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  SET_NO_MEMORY_INFO()
291cb0ef41Sopenharmony_ci  SET_MEMORY_INFO_NAME(MicrotaskQueueWrap)
301cb0ef41Sopenharmony_ci  SET_SELF_SIZE(MicrotaskQueueWrap)
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci private:
331cb0ef41Sopenharmony_ci  std::shared_ptr<v8::MicrotaskQueue> microtask_queue_;
341cb0ef41Sopenharmony_ci};
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cistruct ContextOptions {
371cb0ef41Sopenharmony_ci  v8::Local<v8::String> name;
381cb0ef41Sopenharmony_ci  v8::Local<v8::String> origin;
391cb0ef41Sopenharmony_ci  v8::Local<v8::Boolean> allow_code_gen_strings;
401cb0ef41Sopenharmony_ci  v8::Local<v8::Boolean> allow_code_gen_wasm;
411cb0ef41Sopenharmony_ci  BaseObjectPtr<MicrotaskQueueWrap> microtask_queue_wrap;
421cb0ef41Sopenharmony_ci};
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciclass ContextifyContext : public BaseObject {
451cb0ef41Sopenharmony_ci public:
461cb0ef41Sopenharmony_ci  ContextifyContext(Environment* env,
471cb0ef41Sopenharmony_ci                    v8::Local<v8::Object> wrapper,
481cb0ef41Sopenharmony_ci                    v8::Local<v8::Context> v8_context,
491cb0ef41Sopenharmony_ci                    const ContextOptions& options);
501cb0ef41Sopenharmony_ci  ~ContextifyContext();
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  void MemoryInfo(MemoryTracker* tracker) const override;
531cb0ef41Sopenharmony_ci  SET_MEMORY_INFO_NAME(ContextifyContext)
541cb0ef41Sopenharmony_ci  SET_SELF_SIZE(ContextifyContext)
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  static v8::MaybeLocal<v8::Context> CreateV8Context(
571cb0ef41Sopenharmony_ci      v8::Isolate* isolate,
581cb0ef41Sopenharmony_ci      v8::Local<v8::ObjectTemplate> object_template,
591cb0ef41Sopenharmony_ci      const SnapshotData* snapshot_data,
601cb0ef41Sopenharmony_ci      v8::MicrotaskQueue* queue);
611cb0ef41Sopenharmony_ci  static void Init(Environment* env, v8::Local<v8::Object> target);
621cb0ef41Sopenharmony_ci  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  static ContextifyContext* ContextFromContextifiedSandbox(
651cb0ef41Sopenharmony_ci      Environment* env,
661cb0ef41Sopenharmony_ci      const v8::Local<v8::Object>& sandbox);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  inline v8::Local<v8::Context> context() const {
691cb0ef41Sopenharmony_ci    return PersistentToLocal::Default(env()->isolate(), context_);
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  inline v8::Local<v8::Object> global_proxy() const {
731cb0ef41Sopenharmony_ci    return context()->Global();
741cb0ef41Sopenharmony_ci  }
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  inline v8::Local<v8::Object> sandbox() const {
771cb0ef41Sopenharmony_ci    return context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject)
781cb0ef41Sopenharmony_ci        .As<v8::Object>();
791cb0ef41Sopenharmony_ci  }
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  inline std::shared_ptr<v8::MicrotaskQueue> microtask_queue() const {
821cb0ef41Sopenharmony_ci    if (!microtask_queue_wrap_) return {};
831cb0ef41Sopenharmony_ci    return microtask_queue_wrap_->microtask_queue();
841cb0ef41Sopenharmony_ci  }
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  template <typename T>
871cb0ef41Sopenharmony_ci  static ContextifyContext* Get(const v8::PropertyCallbackInfo<T>& args);
881cb0ef41Sopenharmony_ci  static ContextifyContext* Get(v8::Local<v8::Object> object);
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  static void InitializeGlobalTemplates(IsolateData* isolate_data);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci private:
931cb0ef41Sopenharmony_ci  static BaseObjectPtr<ContextifyContext> New(Environment* env,
941cb0ef41Sopenharmony_ci                                              v8::Local<v8::Object> sandbox_obj,
951cb0ef41Sopenharmony_ci                                              const ContextOptions& options);
961cb0ef41Sopenharmony_ci  // Initialize a context created from CreateV8Context()
971cb0ef41Sopenharmony_ci  static BaseObjectPtr<ContextifyContext> New(v8::Local<v8::Context> ctx,
981cb0ef41Sopenharmony_ci                                              Environment* env,
991cb0ef41Sopenharmony_ci                                              v8::Local<v8::Object> sandbox_obj,
1001cb0ef41Sopenharmony_ci                                              const ContextOptions& options);
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  static bool IsStillInitializing(const ContextifyContext* ctx);
1031cb0ef41Sopenharmony_ci  static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args);
1041cb0ef41Sopenharmony_ci  static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
1051cb0ef41Sopenharmony_ci  static void CompileFunction(
1061cb0ef41Sopenharmony_ci      const v8::FunctionCallbackInfo<v8::Value>& args);
1071cb0ef41Sopenharmony_ci  static void WeakCallback(
1081cb0ef41Sopenharmony_ci      const v8::WeakCallbackInfo<ContextifyContext>& data);
1091cb0ef41Sopenharmony_ci  static void PropertyGetterCallback(
1101cb0ef41Sopenharmony_ci      v8::Local<v8::Name> property,
1111cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Value>& args);
1121cb0ef41Sopenharmony_ci  static void PropertySetterCallback(
1131cb0ef41Sopenharmony_ci      v8::Local<v8::Name> property,
1141cb0ef41Sopenharmony_ci      v8::Local<v8::Value> value,
1151cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Value>& args);
1161cb0ef41Sopenharmony_ci  static void PropertyDescriptorCallback(
1171cb0ef41Sopenharmony_ci      v8::Local<v8::Name> property,
1181cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Value>& args);
1191cb0ef41Sopenharmony_ci  static void PropertyDefinerCallback(
1201cb0ef41Sopenharmony_ci      v8::Local<v8::Name> property,
1211cb0ef41Sopenharmony_ci      const v8::PropertyDescriptor& desc,
1221cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Value>& args);
1231cb0ef41Sopenharmony_ci  static void PropertyDeleterCallback(
1241cb0ef41Sopenharmony_ci      v8::Local<v8::Name> property,
1251cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Boolean>& args);
1261cb0ef41Sopenharmony_ci  static void PropertyEnumeratorCallback(
1271cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Array>& args);
1281cb0ef41Sopenharmony_ci  static void IndexedPropertyGetterCallback(
1291cb0ef41Sopenharmony_ci      uint32_t index,
1301cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Value>& args);
1311cb0ef41Sopenharmony_ci  static void IndexedPropertySetterCallback(
1321cb0ef41Sopenharmony_ci      uint32_t index,
1331cb0ef41Sopenharmony_ci      v8::Local<v8::Value> value,
1341cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Value>& args);
1351cb0ef41Sopenharmony_ci  static void IndexedPropertyDescriptorCallback(
1361cb0ef41Sopenharmony_ci      uint32_t index,
1371cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Value>& args);
1381cb0ef41Sopenharmony_ci  static void IndexedPropertyDefinerCallback(
1391cb0ef41Sopenharmony_ci      uint32_t index,
1401cb0ef41Sopenharmony_ci      const v8::PropertyDescriptor& desc,
1411cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Value>& args);
1421cb0ef41Sopenharmony_ci  static void IndexedPropertyDeleterCallback(
1431cb0ef41Sopenharmony_ci      uint32_t index,
1441cb0ef41Sopenharmony_ci      const v8::PropertyCallbackInfo<v8::Boolean>& args);
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci  v8::Global<v8::Context> context_;
1471cb0ef41Sopenharmony_ci  BaseObjectPtr<MicrotaskQueueWrap> microtask_queue_wrap_;
1481cb0ef41Sopenharmony_ci};
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ciclass ContextifyScript : public BaseObject {
1511cb0ef41Sopenharmony_ci public:
1521cb0ef41Sopenharmony_ci  enum InternalFields {
1531cb0ef41Sopenharmony_ci    kUnboundScriptSlot = BaseObject::kInternalFieldCount,
1541cb0ef41Sopenharmony_ci    kInternalFieldCount
1551cb0ef41Sopenharmony_ci  };
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci  SET_NO_MEMORY_INFO()
1581cb0ef41Sopenharmony_ci  SET_MEMORY_INFO_NAME(ContextifyScript)
1591cb0ef41Sopenharmony_ci  SET_SELF_SIZE(ContextifyScript)
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci  ContextifyScript(Environment* env, v8::Local<v8::Object> object);
1621cb0ef41Sopenharmony_ci  ~ContextifyScript() override;
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  static void Init(Environment* env, v8::Local<v8::Object> target);
1651cb0ef41Sopenharmony_ci  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
1661cb0ef41Sopenharmony_ci  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
1671cb0ef41Sopenharmony_ci  static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
1681cb0ef41Sopenharmony_ci  static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
1691cb0ef41Sopenharmony_ci  static void RunInContext(const v8::FunctionCallbackInfo<v8::Value>& args);
1701cb0ef41Sopenharmony_ci  static bool EvalMachine(v8::Local<v8::Context> context,
1711cb0ef41Sopenharmony_ci                          Environment* env,
1721cb0ef41Sopenharmony_ci                          const int64_t timeout,
1731cb0ef41Sopenharmony_ci                          const bool display_errors,
1741cb0ef41Sopenharmony_ci                          const bool break_on_sigint,
1751cb0ef41Sopenharmony_ci                          const bool break_on_first_line,
1761cb0ef41Sopenharmony_ci                          std::shared_ptr<v8::MicrotaskQueue> microtask_queue,
1771cb0ef41Sopenharmony_ci                          const v8::FunctionCallbackInfo<v8::Value>& args);
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci private:
1801cb0ef41Sopenharmony_ci  v8::Global<v8::UnboundScript> script_;
1811cb0ef41Sopenharmony_ci};
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_civ8::Maybe<bool> StoreCodeCacheResult(
1841cb0ef41Sopenharmony_ci    Environment* env,
1851cb0ef41Sopenharmony_ci    v8::Local<v8::Object> target,
1861cb0ef41Sopenharmony_ci    v8::ScriptCompiler::CompileOptions compile_options,
1871cb0ef41Sopenharmony_ci    const v8::ScriptCompiler::Source& source,
1881cb0ef41Sopenharmony_ci    bool produce_cached_data,
1891cb0ef41Sopenharmony_ci    std::unique_ptr<v8::ScriptCompiler::CachedData> new_cached_data);
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci}  // namespace contextify
1921cb0ef41Sopenharmony_ci}  // namespace node
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci#endif  // SRC_NODE_CONTEXTIFY_H_
197