GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_native_module.h Lines: 1 1 100.0 %
Date: 2022-07-22 04:16:17 Branches: 0 0 - %

Line Branch Exec Source
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 <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
namespace native_module {
22
23
using NativeModuleRecordMap = std::map<std::string, UnionBytes>;
24
using NativeModuleCacheMap =
25
    std::unordered_map<std::string,
26
                       std::unique_ptr<v8::ScriptCompiler::CachedData>>;
27
28
struct CodeCacheInfo {
29
  std::string id;
30
  std::vector<uint8_t> data;
31
};
32
33
// The native (C++) side of the NativeModule in JS land, which
34
// handles compilation and caching of builtin modules (NativeModule)
35
// and bootstrappers, whose source are bundled into the binary
36
// as static data.
37
class NODE_EXTERN_PRIVATE NativeModuleLoader {
38
 public:
39
  NativeModuleLoader(const NativeModuleLoader&) = delete;
40
  NativeModuleLoader& operator=(const NativeModuleLoader&) = 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
  static v8::MaybeLocal<v8::Function> LookupAndCompile(
49
      v8::Local<v8::Context> context,
50
      const char* id,
51
      std::vector<v8::Local<v8::String>>* parameters,
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
60
  static bool CompileAllModules(v8::Local<v8::Context> context);
61
  static void RefreshCodeCache(const std::vector<CodeCacheInfo>& in);
62
  static void CopyCodeCache(std::vector<CodeCacheInfo>* out);
63
64
 private:
65
  // Only allow access from friends.
66
  friend class CodeCacheBuilder;
67
68
  NativeModuleLoader();
69
  static NativeModuleLoader* GetInstance();
70
71
  // Generated by tools/js2c.py as node_javascript.cc
72
  void LoadJavaScriptSource();  // Loads data into source_
73
  UnionBytes GetConfig();       // Return data for config.gypi
74
75
  std::vector<std::string> GetModuleIds();
76
77
  struct ModuleCategories {
78
    bool is_initialized = false;
79
    std::set<std::string> can_be_required;
80
    std::set<std::string> cannot_be_required;
81
  };
82
  void InitializeModuleCategories();
83
  const std::set<std::string>& GetCannotBeRequired();
84
  const std::set<std::string>& GetCanBeRequired();
85
86
  bool CanBeRequired(const char* id);
87
  bool CannotBeRequired(const char* id);
88
89
  NativeModuleCacheMap* code_cache();
90
5267
  const Mutex& code_cache_mutex() const { return code_cache_mutex_; }
91
92
  v8::ScriptCompiler::CachedData* GetCodeCache(const char* id) const;
93
  enum class Result { kWithCache, kWithoutCache };
94
  v8::MaybeLocal<v8::String> LoadBuiltinModuleSource(v8::Isolate* isolate,
95
                                                     const char* id);
96
  // If an exception is encountered (e.g. source code contains
97
  // syntax error), the returned value is empty.
98
  v8::MaybeLocal<v8::Function> LookupAndCompileInternal(
99
      v8::Local<v8::Context> context,
100
      const char* id,
101
      std::vector<v8::Local<v8::String>>* parameters,
102
      Result* result);
103
  v8::MaybeLocal<v8::Function> CompileAsModule(v8::Local<v8::Context> context,
104
                                               const char* id,
105
                                               Result* result);
106
107
  static void RecordResult(const char* id,
108
                           NativeModuleLoader::Result result,
109
                           Environment* env);
110
  static void GetModuleCategories(
111
      v8::Local<v8::Name> property,
112
      const v8::PropertyCallbackInfo<v8::Value>& info);
113
  static void GetCacheUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
114
  // Passing ids of builtin module source code into JS land as
115
  // internalBinding('native_module').moduleIds
116
  static void ModuleIdsGetter(v8::Local<v8::Name> property,
117
                              const v8::PropertyCallbackInfo<v8::Value>& info);
118
  // Passing config.gypi into JS land as internalBinding('native_module').config
119
  static void ConfigStringGetter(
120
      v8::Local<v8::Name> property,
121
      const v8::PropertyCallbackInfo<v8::Value>& info);
122
  // Compile a specific native module as a function
123
  static void CompileFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
124
  static void HasCachedBuiltins(
125
      const v8::FunctionCallbackInfo<v8::Value>& args);
126
127
  static NativeModuleLoader instance_;
128
  ModuleCategories module_categories_;
129
  NativeModuleRecordMap source_;
130
  NativeModuleCacheMap 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 native_module
141
142
}  // namespace node
143
144
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
145
146
#endif  // SRC_NODE_NATIVE_MODULE_H_