* 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_date_time_format.h"
#include "ecmascript/intl/locale_helper.h"
#include "ecmascript/global_env.h"
#include "ecmascript/js_date.h"
#include "ecmascript/js_date_time_format.h"
#include "ecmascript/js_function.h"
#include "ecmascript/js_intl.h"
namespace panda::ecmascript::builtins {
JSTaggedValue BuiltinsDateTimeFormat::DateTimeFormatConstructor(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, DateTimeFormat, 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<JSDateTimeFormat> dateTimeFormat = JSHandle<JSDateTimeFormat>::Cast(newObject);
JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
JSHandle<JSDateTimeFormat> dtf =
JSDateTimeFormat::InitializeDateTimeFormat(thread, dateTimeFormat, 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->GetDateTimeFormatFunction(), thisValue);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
if (isInstanceOf) {
PropertyDescriptor descriptor(thread, JSHandle<JSTaggedValue>::Cast(dtf), 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 dtf.GetTaggedValue();
}
JSTaggedValue BuiltinsDateTimeFormat::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, DateTimeFormat, SupportedLocalesOf);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<TaggedArray> availableLocales = JSDateTimeFormat::GainAvailableLocales(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 BuiltinsDateTimeFormat::Format(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, DateTimeFormat, Format);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
if (!thisValue->IsJSObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "dtf is not object", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> dtfValue = JSDateTimeFormat::UnwrapDateTimeFormat(thread, thisValue);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
if (dtfValue->IsUndefined()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "dtfValue is not object", JSTaggedValue::Exception());
}
JSHandle<JSDateTimeFormat> dtf = JSHandle<JSDateTimeFormat>::Cast(dtfValue);
JSHandle<JSTaggedValue> boundFormat(thread, dtf->GetBoundFormat(thread));
if (boundFormat->IsUndefined()) {
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSIntlBoundFunction> intlBoundFunc = factory->NewJSIntlBoundFunction(
MethodIndex::BUILTINS_DATE_TIME_FORMAT_ANONYMOUS_DATE_TIME_FORMAT);
intlBoundFunc->SetDateTimeFormat(thread, dtf);
dtf->SetBoundFormat(thread, intlBoundFunc);
}
return dtf->GetBoundFormat(thread);
}
JSTaggedValue BuiltinsDateTimeFormat::AnonymousDateTimeFormat(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, DateTimeFormat, AnonymousDateTimeFormat);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSIntlBoundFunction> intlBoundFunc = JSHandle<JSIntlBoundFunction>::Cast(GetConstructor(argv));
JSHandle<JSTaggedValue> dtf(thread, intlBoundFunc->GetDateTimeFormat(thread));
ASSERT_PRINT(dtf->IsJSObject() && dtf->IsJSDateTimeFormat(), "dtf is not object or JSDateTimeFormat");
JSHandle<JSTaggedValue> date = GetCallArg(argv, 0);
double x = 0.0;
if (date->IsUndefined()) {
x = JSDate::Now().GetNumber();
} else {
JSTaggedNumber xNumber = JSTaggedValue::ToNumber(thread, date);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
x = xNumber.GetNumber();
}
JSHandle<EcmaString> result = JSDateTimeFormat::FormatDateTime(thread, JSHandle<JSDateTimeFormat>::Cast(dtf), x);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsDateTimeFormat::FormatToParts(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, DateTimeFormat, FormatToParts);
[[maybe_unused]] EcmaHandleScope scope(thread);
JSHandle<JSTaggedValue> dtf = GetThis(argv);
if (!dtf->IsJSDateTimeFormat()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "is not JSDateTimeFormat", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> date = GetCallArg(argv, 0);
double x = 0.0;
if (date->IsUndefined()) {
x = JSDate::Now().GetNumber();
} else {
JSTaggedNumber xNumber = JSTaggedValue::ToNumber(thread, date);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
x = xNumber.GetNumber();
}
double xValue = JSDate::TimeClip(x);
if (std::isnan(xValue)) {
THROW_RANGE_ERROR_AND_RETURN(thread, "Invalid time value", JSTaggedValue::Exception());
}
JSHandle<JSArray> result =
JSDateTimeFormat::FormatDateTimeToParts(thread, JSHandle<JSDateTimeFormat>::Cast(dtf), xValue);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsDateTimeFormat::ResolvedOptions(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, DateTimeFormat, 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());
}
thisValue = JSDateTimeFormat::UnwrapDateTimeFormat(thread, thisValue);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
if (!thisValue->IsJSDateTimeFormat()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "dtf is not JSDateTimeFormat", 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));
JSDateTimeFormat::ResolvedOptions(thread, JSHandle<JSDateTimeFormat>::Cast(thisValue), options);
return options.GetTaggedValue();
}
JSTaggedValue BuiltinsDateTimeFormat::FormatRange(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, DateTimeFormat, FormatRange);
[[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());
}
if (!thisValue->IsJSDateTimeFormat()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSDateTimeFormat", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> startDate = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> endDate = GetCallArg(argv, 1);
if (startDate->IsUndefined() || endDate->IsUndefined()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "startDate or endDate is undefined", JSTaggedValue::Exception());
}
JSTaggedNumber valueX = JSTaggedValue::ToNumber(thread, startDate);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
double x = valueX.GetNumber();
JSTaggedNumber valueY = JSTaggedValue::ToNumber(thread, endDate);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
double y = valueY.GetNumber();
JSHandle<JSDateTimeFormat> dtf = JSHandle<JSDateTimeFormat>::Cast(thisValue);
JSHandle<EcmaString> result = JSDateTimeFormat::NormDateTimeRange(thread, dtf, x, y);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsDateTimeFormat::FormatRangeToParts(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, DateTimeFormat, FormatRangeToParts);
[[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());
}
if (!thisValue->IsJSDateTimeFormat()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSDateTimeFormat", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> startDate = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> endDate = GetCallArg(argv, 1);
if (startDate->IsUndefined() || endDate->IsUndefined()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "startDate or endDate is undefined", JSTaggedValue::Exception());
}
JSTaggedNumber valueX = JSTaggedValue::ToNumber(thread, startDate);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
double x = valueX.GetNumber();
JSTaggedNumber valueY = JSTaggedValue::ToNumber(thread, endDate);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
double y = valueY.GetNumber();
JSHandle<JSDateTimeFormat> dtf = JSHandle<JSDateTimeFormat>::Cast(thisValue);
JSHandle<JSArray> result = JSDateTimeFormat::NormDateTimeRangeToParts(thread, dtf, x, y);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result.GetTaggedValue();
}
}