1 /*
2 * Copyright (c) 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 "ecmascript/builtins/builtins_list_format.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_list_format.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 {
ListFormatConstructor(EcmaRuntimeCallInfo * argv)28 JSTaggedValue BuiltinsListFormat::ListFormatConstructor(EcmaRuntimeCallInfo *argv)
29 {
30 JSThread *thread = argv->GetThread();
31 BUILTINS_API_TRACE(thread, ListFormat, Constructor);
32 [[maybe_unused]] EcmaHandleScope scope(thread);
33 EcmaVM *ecmaVm = thread->GetEcmaVM();
34 ObjectFactory *factory = ecmaVm->GetFactory();
35
36 // 1. If NewTarget is undefined, throw a TypeError exception.
37 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
38 if (newTarget->IsUndefined()) {
39 THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
40 }
41
42 // 2. Let listFormat be ? OrdinaryCreateFromConstructor
43 // (NewTarget, "%ListFormat.prototype%", « [[InitializedListFormat]], [[Locale]],
44 // [[Type]], [[Style]], [[Templates]] »).
45
46 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
47 JSHandle<JSListFormat> listFormat = JSHandle<JSListFormat>::Cast(
48 factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget));
49 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
50
51 // 3. Perform ? InitializeListFormat(listFormat, locales, options).
52 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
53 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
54 listFormat = JSListFormat::InitializeListFormat(thread, listFormat, locales, options);
55 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
56 return listFormat.GetTaggedValue();
57 }
58
59 // 13.3.2 Intl.ListFormat.supportedLocalesOf ( locales [ , options ] )
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)60 JSTaggedValue BuiltinsListFormat::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
61 {
62 JSThread *thread = argv->GetThread();
63 BUILTINS_API_TRACE(thread, ListFormat, SupportedLocalesOf);
64 [[maybe_unused]] EcmaHandleScope scope(thread);
65
66 // 1. Let availableLocales be %ListFormat%.[[AvailableLocales]].
67 JSHandle<TaggedArray> availableLocales = JSListFormat::GetAvailableLocales(thread);
68
69 // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
70 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
71 JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
72 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
73
74 // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
75 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
76 JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
77 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
78 return result.GetTaggedValue();
79 }
80
81 // 13.4.3 Intl.ListFormat.prototype.format( list )
Format(EcmaRuntimeCallInfo * argv)82 JSTaggedValue BuiltinsListFormat::Format(EcmaRuntimeCallInfo *argv)
83 {
84 JSThread *thread = argv->GetThread();
85 BUILTINS_API_TRACE(thread, ListFormat, Format);
86 [[maybe_unused]] EcmaHandleScope scope(thread);
87
88 // 1. Let lf be the this value.
89 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
90
91 // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
92 if (!thisValue->IsJSListFormat()) {
93 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSListFormat", JSTaggedValue::Exception());
94 }
95
96 // 3. Let stringList be ? StringListFromIterable(list).
97 JSHandle<JSTaggedValue> list = GetCallArg(argv, 0);
98 JSHandle<JSTaggedValue> listArray = JSListFormat::StringListFromIterable(thread, list);
99 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
100
101 // 4. Return FormatList(lf, stringList).
102 JSHandle<JSListFormat> listFormat = JSHandle<JSListFormat>::Cast(thisValue);
103 JSHandle<JSArray> array = JSHandle<JSArray>::Cast(listArray);
104 JSHandle<EcmaString> result = JSListFormat::FormatList(thread, listFormat, array);
105 return result.GetTaggedValue();
106 }
107
108 // 13.4.4 Intl.ListFormat.prototype.formatToParts ( list )
FormatToParts(EcmaRuntimeCallInfo * argv)109 JSTaggedValue BuiltinsListFormat::FormatToParts(EcmaRuntimeCallInfo *argv)
110 {
111 JSThread *thread = argv->GetThread();
112 BUILTINS_API_TRACE(thread, ListFormat, FormatToParts);
113 [[maybe_unused]] EcmaHandleScope scope(thread);
114
115 // 1. Let lf be the this value.
116 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
117
118 // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
119 if (!thisValue->IsJSListFormat()) {
120 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSListFormat", JSTaggedValue::Exception());
121 }
122
123 // 3. Let stringList be ? StringListFromIterable(list).
124 JSHandle<JSTaggedValue> list = GetCallArg(argv, 0);
125 JSHandle<JSTaggedValue> listArray = JSListFormat::StringListFromIterable(thread, list);
126 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
127
128 // 4 Return FormatListToParts(lf, stringList).
129 JSHandle<JSListFormat> listFormat = JSHandle<JSListFormat>::Cast(thisValue);
130 JSHandle<JSArray> array = JSHandle<JSArray>::Cast(listArray);
131
132 JSHandle<JSArray> result = JSListFormat::FormatListToParts(thread, listFormat, array);
133 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
134 return result.GetTaggedValue();
135 }
136
137 // 13.4.5 Intl.ListFormat.prototype.resolvedOptions()
ResolvedOptions(EcmaRuntimeCallInfo * argv)138 JSTaggedValue BuiltinsListFormat::ResolvedOptions(EcmaRuntimeCallInfo *argv)
139 {
140 JSThread *thread = argv->GetThread();
141 BUILTINS_API_TRACE(thread, ListFormat, ResolvedOptions);
142 [[maybe_unused]] EcmaHandleScope scope(thread);
143
144 // 1. Let lf be the this value.
145 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
146
147 // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
148 if (!thisValue->IsJSListFormat()) {
149 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSListFormat", JSTaggedValue::Exception());
150 }
151
152 // 3 .Let options be ! OrdinaryObjectCreate(%Object.prototype%).
153 auto ecmaVm = thread->GetEcmaVM();
154 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
155 ObjectFactory *factory = ecmaVm->GetFactory();
156 JSHandle<JSFunction> ctor(env->GetObjectFunction());
157 JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
158
159 // 4. For each row of Table 9, except the header row, in table order, do
160 // Let p be the Property value of the current row.
161 // Let v be the value of lf's internal slot whose name is the Internal Slot value of the current row.
162 // Assert: v is not undefined.
163 // Perform ! CreateDataPropertyOrThrow(options, p, v).
164 JSHandle<JSListFormat> listFormat = JSHandle<JSListFormat>::Cast(thisValue);
165 JSListFormat::ResolvedOptions(thread, listFormat, options);
166 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
167
168 // 5. Return options.
169 return options.GetTaggedValue();
170 }
171 } // namespace panda::ecmascript::builtins