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/OpaqueJSString.cpp

    r162192 r162205  
    3535PassRefPtr<OpaqueJSString> OpaqueJSString::create(const String& string)
    3636{
    37     if (!string.isNull())
    38         return adoptRef(new OpaqueJSString(string));
    39     return 0;
     37    if (string.isNull())
     38        return nullptr;
     39
     40    return adoptRef(new OpaqueJSString(string));
     41}
     42
     43OpaqueJSString::~OpaqueJSString()
     44{
     45    // m_characters is put in a local here to avoid an extra atomic load.
     46    const UChar* characters = m_characters;
     47    if (!characters)
     48        return;
     49
     50    if (!m_string.is8Bit() && m_string.characters() == characters)
     51        return;
     52
     53    fastFree(const_cast<void*>(static_cast<const void*>(characters)));
    4054}
    4155
     
    6276    return Identifier(vm, m_string.characters16(), m_string.length());
    6377}
     78
     79const UChar* OpaqueJSString::characters()
     80{
     81    if (!this)
     82        return nullptr;
     83
     84    // m_characters is put in a local here to avoid an extra atomic load.
     85    const UChar* characters = m_characters;
     86    if (characters)
     87        return characters;
     88
     89    if (m_string.isNull())
     90        return nullptr;
     91
     92    unsigned length = m_string.length();
     93    UChar* newCharacters = static_cast<UChar*>(fastMalloc(length * sizeof(UChar)));
     94
     95    if (m_string.is8Bit()) {
     96        for (size_t i = 0; i < length; ++i)
     97            newCharacters[i] = m_string.characters8()[i];
     98    } else
     99        memcpy(newCharacters, m_string.characters16(), length * sizeof(UChar));
     100
     101    if (!m_characters.compare_exchange_strong(characters, newCharacters)) {
     102        fastFree(newCharacters);
     103        return characters;
     104    }
     105
     106    return newCharacters;
     107}
     108
     109bool OpaqueJSString::equal(const OpaqueJSString* a, const OpaqueJSString* b)
     110{
     111    if (a == b)
     112        return true;
     113
     114    if (!a || !b)
     115        return false;
     116
     117    return a->m_string == b->m_string;
     118}
Note: See TracChangeset for help on using the changeset viewer.