GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_native_module.h Lines: 1 1 100.0 %
Date: 2019-01-07 12:15:22 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 <set>
8
#include <string>
9
#include "env.h"
10
#include "node_mutex.h"
11
#include "node_union_bytes.h"
12
#include "v8.h"
13
14
namespace node {
15
namespace native_module {
16
17
using NativeModuleRecordMap = std::map<std::string, UnionBytes>;
18
using NativeModuleCacheMap =
19
    std::unordered_map<std::string,
20
                       std::unique_ptr<v8::ScriptCompiler::CachedData>>;
21
22
// The native (C++) side of the NativeModule in JS land, which
23
// handles compilation and caching of builtin modules (NativeModule)
24
// and bootstrappers, whose source are bundled into the binary
25
// as static data.
26
// This class should not depend on a particular isolate, context, or
27
// environment. Rather it should take them as arguments when necessary.
28
// The instances of this class are per-process.
29
3597
class NativeModuleLoader {
30
 public:
31
  NativeModuleLoader();
32
  // TODO(joyeecheung): maybe we should make this a singleton, instead of
33
  // putting it in per_process.
34
  NativeModuleLoader(const NativeModuleLoader&) = delete;
35
  NativeModuleLoader& operator=(const NativeModuleLoader&) = delete;
36
37
  static void Initialize(v8::Local<v8::Object> target,
38
                         v8::Local<v8::Value> unused,
39
                         v8::Local<v8::Context> context,
40
                         void* priv);
41
  v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context) const;
42
  // Returns config.gypi as a JSON string
43
  v8::Local<v8::String> GetConfigString(v8::Isolate* isolate) const;
44
45
  // Run a script with JS source bundled inside the binary as if it's wrapped
46
  // in a function called with a null receiver and arguments specified in C++.
47
  // The returned value is empty if an exception is encountered.
48
  // JS code run with this method can assume that their top-level
49
  // declarations won't affect the global scope.
50
  v8::MaybeLocal<v8::Value> CompileAndCall(
51
      v8::Local<v8::Context> context,
52
      const char* id,
53
      std::vector<v8::Local<v8::String>>* parameters,
54
      std::vector<v8::Local<v8::Value>>* arguments,
55
      Environment* optional_env);
56
57
 private:
58
  static void GetCacheUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
59
  // Passing map of builtin module source code into JS land as
60
  // internalBinding('native_module').source
61
  static void SourceObjectGetter(
62
      v8::Local<v8::Name> property,
63
      const v8::PropertyCallbackInfo<v8::Value>& info);
64
  // Passing config.gypi into JS land as internalBinding('native_module').config
65
  static void ConfigStringGetter(
66
      v8::Local<v8::Name> property,
67
      const v8::PropertyCallbackInfo<v8::Value>& info);
68
  // Get code cache for a specific native module
69
  static void GetCodeCache(const v8::FunctionCallbackInfo<v8::Value>& args);
70
  v8::MaybeLocal<v8::Uint8Array> GetCodeCache(v8::Isolate* isolate,
71
                                              const char* id) const;
72
  // Compile a specific native module as a function
73
  static void CompileFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
74
75
  // Generated by tools/js2c.py as node_javascript.cc
76
  void LoadJavaScriptSource();  // Loads data into source_
77
  UnionBytes GetConfig();       // Return data for config.gypi
78
79
  // Generated by tools/generate_code_cache.js as node_code_cache.cc when
80
  // the build is configured with --code-cache-path=.... They are noops
81
  // in node_code_cache_stub.cc
82
  void LoadCodeCache();      // Loads data into code_cache_
83
84
  // Compile a script as a NativeModule that can be loaded via
85
  // NativeModule.p.require in JS land.
86
  static v8::MaybeLocal<v8::Function> CompileAsModule(Environment* env,
87
                                                      const char* id);
88
89
  // For bootstrappers optional_env may be a nullptr.
90
  // If an exception is encountered (e.g. source code contains
91
  // syntax error), the returned value is empty.
92
  v8::MaybeLocal<v8::Function> LookupAndCompile(
93
      v8::Local<v8::Context> context,
94
      const char* id,
95
      std::vector<v8::Local<v8::String>>* parameters,
96
      Environment* optional_env);
97
98
  NativeModuleRecordMap source_;
99
  NativeModuleCacheMap code_cache_;
100
  UnionBytes config_;
101
102
  // Used to synchronize access to the code cache map
103
  Mutex code_cache_mutex_;
104
};
105
106
}  // namespace native_module
107
108
namespace per_process {
109
extern native_module::NativeModuleLoader native_module_loader;
110
}  // namespace per_process
111
112
}  // namespace node
113
114
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
115
116
#endif  // SRC_NODE_NATIVE_MODULE_H_