diff options
author | Benjamin Coe <bencoe@google.com> | 2019-09-20 11:43:02 -0700 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-09-22 00:39:41 +0200 |
commit | 8f06773a8cd73aef37ddc8f68d2c8202f1a8c45a (patch) | |
tree | 9fc55258099209d89e0eebdd2b6a45128da0f928 /src | |
parent | e74f30894c46c94aa1329e8462f811b8d5e54a91 (diff) | |
download | android-node-v8-8f06773a8cd73aef37ddc8f68d2c8202f1a8c45a.tar.gz android-node-v8-8f06773a8cd73aef37ddc8f68d2c8202f1a8c45a.tar.bz2 android-node-v8-8f06773a8cd73aef37ddc8f68d2c8202f1a8c45a.zip |
process: initial SourceMap support via NODE_V8_COVERAGE
PR-URL: https://github.com/nodejs/node/pull/28960
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/env.h | 1 | ||||
-rw-r--r-- | src/inspector_profiler.cc | 60 | ||||
-rw-r--r-- | src/inspector_profiler.h | 8 |
3 files changed, 67 insertions, 2 deletions
@@ -444,6 +444,7 @@ constexpr size_t kFsStatsBufferLength = V(primordials, v8::Object) \ V(promise_reject_callback, v8::Function) \ V(script_data_constructor_function, v8::Function) \ + V(source_map_cache_getter, v8::Function) \ V(tick_callback_function, v8::Function) \ V(timers_callback_function, v8::Function) \ V(tls_wrap_constructor_function, v8::Function) \ diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc index 867f145ff5..1444a36bdf 100644 --- a/src/inspector_profiler.cc +++ b/src/inspector_profiler.cc @@ -180,6 +180,58 @@ void V8ProfilerConnection::WriteProfile(Local<String> message) { if (!GetProfile(result).ToLocal(&profile)) { return; } + + Local<String> result_s; + if (!v8::JSON::Stringify(context, profile).ToLocal(&result_s)) { + fprintf(stderr, "Failed to stringify %s profile result\n", type()); + return; + } + + // Create the directory if necessary. + std::string directory = GetDirectory(); + DCHECK(!directory.empty()); + if (!EnsureDirectory(directory, type())) { + return; + } + + std::string filename = GetFilename(); + DCHECK(!filename.empty()); + std::string path = directory + kPathSeparator + filename; + + WriteResult(env_, path.c_str(), result_s); +} + +void V8CoverageConnection::WriteProfile(Local<String> message) { + Isolate* isolate = env_->isolate(); + Local<Context> context = env_->context(); + HandleScope handle_scope(isolate); + Context::Scope context_scope(context); + + // Get message.result from the response. + Local<Object> result; + if (!ParseProfile(env_, message, type()).ToLocal(&result)) { + return; + } + // Generate the profile output from the subclass. + Local<Object> profile; + if (!GetProfile(result).ToLocal(&profile)) { + return; + } + + // append source-map cache information to coverage object: + Local<Function> source_map_cache_getter = env_->source_map_cache_getter(); + Local<Value> source_map_cache_v; + if (!source_map_cache_getter->Call(env()->context(), + Undefined(isolate), 0, nullptr) + .ToLocal(&source_map_cache_v)) { + return; + } + // Avoid writing to disk if no source-map data: + if (!source_map_cache_v->IsUndefined()) { + profile->Set(context, FIXED_ONE_BYTE_STRING(isolate, "source-map-cache"), + source_map_cache_v); + } + Local<String> result_s; if (!v8::JSON::Stringify(context, profile).ToLocal(&result_s)) { fprintf(stderr, "Failed to stringify %s profile result\n", type()); @@ -385,12 +437,20 @@ static void SetCoverageDirectory(const FunctionCallbackInfo<Value>& args) { env->set_coverage_directory(*directory); } + +static void SetSourceMapCacheGetter(const FunctionCallbackInfo<Value>& args) { + CHECK(args[0]->IsFunction()); + Environment* env = Environment::GetCurrent(args); + env->set_source_map_cache_getter(args[0].As<Function>()); +} + static void Initialize(Local<Object> target, Local<Value> unused, Local<Context> context, void* priv) { Environment* env = Environment::GetCurrent(context); env->SetMethod(target, "setCoverageDirectory", SetCoverageDirectory); + env->SetMethod(target, "setSourceMapCacheGetter", SetSourceMapCacheGetter); } } // namespace profiler diff --git a/src/inspector_profiler.h b/src/inspector_profiler.h index e7d45d7de3..4a21eb3a2d 100644 --- a/src/inspector_profiler.h +++ b/src/inspector_profiler.h @@ -59,13 +59,15 @@ class V8ProfilerConnection { // which will be then written as a JSON. virtual v8::MaybeLocal<v8::Object> GetProfile( v8::Local<v8::Object> result) = 0; + virtual void WriteProfile(v8::Local<v8::String> message); private: size_t next_id() { return id_++; } - void WriteProfile(v8::Local<v8::String> message); std::unique_ptr<inspector::InspectorSession> session_; - Environment* env_ = nullptr; size_t id_ = 1; + + protected: + Environment* env_ = nullptr; }; class V8CoverageConnection : public V8ProfilerConnection { @@ -81,6 +83,8 @@ class V8CoverageConnection : public V8ProfilerConnection { std::string GetDirectory() const override; std::string GetFilename() const override; v8::MaybeLocal<v8::Object> GetProfile(v8::Local<v8::Object> result) override; + void WriteProfile(v8::Local<v8::String> message) override; + void WriteSourceMapCache(); private: std::unique_ptr<inspector::InspectorSession> session_; |