GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_native_module.h Lines: 1 1 100.0 %
Date: 2022-08-06 04:16:36 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
  // 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::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context);
56
  // Returns config.gypi as a JSON string
57
  static v8::Local<v8::String> GetConfigString(v8::Isolate* isolate);
58
  static bool Exists(const char* id);
59
  static bool Add(const char* id, const UnionBytes& source);
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
  NativeModuleLoader();
70
  static NativeModuleLoader* 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> GetModuleIds();
77
78
  struct ModuleCategories {
79
    bool is_initialized = false;
80
    std::set<std::string> can_be_required;
81
    std::set<std::string> cannot_be_required;
82
  };
83
  void InitializeModuleCategories();
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
  NativeModuleCacheMap* code_cache();
91
5319
  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> LoadBuiltinModuleSource(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
                           NativeModuleLoader::Result result,
107
                           Environment* env);
108
  static void GetModuleCategories(
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 builtin module source code into JS land as
113
  // internalBinding('native_module').moduleIds
114
  static void ModuleIdsGetter(v8::Local<v8::Name> property,
115
                              const v8::PropertyCallbackInfo<v8::Value>& info);
116
  // Passing config.gypi into JS land as internalBinding('native_module').config
117
  static void ConfigStringGetter(
118
      v8::Local<v8::Name> property,
119
      const v8::PropertyCallbackInfo<v8::Value>& info);
120
  // Compile a specific native module 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 NativeModuleLoader instance_;
126
  ModuleCategories module_categories_;
127
  NativeModuleRecordMap source_;
128
  NativeModuleCacheMap code_cache_;
129
  UnionBytes config_;
130
131
  // Used to synchronize access to the code cache map
132
  Mutex code_cache_mutex_;
133
134
  bool has_code_cache_;
135
136
  friend class ::PerProcessTest;
137
};
138
}  // namespace native_module
139
140
}  // namespace node
141
142
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
143
144
#endif  // SRC_NODE_NATIVE_MODULE_H_