/* * 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_relative_time_format.h" namespace panda::ecmascript::builtins { JSTaggedValue BuiltinsRelativeTimeFormat::RelativeTimeFormatConstructor(EcmaRuntimeCallInfo *argv) { JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, RelativeTimeFormat, Constructor); [[maybe_unused]] EcmaHandleScope scope(thread); EcmaVM *ecmaVm = thread->GetEcmaVM(); JSHandle env = ecmaVm->GetGlobalEnv(); ObjectFactory *factory = ecmaVm->GetFactory(); // 1. If NewTarget is undefined, throw a TypeError exception. JSHandle newTarget = GetNewTarget(argv); if (newTarget->IsUndefined()) { THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception()); } // 2. Let relativeTimeFormat be ? OrdinaryCreateFromConstructor // (NewTarget, "%RelativeTimeFormatPrototype%", « [[InitializedRelativeTimeFormat]], // [[Locale]], [[DataLocale]], [[Style]], [[Numeric]], [[NumberFormat]], [[NumberingSystem]], [[PluralRules]] »). JSHandle constructor = GetConstructor(argv); JSHandle newObject = factory->NewJSObjectByConstructor(JSHandle(constructor), newTarget); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); JSHandle relativeTimeFormat = JSHandle::Cast(newObject); // 3. Perform ? InitializeRelativeTimeFormat(relativeTimeFormat, locales, options). JSHandle locales = GetCallArg(argv, 0); JSHandle options = GetCallArg(argv, 1); JSRelativeTimeFormat::InitializeRelativeTimeFormat(thread, relativeTimeFormat, locales, options); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 4. Intl.RelativeTimeFormat.prototype[ @@toStringTag ] // This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }. JSHandle thisValue = GetThis(argv); bool isInstanceOf = JSObject::InstanceOf(thread, thisValue, env->GetRelativeTimeFormatFunction()); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); if (newTarget->IsUndefined() && thisValue->IsJSObject() && isInstanceOf) { PropertyDescriptor descriptor(thread, JSHandle::Cast(relativeTimeFormat), false, false, true); JSHandle key(thread, JSHandle::Cast(env->GetIntlFunction())->GetFallbackSymbol()); JSTaggedValue::DefinePropertyOrThrow(thread, thisValue, key, descriptor); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); return thisValue.GetTaggedValue(); } return relativeTimeFormat.GetTaggedValue(); } JSTaggedValue BuiltinsRelativeTimeFormat::SupportedLocalesOf(EcmaRuntimeCallInfo *argv) { JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, RelativeTimeFormat, SupportedLocalesOf); [[maybe_unused]] EcmaHandleScope scope(thread); // 1. Let availableLocales be %RelativeTimeFormat%.[[AvailableLocales]]. std::vector availableStringLocales = intl::LocaleHelper::GetAvailableLocales(thread, "calendar", nullptr); JSHandle availableLocales = JSLocale::ConstructLocaleList(thread, availableStringLocales); // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales). JSHandle locales = GetCallArg(argv, 0); JSHandle requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options). JSHandle options = GetCallArg(argv, 1); JSHandle result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); return result.GetTaggedValue(); } JSTaggedValue BuiltinsRelativeTimeFormat::Format(EcmaRuntimeCallInfo *argv) { JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, RelativeTimeFormat, Format); [[maybe_unused]] EcmaHandleScope scope(thread); // 1. Let relativeTimeFormat be the this value. JSHandle thisValue = GetThis(argv); // 2. Perform ? RequireInternalSlot(relativeTimeFormat, [[InitializedRelativeTimeFormat]]). if (!thisValue->IsJSRelativeTimeFormat()) { THROW_TYPE_ERROR_AND_RETURN(thread, "this is not rtf object", JSTaggedValue::Exception()); } // 3. Let value be ? ToNumber(value). double x = 0.0; JSHandle value = GetCallArg(argv, 0); JSTaggedNumber temp = JSTaggedValue::ToNumber(thread, value); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); x = temp.GetNumber(); // 4. Let unit be ? ToString(unit). JSHandle unitValue = GetCallArg(argv, 1); JSHandle unit = JSTaggedValue::ToString(thread, unitValue); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 5. Return ? FormatRelativeTime(relativeTimeFormat, value, unit). JSHandle relativeTimeFormat = JSHandle::Cast(thisValue); JSHandle result = JSRelativeTimeFormat::Format(thread, x, unit, relativeTimeFormat); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); return result.GetTaggedValue(); } JSTaggedValue BuiltinsRelativeTimeFormat::FormatToParts(EcmaRuntimeCallInfo *argv) { JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, RelativeTimeFormat, FormatToParts); [[maybe_unused]] EcmaHandleScope scope(thread); // 1. Let relativeTimeFormat be the this value. JSHandle thisValue = GetThis(argv); // 2. Perform ? RequireInternalSlot(relativeTimeFormat, [[InitializedRelativeTimeFormat]]). if (!thisValue->IsJSRelativeTimeFormat()) { THROW_TYPE_ERROR_AND_RETURN(thread, "this is not rtf object", JSTaggedValue::Exception()); } // 3. Let value be ? ToNumber(value). double x = 0.0; JSHandle value = GetCallArg(argv, 0); JSTaggedNumber temp = JSTaggedValue::ToNumber(thread, value); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); x = temp.GetNumber(); // 4. Let unit be ? ToString(unit). JSHandle unitValue = GetCallArg(argv, 1); JSHandle unit = JSTaggedValue::ToString(thread, unitValue); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 5. Return ? FormatRelativeTime(relativeTimeFormat, value, unit). JSHandle relativeTimeFormat = JSHandle::Cast(thisValue); JSHandle result = JSRelativeTimeFormat::FormatToParts(thread, x, unit, relativeTimeFormat); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); return result.GetTaggedValue(); } JSTaggedValue BuiltinsRelativeTimeFormat::ResolvedOptions(EcmaRuntimeCallInfo *argv) { JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, RelativeTimeFormat, ResolvedOptions); [[maybe_unused]] EcmaHandleScope scope(thread); // 1. Let relativeTimeFormat be the this value. JSHandle thisValue = GetThis(argv); // 2. Perform ? RequireInternalSlot(relativeTimeFormat, [[InitializedRelativeTimeFormat]]). if (!thisValue->IsJSRelativeTimeFormat()) { THROW_TYPE_ERROR_AND_RETURN(thread, "this is not rtf object", JSTaggedValue::Exception()); } // 3. Let options be ! ObjectCreate(%ObjectPrototype%). auto ecmaVm = thread->GetEcmaVM(); JSHandle env = ecmaVm->GetGlobalEnv(); ObjectFactory *factory = ecmaVm->GetFactory(); JSHandle ctor(env->GetObjectFunction()); JSHandle options(factory->NewJSObjectByConstructor(ctor)); // 4. perform resolvedOptions JSHandle relativeTimeFormat = JSHandle::Cast(thisValue); JSRelativeTimeFormat::ResolvedOptions(thread, relativeTimeFormat, options); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 5. Return options. return options.GetTaggedValue(); } } // namespace panda::ecmascript::builtins