1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "builtins_displaynames.h"
17
18 #include "ecmascript/intl/locale_helper.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_displaynames.h"
22 #include "ecmascript/js_intl.h"
23 #include "ecmascript/js_locale.h"
24 #include "ecmascript/js_object.h"
25 #include "ecmascript/object_factory.h"
26
27 namespace panda::ecmascript::builtins {
28 // 12.2.1 Intl.DisplayNames ( [ locales [ , options ] ] )
DisplayNamesConstructor(EcmaRuntimeCallInfo * argv)29 JSTaggedValue BuiltinsDisplayNames::DisplayNamesConstructor(EcmaRuntimeCallInfo *argv)
30 {
31 JSThread *thread = argv->GetThread();
32 BUILTINS_API_TRACE(thread, DisplayNames, Constructor);
33 [[maybe_unused]] EcmaHandleScope scope(thread);
34 EcmaVM *ecmaVm = thread->GetEcmaVM();
35 ObjectFactory *factory = ecmaVm->GetFactory();
36
37 // 1. If NewTarget is undefined, throw a TypeError exception.
38 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
39 if (newTarget->IsUndefined()) {
40 THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
41 }
42
43 // 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%DisplayNames.prototype%",
44 // « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[Fields]] »).
45 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
46 JSHandle<JSDisplayNames> displayNames =
47 JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget));
48 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
49
50 // 3. Perform ? InitializeDisplayNames(displayNames, locales, options).
51 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
52 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
53 JSDisplayNames::InitializeDisplayNames(thread, displayNames, locales, options);
54 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
55 return displayNames.GetTaggedValue();
56 }
57
58 // 12.3.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] )
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)59 JSTaggedValue BuiltinsDisplayNames::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
60 {
61 JSThread *thread = argv->GetThread();
62 BUILTINS_API_TRACE(thread, DisplayNames, SupportedLocalesOf);
63 [[maybe_unused]] EcmaHandleScope scope(thread);
64
65 // 1. Let availableLocales be %DisplayNames%.[[AvailableLocales]].
66 JSHandle<TaggedArray> availableLocales = JSDisplayNames::GetAvailableLocales(thread);
67
68 // 2. Let requestedLocales be ? CanonicaliezLocaleList(locales).
69 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
70 JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
71 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
72
73 // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
74 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
75 JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
76 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
77 return result.GetTaggedValue();
78 }
79
80 // 12.4.3 get Intl.DisplayNames.prototype.of
Of(EcmaRuntimeCallInfo * argv)81 JSTaggedValue BuiltinsDisplayNames::Of(EcmaRuntimeCallInfo *argv)
82 {
83 JSThread *thread = argv->GetThread();
84 BUILTINS_API_TRACE(thread, DisplayNames, Of);
85 [[maybe_unused]] EcmaHandleScope scope(thread);
86 // 1. Let displayNames be this value.
87 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
88
89 // 2. Perform ? RequireInternalSlot(displayNames, [[InitializedDisplayNames]]).
90 if (!thisValue->IsJSDisplayNames()) {
91 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not dn object", JSTaggedValue::Exception());
92 }
93
94 // 3. Let code be ? ToString(code).
95 JSHandle<JSTaggedValue> codeValue = GetCallArg(argv, 0);
96 JSHandle<EcmaString> codeTemp = JSTaggedValue::ToString(thread, codeValue);
97 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
98
99 // 4. Let code be ? CanonicalCodeForDisplayNames(displayNames.[[Type]], code).
100 // 5. Let fields be displayNames.[[Fields]].
101 // 6. If fields has a field [[<code>]], return fields.[[<code>]].
102 JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(thisValue);
103 TypednsOption typeOpt = displayNames->GetType();
104 JSHandle<EcmaString> code = JSDisplayNames::CanonicalCodeForDisplayNames(thread, displayNames, typeOpt, codeTemp);
105 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
106 std::string codeString = intl::LocaleHelper::ConvertToStdString(code);
107 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
108 if (codeString.size()) {
109 JSHandle<JSTaggedValue> codeStr = JSHandle<JSTaggedValue>::Cast(code);
110 return codeStr.GetTaggedValue();
111 }
112
113 // 7. If displayNames.[[Fallback]] is "code", return code.
114 FallbackOption fallback = displayNames->GetFallback();
115 if (fallback == FallbackOption::CODE) {
116 return codeValue.GetTaggedValue();
117 }
118 // 8. Return undefined.
119 return JSTaggedValue::Undefined();
120 }
121
122 // 12.4.4 Intl.DisplayNames.prototype.resolvedOptions ()
ResolvedOptions(EcmaRuntimeCallInfo * argv)123 JSTaggedValue BuiltinsDisplayNames::ResolvedOptions(EcmaRuntimeCallInfo *argv)
124 {
125 JSThread *thread = argv->GetThread();
126 BUILTINS_API_TRACE(thread, DisplayNames, ResolvedOptions);
127 [[maybe_unused]] EcmaHandleScope scope(thread);
128
129 // 1. Let DisplayNames be the this value.
130 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
131
132 // 2. Perform ? RequireInternalSlot(DisplayNames, [[InitializedDisplayNames]]).
133 if (!thisValue->IsJSDisplayNames()) {
134 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not dn object", JSTaggedValue::Exception());
135 }
136
137 // 3. Let options be ! OrdinaryObjectCreate(%ObjectPrototype%).
138 auto ecmaVm = thread->GetEcmaVM();
139 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
140 ObjectFactory *factory = ecmaVm->GetFactory();
141 JSHandle<JSFunction> ctor(env->GetObjectFunction());
142 JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
143
144 // 4. For each row of Table 8, except the header row, in table order, do
145 // Let p be the Property value of the current row.
146 // Let v be the value of displayNames's internal slot whose name is the Internal Slot value of the current row.
147 // Assert: v is not undefined.
148 // Perform ! CreateDataPropertyOrThrow(options, p, v).
149 JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(thisValue);
150 JSDisplayNames::ResolvedOptions(thread, displayNames, options);
151 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
152
153 // 5. Return options.
154 return options.GetTaggedValue();
155 }
156 } // namespace panda::ecmascript::builtins