11cb0ef41Sopenharmony_ci// Flags: --expose-internals 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci// This test verifies that if the binary is compiled with code cache, 51cb0ef41Sopenharmony_ci// and the cache is used when built in modules are compiled. 61cb0ef41Sopenharmony_ci// Otherwise, verifies that no cache is used when compiling builtins. 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst { isMainThread } = require('../common'); 91cb0ef41Sopenharmony_ciconst assert = require('assert'); 101cb0ef41Sopenharmony_ciconst { 111cb0ef41Sopenharmony_ci internalBinding 121cb0ef41Sopenharmony_ci} = require('internal/test/binding'); 131cb0ef41Sopenharmony_ciconst { 141cb0ef41Sopenharmony_ci getCacheUsage, 151cb0ef41Sopenharmony_ci builtinCategories: { canBeRequired } 161cb0ef41Sopenharmony_ci} = internalBinding('builtins'); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cifor (const key of canBeRequired) { 191cb0ef41Sopenharmony_ci require(`node:${key}`); 201cb0ef41Sopenharmony_ci} 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci// The computation has to be delayed until we have done loading modules 231cb0ef41Sopenharmony_ciconst { 241cb0ef41Sopenharmony_ci compiledWithoutCache, 251cb0ef41Sopenharmony_ci compiledWithCache, 261cb0ef41Sopenharmony_ci compiledInSnapshot 271cb0ef41Sopenharmony_ci} = getCacheUsage(); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_cifunction extractModules(list) { 301cb0ef41Sopenharmony_ci return list.filter((m) => m.startsWith('NativeModule')) 311cb0ef41Sopenharmony_ci .map((m) => m.replace('NativeModule ', '')); 321cb0ef41Sopenharmony_ci} 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciconst loadedModules = extractModules(process.moduleLoadList); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci// Cross-compiled binaries do not have code cache, verifies that the builtins 371cb0ef41Sopenharmony_ci// are all compiled without cache and we are doing the bookkeeping right. 381cb0ef41Sopenharmony_ciif (!process.features.cached_builtins) { 391cb0ef41Sopenharmony_ci assert(!process.config.variables.node_use_node_code_cache || 401cb0ef41Sopenharmony_ci process.execArgv.includes('--no-node-snapshot')); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci if (isMainThread) { 431cb0ef41Sopenharmony_ci assert.deepStrictEqual(compiledWithCache, new Set()); 441cb0ef41Sopenharmony_ci for (const key of loadedModules) { 451cb0ef41Sopenharmony_ci assert(compiledWithoutCache.has(key), 461cb0ef41Sopenharmony_ci `"${key}" should've been compiled without code cache`); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci } else { 491cb0ef41Sopenharmony_ci // TODO(joyeecheung): create a list of modules whose cache can be shared 501cb0ef41Sopenharmony_ci // from the main thread to the worker thread and check that their 511cb0ef41Sopenharmony_ci // cache are hit 521cb0ef41Sopenharmony_ci assert.notDeepStrictEqual(compiledWithCache, new Set()); 531cb0ef41Sopenharmony_ci } 541cb0ef41Sopenharmony_ci} else { // Native compiled 551cb0ef41Sopenharmony_ci assert(process.config.variables.node_use_node_code_cache); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci const wrong = []; 581cb0ef41Sopenharmony_ci for (const key of loadedModules) { 591cb0ef41Sopenharmony_ci if (key.startsWith('internal/deps/v8/tools')) { 601cb0ef41Sopenharmony_ci continue; 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci if (!compiledWithCache.has(key) && 631cb0ef41Sopenharmony_ci compiledInSnapshot.indexOf(key) === -1) { 641cb0ef41Sopenharmony_ci wrong.push(`"${key}" should've been compiled **with** code cache`); 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci assert.strictEqual(wrong.length, 0, wrong.join('\n')); 681cb0ef41Sopenharmony_ci} 69