Backed out changeset b4c7d481bf10 (bug 1289003)
authorTooru Fujisawa <arai_a@mac.com>
Fri, 02 Sep 2016 21:23:03 +0900
changeset 312425 d18edfa7a8f9d0d9ed6b48c6038039f149c46d1d
parent 312424 6135cb7ef5bda300672ce7290ccf905b2cab68ff
child 312426 075d612841fb1ca14cd83be2d6615cafb993d56a
push id20447
push userkwierso@gmail.com
push dateFri, 02 Sep 2016 20:36:44 +0000
treeherderfx-team@969397f22187 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1289003
milestone51.0a1
backs outb4c7d481bf10a39a99cb41b89bc677434d80dc0c
Backed out changeset b4c7d481bf10 (bug 1289003)
js/public/CharacterEncoding.h
js/src/vm/CharacterEncoding.cpp
--- a/js/public/CharacterEncoding.h
+++ b/js/public/CharacterEncoding.h
@@ -26,18 +26,16 @@ namespace JS {
  * byte is treated as a 2-byte character, and there is no way to pass in a
  * string containing characters beyond U+00FF.
  */
 class Latin1Chars : public mozilla::Range<Latin1Char>
 {
     typedef mozilla::Range<Latin1Char> Base;
 
   public:
-    using CharT = Latin1Char;
-
     Latin1Chars() : Base() {}
     Latin1Chars(char* aBytes, size_t aLength) : Base(reinterpret_cast<Latin1Char*>(aBytes), aLength) {}
     Latin1Chars(const Latin1Char* aBytes, size_t aLength)
       : Base(const_cast<Latin1Char*>(aBytes), aLength)
     {}
     Latin1Chars(const char* aBytes, size_t aLength)
       : Base(reinterpret_cast<Latin1Char*>(const_cast<char*>(aBytes)), aLength)
     {}
@@ -46,18 +44,16 @@ class Latin1Chars : public mozilla::Rang
 /*
  * A Latin1Chars, but with \0 termination for C compatibility.
  */
 class Latin1CharsZ : public mozilla::RangedPtr<Latin1Char>
 {
     typedef mozilla::RangedPtr<Latin1Char> Base;
 
   public:
-    using CharT = Latin1Char;
-
     Latin1CharsZ() : Base(nullptr, 0) {}
 
     Latin1CharsZ(char* aBytes, size_t aLength)
       : Base(reinterpret_cast<Latin1Char*>(aBytes), aLength)
     {
         MOZ_ASSERT(aBytes[aLength] == '\0');
     }
 
@@ -72,18 +68,16 @@ class Latin1CharsZ : public mozilla::Ran
     char* c_str() { return reinterpret_cast<char*>(get()); }
 };
 
 class UTF8Chars : public mozilla::Range<unsigned char>
 {
     typedef mozilla::Range<unsigned char> Base;
 
   public:
-    using CharT = unsigned char;
-
     UTF8Chars() : Base() {}
     UTF8Chars(char* aBytes, size_t aLength)
       : Base(reinterpret_cast<unsigned char*>(aBytes), aLength)
     {}
     UTF8Chars(const char* aBytes, size_t aLength)
       : Base(reinterpret_cast<unsigned char*>(const_cast<char*>(aBytes)), aLength)
     {}
 };
@@ -91,18 +85,16 @@ class UTF8Chars : public mozilla::Range<
 /*
  * SpiderMonkey also deals directly with UTF-8 encoded text in some places.
  */
 class UTF8CharsZ : public mozilla::RangedPtr<unsigned char>
 {
     typedef mozilla::RangedPtr<unsigned char> Base;
 
   public:
-    using CharT = unsigned char;
-
     UTF8CharsZ() : Base(nullptr, 0) {}
 
     UTF8CharsZ(char* aBytes, size_t aLength)
       : Base(reinterpret_cast<unsigned char*>(aBytes), aLength)
     {
         MOZ_ASSERT(aBytes[aLength] == '\0');
     }
 
@@ -123,18 +115,16 @@ class UTF8CharsZ : public mozilla::Range
  * to others.  This differs from UTF8CharsZ in that the chars are
  * const and it allows assignment.
  */
 class ConstUTF8CharsZ
 {
     const char* data_;
 
   public:
-    using CharT = unsigned char;
-
     ConstUTF8CharsZ() : data_(nullptr)
     {}
 
     ConstUTF8CharsZ(const char* aBytes, size_t aLength)
       : data_(aBytes)
     {
         MOZ_ASSERT(aBytes[aLength] == '\0');
 #ifdef DEBUG
@@ -162,33 +152,29 @@ class ConstUTF8CharsZ
  * manually interpreting UTF-16 extension characters embedded in the JS
  * string.
  */
 class TwoByteChars : public mozilla::Range<char16_t>
 {
     typedef mozilla::Range<char16_t> Base;
 
   public:
-    using CharT = char16_t;
-
     TwoByteChars() : Base() {}
     TwoByteChars(char16_t* aChars, size_t aLength) : Base(aChars, aLength) {}
     TwoByteChars(const char16_t* aChars, size_t aLength) : Base(const_cast<char16_t*>(aChars), aLength) {}
 };
 
 /*
  * A TwoByteChars, but \0 terminated for compatibility with JSFlatString.
  */
 class TwoByteCharsZ : public mozilla::RangedPtr<char16_t>
 {
     typedef mozilla::RangedPtr<char16_t> Base;
 
   public:
-    using CharT = char16_t;
-
     TwoByteCharsZ() : Base(nullptr, 0) {}
 
     TwoByteCharsZ(char16_t* chars, size_t length)
       : Base(chars, length)
     {
         MOZ_ASSERT(chars[length] == '\0');
     }
 
@@ -200,18 +186,16 @@ typedef mozilla::RangedPtr<const char16_
 /*
  * Like TwoByteChars, but the chars are const.
  */
 class ConstTwoByteChars : public mozilla::Range<const char16_t>
 {
     typedef mozilla::Range<const char16_t> Base;
 
   public:
-    using CharT = char16_t;
-
     ConstTwoByteChars() : Base() {}
     ConstTwoByteChars(const char16_t* aChars, size_t aLength) : Base(aChars, aLength) {}
 };
 
 /*
  * Convert a 2-byte character sequence to "ISO-Latin-1". This works by
  * truncating each 2-byte pair in the sequence to a 1-byte pair. If the source
  * contains any UTF-16 extension characters, then this may give invalid Latin1
@@ -283,31 +267,14 @@ GetDeflatedUTF8StringLength(JSFlatString
  * the number of Unicode characters written to the buffer (which can be less
  * than the length of the string, if the buffer is exhausted before the string
  * is fully encoded).
  */
 JS_PUBLIC_API(void)
 DeflateStringToUTF8Buffer(JSFlatString* src, mozilla::RangedPtr<char> dst,
                           size_t* dstlenp = nullptr, size_t* numcharsp = nullptr);
 
-/*
-  * Return a null-terminated Latin-1 string copied from the input string,
-  * storing its length (excluding null terminator) in |*outlen|.  Fail and
-  * report an error if the string contains non-Latin-1 codepoints.  Returns
-  * Latin1CharsZ() on failure.
- */
-extern Latin1CharsZ
-UTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen);
-
-/*
- * Return a null-terminated Latin-1 string copied from the input string,
- * storing its length (excluding null terminator) in |*outlen|.  Non-Latin-1
- * codepoints are replaced by '?'.  Returns Latin1CharsZ() on failure.
- */
-extern Latin1CharsZ
-LossyUTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen);
-
 } // namespace JS
 
 inline void JS_free(JS::Latin1CharsZ& ptr) { js_free((void*)ptr.get()); }
 inline void JS_free(JS::UTF8CharsZ& ptr) { js_free((void*)ptr.get()); }
 
 #endif /* js_CharacterEncoding_h */
--- a/js/src/vm/CharacterEncoding.cpp
+++ b/js/src/vm/CharacterEncoding.cpp
@@ -3,18 +3,16 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "js/CharacterEncoding.h"
 
 #include "mozilla/Range.h"
 
-#include <type_traits>
-
 #include "jscntxt.h"
 #include "jsprf.h"
 
 using namespace js;
 
 Latin1CharsZ
 JS::LossyTwoByteCharsToNewLatin1CharsZ(js::ExclusiveContext* cx,
                                        const mozilla::Range<const char16_t> tbchars)
@@ -250,39 +248,38 @@ ReportTooBigCharacter(JSContext* cx, uin
 
 enum InflateUTF8Action {
     CountAndReportInvalids,
     CountAndIgnoreInvalids,
     AssertNoInvalids,
     Copy
 };
 
-static const char16_t REPLACE_UTF8 = 0xFFFD;
-static const Latin1Char REPLACE_UTF8_LATIN1 = '?';
+static const uint32_t REPLACE_UTF8 = 0xFFFD;
 
 // If making changes to this algorithm, make sure to also update
 // LossyConvertUTF8toUTF16() in dom/wifi/WifiUtils.cpp
-template <InflateUTF8Action Action, typename CharT>
+template <InflateUTF8Action Action>
 static bool
-InflateUTF8StringToBuffer(JSContext* cx, const UTF8Chars src, CharT* dst, size_t* dstlenp,
+InflateUTF8StringToBuffer(JSContext* cx, const UTF8Chars src, char16_t* dst, size_t* dstlenp,
                           bool* isAsciip)
 {
     if (Action != AssertNoInvalids)
         *isAsciip = true;
 
-    // Count how many code units need to be in the inflated string.
+    // Count how many char16_t characters need to be in the inflated string.
     // |i| is the index into |src|, and |j| is the the index into |dst|.
     size_t srclen = src.length();
     uint32_t j = 0;
     for (uint32_t i = 0; i < srclen; i++, j++) {
         uint32_t v = uint32_t(src[i]);
         if (!(v & 0x80)) {
             // ASCII code unit.  Simple copy.
             if (Action == Copy)
-                dst[j] = CharT(v);
+                dst[j] = char16_t(v);
 
         } else {
             // Non-ASCII code unit.  Determine its length in bytes (n).
             if (Action != AssertNoInvalids)
                 *isAsciip = false;
             uint32_t n = 1;
             while (v & (0x80 >> n))
                 n++;
@@ -290,24 +287,20 @@ InflateUTF8StringToBuffer(JSContext* cx,
         #define INVALID(report, arg, n2)                                \
             do {                                                        \
                 if (Action == CountAndReportInvalids) {                 \
                     report(cx, arg);                                    \
                     return false;                                       \
                 } else if (Action == AssertNoInvalids) {                \
                     MOZ_CRASH("invalid UTF-8 string: " # report);       \
                 } else {                                                \
-                    if (Action == Copy) {                               \
-                        if (std::is_same<decltype(dst[0]), Latin1Char>::value) \
-                            dst[j] = CharT(REPLACE_UTF8_LATIN1);        \
-                        else                                            \
-                            dst[j] = CharT(REPLACE_UTF8);               \
-                    } else {                                            \
+                    if (Action == Copy)                                 \
+                        dst[j] = char16_t(REPLACE_UTF8);                \
+                    else                                                \
                         MOZ_ASSERT(Action == CountAndIgnoreInvalids);   \
-                    }                                                   \
                     n = n2;                                             \
                     goto invalidMultiByteCodeUnit;                      \
                 }                                                       \
             } while (0)
 
             // Check the leading byte.
             if (n < 2 || n > 4)
                 INVALID(ReportInvalidCharacter, i, 1);
@@ -326,34 +319,35 @@ InflateUTF8StringToBuffer(JSContext* cx,
                 INVALID(ReportInvalidCharacter, i, 1);
             }
 
             // Check the continuation bytes.
             for (uint32_t m = 1; m < n; m++)
                 if ((src[i + m] & 0xC0) != 0x80)
                     INVALID(ReportInvalidCharacter, i, m);
 
-            // Determine the code unit's length in CharT and act accordingly.
+            // Determine the code unit's length in char16_t and act accordingly.
             v = JS::Utf8ToOneUcs4Char((uint8_t*)&src[i], n);
             if (v < 0x10000) {
-                // The n-byte UTF8 code unit will fit in a single CharT.
+                // The n-byte UTF8 code unit will fit in a single char16_t.
                 if (Action == Copy)
-                    dst[j] = CharT(v);
+                    dst[j] = char16_t(v);
+
             } else {
                 v -= 0x10000;
                 if (v <= 0xFFFFF) {
-                    // The n-byte UTF8 code unit will fit in two CharT units.
+                    // The n-byte UTF8 code unit will fit in two char16_t units.
                     if (Action == Copy)
-                        dst[j] = CharT((v >> 10) + 0xD800);
+                        dst[j] = char16_t((v >> 10) + 0xD800);
                     j++;
                     if (Action == Copy)
-                        dst[j] = CharT((v & 0x3FF) + 0xDC00);
+                        dst[j] = char16_t((v & 0x3FF) + 0xDC00);
 
                 } else {
-                    // The n-byte UTF8 code unit won't fit in two CharT units.
+                    // The n-byte UTF8 code unit won't fit in two char16_t units.
                     INVALID(ReportTooBigCharacter, v, 1);
                 }
             }
 
           invalidMultiByteCodeUnit:
             // Move i to the last byte of the multi-byte code unit;  the loop
             // header will do the final i++ to move to the start of the next
             // code unit.
@@ -362,87 +356,74 @@ InflateUTF8StringToBuffer(JSContext* cx,
     }
 
     if (Action != AssertNoInvalids)
         *dstlenp = j;
 
     return true;
 }
 
-template <InflateUTF8Action Action, typename CharsT>
-static CharsT
+template <InflateUTF8Action Action>
+static TwoByteCharsZ
 InflateUTF8StringHelper(JSContext* cx, const UTF8Chars src, size_t* outlen)
 {
-    using CharT = typename CharsT::CharT;
     *outlen = 0;
 
     bool isAscii;
-    if (!InflateUTF8StringToBuffer<Action, CharT>(cx, src, /* dst = */ nullptr, outlen, &isAscii))
-        return CharsT();
+    if (!InflateUTF8StringToBuffer<Action>(cx, src, /* dst = */ nullptr, outlen, &isAscii))
+        return TwoByteCharsZ();
 
-    CharT* dst = cx->pod_malloc<CharT>(*outlen + 1);  // +1 for NUL
+    char16_t* dst = cx->pod_malloc<char16_t>(*outlen + 1);  // +1 for NUL
     if (!dst) {
         ReportOutOfMemory(cx);
-        return CharsT();
+        return TwoByteCharsZ();
     }
 
     if (isAscii) {
         size_t srclen = src.length();
         MOZ_ASSERT(*outlen == srclen);
         for (uint32_t i = 0; i < srclen; i++)
-            dst[i] = CharT(src[i]);
+            dst[i] = char16_t(src[i]);
+
     } else {
-        JS_ALWAYS_TRUE((InflateUTF8StringToBuffer<Copy, CharT>(cx, src, dst, outlen, &isAscii)));
+        JS_ALWAYS_TRUE(InflateUTF8StringToBuffer<Copy>(cx, src, dst, outlen, &isAscii));
     }
 
     dst[*outlen] = 0;    // NUL char
 
-    return CharsT(dst, *outlen);
+    return TwoByteCharsZ(dst, *outlen);
 }
 
 TwoByteCharsZ
 JS::UTF8CharsToNewTwoByteCharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen)
 {
-    return InflateUTF8StringHelper<CountAndReportInvalids, TwoByteCharsZ>(cx, utf8, outlen);
+    return InflateUTF8StringHelper<CountAndReportInvalids>(cx, utf8, outlen);
 }
 
 TwoByteCharsZ
 JS::UTF8CharsToNewTwoByteCharsZ(JSContext* cx, const ConstUTF8CharsZ& utf8, size_t* outlen)
 {
     UTF8Chars chars(utf8.c_str(), strlen(utf8.c_str()));
-    return InflateUTF8StringHelper<CountAndReportInvalids, TwoByteCharsZ>(cx, chars, outlen);
+    return InflateUTF8StringHelper<CountAndReportInvalids>(cx, chars, outlen);
 }
 
 TwoByteCharsZ
 JS::LossyUTF8CharsToNewTwoByteCharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen)
 {
-    return InflateUTF8StringHelper<CountAndIgnoreInvalids, TwoByteCharsZ>(cx, utf8, outlen);
+    return InflateUTF8StringHelper<CountAndIgnoreInvalids>(cx, utf8, outlen);
 }
 
 TwoByteCharsZ
 JS::LossyUTF8CharsToNewTwoByteCharsZ(JSContext* cx, const ConstUTF8CharsZ& utf8, size_t* outlen)
 {
     UTF8Chars chars(utf8.c_str(), strlen(utf8.c_str()));
-    return InflateUTF8StringHelper<CountAndIgnoreInvalids, TwoByteCharsZ>(cx, chars, outlen);
-}
-
-Latin1CharsZ
-JS::UTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen)
-{
-    return InflateUTF8StringHelper<CountAndReportInvalids, Latin1CharsZ>(cx, utf8, outlen);
-}
-
-Latin1CharsZ
-JS::LossyUTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen)
-{
-    return InflateUTF8StringHelper<CountAndIgnoreInvalids, Latin1CharsZ>(cx, utf8, outlen);
+    return InflateUTF8StringHelper<CountAndIgnoreInvalids>(cx, chars, outlen);
 }
 
 #ifdef DEBUG
 void
 JS::ConstUTF8CharsZ::validate(size_t aLength)
 {
     MOZ_ASSERT(data_);
     UTF8Chars chars(data_, aLength);
-    InflateUTF8StringToBuffer<AssertNoInvalids, char16_t>(nullptr, chars, nullptr, nullptr,
-                                                          nullptr);
+    InflateUTF8StringToBuffer<AssertNoInvalids>(nullptr, chars, nullptr, nullptr, nullptr);
 }
 #endif