Skip to content

Commit

Permalink
[MERGE #3466 @obastemur] ToStringTagHelper: Make sure CompoundString …
Browse files Browse the repository at this point in the history
…has enough capacity

Merge pull request #3466 from obastemur:tag_str_fix

Resize / Grow is expensive. Initialize CompoundString with correct capacity.
  • Loading branch information
obastemur committed Aug 4, 2017
2 parents 4559734 + 3508850 commit 5dd3ed0
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/Runtime/Library/JavascriptObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ namespace Js
CrossSite::ForceCrossSiteThunkOnPrototypeChain(newPrototype);
return proxy->SetPrototypeTrap(newPrototype, shouldThrow, scriptContext);
}

// 2. Let extensible be the value of the [[Extensible]] internal data property of O.
// 3. Let current be the value of the [[Prototype]] internal data property of O.
// 4. If SameValue(V, current), then return true.
Expand Down Expand Up @@ -383,18 +383,19 @@ namespace Js
// 15. Let tag be ? Get(O, @@toStringTag).
Var tag = JavascriptOperators::GetProperty(thisArgAsObject, PropertyIds::_symbolToStringTag, scriptContext); // Let tag be the result of Get(O, @@toStringTag).

// 17. Return the String that is the result of concatenating "[object ", tag, and "]".
// 17. Return the String that is the result of concatenating "[object ", tag, and "]".
auto buildToString = [&scriptContext](Var tag) {
JavascriptString *tagStr = JavascriptString::FromVar(tag);
const WCHAR objectStartString[9] = _u("[object ");
const WCHAR objectEndString[1] = { _u(']') };
CompoundString *const cs = CompoundString::NewWithCharCapacity(_countof(objectStartString)
+ _countof(objectEndString) + tagStr->GetLength(), scriptContext->GetLibrary());

CompoundString::Builder<32> stringBuilder(scriptContext);

stringBuilder.AppendChars(_u("[object "));

stringBuilder.AppendChars(tagStr);
stringBuilder.AppendChars(_u(']'));
cs->AppendChars(objectStartString, _countof(objectStartString) - 1 /* ditch \0 */);
cs->AppendChars(tagStr);
cs->AppendChars(objectEndString, _countof(objectEndString));

return stringBuilder.ToString();
return cs;
};
if (tag != nullptr && JavascriptString::Is(tag))
{
Expand Down Expand Up @@ -652,7 +653,7 @@ namespace Js

JavascriptArray* ownPropertyKeys = JavascriptOperators::GetOwnPropertyKeys(obj, scriptContext);
RecyclableObject* resultObj = scriptContext->GetLibrary()->CreateObject(true, (Js::PropertyIndex) ownPropertyKeys->GetLength());

PropertyDescriptor propDesc;
Var propKey = nullptr;

Expand All @@ -665,7 +666,7 @@ namespace Js
{
continue;
}

PropertyRecord const * propertyRecord;
JavascriptConversion::ToPropertyKey(propKey, scriptContext, &propertyRecord);

Expand Down

0 comments on commit 5dd3ed0

Please sign in to comment.