Reviewed by Sam.
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 5 Mar 2008 17:02:17 +0000 (17:02 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 5 Mar 2008 17:02:17 +0000 (17:02 +0000)
        Rewrite NPRuntime string conversion routines to use WebCore::String

        * bridge/NP_jsobject.cpp:
        (_NPN_Evaluate):
        * bridge/c/c_utility.cpp:
        (KJS::Bindings::convertUTF8ToUTF16WithLatin1Fallback):
        (KJS::Bindings::convertNPVariantToValue):
        (KJS::Bindings::convertNPStringToUTF16):
        (KJS::Bindings::identifierFromNPIdentifier):
        * bridge/c/c_utility.h:

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

WebCore/ChangeLog
WebCore/bridge/NP_jsobject.cpp
WebCore/bridge/c/c_utility.cpp
WebCore/bridge/c/c_utility.h

index dc5205652b304bdc02fd685830f7ae85717e82bf..5eb1a64048767ece8dd12a6b21a32e1db50993b7 100644 (file)
@@ -1,3 +1,18 @@
+2008-03-04  Anders Carlsson  <andersca@apple.com>
+
+        Reviewed by Sam.
+
+        Rewrite NPRuntime string conversion routines to use WebCore::String
+        
+        * bridge/NP_jsobject.cpp:
+        (_NPN_Evaluate):
+        * bridge/c/c_utility.cpp:
+        (KJS::Bindings::convertUTF8ToUTF16WithLatin1Fallback):
+        (KJS::Bindings::convertNPVariantToValue):
+        (KJS::Bindings::convertNPStringToUTF16):
+        (KJS::Bindings::identifierFromNPIdentifier):
+        * bridge/c/c_utility.h:
+
 2008-03-05  Oliver Hunt  <oliver@apple.com>
 
         Reviewed by Alexey P.
index 9f5f14382d105172fb07ae35a14235079476e98f..8bffcf176ace52d306c34ee58c5988414db5dc76 100644 (file)
 #include <kjs/PropertyNameArray.h>
 #include "c_utility.h"
 #include <kjs/interpreter.h>
+#include "PlatformString.h"
 #include "npruntime_impl.h"
 #include "npruntime_priv.h"
-
 #include "runtime_root.h"
 
+using WebCore::String;
 using namespace KJS;
 using namespace KJS::Bindings;
 
@@ -193,11 +194,9 @@ bool _NPN_Evaluate(NPP, NPObject* o, NPString* s, NPVariant* variant)
         ExecState* exec = rootObject->globalObject()->globalExec();
         
         JSLock lock;
-        NPUTF16* scriptString;
-        unsigned int UTF16Length;
-        convertNPStringToUTF16(s, &scriptString, &UTF16Length); // requires free() of returned memory
+        String scriptString = convertNPStringToUTF16(s);
         rootObject->globalObject()->startTimeoutCheck();
-        Completion completion = Interpreter::evaluate(rootObject->globalObject()->globalExec(), UString(), 0, UString(reinterpret_cast<const UChar*>(scriptString), UTF16Length));
+        Completion completion = Interpreter::evaluate(rootObject->globalObject()->globalExec(), UString(), 0, scriptString);
         rootObject->globalObject()->stopTimeoutCheck();
         ComplType type = completion.complType();
         
@@ -209,8 +208,6 @@ bool _NPN_Evaluate(NPP, NPObject* o, NPString* s, NPVariant* variant)
         } else
             result = jsUndefined();
 
-        free(scriptString);
-
         convertValueToNPVariant(exec, result, variant);
     
         return true;
index 9c3099a1c31ecafd814ec820da67b8ca317dd2f3..69e4b6a011d52bd3c89fb257cf09645406250678 100644 (file)
 #include "NP_jsobject.h"
 #include "c_instance.h"
 #include <kjs/JSGlobalObject.h>
+#include "PlatformString.h"
 #include "npruntime_impl.h"
 #include "npruntime_priv.h"
 #include "runtime_object.h"
 #include "runtime_root.h"
 #include <wtf/Assertions.h>
 
