GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_native_module.cc Lines: 98 104 94.2 %
Date: 2021-02-11 04:11:15 Branches: 42 50 84.0 %

Line Branch Exec Source
1
#include "node_native_module.h"
2
#include "util-inl.h"
3
4
namespace node {
5
namespace native_module {
6
7
using v8::Context;
8
using v8::EscapableHandleScope;
9
using v8::Function;
10
using v8::Integer;
11
using v8::Isolate;
12
using v8::Local;
13
using v8::MaybeLocal;
14
using v8::Object;
15
using v8::ScriptCompiler;
16
using v8::ScriptOrigin;
17
using v8::String;
18
19
4699
NativeModuleLoader NativeModuleLoader::instance_;
20
21
4699
NativeModuleLoader::NativeModuleLoader() : config_(GetConfig()) {
22
4699
  LoadJavaScriptSource();
23
4699
}
24
25
307797
NativeModuleLoader* NativeModuleLoader::GetInstance() {
26
307797
  return &instance_;
27
}
28
29
15
bool NativeModuleLoader::Exists(const char* id) {
30
15
  return source_.find(id) != source_.end();
31
}
32
33
15
bool NativeModuleLoader::Add(const char* id, const UnionBytes& source) {
34
15
  if (Exists(id)) {
35
    return false;
36
  }
37
15
  source_.emplace(id, source);
38
15
  return true;
39
}
40
41
6
Local<Object> NativeModuleLoader::GetSourceObject(Local<Context> context) {
42
6
  Isolate* isolate = context->GetIsolate();
43
6
  Local<Object> out = Object::New(isolate);
44
1506
  for (auto const& x : source_) {
45
1500
    Local<String> key = OneByteString(isolate, x.first.c_str(), x.first.size());
46
4500
    out->Set(context, key, x.second.ToStringChecked(isolate)).FromJust();
47
  }
48
6
  return out;
49
}
50
51
450
Local<String> NativeModuleLoader::GetConfigString(Isolate* isolate) {
52
450
  return config_.ToStringChecked(isolate);
53
}
54
55
452
std::vector<std::string> NativeModuleLoader::GetModuleIds() {
56
452
  std::vector<std::string> ids;
57
452
  ids.reserve(source_.size());
58
113549
  for (auto const& x : source_) {
59
113092
    ids.emplace_back(x.first);
60
  }
61
452
  return ids;
62
}
63
64
2002
void NativeModuleLoader::InitializeModuleCategories() {
65
2002
  if (module_categories_.is_initialized) {
66
    DCHECK(!module_categories_.can_be_required.empty());
67
1993
    return;
68
  }
69
70
  std::vector<std::string> prefixes = {
71
#if !HAVE_OPENSSL
72
    "internal/crypto/",
73
#endif  // !HAVE_OPENSSL
74
75
    "internal/bootstrap/",
76
    "internal/per_context/",
77
    "internal/deps/",
78
    "internal/main/"
79
18
  };
80
81
  module_categories_.can_be_required.emplace(
82
9
      "internal/deps/cjs-module-lexer/lexer");
83
84
63
  module_categories_.cannot_be_required = std::set<std::string> {
85
#if !HAVE_INSPECTOR
86
      "inspector",
87
      "internal/util/inspector",
88
#endif  // !HAVE_INSPECTOR
89
90
#if !NODE_USE_V8_PLATFORM || !defined(NODE_HAVE_I18N_SUPPORT)
91
      "trace_events",
92
#endif  // !NODE_USE_V8_PLATFORM
93
94
#if !HAVE_OPENSSL
95
      "crypto",
96
      "crypto/promises",
97
      "https",
98
      "http2",
99
      "tls",
100
      "_tls_common",
101
      "_tls_wrap",
102
      "internal/http2/core",
103
      "internal/http2/compat",
104
      "internal/policy/manifest",
105
      "internal/process/policy",
106
      "internal/streams/lazy_transform",
107
#endif  // !HAVE_OPENSSL
108
      "sys",  // Deprecated.
109
      "wasi",  // Experimental.
110
      "internal/test/binding",
111
      "internal/v8_prof_polyfill",
112
      "internal/v8_prof_processor",
113
54
  };
114
115
2259
  for (auto const& x : source_) {
116
2250
    const std::string& id = x.first;
117
11250
    for (auto const& prefix : prefixes) {
118
9000
      if (prefix.length() > id.length()) {
119
3042
        continue;
120
      }
121

6336
      if (id.find(prefix) == 0 &&
122
378
          module_categories_.can_be_required.count(id) == 0) {
123
369
        module_categories_.cannot_be_required.emplace(id);
124
      }
125
    }
126
  }
127
128
2259
  for (auto const& x : source_) {
129
2250
    const std::string& id = x.first;
130
2250
    if (0 == module_categories_.cannot_be_required.count(id)) {
131
1836
      module_categories_.can_be_required.emplace(id);
132
    }
133
  }
134
135
9
  module_categories_.is_initialized = true;
136
}
137
138
1
const std::set<std::string>& NativeModuleLoader::GetCannotBeRequired() {
139
1
  InitializeModuleCategories();
140
1
  return module_categories_.cannot_be_required;
141
}
142
143
2001
const std::set<std::string>& NativeModuleLoader::GetCanBeRequired() {
144
2001
  InitializeModuleCategories();
145
2001
  return module_categories_.can_be_required;
146
}
147
148
2000
bool NativeModuleLoader::CanBeRequired(const char* id) {
149
2000
  return GetCanBeRequired().count(id) == 1;
150
}
151
152
bool NativeModuleLoader::CannotBeRequired(const char* id) {
153
  return GetCannotBeRequired().count(id) == 1;
154
}
155
156
4619
NativeModuleCacheMap* NativeModuleLoader::code_cache() {
157
4619
  return &code_cache_;
158
}
159
160
1632
ScriptCompiler::CachedData* NativeModuleLoader::GetCodeCache(
161
    const char* id) const {
162
3264
  Mutex::ScopedLock lock(code_cache_mutex_);
163
1632
  const auto it = code_cache_.find(id);
164
1632
  if (it == code_cache_.end()) {
165
    // The module has not been compiled before.
166
    return nullptr;
167
  }
168
1632
  return it->second.get();
169
}
170
171
294372
MaybeLocal<Function> NativeModuleLoader::CompileAsModule(
172
    Local<Context> context,
173
    const char* id,
174
    NativeModuleLoader::Result* result) {
175
294372
  Isolate* isolate = context->GetIsolate();
176
  std::vector<Local<String>> parameters = {
177
      FIXED_ONE_BYTE_STRING(isolate, "exports"),
178
      FIXED_ONE_BYTE_STRING(isolate, "require"),
179
      FIXED_ONE_BYTE_STRING(isolate, "module"),
180
      FIXED_ONE_BYTE_STRING(isolate, "process"),
181
      FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
182
588767
      FIXED_ONE_BYTE_STRING(isolate, "primordials")};
183
588775
  return LookupAndCompile(context, id, &parameters, result);
184
}
185
186
#ifdef NODE_BUILTIN_MODULES_PATH
187
static std::string OnDiskFileName(const char* id) {
188
  std::string filename = NODE_BUILTIN_MODULES_PATH;
189
  filename += "/";
190
191
  if (strncmp(id, "internal/deps", strlen("internal/deps")) == 0) {
192
    id += strlen("internal/");
193
  } else {
194
    filename += "lib/";
195
  }
196
  filename += id;
197
  filename += ".js";
198
199
  return filename;
200
}
201
#endif  // NODE_BUILTIN_MODULES_PATH
202
203
303898
MaybeLocal<String> NativeModuleLoader::LoadBuiltinModuleSource(Isolate* isolate,
204
                                                               const char* id) {
205
#ifdef NODE_BUILTIN_MODULES_PATH
206
  std::string filename = OnDiskFileName(id);
207
208
  uv_fs_t req;
209
  uv_file file =
210
      uv_fs_open(nullptr, &req, filename.c_str(), O_RDONLY, 0, nullptr);
211
  CHECK_GE(req.result, 0);
212
  uv_fs_req_cleanup(&req);
213
214
  auto defer_close = OnScopeLeave([file]() {
215
    uv_fs_t close_req;
216
    CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr));
217
    uv_fs_req_cleanup(&close_req);
218
  });
219
220
  std::string contents;
221
  char buffer[4096];
222
  uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer));
