14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021-2022 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 "builtins_displaynames.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/intl/locale_helper.h" 194514f5e3Sopenharmony_ci#include "ecmascript/js_displaynames.h" 204514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins { 214514f5e3Sopenharmony_ci// 12.2.1 Intl.DisplayNames ( [ locales [ , options ] ] ) 224514f5e3Sopenharmony_ciJSTaggedValue BuiltinsDisplayNames::DisplayNamesConstructor(EcmaRuntimeCallInfo *argv) 234514f5e3Sopenharmony_ci{ 244514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 254514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, DisplayNames, Constructor); 264514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 274514f5e3Sopenharmony_ci EcmaVM *ecmaVm = thread->GetEcmaVM(); 284514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVm->GetFactory(); 294514f5e3Sopenharmony_ci 304514f5e3Sopenharmony_ci // 1. If NewTarget is undefined, throw a TypeError exception. 314514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv); 324514f5e3Sopenharmony_ci if (newTarget->IsUndefined()) { 334514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception()); 344514f5e3Sopenharmony_ci } 354514f5e3Sopenharmony_ci 364514f5e3Sopenharmony_ci // 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%DisplayNames.prototype%", 374514f5e3Sopenharmony_ci // « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[Fields]] »). 384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> constructor = GetConstructor(argv); 394514f5e3Sopenharmony_ci JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget); 404514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 414514f5e3Sopenharmony_ci JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(newObject); 424514f5e3Sopenharmony_ci 434514f5e3Sopenharmony_ci // 3. Perform ? InitializeDisplayNames(displayNames, locales, options). 444514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0); 454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> options = GetCallArg(argv, 1); 464514f5e3Sopenharmony_ci JSDisplayNames::InitializeDisplayNames(thread, displayNames, locales, options); 474514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 484514f5e3Sopenharmony_ci return displayNames.GetTaggedValue(); 494514f5e3Sopenharmony_ci} 504514f5e3Sopenharmony_ci 514514f5e3Sopenharmony_ci// 12.3.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] ) 524514f5e3Sopenharmony_ciJSTaggedValue BuiltinsDisplayNames::SupportedLocalesOf(EcmaRuntimeCallInfo *argv) 534514f5e3Sopenharmony_ci{ 544514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 554514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, DisplayNames, SupportedLocalesOf); 564514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 574514f5e3Sopenharmony_ci 584514f5e3Sopenharmony_ci // 1. Let availableLocales be %DisplayNames%.[[AvailableLocales]]. 594514f5e3Sopenharmony_ci JSHandle<TaggedArray> availableLocales = JSDisplayNames::GetAvailableLocales(thread); 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci // 2. Let requestedLocales be ? CanonicaliezLocaleList(locales). 624514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0); 634514f5e3Sopenharmony_ci JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales); 644514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_ci // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options). 674514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> options = GetCallArg(argv, 1); 684514f5e3Sopenharmony_ci JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options); 694514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 704514f5e3Sopenharmony_ci return result.GetTaggedValue(); 714514f5e3Sopenharmony_ci} 724514f5e3Sopenharmony_ci 734514f5e3Sopenharmony_ci// 12.4.3 get Intl.DisplayNames.prototype.of 744514f5e3Sopenharmony_ciJSTaggedValue BuiltinsDisplayNames::Of(EcmaRuntimeCallInfo *argv) 754514f5e3Sopenharmony_ci{ 764514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 774514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, DisplayNames, Of); 784514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 794514f5e3Sopenharmony_ci // 1. Let displayNames be this value. 804514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue = GetThis(argv); 814514f5e3Sopenharmony_ci 824514f5e3Sopenharmony_ci // 2. Perform ? RequireInternalSlot(displayNames, [[InitializedDisplayNames]]). 834514f5e3Sopenharmony_ci if (!thisValue->IsJSDisplayNames()) { 844514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "this is not dn object", JSTaggedValue::Exception()); 854514f5e3Sopenharmony_ci } 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ci // 3. Let code be ? ToString(code). 884514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> codeValue = GetCallArg(argv, 0); 894514f5e3Sopenharmony_ci JSHandle<EcmaString> codeTemp = JSTaggedValue::ToString(thread, codeValue); 904514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 914514f5e3Sopenharmony_ci 924514f5e3Sopenharmony_ci // 4. Let code be ? CanonicalCodeForDisplayNames(displayNames.[[Type]], code). 934514f5e3Sopenharmony_ci // 5. Let fields be displayNames.[[Fields]]. 944514f5e3Sopenharmony_ci // 6. If fields has a field [[<code>]], return fields.[[<code>]]. 954514f5e3Sopenharmony_ci JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(thisValue); 964514f5e3Sopenharmony_ci TypednsOption typeOpt = displayNames->GetType(); 974514f5e3Sopenharmony_ci JSHandle<EcmaString> code = JSDisplayNames::CanonicalCodeForDisplayNames(thread, displayNames, typeOpt, codeTemp); 984514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 994514f5e3Sopenharmony_ci std::string codeString = intl::LocaleHelper::ConvertToStdString(code); 1004514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1014514f5e3Sopenharmony_ci if (codeString.size()) { 1024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> codeStr = JSHandle<JSTaggedValue>::Cast(code); 1034514f5e3Sopenharmony_ci return codeStr.GetTaggedValue(); 1044514f5e3Sopenharmony_ci } 1054514f5e3Sopenharmony_ci 1064514f5e3Sopenharmony_ci // 7. If displayNames.[[Fallback]] is "code", return code. 1074514f5e3Sopenharmony_ci FallbackOption fallback = displayNames->GetFallback(); 1084514f5e3Sopenharmony_ci if (fallback == FallbackOption::CODE) { 1094514f5e3Sopenharmony_ci return codeValue.GetTaggedValue(); 1104514f5e3Sopenharmony_ci } 1114514f5e3Sopenharmony_ci // 8. Return undefined. 1124514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 1134514f5e3Sopenharmony_ci} 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci// 12.4.4 Intl.DisplayNames.prototype.resolvedOptions () 1164514f5e3Sopenharmony_ciJSTaggedValue BuiltinsDisplayNames::ResolvedOptions(EcmaRuntimeCallInfo *argv) 1174514f5e3Sopenharmony_ci{ 1184514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 1194514f5e3Sopenharmony_ci BUILTINS_API_TRACE(thread, DisplayNames, ResolvedOptions); 1204514f5e3Sopenharmony_ci [[maybe_unused]] EcmaHandleScope scope(thread); 1214514f5e3Sopenharmony_ci 1224514f5e3Sopenharmony_ci // 1. Let DisplayNames be the this value. 1234514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue = GetThis(argv); 1244514f5e3Sopenharmony_ci 1254514f5e3Sopenharmony_ci // 2. Perform ? RequireInternalSlot(DisplayNames, [[InitializedDisplayNames]]). 1264514f5e3Sopenharmony_ci if (!thisValue->IsJSDisplayNames()) { 1274514f5e3Sopenharmony_ci THROW_TYPE_ERROR_AND_RETURN(thread, "this is not dn object", JSTaggedValue::Exception()); 1284514f5e3Sopenharmony_ci } 1294514f5e3Sopenharmony_ci 1304514f5e3Sopenharmony_ci // 3. Let options be ! OrdinaryObjectCreate(%ObjectPrototype%). 1314514f5e3Sopenharmony_ci auto ecmaVm = thread->GetEcmaVM(); 1324514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv(); 1334514f5e3Sopenharmony_ci ObjectFactory *factory = ecmaVm->GetFactory(); 1344514f5e3Sopenharmony_ci JSHandle<JSFunction> ctor(env->GetObjectFunction()); 1354514f5e3Sopenharmony_ci JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor)); 1364514f5e3Sopenharmony_ci 1374514f5e3Sopenharmony_ci // 4. For each row of Table 8, except the header row, in table order, do 1384514f5e3Sopenharmony_ci // Let p be the Property value of the current row. 1394514f5e3Sopenharmony_ci // Let v be the value of displayNames's internal slot whose name is the Internal Slot value of the current row. 1404514f5e3Sopenharmony_ci // Assert: v is not undefined. 1414514f5e3Sopenharmony_ci // Perform ! CreateDataPropertyOrThrow(options, p, v). 1424514f5e3Sopenharmony_ci JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(thisValue); 1434514f5e3Sopenharmony_ci JSDisplayNames::ResolvedOptions(thread, displayNames, options); 1444514f5e3Sopenharmony_ci RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); 1454514f5e3Sopenharmony_ci 1464514f5e3Sopenharmony_ci // 5. Return options. 1474514f5e3Sopenharmony_ci return options.GetTaggedValue(); 1484514f5e3Sopenharmony_ci} 1494514f5e3Sopenharmony_ci} // namespace panda::ecmascript::builtins