* 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 "ecmascript/builtins/builtins_number_format.h"
#include "ecmascript/js_function.h"
#include "ecmascript/js_number_format.h"
namespace panda::ecmascript::builtins {
JSTaggedValue BuiltinsNumberFormat::NumberFormatConstructor(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, NumberFormat, Constructor);
[[maybe_unused]] EcmaHandleScope scope(thread);
EcmaVM *ecmaVm = thread->GetEcmaVM();
JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
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<JSNumberFormat> numberFormat = JSHandle<JSNumberFormat>::Cast(newObject);
JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
JSNumberFormat::InitializeNumberFormat(thread, numberFormat, locales, options);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
if (GetNewTarget(argv)->IsUndefined() && thisValue->IsJSObject()) {
bool isInstanceOf = JSFunction::OrdinaryHasInstance(thread, env->GetNumberFormatFunction(), thisValue);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
if (isInstanceOf) {
PropertyDescriptor descriptor(thread, JSHandle<JSTaggedValue>::Cast(numberFormat), false, false, false);
JSHandle<JSTaggedValue> key(thread,
JSHandle<JSIntl>::Cast(env->GetIntlFunction())->GetFallbackSymbol(thread));
JSTaggedValue::DefinePropertyOrThrow(thread, thisValue, key, descriptor);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return thisValue.GetTaggedValue();
}
}
return numberFormat.GetTaggedValue();
}
JSTaggedValue BuiltinsNumberFormat::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, NumberFormat, SupportedLocalesOf);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<TaggedArray> availableLocales = JSNumberFormat::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 BuiltinsNumberFormat::Format(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, NumberFormat, Format);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
if (!thisValue->IsJSObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "nf is not object", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> nf = JSNumberFormat::UnwrapNumberFormat(thread, thisValue);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
if (nf->IsUndefined()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "nf is not object", JSTaggedValue::Exception());
}
JSHandle<JSNumberFormat> typpedNf = JSHandle<JSNumberFormat>::Cast(nf);
JSHandle<JSTaggedValue> boundFunc(thread, typpedNf->GetBoundFormat(thread));
if (boundFunc->IsUndefined()) {
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSIntlBoundFunction> intlBoundFunc = factory->NewJSIntlBoundFunction(
MethodIndex::BUILTINS_NUMBER_FORMAT_NUMBER_FORMAT_INTERNAL_FORMAT_NUMBER);
intlBoundFunc->SetNumberFormat(thread, typpedNf);
typpedNf->SetBoundFormat(thread, intlBoundFunc);
}
return typpedNf->GetBoundFormat(thread);
}
JSTaggedValue BuiltinsNumberFormat::FormatToParts(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, NumberFormat, FormatToParts);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSTaggedValue> nf = GetThis(argv);
if (!nf->IsJSNumberFormat()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not JSNumberFormat", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> x = JSTaggedValue::ToNumeric(thread, value);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSArray> result =
JSNumberFormat::FormatNumericToParts(thread, JSHandle<JSNumberFormat>::Cast(nf), x.GetTaggedValue());
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsNumberFormat::ResolvedOptions(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, NumberFormat, ResolvedOptions);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
if (!thisValue->IsJSObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not object", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> nf = JSNumberFormat::UnwrapNumberFormat(thread, thisValue);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
if (!nf->IsJSNumberFormat()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "nf is not JSNumberFormat", 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));
JSNumberFormat::ResolvedOptions(thread, JSHandle<JSNumberFormat>::Cast(nf), options);
return options.GetTaggedValue();
}
JSTaggedValue BuiltinsNumberFormat::NumberFormatInternalFormatNumber(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSIntlBoundFunction> intlBoundFunc = JSHandle<JSIntlBoundFunction>::Cast(GetConstructor(argv));
JSHandle<JSTaggedValue> nf(thread, intlBoundFunc->GetNumberFormat(thread));
ASSERT(nf->IsJSObject() && nf->IsJSNumberFormat());
JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> x = JSTaggedValue::ToNumeric(thread, value);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSTaggedValue> result =
JSNumberFormat::FormatNumeric(thread, JSHandle<JSNumberFormat>::Cast(nf), x.GetTaggedValue());
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result.GetTaggedValue();
}
}