node/lib/internal/bootstrap/cache.js
Joyee Cheung 9c0c3f1443
lib: fix code cache generation
e7f710c1 broke the code cache generation since internalBinding
is now passed in through the wrapper and cannot be redeclared.
This patch fixes that.

Refs: https://github.com/nodejs/node/issues/21563
PR-URL: https://github.com/nodejs/node/pull/23855
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-10-30 14:38:15 +08:00

71 lines
2.0 KiB
JavaScript

'use strict';
// This is only exposed for internal build steps and testing purposes.
// We create new copies of the source and the code cache
// so the resources eventually used to compile builtin modules
// cannot be tampered with even with --expose-internals
const {
NativeModule
} = require('internal/bootstrap/loaders');
const { hasTracing } = process.binding('config');
function getCodeCache(id) {
const cached = NativeModule.getCached(id);
if (cached && (cached.loaded || cached.loading)) {
return cached.script.createCachedData();
}
// The script has not been compiled and run
NativeModule.require(id);
return getCodeCache(id);
}
const depsModule = Object.keys(NativeModule._source).filter(
(key) => NativeModule.isDepsModule(key) || key.startsWith('internal/deps')
);
// Modules with source code compiled in js2c that
// cannot be compiled with the code cache
const cannotUseCache = [
'config',
'sys', // deprecated
'internal/v8_prof_polyfill',
'internal/v8_prof_processor',
'internal/per_context',
'internal/test/binding',
// TODO(joyeecheung): update the C++ side so that
// the code cache is also used when compiling these
// two files.
'internal/bootstrap/loaders',
'internal/bootstrap/node'
].concat(depsModule);
// Skip modules that cannot be required when they are not
// built into the binary.
if (process.config.variables.v8_enable_inspector !== 1) {
cannotUseCache.push('inspector');
cannotUseCache.push('internal/util/inspector');
}
if (!hasTracing) {
cannotUseCache.push('trace_events');
}
module.exports = {
cachableBuiltins: Object.keys(NativeModule._source).filter(
(key) => !cannotUseCache.includes(key)
),
builtinSource: Object.assign({}, NativeModule._source),
getCodeCache,
getSource: NativeModule.getSource,
codeCache: internalBinding('code_cache'),
compiledWithoutCache: NativeModule.compiledWithoutCache,
compiledWithCache: NativeModule.compiledWithCache,
nativeModuleWrap(script) {
return NativeModule.wrap(script);
},
cannotUseCache
};