GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_builtins.cc Lines: 266 298 89.3 %
Date: 2022-09-07 04:19:57 Branches: 84 108 77.8 %

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::Value;
30
31
BuiltinLoader BuiltinLoader::instance_;
32
33
5545
BuiltinLoader::BuiltinLoader() : config_(GetConfig()), has_code_cache_(false) {
34
5545
  LoadJavaScriptSource();
35
5545
}
36
37
841602
BuiltinLoader* BuiltinLoader::GetInstance() {
38
841602
  return &instance_;
39
}
40
41
bool BuiltinLoader::Exists(const char* id) {
42
  auto& source = GetInstance()->source_;
43
  return source.find(id) != source.end();
44
}
45
46
19
bool BuiltinLoader::Add(const char* id, const UnionBytes& source) {
47
19
  auto result = GetInstance()->source_.emplace(id, source);
48
19
  return result.second;
49
}
50
51
42
Local<Object> BuiltinLoader::GetSourceObject(Local<Context> context) {
52
42
  Isolate* isolate = context->GetIsolate();
53
42
  Local<Object> out = Object::New(isolate);
54
42
  auto& source = GetInstance()->source_;
55
12852
  for (auto const& x : source) {
56
12810
    Local<String> key = OneByteString(isolate, x.first.c_str(), x.first.size());
57
38430
    out->Set(context, key, x.second.ToStringChecked(isolate)).FromJust();
58
  }
59
42
  return out;
60
}
61
62
822
Local<String> BuiltinLoader::GetConfigString(Isolate* isolate) {
63
822
  return GetInstance()->config_.ToStringChecked(isolate);
64
}
65
66
825
std::vector<std::string> BuiltinLoader::GetBuiltinIds() {
67
825
  std::vector<std::string> ids;
68
825
  ids.reserve(source_.size());
69
252633
  for (auto const& x : source_) {
70
251808
    ids.emplace_back(x.first);
71
  }
72
825
  return ids;
73
}
74
75
2
void BuiltinLoader::InitializeBuiltinCategories() {
76
2
  if (builtin_categories_.is_initialized) {
77
    DCHECK(!builtin_categories_.can_be_required.empty());
78
1
    return;
79
  }
80
81
  std::vector<std::string> prefixes = {
82
#if !HAVE_OPENSSL
83
    "internal/crypto/",
84
    "internal/debugger/",
85
#endif  // !HAVE_OPENSSL
86
87
    "internal/bootstrap/",
88
    "internal/per_context/",
89
    "internal/deps/",
90
    "internal/main/"
91
7
  };
92
93
  builtin_categories_.can_be_required.emplace(
94
1
      "internal/deps/cjs-module-lexer/lexer");
95
96
7
  builtin_categories_.cannot_be_required = std::set<std::string> {
97
#if !HAVE_INSPECTOR
98
    "inspector", "internal/util/inspector",
99
#endif  // !HAVE_INSPECTOR
100
101
#if !NODE_USE_V8_PLATFORM || !defined(NODE_HAVE_I18N_SUPPORT)
102
        "trace_events",
103
#endif  // !NODE_USE_V8_PLATFORM || !defined(NODE_HAVE_I18N_SUPPORT)
104
105
#if !HAVE_OPENSSL
106
        "crypto", "crypto/promises", "https", "http2", "tls", "_tls_common",
107
        "_tls_wrap", "internal/tls/secure-pair",
108
        "internal/tls/parse-cert-string", "internal/tls/secure-context",
109
        "internal/http2/core", "internal/http2/compat",
110
        "internal/policy/manifest", "internal/process/policy",
111
        "internal/streams/lazy_transform",
112
#endif           // !HAVE_OPENSSL
113
        "sys",   // Deprecated.
114
        "wasi",  // Experimental.
115
        "internal/test/binding", "internal/v8_prof_polyfill",
116
        "internal/v8_prof_processor",
117
6
  };
118
119
306
  for (auto const& x : source_) {
120
305
    const std::string& id = x.first;
121
1525
    for (auto const& prefix : prefixes) {
122
1220
      if (prefix.length() > id.length()) {
123
348
        continue;
124
      }
125

911
      if (id.find(prefix) == 0 &&
126
39
          builtin_categories_.can_be_required.count(id) == 0) {
127
38
        builtin_categories_.cannot_be_required.emplace(id);
128
      }
129
    }
130
  }
131
132
306
  for (auto const& x : source_) {
133
305
    const std::string& id = x.first;
134
305
    if (0 == builtin_categories_.cannot_be_required.count(id)) {
135
262
      builtin_categories_.can_be_required.emplace(id);
136
    }
137
  }
138
139
1
  builtin_categories_.is_initialized = true;
140
}
141
142
1
const std::set<std::string>& BuiltinLoader::GetCannotBeRequired() {
143
1
  InitializeBuiltinCategories();
144
1
  return builtin_categories_.cannot_be_required;
145
}
146
147
1
const std::set<std::string>& BuiltinLoader::GetCanBeRequired() {
148
1
  InitializeBuiltinCategories();
149
1
  return builtin_categories_.can_be_required;
150
}
151
152
bool BuiltinLoader::CanBeRequired(const char* id) {
153
  return GetCanBeRequired().count(id) == 1;
154
}
155
156
bool BuiltinLoader::CannotBeRequired(const char* id) {
157
  return GetCannotBeRequired().count(id) == 1;
158
}
159
160
5472
BuiltinCodeCacheMap* BuiltinLoader::code_cache() {
161
5472
  return &code_cache_;
162
}
163
164
ScriptCompiler::CachedData* BuiltinLoader::GetCodeCache(const char* id) const {
165
  Mutex::ScopedLock lock(code_cache_mutex_);
166
  const auto it = code_cache_.find(id);
167
  if (it == code_cache_.end()) {
168
    // The module has not been compiled before.
169
    return nullptr;
170
  }
171
  return it->second.get();
172
}
173
174
#ifdef NODE_BUILTIN_MODULES_PATH
175
static std::string OnDiskFileName(const char* id) {
176
  std::string filename = NODE_BUILTIN_MODULES_PATH;
177
  filename += "/";
178
179
  if (strncmp(id, "internal/deps", strlen("internal/deps")) == 0) {
180
    id += strlen("internal/");
181
  } else {
182
    filename += "lib/";
183
  }
184
  filename += id;
185
  filename += ".js";
186
187
  return filename;
188
}
189
#endif  // NODE_BUILTIN_MODULES_PATH
190
191
423367
MaybeLocal<String> BuiltinLoader::LoadBuiltinSource(Isolate* isolate,
192
                                                    const char* id) {
193
#ifdef NODE_BUILTIN_MODULES_PATH
194
  if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
195
#endif  // NODE_BUILTIN_MODULES_PATH
196
423367
    const auto source_it = source_.find(id);
197
423367
    if (UNLIKELY(source_it == source_.end())) {
198
      fprintf(stderr, "Cannot find native builtin: \"%s\".\n", id);
199
      ABORT();
200
    }
201
846734
    return source_it->second.ToStringChecked(isolate);
202
#ifdef NODE_BUILTIN_MODULES_PATH
203
  }
204
  std::string filename = OnDiskFileName(id);
205
206
  std::string contents;
207
  int r = ReadFileSync(&contents, filename.c_str());
208
  if (r != 0) {
209
    const std::string buf = SPrintF("Cannot read local builtin. %s: %s \"%s\"",
210
                                    uv_err_name(r),
211
                                    uv_strerror(r),
212
                                    filename);
213
    Local<String> message = OneByteString(isolate, buf.c_str());
214
    isolate->ThrowException(v8::Exception::Error(message));
215
    return MaybeLocal<String>();
216
  }
217
  return String::NewFromUtf8(
218
      isolate, contents.c_str(), v8::NewStringType::kNormal, contents.length());
219
#endif  // NODE_BUILTIN_MODULES_PATH
220
}
221
222
// Returns Local<Function> of the compiled module if return_code_cache
223
// is false (we are only compiling the function).
224
// Otherwise return a Local<Object> containing the cache.
225
423367
MaybeLocal<Function> BuiltinLoader::LookupAndCompileInternal(
226
    Local<Context> context,
227
    const char* id,
228
    std::vector<Local<String>>* parameters,
229
    BuiltinLoader::Result* result) {
230
423367
  Isolate* isolate = context->GetIsolate();
231
423367
  EscapableHandleScope scope(isolate);
232
233
  Local<String> source;
234
846734
  if (!LoadBuiltinSource(isolate, id).ToLocal(&source)) {
235
    return {};
236
  }
237
238
1270101
  std::string filename_s = std::string("node:") + id;
239
  Local<String> filename =
240
423367
      OneByteString(isolate, filename_s.c_str(), filename_s.size());
241
423367
  ScriptOrigin origin(isolate, filename, 0, 0, true);
242
243
423367
  ScriptCompiler::CachedData* cached_data = nullptr;
244
  {
245
    // Note: The lock here should not extend into the
246
    // `CompileFunction()` call below, because this function may recurse if
247
    // there is a syntax error during bootstrap (because the fatal exception
248
    // handler is invoked, which may load built-in modules).
249
846734
    Mutex::ScopedLock lock(code_cache_mutex_);
250
423367
    auto cache_it = code_cache_.find(id);
251
423367
    if (cache_it != code_cache_.end()) {
252
      // Transfer ownership to ScriptCompiler::Source later.
253
418857
      cached_data = cache_it->second.release();
254
418857
      code_cache_.erase(cache_it);
255
    }
256
  }
257
258
423367
  const bool has_cache = cached_data != nullptr;
259
423367
  ScriptCompiler::CompileOptions options =
260
423367
      has_cache ? ScriptCompiler::kConsumeCodeCache
261
                : ScriptCompiler::kEagerCompile;
262
423367
  ScriptCompiler::Source script_source(source, origin, cached_data);
263
264
  per_process::Debug(DebugCategory::CODE_CACHE,
265
                     "Compiling %s %s code cache\n",
266
                     id,
267
423367
                     has_cache ? "with" : "without");
268
269
  MaybeLocal<Function> maybe_fun =
270
      ScriptCompiler::CompileFunction(context,
271
                                      &script_source,
272
                                      parameters->size(),
273
                                      parameters->data(),
274
                                      0,
275
                                      nullptr,
276
423367
                                      options);
277
278
  // This could fail when there are early errors in the built-in modules,
279
  // e.g. the syntax errors
280
  Local<Function> fun;
281
423367
  if (!maybe_fun.ToLocal(&fun)) {
282
    // In the case of early errors, v8 is already capable of
283
    // decorating the stack for us - note that we use CompileFunction
284
    // so there is no need to worry about wrappers.
285
    return MaybeLocal<Function>();
286
  }
287
288
  // XXX(joyeecheung): this bookkeeping is not exactly accurate because
289
  // it only starts after the Environment is created, so the per_context.js
290
  // will never be in any of these two sets, but the two sets are only for
291
  // testing anyway.
292
293
418857
  *result = (has_cache && !script_source.GetCachedData()->rejected)
294
842224
                ? Result::kWithCache
295
                : Result::kWithoutCache;
296
297
423367
  if (has_cache) {
298
418857
    per_process::Debug(DebugCategory::CODE_CACHE,
299
                       "Code cache of %s (%s) %s\n",
300
                       id,
301
418857
                       script_source.GetCachedData()->buffer_policy ==
302
                               ScriptCompiler::CachedData::BufferNotOwned
303
837714
                           ? "BufferNotOwned"
304
                           : "BufferOwned",
305
418857
                       script_source.GetCachedData()->rejected ? "is rejected"
306
                                                               : "is accepted");
307
  }
308
309
  // Generate new cache for next compilation
310
  std::unique_ptr<ScriptCompiler::CachedData> new_cached_data(
311
423367
      ScriptCompiler::CreateCodeCacheForFunction(fun));
312
423367
  CHECK_NOT_NULL(new_cached_data);
313
314
  {
315
846734
    Mutex::ScopedLock lock(code_cache_mutex_);
316
423367
    const auto it = code_cache_.find(id);
317
    // TODO(joyeecheung): it's safer for each thread to have its own
318
    // copy of the code cache map.
319
423367
    if (it == code_cache_.end()) {
320
422375
      code_cache_.emplace(id, std::move(new_cached_data));
321
    } else {
322
992
      it->second.reset(new_cached_data.release());
323
    }
324
  }
325
326
423367
  return scope.Escape(fun);
327
}
328
329
// Returns Local<Function> of the compiled module if return_code_cache
330
// is false (we are only compiling the function).
331
// Otherwise return a Local<Object> containing the cache.
332
423367
MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
333
    Local<Context> context, const char* id, Environment* optional_env) {
334
  Result result;
335
423367
  std::vector<Local<String>> parameters;
336
423367
  Isolate* isolate = context->GetIsolate();
337
  // Detects parameters of the scripts based on module ids.
338
  // internal/bootstrap/loaders: process, getLinkedBinding,
339
  //                             getInternalBinding, primordials
340
423367
  if (strcmp(id, "internal/bootstrap/loaders") == 0) {
341
786
    parameters = {
342
786
        FIXED_ONE_BYTE_STRING(isolate, "process"),
343
786
        FIXED_ONE_BYTE_STRING(isolate, "getLinkedBinding"),
344
786
        FIXED_ONE_BYTE_STRING(isolate, "getInternalBinding"),
345
786
        FIXED_ONE_BYTE_STRING(isolate, "primordials"),
346
786
    };
347
422581
  } else if (strncmp(id,
348
                     "internal/per_context/",
349
                     strlen("internal/per_context/")) == 0) {
350
    // internal/per_context/*: global, exports, primordials
351
441
    parameters = {
352
441
        FIXED_ONE_BYTE_STRING(isolate, "exports"),
353
441
        FIXED_ONE_BYTE_STRING(isolate, "primordials"),
354
441
    };
355
422140
  } else if (strncmp(id, "internal/main/", strlen("internal/main/")) == 0) {
356
    // internal/main/*: process, require, internalBinding, primordials
357
6290
    parameters = {
358
6290
        FIXED_ONE_BYTE_STRING(isolate, "process"),
359
6290
        FIXED_ONE_BYTE_STRING(isolate, "require"),
360
6290
        FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
361
6290
        FIXED_ONE_BYTE_STRING(isolate, "primordials"),
362
6290
    };
363
415850
  } else if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
364
    // Synthetic embedder main scripts from LoadEnvironment(): process, require
365
19
    parameters = {
366
19
        FIXED_ONE_BYTE_STRING(isolate, "process"),
367
19
        FIXED_ONE_BYTE_STRING(isolate, "require"),
368
19
    };
369
415831
  } else if (strncmp(id,
370
                     "internal/bootstrap/",
371
                     strlen("internal/bootstrap/")) == 0) {
372
    // internal/bootstrap/*: process, require, internalBinding, primordials
373
3156
    parameters = {
374
3156
        FIXED_ONE_BYTE_STRING(isolate, "process"),
375
3156
        FIXED_ONE_BYTE_STRING(isolate, "require"),
376
3156
        FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
377
3156
        FIXED_ONE_BYTE_STRING(isolate, "primordials"),
378
3156
    };
379
  } else {
380
    // others: exports, require, module, process, internalBinding, primordials
381
412675
    parameters = {
382
412675
        FIXED_ONE_BYTE_STRING(isolate, "exports"),
383
412675
        FIXED_ONE_BYTE_STRING(isolate, "require"),
384
412675
        FIXED_ONE_BYTE_STRING(isolate, "module"),
385
412675
        FIXED_ONE_BYTE_STRING(isolate, "process"),
386
412675
        FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
387
412675
        FIXED_ONE_BYTE_STRING(isolate, "primordials"),
388
412675
    };
389
  }
