Skip to content

Commit 6b605a1

Browse files
committed
inspector: expose precise coverage start to JS runtime
Add a `startCoverage` method on the `profiler` internal binding so that V8 precise coverage can be enabled after bootstrap. The method is idempotent against the existing bootstrap path (which creates a V8CoverageConnection when NODE_V8_COVERAGE or --experimental-test-coverage is set) and a no-op when the inspector is unavailable, e.g. in the parent process of `--test --test-isolation=process` where workers handle coverage and Environment::should_create_inspector() returns false. Refs: #60023 Signed-off-by: sangwook <rewq5991@gmail.com>
1 parent 2b112e7 commit 6b605a1

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

src/inspector_profiler.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,30 @@ static void SetSourceMapCacheGetter(const FunctionCallbackInfo<Value>& args) {
548548
env->set_source_map_cache_getter(args[0].As<Function>());
549549
}
550550

551+
static void StartCoverage(const FunctionCallbackInfo<Value>& args) {
552+
Environment* env = Environment::GetCurrent(args);
553+
554+
Debug(env,
555+
DebugCategory::INSPECTOR_PROFILER,
556+
"StartCoverage, connection %s nullptr\n",
557+
env->coverage_connection() == nullptr ? "==" : "!=");
558+
559+
if (env->coverage_connection() != nullptr) {
560+
return;
561+
}
562+
563+
// The parent of `--test --test-isolation=process` intentionally has no
564+
// inspector (see Environment::should_create_inspector); workers handle
565+
// coverage themselves. Without an inspector, V8CoverageConnection would
566+
// get a null session and crash on the first DispatchMessage.
567+
if (!env->should_create_inspector()) {
568+
return;
569+
}
570+
571+
env->set_coverage_connection(std::make_unique<V8CoverageConnection>(env));
572+
env->coverage_connection()->Start();
573+
}
574+
551575
static void TakeCoverage(const FunctionCallbackInfo<Value>& args) {
552576
Environment* env = Environment::GetCurrent(args);
553577
V8CoverageConnection* connection = env->coverage_connection();
@@ -601,6 +625,7 @@ static void Initialize(Local<Object> target,
601625
SetMethod(context, target, "setCoverageDirectory", SetCoverageDirectory);
602626
SetMethod(
603627
context, target, "setSourceMapCacheGetter", SetSourceMapCacheGetter);
628+
SetMethod(context, target, "startCoverage", StartCoverage);
604629
SetMethod(context, target, "takeCoverage", TakeCoverage);
605630
SetMethod(context, target, "stopCoverage", StopCoverage);
606631
SetMethod(context, target, "endCoverage", EndCoverage);
@@ -609,6 +634,7 @@ static void Initialize(Local<Object> target,
609634
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
610635
registry->Register(SetCoverageDirectory);
611636
registry->Register(SetSourceMapCacheGetter);
637+
registry->Register(StartCoverage);
612638
registry->Register(TakeCoverage);
613639
registry->Register(StopCoverage);
614640
registry->Register(EndCoverage);

0 commit comments

Comments
 (0)