This repository was archived by the owner on Oct 15, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 340
optimize StringUtf8::From #348
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,23 +241,77 @@ class ExternalCallback { | |
| void* _data; | ||
| }; | ||
|
|
||
| class StringUtf8 { | ||
| public: | ||
| StringUtf8() : _str(nullptr), _length(0) {} | ||
| ~StringUtf8() { | ||
| if (_str != nullptr) { | ||
| free(_str); | ||
| _str = nullptr; | ||
| _length = 0; | ||
| } | ||
| } | ||
| char *operator*() { return _str; } | ||
| operator const char *() const { return _str; } | ||
| int length() const { return static_cast<int>(_length); } | ||
|
|
||
| napi_status From(JsValueRef strRef) { | ||
| CHAKRA_ASSERT(!_str); | ||
|
|
||
| int strLength = 0; | ||
| CHECK_JSRT_EXPECTED(JsGetStringLength(strRef, &strLength), | ||
| napi_string_expected); | ||
|
|
||
| // assume string contains ascii characters only | ||
| _str = reinterpret_cast<char*>(malloc(strLength + 1)); | ||
|
|
||
| size_t written = 0; | ||
| size_t actualLength = 0; | ||
| CHECK_JSRT_EXPECTED( | ||
| JsCopyString(strRef, _str, strLength, &written, &actualLength), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can you indent continuation lines by four spaces here and below? |
||
| napi_string_expected); | ||
|
|
||
| // if string contains unicode characters, take slow path | ||
| if (actualLength != written) { | ||
| // free previously allocated buffer | ||
| free(_str); | ||
|
|
||
| _str = reinterpret_cast<char*>(malloc(actualLength + 1)); | ||
| if (_str == nullptr) { | ||
| return napi_set_last_error(napi_generic_failure); | ||
| } | ||
| CHECK_JSRT_EXPECTED( | ||
| JsCopyString(strRef, _str, actualLength, &written, nullptr), | ||
| napi_string_expected); | ||
| assert(actualLength == written); | ||
| } else if (strLength != written) { | ||
| return napi_set_last_error(napi_generic_failure); | ||
| } | ||
|
|
||
| _str[written] = '\0'; | ||
| _length = written; | ||
| return napi_ok; | ||
| } | ||
|
|
||
| private: | ||
| // Disallow copying and assigning | ||
| StringUtf8(const StringUtf8&); | ||
| void operator=(const StringUtf8&); | ||
|
|
||
| char* _str; | ||
| int _length; | ||
| }; | ||
|
|
||
| inline napi_status JsPropertyIdFromKey(JsValueRef key, | ||
| JsPropertyIdRef* propertyId) { | ||
| JsValueType keyType; | ||
| CHECK_JSRT(JsGetValueType(key, &keyType)); | ||
|
|
||
| if (keyType == JsString) { | ||
| size_t length; | ||
| CHECK_JSRT_EXPECTED( | ||
| JsCopyString(key, nullptr, 0, nullptr, &length), napi_string_expected); | ||
|
|
||
| std::vector<uint8_t> name; | ||
| name.reserve(length + 1); | ||
| CHECK_JSRT(JsCopyString( | ||
| key, reinterpret_cast<char*>(name.data()), length + 1, &length, nullptr)); | ||
| StringUtf8 str; | ||
| CHECK_NAPI(str.From(key)); | ||
|
|
||
| CHECK_JSRT(JsCreatePropertyId( | ||
| reinterpret_cast<char*>(name.data()), length, propertyId)); | ||
| CHECK_JSRT(JsCreatePropertyId(*str, str.length(), propertyId)); | ||
| } else if (keyType == JsSymbol) { | ||
| CHECK_JSRT(JsGetPropertyIdFromSymbol(key, propertyId)); | ||
| } else { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably check the malloc return value for null and bail out like the code previously did?