Repository navigation
Convert NSStrings returned to JS to UTF-8 only once #59325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
hryhoriiK97
wants to merge
2
commits into
react:main
from
hryhoriiK97:ios-turbomodule-string-conversion
Closed
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
188 changes: 188 additions & 0 deletions
188
packages/rn-tester/RNTesterUnitTests/RCTTurboModuleStringConversionTests.mm
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 |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #import <XCTest/XCTest.h> | ||
|
|
||
| #import <ReactCommon/RCTTurboModule.h> | ||
| #import <hermes/hermes.h> | ||
|
|
||
| #import <memory> | ||
| #import <string> | ||
| #import <vector> | ||
|
|
||
| using namespace facebook; | ||
| using facebook::react::TurboModuleConvertUtils::convertObjCObjectToJSIValue; | ||
|
|
||
| // An NSString subclass that only implements the primitive methods, so | ||
| // CoreFoundation can't hand out a pointer to its characters. | ||
| @interface RCTTurboModuleStringConversionTestsString : NSString | ||
| - (instancetype)initWithCharacters:(std::vector<unichar>)characters; | ||
| @end | ||
|
|
||
| @implementation RCTTurboModuleStringConversionTestsString { | ||
| std::vector<unichar> _characters; | ||
| } | ||
|
|
||
| - (instancetype)initWithCharacters:(std::vector<unichar>)characters | ||
| { | ||
| if (self = [super init]) { | ||
| _characters = std::move(characters); | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (NSUInteger)length | ||
| { | ||
| return _characters.size(); | ||
| } | ||
|
|
||
| - (unichar)characterAtIndex:(NSUInteger)index | ||
| { | ||
| return _characters[index]; | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| namespace { | ||
|
|
||
| std::u16string utf16FromNSString(NSString *string) | ||
| { | ||
| std::u16string utf16(string.length, u'\0'); | ||
| [string getCharacters:reinterpret_cast<unichar *>(utf16.data()) range:NSMakeRange(0, string.length)]; | ||
| return utf16; | ||
| } | ||
|
|
||
| // Strings read from data, like the text that `FileReader` returns. | ||
| NSString *stringFromUTF8Data(NSString *string) | ||
| { | ||
| return [[NSString alloc] initWithData:[string dataUsingEncoding:NSUTF8StringEncoding] encoding:NSUTF8StringEncoding]; | ||
| } | ||
|
|
||
| NSString *stringWithCharacters(std::vector<unichar> characters) | ||
| { | ||
| return [NSString stringWithCharacters:characters.data() length:characters.size()]; | ||
| } | ||
|
|
||
| bool hasASCIIPointer(NSString *string) | ||
| { | ||
| return CFStringGetCStringPtr((__bridge CFStringRef)string, kCFStringEncodingASCII) != nullptr; | ||
| } | ||
|
|
||
| bool hasUTF16Pointer(NSString *string) | ||
| { | ||
| return CFStringGetCharactersPtr((__bridge CFStringRef)string) != nullptr; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| @interface RCTTurboModuleStringConversionTests : XCTestCase | ||
| @end | ||
|
|
||
| @implementation RCTTurboModuleStringConversionTests { | ||
| std::unique_ptr<jsi::Runtime> _runtime; | ||
| } | ||
|
|
||
| - (void)setUp | ||
| { | ||
| [super setUp]; | ||
| _runtime = facebook::hermes::makeHermesRuntime(); | ||
| } | ||
|
|
||
| - (void)tearDown | ||
| { | ||
| _runtime.reset(); | ||
| [super tearDown]; | ||
| } | ||
|
|
||
| - (void)assertConvertsToSameCharacters:(NSString *)string | ||
| { | ||
| jsi::Value value = convertObjCObjectToJSIValue(*_runtime, string); | ||
| XCTAssertTrue(value.isString(), @"Expected a JS string for \"%@\"", string); | ||
| XCTAssertTrue( | ||
| value.asString(*_runtime).utf16(*_runtime) == utf16FromNSString(string), | ||
| @"Expected the JS string to have the same characters as \"%@\"", | ||
| string); | ||
| } | ||
|
|
||
| - (void)testASCIIStringWithASCIIPointer | ||
| { | ||
| NSString *string = stringFromUTF8Data(@"com.facebook.react.uiapp"); | ||
| XCTAssertTrue(hasASCIIPointer(string), @"Expected the string to expose its ASCII characters"); | ||
| [self assertConvertsToSameCharacters:string]; | ||
| [self assertConvertsToSameCharacters:@"constant"]; | ||
| } | ||
|
|
||
| - (void)testTaggedPointerString | ||
| { | ||
| // Short strings like this one are tagged pointers, which have no character buffer. | ||
| NSString *string = stringWithCharacters({u'i', u'd'}); | ||
| XCTAssertFalse(hasASCIIPointer(string), @"Expected the string to have no ASCII pointer"); | ||
| XCTAssertFalse(hasUTF16Pointer(string), @"Expected the string to have no UTF-16 pointer"); | ||
| [self assertConvertsToSameCharacters:string]; | ||
| } | ||
|
|
||
| - (void)testNonASCIIString | ||
| { | ||
| NSString *string = stringFromUTF8Data(@"Zażółć gęślą jaźń"); | ||
| XCTAssertTrue(hasUTF16Pointer(string), @"Expected the string to expose its UTF-16 characters"); | ||
| [self assertConvertsToSameCharacters:string]; | ||
| [self assertConvertsToSameCharacters:stringFromUTF8Data(@"日本語のテキスト")]; | ||
| } | ||
|
|
||
| - (void)testStringWithSurrogatePairs | ||
| { | ||
| [self assertConvertsToSameCharacters:stringFromUTF8Data(@"Rocket 🚀 and family 👨👩👧")]; | ||
| } | ||
|
|
||
| - (void)testEmptyString | ||
| { | ||
| [self assertConvertsToSameCharacters:@""]; | ||
| } | ||
|
|
||
| - (void)testLargeString | ||
| { | ||
| NSMutableString *string = [NSMutableString new]; | ||
| for (int i = 0; i < 20000; i++) { | ||
| [string appendFormat:@"{\"id\":%d,\"name\":\"Zażółć %d 🚀\"},", i, i]; | ||
| } | ||
| [self assertConvertsToSameCharacters:stringFromUTF8Data(string)]; | ||
| } | ||
|
|
||
| - (void)testStringSubclass | ||
| { | ||
| NSString *string = | ||
| [[RCTTurboModuleStringConversionTestsString alloc] initWithCharacters:{u'a', u'ż', 0xD83D, 0xDE80}]; | ||
| XCTAssertFalse(hasASCIIPointer(string), @"Expected the string to have no ASCII pointer"); | ||
| XCTAssertFalse(hasUTF16Pointer(string), @"Expected the string to have no UTF-16 pointer"); | ||
| [self assertConvertsToSameCharacters:string]; | ||
| } | ||
|
|
||
| - (void)testStringWithNullCharacterIsNotTruncated | ||
| { | ||
| NSString *ascii = [[NSString alloc] initWithBytes:"aaaaaaaaaaaaaaaaaaaa\0b" length:22 encoding:NSUTF8StringEncoding]; | ||
| XCTAssertTrue(hasASCIIPointer(ascii), @"Expected the string to expose its ASCII characters"); | ||
| [self assertConvertsToSameCharacters:ascii]; | ||
| [self assertConvertsToSameCharacters:stringWithCharacters({u'a', 0, u'b'})]; | ||
| [self assertConvertsToSameCharacters:stringWithCharacters({u'ż', 0, u'b'})]; | ||
| } | ||
|
|
||
| - (void)testStringWithLoneSurrogateIsKept | ||
| { | ||
| NSString *string = stringWithCharacters({u'a', 0xD800, u'b'}); | ||
| XCTAssertTrue(hasUTF16Pointer(string), @"Expected the string to expose its UTF-16 characters"); | ||
| [self assertConvertsToSameCharacters:string]; | ||
| [self assertConvertsToSameCharacters:stringWithCharacters({u'a', 0xDC00})]; | ||
| } | ||
|
|
||
| - (void)testStringSubclassWithLoneSurrogateIsKept | ||
| { | ||
| NSString *string = [[RCTTurboModuleStringConversionTestsString alloc] initWithCharacters:{u'a', 0xD800, u'b'}]; | ||
| XCTAssertFalse(hasUTF16Pointer(string), @"Expected the string to have no UTF-16 pointer"); | ||
| [self assertConvertsToSameCharacters:string]; | ||
| } | ||
|
|
||
| @end |
Oops, something went wrong.
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.
CFStringGetCharactersPtr would still be preferred here, although also claim it may return null...
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.
Done. It now tries
CFStringGetCharactersPtrfirst and only copies when that returns null. Each branch has its own test.