mirror of
https://github.com/zebrajr/node.git
synced 2025-12-07 12:20:50 +01:00
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>
59 lines
1.4 KiB
JavaScript
59 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
// This test verifies that the binary is compiled with code cache and the
|
|
// cache is used when built in modules are compiled.
|
|
|
|
const common = require('../common');
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
const { spawnSync } = require('child_process');
|
|
const assert = require('assert');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const readline = require('readline');
|
|
|
|
const generator = path.join(
|
|
__dirname, '..', '..', 'tools', 'generate_code_cache.js'
|
|
);
|
|
tmpdir.refresh();
|
|
const dest = path.join(tmpdir.path, 'cache.cc');
|
|
|
|
// Run tools/generate_code_cache.js
|
|
const child = spawnSync(
|
|
process.execPath,
|
|
['--expose-internals', generator, dest]
|
|
);
|
|
assert.ifError(child.error);
|
|
if (child.status !== 0) {
|
|
console.log(child.stderr.toString());
|
|
assert.strictEqual(child.status, 0);
|
|
}
|
|
|
|
// Verifies that:
|
|
// - node::LoadCodeCache()
|
|
// - node::LoadCodeCacheHash()
|
|
// are defined in the generated code.
|
|
// See src/node_native_module.h for explanations.
|
|
|
|
const rl = readline.createInterface({
|
|
input: fs.createReadStream(dest),
|
|
crlfDelay: Infinity
|
|
});
|
|
|
|
let hasCacheDef = false;
|
|
let hasHashDef = false;
|
|
|
|
rl.on('line', common.mustCallAtLeast((line) => {
|
|
if (line.includes('LoadCodeCache(')) {
|
|
hasCacheDef = true;
|
|
}
|
|
if (line.includes('LoadCodeCacheHash(')) {
|
|
hasHashDef = true;
|
|
}
|
|
}, 2));
|
|
|
|
rl.on('close', common.mustCall(() => {
|
|
assert.ok(hasCacheDef);
|
|
assert.ok(hasHashDef);
|
|
}));
|