mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
7778c035a0
Instead of putting the source code and the cache in v8::Objects, put them in per-process std::maps. This has the following benefits: - It's slightly lighter in weight compared to storing things on the v8 heap. Also it may be slightly faster since the preivous v8::Object is already in dictionary mode - though the difference is very small given the number of native modules is limited. - The source and code cache generation templates are now much simpler since they just initialize static arrays and manipulate STL constructs. - The static native module data can be accessed independently of any Environment or Isolate, and it's easy to look them up from the C++'s side. - It's now impossible to mutate the source code used to compile native modules from the JS land since it's completely separate from the v8 heap. We can still get the constant strings from process.binding('natives') but that's all. A few drive-by fixes: - Remove DecorateErrorStack in LookupAndCompile - We don't need to capture the exception to decorate when we encounter errors during native module compilation, as those errors should be syntax errors and v8 is able to decorate them well. We use CompileFunctionInContext so there is no need to worry about wrappers either. - The code cache could be rejected when node is started with v8 flags. Instead of aborting in that case, simply keep a record in the native_module_without_cache set. - Refactor js2c.py a bit, reduce code duplication and inline Render() to make the one-byte/two-byte special treatment easier to read. PR-URL: https://github.com/nodejs/node/pull/24384 Fixes: https://github.com/Remove Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
144 lines
3.6 KiB
JavaScript
144 lines
3.6 KiB
JavaScript
'use strict';
|
|
|
|
// Flags: --expose-internals
|
|
|
|
// This file generates the code cache for builtin modules and
|
|
// writes them into static char arrays of a C++ file that can be
|
|
// compiled into the binary using the `--code-cache-path` option
|
|
// of `configure`.
|
|
|
|
const {
|
|
getSource,
|
|
getCodeCache,
|
|
cachableBuiltins
|
|
} = require('internal/bootstrap/cache');
|
|
|
|
const {
|
|
types: {
|
|
isUint8Array
|
|
}
|
|
} = require('util');
|
|
|
|
function hash(str) {
|
|
if (process.versions.openssl) {
|
|
return require('crypto').createHash('sha256').update(str).digest('hex');
|
|
}
|
|
return '';
|
|
}
|
|
|
|
const fs = require('fs');
|
|
|
|
const resultPath = process.argv[2];
|
|
if (!resultPath) {
|
|
console.error(`Usage: ${process.argv[0]} ${process.argv[1]}` +
|
|
'path/to/node_code_cache.cc');
|
|
process.exit(1);
|
|
}
|
|
|
|
/**
|
|
* Format a number of a size in bytes into human-readable strings
|
|
* @param {number} num
|
|
* @return {string}
|
|
*/
|
|
function formatSize(num) {
|
|
if (num < 1024) {
|
|
return `${(num).toFixed(2)}B`;
|
|
} else if (num < 1024 ** 2) {
|
|
return `${(num / 1024).toFixed(2)}KB`;
|
|
} else if (num < 1024 ** 3) {
|
|
return `${(num / (1024 ** 2)).toFixed(2)}MB`;
|
|
} else {
|
|
return `${(num / (1024 ** 3)).toFixed(2)}GB`;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generates the source code of definitions of the char arrays
|
|
* that contains the code cache and the source code of the
|
|
* initializers of the code cache.
|
|
*
|
|
* @param {string} key ID of the builtin module
|
|
* @param {Uint8Array} cache Code cache of the builtin module
|
|
* @return { definition: string, initializer: string }
|
|
*/
|
|
function getInitalizer(key, cache) {
|
|
const defName = `${key.replace(/\//g, '_').replace(/-/g, '_')}_raw`;
|
|
const definition = `static const uint8_t ${defName}[] = {\n` +
|
|
`${cache.join(',')}\n};`;
|
|
const source = getSource(key);
|
|
const sourceHash = hash(source);
|
|
const initializer =
|
|
'code_cache_.emplace(\n' +
|
|
` "${key}",\n` +
|
|
` UnionBytes(${defName}, arraysize(${defName}))\n` +
|
|
');';
|
|
const hashIntializer =
|
|
'code_cache_hash_.emplace(\n' +
|
|
` "${key}",\n` +
|
|
` "${sourceHash}"\n` +
|
|
');';
|
|
return {
|
|
definition, initializer, hashIntializer, sourceHash
|
|
};
|
|
}
|
|
|
|
const cacheDefinitions = [];
|
|
const cacheInitializers = [];
|
|
const cacheHashInitializers = [];
|
|
let totalCacheSize = 0;
|
|
|
|
function lexical(a, b) {
|
|
if (a < b) {
|
|
return -1;
|
|
}
|
|
if (a > b) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
for (const key of cachableBuiltins.sort(lexical)) {
|
|
const cachedData = getCodeCache(key);
|
|
if (!isUint8Array(cachedData)) {
|
|
console.error(`Failed to generate code cache for '${key}'`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const size = cachedData.byteLength;
|
|
totalCacheSize += size;
|
|
const {
|
|
definition, initializer, hashIntializer, sourceHash
|
|
} = getInitalizer(key, cachedData);
|
|
cacheDefinitions.push(definition);
|
|
cacheInitializers.push(initializer);
|
|
cacheHashInitializers.push(hashIntializer);
|
|
console.log(`Generated cache for '${key}', size = ${formatSize(size)}` +
|
|
`, hash = ${sourceHash}, total = ${formatSize(totalCacheSize)}`);
|
|
}
|
|
|
|
const result = `#include "node_native_module.h"
|
|
#include "node_internals.h"
|
|
|
|
// This file is generated by tools/generate_code_cache.js
|
|
// and is used when configure is run with \`--code-cache-path\`
|
|
|
|
namespace node {
|
|
namespace native_module {
|
|
${cacheDefinitions.join('\n\n')}
|
|
|
|
void NativeModuleLoader::LoadCodeCache() {
|
|
has_code_cache_ = true;
|
|
${cacheInitializers.join('\n ')}
|
|
}
|
|
|
|
void NativeModuleLoader::LoadCodeCacheHash() {
|
|
${cacheHashInitializers.join('\n ')}
|
|
}
|
|
|
|
} // namespace native_module
|
|
} // namespace node
|
|
`;
|
|
|
|
fs.writeFileSync(resultPath, result);
|
|
console.log(`Generated code cache C++ file to ${resultPath}`);
|