GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_native_module.h Lines: 1 1 100.0 %
Date: 2022-06-13 04:15:40 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 "node_mutex.h"
11
#include "node_union_bytes.h"
12
#include "v8.h"
13
14
// Forward declare test fixture for `friend` declaration.
15
class PerProcessTest;
16
17
namespace node {
18
class SnapshotBuilder;
19
namespace native_module {
20
21
using NativeModuleRecordMap = std::map<std::string, UnionBytes>;
22
using NativeModuleCacheMap =
23
    std::unordered_map<std::string,
24
                       std::unique_ptr<v8::ScriptCompiler::CachedData>>;
25
26
struct CodeCacheInfo {
27
  std::string id;
28
  std::vector<uint8_t> data;
29
};
30
31
// The native (C++) side of the NativeModule in JS land, which
32
// handles compilation and caching of builtin modules (NativeModule)
33
// and bootstrappers, whose source are bundled into the binary
34
// as static data.
35
// This class should not depend on any Environment, or depend on access to
36
// the its own singleton - that should be encapsulated in NativeModuleEnv
37
// instead.
38
class NODE_EXTERN_PRIVATE NativeModuleLoader {
39
 public:
40
  NativeModuleLoader(const NativeModuleLoader&) = delete;
41
  NativeModuleLoader& operator=(const NativeModuleLoader&) = delete;
42
43
 private:
44
  // Only allow access from friends.
45
  friend class NativeModuleEnv;
46
  friend class CodeCacheBuilder;
47
48
  NativeModuleLoader();
49
  static NativeModuleLoader* GetInstance();
50
51
  // Generated by tools/js2c.py as node_javascript.cc
52
  void LoadJavaScriptSource();  // Loads data into source_
53
  UnionBytes GetConfig();       // Return data for config.gypi
54
55
  bool Exists(const char* id);
56
  bool Add(const char* id, const UnionBytes& source);
57
58
  v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context);
59
  v8::Local<v8::String> GetConfigString(v8::Isolate* isolate);
60
  std::vector<std::string> GetModuleIds();
61
62
  struct ModuleCategories {
63
    bool is_initialized = false;
64
    std::set<std::string> can_be_required;
65
    std::set<std::string> cannot_be_required;
66
  };
67
  void InitializeModuleCategories();
68
  const std::set<std::string>& GetCannotBeRequired();
69
  const std::set<std::string>& GetCanBeRequired();
70
71
  bool CanBeRequired(const char* id);
72
  bool CannotBeRequired(const char* id);
73
74
  NativeModuleCacheMap* code_cache();
75
5205
  const Mutex& code_cache_mutex() const { return code_cache_mutex_; }
76
77
  v8::ScriptCompiler::CachedData* GetCodeCache(const char* id) const;
78
  enum class Result { kWithCache, kWithoutCache };
79
  v8::MaybeLocal<v8::String> LoadBuiltinModuleSource(v8::Isolate* isolate,
80
                                                     const char* id);
81
  // If an exception is encountered (e.g. source code contains
82
  // syntax error), the returned value is empty.
83
  v8::MaybeLocal<v8::Function> LookupAndCompile(
84
      v8::Local<v8::Context> context,
85
      const char* id,
86
      std::vector<v8::Local<v8::String>>* parameters,
87
      Result* result);
88
  v8::MaybeLocal<v8::Function> CompileAsModule(v8::Local<v8::Context> context,
89
                                               const char* id,
90
                                               Result* result);
91
92
  static NativeModuleLoader instance_;
93
  ModuleCategories module_categories_;
94
  NativeModuleRecordMap source_;
95
  NativeModuleCacheMap code_cache_;
96
  UnionBytes config_;
97
98
  // Used to synchronize access to the code cache map
99
  Mutex code_cache_mutex_;
100
101
  friend class ::PerProcessTest;
102
};
103
}  // namespace native_module
104
105
}  // namespace node
106
107
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
108
109
#endif  // SRC_NODE_NATIVE_MODULE_H_