|
| 1 | +#include "base_object-inl.h" |
| 2 | +#include "debug_utils.h" |
| 3 | +#include "inspector_agent.h" |
| 4 | +#include "node_internals.h" |
| 5 | +#include "v8-inspector.h" |
| 6 | + |
| 7 | +namespace node { |
| 8 | +namespace coverage { |
| 9 | + |
| 10 | +using v8::Context; |
| 11 | +using v8::Function; |
| 12 | +using v8::FunctionCallbackInfo; |
| 13 | +using v8::HandleScope; |
| 14 | +using v8::Isolate; |
| 15 | +using v8::Local; |
| 16 | +using v8::MaybeLocal; |
| 17 | +using v8::NewStringType; |
| 18 | +using v8::Object; |
| 19 | +using v8::ObjectTemplate; |
| 20 | +using v8::String; |
| 21 | +using v8::Value; |
| 22 | + |
| 23 | +using v8_inspector::StringBuffer; |
| 24 | +using v8_inspector::StringView; |
| 25 | + |
| 26 | +std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate, |
| 27 | + Local<Value> value) { |
| 28 | + TwoByteValue buffer(isolate, value); |
| 29 | + return StringBuffer::create(StringView(*buffer, buffer.length())); |
| 30 | +} |
| 31 | + |
| 32 | +class V8CoverageConnection : public BaseObject { |
| 33 | + public: |
| 34 | + class V8CoverageSessionDelegate : public inspector::InspectorSessionDelegate { |
| 35 | + public: |
| 36 | + explicit V8CoverageSessionDelegate(V8CoverageConnection* connection) |
| 37 | + : connection_(connection) {} |
| 38 | + |
| 39 | + void SendMessageToFrontend( |
| 40 | + const v8_inspector::StringView& message) override { |
| 41 | + Environment* env = connection_->env(); |
| 42 | + Local<Function> fn = connection_->env()->on_coverage_message_function(); |
| 43 | + bool ending = !fn.IsEmpty(); |
| 44 | + Debug(env, |
| 45 | + DebugCategory::COVERAGE, |
| 46 | + "Sending message to frontend, ending = %s\n", |
| 47 | + ending ? "true" : "false"); |
| 48 | + if (!ending) { |
| 49 | + return; |
| 50 | + } |
| 51 | + Isolate* isolate = env->isolate(); |
| 52 | + |
| 53 | + HandleScope handle_scope(isolate); |
| 54 | + Context::Scope context_scope(env->context()); |
| 55 | + MaybeLocal<String> v8string = |
| 56 | + String::NewFromTwoByte(isolate, |
| 57 | + message.characters16(), |
| 58 | + NewStringType::kNormal, |
| 59 | + message.length()); |
| 60 | + Local<Value> args[] = {v8string.ToLocalChecked().As<Value>()}; |
| 61 | + USE(MakeCallback(isolate, |
| 62 | + connection_->object(), |
| 63 | + fn, |
| 64 | + arraysize(args), |
| 65 | + args, |
| 66 | + async_context{0, 0})); |
| 67 | + } |
| 68 | + |
| 69 | + private: |
| 70 | + V8CoverageConnection* connection_; |
| 71 | + }; |
| 72 | + |
| 73 | + SET_MEMORY_INFO_NAME(V8CoverageConnection) |
| 74 | + SET_SELF_SIZE(V8CoverageConnection) |
| 75 | + |
| 76 | + void MemoryInfo(MemoryTracker* tracker) const override { |
| 77 | + tracker->TrackFieldWithSize( |
| 78 | + "session", sizeof(*session_), "InspectorSession"); |
| 79 | + } |
| 80 | + |
| 81 | + explicit V8CoverageConnection(Environment* env) |
| 82 | + : BaseObject(env, env->coverage_connection()), session_(nullptr) { |
| 83 | + inspector::Agent* inspector = env->inspector_agent(); |
| 84 | + std::unique_ptr<inspector::InspectorSession> session = inspector->Connect( |
| 85 | + std::make_unique<V8CoverageSessionDelegate>(this), false); |
| 86 | + session_ = std::move(session); |
| 87 | + MakeWeak(); |
| 88 | + } |
| 89 | + |
| 90 | + void Start() { |
| 91 | + Debug(this->env(), |
| 92 | + DebugCategory::COVERAGE, |
| 93 | + "Sending Profiler.startPreciseCoverage\n"); |
| 94 | + Isolate* isolate = this->env()->isolate(); |
| 95 | + Local<Value> enable = FIXED_ONE_BYTE_STRING( |
| 96 | + isolate, "{\"id\": 1, \"method\": \"Profiler.enable\"}"); |
| 97 | + Local<Value> start = FIXED_ONE_BYTE_STRING( |
| 98 | + isolate, |
| 99 | + "{" |
| 100 | + "\"id\": 2," |
| 101 | + "\"method\": \"Profiler.startPreciseCoverage\"," |
| 102 | + "\"params\": {\"callCount\": true, \"detailed\": true}" |
| 103 | + "}"); |
| 104 | + session_->Dispatch(ToProtocolString(isolate, enable)->string()); |
| 105 | + session_->Dispatch(ToProtocolString(isolate, start)->string()); |
| 106 | + } |
| 107 | + |
| 108 | + void End() { |
| 109 | + Debug(this->env(), |
| 110 | + DebugCategory::COVERAGE, |
| 111 | + "Sending Profiler.takePreciseCoverage\n"); |
| 112 | + Isolate* isolate = this->env()->isolate(); |
| 113 | + Local<Value> end = |
| 114 | + FIXED_ONE_BYTE_STRING(isolate, |
| 115 | + "{" |
| 116 | + "\"id\": 3," |
| 117 | + "\"method\": \"Profiler.takePreciseCoverage\"" |
| 118 | + "}"); |
| 119 | + session_->Dispatch(ToProtocolString(isolate, end)->string()); |
| 120 | + } |
| 121 | + |
| 122 | + friend class V8CoverageSessionDelegate; |
| 123 | + |
| 124 | + private: |
| 125 | + std::unique_ptr<inspector::InspectorSession> session_; |
| 126 | +}; |
| 127 | + |
| 128 | +bool StartCoverageCollection(Environment* env) { |
| 129 | + HandleScope scope(env->isolate()); |
| 130 | + |
| 131 | + Local<ObjectTemplate> t = ObjectTemplate::New(env->isolate()); |
| 132 | + t->SetInternalFieldCount(1); |
| 133 | + Local<Object> obj; |
| 134 | + if (!t->NewInstance(env->context()).ToLocal(&obj)) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + obj->SetAlignedPointerInInternalField(0, nullptr); |
| 139 | + |
| 140 | + CHECK(env->coverage_connection().IsEmpty()); |
| 141 | + env->set_coverage_connection(obj); |
| 142 | + V8CoverageConnection* connection = new V8CoverageConnection(env); |
| 143 | + connection->Start(); |
| 144 | + return true; |
| 145 | +} |
| 146 | + |
| 147 | +static void EndCoverageCollection(const FunctionCallbackInfo<Value>& args) { |
| 148 | + Environment* env = Environment::GetCurrent(args); |
| 149 | + CHECK(args[0]->IsFunction()); |
| 150 | + Debug(env, DebugCategory::COVERAGE, "Ending coverage collection\n"); |
| 151 | + env->set_on_coverage_message_function(args[0].As<Function>()); |
| 152 | + V8CoverageConnection* connection = |
| 153 | + Unwrap<V8CoverageConnection>(env->coverage_connection()); |
| 154 | + CHECK_NOT_NULL(connection); |
| 155 | + connection->End(); |
| 156 | +} |
| 157 | + |
| 158 | +static void Initialize(Local<Object> target, |
| 159 | + Local<Value> unused, |
| 160 | + Local<Context> context, |
| 161 | + void* priv) { |
| 162 | + Environment* env = Environment::GetCurrent(context); |
| 163 | + env->SetMethod(target, "end", EndCoverageCollection); |
| 164 | +} |
| 165 | +} // namespace coverage |
| 166 | +} // namespace node |
| 167 | + |
| 168 | +NODE_MODULE_CONTEXT_AWARE_INTERNAL(coverage, node::coverage::Initialize) |
0 commit comments