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 moduleCategories: { canBeRequired, cannotBeRequired } 16} = internalBinding('native_module'); 17 18for (const key of canBeRequired) { 19 require(key); 20} 21 22// The computation has to be delayed until we have done loading modules 23const { 24 compiledWithoutCache, 25 compiledWithCache 26} = getCacheUsage(); 27 28const loadedModules = process.moduleLoadList 29 .filter((m) => m.startsWith('NativeModule')) 30 .map((m) => m.replace('NativeModule ', '')); 31 32// Cross-compiled binaries do not have code cache, verifies that the builtins 33// are all compiled without cache and we are doing the bookkeeping right. 34if (!process.features.cached_builtins) { 35 console.log('The binary is not configured with code cache'); 36 assert(!process.config.variables.node_use_node_code_cache); 37 38 if (isMainThread) { 39 assert.deepStrictEqual(compiledWithCache, new Set()); 40 for (const key of loadedModules) { 41 assert(compiledWithoutCache.has(key), 42 `"${key}" should've been compiled without code cache`); 43 } 44 } else { 45 // TODO(joyeecheung): create a list of modules whose cache can be shared 46 // from the main thread to the worker thread and check that their 47 // cache are hit 48 assert.notDeepStrictEqual(compiledWithCache, new Set()); 49 } 50} else { // Native compiled 51 assert(process.config.variables.node_use_node_code_cache); 52 53 if (!isMainThread) { 54 for (const key of [ 'internal/bootstrap/pre_execution' ]) { 55 canBeRequired.add(key); 56 cannotBeRequired.delete(key); 57 } 58 } 59 60 const wrong = []; 61 for (const key of loadedModules) { 62 if (cannotBeRequired.has(key) && !compiledWithoutCache.has(key)) { 63 wrong.push(`"${key}" should've been compiled **without** code cache`); 64 } 65 if (canBeRequired.has(key) && !compiledWithCache.has(key)) { 66 wrong.push(`"${key}" should've been compiled **with** code cache`); 67 } 68 } 69 assert.strictEqual(wrong.length, 0, wrong.join('\n')); 70} 71