1// Copyright 2020 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#ifndef V8_OBJECTS_JS_SEGMENTER_H_
5#define V8_OBJECTS_JS_SEGMENTER_H_
6
7#ifndef V8_INTL_SUPPORT
8#error Internationalization is expected to be enabled.
9#endif  // V8_INTL_SUPPORT
10
11#include <set>
12#include <string>
13
14#include "src/base/bit-field.h"
15#include "src/execution/isolate.h"
16#include "src/heap/factory.h"
17#include "src/objects/managed.h"
18#include "src/objects/objects.h"
19#include "unicode/uversion.h"
20
21// Has to be the last include (doesn't have include guards):
22#include "src/objects/object-macros.h"
23
24namespace U_ICU_NAMESPACE {
25class BreakIterator;
26}  // namespace U_ICU_NAMESPACE
27
28namespace v8 {
29namespace internal {
30
31#include "torque-generated/src/objects/js-segmenter-tq.inc"
32
33class JSSegmenter : public TorqueGeneratedJSSegmenter<JSSegmenter, JSObject> {
34 public:
35  // Creates segmenter object with properties derived from input locales and
36  // options.
37  V8_WARN_UNUSED_RESULT static MaybeHandle<JSSegmenter> New(
38      Isolate* isolate, Handle<Map> map, Handle<Object> locales,
39      Handle<Object> options);
40
41  V8_WARN_UNUSED_RESULT static Handle<JSObject> ResolvedOptions(
42      Isolate* isolate, Handle<JSSegmenter> segmenter_holder);
43
44  V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
45
46  Handle<String> GranularityAsString(Isolate* isolate) const;
47
48  // Segmenter accessors.
49  DECL_ACCESSORS(icu_break_iterator, Managed<icu::BreakIterator>)
50
51  // Granularity: identifying the segmenter used.
52  //
53  // ecma402 #sec-segmenter-internal-slots
54  enum class Granularity {
55    GRAPHEME,  // for character-breaks
56    WORD,      // for word-breaks
57    SENTENCE   // for sentence-breaks
58  };
59  inline void set_granularity(Granularity granularity);
60  inline Granularity granularity() const;
61
62  Handle<String> static GetGranularityString(Isolate* isolate,
63                                             Granularity granularity);
64
65  // Bit positions in |flags|.
66  DEFINE_TORQUE_GENERATED_JS_SEGMENTER_FLAGS()
67
68  STATIC_ASSERT(Granularity::GRAPHEME <= GranularityBits::kMax);
69  STATIC_ASSERT(Granularity::WORD <= GranularityBits::kMax);
70  STATIC_ASSERT(Granularity::SENTENCE <= GranularityBits::kMax);
71
72  DECL_PRINTER(JSSegmenter)
73
74  TQ_OBJECT_CONSTRUCTORS(JSSegmenter)
75};
76
77}  // namespace internal
78}  // namespace v8
79
80#include "src/objects/object-macros-undef.h"
81
82#endif  // V8_OBJECTS_JS_SEGMENTER_H_
83