Skip to content

Commit 3a9a41d

Browse files
committed
jsrt: JsObject Delete/Get/Has/OwnP../Set Property
AcmeAIR LTO gain (3%) New JSRT property interface that API user can use without going through convert-to-property-id process. See # 3790 for details. Also added a TODO note; ``` TODO: Can we make PropertyString && LiteralStringWithPropertyStringPtr share the string buffer? ``` Once this PR is merged, there will be an additional PR on node-chakracore end to benefit this new interface.
1 parent 212e128 commit 3a9a41d

File tree

10 files changed

+794
-95
lines changed

10 files changed

+794
-95
lines changed

lib/Common/Codex/Utf8Helper.h

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,24 @@ namespace utf8
8383
return WideStringToNarrow(Allocator::allocate, sourceString, sourceCount, destStringPtr, destCount, allocateCount);
8484
}
8585

86-
///
87-
/// Use the codex library to encode a UTF8 string to UTF16.
88-
/// The caller is responsible for freeing the memory, which is allocated
89-
/// using Allocator.
90-
/// The returned string is null terminated.
91-
///
92-
template <typename AllocatorFunction>
93-
HRESULT NarrowStringToWide(_In_ AllocatorFunction allocator,_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount, size_t* allocateCount = nullptr)
86+
inline HRESULT NarrowStringToWideNoAlloc(_In_ LPCSTR sourceString, size_t sourceCount,
87+
__out_ecount(destBufferCount) LPWSTR destString, size_t destBufferCount, _Out_ size_t* destCount)
9488
{
95-
size_t cbSourceString = sourceCount;
9689
size_t sourceStart = 0;
97-
size_t cbDestString = (sourceCount + 1) * sizeof(WCHAR);
98-
if (cbDestString < sourceCount) // overflow ?
90+
size_t cbSourceString = sourceCount;
91+
92+
if (sourceCount >= MAXUINT32)
9993
{
94+
destString[0] = WCHAR(0);
10095
return E_OUTOFMEMORY;
10196
}
10297

103-
WCHAR* destString = (WCHAR*)allocator(cbDestString);
104-
if (destString == nullptr)
98+
if (destString == nullptr || sourceCount > destBufferCount)
10599
{
106-
return E_OUTOFMEMORY;
100+
destString[0] = WCHAR(0);
101+
return E_INVALIDARG;
107102
}
108103

109-
if (allocateCount != nullptr) *allocateCount = cbDestString;
110-
111104
for (; sourceStart < sourceCount; sourceStart++)
112105
{
113106
const char ch = sourceString[sourceStart];
@@ -123,8 +116,6 @@ namespace utf8
123116
if (sourceStart == sourceCount)
124117
{
125118
*destCount = sourceCount;
126-
destString[sourceCount] = WCHAR(0);
127-
*destStringPtr = destString;
128119
}
129120
else
130121
{
@@ -141,12 +132,43 @@ namespace utf8
141132
utf8::DecodeUnitsIntoAndNullTerminateNoAdvance(remDestString, remSourceString, (LPCUTF8) sourceString + cbSourceString, DecodeOptions::doAllowInvalidWCHARs);
142133
Assert(destString[cchDestString] == 0);
143134
static_assert(sizeof(utf8char_t) == sizeof(char), "Needs to be valid for cast");
144-
*destStringPtr = destString;
145135
*destCount = cchDestString;
146136
}
137+
147138
return S_OK;
148139
}
149140

141+
///
142+
/// Use the codex library to encode a UTF8 string to UTF16.
143+
/// The caller is responsible for freeing the memory, which is allocated
144+
/// using Allocator.
145+
/// The returned string is null terminated.
146+
///
147+
template <typename AllocatorFunction>
148+
HRESULT NarrowStringToWide(_In_ AllocatorFunction allocator,_In_ LPCSTR sourceString,
149+
size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount, size_t* allocateCount = nullptr)
150+
{
151+
size_t cbDestString = (sourceCount + 1) * sizeof(WCHAR);
152+
if (cbDestString < sourceCount) // overflow ?
153+
{
154+
return E_OUTOFMEMORY;
155+
}
156+
157+
WCHAR* destString = (WCHAR*)allocator(cbDestString);
158+
if (destString == nullptr)
159+
{
160+
return E_OUTOFMEMORY;
161+
}
162+
163+
if (allocateCount != nullptr) *allocateCount = cbDestString;
164+
165+
*destStringPtr = destString;
166+
HRESULT result = NarrowStringToWideNoAlloc(sourceString, sourceCount, destString, sourceCount, destCount);
167+
destString[sourceCount] = WCHAR(0);
168+
169+
return result;
170+
}
171+
150172
template <class Allocator>
151173
HRESULT NarrowStringToWide(_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount, size_t* allocateCount = nullptr)
152174
{

lib/Jsrt/ChakraCore.h

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,5 +745,118 @@ CHAKRA_API
745745
_Out_opt_ unsigned int *byteOffset,
746746
_Out_opt_ unsigned int *byteLength);
747747

748+
/// <summary>
749+
/// Gets an object's property.
750+
/// </summary>
751+
/// <remarks>
752+
/// Requires an active script context.
753+
/// </remarks>
754+
/// <param name="object">The object that contains the property.</param>
755+
/// <param name="key">The key (JavascriptString) to the property.</param>
756+
/// <param name="value">The value of the property.</param>
757+
/// <returns>
758+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
759+
/// </returns>
760+
CHAKRA_API
761+
JsObjectGetProperty(
762+
_In_ JsValueRef object,
763+
_In_ JsValueRef key,
764+
_Out_ JsValueRef *value);
765+
766+
/// <summary>
767+
/// Puts an object's property.
768+
/// </summary>
769+
/// <remarks>
770+
/// Requires an active script context.
771+
/// </remarks>
772+
/// <param name="object">The object that contains the property.</param>
773+
/// <param name="key">The key (JavascriptString) to the property.</param>
774+
/// <param name="value">The new value of the property.</param>
775+
/// <param name="useStrictRules">The property set should follow strict mode rules.</param>
776+
/// <returns>
777+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
778+
/// </returns>
779+
CHAKRA_API
780+
JsObjectSetProperty(
781+
_In_ JsValueRef object,
782+
_In_ JsValueRef key,
783+
_In_ JsValueRef value,
784+
_In_ bool useStrictRules);
785+
786+
/// <summary>
787+
/// Determines whether an object has a property.
788+
/// </summary>
789+
/// <remarks>
790+
/// Requires an active script context.
791+
/// </remarks>
792+
/// <param name="object">The object that may contain the property.</param>
793+
/// <param name="key">The key (JavascriptString) to the property.</param>
794+
/// <param name="hasProperty">Whether the object (or a prototype) has the property.</param>
795+
/// <returns>
796+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
797+
/// </returns>
798+
CHAKRA_API
799+
JsObjectHasProperty(
800+
_In_ JsValueRef object,
801+
_In_ JsValueRef key,
802+
_Out_ bool *hasProperty);
803+
804+
/// <summary>
805+
/// Defines a new object's own property from a property descriptor.
806+
/// </summary>
807+
/// <remarks>
808+
/// Requires an active script context.
809+
/// </remarks>
810+
/// <param name="object">The object that has the property.</param>
811+
/// <param name="key">The key (JavascriptString) to the property.</param>
812+
/// <param name="propertyDescriptor">The property descriptor.</param>
813+
/// <param name="result">Whether the property was defined.</param>
814+
/// <returns>
815+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
816+
/// </returns>
817+
CHAKRA_API
818+
JsObjectDefineProperty(
819+
_In_ JsValueRef object,
820+
_In_ JsValueRef key,
821+
_In_ JsValueRef propertyDescriptor,
822+
_Out_ bool *result);
823+
824+
/// <summary>
825+
/// Deletes an object's property.
826+
/// </summary>
827+
/// <remarks>
828+
/// Requires an active script context.
829+
/// </remarks>
830+
/// <param name="object">The object that contains the property.</param>
831+
/// <param name="key">The key (JavascriptString) to the property.</param>
832+
/// <param name="useStrictRules">The property set should follow strict mode rules.</param>
833+
/// <param name="result">Whether the property was deleted.</param>
834+
/// <returns>
835+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
836+
/// </returns>
837+
CHAKRA_API
838+
JsObjectDeleteProperty(
839+
_In_ JsValueRef object,
840+
_In_ JsValueRef key,
841+
_In_ bool useStrictRules,
842+
_Out_ JsValueRef *result);
843+
844+
/// <summary>
845+
/// Gets a property descriptor for an object's own property.
846+
/// </summary>
847+
/// <remarks>
848+
/// Requires an active script context.
849+
/// </remarks>
850+
/// <param name="object">The object that has the property.</param>
851+
/// <param name="key">The key (JavascriptString) to the property.</param>
852+
/// <param name="propertyDescriptor">The property descriptor.</param>
853+
/// <returns>
854+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
855+
/// </returns>
856+
CHAKRA_API
857+
JsObjectGetOwnPropertyDescriptor(
858+
_In_ JsValueRef object,
859+
_In_ JsValueRef key,
860+
_Out_ JsValueRef *propertyDescriptor);
748861
#endif // _CHAKRACOREBUILD
749862
#endif // _CHAKRACORE_H_

0 commit comments

Comments
 (0)