summaryrefslogtreecommitdiff
path: root/tools/generate_code_cache.js
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2019-04-04 06:29:02 +0800
committerRefael Ackermann <refack@gmail.com>2019-04-16 18:23:32 -0400
commit4fd71935795fa7c284f5ed621551b65a28b8271c (patch)
tree63a90841b3ac80aab7c28c64d0b6dc46e2f19c82 /tools/generate_code_cache.js
parent1c2616971417bee811ea00da436c87a489f9b1ed (diff)
downloadandroid-node-v8-4fd71935795fa7c284f5ed621551b65a28b8271c.tar.gz
android-node-v8-4fd71935795fa7c284f5ed621551b65a28b8271c.tar.bz2
android-node-v8-4fd71935795fa7c284f5ed621551b65a28b8271c.zip
tools: implement mkcodecache as an executable
This patch implement a mkcodecache executable on top of the `NativeModuleLoader` singleton. This makes it possible to build a Node.js binary with embedded code cache without building itself using the code cache stub - the cache is now initialized by `NativeModuleEnv` instead which can be refactored out of the mkcodecache dependencies. PR-URL: https://github.com/nodejs/node/pull/27161 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'tools/generate_code_cache.js')
-rw-r--r--tools/generate_code_cache.js135
1 files changed, 0 insertions, 135 deletions
diff --git a/tools/generate_code_cache.js b/tools/generate_code_cache.js
deleted file mode 100644
index 8afbc14a9b..0000000000
--- a/tools/generate_code_cache.js
+++ /dev/null
@@ -1,135 +0,0 @@
-'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 { internalBinding } = require('internal/test/binding');
-const {
- moduleCategories: { canBeRequired },
- getCodeCache,
- compileFunction,
-} = internalBinding('native_module');
-
-const {
- types: {
- isUint8Array
- }
-} = require('util');
-
-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 dataDef = 'std::make_unique<v8::ScriptCompiler::CachedData>(' +
- `${defName}, static_cast<int>(arraysize(${defName})), ` +
- 'policy)';
- const initializer =
- 'code_cache->emplace(\n' +
- ` "${key}",\n` +
- ` ${dataDef}\n` +
- ');';
- return {
- definition, initializer
- };
-}
-
-const cacheDefinitions = [];
-const cacheInitializers = [];
-let totalCacheSize = 0;
-
-function lexical(a, b) {
- if (a < b) {
- return -1;
- }
- if (a > b) {
- return 1;
- }
- return 0;
-}
-
-// TODO(joyeecheung): support non-modules that require different
-// parameters in the wrapper.
-for (const key of [...canBeRequired].sort(lexical)) {
- compileFunction(key); // compile it
- 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,
- } = getInitalizer(key, cachedData);
- cacheDefinitions.push(definition);
- cacheInitializers.push(initializer);
- console.log(`Generated cache for '${key}', size = ${formatSize(size)}` +
- `, total = ${formatSize(totalCacheSize)}`);
-}
-
-const result = `#include "node_native_module_env.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 NativeModuleEnv::InitializeCodeCache() {
- NativeModuleCacheMap* code_cache =
- NativeModuleLoader::GetInstance()->code_cache();
- if (!code_cache->empty()) {
- return;
- }
-
- auto policy = v8::ScriptCompiler::CachedData::BufferPolicy::BufferNotOwned;
- ${cacheInitializers.join('\n ')}
-}
-
-} // namespace native_module
-} // namespace node
-`;
-
-fs.writeFileSync(resultPath, result);
-console.log(`Generated code cache C++ file to ${resultPath}`);