Skip to content

Commit fea3125

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 cb6e6d9 commit fea3125

17 files changed

+992
-196
lines changed

lib/Common/Codex/Utf8Helper.h

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

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

106-
WCHAR* destString = (WCHAR*)allocator(cbDestString);
107-
if (destString == nullptr)
101+
if (destString == nullptr || sourceCount >= destBufferCount)
108102
{
109-
return E_OUTOFMEMORY;
103+
destString[0] = WCHAR(0);
104+
return E_INVALIDARG;
110105
}
111106

112-
if (allocateCount != nullptr) *allocateCount = cbDestString;
113-
114107
for (; sourceStart < sourceCount; sourceStart++)
115108
{
116109
const char ch = sourceString[sourceStart];
@@ -127,7 +120,6 @@ namespace utf8
127120
{
128121
*destCount = sourceCount;
129122
destString[sourceCount] = WCHAR(0);
130-
*destStringPtr = destString;
131123
}
132124
else
133125
{
@@ -142,14 +134,44 @@ namespace utf8
142134
// instead of replacing them with the "replacement" chracter. Pass a flag to our
143135
// decoder to require such behavior
144136
utf8::DecodeUnitsIntoAndNullTerminateNoAdvance(remDestString, remSourceString, (LPCUTF8) sourceString + cbSourceString, DecodeOptions::doAllowInvalidWCHARs);
145-
Assert(destString[cchDestString] == 0);
137+
146138
static_assert(sizeof(utf8char_t) == sizeof(char), "Needs to be valid for cast");
147-
*destStringPtr = destString;
148139
*destCount = cchDestString;
149140
}
141+
142+
Assert(destString[*destCount] == 0);
143+
150144
return S_OK;
151145
}
152146

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

lib/Jsrt/ChakraCore.h

Lines changed: 137 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,9 @@ CHAKRA_API
764764
/// </returns>
765765
CHAKRA_API
766766
JsLessThan(
767-
_In_ JsValueRef object1,
768-
_In_ JsValueRef object2,
769-
_Out_ bool *result);
767+
_In_ JsValueRef object1,
768+
_In_ JsValueRef object2,
769+
_Out_ bool *result);
770770

771771
/// <summary>
772772
/// Determine if one JavaScript value is less than or equal to another JavaScript value.
@@ -787,9 +787,140 @@ JsLessThan(
787787
/// </returns>
788788
CHAKRA_API
789789
JsLessThanOrEqual(
790-
_In_ JsValueRef object1,
791-
_In_ JsValueRef object2,
792-
_Out_ bool *result);
790+
_In_ JsValueRef object1,
791+
_In_ JsValueRef object2,
792+
_Out_ bool *result);
793793

