LCOV - code coverage report
Current view: top level - src/objects - intl-objects.h (source / functions) Hit Total Coverage
Test: app.info Lines: 11 12 91.7 %
Date: 2019-01-20 Functions: 17 17 100.0 %

          Line data    Source code
       1             : // Copyright 2013 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #ifndef V8_INTL_SUPPORT
       6             : #error Internationalization is expected to be enabled.
       7             : #endif  // V8_INTL_SUPPORT
       8             : 
       9             : #ifndef V8_OBJECTS_INTL_OBJECTS_H_
      10             : #define V8_OBJECTS_INTL_OBJECTS_H_
      11             : 
      12             : #include <map>
      13             : #include <set>
      14             : #include <string>
      15             : 
      16             : #include "src/base/timezone-cache.h"
      17             : #include "src/contexts.h"
      18             : #include "src/objects.h"
      19             : #include "src/objects/managed.h"
      20             : #include "unicode/locid.h"
      21             : #include "unicode/uversion.h"
      22             : 
      23             : #define V8_MINIMUM_ICU_VERSION 63
      24             : 
      25             : namespace U_ICU_NAMESPACE {
      26             : class BreakIterator;
      27             : class Collator;
      28             : class DecimalFormat;
      29             : class SimpleDateFormat;
      30             : class UnicodeString;
      31             : }
      32             : 
      33             : namespace v8 {
      34             : namespace internal {
      35             : 
      36             : template <typename T>
      37             : class Handle;
      38             : class JSCollator;
      39             : 
      40             : class Intl {
      41             :  public:
      42             :   enum class BoundFunctionContextSlot {
      43             :     kBoundFunction = Context::MIN_CONTEXT_SLOTS,
      44             :     kLength
      45             :   };
      46             : 
      47             :   // Build a set of ICU locales from a list of Locales. If there is a locale
      48             :   // with a script tag then the locales also include a locale without the
      49             :   // script; eg, pa_Guru_IN (language=Panjabi, script=Gurmukhi, country-India)
      50             :   // would include pa_IN.
      51             :   static std::set<std::string> BuildLocaleSet(
      52             :       const icu::Locale* icu_available_locales, int32_t count);
      53             : 
      54             :   static Maybe<std::string> ToLanguageTag(const icu::Locale& locale);
      55             : 
      56             :   // Get the name of the numbering system from locale.
      57             :   // ICU doesn't expose numbering system in any way, so we have to assume that
      58             :   // for given locale NumberingSystem constructor produces the same digits as
      59             :   // NumberFormat/Calendar would.
      60             :   static std::string GetNumberingSystem(const icu::Locale& icu_locale);
      61             : 
      62             :   static V8_WARN_UNUSED_RESULT MaybeHandle<JSObject> SupportedLocalesOf(
      63             :       Isolate* isolate, const char* method,
      64             :       const std::set<std::string>& available_locales, Handle<Object> locales_in,
      65             :       Handle<Object> options_in);
      66             : 
      67             :   // ECMA402 9.2.10. GetOption( options, property, type, values, fallback)
      68             :   // ecma402/#sec-getoption
      69             :   //
      70             :   // This is specialized for the case when type is string.
      71             :   //
      72             :   // Instead of passing undefined for the values argument as the spec
      73             :   // defines, pass in an empty vector.
      74             :   //
      75             :   // Returns true if options object has the property and stores the
      76             :   // result in value. Returns false if the value is not found. The
      77             :   // caller is required to use fallback value appropriately in this
      78             :   // case.
      79             :   //
      80             :   // service is a string denoting the type of Intl object; used when
      81             :   // printing the error message.
      82             :   V8_WARN_UNUSED_RESULT static Maybe<bool> GetStringOption(
      83             :       Isolate* isolate, Handle<JSReceiver> options, const char* property,
      84             :       std::vector<const char*> values, const char* service,
      85             :       std::unique_ptr<char[]>* result);
      86             : 
      87             :   // A helper template to get string from option into a enum.
      88             :   // The enum in the enum_values is the corresponding value to the strings
      89             :   // in the str_values. If the option does not contains name,
      90             :   // default_value will be return.
      91             :   template <typename T>
      92       35055 :   V8_WARN_UNUSED_RESULT static Maybe<T> GetStringOption(
      93             :       Isolate* isolate, Handle<JSReceiver> options, const char* name,
      94        8163 :       const char* method, const std::vector<const char*>& str_values,
      95        3978 :       const std::vector<T>& enum_values, T default_value) {
      96             :     DCHECK_EQ(str_values.size(), enum_values.size());
      97       35055 :     std::unique_ptr<char[]> cstr;
      98             :     Maybe<bool> found = Intl::GetStringOption(isolate, options, name,
      99       70110 :                                               str_values, method, &cstr);
     100       35055 :     MAYBE_RETURN(found, Nothing<T>());
     101       34911 :     if (found.FromJust()) {
     102             :       DCHECK_NOT_NULL(cstr.get());
     103       12348 :       for (size_t i = 0; i < str_values.size(); i++) {
     104       16326 :         if (strcmp(cstr.get(), str_values[i]) == 0) {
     105        3978 :           return Just(enum_values[i]);
     106             :         }
     107             :       }
     108           0 :       UNREACHABLE();
     109             :     }
     110             :     return Just(default_value);
     111             :   }
     112             : 
     113             :   // ECMA402 9.2.10. GetOption( options, property, type, values, fallback)
     114             :   // ecma402/#sec-getoption
     115             :   //
     116             :   // This is specialized for the case when type is boolean.
     117             :   //
     118             :   // Returns true if options object has the property and stores the
     119             :   // result in value. Returns false if the value is not found. The
     120             :   // caller is required to use fallback value appropriately in this
     121             :   // case.
     122             :   //
     123             :   // service is a string denoting the type of Intl object; used when
     124             :   // printing the error message.
     125             :   V8_WARN_UNUSED_RESULT static Maybe<bool> GetBoolOption(
     126             :       Isolate* isolate, Handle<JSReceiver> options, const char* property,
     127             :       const char* service, bool* result);
     128             : 
     129             :   // Canonicalize the locale.
     130             :   // https://tc39.github.io/ecma402/#sec-canonicalizelanguagetag,
     131             :   // including type check and structural validity check.
     132             :   static Maybe<std::string> CanonicalizeLanguageTag(Isolate* isolate,
     133             :                                                     Handle<Object> locale_in);
     134             : 
     135             :   // https://tc39.github.io/ecma402/#sec-canonicalizelocalelist
     136             :   // {only_return_one_result} is an optimization for callers that only
     137             :   // care about the first result.
     138             :   static Maybe<std::vector<std::string>> CanonicalizeLocaleList(
     139             :       Isolate* isolate, Handle<Object> locales,
     140             :       bool only_return_one_result = false);
     141             : 
     142             :   // ecma-402 #sec-intl.getcanonicallocales
     143             :   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> GetCanonicalLocales(
     144             :       Isolate* isolate, Handle<Object> locales);
     145             : 
     146             :   // For locale sensitive functions
     147             :   V8_WARN_UNUSED_RESULT static MaybeHandle<String> StringLocaleConvertCase(
     148             :       Isolate* isolate, Handle<String> s, bool is_upper,
     149             :       Handle<Object> locales);
     150             : 
     151             :   V8_WARN_UNUSED_RESULT static MaybeHandle<String> ConvertToUpper(
     152             :       Isolate* isolate, Handle<String> s);
     153             : 
     154             :   V8_WARN_UNUSED_RESULT static MaybeHandle<String> ConvertToLower(
     155             :       Isolate* isolate, Handle<String> s);
     156             : 
     157             :   V8_WARN_UNUSED_RESULT static MaybeHandle<Object> StringLocaleCompare(
     158             :       Isolate* isolate, Handle<String> s1, Handle<String> s2,
     159             :       Handle<Object> locales, Handle<Object> options);
     160             : 
     161             :   V8_WARN_UNUSED_RESULT static Handle<Object> CompareStrings(
     162             :       Isolate* isolate, const icu::Collator& collator, Handle<String> s1,
     163             :       Handle<String> s2);
     164             : 
     165             :   // ecma402/#sup-properties-of-the-number-prototype-object
     166             :   V8_WARN_UNUSED_RESULT static MaybeHandle<String> NumberToLocaleString(
     167             :       Isolate* isolate, Handle<Object> num, Handle<Object> locales,
     168             :       Handle<Object> options);
     169             : 
     170             :   // ecma402/#sec-setnfdigitoptions
     171             :   V8_WARN_UNUSED_RESULT static Maybe<bool> SetNumberFormatDigitOptions(
     172             :       Isolate* isolate, icu::DecimalFormat* number_format,
     173             :       Handle<JSReceiver> options, int mnfd_default, int mxfd_default);
     174             : 
     175             :   static icu::Locale CreateICULocale(const std::string& bcp47_locale);
     176             : 
     177             :   // Helper funciton to convert a UnicodeString to a Handle<String>
     178             :   V8_WARN_UNUSED_RESULT static MaybeHandle<String> ToString(
     179             :       Isolate* isolate, const icu::UnicodeString& string);
     180             : 
     181             :   // Helper function to convert a substring of UnicodeString to a Handle<String>
     182             :   V8_WARN_UNUSED_RESULT static MaybeHandle<String> ToString(
     183             :       Isolate* isolate, const icu::UnicodeString& string, int32_t begin,
     184             :       int32_t end);
     185             : 
     186             :   // A helper function to implement formatToParts which add element to array as
     187             :   // $array[$index] = { type: $field_type_string, value: $value }
     188             :   static void AddElement(Isolate* isolate, Handle<JSArray> array, int index,
     189             :                          Handle<String> field_type_string,
     190             :                          Handle<String> value);
     191             : 
     192             :   // A helper function to implement formatToParts which add element to array as
     193             :   // $array[$index] = {
     194             :   //   type: $field_type_string, value: $value,
     195             :   //   $additional_property_name: $additional_property_value
     196             :   // }
     197             :   static void AddElement(Isolate* isolate, Handle<JSArray> array, int index,
     198             :                          Handle<String> field_type_string, Handle<String> value,
     199             :                          Handle<String> additional_property_name,
     200             :                          Handle<String> additional_property_value);
     201             : 
     202             :   // In ECMA 402 v1, Intl constructors supported a mode of operation
     203             :   // where calling them with an existing object as a receiver would
     204             :   // transform the receiver into the relevant Intl instance with all
     205             :   // internal slots. In ECMA 402 v2, this capability was removed, to
     206             :   // avoid adding internal slots on existing objects. In ECMA 402 v3,
     207             :   // the capability was re-added as "normative optional" in a mode
     208             :   // which chains the underlying Intl instance on any object, when the
     209             :   // constructor is called
     210             :   //
     211             :   // See ecma402/#legacy-constructor.
     212             :   V8_WARN_UNUSED_RESULT static MaybeHandle<Object> LegacyUnwrapReceiver(
     213             :       Isolate* isolate, Handle<JSReceiver> receiver,
     214             :       Handle<JSFunction> constructor, bool has_initialized_slot);
     215             : 
     216             :   // enum for "caseFirst" option: shared by Intl.Locale and Intl.Collator.
     217             :   enum class CaseFirst { kUpper, kLower, kFalse, kUndefined };
     218             : 
     219             :   // Shared function to read the "caseFirst" option.
     220             :   V8_WARN_UNUSED_RESULT static Maybe<CaseFirst> GetCaseFirst(
     221             :       Isolate* isolate, Handle<JSReceiver> options, const char* method);
     222             : 
     223             :   // enum for "hourCycle" option: shared by Intl.Locale and Intl.DateTimeFormat.
     224             :   enum class HourCycle { kH11, kH12, kH23, kH24, kUndefined };
     225             : 
     226             :   static HourCycle ToHourCycle(const std::string& str);
     227             : 
     228             :   // Shared function to read the "hourCycle" option.
     229             :   V8_WARN_UNUSED_RESULT static Maybe<HourCycle> GetHourCycle(
     230             :       Isolate* isolate, Handle<JSReceiver> options, const char* method);
     231             : 
     232             :   // enum for "localeMatcher" option: shared by many Intl objects.
     233             :   enum class MatcherOption { kBestFit, kLookup };
     234             : 
     235             :   // Shared function to read the "localeMatcher" option.
     236             :   V8_WARN_UNUSED_RESULT static Maybe<MatcherOption> GetLocaleMatcher(
     237             :       Isolate* isolate, Handle<JSReceiver> options, const char* method);
     238             : 
     239       32355 :   struct ResolvedLocale {
     240             :     std::string locale;
     241             :     icu::Locale icu_locale;
     242             :     std::map<std::string, std::string> extensions;
     243             :   };
     244             : 
     245             :   static ResolvedLocale ResolveLocale(
     246             :       Isolate* isolate, const std::set<std::string>& available_locales,
     247             :       const std::vector<std::string>& requested_locales, MatcherOption options,
     248             :       const std::set<std::string>& relevant_extension_keys);
     249             : 
     250             :   // Utility function to set text to BreakIterator.
     251             :   static Managed<icu::UnicodeString> SetTextToBreakIterator(
     252             :       Isolate* isolate, Handle<String> text,
     253             :       icu::BreakIterator* break_iterator);
     254             : 
     255             :   // ecma262 #sec-string.prototype.normalize
     256             :   V8_WARN_UNUSED_RESULT static MaybeHandle<String> Normalize(
     257             :       Isolate* isolate, Handle<String> string, Handle<Object> form_input);
     258             :   static base::TimezoneCache* CreateTimeZoneCache();
     259             : 
     260             :   // Convert a Handle<String> to icu::UnicodeString
     261             :   static icu::UnicodeString ToICUUnicodeString(Isolate* isolate,
     262             :                                                Handle<String> string);
     263             : 
     264             :   static const uint8_t* ToLatin1LowerTable();
     265             : 
     266             :   static String ConvertOneByteToLower(String src, String dst);
     267             : };
     268             : 
     269             : }  // namespace internal
     270             : }  // namespace v8
     271             : 
     272             : #endif  // V8_OBJECTS_INTL_OBJECTS_H_

Generated by: LCOV version 1.10