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