|
13 | 13 |
|
14 | 14 | #include <cinttypes> |
15 | 15 | #include <limits> |
| 16 | +#include <optional> |
16 | 17 | #include <sstream> |
| 18 | +#include "simdjson.h" |
17 | 19 | #include "simdutf.h" |
18 | 20 |
|
19 | 21 | namespace node { |
@@ -109,6 +111,28 @@ bool StringViewToUTF8(const v8_inspector::StringView& source, |
109 | 111 | return *utf8_length == result_len; |
110 | 112 | } |
111 | 113 |
|
| 114 | +// Return the raw JSON of the profile object in `result`: either the |
| 115 | +// `field` member of `result` or, if `field` is nullptr, `result` itself. |
| 116 | +static std::optional<std::string_view> GetProfile( |
| 117 | + simdjson::ondemand::object* result, const char* field, const char* type) { |
| 118 | + simdjson::ondemand::object profile_object; |
| 119 | + if (field == nullptr) { |
| 120 | + profile_object = *result; |
| 121 | + } else if ((*result)[field].get_object().get(profile_object)) { |
| 122 | + fprintf( |
| 123 | + stderr, "'%s' from %s profile result is not an Object\n", field, type); |
| 124 | + return std::nullopt; |
| 125 | + } |
| 126 | + std::string_view profile_raw; |
| 127 | + if (profile_object.raw_json().get(profile_raw)) { |
| 128 | + fprintf(stderr, |
| 129 | + "Cannot get raw string of the 'profile' field from %s profile\n", |
| 130 | + type); |
| 131 | + return std::nullopt; |
| 132 | + } |
| 133 | + return profile_raw; |
| 134 | +} |
| 135 | + |
112 | 136 | void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend( |
113 | 137 | const v8_inspector::StringView& message) { |
114 | 138 | Environment* env = connection_->env(); |
@@ -134,9 +158,10 @@ void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend( |
134 | 158 | return; |
135 | 159 | } |
136 | 160 |
|
| 161 | + simdjson::ondemand::parser json_parser; |
137 | 162 | simdjson::ondemand::document parsed; |
138 | 163 | simdjson::ondemand::object response; |
139 | | - if (connection_->json_parser_ |
| 164 | + if (json_parser |
140 | 165 | .iterate( |
141 | 166 | message_utf8.data(), message_utf8_length, message_utf8.size()) |
142 | 167 | .get(parsed) || |
@@ -184,7 +209,10 @@ void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend( |
184 | 209 | return; |
185 | 210 | } |
186 | 211 |
|
187 | | - connection_->WriteProfile(&result); |
| 212 | + auto profile = GetProfile(&result, connection_->profile_field(), type); |
| 213 | + if (profile.has_value()) { |
| 214 | + connection_->WriteProfile(*profile); |
| 215 | + } |
188 | 216 | connection_->RemoveProfileId(id); |
189 | 217 | } |
190 | 218 |
|
@@ -214,32 +242,7 @@ std::string V8CoverageConnection::GetFilename() const { |
214 | 242 | env()->thread_id()); |
215 | 243 | } |
216 | 244 |
|
217 | | -std::optional<std::string_view> V8ProfilerConnection::GetProfile( |
218 | | - simdjson::ondemand::object* result) { |
219 | | - simdjson::ondemand::object profile_object; |
220 | | - if ((*result)["profile"].get_object().get(profile_object)) { |
221 | | - fprintf( |
222 | | - stderr, "'profile' from %s profile result is not an Object\n", type()); |
223 | | - return std::nullopt; |
224 | | - } |
225 | | - std::string_view profile_raw; |
226 | | - if (profile_object.raw_json().get(profile_raw)) { |
227 | | - fprintf(stderr, |
228 | | - "Cannot get raw string of the 'profile' field from %s profile\n", |
229 | | - type()); |
230 | | - return std::nullopt; |
231 | | - } |
232 | | - return profile_raw; |
233 | | -} |
234 | | - |
235 | | -void V8ProfilerConnection::WriteProfile(simdjson::ondemand::object* result) { |
236 | | - // Generate the profile output from the subclass. |
237 | | - auto profile_opt = GetProfile(result); |
238 | | - if (!profile_opt.has_value()) { |
239 | | - return; |
240 | | - } |
241 | | - std::string_view profile = profile_opt.value(); |
242 | | - |
| 245 | +void V8ProfilerConnection::WriteProfile(std::string_view profile) { |
243 | 246 | // Create the directory if necessary. |
244 | 247 | std::string directory = GetDirectory(); |
245 | 248 | DCHECK(!directory.empty()); |
@@ -254,7 +257,7 @@ void V8ProfilerConnection::WriteProfile(simdjson::ondemand::object* result) { |
254 | 257 | WriteResult(env_, path.c_str(), profile); |
255 | 258 | } |
256 | 259 |
|
257 | | -void V8CoverageConnection::WriteProfile(simdjson::ondemand::object* result) { |
| 260 | +void V8CoverageConnection::WriteProfile(std::string_view profile) { |
258 | 261 | Isolate* isolate = env_->isolate(); |
259 | 262 | HandleScope handle_scope(isolate); |
260 | 263 |
|
@@ -271,13 +274,6 @@ void V8CoverageConnection::WriteProfile(simdjson::ondemand::object* result) { |
271 | 274 | Local<Context> context = env_->context(); |
272 | 275 | Context::Scope context_scope(context); |
273 | 276 |
|
274 | | - // Generate the profile output from the subclass. |
275 | | - auto profile_opt = GetProfile(result); |
276 | | - if (!profile_opt.has_value()) { |
277 | | - return; |
278 | | - } |
279 | | - std::string_view profile = profile_opt.value(); |
280 | | - |
281 | 277 | // append source-map cache information to coverage object: |
282 | 278 | Local<Value> source_map_cache_v; |
283 | 279 | { |
@@ -349,18 +345,6 @@ void V8CoverageConnection::WriteProfile(simdjson::ondemand::object* result) { |
349 | 345 | } |
350 | 346 | } |
351 | 347 |
|
352 | | -std::optional<std::string_view> V8CoverageConnection::GetProfile( |
353 | | - simdjson::ondemand::object* result) { |
354 | | - std::string_view profile_raw; |
355 | | - if (result->raw_json().get(profile_raw)) { |
356 | | - fprintf(stderr, |
357 | | - "Cannot get raw string of the 'profile' field from %s profile\n", |
358 | | - type()); |
359 | | - return std::nullopt; |
360 | | - } |
361 | | - return profile_raw; |
362 | | -} |
363 | | - |
364 | 348 | std::string V8CoverageConnection::GetDirectory() const { |
365 | 349 | return env()->coverage_directory(); |
366 | 350 | } |
|