Skip to content

Commit ec73e08

Browse files
alexkozyCommit bot
authored andcommitted
[V8] Use Function.name in Error.stack
Error.stack contains function.name if its type is string. Otherwise if function have inferred name then .stack contains it. For functions from eval .stack property contains "eval". LOG=N BUG=chromium:17356 R=yurys@chromium.org Review URL: https://codereview.chromium.org/919653002 Cr-Commit-Position: refs/heads/master@{#27186}
1 parent c675217 commit ec73e08

7 files changed

Lines changed: 61 additions & 12 deletions

File tree

src/isolate.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,6 @@ class CaptureStackTraceHelper {
483483
if (options & StackTrace::kFunctionName) {
484484
function_key_ = factory()->InternalizeOneByteString(
485485
STATIC_CHAR_VECTOR("functionName"));
486-
name_key_ =
487-
factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("name"));
488486
}
489487
if (options & StackTrace::kIsEval) {
490488
eval_key_ =
@@ -546,9 +544,7 @@ class CaptureStackTraceHelper {
546544
}
547545

548546
if (!function_key_.is_null()) {
549-
Handle<Object> fun_name = JSObject::GetDataProperty(fun, name_key_);
550-
if (!fun_name->IsString())
551-
fun_name = Handle<Object>(fun->shared()->DebugName(), isolate_);
547+
Handle<Object> fun_name = JSFunction::GetDebugName(fun);
552548
JSObject::AddProperty(stack_frame, function_key_, fun_name, NONE);
553549
}
554550

@@ -579,7 +575,6 @@ class CaptureStackTraceHelper {
579575
Handle<String> function_key_;
580576
Handle<String> eval_key_;
581577
Handle<String> constructor_key_;
582-
Handle<String> name_key_;
583578
};
584579

585580

src/messages.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -833,16 +833,13 @@ function CallSiteGetFunction() {
833833

834834
function CallSiteGetFunctionName() {
835835
// See if the function knows its own name
836-
var name = GET_PRIVATE(this, CallSiteFunctionKey).name;
837-
if (name) {
838-
return name;
839-
}
840-
name = %FunctionGetInferredName(GET_PRIVATE(this, CallSiteFunctionKey));
836+
var fun = GET_PRIVATE(this, CallSiteFunctionKey);
837+
var name = %FunctionGetDebugName(fun);
841838
if (name) {
842839
return name;
843840
}
844841
// Maybe this is an evaluation?
845-
var script = %FunctionGetScript(GET_PRIVATE(this, CallSiteFunctionKey));
842+
var script = %FunctionGetScript(fun);
846843
if (script && script.compilation_type == COMPILATION_TYPE_EVAL) {
847844
return "eval";
848845
}

src/objects.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10327,6 +10327,15 @@ bool JSFunction::PassesFilter(const char* raw_filter) {
1032710327
}
1032810328

1032910329

10330+
Handle<String> JSFunction::GetDebugName(Handle<JSFunction> function) {
10331+
Isolate* isolate = function->GetIsolate();
10332+
Handle<Object> name =
10333+
JSObject::GetDataProperty(function, isolate->factory()->name_string());
10334+
if (name->IsString()) return Handle<String>::cast(name);
10335+
return handle(function->shared()->DebugName(), isolate);
10336+
}
10337+
10338+
1033010339
void Oddball::Initialize(Isolate* isolate,
1033110340
Handle<Oddball> oddball,
1033210341
const char* to_string,

src/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7605,6 +7605,10 @@ class JSFunction: public JSObject {
76057605
// Used for flags such as --hydrogen-filter.
76067606
bool PassesFilter(const char* raw_filter);
76077607

7608+
// The function's name if it is configured, otherwise shared function info
7609+
// debug name.
7610+
static Handle<String> GetDebugName(Handle<JSFunction> function);
7611+
76087612
// Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
76097613
// kSize) is weak and has special handling during garbage collection.
76107614
static const int kCodeEntryOffset = JSObject::kHeaderSize;

src/runtime/runtime-debug.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,6 +2613,15 @@ RUNTIME_FUNCTION(Runtime_FunctionGetInferredName) {
26132613
}
26142614

26152615

2616+
RUNTIME_FUNCTION(Runtime_FunctionGetDebugName) {
2617+
HandleScope scope(isolate);
2618+
DCHECK(args.length() == 1);
2619+
2620+
CONVERT_ARG_HANDLE_CHECKED(JSFunction, f, 0);
2621+
return *JSFunction::GetDebugName(f);
2622+
}
2623+
2624+
26162625
// A testing entry. Returns statement position which is the closest to
26172626
// source_position.
26182627
RUNTIME_FUNCTION(Runtime_GetFunctionCodePositionFromSource) {

src/runtime/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ namespace internal {
564564
F(DebugDisassembleFunction, 1, 1) \
565565
F(DebugDisassembleConstructor, 1, 1) \
566566
F(FunctionGetInferredName, 1, 1) \
567+
F(FunctionGetDebugName, 1, 1) \
567568
F(LiveEditFindSharedFunctionInfosForScript, 1, 1) \
568569
F(LiveEditGatherCompileInfo, 2, 1) \
569570
F(LiveEditReplaceScript, 3, 1) \

test/mjsunit/stack-traces.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,37 @@ function testAnonymousMethod() {
9494
(function () { FAIL }).call([1, 2, 3]);
9595
}
9696

97+
function testFunctionName() {
98+
function gen(name, counter) {
99+
var f = function foo() {
100+
if (counter === 0) {
101+
FAIL;
102+
}
103+
gen(name, counter - 1)();
104+
}
105+
if (counter === 4) {
106+
Object.defineProperty(f, 'name', {get: function(){ throw 239; }});
107+
} else if (counter == 3) {
108+
Object.defineProperty(f, 'name', {value: 'boo' + '_' + counter});
109+
} else {
110+
Object.defineProperty(f, 'name', {writable: true});
111+
if (counter === 2)
112+
f.name = 42;
113+
else
114+
f.name = name + '_' + counter;
115+
}
116+
return f;
117+
}
118+
gen('foo', 4)();
119+
}
120+
121+
function testFunctionInferredName() {
122+
var f = function() {
123+
FAIL;
124+
}
125+
f();
126+
}
127+
97128
function CustomError(message, stripPoint) {
98129
this.message = message;
99130
Error.captureStackTrace(this, stripPoint);
@@ -261,6 +292,9 @@ testTrace("testValue", testValue, ["at Number.causeError"]);
261292
testTrace("testConstructor", testConstructor, ["new Plonk"]);
262293
testTrace("testRenamedMethod", testRenamedMethod, ["Wookie.a$b$c$d [as d]"]);
263294
testTrace("testAnonymousMethod", testAnonymousMethod, ["Array.<anonymous>"]);
295+
testTrace("testFunctionName", testFunctionName,
296+
[" at foo_0 ", " at foo_1", " at foo ", " at boo_3 ", " at foo "]);
297+
testTrace("testFunctionInferredName", testFunctionInferredName, [" at f "]);
264298
testTrace("testDefaultCustomError", testDefaultCustomError,
265299
["hep-hey", "new CustomError"],
266300
["collectStackTrace"]);

0 commit comments

Comments
 (0)