Skip to content

Commit a3268d7

Browse files
committed
[MERGE #5374 @kfarnung] Copy FunctionBody display names if necessary
Merge pull request #5374 from kfarnung:user/kfarnung/literalstring In cached bytecode scenarios the display name can come directly from the bytecode buffer. Since it's not owned by the engine that data needs to be copied to ensure that the buffer remains valid. OS#17525348
2 parents 246a8a8 + 3b1e661 commit a3268d7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/Runtime/Base/FunctionBody.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,11 +1033,32 @@ namespace Js
10331033
}
10341034
}
10351035

1036+
// Returns a raw pointer to the display name which may not have a well-known lifetime. It's safer to use
1037+
// GetExternalDisplayNameObject if the end goal is to create an object anyway.
10361038
const char16* ParseableFunctionInfo::GetExternalDisplayName() const
10371039
{
10381040
return GetExternalDisplayName(this);
10391041
}
10401042

1043+
// Allocates a new JavascriptString object containing the display name associated with the FunctionBody.
1044+
JavascriptString* ParseableFunctionInfo::GetExternalDisplayNameObject(ScriptContext* scriptContext) const
1045+
{
1046+
const char16* name = GetExternalDisplayName();
1047+
1048+
if (!GetDisplayNameIsRecyclerAllocated() && !IsConstantFunctionName(name))
1049+
{
1050+
// The string is allocated in memory that we don't directly control the lifetime of. Copy the string to
1051+
// ensure that the buffer remains valid for the lifetime of the object.
1052+
return Js::JavascriptString::NewCopySz(name, scriptContext);
1053+
}
1054+
else
1055+
{
1056+
// Use the incoming buffer directly to create the object. This only works when the lifetime of the data is
1057+
// static or GC allocated.
1058+
return Js::JavascriptString::NewWithSz(name, scriptContext);
1059+
}
1060+
}
1061+
10411062
RegSlot
10421063
FunctionBody::GetLocalsCount()
10431064
{

lib/Runtime/Base/FunctionBody.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ namespace Js
10961096
// this is also now being used for function.name.
10971097
const char16* GetShortDisplayName(charcount_t * shortNameLength);
10981098

1099-
bool GetDisplayNameIsRecyclerAllocated() { return m_displayNameIsRecyclerAllocated; }
1099+
bool GetDisplayNameIsRecyclerAllocated() const { return m_displayNameIsRecyclerAllocated; }
11001100

11011101
bool IsJitLoopBodyPhaseEnabled() const
11021102
{
@@ -1678,6 +1678,7 @@ namespace Js
16781678
void CopyNestedArray(ParseableFunctionInfo * other);
16791679

16801680
const char16* GetExternalDisplayName() const;
1681+
JavascriptString* GetExternalDisplayNameObject(ScriptContext* scriptContext) const;
16811682

16821683
//
16831684
// Algorithm to retrieve a function body's external display name. Template supports both

0 commit comments

Comments
 (0)