Skip to content

Commit

Permalink
jsrt: JsObject Delete/Get/Has/OwnP../Set Property
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
obastemur committed Oct 4, 2017
1 parent 212e128 commit 99be27c
Show file tree
Hide file tree
Showing 10 changed files with 788 additions and 93 deletions.
50 changes: 33 additions & 17 deletions lib/Common/Codex/Utf8Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,21 @@ namespace utf8
return WideStringToNarrow(Allocator::allocate, sourceString, sourceCount, destStringPtr, destCount, allocateCount);
}

///
/// Use the codex library to encode a UTF8 string to UTF16.
/// The caller is responsible for freeing the memory, which is allocated
/// using Allocator.
/// The returned string is null terminated.
///
template <typename AllocatorFunction>
HRESULT NarrowStringToWide(_In_ AllocatorFunction allocator,_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount, size_t* allocateCount = nullptr)
inline HRESULT NarrowStringToWideNoAlloc(_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR destString, _Out_ size_t* destCount)
{
size_t cbSourceString = sourceCount;
size_t sourceStart = 0;
size_t cbDestString = (sourceCount + 1) * sizeof(WCHAR);
if (cbDestString < sourceCount) // overflow ?
size_t cbSourceString = sourceCount;

if (sourceCount >= MAXUINT32)
{
return E_OUTOFMEMORY;
}

WCHAR* destString = (WCHAR*)allocator(cbDestString);
if (destString == nullptr)
{
return E_OUTOFMEMORY;
return E_INVALIDARG;
}

if (allocateCount != nullptr) *allocateCount = cbDestString;

for (; sourceStart < sourceCount; sourceStart++)
{
const char ch = sourceString[sourceStart];
Expand All @@ -124,7 +114,6 @@ namespace utf8
{
*destCount = sourceCount;
destString[sourceCount] = WCHAR(0);
*destStringPtr = destString;
}
else
{
Expand All @@ -141,12 +130,39 @@ namespace utf8
utf8::DecodeUnitsIntoAndNullTerminateNoAdvance(remDestString, remSourceString, (LPCUTF8) sourceString + cbSourceString, DecodeOptions::doAllowInvalidWCHARs);
Assert(destString[cchDestString] == 0);
static_assert(sizeof(utf8char_t) == sizeof(char), "Needs to be valid for cast");
*destStringPtr = destString;
*destCount = cchDestString;
}

return S_OK;
}

///
/// Use the codex library to encode a UTF8 string to UTF16.
/// The caller is responsible for freeing the memory, which is allocated
/// using Allocator.
/// The returned string is null terminated.
///
template <typename AllocatorFunction>
HRESULT NarrowStringToWide(_In_ AllocatorFunction allocator,_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount, size_t* allocateCount = nullptr)
{
size_t cbDestString = (sourceCount + 1) * sizeof(WCHAR);
if (cbDestString < sourceCount) // overflow ?
{
return E_OUTOFMEMORY;
}

WCHAR* destString = (WCHAR*)allocator(cbDestString);
if (destString == nullptr)
{
return E_OUTOFMEMORY;
}

if (allocateCount != nullptr) *allocateCount = cbDestString;

*destStringPtr = destString;
return NarrowStringToWideNoAlloc(sourceString, sourceCount, destString, destCount);
}

template <class Allocator>
HRESULT NarrowStringToWide(_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount, size_t* allocateCount = nullptr)
{
Expand Down
113 changes: 113 additions & 0 deletions lib/Jsrt/ChakraCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -745,5 +745,118 @@ CHAKRA_API
_Out_opt_ unsigned int *byteOffset,
_Out_opt_ unsigned int *byteLength);

/// <summary>
/// Gets an object's property.
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="object">The object that contains the property.</param>
/// <param name="key">The key (JavascriptString) to the property.</param>
/// <param name="value">The value of the property.</param>
/// <returns>
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
/// </returns>
CHAKRA_API
JsObjectGetProperty(
_In_ JsValueRef object,
_In_ JsValueRef key,
_Out_ JsValueRef *value);

/// <summary>
/// Puts an object's property.
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="object">The object that contains the property.</param>
/// <param name="key">The key (JavascriptString) to the property.</param>
/// <param name="value">The new value of the property.</param>
/// <param name="useStrictRules">The property set should follow strict mode rules.</param>
/// <returns>
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
/// </returns>
CHAKRA_API
JsObjectSetProperty(
_In_ JsValueRef object,
_In_ JsValueRef key,
_In_ JsValueRef value,
_In_ bool useStrictRules);

/// <summary>
/// Determines whether an object has a property.
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="object">The object that may contain the property.</param>
/// <param name="key">The key (JavascriptString) to the property.</param>
/// <param name="hasProperty">Whether the object (or a prototype) has the property.</param>
/// <returns>
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
/// </returns>
CHAKRA_API
JsObjectHasProperty(
_In_ JsValueRef object,
_In_ JsValueRef key,
_Out_ bool *hasProperty);

/// <summary>
/// Defines a new object's own property from a property descriptor.
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="object">The object that has the property.</param>
/// <param name="key">The key (JavascriptString) to the property.</param>
/// <param name="propertyDescriptor">The property descriptor.</param>
/// <param name="result">Whether the property was defined.</param>
/// <returns>
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
/// </returns>
CHAKRA_API
JsObjectDefineProperty(
_In_ JsValueRef object,
_In_ JsValueRef key,
_In_ JsValueRef propertyDescriptor,
_Out_ bool *result);

/// <summary>
/// Deletes an object's property.
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="object">The object that contains the property.</param>
/// <param name="key">The key (JavascriptString) to the property.</param>
/// <param name="useStrictRules">The property set should follow strict mode rules.</param>
/// <param name="result">Whether the property was deleted.</param>
/// <returns>
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
/// </returns>
CHAKRA_API
JsObjectDeleteProperty(
_In_ JsValueRef object,
_In_ JsValueRef key,
_In_ bool useStrictRules,
_Out_ JsValueRef *result);

/// <summary>
/// Gets a property descriptor for an object's own property.
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="object">The object that has the property.</param>
/// <param name="key">The key (JavascriptString) to the property.</param>
/// <param name="propertyDescriptor">The property descriptor.</param>
/// <returns>
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
/// </returns>
CHAKRA_API
JsObjectGetOwnPropertyDescriptor(
_In_ JsValueRef object,
_In_ JsValueRef key,
_Out_ JsValueRef *propertyDescriptor);
#endif // _CHAKRACOREBUILD
#endif // _CHAKRACORE_H_
Loading

0 comments on commit 99be27c

Please sign in to comment.