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 "builtins_collator.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_collator.h"
22 #include "ecmascript/js_intl.h"
23 #include "ecmascript/mem/barriers-inl.h"
24
25 namespace panda::ecmascript::builtins {
26 constexpr uint32_t FUNCTION_LENGTH_TWO = 2;
27
28 // 11.1.2 Intl.Collator ( [ locales [ , options ] ] )
CollatorConstructor(EcmaRuntimeCallInfo * argv)29 JSTaggedValue BuiltinsCollator::CollatorConstructor(EcmaRuntimeCallInfo *argv)
30 {
31 JSThread *thread = argv->GetThread();
32 BUILTINS_API_TRACE(thread, Collator, CollatorConstructor);
33 [[maybe_unused]] EcmaHandleScope scope(thread);
34 EcmaVM *ecmaVm = thread->GetEcmaVM();
35 ObjectFactory *factory = ecmaVm->GetFactory();
36
37 // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
38 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
39 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
40 if (newTarget->IsUndefined()) {
41 newTarget = constructor;
42 }
43 // 2. Let internalSlotsList be « [[InitializedCollator]], [[Locale]], [[Usage]], [[Sensitivity]],
44 // [[IgnorePunctuation]], [[Collation]], [[BoundCompare]] ».
45 // 3. If %Collator%.[[RelevantExtensionKeys]] contains "kn", then
46 // a. Append [[Numeric]] as the last element of internalSlotsList.
47 // 4. If %Collator%.[[RelevantExtensionKeys]] contains "kf", then
48 // a. Append [[CaseFirst]] as the last element of internalSlotsList.
49
50 // 5. Let collator be ? OrdinaryCreateFromConstructor(newTarget, "%CollatorPrototype%", internalSlotsList).
51 JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
52 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
53 JSHandle<JSCollator> collator = JSHandle<JSCollator>::Cast(newObject);
54
55 // 6. Return ? InitializeCollator(collator, locales, options).
56 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
57 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
58 JSHandle<JSCollator> result = JSCollator::InitializeCollator(thread, collator, locales, options);
59 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
60 return result.GetTaggedValue();
61 }
62
63 // 11.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] )
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)64 JSTaggedValue BuiltinsCollator::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
65 {
66 JSThread *thread = argv->GetThread();
67 BUILTINS_API_TRACE(thread, Collator, SupportedLocalesOf);
68 [[maybe_unused]] EcmaHandleScope scope(thread);
69 // 1. Let availableLocales be %Collator%.[[AvailableLocales]].
70 JSHandle<TaggedArray> availableLocales = JSCollator::GetAvailableLocales(thread);
71
72 // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
73 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
74 JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
75 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
76
77 // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
78 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
79 JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
80 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
81 return result.GetTaggedValue();
82 }
83
84 // 11.3.3 get Intl.Collator.prototype.compare
Compare(EcmaRuntimeCallInfo * argv)85 JSTaggedValue BuiltinsCollator::Compare(EcmaRuntimeCallInfo *argv)
86 {
87 JSThread *thread = argv->GetThread();
88 BUILTINS_API_TRACE(thread, Collator, Compare);
89 [[maybe_unused]] EcmaHandleScope scope(thread);
90 // 1. Let collator be this value.
91 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
92
93 // 2. Perform ? RequireInternalSlot(collator, [[InitializedCollator]]).
94 if (!thisValue->IsJSCollator()) {
95 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not collator", JSTaggedValue::Exception());
96 }
97 // 3. If collator.[[BoundCompare]] is undefined, then
98 // a. Let F be a new built-in function object as defined in 11.3.3.1.
99 // b. Set F.[[Collator]] to collator.
100 // c. Set collator.[[BoundCompare]] to F.
101 // 4. Return collator.[[BoundCompare]].
102 JSHandle<JSCollator> collator = JSHandle<JSCollator>::Cast(thisValue);
103 JSHandle<JSTaggedValue> boundCompare(thread, collator->GetBoundCompare());
104 if (boundCompare->IsUndefined()) {
105 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
106 JSHandle<JSIntlBoundFunction> intlBoundFunc = factory->NewJSIntlBoundFunction(
107 MethodIndex::BUILTINS_COLLATOR_ANONYMOUS_COLLATOR, FUNCTION_LENGTH_TWO);
108 intlBoundFunc->SetCollator(thread, collator);
109 collator->SetBoundCompare(thread, intlBoundFunc);
110 }
111 return collator->GetBoundCompare();
112 }
113
114 // 11.3.3.1 Collator Compare Functions
AnonymousCollator(EcmaRuntimeCallInfo * argv)115 JSTaggedValue BuiltinsCollator::AnonymousCollator(EcmaRuntimeCallInfo *argv)
116 {
117 // A Collator compare function is an anonymous built-in function that has a [[Collator]] internal slot.
118 // When a Collator compare function F is called with arguments x and y, the following steps are taken:
119 JSThread *thread = argv->GetThread();
120 BUILTINS_API_TRACE(thread, Collator, AnonymousCollator);
121 [[maybe_unused]] EcmaHandleScope scope(thread);
122 JSHandle<JSIntlBoundFunction> intlBoundFunc = JSHandle<JSIntlBoundFunction>::Cast(GetConstructor(argv));
123
124 // 1. Let collator be F.[[Collator]].
125 JSHandle<JSTaggedValue> collator(thread, intlBoundFunc->GetCollator());
126
127 // 2. Assert: Type(collator) is Object and collator has an [[InitializedCollator]] internal slot.
128 ASSERT_PRINT(collator->IsJSObject() && collator->IsJSCollator(), "collator is not object or JSCollator");
129
130 // 3. If x is not provided, let x be undefined.
131 JSHandle<JSTaggedValue> x = GetCallArg(argv, 0);
132
133 // 4. If y is not provided, let y be undefined.
134 JSHandle<JSTaggedValue> y = GetCallArg(argv, 1);
135
136 // 5. Let X be ? ToString(x).
137 JSHandle<EcmaString> xValue = JSTaggedValue::ToString(thread, x);
138 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Undefined());
139 // 6. Let Y be ? ToString(y).
140 JSHandle<EcmaString> yValue = JSTaggedValue::ToString(thread, y);
141 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Undefined());
142 // 7. Return CompareStrings(collator, X, Y).
143 icu::Collator *icuCollator = (JSHandle<JSCollator>::Cast(collator))->GetIcuCollator();
144 return JSCollator::CompareStrings(icuCollator, xValue, yValue);
145 }
146
147 // 11.3.4 Intl.Collator.prototype.resolvedOptions ()
ResolvedOptions(EcmaRuntimeCallInfo * argv)148 JSTaggedValue BuiltinsCollator::ResolvedOptions(EcmaRuntimeCallInfo *argv)
149 {
150 JSThread *thread = argv->GetThread();
151 BUILTINS_API_TRACE(thread, Collator, ResolvedOptions);
152 [[maybe_unused]] EcmaHandleScope scope(thread);
153 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
154 if (!thisValue->IsJSCollator()) {
155 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not Collator object", JSTaggedValue::Exception());
156 }
157 JSHandle<JSObject> options = JSCollator::ResolvedOptions(thread, JSHandle<JSCollator>::Cast(thisValue));
158 return options.GetTaggedValue();
159 }
160 } // namespace panda::ecmascript::builtins