build: create V8 code cache after script is run

This patch makes it possible to generate the code cache
for the builtins directly from the original script object
(instead of compiling a new one) and after the script has
been run (via `NativeModule.require`). Before this patch
only the top level functions (the wrapped ones)
are included in the cache, after this patch the inner
functions in those modules will be included as well.

Also blacklists modules from dependencies like V8 and
node-inspect since we cannot guarantee that they are suitable
to be executed directly.

PR-URL: https://github.com/nodejs/node/pull/21567
Refs: https://github.com/nodejs/node/issues/21563
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Joyee Cheung authored and Michaël Zasso committed 2018-07-31 09:35:46 +02:00
1 parent 67d7a15886
commit 62024b651e
4 files changed
+55 -35

No files matched your search

+7 -18
View File
@@ -8,12 +8,10 @@
// of `configure`.
const {
nativeModuleWrap,
builtinSource,
cannotUseCache
getCodeCache,
cachableBuiltins
} = require('internal/bootstrap/cache');
const vm = require('vm');
const fs = require('fs');
const resultPath = process.argv[2];
@@ -72,25 +70,16 @@ const cacheInitializers = [];
let totalCacheSize = 0;
for (const key of Object.keys(builtinSource)) {
if (cannotUseCache.includes(key)) continue;
const code = nativeModuleWrap(builtinSource[key]);
// Note that this must corresponds to the code in
// NativeModule.prototype.compile
const script = new vm.Script(code, {
filename: `${key}.js`,
produceCachedData: true
});
if (!script.cachedData) {
for (const key of cachableBuiltins) {
const cachedData = getCodeCache(key);
if (!cachedData.length) {
console.error(`Failed to generate code cache for '${key}'`);
process.exit(1);
}
const length = script.cachedData.length;
const length = cachedData.length;
totalCacheSize += length;
const { definition, initializer } = getInitalizer(key, script.cachedData);
const { definition, initializer } = getInitalizer(key, cachedData);
cacheDefinitions.push(definition);
cacheInitializers.push(initializer);
console.log(`Generated cache for '${key}', size = ${formatSize(length)}` +