Skip to content

Commit 7cfe84a

Browse files
committed
[MERGE chakra-core#914] Function.name on the getter/setter on defineProperty.
Merge pull request chakra-core#914 from akroshg:name For getter/setter function in the object literal we were initially giving them get/set name respectively. However if they appear in the defineProperty we were changing them for diagnostics purpose. In the diagnostics side we were expected to give <propname>.get but due to function.name we have changed it to "get <propname>"., which is not needed and not consistent. This change is removing that and putting the <propname>.get as full name for the current function. The short name will now automatically be given as get/set correctly.
2 parents 63febdb + f928841 commit 7cfe84a

4 files changed

Lines changed: 23 additions & 55 deletions

File tree

lib/Runtime/Library/JavascriptObject.cpp

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,61 +2046,27 @@ namespace Js
20462046
return finalName;
20472047
}
20482048

2049-
/*static*/
2050-
char16 * JavascriptObject::ConstructAccessorNameES6(const PropertyRecord * propertyRecord, const char16 * getOrSetStr, ScriptContext* scriptContext)
2051-
{
2052-
Assert(propertyRecord);
2053-
Assert(scriptContext);
2054-
char16 * finalName = nullptr;
2055-
size_t propertyLength = static_cast<size_t>(propertyRecord->GetLength() + 1); //+ 1 (for null terminator)
2056-
if (propertyLength > 0)
2057-
{
2058-
size_t totalChars;
2059-
const size_t getSetLength = 4; // 4 = 3 (get or set) +1 (for space)
2060-
if (SizeTAdd(propertyLength, getSetLength, &totalChars) == S_OK)
2061-
{
2062-
finalName = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), char16, totalChars);
2063-
Assert(finalName != nullptr);
2064-
2065-
Assert(getOrSetStr != nullptr);
2066-
Assert(wcslen(getOrSetStr) == 4);
2067-
wcscpy_s(finalName, totalChars, getOrSetStr);
2068-
2069-
const char16* propertyName = propertyRecord->GetBuffer();
2070-
Assert(propertyName != nullptr);
2071-
js_wmemcpy_s(finalName + getSetLength, propertyLength, propertyName, propertyLength);
2072-
2073-
}
2074-
}
2075-
return finalName;
2076-
}
2077-
20782049
/*static*/
20792050
void JavascriptObject::ModifyGetterSetterFuncName(const PropertyRecord * propertyRecord, const PropertyDescriptor& descriptor, ScriptContext* scriptContext)
20802051
{
20812052
Assert(scriptContext);
20822053
Assert(propertyRecord);
20832054
if (descriptor.GetterSpecified() || descriptor.SetterSpecified())
20842055
{
2056+
charcount_t propertyLength = propertyRecord->GetLength();
2057+
20852058
if (descriptor.GetterSpecified()
20862059
&& Js::ScriptFunction::Is(descriptor.GetGetter())
20872060
&& _wcsicmp(Js::ScriptFunction::FromVar(descriptor.GetGetter())->GetFunctionProxy()->GetDisplayName(), _u("get")) == 0)
20882061
{
20892062
// modify to name.get
2090-
char16* finalName;
2091-
if (scriptContext->GetConfig()->IsES6FunctionNameEnabled())
2092-
{
2093-
finalName = ConstructAccessorNameES6(propertyRecord, _u("get "), scriptContext);
2094-
}
2095-
else
2096-
{
2097-
finalName = ConstructName(propertyRecord, _u(".get"), scriptContext);
2098-
}
2063+
char16* finalName = ConstructName(propertyRecord, _u(".get"), scriptContext);
20992064
if (finalName != nullptr)
21002065
{
21012066
FunctionProxy::SetDisplayNameFlags flags = (FunctionProxy::SetDisplayNameFlags) (FunctionProxy::SetDisplayNameFlagsDontCopy | FunctionProxy::SetDisplayNameFlagsRecyclerAllocated);
21022067

2103-
Js::ScriptFunction::FromVar(descriptor.GetGetter())->GetFunctionProxy()->SetDisplayName(finalName, propertyRecord->GetLength() + 4 /*".get" or "get "*/, flags);
2068+
Js::ScriptFunction::FromVar(descriptor.GetGetter())->GetFunctionProxy()->SetDisplayName(finalName,
2069+
propertyLength + 4 /*".get"*/, propertyLength + 1, flags);
21042070
}
21052071
}
21062072

@@ -2109,21 +2075,13 @@ namespace Js
21092075
&& _wcsicmp(Js::ScriptFunction::FromVar(descriptor.GetSetter())->GetFunctionProxy()->GetDisplayName(), _u("set")) == 0)
21102076
{
21112077
// modify to name.set
2112-
char16* finalName;
2113-
if (scriptContext->GetConfig()->IsES6FunctionNameEnabled())
2114-
{
2115-
finalName = ConstructAccessorNameES6(propertyRecord, _u("set "), scriptContext);
2116-
}
2117-
else
2118-
{
2119-
finalName = ConstructName(propertyRecord, _u(".set"), scriptContext);
2120-
}
2121-
2078+
char16* finalName = ConstructName(propertyRecord, _u(".set"), scriptContext);
21222079
if (finalName != nullptr)
21232080
{
21242081
FunctionProxy::SetDisplayNameFlags flags = (FunctionProxy::SetDisplayNameFlags) (FunctionProxy::SetDisplayNameFlagsDontCopy | FunctionProxy::SetDisplayNameFlagsRecyclerAllocated);
21252082

2126-
Js::ScriptFunction::FromVar(descriptor.GetSetter())->GetFunctionProxy()->SetDisplayName(finalName, propertyRecord->GetLength() + 4 /*".set" or "set "*/, flags);
2083+
Js::ScriptFunction::FromVar(descriptor.GetSetter())->GetFunctionProxy()->SetDisplayName(finalName,
2084+
propertyLength + 4 /*".set"*/, propertyLength + 1, flags);
21272085
}
21282086
}
21292087
}

