1 /*
2 * Copyright (c) 2021-2024 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_date_time_format.h"
17
18 #include "ecmascript/intl/locale_helper.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_date.h"
21 #include "ecmascript/js_date_time_format.h"
22 #include "ecmascript/js_function.h"
23 #include "ecmascript/js_intl.h"
24
25 namespace panda::ecmascript::builtins {
26 // 13.2.1 Intl.DateTimeFormat ( [ locales [ , options ] ] )
DateTimeFormatConstructor(EcmaRuntimeCallInfo * argv)27 JSTaggedValue BuiltinsDateTimeFormat::DateTimeFormatConstructor(EcmaRuntimeCallInfo *argv)
28 {
29 JSThread *thread = argv->GetThread();
30 BUILTINS_API_TRACE(thread, DateTimeFormat, Constructor);
31 [[maybe_unused]] EcmaHandleScope scope(thread);
32 EcmaVM *ecmaVm = thread->GetEcmaVM();
33 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
34 ObjectFactory *factory = ecmaVm->GetFactory();
35
36 // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
37 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
38 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
39 if (newTarget->IsUndefined()) {
40 newTarget = constructor;
41 }
42
43 // 2. Let dateTimeFormat be ? OrdinaryCreateFromConstructor(newTarget, "%DateTimeFormatPrototype%", «
44 // [[InitializedDateTimeFormat]], [[Locale]], [[Calendar]], [[NumberingSystem]], [[TimeZone]], [[Weekday]],
45 // [[Era]], [[Year]], [[Month]], [[Day]], [[Hour]], [[Minute]], [[Second]], [[TimeZoneName]], [[HourCycle]],
46 // [[Pattern]], [[BoundFormat]] »).
47 JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
48 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
49 JSHandle<JSDateTimeFormat> dateTimeFormat = JSHandle<JSDateTimeFormat>::Cast(newObject);
50
51 // 3. Perform ? InitializeDateTimeFormat(dateTimeFormat, locales, options).
52 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
53 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
54 JSHandle<JSDateTimeFormat> dtf =
55 JSDateTimeFormat::InitializeDateTimeFormat(thread, dateTimeFormat, locales, options);
56 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
57
58 // 4. Let this be the this value.
59 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
60
61 // 5. If NewTarget is undefined and Type(this) is Object and ? InstanceofOperator(this, %DateTimeFormat%) is true,
62 // then
63 // a. Perform ? DefinePropertyOrThrow(this, %Intl%.[[FallbackSymbol]], PropertyDescriptor{ [[Value]]:
64 // dateTimeFormat, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
65 // b. Return this.
66 if (GetNewTarget(argv)->IsUndefined() && thisValue->IsJSObject()) {
67 bool isInstanceOf = JSFunction::OrdinaryHasInstance(thread, env->GetDateTimeFormatFunction(), thisValue);
68 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
69 if (isInstanceOf) {
70 PropertyDescriptor descriptor(thread, JSHandle<JSTaggedValue>::Cast(dtf), false, false, false);
71 JSHandle<JSTaggedValue> key(thread,
72 JSHandle<JSIntl>::Cast(env->GetIntlFunction())->GetFallbackSymbol(thread));
73 JSTaggedValue::DefinePropertyOrThrow(thread, thisValue, key, descriptor);
74 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
75 return thisValue.GetTaggedValue();
76 }
77 }
78
79 // 6. Return dateTimeFormat.
80 return dtf.GetTaggedValue();
81 }
82
83 // 13.3.2 Intl.DateTimeFormat.supportedLocalesOf ( locales [ , options ] )
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)84 JSTaggedValue BuiltinsDateTimeFormat::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
85 {
86 JSThread *thread = argv->GetThread();
87 BUILTINS_API_TRACE(thread, DateTimeFormat, SupportedLocalesOf);
88 [[maybe_unused]] EcmaHandleScope scope(thread);
89 // 1. Let availableLocales be %DateTimeFormat%.[[AvailableLocales]].
90 JSHandle<TaggedArray> availableLocales = JSDateTimeFormat::GainAvailableLocales(thread);
91
92 // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
93 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
94 JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
95 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
96
97 // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
98 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
99 JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
100 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
101 return result.GetTaggedValue();
102 }
103
104 // 13.4.3 get Intl.DateTimeFormat.prototype.format
Format(EcmaRuntimeCallInfo * argv)105 JSTaggedValue BuiltinsDateTimeFormat::Format(EcmaRuntimeCallInfo *argv)
106 {
107 JSThread *thread = argv->GetThread();
108 BUILTINS_API_TRACE(thread, DateTimeFormat, Format);
109 [[maybe_unused]] EcmaHandleScope scope(thread);
110
111 // 1. Let dtf be this value.
112 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
113
114 // 2. If Type(dtf) is not Object, throw a TypeError exception.
115 if (!thisValue->IsJSObject()) {
116 THROW_TYPE_ERROR_AND_RETURN(thread, "dtf is not object", JSTaggedValue::Exception());
117 }
118
119 // 3. Let dtf be ? UnwrapDateTimeFormat(dtf).
120 JSHandle<JSTaggedValue> dtfValue = JSDateTimeFormat::UnwrapDateTimeFormat(thread, thisValue);
121 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
122 if (dtfValue->IsUndefined()) {
123 THROW_TYPE_ERROR_AND_RETURN(thread, "dtfValue is not object", JSTaggedValue::Exception());
124 }
125
126 // 4. If dtf.[[BoundFormat]] is undefined, then
127 // a. Let F be a new built-in function object as defined in DateTime Format Functions (13.1.5).
128 // b. Set F.[[DateTimeFormat]] to dtf.
129 // c. Set dtf.[[BoundFormat]] to F.
130 // 5. Return dtf.[[BoundFormat]].
131 JSHandle<JSDateTimeFormat> dtf = JSHandle<JSDateTimeFormat>::Cast(dtfValue);
132 JSHandle<JSTaggedValue> boundFormat(thread, dtf->GetBoundFormat(thread));
133 if (boundFormat->IsUndefined()) {
134 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
135 JSHandle<JSIntlBoundFunction> intlBoundFunc = factory->NewJSIntlBoundFunction(
136 MethodIndex::BUILTINS_DATE_TIME_FORMAT_ANONYMOUS_DATE_TIME_FORMAT);
137 intlBoundFunc->SetDateTimeFormat(thread, dtf);
138 dtf->SetBoundFormat(thread, intlBoundFunc);
139 }
140 return dtf->GetBoundFormat(thread);
141 }
142
143 // 13.1.5 DateTime Format Functions
AnonymousDateTimeFormat(EcmaRuntimeCallInfo * argv)144 JSTaggedValue BuiltinsDateTimeFormat::AnonymousDateTimeFormat(EcmaRuntimeCallInfo *argv)
145 {
146 // A DateTime format function is an anonymous built-in function that has a [[DateTimeFormat]] internal slot.
147 // When a DateTime format function F is called with optional argument date, the following steps are taken:
148 JSThread *thread = argv->GetThread();
149 BUILTINS_API_TRACE(thread, DateTimeFormat, AnonymousDateTimeFormat);
150 [[maybe_unused]] EcmaHandleScope scope(thread);
151 JSHandle<JSIntlBoundFunction> intlBoundFunc = JSHandle<JSIntlBoundFunction>::Cast(GetConstructor(argv));
152
153 // 1. Let dtf be F.[[DateTimeFormat]].
154 JSHandle<JSTaggedValue> dtf(thread, intlBoundFunc->GetDateTimeFormat(thread));
155
156 // 2. Assert: Type(dtf) is Object and dtf has an [[InitializedDateTimeFormat]] internal slot.
157 ASSERT_PRINT(dtf->IsJSObject() && dtf->IsJSDateTimeFormat(), "dtf is not object or JSDateTimeFormat");
158
159 // 3. If date is not provided or is undefined, then
160 // a. Let x be Call(%Date_now%, undefined).
161 // 4. Else,
162 // a. Let x be ? ToNumber(date).
163 JSHandle<JSTaggedValue> date = GetCallArg(argv, 0);
164 double x = 0.0;
165 if (date->IsUndefined()) {
166 x = JSDate::Now().GetNumber();
167 } else {
168 JSTaggedNumber xNumber = JSTaggedValue::ToNumber(thread, date);
169 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
170 x = xNumber.GetNumber();
171 }
172
173 // 5. Return ? FormatDateTime(dtf, x).
174 JSHandle<EcmaString> result = JSDateTimeFormat::FormatDateTime(thread, JSHandle<JSDateTimeFormat>::Cast(dtf), x);
175 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
176 return result.GetTaggedValue();
177 }
178
179 // 13.4.4 Intl.DateTimeFormat.prototype.formatToParts ( date )
FormatToParts(EcmaRuntimeCallInfo * argv)180 JSTaggedValue BuiltinsDateTimeFormat::FormatToParts(EcmaRuntimeCallInfo *argv)
181 {
182 JSThread *thread = argv->GetThread();
183 BUILTINS_API_TRACE(thread, DateTimeFormat, FormatToParts);
184 [[maybe_unused]] EcmaHandleScope scope(thread);
185 // 1. Let dtf be this value.
186 JSHandle<JSTaggedValue> dtf = GetThis(argv);
187 // 2. Perform ? RequireInternalSlot(dtf, [[InitializedDateTimeFormat]]).
188 if (!dtf->IsJSDateTimeFormat()) {
189 THROW_TYPE_ERROR_AND_RETURN(thread, "is not JSDateTimeFormat", JSTaggedValue::Exception());
190 }
191
192 // 3. If date is undefined, then
193 // a. Let x be Call(%Date_now%, undefined).
194 // 4. Else,
195 // a. Let x be ? ToNumber(date).
196 JSHandle<JSTaggedValue> date = GetCallArg(argv, 0);
197 double x = 0.0;
198 if (date->IsUndefined()) {
199 x = JSDate::Now().GetNumber();
200 } else {
201 JSTaggedNumber xNumber = JSTaggedValue::ToNumber(thread, date);
202 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
203 x = xNumber.GetNumber();
204 }
205
206 double xValue = JSDate::TimeClip(x);
207 if (std::isnan(xValue)) {
208 THROW_RANGE_ERROR_AND_RETURN(thread, "Invalid time value", JSTaggedValue::Exception());
209 }
210
211 // 5. Return ? FormatDateTimeToParts(dtf, x).
212 JSHandle<JSArray> result =
213 JSDateTimeFormat::FormatDateTimeToParts(thread, JSHandle<JSDateTimeFormat>::Cast(dtf), xValue);
214 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
215 return result.GetTaggedValue();
216 }
217
218 // 13.4.5 Intl.DateTimeFormat.prototype.resolvedOptions ()
ResolvedOptions(EcmaRuntimeCallInfo * argv)219 JSTaggedValue BuiltinsDateTimeFormat::ResolvedOptions(EcmaRuntimeCallInfo *argv)
220 {
221 JSThread *thread = argv->GetThread();
222 BUILTINS_API_TRACE(thread, DateTimeFormat, ResolvedOptions);
223 [[maybe_unused]] EcmaHandleScope scope(thread);
224 // 1. Let dtf be this value.
225 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
226 // 2. If Type(dtf) is not Object, throw a TypeError exception.
227 if (!thisValue->IsJSObject()) {
228 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not object", JSTaggedValue::Exception());
229 }
230 // 3. Let dtf be ? UnwrapDateTimeFormat(dtf).
231 thisValue = JSDateTimeFormat::UnwrapDateTimeFormat(thread, thisValue);
232 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
233
234 // Perform ? RequireInternalSlot(dtf, [[InitializedDateTimeFormat]]).
235 if (!thisValue->IsJSDateTimeFormat()) {
236 THROW_TYPE_ERROR_AND_RETURN(thread, "dtf is not JSDateTimeFormat", JSTaggedValue::Exception());
237 }
238
239 auto ecmaVm = thread->GetEcmaVM();
240 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
241 ObjectFactory *factory = ecmaVm->GetFactory();
242 JSHandle<JSFunction> ctor(env->GetObjectFunction());
243 JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
244 JSDateTimeFormat::ResolvedOptions(thread, JSHandle<JSDateTimeFormat>::Cast(thisValue), options);
245 // 6. Return options.
246 return options.GetTaggedValue();
247 }
248
249 // Intl.DateTimeFormat.prototype.formatRange
FormatRange(EcmaRuntimeCallInfo * argv)250 JSTaggedValue BuiltinsDateTimeFormat::FormatRange(EcmaRuntimeCallInfo *argv)
251 {
252 JSThread *thread = argv->GetThread();
253 BUILTINS_API_TRACE(thread, DateTimeFormat, FormatRange);
254 [[maybe_unused]] EcmaHandleScope scope(thread);
255
256 // 1. Let dtf be this value.
257 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
258 // 2. If Type(dtf) is not Object, throw a TypeError exception.
259 if (!thisValue->IsJSObject()) {
260 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not object", JSTaggedValue::Exception());
261 }
262
263 // 3. If dtf does not have an [[InitializedDateTimeFormat]] internal slot, throw a TypeError exception.
264 if (!thisValue->IsJSDateTimeFormat()) {
265 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSDateTimeFormat", JSTaggedValue::Exception());
266 }
267
268 // 4. If startDate is undefined or endDate is undefined, throw a TypeError exception.
269 JSHandle<JSTaggedValue> startDate = GetCallArg(argv, 0);
270 JSHandle<JSTaggedValue> endDate = GetCallArg(argv, 1);
271 if (startDate->IsUndefined() || endDate->IsUndefined()) {
272 THROW_TYPE_ERROR_AND_RETURN(thread, "startDate or endDate is undefined", JSTaggedValue::Exception());
273 }
274
275 // 5. Let x be ? ToNumber(startDate).
276 JSTaggedNumber valueX = JSTaggedValue::ToNumber(thread, startDate);
277 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
278 double x = valueX.GetNumber();
279
280 // 6. Let y be ? ToNumber(endDate).
281 JSTaggedNumber valueY = JSTaggedValue::ToNumber(thread, endDate);
282 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
283 double y = valueY.GetNumber();
284
285 // 8. Return ? FormatDateTimeRange(dtf, x, y)
286 JSHandle<JSDateTimeFormat> dtf = JSHandle<JSDateTimeFormat>::Cast(thisValue);
287 JSHandle<EcmaString> result = JSDateTimeFormat::NormDateTimeRange(thread, dtf, x, y);
288 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
289 return result.GetTaggedValue();
290 }
291
292 // Intl.DateTimeFormat.prototype.formatRangeToParts
FormatRangeToParts(EcmaRuntimeCallInfo * argv)293 JSTaggedValue BuiltinsDateTimeFormat::FormatRangeToParts(EcmaRuntimeCallInfo *argv)
294 {
295 JSThread *thread = argv->GetThread();
296 BUILTINS_API_TRACE(thread, DateTimeFormat, FormatRangeToParts);
297 [[maybe_unused]] EcmaHandleScope scope(thread);
298 // 1. Let dtf be this value.
299 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
300 // 2. If Type(dtf) is not Object, throw a TypeError exception.
301 if (!thisValue->IsJSObject()) {
302 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not object", JSTaggedValue::Exception());
303 }
304
305 // 3. If dtf does not have an [[InitializedDateTimeFormat]] internal slot,
306 // throw a TypeError exception.
307 if (!thisValue->IsJSDateTimeFormat()) {
308 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSDateTimeFormat", JSTaggedValue::Exception());
309 }
310
311 // 4. If startDate is undefined or endDate is undefined, throw a TypeError exception.
312 JSHandle<JSTaggedValue> startDate = GetCallArg(argv, 0);
313 JSHandle<JSTaggedValue> endDate = GetCallArg(argv, 1);
314 if (startDate->IsUndefined() || endDate->IsUndefined()) {
315 THROW_TYPE_ERROR_AND_RETURN(thread, "startDate or endDate is undefined", JSTaggedValue::Exception());
316 }
317
318 // 5. Let x be ? ToNumber(startDate).
319 JSTaggedNumber valueX = JSTaggedValue::ToNumber(thread, startDate);
320 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
321 double x = valueX.GetNumber();
322
323 // 6. Let y be ? ToNumber(endDate).
324 JSTaggedNumber valueY = JSTaggedValue::ToNumber(thread, endDate);
325 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
326 double y = valueY.GetNumber();
327
328 // 8. Return ? FormatDateTimeRangeToParts(dtf, x, y)
329 JSHandle<JSDateTimeFormat> dtf = JSHandle<JSDateTimeFormat>::Cast(thisValue);
330 JSHandle<JSArray> result = JSDateTimeFormat::NormDateTimeRangeToParts(thread, dtf, x, y);
331 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
332 return result.GetTaggedValue();
333 }
334 } // namespace panda::ecmascript::builtins