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