GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_builtins.cc Lines: 259 306 84.6 %
Date: 2022-10-14 05:16:24 Branches: 99 132 75.0 %

Line Branch Exec Source
1
#include "node_builtins.h"
2
#include "debug_utils-inl.h"
3
#include "env-inl.h"
4
#include "node_external_reference.h"
5
#include "node_internals.h"
6
#include "util-inl.h"
7
8
namespace node {
9
namespace builtins {
10
11
using v8::Context;
12
using v8::DEFAULT;
13
using v8::EscapableHandleScope;
14
using v8::Function;
15
using v8::FunctionCallbackInfo;
16
using v8::IntegrityLevel;
17
using v8::Isolate;
18
using v8::Local;
19
using v8::MaybeLocal;
20
using v8::Name;
21
using v8::None;
22
using v8::Object;
23
using v8::PropertyCallbackInfo;
24
using v8::ScriptCompiler;
25
using v8::ScriptOrigin;
26
using v8::Set;
27
using v8::SideEffectType;
28
using v8::String;
29
using v8::Undefined;
30
using v8::Value;
31
32
5622
BuiltinLoader BuiltinLoader::instance_;
33
34
5622
BuiltinLoader::BuiltinLoader() : config_(GetConfig()), has_code_cache_(false) {
35
5622
  LoadJavaScriptSource();
36
#if defined(NODE_HAVE_I18N_SUPPORT)
37
#ifdef NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH
38
  AddExternalizedBuiltin(
39
      "internal/deps/cjs-module-lexer/lexer",
40
      STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH));
41
#endif  // NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH
42
43
#ifdef NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH
44
  AddExternalizedBuiltin(
45
      "internal/deps/cjs-module-lexer/dist/lexer",
46
      STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH));
47
#endif  // NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH
48
49
#ifdef NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH
50
  AddExternalizedBuiltin("internal/deps/undici/undici",
51
                         STRINGIFY(NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH));
52
#endif  // NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH
53
#endif  // NODE_HAVE_I18N_SUPPORT
54
5622
}
55
56
906203
BuiltinLoader* BuiltinLoader::GetInstance() {
57
906203
  return &instance_;
58
}
59
60
bool BuiltinLoader::Exists(const char* id) {
61
  auto& source = GetInstance()->source_;
62
  return source.find(id) != source.end();
63
}
64
65
19
bool BuiltinLoader::Add(const char* id, const UnionBytes& source) {
66
19
  auto result = GetInstance()->source_.emplace(id, source);
67
19
  return result.second;
68
}
69
70
42
Local<Object> BuiltinLoader::GetSourceObject(Local<Context> context) {
71
42
  Isolate* isolate = context->GetIsolate();
72
42
  Local<Object> out = Object::New(isolate);
73
42
  auto& source = GetInstance()->source_;
74
12978
  for (auto const& x : source) {
75
12936
    Local<String> key = OneByteString(isolate, x.first.c_str(), x.first.size());
76
38808
    out->Set(context, key, x.second.ToStringChecked(isolate)).FromJust();
77
  }
78
42
  return out;
79
}
80
81
828
Local<String> BuiltinLoader::GetConfigString(Isolate* isolate) {
82
828
  return GetInstance()->config_.ToStringChecked(isolate);
83
}
84
85
831
std::vector<std::string> BuiltinLoader::GetBuiltinIds() {
86
831
  std::vector<std::string> ids;
87
831
  ids.reserve(source_.size());
88
256962
  for (auto const& x : source_) {
89
256131
    ids.emplace_back(x.first);
90
  }
91
831
  return ids;
92
}
93
94
2
void BuiltinLoader::InitializeBuiltinCategories() {
95
2
  if (builtin_categories_.is_initialized) {
96
    DCHECK(!builtin_categories_.can_be_required.empty());
97
1
    return;
98
  }
99
100
  std::vector<std::string> prefixes = {
101
#if !HAVE_OPENSSL
102
    "internal/crypto/",
103
    "internal/debugger/",
104
#endif  // !HAVE_OPENSSL
105
106
    "internal/bootstrap/",
107
    "internal/per_context/",
108
    "internal/deps/",
109
    "internal/main/"
110
2
  };
111
112
1
  builtin_categories_.can_be_required.emplace(
113
1
      "internal/deps/cjs-module-lexer/lexer");
114
115
7
  builtin_categories_.cannot_be_required = std::set<std::string> {
116
#if !HAVE_INSPECTOR
117
    "inspector", "inspector/promises", "internal/util/inspector",
118
#endif  // !HAVE_INSPECTOR
119
120
#if !NODE_USE_V8_PLATFORM || !defined(NODE_HAVE_I18N_SUPPORT)
121
        "trace_events",
122
#endif  // !NODE_USE_V8_PLATFORM || !defined(NODE_HAVE_I18N_SUPPORT)
123
124
#if !HAVE_OPENSSL
125
        "crypto", "crypto/promises", "https", "http2", "tls", "_tls_common",
126
        "_tls_wrap", "internal/tls/secure-pair",
127
        "internal/tls/parse-cert-string", "internal/tls/secure-context",
128
        "internal/http2/core", "internal/http2/compat",
129
        "internal/policy/manifest", "internal/process/policy",
130
        "internal/streams/lazy_transform",
131
#endif           // !HAVE_OPENSSL
132
        "sys",   // Deprecated.
133
        "wasi",  // Experimental.
134
        "internal/test/binding", "internal/v8_prof_polyfill",
135
        "internal/v8_prof_processor",
136
6
  };
137
138
309
  for (auto const& x : source_) {
139
308
    const std::string& id = x.first;
140
1540
    for (auto const& prefix : prefixes) {
141
1232
      if (prefix.length() > id.length()) {
142
352
        continue;
143
      }
144

919
      if (id.find(prefix) == 0 &&
145
39
          builtin_categories_.can_be_required.count(id) == 0) {
146
38
        builtin_categories_.cannot_be_required.emplace(id);
147
      }
148
    }
149
  }
150
151
309
  for (auto const& x : source_) {
152
308
    const std::string& id = x.first;
153
308
    if (0 == builtin_categories_.cannot_be_required.count(id)) {
154
265
      builtin_categories_.can_be_required.emplace(id);
155
    }
156
  }
157
158
1
  builtin_categories_.is_initialized = true;
159
}
160
161
1
const std::set<std::string>& BuiltinLoader::GetCannotBeRequired() {
162
1
  InitializeBuiltinCategories();
163
1
  return builtin_categories_.cannot_be_required;
164
}
165
166
1
const std::set<std::string>& BuiltinLoader::GetCanBeRequired() {
167
1
  InitializeBuiltinCategories();
168
1
  return builtin_categories_.can_be_required;
169
}
170
171
bool BuiltinLoader::CanBeRequired(const char* id) {
172
  return GetCanBeRequired().count(id) == 1;
173
}
174
175
bool BuiltinLoader::CannotBeRequired(const char* id) {
176
  return GetCannotBeRequired().count(id) == 1;
177
}
178
179
5550
BuiltinCodeCacheMap* BuiltinLoader::code_cache() {
180
5550
  return &code_cache_;
181
}
182
183
ScriptCompiler::CachedData* BuiltinLoader::GetCodeCache(const char* id) const {
184
  Mutex::ScopedLock lock(code_cache_mutex_);
185
  const auto it = code_cache_.find(id);
186
  if (it == code_cache_.end()) {
187
    // The module has not been compiled before.
188
    return nullptr;
189
  }
190
  return it->second.get();
191
}
192
193
#ifdef NODE_BUILTIN_MODULES_PATH
194
static std::string OnDiskFileName(const char* id) {
195
  std::string filename = NODE_BUILTIN_MODULES_PATH;
196
  filename += "/";
197
198
  if (strncmp(id, "internal/deps", strlen("internal/deps")) == 0) {
199
    id += strlen("internal/");
200
  } else {
201
    filename += "lib/";
202
  }
203
  filename += id;
204
  filename += ".js";
205
206
  return filename;
207
}
208
#endif  // NODE_BUILTIN_MODULES_PATH
209
210
455688
MaybeLocal<String> BuiltinLoader::LoadBuiltinSource(Isolate* isolate,
211
                                                    const char* id) {
212
#ifdef NODE_BUILTIN_MODULES_PATH
213
  if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
214
#endif  // NODE_BUILTIN_MODULES_PATH
215
455688
    const auto source_it = source_.find(id);
216
455688
    if (UNLIKELY(source_it == source_.end())) {
217
      fprintf(stderr, "Cannot find native builtin: \"%s\".\n", id);
218
      ABORT();
219
    }
220
911376
    return source_it->second.ToStringChecked(isolate);
221
#ifdef NODE_BUILTIN_MODULES_PATH
222
  }
