Commit 2552c747 authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

[Intl] Merge the reading of "localeMatcher"

Bug: v8:5751
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I5a482014fa482c62b014506df45846496d909a63
Reviewed-on: https://chromium-review.googlesource.com/c/1295933Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56917}
parent 9867aa3f
...@@ -1637,18 +1637,10 @@ MaybeHandle<JSObject> SupportedLocales( ...@@ -1637,18 +1637,10 @@ MaybeHandle<JSObject> SupportedLocales(
// 1. b. Let matcher be ? GetOption(options, "localeMatcher", "string", // 1. b. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit"). // « "lookup", "best fit" », "best fit").
std::unique_ptr<char[]> matcher_str = nullptr; Maybe<Intl::MatcherOption> maybe_locale_matcher = Intl::GetLocaleMatcher(
std::vector<const char*> matcher_values = {"lookup", "best fit"}; isolate, options_obj, ICUServiceToString(service));
Maybe<bool> maybe_found_matcher = Intl::GetStringOption( MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSObject>());
isolate, options_obj, "localeMatcher", matcher_values, matcher = maybe_locale_matcher.FromJust();
ICUServiceToString(service), &matcher_str);
MAYBE_RETURN(maybe_found_matcher, MaybeHandle<JSObject>());
if (maybe_found_matcher.FromJust()) {
DCHECK_NOT_NULL(matcher_str.get());
if (strcmp(matcher_str.get(), "lookup") == 0) {
matcher = Intl::MatcherOption::kLookup;
}
}
} }
// 3. If matcher is "best fit", then // 3. If matcher is "best fit", then
...@@ -2024,5 +2016,22 @@ base::TimezoneCache* Intl::CreateTimeZoneCache() { ...@@ -2024,5 +2016,22 @@ base::TimezoneCache* Intl::CreateTimeZoneCache() {
: base::OS::CreateTimezoneCache(); : base::OS::CreateTimezoneCache();
} }
Maybe<Intl::MatcherOption> Intl::GetLocaleMatcher(Isolate* isolate,
Handle<JSReceiver> options,
const char* method) {
const std::vector<const char*> values = {"lookup", "best fit"};
std::unique_ptr<char[]> matcher_str = nullptr;
Maybe<bool> found_matcher = Intl::GetStringOption(
isolate, options, "localeMatcher", values, method, &matcher_str);
MAYBE_RETURN(found_matcher, Nothing<Intl::MatcherOption>());
if (found_matcher.FromJust()) {
DCHECK_NOT_NULL(matcher_str.get());
if (strcmp(matcher_str.get(), "lookup") == 0) {
return Just(Intl::MatcherOption::kLookup);
}
}
return Just(Intl::MatcherOption::kBestFit);
}
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -211,6 +211,10 @@ class Intl { ...@@ -211,6 +211,10 @@ class Intl {
const std::vector<std::string>& requested_locales, MatcherOption options, const std::vector<std::string>& requested_locales, MatcherOption options,
const std::set<std::string>& relevant_extension_keys); const std::set<std::string>& relevant_extension_keys);
// Shared function to read the "localeMatcher" option.
V8_WARN_UNUSED_RESULT static Maybe<MatcherOption> GetLocaleMatcher(
Isolate* isolate, Handle<JSReceiver> options, const char* method);
// Utility function to set text to BreakIterator. // Utility function to set text to BreakIterator.
static Managed<icu::UnicodeString>* SetTextToBreakIterator( static Managed<icu::UnicodeString>* SetTextToBreakIterator(
Isolate* isolate, Handle<String> text, Isolate* isolate, Handle<String> text,
......
...@@ -46,19 +46,11 @@ MaybeHandle<JSV8BreakIterator> JSV8BreakIterator::Initialize( ...@@ -46,19 +46,11 @@ MaybeHandle<JSV8BreakIterator> JSV8BreakIterator::Initialize(
} }
// Extract locale string // Extract locale string
const std::vector<const char*> values = {"lookup", "best fit"}; Maybe<Intl::MatcherOption> maybe_locale_matcher =
std::unique_ptr<char[]> matcher_str = nullptr; Intl::GetLocaleMatcher(isolate, options, "Intl.JSV8BreakIterator");
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit; MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSV8BreakIterator>());
Maybe<bool> found_matcher = Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
Intl::GetStringOption(isolate, options, "localeMatcher", values,
"Intl.JSV8BreakIterator", &matcher_str);
MAYBE_RETURN(found_matcher, MaybeHandle<JSV8BreakIterator>());
if (found_matcher.FromJust()) {
DCHECK_NOT_NULL(matcher_str.get());
if (strcmp(matcher_str.get(), "lookup") == 0) {
matcher = Intl::MatcherOption::kLookup;
}
}
std::set<std::string> available_locales = std::set<std::string> available_locales =
Intl::GetAvailableLocales(Intl::ICUService::kBreakIterator); Intl::GetAvailableLocales(Intl::ICUService::kBreakIterator);
Intl::ResolvedLocale r = Intl::ResolveLocale(isolate, available_locales, Intl::ResolvedLocale r = Intl::ResolveLocale(isolate, available_locales,
......
...@@ -262,18 +262,10 @@ MaybeHandle<JSCollator> JSCollator::Initialize(Isolate* isolate, ...@@ -262,18 +262,10 @@ MaybeHandle<JSCollator> JSCollator::Initialize(Isolate* isolate,
// 9. Let matcher be ? GetOption(options, "localeMatcher", "string", // 9. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit"). // « "lookup", "best fit" », "best fit").
// 10. Set opt.[[localeMatcher]] to matcher. // 10. Set opt.[[localeMatcher]] to matcher.
values = {"lookup", "best fit"}; Maybe<Intl::MatcherOption> maybe_locale_matcher =
std::unique_ptr<char[]> matcher_str = nullptr; Intl::GetLocaleMatcher(isolate, options, "Intl.Collator");
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit; MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSCollator>());
Maybe<bool> found_matcher = Intl::GetStringOption( Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
isolate, options, "localeMatcher", values, "Intl.Collator", &matcher_str);
MAYBE_RETURN(found_matcher, MaybeHandle<JSCollator>());
if (found_matcher.FromJust()) {
DCHECK_NOT_NULL(matcher_str.get());
if (strcmp(matcher_str.get(), "lookup") == 0) {
matcher = Intl::MatcherOption::kLookup;
}
}
// 11. Let numeric be ? GetOption(options, "numeric", "boolean", // 11. Let numeric be ? GetOption(options, "numeric", "boolean",
// undefined, undefined). // undefined, undefined).
......
...@@ -739,19 +739,10 @@ MaybeHandle<JSDateTimeFormat> JSDateTimeFormat::Initialize( ...@@ -739,19 +739,10 @@ MaybeHandle<JSDateTimeFormat> JSDateTimeFormat::Initialize(
// 4. Let matcher be ? GetOption(options, "localeMatcher", "string", // 4. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit"). // « "lookup", "best fit" », "best fit").
// 5. Set opt.[[localeMatcher]] to matcher. // 5. Set opt.[[localeMatcher]] to matcher.
std::vector<const char*> values = {"lookup", "best fit"}; Maybe<Intl::MatcherOption> maybe_locale_matcher =
std::unique_ptr<char[]> locale_matcher_str = nullptr; Intl::GetLocaleMatcher(isolate, options, "Intl.DateTimeFormat");
Intl::MatcherOption locale_matcher = Intl::MatcherOption::kBestFit; MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSDateTimeFormat>());
Maybe<bool> found_locale_matcher = Intl::MatcherOption locale_matcher = maybe_locale_matcher.FromJust();
Intl::GetStringOption(isolate, options, "localeMatcher", values,
"Intl.DateTimeFormat", &locale_matcher_str);
MAYBE_RETURN(found_locale_matcher, MaybeHandle<JSDateTimeFormat>());
if (found_locale_matcher.FromJust()) {
DCHECK_NOT_NULL(locale_matcher_str.get());
if (strcmp(locale_matcher_str.get(), "lookup") == 0) {
locale_matcher = Intl::MatcherOption::kLookup;
}
}
// 6. Let hour12 be ? GetOption(options, "hour12", "boolean", undefined, // 6. Let hour12 be ? GetOption(options, "hour12", "boolean", undefined,
// undefined). // undefined).
......
...@@ -178,19 +178,10 @@ MaybeHandle<JSListFormat> JSListFormat::Initialize( ...@@ -178,19 +178,10 @@ MaybeHandle<JSListFormat> JSListFormat::Initialize(
// 12. Let matcher be ? GetOption(options, "localeMatcher", "string", « // 12. Let matcher be ? GetOption(options, "localeMatcher", "string", «
// "lookup", "best fit" », "best fit"). // "lookup", "best fit" », "best fit").
const std::vector<const char*> values = {"lookup", "best fit"}; Maybe<Intl::MatcherOption> maybe_locale_matcher =
std::unique_ptr<char[]> matcher_str = nullptr; Intl::GetLocaleMatcher(isolate, options, "Intl.ListFormat");
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit; MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSListFormat>());
Maybe<bool> found_matcher = Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
Intl::GetStringOption(isolate, options, "localeMatcher", values,
"Intl.ListFormat", &matcher_str);
MAYBE_RETURN(found_matcher, MaybeHandle<JSListFormat>());
if (found_matcher.FromJust()) {
DCHECK_NOT_NULL(matcher_str.get());
if (strcmp(matcher_str.get(), "lookup") == 0) {
matcher = Intl::MatcherOption::kLookup;
}
}
// 14. If style is "narrow" and type is not "unit", throw a RangeError // 14. If style is "narrow" and type is not "unit", throw a RangeError
// exception. // exception.
......
...@@ -216,19 +216,10 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::Initialize( ...@@ -216,19 +216,10 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::Initialize(
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string", « // 5. Let matcher be ? GetOption(options, "localeMatcher", "string", «
// "lookup", "best fit" », "best fit"). // "lookup", "best fit" », "best fit").
// 6. Set opt.[[localeMatcher]] to matcher. // 6. Set opt.[[localeMatcher]] to matcher.
const std::vector<const char*> values = {"lookup", "best fit"}; Maybe<Intl::MatcherOption> maybe_locale_matcher =
std::unique_ptr<char[]> matcher_str = nullptr; Intl::GetLocaleMatcher(isolate, options, "Intl.NumberFormat");
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit; MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSNumberFormat>());
Maybe<bool> found_matcher = Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
Intl::GetStringOption(isolate, options, "localeMatcher", values,
"Intl.NumberFormat", &matcher_str);
MAYBE_RETURN(found_matcher, MaybeHandle<JSNumberFormat>());
if (found_matcher.FromJust()) {
DCHECK_NOT_NULL(matcher_str.get());
if (strcmp(matcher_str.get(), "lookup") == 0) {
matcher = Intl::MatcherOption::kLookup;
}
}
// 7. Let localeData be %NumberFormat%.[[LocaleData]]. // 7. Let localeData be %NumberFormat%.[[LocaleData]].
// 8. Let r be ResolveLocale(%NumberFormat%.[[AvailableLocales]], // 8. Let r be ResolveLocale(%NumberFormat%.[[AvailableLocales]],
......
...@@ -111,23 +111,14 @@ MaybeHandle<JSPluralRules> JSPluralRules::Initialize( ...@@ -111,23 +111,14 @@ MaybeHandle<JSPluralRules> JSPluralRules::Initialize(
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string", // 5. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit"). // « "lookup", "best fit" », "best fit").
// 6. Set opt.[[localeMatcher]] to matcher. // 6. Set opt.[[localeMatcher]] to matcher.
std::vector<const char*> values = {"lookup", "best fit"}; Maybe<Intl::MatcherOption> maybe_locale_matcher =
std::unique_ptr<char[]> matcher_str = nullptr; Intl::GetLocaleMatcher(isolate, options, "Intl.PluralRules");
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit; MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSPluralRules>());
Maybe<bool> found_matcher = Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
Intl::GetStringOption(isolate, options, "localeMatcher", values,
"Intl.PluralRules", &matcher_str);
MAYBE_RETURN(found_matcher, MaybeHandle<JSPluralRules>());
if (found_matcher.FromJust()) {
DCHECK_NOT_NULL(matcher_str.get());
if (strcmp(matcher_str.get(), "lookup") == 0) {
matcher = Intl::MatcherOption::kLookup;
}
}
// 7. Let t be ? GetOption(options, "type", "string", « "cardinal", // 7. Let t be ? GetOption(options, "type", "string", « "cardinal",
// "ordinal" », "cardinal"). // "ordinal" », "cardinal").
values = {"cardinal", "ordinal"}; std::vector<const char*> values = {"cardinal", "ordinal"};
std::unique_ptr<char[]> type_str = nullptr; std::unique_ptr<char[]> type_str = nullptr;
const char* type_cstr = "cardinal"; const char* type_cstr = "cardinal";
Maybe<bool> found = Intl::GetStringOption(isolate, options, "type", values, Maybe<bool> found = Intl::GetStringOption(isolate, options, "type", values,
......
...@@ -83,19 +83,10 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::Initialize( ...@@ -83,19 +83,10 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::Initialize(
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string", « // 5. Let matcher be ? GetOption(options, "localeMatcher", "string", «
// "lookup", "best fit" », "best fit"). // "lookup", "best fit" », "best fit").
// 6. Set opt.[[localeMatcher]] to matcher. // 6. Set opt.[[localeMatcher]] to matcher.
const std::vector<const char*> values = {"lookup", "best fit"}; Maybe<Intl::MatcherOption> maybe_locale_matcher =
std::unique_ptr<char[]> matcher_str = nullptr; Intl::GetLocaleMatcher(isolate, options, "Intl.RelativeTimeFormat");
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit; MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSRelativeTimeFormat>());
Maybe<bool> found_matcher = Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
Intl::GetStringOption(isolate, options, "localeMatcher", values,
"Intl.RelativeTimeFormat", &matcher_str);
MAYBE_RETURN(found_matcher, MaybeHandle<JSRelativeTimeFormat>());
if (found_matcher.FromJust()) {
DCHECK_NOT_NULL(matcher_str.get());
if (strcmp(matcher_str.get(), "lookup") == 0) {
matcher = Intl::MatcherOption::kLookup;
}
}
// 7. Let localeData be %RelativeTimeFormat%.[[LocaleData]]. // 7. Let localeData be %RelativeTimeFormat%.[[LocaleData]].
// 8. Let r be // 8. Let r be
......
...@@ -67,19 +67,10 @@ MaybeHandle<JSSegmenter> JSSegmenter::Initialize( ...@@ -67,19 +67,10 @@ MaybeHandle<JSSegmenter> JSSegmenter::Initialize(
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string", // 5. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit"). // « "lookup", "best fit" », "best fit").
// 6. Set opt.[[localeMatcher]] to matcher. // 6. Set opt.[[localeMatcher]] to matcher.
const std::vector<const char*> values = {"lookup", "best fit"}; Maybe<Intl::MatcherOption> maybe_locale_matcher =
std::unique_ptr<char[]> matcher_str = nullptr; Intl::GetLocaleMatcher(isolate, options, "Intl.Segmenter");
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit; MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSSegmenter>());
Maybe<bool> found_matcher = Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
Intl::GetStringOption(isolate, options, "localeMatcher", values,
"Intl.Segmenter", &matcher_str);
MAYBE_RETURN(found_matcher, MaybeHandle<JSSegmenter>());
if (found_matcher.FromJust()) {
DCHECK_NOT_NULL(matcher_str.get());
if (strcmp(matcher_str.get(), "lookup") == 0) {
matcher = Intl::MatcherOption::kLookup;
}
}
// 8. Set opt.[[lb]] to lineBreakStyle. // 8. Set opt.[[lb]] to lineBreakStyle.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment