• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4// This test verifies that if the binary is compiled with code cache,
5// and the cache is used when built in modules are compiled.
6// Otherwise, verifies that no cache is used when compiling builtins.
7
8const { isMainThread } = require('../common');
9const assert = require('assert');
10const {
11  internalBinding
12} = require('internal/test/binding');
13const {
14  getCacheUsage,
15  builtinCategories: { canBeRequired }
16} = internalBinding('builtins');
17
18for (const key of canBeRequired) {
19  require(`node:${key}`);
20}
21
22// The computation has to be delayed until we have done loading modules
23const {
24  compiledWithoutCache,
25  compiledWithCache,
26  compiledInSnapshot
27} = getCacheUsage();
28
29function extractModules(list) {
30  return list.filter((m) => m.startsWith('NativeModule'))
31  .map((m) => m.replace('NativeModule ', ''));
32}
33
34const loadedModules = extractModules(process.moduleLoadList);
35
36// Cross-compiled binaries do not have code cache, verifies that the builtins
37// are all compiled without cache and we are doing the bookkeeping right.
38if (!process.features.cached_builtins) {
39  assert(!process.config.variables.node_use_node_code_cache ||
40         process.execArgv.includes('--no-node-snapshot'));
41
42  if (isMainThread) {
43    assert.deepStrictEqual(compiledWithCache, new Set());
44    for (const key of loadedModules) {
45      assert(compiledWithoutCache.has(key),
46             `"${key}" should've been compiled without code cache`);
47    }
48  } else {
49    // TODO(joyeecheung): create a list of modules whose cache can be shared
50    // from the main thread to the worker thread and check that their
51    // cache are hit
52    assert.notDeepStrictEqual(compiledWithCache, new Set());
53  }
54} else {  // Native compiled
55  assert(process.config.variables.node_use_node_code_cache);
56
57  const wrong = [];
58  for (const key of loadedModules) {
59    if (key.startsWith('internal/deps/v8/tools')) {
60      continue;
61    }
62    if (!compiledWithCache.has(key) &&
63      compiledInSnapshot.indexOf(key) === -1) {
64      wrong.push(`"${key}" should've been compiled **with** code cache`);
65    }
66  }
67  assert.strictEqual(wrong.length, 0, wrong.join('\n'));
68}
69