223
224
  while (true) {
225
    const int r =
226
        uv_fs_read(nullptr, &req, file, &buf, 1, contents.length(), nullptr);
227
    CHECK_GE(req.result, 0);
228
    uv_fs_req_cleanup(&req);
229
    if (r <= 0) {
230
      break;
231
    }
232
    contents.append(buf.base, r);
233
  }
234
235
  return String::NewFromUtf8(
236
      isolate, contents.c_str(), v8::NewStringType::kNormal, contents.length());
237
#else
238
303898
  const auto source_it = source_.find(id);
239
303902
  CHECK_NE(source_it, source_.end());
240
607791
  return source_it->second.ToStringChecked(isolate);
241
#endif  // NODE_BUILTIN_MODULES_PATH
242
}
243
244
// Returns Local<Function> of the compiled module if return_code_cache
245
// is false (we are only compiling the function).
246
// Otherwise return a Local<Object> containing the cache.
247
303893
MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
248
    Local<Context> context,
249
    const char* id,
250
    std::vector<Local<String>>* parameters,
251
    NativeModuleLoader::Result* result) {
252
303893
  Isolate* isolate = context->GetIsolate();
253
303896
  EscapableHandleScope scope(isolate);
254
255
  Local<String> source;
256
607798
  if (!LoadBuiltinModuleSource(isolate, id).ToLocal(&source)) {
257
    return {};
258
  }
259
260
607806
  std::string filename_s = std::string("node:") + id;
261
  Local<String> filename =
262
303897
      OneByteString(isolate, filename_s.c_str(), filename_s.size());
263
303893
  Local<Integer> line_offset = Integer::New(isolate, 0);
264
303888
  Local<Integer> column_offset = Integer::New(isolate, 0);
265
303904
  ScriptOrigin origin(filename, line_offset, column_offset, True(isolate));
266
267
303904
  ScriptCompiler::CachedData* cached_data = nullptr;
268
  {
269
    // Note: The lock here should not extend into the
270
    // `CompileFunctionInContext()` call below, because this function may
271
    // recurse if there is a syntax error during bootstrap (because the fatal
272
    // exception handler is invoked, which may load built-in modules).
273
607814
    Mutex::ScopedLock lock(code_cache_mutex_);
274
303910
    auto cache_it = code_cache_.find(id);
275
303910
    if (cache_it != code_cache_.end()) {
276
      // Transfer ownership to ScriptCompiler::Source later.
277
289356
      cached_data = cache_it->second.release();
278
289356
      code_cache_.erase(cache_it);
279
    }
280
  }
281
282
303909
  const bool has_cache = cached_data != nullptr;
283
  ScriptCompiler::CompileOptions options =
284
303909
      has_cache ? ScriptCompiler::kConsumeCodeCache
285
303909
                : ScriptCompiler::kEagerCompile;
286
  ScriptCompiler::Source script_source(source, origin, cached_data);
287
288
  MaybeLocal<Function> maybe_fun =
289
      ScriptCompiler::CompileFunctionInContext(context,
290
                                               &script_source,
291
                                               parameters->size(),
292
                                               parameters->data(),
293
                                               0,
294
                                               nullptr,
295
303909
                                               options);
296
297
  // This could fail when there are early errors in the native modules,
298
  // e.g. the syntax errors
299
  Local<Function> fun;
300
303907
  if (!maybe_fun.ToLocal(&fun)) {
301
    // In the case of early errors, v8 is already capable of
302
    // decorating the stack for us - note that we use CompileFunctionInContext
303
    // so there is no need to worry about wrappers.
304
    return MaybeLocal<Function>();
305
  }
306
307
  // XXX(joyeecheung): this bookkeeping is not exactly accurate because
308
  // it only starts after the Environment is created, so the per_context.js
309
  // will never be in any of these two sets, but the two sets are only for
310
  // testing anyway.
311
312
593260
  *result = (has_cache && !script_source.GetCachedData()->rejected)
313
586354
                ? Result::kWithCache
314
                : Result::kWithoutCache;
315
  // Generate new cache for next compilation
316
  std::unique_ptr<ScriptCompiler::CachedData> new_cached_data(
317
607817
      ScriptCompiler::CreateCodeCacheForFunction(fun));
318
303902
  CHECK_NOT_NULL(new_cached_data);
319
320
  {
321
607808
    Mutex::ScopedLock lock(code_cache_mutex_);
322
    // The old entry should've been erased by now so we can just emplace.
323
    // If another thread did the same thing in the meantime, that should not
324
    // be an issue.
325
303910
    code_cache_.emplace(id, std::move(new_cached_data));
326
  }
327
328
303910
  return scope.Escape(fun);
329
}
330
331
}  // namespace native_module
332

14097
}  // namespace node