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

[Intl] Use bit flags for JSPluralRules

Bug: v8:5751
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I9653b862f58f25e35b5443fb5d47d69c36792768
Reviewed-on: https://chromium-review.googlesource.com/c/1295929
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56921}
parent 266c0b96
......@@ -792,6 +792,7 @@ BUILTIN(PluralRulesConstructor) {
JSObject::New(target, new_target, Handle<AllocationSite>::null()));
Handle<JSPluralRules> plural_rules =
Handle<JSPluralRules>::cast(plural_rules_obj);
plural_rules->set_flags(0);
// 3. Return ? InitializePluralRules(pluralRules, locales, options).
RETURN_RESULT_OR_FAILURE(
......
......@@ -9,6 +9,7 @@
#define INTERNALIZED_STRING_LIST_GENERATOR_INTL(V, _) \
V(_, breakType_string, "breakType") \
V(_, calendar_string, "calendar") \
V(_, cardinal_string, "cardinal") \
V(_, caseFirst_string, "caseFirst") \
V(_, day_string, "day") \
V(_, dayPeriod_string, "dayPeriod") \
......@@ -54,6 +55,7 @@
V(_, normal_string, "normal") \
V(_, numberingSystem_string, "numberingSystem") \
V(_, numeric_string, "numeric") \
V(_, ordinal_string, "ordinal") \
V(_, percentSign_string, "percentSign") \
V(_, plusSign_string, "plusSign") \
V(_, quarter_string, "quarter") \
......
......@@ -2015,7 +2015,7 @@ void JSPluralRules::JSPluralRulesVerify(Isolate* isolate) {
CHECK(IsJSPluralRules());
JSObjectVerify(isolate);
VerifyObjectField(isolate, kLocaleOffset);
VerifyObjectField(isolate, kTypeOffset);
VerifyObjectField(isolate, kFlagsOffset);
VerifyObjectField(isolate, kICUPluralRulesOffset);
VerifyObjectField(isolate, kICUDecimalFormatOffset);
}
......
......@@ -2056,7 +2056,7 @@ void JSNumberFormat::JSNumberFormatPrint(std::ostream& os) { // NOLINT
void JSPluralRules::JSPluralRulesPrint(std::ostream& os) { // NOLINT
JSObjectPrintHeader(os, this, "JSPluralRules");
os << "\n - locale: " << Brief(locale());
os << "\n - type: " << Brief(type());
os << "\n - type: " << TypeAsString();
os << "\n - icu plural rules: " << Brief(icu_plural_rules());
os << "\n - icu decimal format: " << Brief(icu_decimal_format());
JSObjectPrintBody(os, this);
......
......@@ -20,12 +20,23 @@ namespace v8 {
namespace internal {
ACCESSORS(JSPluralRules, locale, String, kLocaleOffset)
ACCESSORS(JSPluralRules, type, String, kTypeOffset)
SMI_ACCESSORS(JSPluralRules, flags, kFlagsOffset)
ACCESSORS(JSPluralRules, icu_plural_rules, Managed<icu::PluralRules>,
kICUPluralRulesOffset)
ACCESSORS(JSPluralRules, icu_decimal_format, Managed<icu::DecimalFormat>,
kICUDecimalFormatOffset)
inline void JSPluralRules::set_type(Type type) {
DCHECK_LT(type, Type::COUNT);
int hints = flags();
hints = TypeBits::update(hints, type);
set_flags(hints);
}
inline JSPluralRules::Type JSPluralRules::type() const {
return TypeBits::decode(flags());
}
CAST_ACCESSOR(JSPluralRules);
} // namespace internal
......
......@@ -23,22 +23,22 @@ namespace internal {
namespace {
bool CreateICUPluralRules(Isolate* isolate, const icu::Locale& icu_locale,
const char* type_string,
JSPluralRules::Type type,
std::unique_ptr<icu::PluralRules>* pl,
std::unique_ptr<icu::DecimalFormat>* nf) {
// Make formatter from options. Numbering system is added
// to the locale as Unicode extension (if it was specified at all).
UErrorCode status = U_ZERO_ERROR;
UPluralType type = UPLURAL_TYPE_CARDINAL;
if (strcmp(type_string, "ordinal") == 0) {
type = UPLURAL_TYPE_ORDINAL;
UPluralType icu_type = UPLURAL_TYPE_CARDINAL;
if (type == JSPluralRules::Type::ORDINAL) {
icu_type = UPLURAL_TYPE_ORDINAL;
} else {
CHECK_EQ(0, strcmp(type_string, "cardinal"));
CHECK_EQ(JSPluralRules::Type::CARDINAL, type);
}
std::unique_ptr<icu::PluralRules> plural_rules(
icu::PluralRules::forLocale(icu_locale, type, status));
icu::PluralRules::forLocale(icu_locale, icu_type, status));
if (U_FAILURE(status)) {
return false;
}
......@@ -59,7 +59,7 @@ bool CreateICUPluralRules(Isolate* isolate, const icu::Locale& icu_locale,
}
void InitializeICUPluralRules(
Isolate* isolate, const icu::Locale& icu_locale, const char* type,
Isolate* isolate, const icu::Locale& icu_locale, JSPluralRules::Type type,
std::unique_ptr<icu::PluralRules>* plural_rules,
std::unique_ptr<icu::DecimalFormat>* number_format) {
bool success = CreateICUPluralRules(isolate, icu_locale, type, plural_rules,
......@@ -81,10 +81,22 @@ void InitializeICUPluralRules(
} // namespace
Handle<String> JSPluralRules::TypeAsString() const {
switch (type()) {
case Type::CARDINAL:
return GetReadOnlyRoots().cardinal_string_handle();
case Type::ORDINAL:
return GetReadOnlyRoots().ordinal_string_handle();
case Type::COUNT:
UNREACHABLE();
}
}
// static
MaybeHandle<JSPluralRules> JSPluralRules::Initialize(
Isolate* isolate, Handle<JSPluralRules> plural_rules,
Handle<Object> locales, Handle<Object> options_obj) {
plural_rules->set_flags(0);
// 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
Maybe<std::vector<std::string>> maybe_requested_locales =
Intl::CanonicalizeLocaleList(isolate, locales);
......@@ -120,18 +132,21 @@ MaybeHandle<JSPluralRules> JSPluralRules::Initialize(
// "ordinal" », "cardinal").
std::vector<const char*> values = {"cardinal", "ordinal"};
std::unique_ptr<char[]> type_str = nullptr;
const char* type_cstr = "cardinal";
JSPluralRules::Type type = JSPluralRules::Type::CARDINAL;
Maybe<bool> found = Intl::GetStringOption(isolate, options, "type", values,
"Intl.PluralRules", &type_str);
MAYBE_RETURN(found, MaybeHandle<JSPluralRules>());
if (found.FromJust()) {
type_cstr = type_str.get();
DCHECK_NOT_NULL(type_str.get());
if (strcmp("ordinal", type_str.get()) == 0) {
type = JSPluralRules::Type::ORDINAL;
} else {
DCHECK_EQ(0, strcmp("cardinal", type_str.get()));
}
}
// 8. Set pluralRules.[[Type]] to t.
Handle<String> type =
isolate->factory()->NewStringFromAsciiChecked(type_cstr);
plural_rules->set_type(*type);
plural_rules->set_type(type);
// Note: The spec says we should do ResolveLocale after performing
// SetNumberFormatDigitOptions but we need the locale to create all
......@@ -160,7 +175,7 @@ MaybeHandle<JSPluralRules> JSPluralRules::Initialize(
std::unique_ptr<icu::PluralRules> icu_plural_rules;
std::unique_ptr<icu::DecimalFormat> icu_decimal_format;
InitializeICUPluralRules(isolate, icu_locale, type_cstr, &icu_plural_rules,
InitializeICUPluralRules(isolate, icu_locale, type, &icu_plural_rules,
&icu_decimal_format);
CHECK_NOT_NULL(icu_plural_rules.get());
CHECK_NOT_NULL(icu_decimal_format.get());
......@@ -245,8 +260,8 @@ Handle<JSObject> JSPluralRules::ResolvedOptions(
Handle<String> locale_value(plural_rules->locale(), isolate);
CreateDataPropertyForOptions(isolate, options, locale_value, "locale");
Handle<String> type_value(plural_rules->type(), isolate);
CreateDataPropertyForOptions(isolate, options, type_value, "type");
CreateDataPropertyForOptions(isolate, options, plural_rules->TypeAsString(),
"type");
icu::DecimalFormat* icu_decimal_format =
plural_rules->icu_decimal_format()->raw();
......
......@@ -37,16 +37,36 @@ class JSPluralRules : public JSObject {
V8_WARN_UNUSED_RESULT static MaybeHandle<String> ResolvePlural(
Isolate* isolate, Handle<JSPluralRules> plural_rules, double number);
// [[Type]] is one of the values "cardinal" or "ordinal",
// identifying the plural rules used.
enum class Type {
CARDINAL,
ORDINAL,
COUNT
};
inline void set_type(Type type);
inline Type type() const;
Handle<String> TypeAsString() const;
DECL_CAST(JSPluralRules)
DECL_PRINTER(JSPluralRules)
DECL_VERIFIER(JSPluralRules)
// Bit positions in |flags|.
#define FLAGS_BIT_FIELDS(V, _) V(TypeBits, Type, 1, _)
DEFINE_BIT_FIELDS(FLAGS_BIT_FIELDS)
#undef FLAGS_BIT_FIELDS
STATIC_ASSERT(Type::CARDINAL <= TypeBits::kMax);
STATIC_ASSERT(Type::ORDINAL <= TypeBits::kMax);
// Layout description.
#define JS_PLURAL_RULES_FIELDS(V) \
V(kLocaleOffset, kPointerSize) \
/* In the future, this can be an enum, \
and not a string. */ \
V(kTypeOffset, kPointerSize) \
V(kFlagsOffset, kPointerSize) \
V(kICUPluralRulesOffset, kPointerSize) \
V(kICUDecimalFormatOffset, kPointerSize) \
/* Total size. */ \
......@@ -56,7 +76,7 @@ class JSPluralRules : public JSObject {
#undef JS_PLURAL_RULES_FIELDS
DECL_ACCESSORS(locale, String)
DECL_ACCESSORS(type, String)
DECL_INT_ACCESSORS(flags)
DECL_ACCESSORS(icu_plural_rules, Managed<icu::PluralRules>)
DECL_ACCESSORS(icu_decimal_format, Managed<icu::DecimalFormat>)
......
......@@ -296,41 +296,41 @@ KNOWN_MAPS = {
("RO_SPACE", 0x02699): (171, "Tuple2Map"),
("RO_SPACE", 0x02739): (173, "ArrayBoilerplateDescriptionMap"),
("RO_SPACE", 0x02a79): (161, "InterceptorInfoMap"),
("RO_SPACE", 0x05079): (153, "AccessCheckInfoMap"),
("RO_SPACE", 0x050c9): (154, "AccessorInfoMap"),
("RO_SPACE", 0x05119): (155, "AccessorPairMap"),
("RO_SPACE", 0x05169): (156, "AliasedArgumentsEntryMap"),
("RO_SPACE", 0x051b9): (157, "AllocationMementoMap"),
("RO_SPACE", 0x05209): (158, "AsyncGeneratorRequestMap"),
("RO_SPACE", 0x05259): (159, "DebugInfoMap"),
("RO_SPACE", 0x052a9): (160, "FunctionTemplateInfoMap"),
("RO_SPACE", 0x052f9): (162, "InterpreterDataMap"),
("RO_SPACE", 0x05349): (163, "ModuleInfoEntryMap"),
("RO_SPACE", 0x05399): (164, "ModuleMap"),
("RO_SPACE", 0x053e9): (165, "ObjectTemplateInfoMap"),
("RO_SPACE", 0x05439): (166, "PromiseCapabilityMap"),
("RO_SPACE", 0x05489): (167, "PromiseReactionMap"),
("RO_SPACE", 0x054d9): (168, "PrototypeInfoMap"),
("RO_SPACE", 0x05529): (169, "ScriptMap"),
("RO_SPACE", 0x05579): (170, "StackFrameInfoMap"),
("RO_SPACE", 0x055c9): (172, "Tuple3Map"),
("RO_SPACE", 0x05619): (174, "WasmDebugInfoMap"),
("RO_SPACE", 0x05669): (175, "WasmExportedFunctionDataMap"),
("RO_SPACE", 0x056b9): (176, "CallableTaskMap"),
("RO_SPACE", 0x05709): (177, "CallbackTaskMap"),
("RO_SPACE", 0x05759): (178, "PromiseFulfillReactionJobTaskMap"),
("RO_SPACE", 0x057a9): (179, "PromiseRejectReactionJobTaskMap"),
("RO_SPACE", 0x057f9): (180, "PromiseResolveThenableJobTaskMap"),
("RO_SPACE", 0x05849): (181, "MicrotaskQueueMap"),
("RO_SPACE", 0x05899): (182, "AllocationSiteWithWeakNextMap"),
("RO_SPACE", 0x058e9): (182, "AllocationSiteWithoutWeakNextMap"),
("RO_SPACE", 0x05939): (214, "LoadHandler1Map"),
("RO_SPACE", 0x05989): (214, "LoadHandler2Map"),
("RO_SPACE", 0x059d9): (214, "LoadHandler3Map"),
("RO_SPACE", 0x05a29): (221, "StoreHandler0Map"),
("RO_SPACE", 0x05a79): (221, "StoreHandler1Map"),
("RO_SPACE", 0x05ac9): (221, "StoreHandler2Map"),
("RO_SPACE", 0x05b19): (221, "StoreHandler3Map"),
("RO_SPACE", 0x050a9): (153, "AccessCheckInfoMap"),
("RO_SPACE", 0x050f9): (154, "AccessorInfoMap"),
("RO_SPACE", 0x05149): (155, "AccessorPairMap"),
("RO_SPACE", 0x05199): (156, "AliasedArgumentsEntryMap"),
("RO_SPACE", 0x051e9): (157, "AllocationMementoMap"),
("RO_SPACE", 0x05239): (158, "AsyncGeneratorRequestMap"),
("RO_SPACE", 0x05289): (159, "DebugInfoMap"),
("RO_SPACE", 0x052d9): (160, "FunctionTemplateInfoMap"),
("RO_SPACE", 0x05329): (162, "InterpreterDataMap"),
("RO_SPACE", 0x05379): (163, "ModuleInfoEntryMap"),
("RO_SPACE", 0x053c9): (164, "ModuleMap"),
("RO_SPACE", 0x05419): (165, "ObjectTemplateInfoMap"),
("RO_SPACE", 0x05469): (166, "PromiseCapabilityMap"),
("RO_SPACE", 0x054b9): (167, "PromiseReactionMap"),
("RO_SPACE", 0x05509): (168, "PrototypeInfoMap"),
("RO_SPACE", 0x05559): (169, "ScriptMap"),
("RO_SPACE", 0x055a9): (170, "StackFrameInfoMap"),
("RO_SPACE", 0x055f9): (172, "Tuple3Map"),
("RO_SPACE", 0x05649): (174, "WasmDebugInfoMap"),
("RO_SPACE", 0x05699): (175, "WasmExportedFunctionDataMap"),
("RO_SPACE", 0x056e9): (176, "CallableTaskMap"),
("RO_SPACE", 0x05739): (177, "CallbackTaskMap"),
("RO_SPACE", 0x05789): (178, "PromiseFulfillReactionJobTaskMap"),
("RO_SPACE", 0x057d9): (179, "PromiseRejectReactionJobTaskMap"),
("RO_SPACE", 0x05829): (180, "PromiseResolveThenableJobTaskMap"),
("RO_SPACE", 0x05879): (181, "MicrotaskQueueMap"),
("RO_SPACE", 0x058c9): (182, "AllocationSiteWithWeakNextMap"),
("RO_SPACE", 0x05919): (182, "AllocationSiteWithoutWeakNextMap"),
("RO_SPACE", 0x05969): (214, "LoadHandler1Map"),
("RO_SPACE", 0x059b9): (214, "LoadHandler2Map"),
("RO_SPACE", 0x05a09): (214, "LoadHandler3Map"),
("RO_SPACE", 0x05a59): (221, "StoreHandler0Map"),
("RO_SPACE", 0x05aa9): (221, "StoreHandler1Map"),
("RO_SPACE", 0x05af9): (221, "StoreHandler2Map"),
("RO_SPACE", 0x05b49): (221, "StoreHandler3Map"),
("MAP_SPACE", 0x00139): (1057, "ExternalMap"),
("MAP_SPACE", 0x00189): (1073, "JSMessageObjectMap"),
}
......
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