V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
js-segmenter.h
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_SEGMENTER_H_
10 #define V8_OBJECTS_JS_SEGMENTER_H_
11 
12 #include <set>
13 #include <string>
14 
15 #include "src/heap/factory.h"
16 #include "src/isolate.h"
17 #include "src/objects.h"
18 #include "src/objects/managed.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 
24 namespace U_ICU_NAMESPACE {
25 class BreakIterator;
26 }
27 
28 namespace v8 {
29 namespace internal {
30 
31 class JSSegmenter : public JSObject {
32  public:
33  // Initializes segmenter object with properties derived from input
34  // locales and options.
35  V8_WARN_UNUSED_RESULT static MaybeHandle<JSSegmenter> Initialize(
36  Isolate* isolate, Handle<JSSegmenter> segmenter_holder,
37  Handle<Object> locales, Handle<Object> options);
38 
39  V8_WARN_UNUSED_RESULT static Handle<JSObject> ResolvedOptions(
40  Isolate* isolate, Handle<JSSegmenter> segmenter_holder);
41 
42  static std::set<std::string> GetAvailableLocales();
43 
44  Handle<String> LineBreakStyleAsString() const;
45  const char* LineBreakStyleAsCString() const;
46  Handle<String> GranularityAsString() const;
47 
48  DECL_CAST(JSSegmenter)
49 
50  // Segmenter accessors.
51  DECL_ACCESSORS2(locale, String)
52 
53  DECL_ACCESSORS(icu_break_iterator, Managed<icu::BreakIterator>)
54 
55  // LineBreakStyle: identifying the style used for line break.
56  //
57  // ecma402 #sec-segmenter-internal-slots
58 
59  enum class LineBreakStyle {
60  NOTSET, // While the granularity is not LINE
61  STRICT, // CSS level 3 line-break=strict, e.g. treat CJ as NS
62  NORMAL, // CSS level 3 line-break=normal, e.g. treat CJ as ID, break before
63  // hyphens for ja,zh
64  LOOSE, // CSS level 3 line-break=loose
65  COUNT
66  };
67  inline void set_line_break_style(LineBreakStyle line_break_style);
68  inline LineBreakStyle line_break_style() const;
69 
70  // Granularity: identifying the segmenter used.
71  //
72  // ecma402 #sec-segmenter-internal-slots
73  enum class Granularity {
74  GRAPHEME, // for character-breaks
75  WORD, // for word-breaks
76  SENTENCE, // for sentence-breaks
77  LINE, // for line-breaks
78  COUNT
79  };
80  inline void set_granularity(Granularity granularity);
81  inline Granularity granularity() const;
82 
83 // Bit positions in |flags|.
84 #define FLAGS_BIT_FIELDS(V, _) \
85  V(LineBreakStyleBits, LineBreakStyle, 3, _) \
86  V(GranularityBits, Granularity, 3, _)
87  DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
88 #undef FLAGS_BIT_FIELDS
89 
90  STATIC_ASSERT(LineBreakStyle::NOTSET <= LineBreakStyleBits::kMax);
91  STATIC_ASSERT(LineBreakStyle::STRICT <= LineBreakStyleBits::kMax);
92  STATIC_ASSERT(LineBreakStyle::NORMAL <= LineBreakStyleBits::kMax);
93  STATIC_ASSERT(LineBreakStyle::LOOSE <= LineBreakStyleBits::kMax);
94  STATIC_ASSERT(Granularity::GRAPHEME <= GranularityBits::kMax);
95  STATIC_ASSERT(Granularity::WORD <= GranularityBits::kMax);
96  STATIC_ASSERT(Granularity::SENTENCE <= GranularityBits::kMax);
97  STATIC_ASSERT(Granularity::LINE <= GranularityBits::kMax);
98 
99  // [flags] Bit field containing various flags about the function.
100  DECL_INT_ACCESSORS(flags)
101 
102  DECL_PRINTER(JSSegmenter)
103  DECL_VERIFIER(JSSegmenter)
104 
105  // Layout description.
106 #define JS_SEGMENTER_FIELDS(V) \
107  V(kJSSegmenterOffset, kTaggedSize) \
108  V(kLocaleOffset, kTaggedSize) \
109  V(kICUBreakIteratorOffset, kTaggedSize) \
110  V(kFlagsOffset, kTaggedSize) \
111  /* Header size. */ \
112  V(kSize, 0)
113 
114  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize, JS_SEGMENTER_FIELDS)
115 #undef JS_SEGMENTER_FIELDS
116 
117  private:
118  static LineBreakStyle GetLineBreakStyle(const char* str);
119  static Granularity GetGranularity(const char* str);
120 
121  DISALLOW_IMPLICIT_CONSTRUCTORS(JSSegmenter);
122 };
123 
124 } // namespace internal
125 } // namespace v8
126 
127 #include "src/objects/object-macros-undef.h"
128 
129 #endif // V8_OBJECTS_JS_SEGMENTER_H_
Definition: libplatform.h:13