v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
js-segmenter.cc
Go to the documentation of this file.
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
10
11#include <map>
12#include <memory>
13#include <string>
14
16#include "src/heap/factory.h"
22#include "unicode/brkiter.h"
23
24namespace v8 {
25namespace internal {
26
28 Isolate* isolate, DirectHandle<Map> map, DirectHandle<Object> locales,
29 DirectHandle<Object> input_options) {
30 // 4. Let requestedLocales be ? CanonicalizeLocaleList(locales).
31 Maybe<std::vector<std::string>> maybe_requested_locales =
32 Intl::CanonicalizeLocaleList(isolate, locales);
33 MAYBE_RETURN(maybe_requested_locales, DirectHandle<JSSegmenter>());
34 std::vector<std::string> requested_locales =
35 maybe_requested_locales.FromJust();
36
38 const char* service = "Intl.Segmenter";
39 // 5. Let options be GetOptionsObject(_options_).
40 ASSIGN_RETURN_ON_EXCEPTION(isolate, options,
41 GetOptionsObject(isolate, input_options, service));
42
43 // 7. Let opt be a new Record.
44 // 8. Let matcher be ? GetOption(options, "localeMatcher", "string",
45 // « "lookup", "best fit" », "best fit").
46 // 9. Set opt.[[localeMatcher]] to matcher.
47 Maybe<Intl::MatcherOption> maybe_locale_matcher =
48 Intl::GetLocaleMatcher(isolate, options, service);
49 MAYBE_RETURN(maybe_locale_matcher, MaybeDirectHandle<JSSegmenter>());
50 Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
51
52 // 10. Let localeData be %Segmenter%.[[LocaleData]].
53
54 // 11. Let r be ResolveLocale(%Segmenter%.[[AvailableLocales]],
55 // requestedLocales, opt, %Segmenter%.[[RelevantExtensionKeys]]).
56 Maybe<Intl::ResolvedLocale> maybe_resolve_locale =
58 requested_locales, matcher, {});
59 if (maybe_resolve_locale.IsNothing()) {
60 THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kIcuError));
61 }
62 Intl::ResolvedLocale r = maybe_resolve_locale.FromJust();
63
64 // 12. Set segmenter.[[Locale]] to the value of r.[[locale]].
65 DirectHandle<String> locale_str =
66 isolate->factory()->NewStringFromAsciiChecked(r.locale.c_str());
67
68 // 13. Let granularity be ? GetOption(options, "granularity", "string", «
69 // "grapheme", "word", "sentence" », "grapheme").
71 isolate, options, "granularity", service,
72 {"grapheme", "word", "sentence"},
75 MAYBE_RETURN(maybe_granularity, MaybeDirectHandle<JSSegmenter>());
76 Granularity granularity_enum = maybe_granularity.FromJust();
77
78 icu::Locale icu_locale = r.icu_locale;
79 DCHECK(!icu_locale.isBogus());
80
81 UErrorCode status = U_ZERO_ERROR;
82 std::unique_ptr<icu::BreakIterator> icu_break_iterator;
83
84 switch (granularity_enum) {
86 icu_break_iterator.reset(
87 icu::BreakIterator::createCharacterInstance(icu_locale, status));
88 break;
90 icu_break_iterator.reset(
91 icu::BreakIterator::createWordInstance(icu_locale, status));
92 break;
94 icu_break_iterator.reset(
95 icu::BreakIterator::createSentenceInstance(icu_locale, status));
96 break;
97 }
98
99 DCHECK(U_SUCCESS(status));
100 DCHECK_NOT_NULL(icu_break_iterator.get());
101
102 DirectHandle<Managed<icu::BreakIterator>> managed_break_iterator =
104 std::move(icu_break_iterator));
105
106 // Now all properties are ready, so we can allocate the result object.
107 DirectHandle<JSSegmenter> segmenter =
108 Cast<JSSegmenter>(isolate->factory()->NewFastOrSlowJSObjectFromMap(map));
110 segmenter->set_flags(0);
111
112 // 12. Set segmenter.[[Locale]] to the value of r.[[Locale]].
113 segmenter->set_locale(*locale_str);
114
115 // 14. Set segmenter.[[SegmenterGranularity]] to granularity.
116 segmenter->set_granularity(granularity_enum);
117
118 segmenter->set_icu_break_iterator(*managed_break_iterator);
119
120 // 15. Return segmenter.
121 return segmenter;
122}
123
124// ecma402 #sec-Intl.Segmenter.prototype.resolvedOptions
126 Isolate* isolate, DirectHandle<JSSegmenter> segmenter) {
127 Factory* factory = isolate->factory();
128 // 3. Let options be ! ObjectCreate(%ObjectPrototype%).
130 factory->NewJSObject(isolate->object_function());
131 // 4. For each row of Table 1, except the header row, do
132 // a. Let p be the Property value of the current row.
133 // b. Let v be the value of pr's internal slot whose name is the Internal Slot
134 // value of the current row.
135 //
136 // c. If v is not undefined, then
137 // i. Perform ! CreateDataPropertyOrThrow(options, p, v).
138 // Table 1: Resolved Options of Segmenter Instances
139 // Internal Slot Property
140 // [[Locale]] "locale"
141 // [[SegmenterGranularity]] "granularity"
142
143 DirectHandle<String> locale(segmenter->locale(), isolate);
144 JSObject::AddProperty(isolate, result, factory->locale_string(), locale,
145 NONE);
146 JSObject::AddProperty(isolate, result, factory->granularity_string(),
147 segmenter->GranularityAsString(isolate), NONE);
148 // 5. Return options.
149 return result;
150}
151
155
157 Granularity granularity) {
158 Factory* factory = isolate->factory();
159 switch (granularity) {
161 return factory->grapheme_string();
163 return factory->word_string();
165 return factory->sentence_string();
166 }
167 UNREACHABLE();
168}
169
170const std::set<std::string>& JSSegmenter::GetAvailableLocales() {
172}
173
174} // namespace internal
175} // namespace v8
V8_INLINE T FromJust() const &
Definition v8-maybe.h:64
V8_INLINE bool IsNothing() const
Definition v8-maybe.h:35
Handle< JSObject > NewJSObject(DirectHandle< JSFunction > constructor, AllocationType allocation=AllocationType::kYoung, NewJSObjectType=NewJSObjectType::kNoAPIWrapper)
Definition factory.cc:2985
static const std::set< std::string > & GetAvailableLocales()
static Maybe< std::vector< std::string > > CanonicalizeLocaleList(Isolate *isolate, DirectHandle< Object > locales, bool only_return_one_result=false)
static Maybe< ResolvedLocale > ResolveLocale(Isolate *isolate, const std::set< std::string > &available_locales, const std::vector< std::string > &requested_locales, MatcherOption options, const std::set< std::string > &relevant_extension_keys)
static V8_WARN_UNUSED_RESULT Maybe< MatcherOption > GetLocaleMatcher(Isolate *isolate, DirectHandle< JSReceiver > options, const char *method_name)
static V8_EXPORT_PRIVATE void AddProperty(Isolate *isolate, DirectHandle< JSObject > object, DirectHandle< Name > name, DirectHandle< Object > value, PropertyAttributes attributes)
static Handle< String > GetGranularityString(Isolate *isolate, Granularity granularity)
static V8_WARN_UNUSED_RESULT MaybeDirectHandle< JSSegmenter > New(Isolate *isolate, DirectHandle< Map > map, DirectHandle< Object > locales, DirectHandle< Object > options)
static V8_EXPORT_PRIVATE const std::set< std::string > & GetAvailableLocales()
Granularity granularity() const
Handle< String > GranularityAsString(Isolate *isolate) const
static V8_WARN_UNUSED_RESULT DirectHandle< JSObject > ResolvedOptions(Isolate *isolate, DirectHandle< JSSegmenter > segmenter_holder)
static DirectHandle< Managed< CppType > > From(Isolate *isolate, size_t estimated_size, std::shared_ptr< CppType > shared_ptr, AllocationType allocation_type=AllocationType::kYoung)
Definition managed-inl.h:27
#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call)
Definition isolate.h:291
#define THROW_NEW_ERROR(isolate, call)
Definition isolate.h:307
#define MAYBE_RETURN(call, value)
Definition isolate.h:408
DirectHandle< JSReceiver > options
ZoneVector< RpoNumber > & result
int r
Definition mul-fft.cc:298
MaybeDirectHandle< JSReceiver > GetOptionsObject(Isolate *isolate, DirectHandle< Object > options, const char *method_name)
Maybe< bool > GetStringOption(Isolate *isolate, DirectHandle< JSReceiver > options, const char *property, const std::vector< const char * > &values, const char *method_name, std::unique_ptr< char[]> *result)
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define DCHECK_NOT_NULL(val)
Definition logging.h:492
#define DCHECK(condition)
Definition logging.h:482