390
391
  MaybeLocal<Function> maybe = GetInstance()->LookupAndCompileInternal(
392
423367
      context, id, &parameters, &result);
393
423367
  if (optional_env != nullptr) {
394
421180
    RecordResult(id, result, optional_env);
395
  }
396
423367
  return maybe;
397
}
398
399
6
bool BuiltinLoader::CompileAllBuiltins(Local<Context> context) {
400
6
  BuiltinLoader* loader = GetInstance();
401
12
  std::vector<std::string> ids = loader->GetBuiltinIds();
402
6
  bool all_succeeded = true;
403
6
  std::string v8_tools_prefix = "internal/deps/v8/tools/";
404
1836
  for (const auto& id : ids) {
405
1830
    if (id.compare(0, v8_tools_prefix.size(), v8_tools_prefix) == 0) {
406
66
      continue;
407
    }
408
3528
    v8::TryCatch bootstrapCatch(context->GetIsolate());
409
1764
    USE(loader->LookupAndCompile(context, id.c_str(), nullptr));
410
1764
    if (bootstrapCatch.HasCaught()) {
411
      per_process::Debug(DebugCategory::CODE_CACHE,
412
                         "Failed to compile code cache for %s\n",
413
                         id.c_str());
414
      all_succeeded = false;
415
      PrintCaughtException(context->GetIsolate(), context, bootstrapCatch);
416
    }
417
  }
418
6
  return all_succeeded;
419
}
420
421
6
void BuiltinLoader::CopyCodeCache(std::vector<CodeCacheInfo>* out) {
422
6
  BuiltinLoader* loader = GetInstance();
423
12
  Mutex::ScopedLock lock(loader->code_cache_mutex());
424
6
  auto in = loader->code_cache();
425
1770
  for (auto const& item : *in) {
426
1764
    out->push_back(
427
1764
        {item.first,
428
1764
         {item.second->data, item.second->data + item.second->length}});
429
  }
430
6
}
431
432
5466
void BuiltinLoader::RefreshCodeCache(const std::vector<CodeCacheInfo>& in) {
433
5466
  BuiltinLoader* loader = GetInstance();
434
5466
  Mutex::ScopedLock lock(loader->code_cache_mutex());
435
5466
  auto out = loader->code_cache();
436
1612470
  for (auto const& item : in) {
437
1607004
    size_t length = item.data.size();
438
1607004
    uint8_t* buffer = new uint8_t[length];
439
1607004
    memcpy(buffer, item.data.data(), length);
440
    auto new_cache = std::make_unique<v8::ScriptCompiler::CachedData>(
441
3214008
        buffer, length, v8::ScriptCompiler::CachedData::BufferOwned);
442
1607004
    auto cache_it = out->find(item.id);
443
1607004
    if (cache_it != out->end()) {
444
      // Release the old cache and replace it with the new copy.
445
      cache_it->second.reset(new_cache.release());
446
    } else {
447
1607004
      out->emplace(item.id, new_cache.release());
448
    }
449
  }
450
5466
  loader->has_code_cache_ = true;
451
5466
}
452
453
1
void BuiltinLoader::GetBuiltinCategories(
454
    Local<Name> property, const PropertyCallbackInfo<Value>& info) {
455
1
  Environment* env = Environment::GetCurrent(info);
456
1
  Isolate* isolate = env->isolate();
457
1
  Local<Context> context = env->context();
458
1
  Local<Object> result = Object::New(isolate);
459
460
  // Copy from the per-process categories
461
  std::set<std::string> cannot_be_required =
462
1
      GetInstance()->GetCannotBeRequired();
463
1
  std::set<std::string> can_be_required = GetInstance()->GetCanBeRequired();
464
465
1
  if (!env->owns_process_state()) {
466
    can_be_required.erase("trace_events");
467
    cannot_be_required.insert("trace_events");
468
  }
469
470
  Local<Value> cannot_be_required_js;
471
  Local<Value> can_be_required_js;
472
473
2
  if (!ToV8Value(context, cannot_be_required).ToLocal(&cannot_be_required_js))
474
    return;
475
1
  if (result
476
1
          ->Set(context,
477
                OneByteString(isolate, "cannotBeRequired"),
478
2
                cannot_be_required_js)
479
1
          .IsNothing())
480
    return;
481
2
  if (!ToV8Value(context, can_be_required).ToLocal(&can_be_required_js)) return;
482
1
  if (result
483
1
          ->Set(context,
484
                OneByteString(isolate, "canBeRequired"),
485
2
                can_be_required_js)
486
1
          .IsNothing()) {
487
    return;
488
  }
489
2
  info.GetReturnValue().Set(result);
490
}
491
492
1
void BuiltinLoader::GetCacheUsage(const FunctionCallbackInfo<Value>& args) {
493
1
  Environment* env = Environment::GetCurrent(args);
494
1
  Isolate* isolate = env->isolate();
495
1
  Local<Context> context = env->context();
496
1
  Local<Object> result = Object::New(isolate);
497
498
  Local<Value> builtins_with_cache_js;
499
  Local<Value> builtins_without_cache_js;
500
  Local<Value> builtins_in_snapshot_js;
501
1
  if (!ToV8Value(context, env->builtins_with_cache)
502
1
           .ToLocal(&builtins_with_cache_js)) {
503
    return;
504
  }
505
1
  if (result
506
1
          ->Set(env->context(),
507
                OneByteString(isolate, "compiledWithCache"),
508
2
                builtins_with_cache_js)
509
1
          .IsNothing()) {
510
    return;
511
  }
512
513
1
  if (!ToV8Value(context, env->builtins_without_cache)
514
1
           .ToLocal(&builtins_without_cache_js)) {
515
    return;
516
  }
517
1
  if (result
518
1
          ->Set(env->context(),
519
                OneByteString(isolate, "compiledWithoutCache"),
520
2
                builtins_without_cache_js)
521
1
          .IsNothing()) {
522
    return;
523
  }
524
525
1
  if (!ToV8Value(context, env->builtins_in_snapshot)
526
1
           .ToLocal(&builtins_without_cache_js)) {
527
    return;
528
  }
529
1
  if (result
530
1
          ->Set(env->context(),
531
                OneByteString(isolate, "compiledInSnapshot"),
532
2
                builtins_without_cache_js)
533
1
          .IsNothing()) {
534
    return;
535
  }
536
537
2
  args.GetReturnValue().Set(result);
538
}
539
540
819
void BuiltinLoader::BuiltinIdsGetter(Local<Name> property,
541
                                     const PropertyCallbackInfo<Value>& info) {
542
819
  Isolate* isolate = info.GetIsolate();
543
544
819
  std::vector<std::string> ids = GetInstance()->GetBuiltinIds();
545
2457
  info.GetReturnValue().Set(
546
1638
      ToV8Value(isolate->GetCurrentContext(), ids).ToLocalChecked());
547
819
}
548
549
780
void BuiltinLoader::ConfigStringGetter(
550
    Local<Name> property, const PropertyCallbackInfo<Value>& info) {
551
780
  info.GetReturnValue().Set(GetConfigString(info.GetIsolate()));
552
780
}
553
554
421180
void BuiltinLoader::RecordResult(const char* id,
555
                                 BuiltinLoader::Result result,
556
                                 Environment* env) {
557
421180
  if (result == BuiltinLoader::Result::kWithCache) {
558
409807
    env->builtins_with_cache.insert(id);
559
  } else {
560
11373
    env->builtins_without_cache.insert(id);
561
  }
562
421180
}
563
564
411049
void BuiltinLoader::CompileFunction(const FunctionCallbackInfo<Value>& args) {
565
411049
  Environment* env = Environment::GetCurrent(args);
566
822098
  CHECK(args[0]->IsString());
567
1233147
  node::Utf8Value id_v(env->isolate(), args[0].As<String>());
568
411049
  const char* id = *id_v;
569
  MaybeLocal<Function> maybe =
570
411049
      GetInstance()->LookupAndCompile(env->context(), id, env);
571
  Local<Function> fn;
572
411049
  if (maybe.ToLocal(&fn)) {
573
822098
    args.GetReturnValue().Set(fn);
574
  }
575
411049
}
576
577
4
void BuiltinLoader::HasCachedBuiltins(const FunctionCallbackInfo<Value>& args) {
578
4
  args.GetReturnValue().Set(
579
4
      v8::Boolean::New(args.GetIsolate(), GetInstance()->has_code_cache_));
580
4
}
581
582
// TODO(joyeecheung): It is somewhat confusing that Class::Initialize
583
// is used to initialize to the binding, but it is the current convention.
584
// Rename this across the code base to something that makes more sense.
585
780
void BuiltinLoader::Initialize(Local<Object> target,
586
                               Local<Value> unused,
587
                               Local<Context> context,
588
                               void* priv) {
589
780
  Environment* env = Environment::GetCurrent(context);
590
780
  Isolate* isolate = env->isolate();
591
592
  target
593
780
      ->SetAccessor(context,
594
                    env->config_string(),
595
                    ConfigStringGetter,
596
                    nullptr,
597
                    MaybeLocal<Value>(),
598
                    DEFAULT,
599
                    None,
600
3120
                    SideEffectType::kHasNoSideEffect)
601
      .Check();
602
  target
603
780
      ->SetAccessor(context,
604
                    FIXED_ONE_BYTE_STRING(isolate, "builtinIds"),
605
                    BuiltinIdsGetter,
606
                    nullptr,
607
                    MaybeLocal<Value>(),
608
                    DEFAULT,
609
                    None,
610
3120
                    SideEffectType::kHasNoSideEffect)
611
      .Check();
612
613
  target
614
780
      ->SetAccessor(context,
615
                    FIXED_ONE_BYTE_STRING(isolate, "builtinCategories"),
616
                    GetBuiltinCategories,
617
                    nullptr,
618
                    Local<Value>(),
619
                    DEFAULT,
620
                    None,
621
1560
                    SideEffectType::kHasNoSideEffect)
622
      .Check();
623
624
780
  SetMethod(context, target, "getCacheUsage", BuiltinLoader::GetCacheUsage);
625
780
  SetMethod(context, target, "compileFunction", BuiltinLoader::CompileFunction);
626
780
  SetMethod(context, target, "hasCachedBuiltins", HasCachedBuiltins);
627
  // internalBinding('builtins') should be frozen
628
780
  target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
629
780
}
630
631
5473
void BuiltinLoader::RegisterExternalReferences(
632
    ExternalReferenceRegistry* registry) {
633
5473
  registry->Register(ConfigStringGetter);
634
5473
  registry->Register(BuiltinIdsGetter);
635
5473
  registry->Register(GetBuiltinCategories);
636
5473
  registry->Register(GetCacheUsage);
637
5473
  registry->Register(CompileFunction);
638
5473
  registry->Register(HasCachedBuiltins);
639
5473
}
640
641
}  // namespace builtins
642
}  // namespace node
643
644
5545
NODE_MODULE_CONTEXT_AWARE_INTERNAL(builtins,
645
                                   node::builtins::BuiltinLoader::Initialize)
646
5473
NODE_MODULE_EXTERNAL_REFERENCE(
647
    builtins, node::builtins::BuiltinLoader::RegisterExternalReferences)