223
  std::string filename = OnDiskFileName(id);
224
225
  std::string contents;
226
  int r = ReadFileSync(&contents, filename.c_str());
227
  if (r != 0) {
228
    const std::string buf = SPrintF("Cannot read local builtin. %s: %s \"%s\"",
229
                                    uv_err_name(r),
230
                                    uv_strerror(r),
231
                                    filename);
232
    Local<String> message = OneByteString(isolate, buf.c_str());
233
    isolate->ThrowException(v8::Exception::Error(message));
234
    return MaybeLocal<String>();
235
  }
236
  return String::NewFromUtf8(
237
      isolate, contents.c_str(), v8::NewStringType::kNormal, contents.length());
238
#endif  // NODE_BUILTIN_MODULES_PATH
239
}
240
241
#if defined(NODE_HAVE_I18N_SUPPORT)
242
void BuiltinLoader::AddExternalizedBuiltin(const char* id,
243
                                           const char* filename) {
244
  std::string source;
245
  int r = ReadFileSync(&source, filename);
246
  if (r != 0) {
247
    fprintf(
248
        stderr, "Cannot load externalized builtin: \"%s:%s\".\n", id, filename);
249
    ABORT();
250
    return;
251
  }
252
253
  icu::UnicodeString utf16 = icu::UnicodeString::fromUTF8(
254
      icu::StringPiece(source.data(), source.length()));
255
  auto source_utf16 = std::make_unique<icu::UnicodeString>(utf16);
256
  Add(id,
257
      UnionBytes(reinterpret_cast<const uint16_t*>((*source_utf16).getBuffer()),
258
                 utf16.length()));
259
  // keep source bytes for builtin alive while BuiltinLoader exists
260
  GetInstance()->externalized_source_bytes_.push_back(std::move(source_utf16));
261
}
262
#endif  // NODE_HAVE_I18N_SUPPORT
263
264
// Returns Local<Function> of the compiled module if return_code_cache
265
// is false (we are only compiling the function).
266
// Otherwise return a Local<Object> containing the cache.
267
455688
MaybeLocal<Function> BuiltinLoader::LookupAndCompileInternal(
268
    Local<Context> context,
269
    const char* id,
270
    std::vector<Local<String>>* parameters,
271
    BuiltinLoader::Result* result) {
272
455688
  Isolate* isolate = context->GetIsolate();
273
455688
  EscapableHandleScope scope(isolate);
274
275
  Local<String> source;
276
911376
  if (!LoadBuiltinSource(isolate, id).ToLocal(&source)) {
277
    return {};
278
  }
279
280
911376
  std::string filename_s = std::string("node:") + id;
281
  Local<String> filename =
282
455688
      OneByteString(isolate, filename_s.c_str(), filename_s.size());
283
455688
  ScriptOrigin origin(isolate, filename, 0, 0, true);
284
285
455688
  ScriptCompiler::CachedData* cached_data = nullptr;
286
  {
287
    // Note: The lock here should not extend into the
288
    // `CompileFunction()` call below, because this function may recurse if
289
    // there is a syntax error during bootstrap (because the fatal exception
290
    // handler is invoked, which may load built-in modules).
291
911376
    Mutex::ScopedLock lock(code_cache_mutex_);
292
455688
    auto cache_it = code_cache_.find(id);
293
455688
    if (cache_it != code_cache_.end()) {
294
      // Transfer ownership to ScriptCompiler::Source later.
295
450825
      cached_data = cache_it->second.release();
296
450825
      code_cache_.erase(cache_it);
297
    }
298
  }
299
300
455688
  const bool has_cache = cached_data != nullptr;
301
455688
  ScriptCompiler::CompileOptions options =
302
455688
      has_cache ? ScriptCompiler::kConsumeCodeCache
303
                : ScriptCompiler::kEagerCompile;
304
  ScriptCompiler::Source script_source(source, origin, cached_data);
305
306
  per_process::Debug(DebugCategory::CODE_CACHE,
307
                     "Compiling %s %s code cache\n",
308
                     id,
309
911376
                     has_cache ? "with" : "without");
310
311
  MaybeLocal<Function> maybe_fun =
312
      ScriptCompiler::CompileFunction(context,
313
                                      &script_source,
314
                                      parameters->size(),
315
                                      parameters->data(),
316
                                      0,
317
                                      nullptr,
318
455688
                                      options);
319
320
  // This could fail when there are early errors in the built-in modules,
321
  // e.g. the syntax errors
322
  Local<Function> fun;
323
455688
  if (!maybe_fun.ToLocal(&fun)) {
324
    // In the case of early errors, v8 is already capable of
325
    // decorating the stack for us - note that we use CompileFunction
326
    // so there is no need to worry about wrappers.
327
    return MaybeLocal<Function>();
328
  }
329
330
  // XXX(joyeecheung): this bookkeeping is not exactly accurate because
331
  // it only starts after the Environment is created, so the per_context.js
332
  // will never be in any of these two sets, but the two sets are only for
333
  // testing anyway.
334
335
906513
  *result = (has_cache && !script_source.GetCachedData()->rejected)
336
897993
                ? Result::kWithCache
337
                : Result::kWithoutCache;
338
339
455688
  if (has_cache) {
340
450825
    per_process::Debug(DebugCategory::CODE_CACHE,
341
                       "Code cache of %s (%s) %s\n",
342
                       id,
343
450825
                       script_source.GetCachedData()->buffer_policy ==
344
                               ScriptCompiler::CachedData::BufferNotOwned
345
901650
                           ? "BufferNotOwned"
346
                           : "BufferOwned",
347
450825
                       script_source.GetCachedData()->rejected ? "is rejected"
348
                                                               : "is accepted");
349
  }
350
351
  // Generate new cache for next compilation
352
  std::unique_ptr<ScriptCompiler::CachedData> new_cached_data(
353
911376
      ScriptCompiler::CreateCodeCacheForFunction(fun));
354
455688
  CHECK_NOT_NULL(new_cached_data);
355
356
  {
357
911376
    Mutex::ScopedLock lock(code_cache_mutex_);
358
455688
    const auto it = code_cache_.find(id);
359
    // TODO(joyeecheung): it's safer for each thread to have its own
360
    // copy of the code cache map.
361
455688
    if (it == code_cache_.end()) {
362
454422
      code_cache_.emplace(id, std::move(new_cached_data));
363
    } else {
364
1266
      it->second.reset(new_cached_data.release());
365
    }
366
  }
367
368
455688
  return scope.Escape(fun);
369
}
370
371
// Returns Local<Function> of the compiled module if return_code_cache
372
// is false (we are only compiling the function).
373
// Otherwise return a Local<Object> containing the cache.
374
455688
MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
375
    Local<Context> context, const char* id, Environment* optional_env) {
376
  Result result;
377
911376
  std::vector<Local<String>> parameters;
378
455688
  Isolate* isolate = context->GetIsolate();
379
  // Detects parameters of the scripts based on module ids.
380
  // internal/bootstrap/loaders: process, getLinkedBinding,
381
  //                             getInternalBinding, primordials
382
455688
  if (strcmp(id, "internal/bootstrap/loaders") == 0) {
383
792
    parameters = {
384
        FIXED_ONE_BYTE_STRING(isolate, "process"),
385
        FIXED_ONE_BYTE_STRING(isolate, "getLinkedBinding"),
386
        FIXED_ONE_BYTE_STRING(isolate, "getInternalBinding"),
387
        FIXED_ONE_BYTE_STRING(isolate, "primordials"),
388
    };
389
454896
  } else if (strncmp(id,
390
                     "internal/per_context/",
391
                     strlen("internal/per_context/")) == 0) {
392
    // internal/per_context/*: global, exports, primordials
393
441
    parameters = {
394
        FIXED_ONE_BYTE_STRING(isolate, "exports"),
395
        FIXED_ONE_BYTE_STRING(isolate, "primordials"),
396
    };
397

902537
  } else if (strncmp(id, "internal/main/", strlen("internal/main/")) == 0 ||
398
448082
             strncmp(id,
399
                     "internal/bootstrap/",
400
                     strlen("internal/bootstrap/")) == 0) {
401
    // internal/main/*, internal/bootstrap/*: process, require,
402
    //                                        internalBinding, primordials
403
9553
    parameters = {
404
        FIXED_ONE_BYTE_STRING(isolate, "process"),
405
        FIXED_ONE_BYTE_STRING(isolate, "require"),
406
        FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
407
        FIXED_ONE_BYTE_STRING(isolate, "primordials"),
408
    };
409
444902
  } else if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
410
    // Synthetic embedder main scripts from LoadEnvironment(): process, require
411
19
    parameters = {
412
        FIXED_ONE_BYTE_STRING(isolate, "process"),
413
        FIXED_ONE_BYTE_STRING(isolate, "require"),
414
    };
415
  } else {
416
    // others: exports, require, module, process, internalBinding, primordials
417
444883
    parameters = {
418
        FIXED_ONE_BYTE_STRING(isolate, "exports"),
419
        FIXED_ONE_BYTE_STRING(isolate, "require"),
420
        FIXED_ONE_BYTE_STRING(isolate, "module"),
421
        FIXED_ONE_BYTE_STRING(isolate, "process"),
422
        FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
423
        FIXED_ONE_BYTE_STRING(isolate, "primordials"),
424
    };
425
  }