lib/Runtime/Library/JavascriptObject.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ namespace Js
115115

116116
static void ModifyGetterSetterFuncName(const PropertyRecord * propertyRecord, const PropertyDescriptor& descriptor, ScriptContext* scriptContext);
117117
static char16 * ConstructName(const PropertyRecord * propertyRecord, const char16 * getOrSetStr, ScriptContext* scriptContext);
118-
static char16 * ConstructAccessorNameES6(const PropertyRecord * propertyRecord, const char16 * getOrSetStr, ScriptContext* scriptContext);
119118
static Var DefinePropertiesHelper(RecyclableObject* object, RecyclableObject* properties, ScriptContext* scriptContext);
120119
static Var DefinePropertiesHelperForGenericObjects(RecyclableObject* object, RecyclableObject* properties, ScriptContext* scriptContext);
121120
static Var DefinePropertiesHelperForProxyObjects(RecyclableObject* object, RecyclableObject* properties, ScriptContext* scriptContext);

test/StackTrace/StackTraceLimit.baseline

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,10 @@ Error: Out of stack space
267267

268268
--Throw new Error() in getter for a number of times
269269
Error: My error in custom stackTraceLimit getter
270-
at get stackTraceLimit (stacktracelimit.js:94:13)
271-
at get stackTraceLimit (stacktracelimit.js:94:13)
272-
at get stackTraceLimit (stacktracelimit.js:94:13)
273-
at get stackTraceLimit (stacktracelimit.js:94:13)
270+
at stackTraceLimit.get (stacktracelimit.js:94:13)
271+
at stackTraceLimit.get (stacktracelimit.js:94:13)
272+
at stackTraceLimit.get (stacktracelimit.js:94:13)
273+
at stackTraceLimit.get (stacktracelimit.js:94:13)
274274
at throwException (longcallstackthrow.js:33:5)
275275
at throwExceptionWithCatch (longcallstackthrow.js:22:9)
276276
at Anonymous function (longcallstackthrow.js:45:17)

test/es6/function.name.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,17 @@ var tests = [
750750
class C { foo(){} };
751751
assert.areEqual("foo",(new C).foo.name);
752752
}
753+
},
754+
{
755+
name: "Getter and setter have correct name in defineProperty",
756+
body: function()
757+
{
758+
var obj = {};
759+
Object.defineProperty(obj, 'test', {get : function () {}, set : function () {} });
760+
var desc = Object.getOwnPropertyDescriptor(obj, 'test');
761+
assert.areEqual("get", desc.get.name);
762+
assert.areEqual("set", desc.set.name);
763+
}
753764
}
754765

755766
];

0 commit comments

Comments
 (0)