1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/builtins/builtins_plural_rules.h"
17
18 #include "ecmascript/intl/locale_helper.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_locale.h"
21 #include "ecmascript/js_object.h"
22 #include "ecmascript/js_plural_rules.h"
23 #include "ecmascript/object_factory.h"
24
25 namespace panda::ecmascript::builtins {
PluralRulesConstructor(EcmaRuntimeCallInfo * argv)26 JSTaggedValue BuiltinsPluralRules::PluralRulesConstructor(EcmaRuntimeCallInfo *argv)
27 {
28 JSThread *thread = argv->GetThread();
29 BUILTINS_API_TRACE(thread, PluralRules, Constructor);
30 [[maybe_unused]] EcmaHandleScope scope(thread);
31 EcmaVM *ecmaVm = thread->GetEcmaVM();
32 ObjectFactory *factory = ecmaVm->GetFactory();
33
34 // 1. If NewTarget is undefined, throw a TypeError exception.
35 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
36 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
37 if (newTarget->IsUndefined()) {
38 THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
39 }
40
41 // 2. Let pluralRules be ? OrdinaryCreateFromConstructor(NewTarget, "%PluralRulesPrototype%",
42 // « [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]],
43 // [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]] »).
44 JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
45 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
46 JSHandle<JSPluralRules> pluralRules = JSHandle<JSPluralRules>::Cast(newObject);
47
48 // 3. Return ? InitializePluralRules(pluralRules, locales, options).
49 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
50 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
51 JSPluralRules::InitializePluralRules(thread, pluralRules, locales, options);
52 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
53
54 return pluralRules.GetTaggedValue();
55 }
56
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)57 JSTaggedValue BuiltinsPluralRules::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
58 {
59 JSThread *thread = argv->GetThread();
60 BUILTINS_API_TRACE(thread, PluralRules, SupportedLocalesOf);
61 [[maybe_unused]] EcmaHandleScope scope(thread);
62
63 // 1. Let availableLocales be %PluralRules%.[[AvailableLocales]].
64 JSHandle<TaggedArray> availableLocales = JSPluralRules::GetAvailableLocales(thread);
65
66 // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
67 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
68 JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
69 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
70
71 // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
72 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
73 JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
74 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
75 return result.GetTaggedValue();
76 }
77
Select(EcmaRuntimeCallInfo * argv)78 JSTaggedValue BuiltinsPluralRules::Select(EcmaRuntimeCallInfo *argv)
79 {
80 JSThread *thread = argv->GetThread();
81 BUILTINS_API_TRACE(thread, PluralRules, Select);
82 [[maybe_unused]] EcmaHandleScope scope(thread);
83
84 // 1. Let pr be the this value.
85 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
86
87 // 2. Perform ? RequireInternalSlot(pr, [[InitializedPluralRules]]).
88 if (!thisValue->IsJSPluralRules()) {
89 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not pr object", JSTaggedValue::Exception());
90 }
91
92 // 3. Let n be ? ToNumber(value).
93 double x = 0.0;
94 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
95 JSTaggedNumber temp = JSTaggedValue::ToNumber(thread, value);
96 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
97 x = temp.GetNumber();
98
99 // 4. Return ? ResolvePlural(pr, n).
100 JSHandle<JSPluralRules> pluralRules = JSHandle<JSPluralRules>::Cast(thisValue);
101 JSHandle<EcmaString> result = JSPluralRules::ResolvePlural(thread, pluralRules, x);
102 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
103 return result.GetTaggedValue();
104 }
105
ResolvedOptions(EcmaRuntimeCallInfo * argv)106 JSTaggedValue BuiltinsPluralRules::ResolvedOptions(EcmaRuntimeCallInfo *argv)
107 {
108 JSThread *thread = argv->GetThread();
109 BUILTINS_API_TRACE(thread, PluralRules, ResolvedOptions);
110 [[maybe_unused]] EcmaHandleScope scope(thread);
111
112 // 1. Let thisValue be the this value;
113 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
114
115 // 2. Perform ? RequireInternalSlot(pr, [[InitializedPluralRules]]).
116 if (!thisValue->IsJSPluralRules()) {
117 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not pr object", JSTaggedValue::Exception());
118 }
119
120 // 3. Let options be ! ObjectCreate(%ObjectPrototype%).
121 auto ecmaVm = thread->GetEcmaVM();
122 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
123 ObjectFactory *factory = ecmaVm->GetFactory();
124 JSHandle<JSFunction> ctor(env->GetObjectFunction());
125 JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
126
127 // 4. Perform resolvedOptions
128 JSHandle<JSPluralRules> pluralRules = JSHandle<JSPluralRules>::Cast(thisValue);
129 JSPluralRules::ResolvedOptions(thread, pluralRules, options);
130 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
131
132 // 5. Return options.
133 return options.GetTaggedValue();
134 }
135 } // namespace panda::ecmascript::builtins