426
427
  MaybeLocal<Function> maybe = GetInstance()->LookupAndCompileInternal(
428
455688
      context, id, &parameters, &result);
429
455688
  if (optional_env != nullptr) {
430
453483
    RecordResult(id, result, optional_env);
431
  }
432
911376
  return maybe;
433
}
434
435
10244
MaybeLocal<Value> BuiltinLoader::CompileAndCall(Local<Context> context,
436
                                                const char* id,
437
                                                Realm* realm) {
438
10244
  Isolate* isolate = context->GetIsolate();
439
  // Arguments must match the parameters specified in
440
  // BuiltinLoader::LookupAndCompile().
441
20205
  std::vector<Local<Value>> arguments;
442
  // Detects parameters of the scripts based on module ids.
443
  // internal/bootstrap/loaders: process, getLinkedBinding,
444
  //                             getInternalBinding, primordials
445
10244
  if (strcmp(id, "internal/bootstrap/loaders") == 0) {
446
    Local<Value> get_linked_binding;
447
    Local<Value> get_internal_binding;
448
2358
    if (!NewFunctionTemplate(isolate, binding::GetLinkedBinding)
449
1572
             ->GetFunction(context)
450

2358
             .ToLocal(&get_linked_binding) ||
451
2358
        !NewFunctionTemplate(isolate, binding::GetInternalBinding)
452
1572
             ->GetFunction(context)
453
786
             .ToLocal(&get_internal_binding)) {
454
      return MaybeLocal<Value>();
455
    }
456
2358
    arguments = {realm->process_object(),
457
                 get_linked_binding,
458
                 get_internal_binding,
459
                 realm->primordials()};
460

12621
  } else if (strncmp(id, "internal/main/", strlen("internal/main/")) == 0 ||
461
3163
             strncmp(id,
462
                     "internal/bootstrap/",
463
                     strlen("internal/bootstrap/")) == 0) {
464
    // internal/main/*, internal/bootstrap/*: process, require,
465
    //                                        internalBinding, primordials
466
47195
    arguments = {realm->process_object(),
467
                 realm->builtin_module_require(),
468
                 realm->internal_binding_loader(),
469
                 realm->primordials()};
470
19
  } else if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
471
    // Synthetic embedder main scripts from LoadEnvironment(): process, require
472
57
    arguments = {
473
        realm->process_object(),
474
        realm->builtin_module_require(),
475
    };
476
  } else {
477
    // This should be invoked with the other CompileAndCall() methods, as
478
    // we are unable to generate the arguments.
479
    // Currently there are two cases:
480
    // internal/per_context/*: the arguments are generated in
481
    //                         InitializePrimordials()
482
    // all the other cases: the arguments are generated in the JS-land loader.
483
    UNREACHABLE();
484
  }
