Home | History | Annotate | Download | only in objects
      1 // Copyright 2018 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_JS_RELATIVE_TIME_FORMAT_H_
     10 #define V8_OBJECTS_JS_RELATIVE_TIME_FORMAT_H_
     11 
     12 #include "src/heap/factory.h"
     13 #include "src/isolate.h"
     14 #include "src/objects.h"
     15 #include "unicode/uversion.h"
     16 
     17 // Has to be the last include (doesn't have include guards):
     18 #include "src/objects/object-macros.h"
     19 
     20 namespace U_ICU_NAMESPACE {
     21 class RelativeDateTimeFormatter;
     22 }
     23 
     24 namespace v8 {
     25 namespace internal {
     26 
     27 class JSRelativeTimeFormat : public JSObject {
     28  public:
     29   // Initializes relative time format object with properties derived from input
     30   // locales and options.
     31   static MaybeHandle<JSRelativeTimeFormat> InitializeRelativeTimeFormat(
     32       Isolate* isolate,
     33       Handle<JSRelativeTimeFormat> relative_time_format_holder,
     34       Handle<Object> locales, Handle<Object> options);
     35 
     36   static Handle<JSObject> ResolvedOptions(
     37       Isolate* isolate, Handle<JSRelativeTimeFormat> format_holder);
     38 
     39   // Unpacks formatter object from corresponding JavaScript object.
     40   static icu::RelativeDateTimeFormatter* UnpackFormatter(
     41       Handle<JSRelativeTimeFormat> relative_time_format_holder);
     42   Handle<String> StyleAsString() const;
     43   Handle<String> NumericAsString() const;
     44 
     45   DECL_CAST(JSRelativeTimeFormat)
     46 
     47   // RelativeTimeFormat accessors.
     48   DECL_ACCESSORS(locale, String)
     49 
     50   DECL_ACCESSORS(formatter, Foreign)
     51 
     52   // Style: identifying the relative time format style used.
     53   //
     54   // ecma402/#sec-properties-of-intl-relativetimeformat-instances
     55 
     56   enum class Style {
     57     LONG,    // Everything spelled out.
     58     SHORT,   // Abbreviations used when possible.
     59     NARROW,  // Use the shortest possible form.
     60     COUNT
     61   };
     62   inline void set_style(Style style);
     63   inline Style style() const;
     64 
     65   // Numeric: identifying whether numerical descriptions are always used, or
     66   // used only when no more specific version is available (e.g., "1 day ago" vs
     67   // "yesterday").
     68   //
     69   // ecma402/#sec-properties-of-intl-relativetimeformat-instances
     70   enum class Numeric {
     71     ALWAYS,  // numerical descriptions are always used ("1 day ago")
     72     AUTO,    // numerical descriptions are used only when no more specific
     73              // version is available ("yesterday")
     74     COUNT
     75   };
     76   inline void set_numeric(Numeric numeric);
     77   inline Numeric numeric() const;
     78 
     79 // Bit positions in |flags|.
     80 #define FLAGS_BIT_FIELDS(V, _) \
     81   V(StyleBits, Style, 2, _)    \
     82   V(NumericBits, Numeric, 1, _)
     83   DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
     84 #undef FLAGS_BIT_FIELDS
     85 
     86   STATIC_ASSERT(Style::LONG <= StyleBits::kMax);
     87   STATIC_ASSERT(Style::SHORT <= StyleBits::kMax);
     88   STATIC_ASSERT(Style::NARROW <= StyleBits::kMax);
     89   STATIC_ASSERT(Numeric::AUTO <= NumericBits::kMax);
     90   STATIC_ASSERT(Numeric::ALWAYS <= NumericBits::kMax);
     91 
     92   // [flags] Bit field containing various flags about the function.
     93   DECL_INT_ACCESSORS(flags)
     94 
     95   DECL_PRINTER(JSRelativeTimeFormat)
     96   DECL_VERIFIER(JSRelativeTimeFormat)
     97 
     98   // Layout description.
     99   static const int kJSRelativeTimeFormatOffset = JSObject::kHeaderSize;
    100   static const int kLocaleOffset = kJSRelativeTimeFormatOffset + kPointerSize;
    101   static const int kFormatterOffset = kLocaleOffset + kPointerSize;
    102   static const int kFlagsOffset = kFormatterOffset + kPointerSize;
    103   static const int kSize = kFlagsOffset + kPointerSize;
    104 
    105  private:
    106   static Style getStyle(const char* str);
    107   static Numeric getNumeric(const char* str);
    108 
    109   DISALLOW_IMPLICIT_CONSTRUCTORS(JSRelativeTimeFormat);
    110 };
    111 
    112 }  // namespace internal
    113 }  // namespace v8
    114 
    115 #include "src/objects/object-macros-undef.h"
    116 
    117 #endif  // V8_OBJECTS_JS_RELATIVE_TIME_FORMAT_H_
    118