2009-12-04 Anton Muhin <antonm@chromium.org>
authoreric@webkit.org <eric@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Dec 2009 20:16:02 +0000 (20:16 +0000)
committereric@webkit.org <eric@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Dec 2009 20:16:02 +0000 (20:16 +0000)
        Reviewed by Dmitry Titov.

        Do not use WebCore::String::String(const UChar*, int length) to convert
        short v8 strings.

        Plus added string traits.
        https://bugs.webkit.org/show_bug.cgi?id=31415

        * bindings/v8/V8Binding.cpp:
        (WebCore::):
        (WebCore::v8StringToWebCoreString):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@51707 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/bindings/v8/V8Binding.cpp

index a65c96961fff98e6852db4b738e5221f07937014..3d33182cfbc482ee8c3739c4100b9e1f8f0b78d2 100644 (file)
@@ -1,3 +1,17 @@
+2009-12-04  Anton Muhin  <antonm@chromium.org>
+
+        Reviewed by Dmitry Titov.
+
+        Do not use WebCore::String::String(const UChar*, int length) to convert
+        short v8 strings.
+
+        Plus added string traits.
+        https://bugs.webkit.org/show_bug.cgi?id=31415
+
+        * bindings/v8/V8Binding.cpp:
+        (WebCore::):
+        (WebCore::v8StringToWebCoreString):
+
 2009-12-04  Chris Fleizach  <cfleizach@apple.com>
 
         Reviewed by Darin Adler.
index c25a81c4716d06d667e662d27652a9cb1967baf6..00286e5913b2ab4f0d7a3d918d74b0c39b8b43dd 100644 (file)
@@ -106,10 +106,6 @@ public:
         return m_atomicString;
     }
 
-    // Returns right string type based on a dummy parameter.
-    String string(String) { return webcoreString(); }
-    AtomicString string(AtomicString) { return atomicString(); }
-
     static WebCoreStringResource* toStringResource(v8::Handle<v8::String> v8String)
     {
         return static_cast<WebCoreStringResource*>(v8String->GetExternalStringResource());
@@ -244,12 +240,63 @@ v8::Handle<v8::Value> v8StringOrFalse(const String& str)
 }
 
 
+template <class S> struct StringTraits
+{
+    static S fromStringResource(WebCoreStringResource* resource);
+
+    static S fromV8String(v8::Handle<v8::String> v8String, int length);
+};
+
+template<>
+struct StringTraits<String>
+{
+    static String fromStringResource(WebCoreStringResource* resource)
+    {
+        return resource->webcoreString();
+    }
+
+    static String fromV8String(v8::Handle<v8::String> v8String, int length)
+    {
+        ASSERT(v8String->Length() == length);
+        // NOTE: as of now, String(const UChar*, int) performs String::createUninitialized
+        // anyway, so no need to optimize like we do for AtomicString below.
+        UChar* buffer;
+        String result = String::createUninitialized(length, buffer);
+        v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
+        return result;
+    }
+};
+
+template<>
+struct StringTraits<AtomicString>
+{
+    static AtomicString fromStringResource(WebCoreStringResource* resource)
+    {
+        return resource->atomicString();
+    }
+
+    static AtomicString fromV8String(v8::Handle<v8::String> v8String, int length)
+    {
+        ASSERT(v8String->Length() == length);
+        static const int inlineBufferSize = 16;
+        if (length <= inlineBufferSize) {
+            UChar inlineBuffer[inlineBufferSize];
+            v8String->Write(reinterpret_cast<uint16_t*>(inlineBuffer), 0, length);
+            return AtomicString(inlineBuffer, length);
+        }
+        UChar* buffer;
+        String tmp = String::createUninitialized(length, buffer);
+        v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
+        return AtomicString(tmp);
+    }
+};
+
 template <typename StringType>
 StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode external)
 {
     WebCoreStringResource* stringResource = WebCoreStringResource::toStringResource(v8String);
     if (stringResource)
-        return stringResource->string(StringType());
+        return StringTraits<StringType>::fromStringResource(stringResource);
 
     int length = v8String->Length();
     if (!length) {
@@ -257,18 +304,7 @@ StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode
         return StringImpl::empty();
     }
 
-    StringType result;
-    static const int inlineBufferSize = 16;
-    if (length <= inlineBufferSize) {
-        UChar inlineBuffer[inlineBufferSize];
-        v8String->Write(reinterpret_cast<uint16_t*>(inlineBuffer), 0, length);
-        result = StringType(inlineBuffer, length);
-    } else {
-        UChar* buffer;
-        String tmp = String::createUninitialized(length, buffer);
-        v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
-        result = StringType(tmp);
-    }
+    StringType result(StringTraits<StringType>::fromV8String(v8String, length));
 
     if (external == Externalize && v8String->CanMakeExternal()) {
         stringResource = new WebCoreStringResource(result);