Bug 1037100 - Replace a bunch of uses of Scoped with uses of UniquePtr in ICU-related code, and remove a couple pointless Scoped.h #includes. r=anba
authorJeff Walden <jwalden@mit.edu>
Mon, 25 Feb 2019 15:02:01 -0800
changeset 519615 7a0f04b2ce3e97fbe8f37d1955e253d677a69a05
parent 519614 92516792e2aed92088ae8e6054aad22a4c8ef99f
child 519616 9eb404c0248ec0360f88b1785cfde7b6dec56ed0
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersanba
bugs1037100
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1037100 - Replace a bunch of uses of Scoped with uses of UniquePtr in ICU-related code, and remove a couple pointless Scoped.h #includes. r=anba
intl/unicharutil/util/ICUUtils.cpp
intl/unicharutil/util/ICUUtils.h
js/src/vm/Runtime.h
js/src/vm/UbiNode.cpp
--- a/intl/unicharutil/util/ICUUtils.cpp
+++ b/intl/unicharutil/util/ICUUtils.cpp
@@ -1,26 +1,40 @@
 /* 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/. */
 
 #ifdef MOZILLA_INTERNAL_API
 
+#include "mozilla/Assertions.h"
+#include "mozilla/UniquePtr.h"
+
 #  include "ICUUtils.h"
 #  include "mozilla/Preferences.h"
 #  include "mozilla/intl/LocaleService.h"
 #  include "nsIContent.h"
 #  include "mozilla/dom/Document.h"
 #  include "nsString.h"
 #  include "unicode/uloc.h"
 #  include "unicode/unum.h"
 
 using namespace mozilla;
 using mozilla::intl::LocaleService;
 
+class NumberFormatDeleter {
+ public:
+  void operator()(UNumberFormat* aPtr) {
+    MOZ_ASSERT(aPtr != nullptr,
+               "UniquePtr deleter shouldn't be called for nullptr");
+    unum_close(aPtr);
+  }
+};
+
+using UniqueUNumberFormat = UniquePtr<UNumberFormat, NumberFormatDeleter>;
+
 /**
  * This pref just controls whether we format the number with grouping separator
  * characters when the internal value is set or updated. It does not stop the
  * user from typing in a number and using grouping separators.
  */
 static bool gLocaleNumberGroupingEnabled;
 static const char LOCALE_NUMBER_GROUPING_PREF_STR[] =
     "dom.forms.number.grouping";
