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(
// 1. b. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit").
std::unique_ptr<char[]> matcher_str = nullptr;
std::vector<const char*> matcher_values = {"lookup", "best fit"};
Maybe<bool> maybe_found_matcher = Intl::GetStringOption(
isolate, options_obj, "localeMatcher", matcher_values,
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;
}
}
Maybe<Intl::MatcherOption> maybe_locale_matcher = Intl::GetLocaleMatcher(
isolate, options_obj, ICUServiceToString(service));
MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSObject>());
matcher = maybe_locale_matcher.FromJust();
}
// 3. If matcher is "best fit", then
......@@ -2024,5 +2016,22 @@ base::TimezoneCache* Intl::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 v8
......@@ -211,6 +211,10 @@ class Intl {
const std::vector<std::string>& requested_locales, MatcherOption options,
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.
static Managed<icu::UnicodeString>* SetTextToBreakIterator(
Isolate* isolate, Handle<String> text,
......
......@@ -46,19 +46,11 @@ MaybeHandle<JSV8BreakIterator> JSV8BreakIterator::Initialize(
}
// Extract locale string
const std::vector<const char*> values = {"lookup", "best fit"};
std::unique_ptr<char[]> matcher_str = nullptr;
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit;
Maybe<bool> found_matcher =
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;
}
}
Maybe<Intl::MatcherOption> maybe_locale_matcher =
Intl::GetLocaleMatcher(isolate, options, "Intl.JSV8BreakIterator");
MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSV8BreakIterator>());
Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
std::set<std::string> available_locales =
Intl::GetAvailableLocales(Intl::ICUService::kBreakIterator);
Intl::ResolvedLocale r = Intl::ResolveLocale(isolate, available_locales,
......
......@@ -262,18 +262,10 @@ MaybeHandle<JSCollator> JSCollator::Initialize(Isolate* isolate,
// 9. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit").
// 10. Set opt.[[localeMatcher]] to matcher.
values = {"lookup", "best fit"};
std::unique_ptr<char[]> matcher_str = nullptr;
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit;
Maybe<bool> found_matcher = Intl::GetStringOption(
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;
}
}
Maybe<Intl::MatcherOption> maybe_locale_matcher =
Intl::GetLocaleMatcher(isolate, options, "Intl.Collator");
MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSCollator>());
Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
// 11. Let numeric be ? GetOption(options, "numeric", "boolean",
// undefined, undefined).
......
......@@ -739,19 +739,10 @@ MaybeHandle<JSDateTimeFormat> JSDateTimeFormat::Initialize(
// 4. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit").
// 5. Set opt.[[localeMatcher]] to matcher.
std::vector<const char*> values = {"lookup", "best fit"};
std::unique_ptr<char[]> locale_matcher_str = nullptr;
Intl::MatcherOption locale_matcher = Intl::MatcherOption::kBestFit;
Maybe<bool> found_locale_matcher =
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;
}
}
Maybe<Intl::MatcherOption> maybe_locale_matcher =
Intl::GetLocaleMatcher(isolate, options, "Intl.DateTimeFormat");
MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSDateTimeFormat>());
Intl::MatcherOption locale_matcher = maybe_locale_matcher.FromJust();
// 6. Let hour12 be ? GetOption(options, "hour12", "boolean", undefined,
// undefined).
......
......@@ -178,19 +178,10 @@ MaybeHandle<JSListFormat> JSListFormat::Initialize(
// 12. Let matcher be ? GetOption(options, "localeMatcher", "string", «
// "lookup", "best fit" », "best fit").
const std::vector<const char*> values = {"lookup", "best fit"};
std::unique_ptr<char[]> matcher_str = nullptr;
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit;
Maybe<bool> found_matcher =
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;
}
}
Maybe<Intl::MatcherOption> maybe_locale_matcher =
Intl::GetLocaleMatcher(isolate, options, "Intl.ListFormat");
MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSListFormat>());
Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
// 14. If style is "narrow" and type is not "unit", throw a RangeError
// exception.
......
......@@ -216,19 +216,10 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::Initialize(
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string", «
// "lookup", "best fit" », "best fit").
// 6. Set opt.[[localeMatcher]] to matcher.
const std::vector<const char*> values = {"lookup", "best fit"};
std::unique_ptr<char[]> matcher_str = nullptr;
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit;
Maybe<bool> found_matcher =
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;
}
}
Maybe<Intl::MatcherOption> maybe_locale_matcher =
Intl::GetLocaleMatcher(isolate, options, "Intl.NumberFormat");
MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSNumberFormat>());
Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
// 7. Let localeData be %NumberFormat%.[[LocaleData]].
// 8. Let r be ResolveLocale(%NumberFormat%.[[AvailableLocales]],
......
......@@ -111,23 +111,14 @@ MaybeHandle<JSPluralRules> JSPluralRules::Initialize(
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit").
// 6. Set opt.[[localeMatcher]] to matcher.
std::vector<const char*> values = {"lookup", "best fit"};
std::unique_ptr<char[]> matcher_str = nullptr;
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit;
Maybe<bool> found_matcher =
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;
}
}
Maybe<Intl::MatcherOption> maybe_locale_matcher =
Intl::GetLocaleMatcher(isolate, options, "Intl.PluralRules");
MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSPluralRules>());
Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
// 7. Let t be ? GetOption(options, "type", "string", « "cardinal",
// "ordinal" », "cardinal").
values = {"cardinal", "ordinal"};
std::vector<const char*> values = {"cardinal", "ordinal"};
std::unique_ptr<char[]> type_str = nullptr;
const char* type_cstr = "cardinal";
Maybe<bool> found = Intl::GetStringOption(isolate, options, "type", values,
......
......@@ -83,19 +83,10 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::Initialize(
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string", «
// "lookup", "best fit" », "best fit").
// 6. Set opt.[[localeMatcher]] to matcher.
const std::vector<const char*> values = {"lookup", "best fit"};
std::unique_ptr<char[]> matcher_str = nullptr;
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit;
Maybe<bool> found_matcher =
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;
}
}
Maybe<Intl::MatcherOption> maybe_locale_matcher =
Intl::GetLocaleMatcher(isolate, options, "Intl.RelativeTimeFormat");
MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSRelativeTimeFormat>());
Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
// 7. Let localeData be %RelativeTimeFormat%.[[LocaleData]].
// 8. Let r be
......
......@@ -67,19 +67,10 @@ MaybeHandle<JSSegmenter> JSSegmenter::Initialize(
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string",
// « "lookup", "best fit" », "best fit").
// 6. Set opt.[[localeMatcher]] to matcher.
const std::vector<const char*> values = {"lookup", "best fit"};
std::unique_ptr<char[]> matcher_str = nullptr;
Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit;
Maybe<bool> found_matcher =
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;
}
}
Maybe<Intl::MatcherOption> maybe_locale_matcher =
Intl::GetLocaleMatcher(isolate, options, "Intl.Segmenter");
MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSSegmenter>());
Intl::MatcherOption matcher = maybe_locale_matcher.FromJust();
// 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