-// FIXME: Rewrite convertUTF8ToUTF16WithLatin1Fallback to not use the WTF UTF-8 support.
-#if 0
-#include <wtf/unicode/UTF8.h>
-#endif
+using WebCore::String;
 
 namespace KJS { namespace Bindings {
 
-// Requires free() of returned UTF16Chars.
-static void convertUTF8ToUTF16WithLatin1Fallback(const NPUTF8* UTF8Chars, int UTF8Length, NPUTF16** UTF16Chars, unsigned int* UTF16Length)
+static String convertUTF8ToUTF16WithLatin1Fallback(const NPUTF8* UTF8Chars, int UTF8Length)
 {
-#if 0
     ASSERT(UTF8Chars || UTF8Length == 0);
-    ASSERT(UTF16Chars);
     
     if (UTF8Length == -1)
         UTF8Length = static_cast<int>(strlen(UTF8Chars));
 
-    *UTF16Length = UTF8Length; 
-    *UTF16Chars = static_cast<NPUTF16*>(malloc(sizeof(NPUTF16) * (*UTF16Length)));
-    
-    const char* sourcestart = UTF8Chars;
-    const char* sourceend = sourcestart + UTF8Length;
-
-    ::UChar* targetstart = reinterpret_cast< ::UChar*>(*UTF16Chars);
-    ::UChar* targetend = targetstart + UTF8Length;
+    String result = String::fromUTF8(UTF8Chars, UTF8Length);
     
-    ConversionResult result = convertUTF8ToUTF16(&sourcestart, sourceend, &targetstart, targetend);
-    
-    *UTF16Length = targetstart - reinterpret_cast< ::UChar*>(*UTF16Chars);
-
-    // Check to see if the conversion was successful
+    // If we got back a null string indicating an unsuccessful conversion, fall back to latin 1.
     // Some plugins return invalid UTF-8 in NPVariantType_String, see <http://bugs.webkit.org/show_bug.cgi?id=5163>
     // There is no "bad data" for latin1. It is unlikely that the plugin was really sending text in this encoding,
     // but it should have used UTF-8, and now we are simply avoiding a crash.
-    if (result != conversionOK) {
-        *UTF16Length = UTF8Length;
-        
-        if (!*UTF16Chars)   // If the memory wasn't allocated, allocate it.
-            *UTF16Chars = (NPUTF16*)malloc(sizeof(NPUTF16) * (*UTF16Length));
-        for (unsigned i = 0; i < *UTF16Length; i++)
-            (*UTF16Chars)[i] = UTF8Chars[i] & 0xFF;
-    }
-#endif
+    if (result.isNull())
+        result = String(UTF8Chars, UTF8Length);
+    
+    return result;
 }
 
 // Variant value must be released with NPReleaseVariantValue()
-void convertValueToNPVariant(ExecState *exec, JSValue *value, NPVariant *result)
+void convertValueToNPVariant(ExecState* exec, JSValue* value, NPVariant* result)
 {
     JSLock lock;
     
@@ -97,7 +75,7 @@ void convertValueToNPVariant(ExecState *exec, JSValue *value, NPVariant *result)
     if (type == StringType) {
         UString ustring = value->toString(exec);
         CString cstring = ustring.UTF8String();
-        NPString string = { (const NPUTF8 *)cstring.c_str(), static_cast<uint32_t>(cstring.size()) };
+        NPString string = { (const NPUTF8*)cstring.c_str(), static_cast<uint32_t>(cstring.size()) };
         NPN_InitializeVariantWithStringCopy(result, &string);
     } else if (type == NumberType) {
         DOUBLE_TO_NPVARIANT(value->toNumber(exec), *result);
@@ -110,7 +88,7 @@ void convertValueToNPVariant(ExecState *exec, JSValue *value, NPVariant *result)
     } else if (type == ObjectType) {
         JSObject* object = static_cast<JSObject*>(value);
         if (object->classInfo() == &RuntimeObjectImp::info) {
-            RuntimeObjectImp* imp = static_cast<RuntimeObjectImp *>(value);
+            RuntimeObjectImp* imp = static_cast<RuntimeObjectImp*>(value);
             CInstance* instance = static_cast<CInstance*>(imp->getInternalInstance());
             if (instance) {
                 NPObject* obj = instance->getObject();
@@ -129,7 +107,7 @@ void convertValueToNPVariant(ExecState *exec, JSValue *value, NPVariant *result)
     }
 }
 
-JSValue *convertNPVariantToValue(ExecState*, const NPVariant* variant, RootObject* rootObject)
+JSValueconvertNPVariantToValue(ExecState*, const NPVariant* variant, RootObject* rootObject)
 {
     JSLock lock;
     
@@ -145,20 +123,14 @@ JSValue *convertNPVariantToValue(ExecState*, const NPVariant* variant, RootObjec
         return jsNumber(NPVARIANT_TO_INT32(*variant));
     if (type == NPVariantType_Double)
         return jsNumber(NPVARIANT_TO_DOUBLE(*variant));
-    if (type == NPVariantType_String) {
-        NPUTF16 *stringValue;
-        unsigned int UTF16Length;
-        convertNPStringToUTF16(&variant->value.stringValue, &stringValue, &UTF16Length); // requires free() of returned memory
-        UString resultString((const UChar *)stringValue,UTF16Length);
-        free(stringValue);
-        return jsString(resultString);
-    }
+    if (type == NPVariantType_String)
+        return jsString(convertNPStringToUTF16(&variant->value.stringValue));
     if (type == NPVariantType_Object) {
-        NPObject *obj = variant->value.objectValue;
+        NPObjectobj = variant->value.objectValue;
         
         if (obj->_class == NPScriptObjectClass)
             // Get JSObject from NP_JavaScriptObject.
-            return ((JavaScriptObject *)obj)->imp;
+            return ((JavaScriptObject*)obj)->imp;
 
         // Wrap NPObject in a CInstance.
         return Instance::createRuntimeObject(Instance::CLanguage, obj, rootObject);
@@ -167,20 +139,14 @@ JSValue *convertNPVariantToValue(ExecState*, const NPVariant* variant, RootObjec
     return jsUndefined();
 }
 
-// Requires free() of returned UTF16Chars.
-void convertNPStringToUTF16(const NPString *string, NPUTF16 **UTF16Chars, unsigned int *UTF16Length)
+String convertNPStringToUTF16(const NPString* string)
 {
-    convertUTF8ToUTF16WithLatin1Fallback(string->UTF8Characters, string->UTF8Length, UTF16Chars, UTF16Length);
+    return convertUTF8ToUTF16WithLatin1Fallback(string->UTF8Characters, string->UTF8Length);
 }
 
 Identifier identifierFromNPIdentifier(const NPUTF8* name)
 {
-    NPUTF16 *methodName;
-    unsigned UTF16Length;
-    convertUTF8ToUTF16WithLatin1Fallback(name, -1, &methodName, &UTF16Length); // requires free() of returned memory.
-    Identifier identifier((const KJS::UChar*)methodName, UTF16Length);
-    free(methodName);
-    return identifier;
+    return convertUTF8ToUTF16WithLatin1Fallback(name, -1);
 }
 
 } }
index 18bc0d499c69ecc26f6891fca2e2c24e3bb2b16b..bb5434e349171f06259b8025a4033483aec5f27c 100644 (file)
 
 #include "npruntime_internal.h"
 
+namespace WebCore {
+    class String;
+}
+
 namespace KJS {
 
 class ExecState;
@@ -52,7 +56,7 @@ enum NP_ValueType {
     NP_InvalidValueType
 };
 
-void convertNPStringToUTF16(const NPString*, NPUTF16** UTF16Chars, unsigned int* UTF16Length);
+WebCore::String convertNPStringToUTF16(const NPString *string);
 void convertValueToNPVariant(ExecState*, JSValue*, NPVariant* result);
 JSValue* convertNPVariantToValue(ExecState*, const NPVariant*, RootObject*);
 Identifier identifierFromNPIdentifier(const NPUTF8* name);