@@ -86,32 +100,32 @@ void ICUUtils::LanguageTagIterForContent
   static const int32_t kBufferSize = 256;
 
   UChar buffer[kBufferSize];
 
   nsAutoCString langTag;
   aLangTags.GetNext(langTag);
   while (!langTag.IsEmpty()) {
     UErrorCode status = U_ZERO_ERROR;
-    AutoCloseUNumberFormat format(
+    UniqueUNumberFormat format(
         unum_open(UNUM_DECIMAL, nullptr, 0, langTag.get(), nullptr, &status));
     // Since unum_setAttribute have no UErrorCode parameter, we have to
     // check error status.
     if (U_FAILURE(status)) {
       aLangTags.GetNext(langTag);
       continue;
     }
-    unum_setAttribute(format, UNUM_GROUPING_USED,
+    unum_setAttribute(format.get(), UNUM_GROUPING_USED,
                       LocaleNumberGroupingIsEnabled());
     // ICU default is a maximum of 3 significant fractional digits. We don't
     // want that limit, so we set it to the maximum that a double can represent
     // (14-16 decimal fractional digits).
-    unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, 16);
-    int32_t length = unum_formatDouble(format, aValue, buffer, kBufferSize,
-                                       nullptr, &status);
+    unum_setAttribute(format.get(), UNUM_MAX_FRACTION_DIGITS, 16);
+    int32_t length = unum_formatDouble(format.get(), aValue, buffer,
+                                       kBufferSize, nullptr, &status);
     NS_ASSERTION(length < kBufferSize && status != U_BUFFER_OVERFLOW_ERROR &&
                      status != U_STRING_NOT_TERMINATED_WARNING,
                  "Need a bigger buffer?!");
     if (U_SUCCESS(status)) {
       ICUUtils::AssignUCharArrayToString(buffer, length, aLocalizedValue);
       return true;
     }
     aLangTags.GetNext(langTag);
@@ -128,26 +142,27 @@ void ICUUtils::LanguageTagIterForContent
   }
 
   uint32_t length = aValue.Length();
 
   nsAutoCString langTag;
   aLangTags.GetNext(langTag);
   while (!langTag.IsEmpty()) {
     UErrorCode status = U_ZERO_ERROR;
-    AutoCloseUNumberFormat format(
+    UniqueUNumberFormat format(
         unum_open(UNUM_DECIMAL, nullptr, 0, langTag.get(), nullptr, &status));
     if (!LocaleNumberGroupingIsEnabled()) {
-      unum_setAttribute(format.rwget(), UNUM_GROUPING_USED, UBool(0));
+      unum_setAttribute(format.get(), UNUM_GROUPING_USED, UBool(0));
     }
     int32_t parsePos = 0;
     static_assert(sizeof(UChar) == 2 && sizeof(nsAString::char_type) == 2,
                   "Unexpected character size - the following cast is unsafe");
     double val =
-        unum_parseDouble(format, (const UChar*)PromiseFlatString(aValue).get(),
+        unum_parseDouble(format.get(),
+                         (const UChar*)PromiseFlatString(aValue).get(),
                          length, &parsePos, &status);
     if (U_SUCCESS(status) && parsePos == (int32_t)length) {
       return val;
     }
     aLangTags.GetNext(langTag);
   }
   return std::numeric_limits<float>::quiet_NaN();
 }
--- a/intl/unicharutil/util/ICUUtils.h
+++ b/intl/unicharutil/util/ICUUtils.h
@@ -5,31 +5,21 @@
 
 #ifndef mozilla_ICUUtils_h__
 #define mozilla_ICUUtils_h__
 
 // The ICU utils implementation needs internal things like XPCOM strings and
 // nsGkAtom, so we only build when included into internal libs:
 #ifdef MOZILLA_INTERNAL_API
 
-#  include "mozilla/Scoped.h"
 #  include "nsString.h"
 #  include "unicode/unum.h"  // for UNumberFormat
 
 class nsIContent;
 
-struct ScopedUNumberFormatTraits {
-  typedef UNumberFormat* type;
-  static type empty() { return nullptr; }
-  static void release(type handle) {
-    if (handle) unum_close(handle);
-  }
-};
-typedef mozilla::Scoped<ScopedUNumberFormatTraits> AutoCloseUNumberFormat;
-
 class ICUUtils {
  public:
   /**
    * This class is used to encapsulate an nsIContent object to allow lazy
    * iteration over its primary and fallback BCP 47 language tags.
    */
   class LanguageTagIterForContent {
    public:
--- a/js/src/vm/Runtime.h
+++ b/js/src/vm/Runtime.h
@@ -9,17 +9,16 @@
 
 #include "mozilla/Atomics.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/DoublyLinkedList.h"
 #include "mozilla/LinkedList.h"
 #include "mozilla/Maybe.h"
 #include "mozilla/MaybeOneOf.h"
 #include "mozilla/MemoryReporting.h"
-#include "mozilla/Scoped.h"
 #include "mozilla/ThreadLocal.h"
 #include "mozilla/TimeStamp.h"
 #include "mozilla/Vector.h"
 
 #include <algorithm>
 #include <setjmp.h>
 
 #include "builtin/AtomicsObject.h"
--- a/js/src/vm/UbiNode.cpp
+++ b/js/src/vm/UbiNode.cpp
@@ -4,17 +4,16 @@
  * 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/UbiNode.h"
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/Range.h"
-#include "mozilla/Scoped.h"
 
 #include <algorithm>
 
 #include "builtin/String.h"
 
 #include "jit/IonCode.h"
 #include "js/Debug.h"
 #include "js/TracingAPI.h"