Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/compile_cache.h
1 #ifndef SRC_COMPILE_CACHE_H_ 2 #define SRC_COMPILE_CACHE_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include <cinttypes> 7 #include <memory> 8 #include <string> 9 #include <string_view> 10 #include <unordered_map> 11 #include "v8.h" 12 13 namespace node { 14 class Environment; 15 16 // TODO(joyeecheung): move it into a CacheHandler class. 17 enum class CachedCodeType : uint8_t { 18 kCommonJS = 0, 19 kESM, 20 }; 21 22 struct CompileCacheEntry { 23 std::unique_ptr<v8::ScriptCompiler::CachedData> cache{nullptr}; 24 uint32_t cache_key; 25 uint32_t code_hash; 26 uint32_t code_size; 27 28 std::string cache_filename; 29 std::string source_filename; 30 CachedCodeType type; 31 bool refreshed = false; 32 bool persisted = false; 33 34 // Copy the cache into a new store for V8 to consume. Caller takes 35 // ownership. 36 v8::ScriptCompiler::CachedData* CopyCache() const; 37 }; 38 39 #define COMPILE_CACHE_STATUS(V) \ 40 V(FAILED) /* Failed to enable the cache */ \ 41 V(ENABLED) /* Was not enabled before, and now enabled. */ \ 42 V(ALREADY_ENABLED) /* Was already enabled. */ \ 43 V(DISABLED) /* Has been disabled by NODE_DISABLE_COMPILE_CACHE. */ 44 45 enum class CompileCacheEnableStatus : uint8_t { 46 #define V(status) status, 47 COMPILE_CACHE_STATUS(V) 48 #undef V 49 }; 50 51 struct CompileCacheEnableResult { 52 CompileCacheEnableStatus status; 53 std::string cache_directory; 54 std::string message; // Set in case of failure. 55 }; 56 57 class CompileCacheHandler { 58 public: 59 explicit CompileCacheHandler(Environment* env); 60 CompileCacheEnableResult Enable(Environment* env, const std::string& dir); 61 62 void Persist(); 63 64 CompileCacheEntry* GetOrInsert(v8::Local<v8::String> code, 65 v8::Local<v8::String> filename, 66 CachedCodeType type); 67 void MaybeSave(CompileCacheEntry* entry, 68 v8::Local<v8::Function> func, 69 bool rejected); 70 void MaybeSave(CompileCacheEntry* entry, 71 v8::Local<v8::Module> mod, 72 bool rejected); 73 std::string_view cache_dir() { return compile_cache_dir_; } 74 75 private: 76 void ReadCacheFile(CompileCacheEntry* entry); 77 78 template <typename T> 79 void MaybeSaveImpl(CompileCacheEntry* entry, 80 v8::Local<T> func_or_mod, 81 bool rejected); 82 83 template <typename... Args> 84 inline void Debug(const char* format, Args&&... args) const; 85 86 static constexpr size_t kMagicNumberOffset = 0; 87 static constexpr size_t kCodeSizeOffset = 1; 88 static constexpr size_t kCacheSizeOffset = 2; 89 static constexpr size_t kCodeHashOffset = 3; 90 static constexpr size_t kCacheHashOffset = 4; 91 static constexpr size_t kHeaderCount = 5; 92 93 v8::Isolate* isolate_ = nullptr; 94 bool is_debug_ = false; 95 96 std::string compile_cache_dir_; 97 std::unordered_map<uint32_t, std::unique_ptr<CompileCacheEntry>> 98 compiler_cache_store_; 99 }; 100 } // namespace node 101 102 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 103 104 #endif // SRC_COMPILE_CACHE_H_