From 89e859fb2b5cf5c30aca3492fdb6e98fa87950a3 Mon Sep 17 00:00:00 2001 From: kozyatinskiy Date: Thu, 19 Nov 2015 11:32:31 -0800 Subject: [PATCH] [V8] Unify get function name for debugging purpose Following logic is using for getting function name in JSFunction::GetDebugName: 1. if function has displayName and its type is string then use it 2. if function has defined property Function.name as value and its type string then use it 3. otherwise use SharedFunctionInfo::DebugName as functionName. JSFunction::GetDebugName is exposed in V8 API and in FunctionMirror interface. BUG=chromium:17356 R=yangguo@chromium.org,mstarzinger@chromium.org LOG=Y Review URL: https://codereview.chromium.org/1449473005 Cr-Commit-Position: refs/heads/master@{#32124} --- include/v8.h | 6 ++ src/api.cc | 12 ++++ src/debug/mirrors.js | 10 +++ src/heap/heap.h | 1 + src/messages.cc | 3 +- src/objects.cc | 11 ++- src/objects.h | 5 ++ src/runtime/runtime-debug.cc | 10 +++ src/runtime/runtime.h | 1 + test/cctest/test-api.cc | 134 +++++++++++++++++++++++++++++++++++ 10 files changed, 191 insertions(+), 2 deletions(-) diff --git a/include/v8.h b/include/v8.h index 0761a20187..53db6ea6a0 100644 --- a/include/v8.h +++ b/include/v8.h @@ -3251,6 +3251,12 @@ class V8_EXPORT Function : public Object { */ Local GetInferredName() const; + /** + * displayName if it is set, otherwise name if it is configured, otherwise + * function name, otherwise inferred name. + */ + Local GetDebugName() const; + /** * User-defined name assigned to the "displayName" property of this function. * Used to facilitate debugging and profiling of JavaScript code. diff --git a/src/api.cc b/src/api.cc index fde2326206..1d679dfc21 100644 --- a/src/api.cc +++ b/src/api.cc @@ -4465,6 +4465,18 @@ Local Function::GetInferredName() const { } +Local Function::GetDebugName() const { + auto self = Utils::OpenHandle(this); + if (!self->IsJSFunction()) { + return ToApiHandle( + self->GetIsolate()->factory()->undefined_value()); + } + auto func = i::Handle::cast(self); + i::Handle name = i::JSFunction::GetDebugName(func); + return Utils::ToLocal(i::Handle(*name, name->GetIsolate())); +} + + Local Function::GetDisplayName() const { i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); ENTER_V8(isolate); diff --git a/src/debug/mirrors.js b/src/debug/mirrors.js index 5ff3e34955..f205fed4fa 100644 --- a/src/debug/mirrors.js +++ b/src/debug/mirrors.js @@ -986,6 +986,16 @@ FunctionMirror.prototype.name = function() { }; +/** + * Returns the displayName if it is set, otherwise name, otherwise inferred + * name. + * @return {string} Name of the function + */ +FunctionMirror.prototype.debugName = function() { + return %FunctionGetDebugName(this.value_); +} + + /** * Returns the inferred name of the function. * @return {string} Name of the function diff --git a/src/heap/heap.h b/src/heap/heap.h index fe51e0b0e8..071681076f 100644 --- a/src/heap/heap.h +++ b/src/heap/heap.h @@ -239,6 +239,7 @@ namespace internal { V(default_string, "default") \ V(defineProperty_string, "defineProperty") \ V(deleteProperty_string, "deleteProperty") \ + V(display_name_string, "displayName") \ V(done_string, "done") \ V(dot_result_string, ".result") \ V(dot_string, ".") \ diff --git a/src/messages.cc b/src/messages.cc index 27ee8e4334..706fee43ee 100644 --- a/src/messages.cc +++ b/src/messages.cc @@ -168,8 +168,9 @@ Handle CallSite::GetFileName() { Handle CallSite::GetFunctionName() { - Handle result = JSFunction::GetDebugName(fun_); + Handle result = JSFunction::GetName(fun_); if (result->length() != 0) return result; + Handle script(fun_->shared()->script(), isolate_); if (script->IsScript() && Handle