• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_NODE_BUILTINS_H_
2 #define SRC_NODE_BUILTINS_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include <list>
7 #include <map>
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <vector>
12 #include "node_mutex.h"
13 #include "node_union_bytes.h"
14 #include "v8.h"
15 
16 // Forward declare test fixture for `friend` declaration.
17 class PerProcessTest;
18 
19 namespace node {
20 class SnapshotBuilder;
21 class ExternalReferenceRegistry;
22 namespace builtins {
23 
24 using BuiltinSourceMap = std::map<std::string, UnionBytes>;
25 using BuiltinCodeCacheMap =
26     std::unordered_map<std::string,
27                        std::unique_ptr<v8::ScriptCompiler::CachedData>>;
28 
29 struct CodeCacheInfo {
30   std::string id;
31   std::vector<uint8_t> data;
32 };
33 
34 // Handles compilation and caching of built-in JavaScript modules and
35 // bootstrap scripts, whose source are bundled into the binary as static data.
36 class NODE_EXTERN_PRIVATE BuiltinLoader {
37  public:
38   BuiltinLoader(const BuiltinLoader&) = delete;
39   BuiltinLoader& operator=(const BuiltinLoader&) = delete;
40 
41   static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
42   static void Initialize(v8::Local<v8::Object> target,
43                          v8::Local<v8::Value> unused,
44                          v8::Local<v8::Context> context,
45                          void* priv);
46 
47   // The parameters used to compile the scripts are detected based on
48   // the pattern of the id.
49   static v8::MaybeLocal<v8::Function> LookupAndCompile(
50       v8::Local<v8::Context> context,
51       const char* id,
52       Environment* optional_env);
53 
54   static v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context);
55   // Returns config.gypi as a JSON string
56   static v8::Local<v8::String> GetConfigString(v8::Isolate* isolate);
57   static bool Exists(const char* id);
58   static bool Add(const char* id, const UnionBytes& source);
59   static bool Add(const char* id, std::string_view utf8source);
60 
61   static bool CompileAllBuiltins(v8::Local<v8::Context> context);
62   static void RefreshCodeCache(const std::vector<CodeCacheInfo>& in);
63   static void CopyCodeCache(std::vector<CodeCacheInfo>* out);
64 
65  private:
66   // Only allow access from friends.
67   friend class CodeCacheBuilder;
68 
69   BuiltinLoader();
70   static BuiltinLoader* GetInstance();
71 
72   // Generated by tools/js2c.py as node_javascript.cc
73   void LoadJavaScriptSource();  // Loads data into source_
74   UnionBytes GetConfig();       // Return data for config.gypi
75 
76   std::vector<std::string> GetBuiltinIds();
77 
78   struct BuiltinCategories {
79     bool is_initialized = false;
80     std::set<std::string> can_be_required;
81     std::set<std::string> cannot_be_required;
82   };
83   void InitializeBuiltinCategories();
84   const std::set<std::string>& GetCannotBeRequired();
85   const std::set<std::string>& GetCanBeRequired();
86 
87   bool CanBeRequired(const char* id);
88   bool CannotBeRequired(const char* id);
89 
90   BuiltinCodeCacheMap* code_cache();
code_cache_mutex()91   const Mutex& code_cache_mutex() const { return code_cache_mutex_; }
92 
93   v8::ScriptCompiler::CachedData* GetCodeCache(const char* id) const;
94   enum class Result { kWithCache, kWithoutCache };
95   v8::MaybeLocal<v8::String> LoadBuiltinSource(v8::Isolate* isolate,
96                                                const char* id);
97   // If an exception is encountered (e.g. source code contains
98   // syntax error), the returned value is empty.
99   v8::MaybeLocal<v8::Function> LookupAndCompileInternal(
100       v8::Local<v8::Context> context,
101       const char* id,
102       std::vector<v8::Local<v8::String>>* parameters,
103       Result* result);
104 
105   static void RecordResult(const char* id,
106                            BuiltinLoader::Result result,
107                            Environment* env);
108   static void GetBuiltinCategories(
109       v8::Local<v8::Name> property,
110       const v8::PropertyCallbackInfo<v8::Value>& info);
111   static void GetCacheUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
112   // Passing ids of built-in source code into JS land as
113   // internalBinding('builtins').builtinIds
114   static void BuiltinIdsGetter(v8::Local<v8::Name> property,
115                                const v8::PropertyCallbackInfo<v8::Value>& info);
116   // Passing config.gypi into JS land as internalBinding('builtins').config
117   static void ConfigStringGetter(
118       v8::Local<v8::Name> property,
119       const v8::PropertyCallbackInfo<v8::Value>& info);
120   // Compile a specific built-in as a function
121   static void CompileFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
122   static void HasCachedBuiltins(
123       const v8::FunctionCallbackInfo<v8::Value>& args);
124 
125   static void AddExternalizedBuiltin(const char* id, const char* filename);
126 
127   static BuiltinLoader instance_;
128   BuiltinCategories builtin_categories_;
129   BuiltinSourceMap source_;
130   BuiltinCodeCacheMap code_cache_;
131   UnionBytes config_;
132 
133   // Used to synchronize access to the code cache map
134   Mutex code_cache_mutex_;
135 
136   bool has_code_cache_;
137 
138   friend class ::PerProcessTest;
139 };
140 }  // namespace builtins
141 
142 }  // namespace node
143 
144 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
145 
146 #endif  // SRC_NODE_BUILTINS_H_
147