-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
process: start coverage collection before bootstrap #26006
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dc2d5e9
process: start coverage collection before bootstrap
joyeecheung 0bf9ae9
fixup! process: start coverage collection before bootstrap
joyeecheung 44ff18b
fixup! fixup! process: start coverage collection before bootstrap
joyeecheung de2845b
fixup! fixup! fixup! process: start coverage collection before bootstrap
joyeecheung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#include "base_object-inl.h" | ||
#include "debug_utils.h" | ||
#include "inspector_agent.h" | ||
#include "node_internals.h" | ||
#include "v8-inspector.h" | ||
|
||
namespace node { | ||
namespace coverage { | ||
|
||
using v8::Context; | ||
using v8::Function; | ||
using v8::FunctionCallbackInfo; | ||
using v8::HandleScope; | ||
using v8::Isolate; | ||
using v8::Local; | ||
using v8::MaybeLocal; | ||
using v8::NewStringType; | ||
using v8::Object; | ||
using v8::ObjectTemplate; | ||
using v8::String; | ||
using v8::Value; | ||
|
||
using v8_inspector::StringBuffer; | ||
using v8_inspector::StringView; | ||
|
||
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate, | ||
Local<Value> value) { | ||
TwoByteValue buffer(isolate, value); | ||
return StringBuffer::create(StringView(*buffer, buffer.length())); | ||
} | ||
|
||
class V8CoverageConnection : public BaseObject { | ||
public: | ||
class V8CoverageSessionDelegate : public inspector::InspectorSessionDelegate { | ||
public: | ||
explicit V8CoverageSessionDelegate(V8CoverageConnection* connection) | ||
: connection_(connection) {} | ||
|
||
void SendMessageToFrontend( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, seems like a huge benefit to move coverage standup and teardown into C++. |
||
const v8_inspector::StringView& message) override { | ||
Environment* env = connection_->env(); | ||
Local<Function> fn = connection_->env()->on_coverage_message_function(); | ||
bool ending = !fn.IsEmpty(); | ||
Debug(env, | ||
DebugCategory::COVERAGE, | ||
"Sending message to frontend, ending = %s\n", | ||
ending ? "true" : "false"); | ||
if (!ending) { | ||
return; | ||
} | ||
Isolate* isolate = env->isolate(); | ||
|
||
HandleScope handle_scope(isolate); | ||
Context::Scope context_scope(env->context()); | ||
MaybeLocal<String> v8string = | ||
String::NewFromTwoByte(isolate, | ||
message.characters16(), | ||
NewStringType::kNormal, | ||
message.length()); | ||
Local<Value> args[] = {v8string.ToLocalChecked().As<Value>()}; | ||
USE(MakeCallback(isolate, | ||
connection_->object(), | ||
fn, | ||
arraysize(args), | ||
args, | ||
async_context{0, 0})); | ||
} | ||
|
||
private: | ||
V8CoverageConnection* connection_; | ||
}; | ||
|
||
SET_MEMORY_INFO_NAME(V8CoverageConnection) | ||
SET_SELF_SIZE(V8CoverageConnection) | ||
|
||
void MemoryInfo(MemoryTracker* tracker) const override { | ||
tracker->TrackFieldWithSize( | ||
"session", sizeof(*session_), "InspectorSession"); | ||
} | ||
|
||
explicit V8CoverageConnection(Environment* env) | ||
: BaseObject(env, env->coverage_connection()), session_(nullptr) { | ||
inspector::Agent* inspector = env->inspector_agent(); | ||
std::unique_ptr<inspector::InspectorSession> session = inspector->Connect( | ||
std::make_unique<V8CoverageSessionDelegate>(this), false); | ||
session_ = std::move(session); | ||
MakeWeak(); | ||
} | ||
|
||
void Start() { | ||
Debug(this->env(), | ||
DebugCategory::COVERAGE, | ||
"Sending Profiler.startPreciseCoverage\n"); | ||
Isolate* isolate = this->env()->isolate(); | ||
Local<Value> enable = FIXED_ONE_BYTE_STRING( | ||
isolate, "{\"id\": 1, \"method\": \"Profiler.enable\"}"); | ||
Local<Value> start = FIXED_ONE_BYTE_STRING( | ||
isolate, | ||
"{" | ||
"\"id\": 2," | ||
"\"method\": \"Profiler.startPreciseCoverage\"," | ||
"\"params\": {\"callCount\": true, \"detailed\": true}" | ||
"}"); | ||
session_->Dispatch(ToProtocolString(isolate, enable)->string()); | ||
session_->Dispatch(ToProtocolString(isolate, start)->string()); | ||
} | ||
|
||
void End() { | ||
Debug(this->env(), | ||
DebugCategory::COVERAGE, | ||
"Sending Profiler.takePreciseCoverage\n"); | ||
Isolate* isolate = this->env()->isolate(); | ||
Local<Value> end = | ||
FIXED_ONE_BYTE_STRING(isolate, | ||
"{" | ||
"\"id\": 3," | ||
"\"method\": \"Profiler.takePreciseCoverage\"" | ||
"}"); | ||
session_->Dispatch(ToProtocolString(isolate, end)->string()); | ||
} | ||
|
||
friend class V8CoverageSessionDelegate; | ||
|
||
private: | ||
std::unique_ptr<inspector::InspectorSession> session_; | ||
}; | ||
|
||
bool StartCoverageCollection(Environment* env) { | ||
HandleScope scope(env->isolate()); | ||
|
||
Local<ObjectTemplate> t = ObjectTemplate::New(env->isolate()); | ||
t->SetInternalFieldCount(1); | ||
Local<Object> obj; | ||
if (!t->NewInstance(env->context()).ToLocal(&obj)) { | ||
return false; | ||
} | ||
|
||
obj->SetAlignedPointerInInternalField(0, nullptr); | ||
|
||
CHECK(env->coverage_connection().IsEmpty()); | ||
env->set_coverage_connection(obj); | ||
V8CoverageConnection* connection = new V8CoverageConnection(env); | ||
connection->Start(); | ||
return true; | ||
} | ||
|
||
static void EndCoverageCollection(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
CHECK(args[0]->IsFunction()); | ||
Debug(env, DebugCategory::COVERAGE, "Ending coverage collection\n"); | ||
env->set_on_coverage_message_function(args[0].As<Function>()); | ||
V8CoverageConnection* connection = | ||
Unwrap<V8CoverageConnection>(env->coverage_connection()); | ||
CHECK_NOT_NULL(connection); | ||
connection->End(); | ||
} | ||
|
||
static void Initialize(Local<Object> target, | ||
Local<Value> unused, | ||
Local<Context> context, | ||
void* priv) { | ||
Environment* env = Environment::GetCurrent(context); | ||
env->SetMethod(target, "end", EndCoverageCollection); | ||
} | ||
} // namespace coverage | ||
} // namespace node | ||
|
||
NODE_MODULE_CONTEXT_AWARE_INTERNAL(coverage, node::coverage::Initialize) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems to be async now? should the try/catch be moved to the callback?