[intl] Port pluralrules#select to C++

Bug: v8:5751
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I7c8aab3f0420e5a7e64aa78c642320bec4142d03
Reviewed-on: https://chromium-review.googlesource.com/1208653
Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55842}
This commit is contained in:
Ujjwal Sharma 2018-09-13 04:25:01 +05:30 committed by Commit Bot
parent 34d56ca281
commit 6020cd5b54
9 changed files with 50 additions and 39 deletions

View File

@ -3076,6 +3076,9 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
SimpleInstallFunction(isolate_, prototype, "resolvedOptions",
Builtins::kPluralRulesPrototypeResolvedOptions, 0,
false);
SimpleInstallFunction(isolate_, prototype, "select",
Builtins::kPluralRulesPrototypeSelect, 1, false);
}
}
#endif // V8_INTL_SUPPORT

View File

@ -1403,6 +1403,8 @@ namespace internal {
/* ecma402 #sec-intl.pluralrules */ \
CPP(PluralRulesConstructor) \
CPP(PluralRulesPrototypeResolvedOptions) \
/* ecma402 #sec-intl.pluralrules.prototype.select */ \
CPP(PluralRulesPrototypeSelect) \
/* ecma402 #sec-intl.pluralrules.supportedlocalesof */ \
CPP(PluralRulesSupportedLocalesOf) \
/* ecma402 #sec-intl.RelativeTimeFormat.constructor */ \

View File

@ -824,6 +824,27 @@ BUILTIN(PluralRulesPrototypeResolvedOptions) {
return *JSPluralRules::ResolvedOptions(isolate, plural_rules_holder);
}
BUILTIN(PluralRulesPrototypeSelect) {
HandleScope scope(isolate);
// 1. Let pr be the this value.
// 2. If Type(pr) is not Object, throw a TypeError exception.
// 3. If pr does not have an [[InitializedPluralRules]] internal slot, throw a
// TypeError exception.
CHECK_RECEIVER(JSPluralRules, plural_rules,
"Intl.PluralRules.prototype.select");
// 4. Let n be ? ToNumber(value).
Handle<Object> number = args.atOrUndefined(isolate, 1);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number,
Object::ToNumber(isolate, number));
double number_double = number->Number();
// 5. Return ? ResolvePlural(pr, n).
RETURN_RESULT_OR_FAILURE(isolate, JSPluralRules::ResolvePlural(
isolate, plural_rules, number_double));
}
BUILTIN(PluralRulesSupportedLocalesOf) {
HandleScope scope(isolate);
RETURN_RESULT_OR_FAILURE(

View File

@ -384,13 +384,6 @@ DEFINE_METHOD(
}
);
DEFINE_METHOD(
GlobalIntlPluralRules.prototype,
select(value) {
return %PluralRulesSelect(this, TO_NUMBER(value) + 0);
}
);
/**
* DateTimeFormat resolvedOptions method.
*/

View File

@ -190,8 +190,7 @@ MaybeHandle<JSPluralRules> JSPluralRules::InitializePluralRules(
}
MaybeHandle<String> JSPluralRules::ResolvePlural(
Isolate* isolate, Handle<JSPluralRules> plural_rules,
Handle<Object> number) {
Isolate* isolate, Handle<JSPluralRules> plural_rules, double number) {
icu::PluralRules* icu_plural_rules = plural_rules->icu_plural_rules()->raw();
CHECK_NOT_NULL(icu_plural_rules);
@ -207,7 +206,7 @@ MaybeHandle<String> JSPluralRules::ResolvePlural(
// this step, then switch to that API. Bug thread:
// http://bugs.icu-project.org/trac/ticket/12763
icu::UnicodeString rounded_string;
icu_decimal_format->format(number->Number(), rounded_string);
icu_decimal_format->format(number, rounded_string);
icu::Formattable formattable;
UErrorCode status = U_ZERO_ERROR;

View File

@ -35,8 +35,7 @@ class JSPluralRules : public JSObject {
Handle<JSPluralRules> plural_rules);
V8_WARN_UNUSED_RESULT static MaybeHandle<String> ResolvePlural(
Isolate* isolate, Handle<JSPluralRules> plural_rules,
Handle<Object> number);
Isolate* isolate, Handle<JSPluralRules> plural_rules, double number);
DECL_CAST(JSPluralRules)
DECL_PRINTER(JSPluralRules)

View File

@ -131,32 +131,6 @@ RUNTIME_FUNCTION(Runtime_DateTimeFormatResolvedOptions) {
isolate, JSDateTimeFormat::ResolvedOptions(isolate, format_holder));
}
RUNTIME_FUNCTION(Runtime_PluralRulesSelect) {
HandleScope scope(isolate);
DCHECK_EQ(2, args.length());
CONVERT_ARG_HANDLE_CHECKED(Object, plural_rules_obj, 0);
CONVERT_ARG_HANDLE_CHECKED(Object, number, 1);
// 3. If pr does not have an [[InitializedPluralRules]] internal
// slot, throw a TypeError exception.
if (!plural_rules_obj->IsJSPluralRules()) {
Handle<String> method_str = isolate->factory()->NewStringFromStaticChars(
"Intl.PluralRules.prototype.select");
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
method_str, plural_rules_obj));
}
Handle<JSPluralRules> plural_rules =
Handle<JSPluralRules>::cast(plural_rules_obj);
// 4. Return ? ResolvePlural(pr, n).
RETURN_RESULT_OR_FAILURE(
isolate, JSPluralRules::ResolvePlural(isolate, plural_rules, number));
}
RUNTIME_FUNCTION(Runtime_StringToLowerCaseIntl) {
HandleScope scope(isolate);
DCHECK_EQ(args.length(), 1);

View File

@ -207,7 +207,6 @@ namespace internal {
F(FormatList, 2, 1) \
F(FormatListToParts, 2, 1) \
F(GetDefaultICULocale, 0, 1) \
F(PluralRulesSelect, 2, 1) \
F(StringToLowerCaseIntl, 1, 1) \
F(StringToUpperCaseIntl, 1, 1) // End of macro.
#else

View File

@ -0,0 +1,21 @@
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const pr = new Intl.PluralRules();
const inputs = [undefined, null, true, false, 1, '', 'test', {}, { a: 1 }];
inputs.forEach(input => {
const number = Number(input);
const expected = pr.select(number);
const actual = pr.select(input);
assertEquals(actual, expected);
});
let count = 0;
const dummyObject = {};
dummyObject[Symbol.toPrimitive] = () => ++count;
assertEquals(pr.select(dummyObject), pr.select(count));
assertEquals(count, 1);
assertEquals(pr.select(0), pr.select(-0))