1 #ifndef SRC_MODULE_WRAP_H_ 2 #define SRC_MODULE_WRAP_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include <unordered_map> 7 #include <string> 8 #include <vector> 9 #include "base_object.h" 10 11 namespace node { 12 13 class Environment; 14 15 namespace contextify { 16 class ContextifyContext; 17 } 18 19 namespace loader { 20 21 enum ScriptType : int { 22 kScript, 23 kModule, 24 kFunction, 25 }; 26 27 enum HostDefinedOptions : int { 28 kType = 8, 29 kID = 9, 30 kLength = 10, 31 }; 32 33 class ModuleWrap : public BaseObject { 34 public: 35 enum InternalFields { 36 kModuleWrapBaseField = BaseObject::kInternalFieldCount, 37 kURLSlot, 38 kSyntheticEvaluationStepsSlot, 39 kContextObjectSlot, // Object whose creation context is the target Context 40 kInternalFieldCount 41 }; 42 43 static void Initialize(v8::Local<v8::Object> target, 44 v8::Local<v8::Value> unused, 45 v8::Local<v8::Context> context, 46 void* priv); 47 static void HostInitializeImportMetaObjectCallback( 48 v8::Local<v8::Context> context, 49 v8::Local<v8::Module> module, 50 v8::Local<v8::Object> meta); 51 MemoryInfo(MemoryTracker * tracker)52 void MemoryInfo(MemoryTracker* tracker) const override { 53 tracker->TrackField("resolve_cache", resolve_cache_); 54 } 55 id()56 inline uint32_t id() { return id_; } 57 v8::Local<v8::Context> context() const; 58 static ModuleWrap* GetFromID(node::Environment*, uint32_t id); 59 60 SET_MEMORY_INFO_NAME(ModuleWrap) SET_SELF_SIZE(ModuleWrap)61 SET_SELF_SIZE(ModuleWrap) 62 63 bool IsNotIndicativeOfMemoryLeakAtExit() const override { 64 // XXX: The garbage collection rules for ModuleWrap are *super* unclear. 65 // Do these objects ever get GC'd? Are we just okay with leaking them? 66 return true; 67 } 68 69 private: 70 ModuleWrap(Environment* env, 71 v8::Local<v8::Object> object, 72 v8::Local<v8::Module> module, 73 v8::Local<v8::String> url); 74 ~ModuleWrap() override; 75 76 static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 77 static void Link(const v8::FunctionCallbackInfo<v8::Value>& args); 78 static void Instantiate(const v8::FunctionCallbackInfo<v8::Value>& args); 79 static void Evaluate(const v8::FunctionCallbackInfo<v8::Value>& args); 80 static void GetNamespace(const v8::FunctionCallbackInfo<v8::Value>& args); 81 static void GetStatus(const v8::FunctionCallbackInfo<v8::Value>& args); 82 static void GetError(const v8::FunctionCallbackInfo<v8::Value>& args); 83 static void GetStaticDependencySpecifiers( 84 const v8::FunctionCallbackInfo<v8::Value>& args); 85 86 static void SetImportModuleDynamicallyCallback( 87 const v8::FunctionCallbackInfo<v8::Value>& args); 88 static void SetInitializeImportMetaObjectCallback( 89 const v8::FunctionCallbackInfo<v8::Value>& args); 90 static v8::MaybeLocal<v8::Value> SyntheticModuleEvaluationStepsCallback( 91 v8::Local<v8::Context> context, v8::Local<v8::Module> module); 92 static void SetSyntheticExport( 93 const v8::FunctionCallbackInfo<v8::Value>& args); 94 static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args); 95 96 static v8::MaybeLocal<v8::Module> ResolveCallback( 97 v8::Local<v8::Context> context, 98 v8::Local<v8::String> specifier, 99 v8::Local<v8::Module> referrer); 100 static ModuleWrap* GetFromModule(node::Environment*, v8::Local<v8::Module>); 101 102 v8::Global<v8::Module> module_; 103 std::unordered_map<std::string, v8::Global<v8::Promise>> resolve_cache_; 104 contextify::ContextifyContext* contextify_context_ = nullptr; 105 bool synthetic_ = false; 106 bool linked_ = false; 107 uint32_t id_; 108 }; 109 110 } // namespace loader 111 } // namespace node 112 113 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 114 115 #endif // SRC_MODULE_WRAP_H_ 116