Cherry-pick WebKit change 53449 to move JSC-specific implementation of JavaString to a private implementation class

See http://trac.webkit.org/changeset/53449
This is required to sync the Android tree with webkit.org to allow unforking in WebCore/bridge.

Change-Id: I61cfea194d679d506d92ca3951032531d6908965
diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog
index 9b4044a..512bf28 100644
--- a/WebCore/ChangeLog
+++ b/WebCore/ChangeLog
@@ -1,3 +1,34 @@
+2010-01-18  Steve Block  <steveblock@google.com>
+
+        Reviewed by Adam Barth.
+
+        Moves JSC-specific implementation of JavaString to a private implementation class.
+        https://bugs.webkit.org/show_bug.cgi?id=33558
+
+        Also modifies JavaField::name and JavaMethod::name to return const JavaString&, rather than UString::Rep*,
+        which is JSC-specific. This allows this code to be used with both JSC and V8, as required by Android.
+
+        No new tests, refactoring only.
+
+        * WebCore.xcodeproj/project.pbxproj: Modified. Added JavaStringJSC.h
+        * bridge/jni/jni_class.cpp: Modified.
+        (JavaClass::JavaClass): Modified. Updates call sites of JavaField::name and JavaMethod::name.
+        * bridge/jni/jni_runtime.h: Modified.
+        (JSC::Bindings::JavaString::JavaString): Modified. Pass through to implementation.
+        (JSC::Bindings::JavaString::UTF8String): Modified. Pass through to implementation.
+        (JSC::Bindings::JavaString::uchars): Modified. Pass through to implementation.
+        (JSC::Bindings::JavaString::length): Modified. Pass through to implementation.
+        (JSC::Bindings::JavaString::operator UString): Modified. Pass through to implementation.
+        (JSC::Bindings::JavaField::name): Modified. Pass through to implementation.
+        (JSC::Bindings::JavaMethod::name): Modified. Pass through to implementation.
+        * bridge/jni/jsc/JavaStringJSC.h: Added.
+        (JSC::Bindings::JavaStringImpl::~JavaStringImpl):
+        (JSC::Bindings::JavaStringImpl::init):
+        (JSC::Bindings::JavaStringImpl::UTF8String):
+        (JSC::Bindings::JavaStringImpl::uchars):
+        (JSC::Bindings::JavaStringImpl::length):
+        (JSC::Bindings::JavaStringImpl::uString):
+
 2010-01-19  Steve Block  <steveblock@google.com>
 
         Reviewed by Adam Barth.
diff --git a/WebCore/WebCore.xcodeproj/project.pbxproj b/WebCore/WebCore.xcodeproj/project.pbxproj
index f912137..987136d 100644
--- a/WebCore/WebCore.xcodeproj/project.pbxproj
+++ b/WebCore/WebCore.xcodeproj/project.pbxproj
@@ -1101,6 +1101,7 @@
 		599E759211055A2A00D904FA /* Bridge.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 599E759111055A2A00D904FA /* Bridge.cpp */; };
 		59A9E7B01104758800DFB4C1 /* JavaInstanceJSC.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59A9E7AF1104758800DFB4C1 /* JavaInstanceJSC.cpp */; };
 		59A9E7B21104759400DFB4C1 /* JavaInstanceJSC.h in Headers */ = {isa = PBXBuildFile; fileRef = 59A9E7B11104759400DFB4C1 /* JavaInstanceJSC.h */; };
