Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/module_wrap.h
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 <optional> 7 #include <string> 8 #include <unordered_map> 9 #include <vector> 10 #include "base_object.h" 11 #include "v8-script.h" 12 13 namespace node { 14 15 class IsolateData; 16 class Environment; 17 class ExternalReferenceRegistry; 18 19 namespace contextify { 20 class ContextifyContext; 21 } 22 23 namespace loader { 24 25 enum ScriptType : int { 26 kScript, 27 kModule, 28 kFunction, 29 }; 30 31 enum HostDefinedOptions : int { 32 kID = 8, 33 kLength = 9, 34 }; 35 36 /** 37 * ModuleCacheKey is used to uniquely identify a module request 38 * in the module cache. It is a composition of the module specifier 39 * and the import attributes. 40 */ 41 struct ModuleCacheKey : public MemoryRetainer { 42 using ImportAttributeVector = 43 std::vector<std::pair<std::string, std::string>>; 44 45 std::string specifier; 46 ImportAttributeVector import_attributes; 47 // A hash of the specifier, and import attributes. 48 // This does not guarantee uniqueness, but is used to reduce 49 // the number of comparisons needed when checking for equality. 50 std::size_t hash; 51 52 SET_MEMORY_INFO_NAME(ModuleCacheKey) 53 SET_SELF_SIZE(ModuleCacheKey) 54 void MemoryInfo(MemoryTracker* tracker) const override; 55 56 // Returns a string representation of the ModuleCacheKey. 57 std::string ToString() const; 58 59 template <int elements_per_attribute = 3> 60 static ModuleCacheKey From(v8::Local<v8::Context> context, 61 v8::Local<v8::String> specifier, 62 v8::Local<v8::FixedArray> import_attributes); 63 static ModuleCacheKey From(v8::Local<v8::Context> context, 64 v8::Local<v8::ModuleRequest> v8_request); 65 66 struct Hash { 67 std::size_t operator()(const ModuleCacheKey& request) const { 68 return request.hash; 69 } 70 }; 71 72 // Equality operator for ModuleCacheKey. 73 bool operator==(const ModuleCacheKey& other) const { 74 // Hash does not provide uniqueness guarantee, so ignore it. 75 return specifier == other.specifier && 76 import_attributes == other.import_attributes; 77 } 78 79 private: 80 // Use public ModuleCacheKey::From to create instances. 81 ModuleCacheKey(std::string specifier, 82 ImportAttributeVector import_attributes, 83 std::size_t hash) 84 : specifier(specifier), 85 import_attributes(import_attributes), 86 hash(hash) {} 87 }; 88 89 class ModuleWrap : public BaseObject { 90 using ResolveCache = 91 std::unordered_map<ModuleCacheKey, uint32_t, ModuleCacheKey::Hash>; 92 93 public: 94 enum InternalFields { 95 kModuleSlot = BaseObject::kInternalFieldCount, 96 kURLSlot, 97 kSyntheticEvaluationStepsSlot, 98 kContextObjectSlot, // Object whose creation context is the target Context 99 kLinkedRequestsSlot, // Array of linked requests 100 kInternalFieldCount 101 }; 102 103 static void CreatePerIsolateProperties(IsolateData* isolate_data, 104 v8::Local<v8::ObjectTemplate> target); 105 static void CreatePerContextProperties(v8::Local<v8::Object> target, 106 v8::Local<v8::Value> unused, 107 v8::Local<v8::Context> context, 108 void* priv); 109 static void RegisterExternalReferences(ExternalReferenceRegistry* registry); 110 static void HostInitializeImportMetaObjectCallback( 111 v8::Local<v8::Context> context, 112 v8::Local<v8::Module> module, 113 v8::Local<v8::Object> meta); 114 115 static void HasTopLevelAwait(const v8::FunctionCallbackInfo<v8::Value>& args); 116 117 v8::Local<v8::Context> context() const; 118 v8::Maybe<bool> CheckUnsettledTopLevelAwait(); 119 120 SET_MEMORY_INFO_NAME(ModuleWrap) 121 SET_SELF_SIZE(ModuleWrap) 122 SET_NO_MEMORY_INFO() 123 124 bool IsNotIndicativeOfMemoryLeakAtExit() const override { 125 // XXX: The garbage collection rules for ModuleWrap are *super* unclear. 126 // Do these objects ever get GC'd? Are we just okay with leaking them? 127 return true; 128 } 129 130 bool IsLinked() const { return linked_; } 131 132 ModuleWrap* GetLinkedRequest(uint32_t index); 133 134 static v8::Local<v8::PrimitiveArray> GetHostDefinedOptions( 135 v8::Isolate* isolate, v8::Local<v8::Symbol> symbol); 136 137 // When user_cached_data is not std::nullopt, use the code cache if it's not 138 // nullptr, otherwise don't use code cache. 139 // TODO(joyeecheung): when it is std::nullopt, use on-disk cache 140 // See: https://github.com/nodejs/node/issues/47472 141 static v8::MaybeLocal<v8::Module> CompileSourceTextModule( 142 Realm* realm, 143 v8::Local<v8::String> source_text, 144 v8::Local<v8::String> url, 145 int line_offset, 146 int column_offset, 147 v8::Local<v8::PrimitiveArray> host_defined_options, 148 std::optional<v8::ScriptCompiler::CachedData*> user_cached_data, 149 bool* cache_rejected); 150 151 static void CreateRequiredModuleFacade( 152 const v8::FunctionCallbackInfo<v8::Value>& args); 153 154 private: 155 ModuleWrap(Realm* realm, 156 v8::Local<v8::Object> object, 157 v8::Local<v8::Module> module, 158 v8::Local<v8::String> url, 159 v8::Local<v8::Object> context_object, 160 v8::Local<v8::Value> synthetic_evaluation_step); 161 ~ModuleWrap() override; 162 163 static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 164 static void GetModuleRequests( 165 const v8::FunctionCallbackInfo<v8::Value>& args); 166 static void InstantiateSync(const v8::FunctionCallbackInfo<v8::Value>& args); 167 static void EvaluateSync(const v8::FunctionCallbackInfo<v8::Value>& args); 168 static void GetNamespaceSync(const v8::FunctionCallbackInfo<v8::Value>& args); 169 170 static void Link(const v8::FunctionCallbackInfo<v8::Value>& args); 171 static void Instantiate(const v8::FunctionCallbackInfo<v8::Value>& args); 172 static void Evaluate(const v8::FunctionCallbackInfo<v8::Value>& args); 173 static void GetNamespace(const v8::FunctionCallbackInfo<v8::Value>& args); 174 static void IsGraphAsync(const v8::FunctionCallbackInfo<v8::Value>& args); 175 static void GetStatus(const v8::FunctionCallbackInfo<v8::Value>& args); 176 static void GetError(const v8::FunctionCallbackInfo<v8::Value>& args); 177 178 static void SetImportModuleDynamicallyCallback( 179 const v8::FunctionCallbackInfo<v8::Value>& args); 180 static void SetInitializeImportMetaObjectCallback( 181 const v8::FunctionCallbackInfo<v8::Value>& args); 182 static v8::MaybeLocal<v8::Value> SyntheticModuleEvaluationStepsCallback( 183 v8::Local<v8::Context> context, v8::Local<v8::Module> module); 184 static void SetSyntheticExport( 185 const v8::FunctionCallbackInfo<v8::Value>& args); 186 static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args); 187 188 static v8::MaybeLocal<v8::Module> ResolveModuleCallback( 189 v8::Local<v8::Context> context, 190 v8::Local<v8::String> specifier, 191 v8::Local<v8::FixedArray> import_attributes, 192 v8::Local<v8::Module> referrer); 193 static ModuleWrap* GetFromModule(node::Environment*, v8::Local<v8::Module>); 194 195 // This method may throw a JavaScript exception, so the return type is 196 // wrapped in a Maybe. 197 static v8::Maybe<ModuleWrap*> ResolveModule( 198 v8::Local<v8::Context> context, 199 v8::Local<v8::String> specifier, 200 v8::Local<v8::FixedArray> import_attributes, 201 v8::Local<v8::Module> referrer); 202 203 v8::Global<v8::Module> module_; 204 ResolveCache resolve_cache_; 205 contextify::ContextifyContext* contextify_context_ = nullptr; 206 bool synthetic_ = false; 207 bool linked_ = false; 208 int module_hash_; 209 }; 210 211 } // namespace loader 212 } // namespace node 213 214 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 215 216 #endif // SRC_MODULE_WRAP_H_