794+
/// <summary>
795+
/// Gets an object's property.
796+
/// </summary>
797+
/// <remarks>
798+
/// Requires an active script context.
799+
/// </remarks>
800+
/// <param name="object">The object that contains the property.</param>
801+
/// <param name="key">The key (JavascriptString) to the property.</param>
802+
/// <param name="value">The value of the property.</param>
803+
/// <returns>
804+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
805+
/// </returns>
806+
CHAKRA_API
807+
JsObjectGetProperty(
808+
_In_ JsValueRef object,
809+
_In_ JsValueRef key,
810+
_Out_ JsValueRef *value);
811+
812+
/// <summary>
813+
/// Puts an object's property.
814+
/// </summary>
815+
/// <remarks>
816+
/// Requires an active script context.
817+
/// </remarks>
818+
/// <param name="object">The object that contains the property.</param>
819+
/// <param name="key">The key (JavascriptString) to the property.</param>
820+
/// <param name="value">The new value of the property.</param>
821+
/// <param name="useStrictRules">The property set should follow strict mode rules.</param>
822+
/// <returns>
823+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
824+
/// </returns>
825+
CHAKRA_API
826+
JsObjectSetProperty(
827+
_In_ JsValueRef object,
828+
_In_ JsValueRef key,
829+
_In_ JsValueRef value,
830+
_In_ bool useStrictRules);
831+
832+
/// <summary>
833+
/// Determines whether an object has a property.
834+
/// </summary>
835+
/// <remarks>
836+
/// Requires an active script context.
837+
/// </remarks>
838+
/// <param name="object">The object that may contain the property.</param>
839+
/// <param name="key">The key (JavascriptString) to the property.</param>
840+
/// <param name="hasProperty">Whether the object (or a prototype) has the property.</param>
841+
/// <returns>
842+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
843+
/// </returns>
844+
CHAKRA_API
845+
JsObjectHasProperty(
846+
_In_ JsValueRef object,
847+
_In_ JsValueRef key,
848+
_Out_ bool *hasProperty);
849+
850+
/// <summary>
851+
/// Defines a new object's own property from a property descriptor.
852+
/// </summary>
853+
/// <remarks>
854+
/// Requires an active script context.
855+
/// </remarks>
856+
/// <param name="object">The object that has the property.</param>
857+
/// <param name="key">The key (JavascriptString) to the property.</param>
858+
/// <param name="propertyDescriptor">The property descriptor.</param>
859+
/// <param name="result">Whether the property was defined.</param>
860+
/// <returns>
861+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
862+
/// </returns>
863+
CHAKRA_API
864+
JsObjectDefineProperty(
865+
_In_ JsValueRef object,
866+
_In_ JsValueRef key,
867+
_In_ JsValueRef propertyDescriptor,
868+
_Out_ bool *result);
869+
870+
/// <summary>
871+
/// Deletes an object's property.
872+
/// </summary>
873+
/// <remarks>
874+
/// Requires an active script context.
875+
/// </remarks>
876+
/// <param name="object">The object that contains the property.</param>
877+
/// <param name="key">The key (JavascriptString) to the property.</param>
878+
/// <param name="useStrictRules">The property set should follow strict mode rules.</param>
879+
/// <param name="result">Whether the property was deleted.</param>
880+
/// <returns>
881+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
882+
/// </returns>
883+
CHAKRA_API
884+
JsObjectDeleteProperty(
885+
_In_ JsValueRef object,
886+
_In_ JsValueRef key,
887+
_In_ bool useStrictRules,
888+
_Out_ JsValueRef *result);
889+
890+
/// <summary>
891+
/// Gets a property descriptor for an object's own property.
892+
/// </summary>
893+
/// <remarks>
894+
/// Requires an active script context.
895+
/// </remarks>
896+
/// <param name="object">The object that has the property.</param>
897+
/// <param name="key">The key (JavascriptString) to the property.</param>
898+
/// <param name="propertyDescriptor">The property descriptor.</param>
899+
/// <returns>
900+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
901+
/// </returns>
902+
CHAKRA_API
903+
JsObjectGetOwnPropertyDescriptor(
904+
_In_ JsValueRef object,
905+
_In_ JsValueRef key,
906+
_Out_ JsValueRef *propertyDescriptor);
907+
908+
/// <summary>
909+
/// Determines whether an object has a non-inherited property.
910+
/// </summary>
911+
/// <remarks>
912+
/// Requires an active script context.
913+
/// </remarks>
914+
/// <param name="object">The object that may contain the property.</param>
915+
/// <param name="key">The key (JavascriptString) to the property.</param>
916+
/// <param name="hasOwnProperty">Whether the object has the non-inherited property.</param>
917+
/// <returns>
918+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
919+
/// </returns>
920+
CHAKRA_API
921+
JsObjectHasOwnProperty(
922+
_In_ JsValueRef object,
923+
_In_ JsValueRef key,
924+
_Out_ bool *hasOwnProperty);
794925
#endif // _CHAKRACOREBUILD
795926
#endif // _CHAKRACORE_H_

0 commit comments

Comments
 (0)