+		59BC393F11054A1300FD85DB /* JavaStringJSC.h in Headers */ = {isa = PBXBuildFile; fileRef = 59BC393E11054A1300FD85DB /* JavaStringJSC.h */; };
 		59C77F2A10545B3B00506104 /* GeolocationServiceMock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59C77F2810545B3B00506104 /* GeolocationServiceMock.cpp */; };
 		59C77F2B10545B3B00506104 /* GeolocationServiceMock.h in Headers */ = {isa = PBXBuildFile; fileRef = 59C77F2910545B3B00506104 /* GeolocationServiceMock.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		59E560A71105336600AA1258 /* JavaClassJSC.h in Headers */ = {isa = PBXBuildFile; fileRef = 59E560A61105336600AA1258 /* JavaClassJSC.h */; };
@@ -6425,6 +6426,7 @@
 		599E759111055A2A00D904FA /* Bridge.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Bridge.cpp; path = bridge/Bridge.cpp; sourceTree = "<group>"; };
 		59A9E7AF1104758800DFB4C1 /* JavaInstanceJSC.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = JavaInstanceJSC.cpp; path = jsc/JavaInstanceJSC.cpp; sourceTree = "<group>"; };
 		59A9E7B11104759400DFB4C1 /* JavaInstanceJSC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JavaInstanceJSC.h; path = jsc/JavaInstanceJSC.h; sourceTree = "<group>"; };
+		59BC393E11054A1300FD85DB /* JavaStringJSC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JavaStringJSC.h; path = jsc/JavaStringJSC.h; sourceTree = "<group>"; };
 		59C77F2810545B3B00506104 /* GeolocationServiceMock.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = GeolocationServiceMock.cpp; path = mock/GeolocationServiceMock.cpp; sourceTree = "<group>"; };
 		59C77F2910545B3B00506104 /* GeolocationServiceMock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GeolocationServiceMock.h; path = mock/GeolocationServiceMock.h; sourceTree = "<group>"; };
 		59E560A61105336600AA1258 /* JavaClassJSC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JavaClassJSC.h; path = jsc/JavaClassJSC.h; sourceTree = "<group>"; };
@@ -10982,6 +10984,7 @@
 		599D1E2F10C97D4C00E0EF12 /* jsc */ = {
 			isa = PBXGroup;
 			children = (
+				59BC393E11054A1300FD85DB /* JavaStringJSC.h */,
 				59E560A81105336F00AA1258 /* JavaClassJSC.cpp */,
 				59E560A61105336600AA1258 /* JavaClassJSC.h */,
 				59A9E7B11104759400DFB4C1 /* JavaInstanceJSC.h */,
@@ -18288,6 +18291,7 @@
 			buildConfigurationList = 149C284308902B11008A9EFC /* Build configuration list for PBXProject "WebCore" */;
 			compatibilityVersion = "Xcode 2.4";
 			hasScannedForEncodings = 1;
+				59BC393F11054A1300FD85DB /* JavaStringJSC.h in Headers */,
 			knownRegions = (
 				English,
 				Japanese,
diff --git a/WebCore/bridge/jni/jni_runtime.h b/WebCore/bridge/jni/jni_runtime.h
index 3b3aaf6..5c0dc66 100644
--- a/WebCore/bridge/jni/jni_runtime.h
+++ b/WebCore/bridge/jni/jni_runtime.h
@@ -30,8 +30,10 @@
 
 #include "JavaInstanceJSC.h"
 #include "jni_utility.h"
-#include <runtime/JSLock.h>
 
+#if USE(JSC)
+#include "JavaStringJSC.h"
+#endif
 
 namespace JSC
 {
@@ -46,49 +48,28 @@
 public:
     JavaString()
     {
-        JSLock lock(SilenceAssertionsOnly);
-        _rep = UString().rep();
+        m_impl.init();
     }
 
-    void _commonInit (JNIEnv *e, jstring s)
+    JavaString(JNIEnv* e, jstring s)
     {
-        int _size = e->GetStringLength (s);
-        const jchar *uc = getUCharactersFromJStringInEnv (e, s);
-        {
-            JSLock lock(SilenceAssertionsOnly);
-            _rep = UString(reinterpret_cast<const UChar*>(uc), _size).rep();
-        }
-        releaseUCharactersForJStringInEnv (e, s, uc);
+        m_impl.init(e, s);
     }
-    
-    JavaString (JNIEnv *e, jstring s) {
-        _commonInit (e, s);
-    }
-    
-    JavaString (jstring s) {
-        _commonInit (getJNIEnv(), s);
-    }
-    
-    ~JavaString()
+
+    JavaString(jstring s)
     {
-        JSLock lock(SilenceAssertionsOnly);
-        _rep = 0;
+        m_impl.init(getJNIEnv(), s);
     }
-    
-    const char *UTF8String() const { 
-        if (_utf8String.c_str() == 0) {
-            JSLock lock(SilenceAssertionsOnly);
-            _utf8String = UString(_rep).UTF8String();
-        }
-        return _utf8String.c_str();
-    }
-    const jchar *uchars() const { return (const jchar *)_rep->data(); }
-    int length() const { return _rep->size(); }
-    operator UString() const { return UString(_rep); }
+
+    const char* UTF8String() const { return m_impl.UTF8String(); }
+    const jchar* uchars() const { return m_impl.uchars(); }
+    int length() const { return m_impl.length(); }
+#if USE(JSC)
+    operator UString() const { return m_impl.uString(); }
+#endif
 
 private:
-    RefPtr<UString::Rep> _rep;
-    mutable CString _utf8String;
+    JavaStringImpl m_impl;
 };
 
 class JavaParameter
@@ -115,7 +96,7 @@
     virtual JSValue valueFromInstance(ExecState *exec, const Instance *instance) const;
     virtual void setValueToInstance(ExecState *exec, const Instance *instance, JSValue aValue) const;
     
-    UString::Rep* name() const { return ((UString)_name).rep(); }
+    const JavaString& name() const { return _name; }
     virtual RuntimeType type() const { return _type.UTF8String(); }
 
     JNIType getJNIType() const { return _JNIType; }
@@ -137,7 +118,7 @@
     JavaMethod(JNIEnv* env, jobject aMethod);
     ~JavaMethod();
 
-    UString::Rep* name() const { return ((UString)_name).rep(); }
+    const JavaString& name() const { return _name; }
     RuntimeType returnType() const { return _returnType.UTF8String(); };
     JavaParameter* parameterAt(int i) const { return &_parameters[i]; };
     int numParameters() const { return _numParameters; };
diff --git a/WebCore/bridge/jni/jsc/JavaClassJSC.cpp b/WebCore/bridge/jni/jsc/JavaClassJSC.cpp
index 3e30a4a..b242cff 100644
--- a/WebCore/bridge/jni/jsc/JavaClassJSC.cpp
+++ b/WebCore/bridge/jni/jsc/JavaClassJSC.cpp
@@ -61,7 +61,7 @@
         JavaField *aField = new JavaField(env, aJField); // deleted in the JavaClass destructor
         {
             JSLock lock(SilenceAssertionsOnly);
-            _fields.set(aField->name(), aField);
+            _fields.set(((UString)aField->name()).rep(), aField);
         }
         env->DeleteLocalRef(aJField);
     }
@@ -76,10 +76,10 @@
         {
             JSLock lock(SilenceAssertionsOnly);
 
-            methodList = _methods.get(aMethod->name());
+            methodList = _methods.get(((UString)aMethod->name()).rep());
             if (!methodList) {
                 methodList = new MethodList();
-                _methods.set(aMethod->name(), methodList);
+                _methods.set(((UString)aMethod->name()).rep(), methodList);
             }
         }
         methodList->append(aMethod);
diff --git a/WebCore/bridge/jni/jsc/JavaStringJSC.h b/WebCore/bridge/jni/jsc/JavaStringJSC.h
new file mode 100644
index 0000000..d3ed56b
--- /dev/null
+++ b/WebCore/bridge/jni/jsc/JavaStringJSC.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2010 Apple Computer, Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef JavaStringJSC_h
+#define JavaStringJSC_h
+
+#include "JavaInstanceJSC.h"
+#include "jni_utility.h"
+#include <runtime/JSLock.h>
+
+
+namespace JSC {
+
+namespace Bindings {
+
+class JavaStringImpl {
+public:
+    ~JavaStringImpl()
+    {
+        JSLock lock(SilenceAssertionsOnly);
+        m_rep = 0;
+    }
+
+    void init()
+    {
+        JSLock lock(SilenceAssertionsOnly);
+        m_rep = UString().rep();
+    }
+
+    void init(JNIEnv* e, jstring s)
+    {
+        int size = e->GetStringLength(s);
+        const jchar* uc = getUCharactersFromJStringInEnv(e, s);
+        {
+            JSLock lock(SilenceAssertionsOnly);
+            m_rep = UString(reinterpret_cast<const UChar*>(uc), size).rep();
+        }
+        releaseUCharactersForJStringInEnv(e, s, uc);
+    }
+
+    const char* UTF8String() const
+    {
+        if (!m_utf8String.c_str()) {
+            JSLock lock(SilenceAssertionsOnly);
+            m_utf8String = UString(m_rep).UTF8String();
+        }
+        return m_utf8String.c_str();
+    }
+    const jchar* uchars() const { return (const jchar*)m_rep->data(); }
+    int length() const { return m_rep->size(); }
+    UString uString() const { return UString(m_rep); }
+
+private:
+    RefPtr<UString::Rep> m_rep;
+    mutable CString m_utf8String;
+};
+
+} // namespace Bindings
+
+} // namespace JSC
+
+#endif // JavaStringJSC_h