485
  return CompileAndCall(
486
10244
      context, id, arguments.size(), arguments.data(), realm->env());
487
}
488
489
10667
MaybeLocal<Value> BuiltinLoader::CompileAndCall(Local<Context> context,
490
                                                const char* id,
491
                                                int argc,
492
                                                Local<Value> argv[],
493
                                                Environment* optional_env) {
494
  // Arguments must match the parameters specified in
495
  // BuiltinLoader::LookupAndCompile().
496
10667
  MaybeLocal<Function> maybe_fn = LookupAndCompile(context, id, optional_env);
497
  Local<Function> fn;
498
10667
  if (!maybe_fn.ToLocal(&fn)) {
499
    return MaybeLocal<Value>();
500
  }
501
10667
  Local<Value> undefined = Undefined(context->GetIsolate());
502
10667
  return fn->Call(context, undefined, argc, argv);
503
}
504
505
6
bool BuiltinLoader::CompileAllBuiltins(Local<Context> context) {
506
6
  BuiltinLoader* loader = GetInstance();
507
12
  std::vector<std::string> ids = loader->GetBuiltinIds();
508
6
  bool all_succeeded = true;
509
12
  std::string v8_tools_prefix = "internal/deps/v8/tools/";
510
1854
  for (const auto& id : ids) {
511
1848
    if (id.compare(0, v8_tools_prefix.size(), v8_tools_prefix) == 0) {
512
66
      continue;
513
    }
514
3564
    v8::TryCatch bootstrapCatch(context->GetIsolate());
515
1782
    USE(loader->LookupAndCompile(context, id.c_str(), nullptr));
516
1782
    if (bootstrapCatch.HasCaught()) {
517
      per_process::Debug(DebugCategory::CODE_CACHE,
518
                         "Failed to compile code cache for %s\n",
519
                         id.c_str());
520
      all_succeeded = false;
521
      PrintCaughtException(context->GetIsolate(), context, bootstrapCatch);
522
    }
523
  }
524
12
  return all_succeeded;
525
}
526
527
6
void BuiltinLoader::CopyCodeCache(std::vector<CodeCacheInfo>* out) {
528
6
  BuiltinLoader* loader = GetInstance();
529
12
  Mutex::ScopedLock lock(loader->code_cache_mutex());
530
6
  auto in = loader->code_cache();
531
1788
  for (auto const& item : *in) {
532
5346
    out->push_back(
533
        {item.first,
534
3564
         {item.second->data, item.second->data + item.second->length}});
535
  }
536
6
}
537
538
5544
void BuiltinLoader::RefreshCodeCache(const std::vector<CodeCacheInfo>& in) {
539
5544
  BuiltinLoader* loader = GetInstance();
540
11088
  Mutex::ScopedLock lock(loader->code_cache_mutex());
541
5544
  auto out = loader->code_cache();
542
1652112
  for (auto const& item : in) {
543
1646568
    size_t length = item.data.size();
544
1646568
    uint8_t* buffer = new uint8_t[length];
545
1646568
    memcpy(buffer, item.data.data(), length);
546
    auto new_cache = std::make_unique<v8::ScriptCompiler::CachedData>(
547
3293136
        buffer, length, v8::ScriptCompiler::CachedData::BufferOwned);
548
1646568
    auto cache_it = out->find(item.id);
549
1646568
    if (cache_it != out->end()) {
550
      // Release the old cache and replace it with the new copy.
551
      cache_it->second.reset(new_cache.release());
552
    } else {
553
1646568
      out->emplace(item.id, new_cache.release());
554
    }
555
  }
556
5544
  loader->has_code_cache_ = true;
557
5544
}
558
559
1
void BuiltinLoader::GetBuiltinCategories(
560
    Local<Name> property, const PropertyCallbackInfo<Value>& info) {
561
1
  Environment* env = Environment::GetCurrent(info);
562
1
  Isolate* isolate = env->isolate();
563
1
  Local<Context> context = env->context();
564
1
  Local<Object> result = Object::New(isolate);
565
566
  // Copy from the per-process categories
567
  std::set<std::string> cannot_be_required =
568
2
      GetInstance()->GetCannotBeRequired();
569
2
  std::set<std::string> can_be_required = GetInstance()->GetCanBeRequired();
570
571
1
  if (!env->owns_process_state()) {
572
    can_be_required.erase("trace_events");
573
    cannot_be_required.insert("trace_events");
574
  }
575
576
  Local<Value> cannot_be_required_js;
577
  Local<Value> can_be_required_js;
578
579
2
  if (!ToV8Value(context, cannot_be_required).ToLocal(&cannot_be_required_js))
580
    return;
581
1
  if (result
582
2
          ->Set(context,
583
                OneByteString(isolate, "cannotBeRequired"),
584
2
                cannot_be_required_js)
585
1
          .IsNothing())
586
    return;
587
2
  if (!ToV8Value(context, can_be_required).ToLocal(&can_be_required_js)) return;
588
1
  if (result
589
2
          ->Set(context,
590
                OneByteString(isolate, "canBeRequired"),
591
2
                can_be_required_js)
592
1
          .IsNothing()) {
593
    return;
594
  }
595
2
  info.GetReturnValue().Set(result);
596
}
597
598
1
void BuiltinLoader::GetCacheUsage(const FunctionCallbackInfo<Value>& args) {
599
1
  Environment* env = Environment::GetCurrent(args);
600
1
  Isolate* isolate = env->isolate();
601
1
  Local<Context> context = env->context();
602
1
  Local<Object> result = Object::New(isolate);
603
604
  Local<Value> builtins_with_cache_js;
605
  Local<Value> builtins_without_cache_js;
606
  Local<Value> builtins_in_snapshot_js;
607
2
  if (!ToV8Value(context, env->builtins_with_cache)
608
1
           .ToLocal(&builtins_with_cache_js)) {
609
    return;
610
  }
611
1
  if (result
612
2
          ->Set(env->context(),
613
                OneByteString(isolate, "compiledWithCache"),
614
3
                builtins_with_cache_js)
615
1
          .IsNothing()) {
616
    return;
617
  }
618
619
2
  if (!ToV8Value(context, env->builtins_without_cache)
620
1
           .ToLocal(&builtins_without_cache_js)) {
621
    return;
622
  }
623
1
  if (result
624
2
          ->Set(env->context(),
625
                OneByteString(isolate, "compiledWithoutCache"),
626
3
                builtins_without_cache_js)
627
1
          .IsNothing()) {
628
    return;
629
  }
630
631
2
  if (!ToV8Value(context, env->builtins_in_snapshot)
632
1
           .ToLocal(&builtins_without_cache_js)) {
633
    return;
634
  }
635
1
  if (result
636
2
          ->Set(env->context(),
637
                OneByteString(isolate, "compiledInSnapshot"),
638
3
                builtins_without_cache_js)
639
1
          .IsNothing()) {
640
    return;
641
  }
642
643
2
  args.GetReturnValue().Set(result);
644
}
645
646
825
void BuiltinLoader::BuiltinIdsGetter(Local<Name> property,
647
                                     const PropertyCallbackInfo<Value>& info) {
648
825
  Isolate* isolate = info.GetIsolate();
649
650
1650
  std::vector<std::string> ids = GetInstance()->GetBuiltinIds();
651
1650
  info.GetReturnValue().Set(
652
1650
      ToV8Value(isolate->GetCurrentContext(), ids).ToLocalChecked());
653
825
}
654
655
786
void BuiltinLoader::ConfigStringGetter(
656
    Local<Name> property, const PropertyCallbackInfo<Value>& info) {
657
2358
  info.GetReturnValue().Set(GetConfigString(info.GetIsolate()));
658
786
}
659
660
453483
void BuiltinLoader::RecordResult(const char* id,
661
                                 BuiltinLoader::Result result,
662
                                 Environment* env) {
663
453483
  if (result == BuiltinLoader::Result::kWithCache) {
664
441216
    env->builtins_with_cache.insert(id);
665
  } else {
666
12267
    env->builtins_without_cache.insert(id);
667
  }
668
453483
}
669
670
443239
void BuiltinLoader::CompileFunction(const FunctionCallbackInfo<Value>& args) {
671
443239
  Environment* env = Environment::GetCurrent(args);
672
1329717
  CHECK(args[0]->IsString());
673
1329717
  node::Utf8Value id_v(env->isolate(), args[0].As<String>());
674
443239
  const char* id = *id_v;
675
  MaybeLocal<Function> maybe =
676
443239
      GetInstance()->LookupAndCompile(env->context(), id, env);
677
  Local<Function> fn;
678
443239
  if (maybe.ToLocal(&fn)) {
679
886478
    args.GetReturnValue().Set(fn);
680
  }
681
443239
}
682
683
4
void BuiltinLoader::HasCachedBuiltins(const FunctionCallbackInfo<Value>& args) {
684
16
  args.GetReturnValue().Set(
685
4
      v8::Boolean::New(args.GetIsolate(), GetInstance()->has_code_cache_));
686
4
}
687
688
// TODO(joyeecheung): It is somewhat confusing that Class::Initialize
689
// is used to initialize to the binding, but it is the current convention.
690
// Rename this across the code base to something that makes more sense.
691
786
void BuiltinLoader::Initialize(Local<Object> target,
692
                               Local<Value> unused,
693
                               Local<Context> context,
694
                               void* priv) {
695
786
  Environment* env = Environment::GetCurrent(context);
696
786
  Isolate* isolate = env->isolate();
697
698
  target
699
1572
      ->SetAccessor(context,
700
                    env->config_string(),
701
                    ConfigStringGetter,
702
                    nullptr,
703
                    MaybeLocal<Value>(),
704
                    DEFAULT,
705
                    None,
706
2358
                    SideEffectType::kHasNoSideEffect)
707
      .Check();
708
  target
709
1572
      ->SetAccessor(context,
710
                    FIXED_ONE_BYTE_STRING(isolate, "builtinIds"),
711
                    BuiltinIdsGetter,
712
                    nullptr,
713
                    MaybeLocal<Value>(),
714
                    DEFAULT,
715
                    None,
716
2358
                    SideEffectType::kHasNoSideEffect)
717
      .Check();
718
719
  target
720
1572
      ->SetAccessor(context,
721
                    FIXED_ONE_BYTE_STRING(isolate, "builtinCategories"),
722
                    GetBuiltinCategories,
723
                    nullptr,
724
                    Local<Value>(),
725
                    DEFAULT,
726
                    None,
727
2358
                    SideEffectType::kHasNoSideEffect)
728
      .Check();
729
730
786
  SetMethod(context, target, "getCacheUsage", BuiltinLoader::GetCacheUsage);
731
786
  SetMethod(context, target, "compileFunction", BuiltinLoader::CompileFunction);
732
786
  SetMethod(context, target, "hasCachedBuiltins", HasCachedBuiltins);
733
  // internalBinding('builtins') should be frozen
734
1572
  target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
735
786
}
736
737
5551
void BuiltinLoader::RegisterExternalReferences(
738
    ExternalReferenceRegistry* registry) {
739
5551
  registry->Register(ConfigStringGetter);
740
5551
  registry->Register(BuiltinIdsGetter);
741
5551
  registry->Register(GetBuiltinCategories);
742
5551
  registry->Register(GetCacheUsage);
743
5551
  registry->Register(CompileFunction);
744
5551
  registry->Register(HasCachedBuiltins);
745
5551
}
746
747
}  // namespace builtins
748
}  // namespace node
749
750
5622
NODE_MODULE_CONTEXT_AWARE_INTERNAL(builtins,
751
                                   node::builtins::BuiltinLoader::Initialize)
752

22417
NODE_MODULE_EXTERNAL_REFERENCE(
753
    builtins, node::builtins::BuiltinLoader::RegisterExternalReferences)