Ignore:
Timestamp:
Jan 17, 2014, 9:44:49 AM (11 years ago)
Author:
[email protected]
Message:

Get rid of OpaqueJSString::deprecatedCharacters()
https://bugs.webkit.org/show_bug.cgi?id=127161

Reviewed by Sam Weinig.

Handle OpaqueJSString::m_string being either 8-bit or 16-bit and add extra
code paths for the 8-bit cases.

Unfortunately, JSStringGetCharactersPtr is still expected to return a 16-bit character pointer.
Handle this by storing a separate 16-bit string and initializing it on demand when JSStringGetCharactersPtr
is called and the backing string is 8-bit.

This has the nice side effect of making JSStringGetCharactersPtr thread-safe when it wasn't before.
(In theory, someone could have a JSStringRef backed by an 8-bit string and call JSStringGetCharactersPtr on it
causing an unsafe upconversion to a 16-bit string).

  • API/JSStringRef.cpp:

(JSStringGetCharactersPtr):
Call OpaqueJSString::characters.

(JSStringGetUTF8CString):
Add a code path that handles 8-bit strings.

(JSStringIsEqual):
Call OpaqueJSString::equal.

  • API/JSStringRefCF.cpp:

(JSStringCreateWithCFString):
Reformat the code to use an early return instead of putting most of the code inside the body of an if statement.

(JSStringCopyCFString):
Create an 8-bit CFStringRef if possible.

  • API/OpaqueJSString.cpp:

(OpaqueJSString::create):
Use nullptr.

(OpaqueJSString::~OpaqueJSString):
Free m_characters.

(OpaqueJSString::characters):
Do the up-conversion and store the result in m_characters.

(OpaqueJSString::equal):
New helper function.

  • API/OpaqueJSString.h:

(OpaqueJSString::is8Bit):
New function that returns whether a string is 8-bit or not.

(OpaqueJSString::characters8):
(OpaqueJSString::characters16):
Add getters.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/API/JSStringRefCF.cpp

    r162192 r162205  
    4141    // it can hold.  (<rdar://problem/6806478>)
    4242    size_t length = CFStringGetLength(string);
    43     if (length) {
    44         Vector<LChar, 1024> lcharBuffer(length);
    45         CFIndex usedBufferLength;
    46         CFIndex convertedSize = CFStringGetBytes(string, CFRangeMake(0, length), kCFStringEncodingISOLatin1, 0, false, lcharBuffer.data(), length, &usedBufferLength);
    47         if (static_cast<size_t>(convertedSize) == length && static_cast<size_t>(usedBufferLength) == length)
    48             return OpaqueJSString::create(lcharBuffer.data(), length).leakRef();
     43    if (!length)
     44        return OpaqueJSString::create(reinterpret_cast<const LChar*>(""), 0).leakRef();
    4945
    50         auto buffer = std::make_unique<UniChar[]>(length);
    51         CFStringGetCharacters(string, CFRangeMake(0, length), buffer.get());
    52         COMPILE_ASSERT(sizeof(UniChar) == sizeof(UChar), unichar_and_uchar_must_be_same_size);
    53         return OpaqueJSString::create(reinterpret_cast<UChar*>(buffer.get()), length).leakRef();
    54     }
    55    
    56     return OpaqueJSString::create(reinterpret_cast<const LChar*>(""), 0).leakRef();
     46    Vector<LChar, 1024> lcharBuffer(length);
     47    CFIndex usedBufferLength;
     48    CFIndex convertedSize = CFStringGetBytes(string, CFRangeMake(0, length), kCFStringEncodingISOLatin1, 0, false, lcharBuffer.data(), length, &usedBufferLength);
     49    if (static_cast<size_t>(convertedSize) == length && static_cast<size_t>(usedBufferLength) == length)
     50        return OpaqueJSString::create(lcharBuffer.data(), length).leakRef();
     51
     52    auto buffer = std::make_unique<UniChar[]>(length);
     53    CFStringGetCharacters(string, CFRangeMake(0, length), buffer.get());
     54    static_assert(sizeof(UniChar) == sizeof(UChar), "UniChar and UChar must be same size");
     55    return OpaqueJSString::create(reinterpret_cast<UChar*>(buffer.get()), length).leakRef();
    5756}
    5857
    59 CFStringRef JSStringCopyCFString(CFAllocatorRef alloc, JSStringRef string)
     58CFStringRef JSStringCopyCFString(CFAllocatorRef allocator, JSStringRef string)
    6059{
    6160    if (!string->length())
    6261        return CFSTR("");
    6362
    64     return CFStringCreateWithCharacters(alloc, reinterpret_cast<const UniChar*>(string->deprecatedCharacters()), string->length());
     63    if (string->is8Bit())
     64        return CFStringCreateWithBytes(allocator, reinterpret_cast<const UInt8*>(string->characters8()), string->length(), kCFStringEncodingISOLatin1, false);
     65
     66    return CFStringCreateWithCharacters(allocator, reinterpret_cast<const UniChar*>(string->characters16()), string->length());
    6567}
Note: See TracChangeset for help on using the changeset viewer.