js-list-format.h 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_INTL_SUPPORT
#error Internationalization is expected to be enabled.
#endif  // V8_INTL_SUPPORT

#ifndef V8_OBJECTS_JS_LIST_FORMAT_H_
#define V8_OBJECTS_JS_LIST_FORMAT_H_

12 13 14
#include <set>
#include <string>

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 21 22 23 24 25 26
#include "unicode/uversion.h"

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace U_ICU_NAMESPACE {
class ListFormatter;
27
}  // namespace U_ICU_NAMESPACE
28 29 30 31

namespace v8 {
namespace internal {

32 33
#include "torque-generated/src/objects/js-list-format-tq.inc"

34 35
class JSListFormat
    : public TorqueGeneratedJSListFormat<JSListFormat, JSObject> {
36
 public:
37
  // Creates relative time format object with properties derived from input
38
  // locales and options.
39 40 41
  static MaybeHandle<JSListFormat> New(Isolate* isolate, Handle<Map> map,
                                       Handle<Object> locales,
                                       Handle<Object> options);
42 43 44 45

  static Handle<JSObject> ResolvedOptions(Isolate* isolate,
                                          Handle<JSListFormat> format_holder);

46 47 48
  // ecma402 #sec-formatlist
  V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatList(
      Isolate* isolate, Handle<JSListFormat> format_holder,
49
      Handle<FixedArray> list);
50 51 52 53

  // ecma42 #sec-formatlisttoparts
  V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatListToParts(
      Isolate* isolate, Handle<JSListFormat> format_holder,
54
      Handle<FixedArray> list);
55

56
  V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
57

58 59 60 61
  Handle<String> StyleAsString() const;
  Handle<String> TypeAsString() const;

  // ListFormat accessors.
62
  DECL_ACCESSORS(icu_formatter, Managed<icu::ListFormatter>)
63 64 65 66 67

  // Style: identifying the relative time format style used.
  //
  // ecma402/#sec-properties-of-intl-listformat-instances
  enum class Style {
68 69 70
    LONG,   // Everything spelled out.
    SHORT,  // Abbreviations used when possible.
    NARROW  // Use the shortest possible form.
71 72 73 74 75 76 77 78 79 80
  };
  inline void set_style(Style style);
  inline Style style() const;

  // Type: identifying the list of types used.
  //
  // ecma402/#sec-properties-of-intl-listformat-instances
  enum class Type {
    CONJUNCTION,  // for "and"-based lists (e.g., "A, B and C")
    DISJUNCTION,  // for "or"-based lists (e.g., "A, B or C"),
81
    UNIT  // for lists of values with units (e.g., "5 pounds, 12 ounces").
82 83 84 85
  };
  inline void set_type(Type type);
  inline Type type() const;

86 87
  // Bit positions in |flags|.
  DEFINE_TORQUE_GENERATED_JS_LIST_FORMAT_FLAGS()
88 89 90 91 92 93 94 95

  STATIC_ASSERT(Style::LONG <= StyleBits::kMax);
  STATIC_ASSERT(Style::SHORT <= StyleBits::kMax);
  STATIC_ASSERT(Style::NARROW <= StyleBits::kMax);
  STATIC_ASSERT(Type::CONJUNCTION <= TypeBits::kMax);
  STATIC_ASSERT(Type::DISJUNCTION <= TypeBits::kMax);
  STATIC_ASSERT(Type::UNIT <= TypeBits::kMax);

96 97
  DECL_PRINTER(JSListFormat)

98
  TQ_OBJECT_CONSTRUCTORS(JSListFormat)
99 100 101 102 103 104 105 106
};

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_JS_LIST_FORMAT_H_