* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/builtins/builtins_plural_rules.h"
#include "ecmascript/intl/locale_helper.h"
#include "ecmascript/global_env.h"
#include "ecmascript/js_plural_rules.h"
namespace panda::ecmascript::builtins {
JSTaggedValue BuiltinsPluralRules::PluralRulesConstructor(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PluralRules, Constructor);
[[maybe_unused]] EcmaHandleScope scope(thread);
EcmaVM *ecmaVm = thread->GetEcmaVM();
ObjectFactory *factory = ecmaVm->GetFactory();
JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
if (newTarget->IsUndefined()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
}
JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSPluralRules> pluralRules = JSHandle<JSPluralRules>::Cast(newObject);
JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
JSPluralRules::InitializePluralRules(thread, pluralRules, locales, options);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return pluralRules.GetTaggedValue();
}
JSTaggedValue BuiltinsPluralRules::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PluralRules, SupportedLocalesOf);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<TaggedArray> availableLocales = JSPluralRules::GetAvailableLocales(thread);
JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsPluralRules::Select(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PluralRules, Select);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
if (!thisValue->IsJSPluralRules()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not pr object", JSTaggedValue::Exception());
}
double x = 0.0;
JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
JSTaggedNumber temp = JSTaggedValue::ToNumber(thread, value);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
x = temp.GetNumber();
JSHandle<JSPluralRules> pluralRules = JSHandle<JSPluralRules>::Cast(thisValue);
JSHandle<EcmaString> result = JSPluralRules::ResolvePlural(thread, pluralRules, x);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsPluralRules::ResolvedOptions(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PluralRules, ResolvedOptions);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
if (!thisValue->IsJSPluralRules()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not pr object", JSTaggedValue::Exception());
}
auto ecmaVm = thread->GetEcmaVM();
JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
ObjectFactory *factory = ecmaVm->GetFactory();
JSHandle<JSFunction> ctor(env->GetObjectFunction());
JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
JSHandle<JSPluralRules> pluralRules = JSHandle<JSPluralRules>::Cast(thisValue);
JSPluralRules::ResolvedOptions(thread, pluralRules, options);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return options.GetTaggedValue();
}
}