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_relative_time_format.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins { 194514f5e3Sopenharmony_ciJSTaggedValue BuiltinsRelativeTimeFormat::RelativeTimeFormatConstructor(EcmaRuntimeCallInfo *argv) 204514f5e3Sopenharmony_ci{ 214514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 224514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, RelativeTimeFormat, Constructor); 234514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 244514f5e3Sopenharmony_ci EcmaVM *ecmaVm = thread->GetEcmaVM(); 254514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv(); 264514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVm->GetFactory(); 274514f5e3Sopenharmony_ci 284514f5e3Sopenharmony_ci // 1. If NewTarget is undefined, throw a TypeError exception. 294514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv); 304514f5e3Sopenharmony_ci if (newTarget->IsUndefined()) { 314514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception()); 324514f5e3Sopenharmony_ci } 334514f5e3Sopenharmony_ci 344514f5e3Sopenharmony_ci // 2. Let relativeTimeFormat be ? OrdinaryCreateFromConstructor 354514f5e3Sopenharmony_ci // (NewTarget, "%RelativeTimeFormatPrototype%", « [[InitializedRelativeTimeFormat]], 364514f5e3Sopenharmony_ci // [[Locale]], [[DataLocale]], [[Style]], [[Numeric]], [[NumberFormat]], [[NumberingSystem]], [[PluralRules]] »). 374514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> constructor = GetConstructor(argv); 384514f5e3Sopenharmony_ci JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget); 394514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 404514f5e3Sopenharmony_ci JSHandle<JSRelativeTimeFormat> relativeTimeFormat = JSHandle<JSRelativeTimeFormat>::Cast(newObject); 414514f5e3Sopenharmony_ci 424514f5e3Sopenharmony_ci // 3. Perform ? InitializeRelativeTimeFormat(relativeTimeFormat, locales, options). 434514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0); 444514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> options = GetCallArg(argv, 1); 454514f5e3Sopenharmony_ci JSRelativeTimeFormat::InitializeRelativeTimeFormat(thread, relativeTimeFormat, locales, options); 464514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 474514f5e3Sopenharmony_ci 484514f5e3Sopenharmony_ci // 4. Intl.RelativeTimeFormat.prototype[ @@toStringTag ] 494514f5e3Sopenharmony_ci // This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }. 504514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue = GetThis(argv); 514514f5e3Sopenharmony_ci bool isInstanceOf = JSObject::InstanceOf(thread, thisValue, env->GetRelativeTimeFormatFunction()); 524514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 534514f5e3Sopenharmony_ci if (newTarget->IsUndefined() && thisValue->IsJSObject() && isInstanceOf) { 544514f5e3Sopenharmony_ci PropertyDescriptor descriptor(thread, JSHandle<JSTaggedValue>::Cast(relativeTimeFormat), false, false, true); 554514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(thread, JSHandle<JSIntl>::Cast(env->GetIntlFunction())->GetFallbackSymbol()); 564514f5e3Sopenharmony_ci JSTaggedValue::DefinePropertyOrThrow(thread, thisValue, key, descriptor); 574514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 584514f5e3Sopenharmony_ci return thisValue.GetTaggedValue(); 594514f5e3Sopenharmony_ci } 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci return relativeTimeFormat.GetTaggedValue(); 624514f5e3Sopenharmony_ci} 634514f5e3Sopenharmony_ci 644514f5e3Sopenharmony_ciJSTaggedValue BuiltinsRelativeTimeFormat::SupportedLocalesOf(EcmaRuntimeCallInfo *argv) 654514f5e3Sopenharmony_ci{ 664514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 674514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, RelativeTimeFormat, SupportedLocalesOf); 684514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 694514f5e3Sopenharmony_ci 704514f5e3Sopenharmony_ci // 1. Let availableLocales be %RelativeTimeFormat%.[[AvailableLocales]]. 714514f5e3Sopenharmony_ci std::vector<std::string> availableStringLocales = 724514f5e3Sopenharmony_ci intl::LocaleHelper::GetAvailableLocales(thread, "calendar", nullptr); 734514f5e3Sopenharmony_ci JSHandle<TaggedArray> availableLocales = JSLocale::ConstructLocaleList(thread, availableStringLocales); 744514f5e3Sopenharmony_ci 754514f5e3Sopenharmony_ci // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales). 764514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0); 774514f5e3Sopenharmony_ci JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales); 784514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 794514f5e3Sopenharmony_ci 804514f5e3Sopenharmony_ci // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options). 814514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> options = GetCallArg(argv, 1); 824514f5e3Sopenharmony_ci JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options); 834514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 844514f5e3Sopenharmony_ci return result.GetTaggedValue(); 854514f5e3Sopenharmony_ci} 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ciJSTaggedValue BuiltinsRelativeTimeFormat::Format(EcmaRuntimeCallInfo *argv) 884514f5e3Sopenharmony_ci{ 894514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 904514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, RelativeTimeFormat, Format); 914514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ci // 1. Let relativeTimeFormat be the this value. 944514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue = GetThis(argv); 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci // 2. Perform ? RequireInternalSlot(relativeTimeFormat, [[InitializedRelativeTimeFormat]]). 974514f5e3Sopenharmony_ci if (!thisValue->IsJSRelativeTimeFormat()) { 984514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "this is not rtf object", JSTaggedValue::Exception()); 994514f5e3Sopenharmony_ci } 1004514f5e3Sopenharmony_ci 1014514f5e3Sopenharmony_ci // 3. Let value be ? ToNumber(value). 1024514f5e3Sopenharmony_ci double x = 0.0; 1034514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 1044514f5e3Sopenharmony_ci JSTaggedNumber temp = JSTaggedValue::ToNumber(thread, value); 1054514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1064514f5e3Sopenharmony_ci x = temp.GetNumber(); 1074514f5e3Sopenharmony_ci 1084514f5e3Sopenharmony_ci // 4. Let unit be ? ToString(unit). 1094514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> unitValue = GetCallArg(argv, 1); 1104514f5e3Sopenharmony_ci JSHandle<EcmaString> unit = JSTaggedValue::ToString(thread, unitValue); 1114514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1124514f5e3Sopenharmony_ci 1134514f5e3Sopenharmony_ci // 5. Return ? FormatRelativeTime(relativeTimeFormat, value, unit). 1144514f5e3Sopenharmony_ci JSHandle<JSRelativeTimeFormat> relativeTimeFormat = JSHandle<JSRelativeTimeFormat>::Cast(thisValue); 1154514f5e3Sopenharmony_ci JSHandle<EcmaString> result = JSRelativeTimeFormat::Format(thread, x, unit, relativeTimeFormat); 1164514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1174514f5e3Sopenharmony_ci return result.GetTaggedValue(); 1184514f5e3Sopenharmony_ci} 1194514f5e3Sopenharmony_ci 1204514f5e3Sopenharmony_ciJSTaggedValue BuiltinsRelativeTimeFormat::FormatToParts(EcmaRuntimeCallInfo *argv) 1214514f5e3Sopenharmony_ci{ 1224514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1234514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, RelativeTimeFormat, FormatToParts); 1244514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 1254514f5e3Sopenharmony_ci 1264514f5e3Sopenharmony_ci // 1. Let relativeTimeFormat be the this value. 1274514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue = GetThis(argv); 1284514f5e3Sopenharmony_ci 1294514f5e3Sopenharmony_ci // 2. Perform ? RequireInternalSlot(relativeTimeFormat, [[InitializedRelativeTimeFormat]]). 1304514f5e3Sopenharmony_ci if (!thisValue->IsJSRelativeTimeFormat()) { 1314514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "this is not rtf object", JSTaggedValue::Exception()); 1324514f5e3Sopenharmony_ci } 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ci // 3. Let value be ? ToNumber(value). 1354514f5e3Sopenharmony_ci double x = 0.0; 1364514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = GetCallArg(argv, 0); 1374514f5e3Sopenharmony_ci JSTaggedNumber temp = JSTaggedValue::ToNumber(thread, value); 1384514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1394514f5e3Sopenharmony_ci x = temp.GetNumber(); 1404514f5e3Sopenharmony_ci 1414514f5e3Sopenharmony_ci // 4. Let unit be ? ToString(unit). 1424514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> unitValue = GetCallArg(argv, 1); 1434514f5e3Sopenharmony_ci JSHandle<EcmaString> unit = JSTaggedValue::ToString(thread, unitValue); 1444514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1454514f5e3Sopenharmony_ci 1464514f5e3Sopenharmony_ci // 5. Return ? FormatRelativeTime(relativeTimeFormat, value, unit). 1474514f5e3Sopenharmony_ci JSHandle<JSRelativeTimeFormat> relativeTimeFormat = JSHandle<JSRelativeTimeFormat>::Cast(thisValue); 1484514f5e3Sopenharmony_ci JSHandle<JSArray> result = JSRelativeTimeFormat::FormatToParts(thread, x, unit, relativeTimeFormat); 1494514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1504514f5e3Sopenharmony_ci return result.GetTaggedValue(); 1514514f5e3Sopenharmony_ci} 1524514f5e3Sopenharmony_ci 1534514f5e3Sopenharmony_ciJSTaggedValue BuiltinsRelativeTimeFormat::ResolvedOptions(EcmaRuntimeCallInfo *argv) 1544514f5e3Sopenharmony_ci{ 1554514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1564514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, RelativeTimeFormat, ResolvedOptions); 1574514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 1584514f5e3Sopenharmony_ci 1594514f5e3Sopenharmony_ci // 1. Let relativeTimeFormat be the this value. 1604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue = GetThis(argv); 1614514f5e3Sopenharmony_ci 1624514f5e3Sopenharmony_ci // 2. Perform ? RequireInternalSlot(relativeTimeFormat, [[InitializedRelativeTimeFormat]]). 1634514f5e3Sopenharmony_ci if (!thisValue->IsJSRelativeTimeFormat()) { 1644514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "this is not rtf object", JSTaggedValue::Exception()); 1654514f5e3Sopenharmony_ci } 1664514f5e3Sopenharmony_ci 1674514f5e3Sopenharmony_ci // 3. Let options be ! ObjectCreate(%ObjectPrototype%). 1684514f5e3Sopenharmony_ci auto ecmaVm = thread->GetEcmaVM(); 1694514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv(); 1704514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVm->GetFactory(); 1714514f5e3Sopenharmony_ci JSHandle<JSFunction> ctor(env->GetObjectFunction()); 1724514f5e3Sopenharmony_ci JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor)); 1734514f5e3Sopenharmony_ci 1744514f5e3Sopenharmony_ci // 4. perform resolvedOptions 1754514f5e3Sopenharmony_ci JSHandle<JSRelativeTimeFormat> relativeTimeFormat = JSHandle<JSRelativeTimeFormat>::Cast(thisValue); 1764514f5e3Sopenharmony_ci JSRelativeTimeFormat::ResolvedOptions(thread, relativeTimeFormat, options); 1774514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1784514f5e3Sopenharmony_ci // 5. Return options. 1794514f5e3Sopenharmony_ci return options.GetTaggedValue(); 1804514f5e3Sopenharmony_ci} 1814514f5e3Sopenharmony_ci} // namespace panda::ecmascript::builtins