* Copyright (c) 2021-2024 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 "builtins_collator.h"
#include "ecmascript/intl/locale_helper.h"
#include "ecmascript/js_collator.h"
#include "ecmascript/js_function.h"
namespace panda::ecmascript::builtins {
constexpr uint32_t FUNCTION_LENGTH_TWO = 2;
JSTaggedValue BuiltinsCollator::CollatorConstructor(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Collator, CollatorConstructor);
[[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()) {
newTarget = constructor;
}
JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSCollator> collator = JSHandle<JSCollator>::Cast(newObject);
JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
JSHandle<JSCollator> result = JSCollator::InitializeCollator(thread, collator, locales, options);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsCollator::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Collator, SupportedLocalesOf);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<TaggedArray> availableLocales = JSCollator::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 BuiltinsCollator::Compare(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Collator, Compare);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
if (!thisValue->IsJSCollator()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not collator", JSTaggedValue::Exception());
}
JSHandle<JSCollator> collator = JSHandle<JSCollator>::Cast(thisValue);
JSHandle<JSTaggedValue> boundCompare(thread, collator->GetBoundCompare(thread));
if (boundCompare->IsUndefined()) {
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSIntlBoundFunction> intlBoundFunc = factory->NewJSIntlBoundFunction(
MethodIndex::BUILTINS_COLLATOR_ANONYMOUS_COLLATOR, FUNCTION_LENGTH_TWO);
intlBoundFunc->SetCollator(thread, collator);
collator->SetBoundCompare(thread, intlBoundFunc);
}
return collator->GetBoundCompare(thread);
}
JSTaggedValue BuiltinsCollator::AnonymousCollator(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Collator, AnonymousCollator);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSIntlBoundFunction> intlBoundFunc = JSHandle<JSIntlBoundFunction>::Cast(GetConstructor(argv));
JSHandle<JSTaggedValue> collator(thread, intlBoundFunc->GetCollator(thread));
ASSERT_PRINT(collator->IsJSObject() && collator->IsJSCollator(), "collator is not object or JSCollator");
JSHandle<JSTaggedValue> x = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> y = GetCallArg(argv, 1);
JSHandle<EcmaString> xValue = JSTaggedValue::ToString(thread, x);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Undefined());
JSHandle<EcmaString> yValue = JSTaggedValue::ToString(thread, y);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Undefined());
icu::Collator *icuCollator = (JSHandle<JSCollator>::Cast(collator))->GetIcuCollator(thread);
return JSCollator::CompareStrings(thread, icuCollator, xValue, yValue);
}
JSTaggedValue BuiltinsCollator::ResolvedOptions(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, Collator, ResolvedOptions);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
if (!thisValue->IsJSCollator()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not Collator object", JSTaggedValue::Exception());
}
JSHandle<JSObject> options = JSCollator::ResolvedOptions(thread, JSHandle<JSCollator>::Cast(thisValue));
return options.GetTaggedValue();
}
}