Skip to content

Commit f618d87

Browse files
joyeecheungtargos
authored andcommitted
src: refactor coverage connection
- Refactor the C++ class to be resuable for other types of profiles - Move the try-catch block around coverage collection callback to be inside the callback to silence potential JSON or write errors. - Use Function::Call instead of MakeCallback to call the coverage message callback since it does not actually need async hook handling. This way we no longer needs to disable the async hooks when writing the coverage results. - Renames `lib/internal/coverage-gen/with_profiler.js` to `lib/internal/profiler.js` because it is now the only way to generate coverage. PR-URL: #26513 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Coe <[email protected]>
1 parent beac445 commit f618d87

File tree

11 files changed

+231
-194
lines changed

11 files changed

+231
-194
lines changed

lib/internal/bootstrap/pre_execution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function setupCoverageHooks(dir) {
7878
const {
7979
writeCoverage,
8080
setCoverageDirectory
81-
} = require('internal/coverage-gen/with_profiler');
81+
} = require('internal/profiler');
8282
setCoverageDirectory(coverageDirectory);
8383
process.on('exit', writeCoverage);
8484
process.reallyExit = (code) => {

lib/internal/process/per_thread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function wrapProcessMethods(binding) {
158158
function kill(pid, sig) {
159159
var err;
160160
if (process.env.NODE_V8_COVERAGE) {
161-
const { writeCoverage } = require('internal/coverage-gen/with_profiler');
161+
const { writeCoverage } = require('internal/profiler');
162162
writeCoverage();
163163
}
164164

lib/internal/coverage-gen/with_profiler.js renamed to lib/internal/profiler.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,16 @@ function writeCoverage() {
2121
}
2222

2323
const target = join(coverageDirectory, filename);
24-
try {
25-
disableAllAsyncHooks();
26-
internalBinding('coverage').end((msg) => {
24+
internalBinding('profiler').endCoverage((msg) => {
25+
try {
2726
const coverageInfo = JSON.parse(msg).result;
2827
if (coverageInfo) {
2928
writeFileSync(target, JSON.stringify(coverageInfo));
3029
}
31-
});
32-
} catch (err) {
33-
console.error(err);
34-
}
35-
}
36-
37-
function disableAllAsyncHooks() {
38-
const { getHookArrays } = require('internal/async_hooks');
39-
const [hooks_array] = getHookArrays();
40-
hooks_array.forEach((hook) => { hook.disable(); });
30+
} catch (err) {
31+
console.error(err);
32+
}
33+
});
4134
}
4235

4336
function setCoverageDirectory(dir) {

node.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
'lib/internal/cluster/worker.js',
103103
'lib/internal/console/constructor.js',
104104
'lib/internal/console/global.js',
105-
'lib/internal/coverage-gen/with_profiler.js',
106105
'lib/internal/crypto/certificate.js',
107106
'lib/internal/crypto/cipher.js',
108107
'lib/internal/crypto/diffiehellman.js',
@@ -170,6 +169,7 @@
170169
'lib/internal/process/worker_thread_only.js',
171170
'lib/internal/process/report.js',
172171
'lib/internal/process/task_queues.js',
172+
'lib/internal/profiler.js',
173173
'lib/internal/querystring.js',
174174
'lib/internal/readline.js',
175175
'lib/internal/repl.js',

src/env.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ struct CompileFnEntry {
469469
#define DEBUG_CATEGORY_NAMES(V) \
470470
NODE_ASYNC_PROVIDER_TYPES(V) \
471471
V(INSPECTOR_SERVER) \
472-
V(COVERAGE)
472+
V(INSPECTOR_PROFILER)
473473

474474
enum class DebugCategory {
475475
#define V(name) name,

src/inspector/node_inspector.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
'../../src/inspector_io.cc',
4646
'../../src/inspector_agent.h',
4747
'../../src/inspector_io.h',
48-
'../../src/inspector_coverage.cc',
48+
'../../src/inspector_profiler.cc',
4949
'../../src/inspector_js_api.cc',
5050
'../../src/inspector_socket.cc',
5151
'../../src/inspector_socket.h',

src/inspector_coverage.cc

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)