Commit eadd2a22 authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

Simplify JSRelativeTimeFormat

Remove the style from flag and use value return from ICU object instead.

Change-Id: I89732c4cd9f093136d18fcd1122e8858c6ca50dc
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2057975Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66352}
parent c0de0ef3
......@@ -2081,7 +2081,6 @@ void JSRelativeTimeFormat::JSRelativeTimeFormatPrint(
JSObjectPrintHeader(os, *this, "JSRelativeTimeFormat");
os << "\n - locale: " << Brief(locale());
os << "\n - numberingSystem: " << Brief(numberingSystem());
os << "\n - style: " << StyleAsString();
os << "\n - numeric: " << NumericAsString();
os << "\n - icu formatter: " << Brief(icu_formatter());
os << "\n";
......
......@@ -27,17 +27,6 @@ ACCESSORS(JSRelativeTimeFormat, icu_formatter,
Managed<icu::RelativeDateTimeFormatter>, kIcuFormatterOffset)
SMI_ACCESSORS(JSRelativeTimeFormat, flags, kFlagsOffset)
inline void JSRelativeTimeFormat::set_style(Style style) {
DCHECK_GE(StyleBits::kMax, style);
int hints = flags();
hints = StyleBits::update(hints, style);
set_flags(hints);
}
inline JSRelativeTimeFormat::Style JSRelativeTimeFormat::style() const {
return StyleBits::decode(flags());
}
inline void JSRelativeTimeFormat::set_numeric(Numeric numeric) {
DCHECK_GE(NumericBits::kMax, numeric);
int hints = flags();
......
......@@ -24,34 +24,43 @@
namespace v8 {
namespace internal {
// Style: identifying the relative time format style used.
//
// ecma402/#sec-properties-of-intl-relativetimeformat-instances
enum class Style {
LONG, // Everything spelled out.
SHORT, // Abbreviations used when possible.
NARROW // Use the shortest possible form.
};
namespace {
UDateRelativeDateTimeFormatterStyle getIcuStyle(
JSRelativeTimeFormat::Style style) {
UDateRelativeDateTimeFormatterStyle toIcuStyle(Style style) {
switch (style) {
case JSRelativeTimeFormat::Style::LONG:
case Style::LONG:
return UDAT_STYLE_LONG;
case JSRelativeTimeFormat::Style::SHORT:
case Style::SHORT:
return UDAT_STYLE_SHORT;
case JSRelativeTimeFormat::Style::NARROW:
case Style::NARROW:
return UDAT_STYLE_NARROW;
}
UNREACHABLE();
}
} // namespace
JSRelativeTimeFormat::Style JSRelativeTimeFormat::getStyle(const char* str) {
if (strcmp(str, "long") == 0) return JSRelativeTimeFormat::Style::LONG;
if (strcmp(str, "short") == 0) return JSRelativeTimeFormat::Style::SHORT;
if (strcmp(str, "narrow") == 0) return JSRelativeTimeFormat::Style::NARROW;
UNREACHABLE();
}
JSRelativeTimeFormat::Numeric JSRelativeTimeFormat::getNumeric(
const char* str) {
if (strcmp(str, "auto") == 0) return JSRelativeTimeFormat::Numeric::AUTO;
if (strcmp(str, "always") == 0) return JSRelativeTimeFormat::Numeric::ALWAYS;
Style fromIcuStyle(UDateRelativeDateTimeFormatterStyle icu_style) {
switch (icu_style) {
case UDAT_STYLE_LONG:
return Style::LONG;
case UDAT_STYLE_SHORT:
return Style::SHORT;
case UDAT_STYLE_NARROW:
return Style::NARROW;
case UDAT_STYLE_COUNT:
UNREACHABLE();
}
UNREACHABLE();
}
} // namespace
MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::New(
Isolate* isolate, Handle<Map> map, Handle<Object> locales,
......@@ -184,7 +193,7 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::New(
// Ref: https://github.com/tc39/proposal-intl-relative-time/issues/11
icu::RelativeDateTimeFormatter* icu_formatter =
new icu::RelativeDateTimeFormatter(icu_locale, number_format,
getIcuStyle(style_enum),
toIcuStyle(style_enum),
UDISPCTX_CAPITALIZATION_NONE, status);
if (U_FAILURE(status)) {
delete icu_formatter;
......@@ -210,7 +219,6 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::New(
relative_time_format_holder->set_flags(0);
relative_time_format_holder->set_locale(*locale_str);
relative_time_format_holder->set_numberingSystem(*numbering_system_string);
relative_time_format_holder->set_style(style_enum);
relative_time_format_holder->set_numeric(numeric_enum);
relative_time_format_holder->set_icu_formatter(*managed_formatter);
......@@ -218,16 +226,36 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::New(
return relative_time_format_holder;
}
namespace {
Handle<String> StyleAsString(Isolate* isolate, Style style) {
switch (style) {
case Style::LONG:
return ReadOnlyRoots(isolate).long_string_handle();
case Style::SHORT:
return ReadOnlyRoots(isolate).short_string_handle();
case Style::NARROW:
return ReadOnlyRoots(isolate).narrow_string_handle();
}
UNREACHABLE();
}
} // namespace
Handle<JSObject> JSRelativeTimeFormat::ResolvedOptions(
Isolate* isolate, Handle<JSRelativeTimeFormat> format_holder) {
Factory* factory = isolate->factory();
icu::RelativeDateTimeFormatter* formatter =
format_holder->icu_formatter().raw();
CHECK_NOT_NULL(formatter);
Handle<JSObject> result = factory->NewJSObject(isolate->object_function());
Handle<String> locale(format_holder->locale(), isolate);
Handle<String> numberingSystem(format_holder->numberingSystem(), isolate);
JSObject::AddProperty(isolate, result, factory->locale_string(), locale,
NONE);
JSObject::AddProperty(isolate, result, factory->style_string(),
format_holder->StyleAsString(), NONE);
JSObject::AddProperty(
isolate, result, factory->style_string(),
StyleAsString(isolate, fromIcuStyle(formatter->getFormatStyle())), NONE);
JSObject::AddProperty(isolate, result, factory->numeric_string(),
format_holder->NumericAsString(), NONE);
JSObject::AddProperty(isolate, result, factory->numberingSystem_string(),
......@@ -235,18 +263,6 @@ Handle<JSObject> JSRelativeTimeFormat::ResolvedOptions(
return result;
}
Handle<String> JSRelativeTimeFormat::StyleAsString() const {
switch (style()) {
case Style::LONG:
return GetReadOnlyRoots().long_string_handle();
case Style::SHORT:
return GetReadOnlyRoots().short_string_handle();
case Style::NARROW:
return GetReadOnlyRoots().narrow_string_handle();
}
UNREACHABLE();
}
Handle<String> JSRelativeTimeFormat::NumericAsString() const {
switch (numeric()) {
case Numeric::ALWAYS:
......
......@@ -40,7 +40,6 @@ class JSRelativeTimeFormat : public JSObject {
V8_WARN_UNUSED_RESULT static Handle<JSObject> ResolvedOptions(
Isolate* isolate, Handle<JSRelativeTimeFormat> format_holder);
Handle<String> StyleAsString() const;
Handle<String> NumericAsString() const;
// ecma402/#sec-Intl.RelativeTimeFormat.prototype.format
......@@ -63,18 +62,6 @@ class JSRelativeTimeFormat : public JSObject {
DECL_ACCESSORS(icu_formatter, Managed<icu::RelativeDateTimeFormatter>)
// Style: identifying the relative time format style used.
//
// ecma402/#sec-properties-of-intl-relativetimeformat-instances
enum class Style {
LONG, // Everything spelled out.
SHORT, // Abbreviations used when possible.
NARROW // Use the shortest possible form.
};
inline void set_style(Style style);
inline Style style() const;
// Numeric: identifying whether numerical descriptions are always used, or
// used only when no more specific version is available (e.g., "1 day ago" vs
// "yesterday").
......@@ -90,14 +77,10 @@ class JSRelativeTimeFormat : public JSObject {
// Bit positions in |flags|.
#define FLAGS_BIT_FIELDS(V, _) \
V(StyleBits, Style, 2, _) \
V(NumericBits, Numeric, 1, _)
DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
#undef FLAGS_BIT_FIELDS
STATIC_ASSERT(Style::LONG <= StyleBits::kMax);
STATIC_ASSERT(Style::SHORT <= StyleBits::kMax);
STATIC_ASSERT(Style::NARROW <= StyleBits::kMax);
STATIC_ASSERT(Numeric::AUTO <= NumericBits::kMax);
STATIC_ASSERT(Numeric::ALWAYS <= NumericBits::kMax);
......@@ -112,9 +95,6 @@ class JSRelativeTimeFormat : public JSObject {
TORQUE_GENERATED_JS_RELATIVE_TIME_FORMAT_FIELDS)
private:
static Style getStyle(const char* str);
static Numeric getNumeric(const char* str);
OBJECT_CONSTRUCTORS(JSRelativeTimeFormat, JSObject);
};
......
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