Reviewed by Geoffrey Garen.
authorap@webkit.org <ap@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 13 May 2008 09:46:47 +0000 (09:46 +0000)
committerap@webkit.org <ap@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 13 May 2008 09:46:47 +0000 (09:46 +0000)
        <rdar://problem/4949018> JavaScriptCore API claims to work with UTF8 strings, but only works
        with ASCII strings

        * kjs/ustring.h:
        * kjs/ustring.cpp:
        (KJS::UString::Rep::createFromUTF8):
        Added. Implementation adapted from JSStringCreateWithUTF8CString().

        * API/JSStringRef.cpp:
        (JSStringCreateWithUTF8CString):
        * API/JSClassRef.cpp:
        (OpaqueJSClass::OpaqueJSClass):
        Use UString::Rep::createFromUTF8().

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

JavaScriptCore/API/JSClassRef.cpp
JavaScriptCore/API/JSStringRef.cpp
JavaScriptCore/ChangeLog
JavaScriptCore/kjs/ustring.cpp
JavaScriptCore/kjs/ustring.h

index ee1717c..2c01a40 100644 (file)
@@ -39,8 +39,7 @@ using namespace KJS;
 const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
 OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass) 
-    // FIXME: <rdar://problem/4949018>
-    : className(definition->className)
+    : className(UString::Rep::createFromUTF8(definition->className))
     , parentClass(definition->parentClass)
     , prototypeClass(0)
     , staticValues(0)
@@ -61,8 +60,7 @@ OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass*
     if (const JSStaticValue* staticValue = definition->staticValues) {
         staticValues = new StaticValuesTable();
         while (staticValue->name) {
-            // FIXME: <rdar://problem/4949018>
-            staticValues->add(Identifier(staticValue->name).ustring().rep(), 
+            staticValues->add(Identifier(UString::Rep::createFromUTF8(staticValue->name).get()).ustring().rep(), 
                               new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes));
             ++staticValue;
         }
@@ -71,8 +69,7 @@ OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass*
     if (const JSStaticFunction* staticFunction = definition->staticFunctions) {
         staticFunctions = new StaticFunctionsTable();
         while (staticFunction->name) {
-            // FIXME: <rdar://problem/4949018>
-            staticFunctions->add(Identifier(staticFunction->name).ustring().rep(), 
+            staticFunctions->add(Identifier(UString::Rep::createFromUTF8(staticFunction->name).get()).ustring().rep(), 
                                  new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes));
             ++staticFunction;
         }
index 9e9f06d..13dfae6 100644 (file)
@@ -51,13 +51,11 @@ JSStringRef JSStringCreateWithUTF8CString(const char* string)
 {
     JSLock lock;
 
-    size_t length = strlen(string);
-    Vector<UChar, 1024> buffer(length);
-    UChar* p = buffer.data();
-    if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
+    RefPtr<UString::Rep> result = UString::Rep::createFromUTF8(string);
+    if (result.get() == &UString::Rep::null)
         return 0;
 
-    return toRef(UString(buffer.data(), p - buffer.data()).rep()->ref());
+    return toRef(result.release().releaseRef());
 }
 
 JSStringRef JSStringRetain(JSStringRef string)
index fae525f..a2cfa81 100644 (file)
@@ -1,3 +1,21 @@
+2008-05-13  Alexey Proskuryakov  <ap@webkit.org>
+
+        Reviewed by Geoffrey Garen.
+
+        <rdar://problem/4949018> JavaScriptCore API claims to work with UTF8 strings, but only works
+        with ASCII strings
+        
+        * kjs/ustring.h:
+        * kjs/ustring.cpp:
+        (KJS::UString::Rep::createFromUTF8):
+        Added. Implementation adapted from JSStringCreateWithUTF8CString().
+
+        * API/JSStringRef.cpp:
+        (JSStringCreateWithUTF8CString):
+        * API/JSClassRef.cpp:
+        (OpaqueJSClass::OpaqueJSClass):
+        Use UString::Rep::createFromUTF8().
+
 2008-05-12  Mark Rowe  <mrowe@apple.com>
 
         Reviewed by Tim Hatcher.
index 152e2d5..4b351a8 100644 (file)
@@ -238,6 +238,20 @@ PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<Rep> base, int offset,
   return adoptRef(r);
 }
 
+PassRefPtr<UString::Rep> UString::Rep::createFromUTF8(const char* string)
+{
+    if (!string)
+        return &UString::Rep::null;
+
+    size_t length = strlen(string);
+    Vector<UChar, 1024> buffer(length);
+    UChar* p = buffer.data();
+    if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
+        return &UString::Rep::null;
+
+    return UString::Rep::createCopying(buffer.data(), p - buffer.data());
+}
+
 void UString::Rep::destroy()
 {
   // Static null and empty strings can never be destroyed, but we cannot rely on reference counting, because ref/deref are not thread-safe.
index 33b5809..2e44949 100644 (file)
@@ -94,6 +94,10 @@ namespace KJS {
       static PassRefPtr<Rep> createCopying(const UChar *d, int l);
       static PassRefPtr<Rep> create(PassRefPtr<Rep> base, int offset, int length);
 
+      // Constructs a string from a UTF-8 string, using strict conversion (see comments in UTF8.h).
+      // Returns UString::Rep::null for null input or conversion failure.
+      static PassRefPtr<Rep> createFromUTF8(const char*);
+
       void destroy();
       
       bool baseIsSelf() const { return baseString == this; }