14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_plural_rules.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/intl/locale_helper.h" 194514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_plural_rules.h" 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins { 234514f5e3Sopenharmony_ciJSTaggedValue BuiltinsPluralRules::PluralRulesConstructor(EcmaRuntimeCallInfo *argv) 244514f5e3Sopenharmony_ci{ 254514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 264514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PluralRules, Constructor); 274514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 284514f5e3Sopenharmony_ci EcmaVM *ecmaVm = thread->GetEcmaVM(); 294514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVm->GetFactory(); 304514f5e3Sopenharmony_ci 314514f5e3Sopenharmony_ci // 1. If NewTarget is undefined, throw a TypeError exception. 324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> constructor = GetConstructor(argv); 334514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv); 344514f5e3Sopenharmony_ci if (newTarget->IsUndefined()) { 354514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception()); 364514f5e3Sopenharmony_ci } 374514f5e3Sopenharmony_ci 384514f5e3Sopenharmony_ci // 2. Let pluralRules be ? OrdinaryCreateFromConstructor(NewTarget, "%PluralRulesPrototype%", 394514f5e3Sopenharmony_ci // « [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], 404514f5e3Sopenharmony_ci // [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]] »). 414514f5e3Sopenharmony_ci JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget); 424514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 434514f5e3Sopenharmony_ci JSHandle<JSPluralRules> pluralRules = JSHandle<JSPluralRules>::Cast(newObject); 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_ci // 3. Return ? InitializePluralRules(pluralRules, locales, options). 464514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0); 474514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> options = GetCallArg(argv, 1); 484514f5e3Sopenharmony_ci JSPluralRules::InitializePluralRules(thread, pluralRules, locales, options); 494514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 504514f5e3Sopenharmony_ci 514514f5e3Sopenharmony_ci return pluralRules.GetTaggedValue(); 524514f5e3Sopenharmony_ci} 534514f5e3Sopenharmony_ci 544514f5e3Sopenharmony_ciJSTaggedValue BuiltinsPluralRules::SupportedLocalesOf(EcmaRuntimeCallInfo *argv) 554514f5e3Sopenharmony_ci{ 564514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 574514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PluralRules, SupportedLocalesOf); 584514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 594514f5e3Sopenharmony_ci 604514f5e3Sopenharmony_ci // 1. Let availableLocales be %PluralRules%.[[AvailableLocales]]. 614514f5e3Sopenharmony_ci JSHandle<TaggedArray> availableLocales = JSPluralRules::GetAvailableLocales(thread); 624514f5e3Sopenharmony_ci 634514f5e3Sopenharmony_ci // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales). 644514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0); 654514f5e3Sopenharmony_ci JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales); 664514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 674514f5e3Sopenharmony_ci 684514f5e3Sopenharmony_ci // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options). 694514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> options = GetCallArg(argv, 1); 704514f5e3Sopenharmony_ci JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options); 714514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 724514f5e3Sopenharmony_ci return result.GetTaggedValue(); 734514f5e3Sopenharmony_ci} 744514f5e3Sopenharmony_ci 754514f5e3Sopenharmony_ciJSTaggedValue BuiltinsPluralRules::Select(EcmaRuntimeCallInfo *argv) 764514f5e3Sopenharmony_ci{ 774514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 784514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PluralRules, Select); 794514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 804514f5e3Sopenharmony_ci 814514f5e3Sopenharmony_ci // 1. Let pr be the this value. 824514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue = GetThis(argv); 834514f5e3Sopenharmony_ci 844514f5e3Sopenharmony_ci // 2. Perform ? RequireInternalSlot(pr, [[InitializedPluralRules]]). 854514f5e3Sopenharmony_ci if (!thisValue->IsJSPluralRules()) { 864514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "this is not pr object", JSTaggedValue::Exception()); 874514f5e3Sopenharmony_ci } 884514f5e3Sopenharmony_ci 894514f5e3Sopenharmony_ci // 3. Let n be ? ToNumber(value). 904514f5e3Sopenharmony_ci double x = 0.0; 914514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 924514f5e3Sopenharmony_ci JSTaggedNumber temp = JSTaggedValue::ToNumber(thread, value); 934514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 944514f5e3Sopenharmony_ci x = temp.GetNumber(); 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci // 4. Return ? ResolvePlural(pr, n). 974514f5e3Sopenharmony_ci JSHandle<JSPluralRules> pluralRules = JSHandle<JSPluralRules>::Cast(thisValue); 984514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSPluralRules::ResolvePlural(thread, pluralRules, x); 994514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1004514f5e3Sopenharmony_ci return result.GetTaggedValue(); 1014514f5e3Sopenharmony_ci} 1024514f5e3Sopenharmony_ci 1034514f5e3Sopenharmony_ciJSTaggedValue BuiltinsPluralRules::ResolvedOptions(EcmaRuntimeCallInfo *argv) 1044514f5e3Sopenharmony_ci{ 1054514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1064514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, PluralRules, ResolvedOptions); 1074514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_ci // 1. Let thisValue be the this value; 1104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue = GetThis(argv); 1114514f5e3Sopenharmony_ci 1124514f5e3Sopenharmony_ci // 2. Perform ? RequireInternalSlot(pr, [[InitializedPluralRules]]). 1134514f5e3Sopenharmony_ci if (!thisValue->IsJSPluralRules()) { 1144514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "this is not pr object", JSTaggedValue::Exception()); 1154514f5e3Sopenharmony_ci } 1164514f5e3Sopenharmony_ci 1174514f5e3Sopenharmony_ci // 3. Let options be ! ObjectCreate(%ObjectPrototype%). 1184514f5e3Sopenharmony_ci auto ecmaVm = thread->GetEcmaVM(); 1194514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv(); 1204514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVm->GetFactory(); 1214514f5e3Sopenharmony_ci JSHandle<JSFunction> ctor(env->GetObjectFunction()); 1224514f5e3Sopenharmony_ci JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor)); 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_ci // 4. Perform resolvedOptions 1254514f5e3Sopenharmony_ci JSHandle<JSPluralRules> pluralRules = JSHandle<JSPluralRules>::Cast(thisValue); 1264514f5e3Sopenharmony_ci JSPluralRules::ResolvedOptions(thread, pluralRules, options); 1274514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1284514f5e3Sopenharmony_ci 1294514f5e3Sopenharmony_ci // 5. Return options. 1304514f5e3Sopenharmony_ci return options.GetTaggedValue(); 1314514f5e3Sopenharmony_ci} 1324514f5e3Sopenharmony_ci} // namespace panda::ecmascript::builtins