src: compile native modules and their code cache in C++

This patch refactors out a part of NativeModule.prototype.compile
(in JS land) into a C++ NativeModule class, this enables a
couple of possibilities:

1. By moving the code to the C++ land, we have more opportunity
  to specialize the compilation process of the native modules
  (e.g. compilation options, code cache) that is orthogonal to
  how user land modules are compiled
2. We can reuse the code to compile bootstrappers and context
  fixers and enable them to be compiled with the code cache later,
  since they are not loaded by NativeModule in the JS land their
  caching must be done in C++.
3. Since there is no need to pass the static data to JS for
  compilation anymore, this enables us to use
  (std::map<std::string, const char*>) in the generated
  node_code_cache.cc and node_javascript.cc later, and scope
  every actual access to the source of native modules to a
  std::map lookup instead of a lookup on a v8::Object in
  dictionary mode.

This patch also refactor the code cache generator and tests
a bit and trace the `withCodeCache` and `withoutCodeCache`
in a Set instead of an Array, and makes sure that all the cachable
builtins are tested.

PR-URL: https://github.com/nodejs/node/pull/24221
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Joyee Cheung
2018-11-03 14:26:32 +08:00
parent 3856d8a14c
commit bd765d61d7
12 changed files with 680 additions and 364 deletions

View File

@@ -7,17 +7,28 @@
require('../common');
const assert = require('assert');
const {
types: {
isUint8Array
}
} = require('util');
const {
cachableBuiltins,
codeCache,
compiledWithCache,
compiledWithoutCache
cannotUseCache
} = require('internal/bootstrap/cache');
const {
isMainThread
} = require('worker_threads');
const {
internalBinding
} = require('internal/test/binding');
const {
compiledWithoutCache,
compiledWithCache
} = internalBinding('native_module');
for (const key of cachableBuiltins) {
if (!isMainThread && key === 'trace_events') {
continue; // Cannot load trace_events in workers
}
require(key);
}
const loadedModules = process.moduleLoadList
.filter((m) => m.startsWith('NativeModule'))
@@ -26,29 +37,23 @@ const loadedModules = process.moduleLoadList
// The binary is not configured with code cache, verifies that the builtins
// are all compiled without cache and we are doing the bookkeeping right.
if (process.config.variables.node_code_cache_path === undefined) {
assert.deepStrictEqual(compiledWithCache, []);
assert.notStrictEqual(compiledWithoutCache.length, 0);
for (const key of loadedModules) {
assert(compiledWithoutCache.includes(key),
`"${key}" should not have been compiled with code cache`);
}
console.log('The binary is not configured with code cache');
assert.deepStrictEqual(compiledWithCache, new Set());
assert.deepStrictEqual(compiledWithoutCache, new Set(loadedModules));
} else {
// The binary is configured with code cache.
console.log('The binary is configured with code cache');
assert.strictEqual(
typeof process.config.variables.node_code_cache_path,
'string'
);
assert.deepStrictEqual(compiledWithoutCache, []);
for (const key of loadedModules) {
assert(compiledWithCache.includes(key),
`"${key}" should've been compiled with code cache`);
}
for (const key of cachableBuiltins) {
assert(isUint8Array(codeCache[key]) && codeCache[key].length > 0,
`Code cache for "${key}" should've been generated`);
if (cannotUseCache.includes(key)) {
assert(compiledWithoutCache.has(key),
`"${key}" should've been compiled without code cache`);
} else {
assert(compiledWithCache.has(key),
`"${key}" should've been compiled with code cache`);
}
}
}