1 /*
2 * Copyright (c) 2021 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_locale.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_locale.h"
22 #include "ecmascript/object_factory.h"
23
24 namespace panda::ecmascript::builtins {
25 // 10.1.3 Intl.Locale( tag [, options] )
LocaleConstructor(EcmaRuntimeCallInfo * argv)26 JSTaggedValue BuiltinsLocale::LocaleConstructor(EcmaRuntimeCallInfo *argv)
27 {
28 JSThread *thread = argv->GetThread();
29 BUILTINS_API_TRACE(thread, Locale, Constructor);
30 [[maybe_unused]] EcmaHandleScope scope(thread);
31 EcmaVM *ecmaVm = thread->GetEcmaVM();
32 ObjectFactory *factory = ecmaVm->GetFactory();
33
34 // 1. If NewTarget is undefined, throw a TypeError exception.
35 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
36 if (newTarget->IsUndefined()) {
37 THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
38 }
39
40 // 6. Let locale be ? OrdinaryCreateFromConstructor(NewTarget, %LocalePrototype%, internalSlotsList).
41 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
42 JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
43 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
44 JSHandle<JSLocale> locale =JSHandle<JSLocale>::Cast(newObject);
45
46 // 7. If Type(tag) is not String or Object, throw a TypeError exception.
47 JSHandle<JSTaggedValue> tag = GetCallArg(argv, 0);
48 if (!tag->IsString() && !tag->IsJSObject()) {
49 THROW_TYPE_ERROR_AND_RETURN(thread, "tag is not String or Object", JSTaggedValue::Exception());
50 }
51
52 // 8. If Type(tag) is Object and tag has an [[InitializedLocale]] internal slot, then
53 // a.Let tag be tag.[[Locale]].
54 // 9. Else,
55 // a.Let tag be ? ToString(tag).
56 JSHandle<EcmaString> localeString = factory->GetEmptyString();
57 if (!tag->IsJSLocale()) {
58 localeString = JSTaggedValue::ToString(thread, tag);
59 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
60 } else {
61 icu::Locale *icuLocale = (JSHandle<JSLocale>::Cast(tag))->GetIcuLocale();
62 localeString = intl::LocaleHelper::ToLanguageTag(thread, *icuLocale);
63 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
64 }
65 // 10. If options is undefined, then
66 // a.Let options be ! ObjectCreate(null).
67 // 11. Else
68 // a.Let options be ? ToObject(options).
69 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
70 JSHandle<JSObject> optionsObj;
71 if (options->IsUndefined()) {
72 optionsObj = factory->CreateNullJSObject();
73 } else {
74 optionsObj = JSTaggedValue::ToObject(thread, options);
75 }
76 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
77
78 JSHandle<JSLocale> result = JSLocale::InitializeLocale(thread, locale, localeString, optionsObj);
79 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
80 return result.GetTaggedValue();
81 }
82
Maximize(EcmaRuntimeCallInfo * argv)83 JSTaggedValue BuiltinsLocale::Maximize(EcmaRuntimeCallInfo *argv)
84 {
85 JSThread *thread = argv->GetThread();
86 BUILTINS_API_TRACE(thread, Locale, Maximize);
87 [[maybe_unused]] EcmaHandleScope scope(thread);
88 // 1. Let loc be the this value.
89 JSHandle<JSTaggedValue> loc = GetThis(argv);
90
91 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
92 if (!loc->IsJSLocale()) {
93 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
94 }
95 // 3. Let maximal be the result of the Add Likely Subtags algorithm applied to loc.[[Locale]]. If an error is
96 // signaled, set maximal to loc.[[Locale]].
97 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
98 icu::Locale source(*(locale->GetIcuLocale()));
99 UErrorCode status = U_ZERO_ERROR;
100 source.addLikelySubtags(status);
101 ASSERT(U_SUCCESS(status));
102 ASSERT(!source.isBogus());
103
104 // 4. Return ! Construct(%Locale%, maximal).
105 EcmaVM *ecmaVm = thread->GetEcmaVM();
106 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
107 ObjectFactory *factory = ecmaVm->GetFactory();
108
109 JSHandle<JSFunction> ctor(env->GetLocaleFunction());
110 JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(ctor);
111 factory->NewJSIntlIcuData(JSHandle<JSLocale>::Cast(obj), source, JSLocale::FreeIcuLocale);
112 return obj.GetTaggedValue();
113 }
114
Minimize(EcmaRuntimeCallInfo * argv)115 JSTaggedValue BuiltinsLocale::Minimize(EcmaRuntimeCallInfo *argv)
116 {
117 JSThread *thread = argv->GetThread();
118 BUILTINS_API_TRACE(thread, Locale, Minimize);
119 [[maybe_unused]] EcmaHandleScope scope(thread);
120 // 1. Let loc be the this value.
121 JSHandle<JSTaggedValue> loc = GetThis(argv);
122
123 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
124 if (!loc->IsJSLocale()) {
125 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
126 }
127
128 // 3. Let minimal be the result of the Remove Likely Subtags algorithm applied to loc.[[Locale]].
129 // If an error is signaled, set minimal to loc.[[Locale]].
130 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
131 icu::Locale source(*(locale->GetIcuLocale()));
132 UErrorCode status = U_ZERO_ERROR;
133 source.minimizeSubtags(status);
134 ASSERT(U_SUCCESS(status));
135 ASSERT(!source.isBogus());
136
137 [[maybe_unused]] auto res = source.toLanguageTag<CString>(status);
138
139 // 4. Return ! Construct(%Locale%, minimal).
140 EcmaVM *ecmaVm = thread->GetEcmaVM();
141 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
142 ObjectFactory *factory = ecmaVm->GetFactory();
143
144 JSHandle<JSFunction> ctor(env->GetLocaleFunction());
145 JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(ctor);
146 factory->NewJSIntlIcuData(JSHandle<JSLocale>::Cast(obj), source, JSLocale::FreeIcuLocale);
147 return obj.GetTaggedValue();
148 }
149
ToString(EcmaRuntimeCallInfo * argv)150 JSTaggedValue BuiltinsLocale::ToString(EcmaRuntimeCallInfo *argv)
151 {
152 JSThread *thread = argv->GetThread();
153 BUILTINS_API_TRACE(thread, Locale, ToString);
154 [[maybe_unused]] EcmaHandleScope scope(thread);
155 // 1. Let loc be the this value.
156 JSHandle<JSTaggedValue> loc = GetThis(argv);
157 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
158 if (!loc->IsJSLocale()) {
159 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
160 }
161 // 3. Return loc.[[Locale]].
162 JSHandle<EcmaString> result = JSLocale::ToString(thread, JSHandle<JSLocale>::Cast(loc));
163 return result.GetTaggedValue();
164 }
165
GetBaseName(EcmaRuntimeCallInfo * argv)166 JSTaggedValue BuiltinsLocale::GetBaseName(EcmaRuntimeCallInfo *argv)
167 {
168 JSThread *thread = argv->GetThread();
169 BUILTINS_API_TRACE(thread, Locale, GetBaseName);
170 [[maybe_unused]] EcmaHandleScope scope(thread);
171 // 1. Let loc be the this value.
172 JSHandle<JSTaggedValue> loc = GetThis(argv);
173 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
174 if (!loc->IsJSLocale()) {
175 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
176 }
177 // 3. Let locale be loc.[[Locale]].
178 // 4. Return the substring of locale corresponding to the unicode_language_id production.
179 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
180 icu::Locale icuLocale = icu::Locale::createFromName(locale->GetIcuLocale()->getBaseName());
181 JSHandle<EcmaString> baseName = intl::LocaleHelper::ToLanguageTag(thread, icuLocale);
182 return baseName.GetTaggedValue();
183 }
184
GetCalendar(EcmaRuntimeCallInfo * argv)185 JSTaggedValue BuiltinsLocale::GetCalendar(EcmaRuntimeCallInfo *argv)
186 {
187 JSThread *thread = argv->GetThread();
188 BUILTINS_API_TRACE(thread, Locale, GetCalendar);
189 [[maybe_unused]] EcmaHandleScope scope(thread);
190 // 1. Let loc be the this value.
191 JSHandle<JSTaggedValue> loc = GetThis(argv);
192 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
193 if (!loc->IsJSLocale()) {
194 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
195 }
196 // 3. Return loc.[[Calendar]].
197 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
198 JSHandle<EcmaString> calendar = JSLocale::NormalizeKeywordValue(thread, locale, "ca");
199 return calendar.GetTaggedValue();
200 }
201
GetCaseFirst(EcmaRuntimeCallInfo * argv)202 JSTaggedValue BuiltinsLocale::GetCaseFirst(EcmaRuntimeCallInfo *argv)
203 {
204 // This property only exists if %Locale%.[[RelevantExtensionKeys]] contains "kf".
205 JSThread *thread = argv->GetThread();
206 BUILTINS_API_TRACE(thread, Locale, GetCaseFirst);
207 [[maybe_unused]] EcmaHandleScope scope(thread);
208 // 1. Let loc be the this value.
209 JSHandle<JSTaggedValue> loc = GetThis(argv);
210 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
211 if (!loc->IsJSLocale()) {
212 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
213 }
214 // 3. Return loc.[[CaseFirst]].
215 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
216 JSHandle<EcmaString> caseFirst = JSLocale::NormalizeKeywordValue(thread, locale, "kf");
217 return caseFirst.GetTaggedValue();
218 }
219
GetCollation(EcmaRuntimeCallInfo * argv)220 JSTaggedValue BuiltinsLocale::GetCollation(EcmaRuntimeCallInfo *argv)
221 {
222 JSThread *thread = argv->GetThread();
223 BUILTINS_API_TRACE(thread, Locale, GetCollation);
224 [[maybe_unused]] EcmaHandleScope scope(thread);
225 // 1. Let loc be the this value.
226 JSHandle<JSTaggedValue> loc = GetThis(argv);
227 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
228 if (!loc->IsJSLocale()) {
229 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
230 }
231 // 3. Return loc.[[Collation]].
232 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
233 JSHandle<EcmaString> collation = JSLocale::NormalizeKeywordValue(thread, locale, "co");
234 return collation.GetTaggedValue();
235 }
236
GetHourCycle(EcmaRuntimeCallInfo * argv)237 JSTaggedValue BuiltinsLocale::GetHourCycle(EcmaRuntimeCallInfo *argv)
238 {
239 JSThread *thread = argv->GetThread();
240 BUILTINS_API_TRACE(thread, Locale, GetHourCycle);
241 [[maybe_unused]] EcmaHandleScope scope(thread);
242 // 1. Let loc be the this value.
243 JSHandle<JSTaggedValue> loc = GetThis(argv);
244 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
245 if (!loc->IsJSLocale()) {
246 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
247 }
248 // 3. Return loc.[[HourCycle]].
249 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
250 JSHandle<EcmaString> hourCycle = JSLocale::NormalizeKeywordValue(thread, locale, "hc");
251 return hourCycle.GetTaggedValue();
252 }
253
GetNumeric(EcmaRuntimeCallInfo * argv)254 JSTaggedValue BuiltinsLocale::GetNumeric(EcmaRuntimeCallInfo *argv)
255 {
256 // This property only exists if %Locale%.[[RelevantExtensionKeys]] contains "kn".
257 JSThread *thread = argv->GetThread();
258 BUILTINS_API_TRACE(thread, Locale, GetNumeric);
259 [[maybe_unused]] EcmaHandleScope scope(thread);
260 // 1. Let loc be the this value.
261 JSHandle<JSTaggedValue> loc = GetThis(argv);
262 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
263 if (!loc->IsJSLocale()) {
264 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
265 }
266 // 3. Return loc.[[Numeric]].
267 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
268 icu::Locale *icuLocale = locale->GetIcuLocale();
269 UErrorCode status = U_ZERO_ERROR;
270 auto numeric = icuLocale->getUnicodeKeywordValue<CString>("kn", status);
271 JSTaggedValue result = (numeric == "true") ? JSTaggedValue::True() : JSTaggedValue::False();
272 return result;
273 }
274
GetNumberingSystem(EcmaRuntimeCallInfo * argv)275 JSTaggedValue BuiltinsLocale::GetNumberingSystem(EcmaRuntimeCallInfo *argv)
276 {
277 JSThread *thread = argv->GetThread();
278 BUILTINS_API_TRACE(thread, Locale, GetNumberingSystem);
279 [[maybe_unused]] EcmaHandleScope scope(thread);
280 // 1. Let loc be the this value.
281 JSHandle<JSTaggedValue> loc = GetThis(argv);
282 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
283 if (!loc->IsJSLocale()) {
284 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
285 }
286 // 3. Return loc.[[NumberingSystem]].
287 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
288 JSHandle<EcmaString> numberingSystem = JSLocale::NormalizeKeywordValue(thread, locale, "nu");
289 return numberingSystem.GetTaggedValue();
290 }
291
GetLanguage(EcmaRuntimeCallInfo * argv)292 JSTaggedValue BuiltinsLocale::GetLanguage(EcmaRuntimeCallInfo *argv)
293 {
294 JSThread *thread = argv->GetThread();
295 BUILTINS_API_TRACE(thread, Locale, GetLanguage);
296 [[maybe_unused]] EcmaHandleScope scope(thread);
297 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
298 // 1. Let loc be the this value.
299 JSHandle<JSTaggedValue> loc = GetThis(argv);
300 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
301 if (!loc->IsJSLocale()) {
302 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
303 }
304 // 3. Let locale be loc.[[Locale]].
305 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
306 // 4. Assert: locale matches the unicode_locale_id production.
307 // 5. Return the substring of locale corresponding to the unicode_language_subtag production of the
308 // unicode_language_id.
309 JSHandle<EcmaString> result = JSHandle<EcmaString>::Cast(thread->GlobalConstants()->GetHandledUndefinedString());
310 CString language = locale->GetIcuLocale()->getLanguage();
311 if (language.empty()) {
312 return result.GetTaggedValue();
313 }
314 result = factory->NewFromUtf8(language);
315 return result.GetTaggedValue();
316 }
317
GetScript(EcmaRuntimeCallInfo * argv)318 JSTaggedValue BuiltinsLocale::GetScript(EcmaRuntimeCallInfo *argv)
319 {
320 JSThread *thread = argv->GetThread();
321 BUILTINS_API_TRACE(thread, Locale, GetScript);
322 [[maybe_unused]] EcmaHandleScope scope(thread);
323 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
324 // 1. Let loc be the this value.
325 JSHandle<JSTaggedValue> loc = GetThis(argv);
326 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
327 if (!loc->IsJSLocale()) {
328 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
329 }
330 // 3. Let locale be loc.[[Locale]].
331 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
332
333 // 4. Assert: locale matches the unicode_locale_id production.
334 // 5. If the unicode_language_id production of locale does not contain the ["-" unicode_script_subtag] sequence,
335 // return undefined.
336 // 6. Return the substring of locale corresponding to the unicode_script_subtag production of the
337 // unicode_language_id.
338 JSHandle<EcmaString> result(thread, JSTaggedValue::Undefined());
339 CString script = locale->GetIcuLocale()->getScript();
340 if (script.empty()) {
341 return result.GetTaggedValue();
342 }
343 result = factory->NewFromUtf8(script);
344 return result.GetTaggedValue();
345 }
346
GetRegion(EcmaRuntimeCallInfo * argv)347 JSTaggedValue BuiltinsLocale::GetRegion(EcmaRuntimeCallInfo *argv)
348 {
349 JSThread *thread = argv->GetThread();
350 BUILTINS_API_TRACE(thread, Locale, GetRegion);
351 [[maybe_unused]] EcmaHandleScope scope(thread);
352 EcmaVM *ecmaVm = thread->GetEcmaVM();
353 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
354 ObjectFactory *factory = ecmaVm->GetFactory();
355 // 1. Let loc be the this value.
356 JSHandle<JSTaggedValue> loc = GetThis(argv);
357 // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
358 if (!loc->IsJSLocale()) {
359 THROW_TYPE_ERROR_AND_RETURN(thread, "not locale", JSTaggedValue::Exception());
360 }
361 // 3. Let locale be loc.[[Locale]].
362 JSHandle<JSLocale> locale = JSHandle<JSLocale>::Cast(loc);
363 // 4. Assert: locale matches the unicode_locale_id production.
364 // 5. If the unicode_language_id production of locale does not contain the ["-" unicode_region_subtag] sequence,
365 // return undefined.
366 // 6. Return the substring of locale corresponding to the unicode_region_subtag production of the
367 // unicode_language_id.
368 CString region = locale->GetIcuLocale()->getCountry();
369 if (region.empty()) {
370 return globalConst->GetUndefined();
371 }
372 return factory->NewFromUtf8(region).GetTaggedValue();
373 }
374 } // namespace panda::ecmascript::builtins
375