GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_builtins.h Lines: 1 1 100.0 %
Date: 2022-12-31 04:22:30 Branches: 0 0 - %

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