1 #ifndef SRC_NODE_NATIVE_MODULE_H_ 2 #define SRC_NODE_NATIVE_MODULE_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include <map> 7 #include <memory> 8 #include <set> 9 #include <string> 10 #include "node_mutex.h" 11 #include "node_union_bytes.h" 12 #include "v8.h" 13 14 // Forward declare test fixture for `friend` declaration. 15 class PerProcessTest; 16 17 namespace node { 18 namespace native_module { 19 20 using NativeModuleRecordMap = std::map<std::string, UnionBytes>; 21 using NativeModuleCacheMap = 22 std::unordered_map<std::string, 23 std::unique_ptr<v8::ScriptCompiler::CachedData>>; 24 25 // The native (C++) side of the NativeModule in JS land, which 26 // handles compilation and caching of builtin modules (NativeModule) 27 // and bootstrappers, whose source are bundled into the binary 28 // as static data. 29 // This class should not depend on any Environment, or depend on access to 30 // the its own singleton - that should be encapsulated in NativeModuleEnv 31 // instead. 32 class NativeModuleLoader { 33 public: 34 NativeModuleLoader(const NativeModuleLoader&) = delete; 35 NativeModuleLoader& operator=(const NativeModuleLoader&) = delete; 36 37 private: 38 // Only allow access from friends. 39 friend class NativeModuleEnv; 40 friend class CodeCacheBuilder; 41 42 NativeModuleLoader(); 43 static NativeModuleLoader* GetInstance(); 44 45 // Generated by tools/js2c.py as node_javascript.cc 46 void LoadJavaScriptSource(); // Loads data into source_ 47 UnionBytes GetConfig(); // Return data for config.gypi 48 49 bool Exists(const char* id); 50 bool Add(const char* id, const UnionBytes& source); 51 52 v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context); 53 v8::Local<v8::String> GetConfigString(v8::Isolate* isolate); 54 std::vector<std::string> GetModuleIds(); 55 56 struct ModuleCategories { 57 bool is_initialized = false; 58 std::set<std::string> can_be_required; 59 std::set<std::string> cannot_be_required; 60 }; 61 void InitializeModuleCategories(); 62 const std::set<std::string>& GetCannotBeRequired(); 63 const std::set<std::string>& GetCanBeRequired(); 64 65 bool CanBeRequired(const char* id); 66 bool CannotBeRequired(const char* id); 67 68 NativeModuleCacheMap* code_cache(); 69 v8::ScriptCompiler::CachedData* GetCodeCache(const char* id) const; 70 enum class Result { kWithCache, kWithoutCache }; 71 v8::MaybeLocal<v8::String> LoadBuiltinModuleSource(v8::Isolate* isolate, 72 const char* id); 73 // If an exception is encountered (e.g. source code contains 74 // syntax error), the returned value is empty. 75 v8::MaybeLocal<v8::Function> LookupAndCompile( 76 v8::Local<v8::Context> context, 77 const char* id, 78 std::vector<v8::Local<v8::String>>* parameters, 79 Result* result); 80 v8::MaybeLocal<v8::Function> CompileAsModule(v8::Local<v8::Context> context, 81 const char* id, 82 Result* result); 83 84 static NativeModuleLoader instance_; 85 ModuleCategories module_categories_; 86 NativeModuleRecordMap source_; 87 NativeModuleCacheMap code_cache_; 88 UnionBytes config_; 89 90 // Used to synchronize access to the code cache map 91 Mutex code_cache_mutex_; 92 93 friend class ::PerProcessTest; 94 }; 95 } // namespace native_module 96 97 } // namespace node 98 99 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 100 101 #endif // SRC_NODE_NATIVE_MODULE_H_ 102