• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <set>
13 #include <string>
14 
15 #include "src/base/bit-field.h"
16 #include "src/execution/isolate.h"
17 #include "src/heap/factory.h"
18 #include "src/objects/managed.h"
19 #include "src/objects/objects.h"
20 #include "unicode/uversion.h"
21 
22 // Has to be the last include (doesn't have include guards):
23 #include "src/objects/object-macros.h"
24 
25 namespace U_ICU_NAMESPACE {
26 class RelativeDateTimeFormatter;
27 }  // namespace U_ICU_NAMESPACE
28 
29 namespace v8 {
30 namespace internal {
31 
32 #include "torque-generated/src/objects/js-relative-time-format-tq.inc"
33 
34 class JSRelativeTimeFormat
35     : public TorqueGeneratedJSRelativeTimeFormat<JSRelativeTimeFormat,
36                                                  JSObject> {
37  public:
38   // Creates relative time format object with properties derived from input
39   // locales and options.
40   V8_WARN_UNUSED_RESULT static MaybeHandle<JSRelativeTimeFormat> New(
41       Isolate* isolate, Handle<Map> map, Handle<Object> locales,
42       Handle<Object> options);
43 
44   V8_WARN_UNUSED_RESULT static Handle<JSObject> ResolvedOptions(
45       Isolate* isolate, Handle<JSRelativeTimeFormat> format_holder);
46 
47   Handle<String> NumericAsString() const;
48 
49   // ecma402/#sec-Intl.RelativeTimeFormat.prototype.format
50   V8_WARN_UNUSED_RESULT static MaybeHandle<String> Format(
51       Isolate* isolate, Handle<Object> value_obj, Handle<Object> unit_obj,
52       Handle<JSRelativeTimeFormat> format);
53 
54   // ecma402/#sec-Intl.RelativeTimeFormat.prototype.formatToParts
55   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatToParts(
56       Isolate* isolate, Handle<Object> value_obj, Handle<Object> unit_obj,
57       Handle<JSRelativeTimeFormat> format);
58 
59   V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
60 
61   // RelativeTimeFormat accessors.
62   DECL_ACCESSORS(icu_formatter, Managed<icu::RelativeDateTimeFormatter>)
63 
64   // Numeric: identifying whether numerical descriptions are always used, or
65   // used only when no more specific version is available (e.g., "1 day ago" vs
66   // "yesterday").
67   //
68   // ecma402/#sec-properties-of-intl-relativetimeformat-instances
69   enum class Numeric {
70     ALWAYS,  // numerical descriptions are always used ("1 day ago")
71     AUTO     // numerical descriptions are used only when no more specific
72              // version is available ("yesterday")
73   };
74   inline void set_numeric(Numeric numeric);
75   inline Numeric numeric() const;
76 
77   // Bit positions in |flags|.
78   DEFINE_TORQUE_GENERATED_JS_RELATIVE_TIME_FORMAT_FLAGS()
79 
80   STATIC_ASSERT(Numeric::AUTO <= NumericBit::kMax);
81   STATIC_ASSERT(Numeric::ALWAYS <= NumericBit::kMax);
82 
83   DECL_PRINTER(JSRelativeTimeFormat)
84 
85   TQ_OBJECT_CONSTRUCTORS(JSRelativeTimeFormat)
86 };
87 
88 }  // namespace internal
89 }  // namespace v8
90 
91 #include "src/objects/object-macros-undef.h"
92 
93 #endif  // V8_OBJECTS_JS_RELATIVE_TIME_FORMAT_H_
94