Commit 5258f822 authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

Remove flag from JSNumberFormat

Find out style from skeleton instead to reduce object size.

Bug: v8:10208
Change-Id: I7d26113311f51bc64ed0b259c3df81d526d5ab1b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2055660Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66274}
parent 5b2822c9
...@@ -40,7 +40,6 @@ extern class JSNumberFormat extends JSObject { ...@@ -40,7 +40,6 @@ extern class JSNumberFormat extends JSObject {
icu_number_formatter: icu_number_formatter:
Foreign; // Managed<icu::number::LocalizedNumberFormatter> Foreign; // Managed<icu::number::LocalizedNumberFormatter>
bound_format: JSFunction|Undefined; bound_format: JSFunction|Undefined;
flags: Smi;
} }
extern class JSPluralRules extends JSObject { extern class JSPluralRules extends JSObject {
......
...@@ -26,19 +26,6 @@ ACCESSORS(JSNumberFormat, icu_number_formatter, ...@@ -26,19 +26,6 @@ ACCESSORS(JSNumberFormat, icu_number_formatter,
kIcuNumberFormatterOffset) kIcuNumberFormatterOffset)
ACCESSORS(JSNumberFormat, bound_format, Object, kBoundFormatOffset) ACCESSORS(JSNumberFormat, bound_format, Object, kBoundFormatOffset)
SMI_ACCESSORS(JSNumberFormat, flags, kFlagsOffset)
inline void JSNumberFormat::set_style(Style style) {
DCHECK_GE(StyleBits::kMax, style);
int hints = flags();
hints = StyleBits::update(hints, style);
set_flags(hints);
}
inline JSNumberFormat::Style JSNumberFormat::style() const {
return StyleBits::decode(flags());
}
CAST_ACCESSOR(JSNumberFormat) CAST_ACCESSOR(JSNumberFormat)
} // namespace internal } // namespace internal
......
...@@ -32,6 +32,10 @@ namespace internal { ...@@ -32,6 +32,10 @@ namespace internal {
namespace { namespace {
// [[Style]] is one of the values "decimal", "percent", "currency",
// or "unit" identifying the style of the number format.
enum class Style { DECIMAL, PERCENT, CURRENCY, UNIT };
// [[CurrencyDisplay]] is one of the values "code", "symbol", "name", // [[CurrencyDisplay]] is one of the values "code", "symbol", "name",
// or "narrowSymbol" identifying the display of the currency number format. // or "narrowSymbol" identifying the display of the currency number format.
enum class CurrencyDisplay { enum class CurrencyDisplay {
...@@ -306,15 +310,15 @@ bool IsWellFormedCurrencyCode(const std::string& currency) { ...@@ -306,15 +310,15 @@ bool IsWellFormedCurrencyCode(const std::string& currency) {
} }
// Return the style as a String. // Return the style as a String.
Handle<String> StyleAsString(Isolate* isolate, JSNumberFormat::Style style) { Handle<String> StyleAsString(Isolate* isolate, Style style) {
switch (style) { switch (style) {
case JSNumberFormat::Style::PERCENT: case Style::PERCENT:
return ReadOnlyRoots(isolate).percent_string_handle(); return ReadOnlyRoots(isolate).percent_string_handle();
case JSNumberFormat::Style::CURRENCY: case Style::CURRENCY:
return ReadOnlyRoots(isolate).currency_string_handle(); return ReadOnlyRoots(isolate).currency_string_handle();
case JSNumberFormat::Style::UNIT: case Style::UNIT:
return ReadOnlyRoots(isolate).unit_string_handle(); return ReadOnlyRoots(isolate).unit_string_handle();
case JSNumberFormat::Style::DECIMAL: case Style::DECIMAL:
return ReadOnlyRoots(isolate).decimal_string_handle(); return ReadOnlyRoots(isolate).decimal_string_handle();
} }
UNREACHABLE(); UNREACHABLE();
...@@ -627,6 +631,19 @@ std::string UnitFromSkeleton(const icu::UnicodeString& skeleton) { ...@@ -627,6 +631,19 @@ std::string UnitFromSkeleton(const icu::UnicodeString& skeleton) {
return result + "-per-" + str.substr(begin, end - begin); return result + "-per-" + str.substr(begin, end - begin);
} }
Style StyleFromSkeleton(const icu::UnicodeString& skeleton) {
if (skeleton.indexOf("currency/") >= 0) {
return Style::CURRENCY;
}
if (skeleton.indexOf("measure-unit/") >= 0) {
return Style::UNIT;
}
if (skeleton.indexOf("percent ") >= 0) {
return Style::PERCENT;
}
return Style::DECIMAL;
}
} // anonymous namespace } // anonymous namespace
icu::number::LocalizedNumberFormatter icu::number::LocalizedNumberFormatter
...@@ -696,7 +713,7 @@ Handle<JSObject> JSNumberFormat::ResolvedOptions( ...@@ -696,7 +713,7 @@ Handle<JSObject> JSNumberFormat::ResolvedOptions(
factory->NewStringFromAsciiChecked(numberingSystem.c_str()), factory->NewStringFromAsciiChecked(numberingSystem.c_str()),
Just(kDontThrow)) Just(kDontThrow))
.FromJust()); .FromJust());
JSNumberFormat::Style style = number_format->style(); Style style = StyleFromSkeleton(skeleton);
CHECK(JSReceiver::CreateDataProperty( CHECK(JSReceiver::CreateDataProperty(
isolate, options, factory->style_string(), isolate, options, factory->style_string(),
StyleAsString(isolate, style), Just(kDontThrow)) StyleAsString(isolate, style), Just(kDontThrow))
...@@ -719,7 +736,7 @@ Handle<JSObject> JSNumberFormat::ResolvedOptions( ...@@ -719,7 +736,7 @@ Handle<JSObject> JSNumberFormat::ResolvedOptions(
.FromJust()); .FromJust());
} }
if (style == JSNumberFormat::Style::UNIT) { if (style == Style::UNIT) {
std::string unit = UnitFromSkeleton(skeleton); std::string unit = UnitFromSkeleton(skeleton);
if (!unit.empty()) { if (!unit.empty()) {
CHECK(JSReceiver::CreateDataProperty( CHECK(JSReceiver::CreateDataProperty(
...@@ -918,15 +935,13 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate, ...@@ -918,15 +935,13 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
// 3. Let style be ? GetOption(options, "style", "string", « "decimal", // 3. Let style be ? GetOption(options, "style", "string", « "decimal",
// "percent", "currency", "unit" », "decimal"). // "percent", "currency", "unit" », "decimal").
Maybe<JSNumberFormat::Style> maybe_style = Maybe<Style> maybe_style = Intl::GetStringOption<Style>(
Intl::GetStringOption<JSNumberFormat::Style>(
isolate, options, "style", service, isolate, options, "style", service,
{"decimal", "percent", "currency", "unit"}, {"decimal", "percent", "currency", "unit"},
{JSNumberFormat::Style::DECIMAL, JSNumberFormat::Style::PERCENT, {Style::DECIMAL, Style::PERCENT, Style::CURRENCY, Style::UNIT},
JSNumberFormat::Style::CURRENCY, JSNumberFormat::Style::UNIT}, Style::DECIMAL);
JSNumberFormat::Style::DECIMAL);
MAYBE_RETURN(maybe_style, MaybeHandle<JSNumberFormat>()); MAYBE_RETURN(maybe_style, MaybeHandle<JSNumberFormat>());
JSNumberFormat::Style style = maybe_style.FromJust(); Style style = maybe_style.FromJust();
// 4. Set intlObj.[[Style]] to style. // 4. Set intlObj.[[Style]] to style.
...@@ -1014,7 +1029,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate, ...@@ -1014,7 +1029,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
// 12. If style is "currency", then // 12. If style is "currency", then
icu::UnicodeString currency_ustr; icu::UnicodeString currency_ustr;
if (style == JSNumberFormat::Style::CURRENCY) { if (style == Style::CURRENCY) {
// 12.a. If currency is undefined, throw a TypeError exception. // 12.a. If currency is undefined, throw a TypeError exception.
if (!found_currency.FromJust()) { if (!found_currency.FromJust()) {
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kCurrencyCode), THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kCurrencyCode),
...@@ -1047,7 +1062,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate, ...@@ -1047,7 +1062,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
} }
// 13. If style is "unit", then // 13. If style is "unit", then
if (style == JSNumberFormat::Style::UNIT) { if (style == Style::UNIT) {
// 13.a If unit is undefined, throw a TypeError exception. // 13.a If unit is undefined, throw a TypeError exception.
if (unit == "") { if (unit == "") {
THROW_NEW_ERROR(isolate, THROW_NEW_ERROR(isolate,
...@@ -1076,14 +1091,14 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate, ...@@ -1076,14 +1091,14 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
} }
} }
if (style == JSNumberFormat::Style::PERCENT) { if (style == Style::PERCENT) {
icu_number_formatter = icu_number_formatter.unit(icu::NoUnit::percent()) icu_number_formatter = icu_number_formatter.unit(icu::NoUnit::percent())
.scale(icu::number::Scale::powerOfTen(2)); .scale(icu::number::Scale::powerOfTen(2));
} }
// 23. If style is "currency", then // 23. If style is "currency", then
int mnfd_default, mxfd_default; int mnfd_default, mxfd_default;
if (style == JSNumberFormat::Style::CURRENCY) { if (style == Style::CURRENCY) {
// b. Let cDigits be CurrencyDigits(currency). // b. Let cDigits be CurrencyDigits(currency).
int c_digits = CurrencyDigits(currency_ustr); int c_digits = CurrencyDigits(currency_ustr);
// c. Let mnfdDefault be cDigits. // c. Let mnfdDefault be cDigits.
...@@ -1095,7 +1110,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate, ...@@ -1095,7 +1110,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
// a. Let mnfdDefault be 0. // a. Let mnfdDefault be 0.
mnfd_default = 0; mnfd_default = 0;
// b. If style is "percent", then // b. If style is "percent", then
if (style == JSNumberFormat::Style::PERCENT) { if (style == Style::PERCENT) {
// i. Let mxfdDefault be 0. // i. Let mxfdDefault be 0.
mxfd_default = 0; mxfd_default = 0;
} else { } else {
...@@ -1201,8 +1216,6 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate, ...@@ -1201,8 +1216,6 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
Handle<JSNumberFormat> number_format = Handle<JSNumberFormat>::cast( Handle<JSNumberFormat> number_format = Handle<JSNumberFormat>::cast(
isolate->factory()->NewFastOrSlowJSObjectFromMap(map)); isolate->factory()->NewFastOrSlowJSObjectFromMap(map));
DisallowHeapAllocation no_gc; DisallowHeapAllocation no_gc;
number_format->set_flags(0);
number_format->set_style(style);
number_format->set_locale(*locale_str); number_format->set_locale(*locale_str);
number_format->set_icu_number_formatter(*managed_number_formatter); number_format->set_icu_number_formatter(*managed_number_formatter);
...@@ -1442,10 +1455,15 @@ MaybeHandle<JSArray> JSNumberFormat::FormatToParts( ...@@ -1442,10 +1455,15 @@ MaybeHandle<JSArray> JSNumberFormat::FormatToParts(
IcuFormatNumber(isolate, *fmt, numeric_obj, &fp_iter); IcuFormatNumber(isolate, *fmt, numeric_obj, &fp_iter);
MAYBE_RETURN(maybe_format, Handle<JSArray>()); MAYBE_RETURN(maybe_format, Handle<JSArray>());
UErrorCode status = U_ZERO_ERROR;
bool style_is_unit =
Style::UNIT == StyleFromSkeleton(fmt->toSkeleton(status));
CHECK(U_SUCCESS(status));
Handle<JSArray> result = factory->NewJSArray(0); Handle<JSArray> result = factory->NewJSArray(0);
Maybe<int> maybe_format_to_parts = ConstructParts( Maybe<int> maybe_format_to_parts =
isolate, maybe_format.FromJust(), &fp_iter, result, 0, numeric_obj, ConstructParts(isolate, maybe_format.FromJust(), &fp_iter, result, 0,
number_format->style() == JSNumberFormat::Style::UNIT); numeric_obj, style_is_unit);
MAYBE_RETURN(maybe_format_to_parts, Handle<JSArray>()); MAYBE_RETURN(maybe_format_to_parts, Handle<JSArray>());
return result; return result;
......
...@@ -73,30 +73,10 @@ class JSNumberFormat : public JSObject { ...@@ -73,30 +73,10 @@ class JSNumberFormat : public JSObject {
DECL_PRINTER(JSNumberFormat) DECL_PRINTER(JSNumberFormat)
DECL_VERIFIER(JSNumberFormat) DECL_VERIFIER(JSNumberFormat)
// [[Style]] is one of the values "decimal", "percent", "currency",
// or "unit" identifying the style of the number format.
// Note: "unit" is added in proposal-unified-intl-numberformat
enum class Style { DECIMAL, PERCENT, CURRENCY, UNIT };
inline void set_style(Style style);
inline Style style() const;
// Layout description. // Layout description.
DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize, DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize,
TORQUE_GENERATED_JS_NUMBER_FORMAT_FIELDS) TORQUE_GENERATED_JS_NUMBER_FORMAT_FIELDS)
// Bit positions in |flags|.
#define FLAGS_BIT_FIELDS(V, _) \
V(StyleBits, Style, 2, _)
DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
#undef FLAGS_BIT_FIELDS
STATIC_ASSERT(Style::DECIMAL <= StyleBits::kMax);
STATIC_ASSERT(Style::PERCENT <= StyleBits::kMax);
STATIC_ASSERT(Style::CURRENCY <= StyleBits::kMax);
STATIC_ASSERT(Style::UNIT <= StyleBits::kMax);
DECL_ACCESSORS(locale, String) DECL_ACCESSORS(locale, String)
DECL_ACCESSORS(icu_number_formatter, DECL_ACCESSORS(icu_number_formatter,
Managed<icu::number::LocalizedNumberFormatter>) Managed<icu::number::LocalizedNumberFormatter>)
......
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