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