• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_list_format.h"
17 
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_list_format.h"
21 #include "ecmascript/js_intl.h"
22 #include "ecmascript/js_locale.h"
23 #include "ecmascript/js_object.h"
24 #include "ecmascript/object_factory.h"
25 
26 namespace panda::ecmascript::builtins {
ListFormatConstructor(EcmaRuntimeCallInfo * argv)27 JSTaggedValue BuiltinsListFormat::ListFormatConstructor(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, 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     // 2. Let listFormat be ? OrdinaryCreateFromConstructor
41     // (NewTarget, "%ListFormat.prototype%", « [[InitializedListFormat]], [[Locale]],
42     // [[Type]], [[Style]], [[Templates]] »).
43 
44     JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
45     JSHandle<JSListFormat> listFormat = JSHandle<JSListFormat>::Cast(
46         factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget));
47     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
48 
49     // 3. Perform ? InitializeListFormat(listFormat, locales, options).
50     JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
51     JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
52     listFormat = JSListFormat::InitializeListFormat(thread, listFormat, locales, options);
53     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
54     return listFormat.GetTaggedValue();
55 }
56 
57 // 13.3.2 Intl.ListFormat.supportedLocalesOf ( locales [ , options ] )
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)58 JSTaggedValue BuiltinsListFormat::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
59 {
60     JSThread *thread = argv->GetThread();
61     [[maybe_unused]] EcmaHandleScope scope(thread);
62 
63     // 1. Let availableLocales be %ListFormat%.[[AvailableLocales]].
64     JSHandle<TaggedArray> availableLocales = JSListFormat::GetAvailableLocales(thread);
65 
66     // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
67     JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
68     JSHandle<TaggedArray> requestedLocales = JSLocale::CanonicalizeLocaleList(thread, locales);
69     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
70 
71     // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
72     JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
73     JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
74     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
75     return result.GetTaggedValue();
76 }
77 
78 // 13.4.3 Intl.ListFormat.prototype.format( list )
Format(EcmaRuntimeCallInfo * argv)79 JSTaggedValue BuiltinsListFormat::Format(EcmaRuntimeCallInfo *argv)
80 {
81     JSThread *thread = argv->GetThread();
82     [[maybe_unused]] EcmaHandleScope scope(thread);
83 
84     // 1. Let lf be the this value.
85     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
86 
87     // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
88     if (!thisValue->IsJSListFormat()) {
89         THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSListFormat", JSTaggedValue::Exception());
90     }
91 
92     // 3. Let stringList be ? StringListFromIterable(list).
93     JSHandle<JSTaggedValue> list = GetCallArg(argv, 0);
94     JSHandle<JSTaggedValue> listArray = JSListFormat::StringListFromIterable(thread, list);
95     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
96 
97     // 4. Return FormatList(lf, stringList).
98     JSHandle<JSListFormat> listFormat = JSHandle<JSListFormat>::Cast(thisValue);
99     JSHandle<JSArray> array = JSHandle<JSArray>::Cast(listArray);
100     JSHandle<EcmaString> result = JSListFormat::FormatList(thread, listFormat, array);
101     return result.GetTaggedValue();
102 }
103 
104 // 13.4.4 Intl.ListFormat.prototype.formatToParts ( list )
FormatToParts(EcmaRuntimeCallInfo * argv)105 JSTaggedValue BuiltinsListFormat::FormatToParts(EcmaRuntimeCallInfo *argv)
106 {
107     JSThread *thread = argv->GetThread();
108     [[maybe_unused]] EcmaHandleScope scope(thread);
109 
110     // 1. Let lf be the this value.
111     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
112 
113     // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
114     if (!thisValue->IsJSListFormat()) {
115         THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSListFormat", JSTaggedValue::Exception());
116     }
117 
118     // 3. Let stringList be ? StringListFromIterable(list).
119     JSHandle<JSTaggedValue> list = GetCallArg(argv, 0);
120     JSHandle<JSTaggedValue> listArray = JSListFormat::StringListFromIterable(thread, list);
121     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
122 
123     // 4 Return FormatListToParts(lf, stringList).
124     JSHandle<JSListFormat> listFormat = JSHandle<JSListFormat>::Cast(thisValue);
125     JSHandle<JSArray> array = JSHandle<JSArray>::Cast(listArray);
126 
127     JSHandle<JSArray> result = JSListFormat::FormatListToParts(thread, listFormat, array);
128     return result.GetTaggedValue();
129 }
130 
131 // 13.4.5 Intl.ListFormat.prototype.resolvedOptions()
ResolvedOptions(EcmaRuntimeCallInfo * argv)132 JSTaggedValue BuiltinsListFormat::ResolvedOptions(EcmaRuntimeCallInfo *argv)
133 {
134     JSThread *thread = argv->GetThread();
135     [[maybe_unused]] EcmaHandleScope scope(thread);
136 
137     // 1. Let lf be the this value.
138     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
139 
140     // 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
141     if (!thisValue->IsJSListFormat()) {
142         THROW_TYPE_ERROR_AND_RETURN(thread, "this is not JSListFormat", JSTaggedValue::Exception());
143     }
144 
145     // 3 .Let options be ! OrdinaryObjectCreate(%Object.prototype%).
146     auto ecmaVm = thread->GetEcmaVM();
147     JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
148     ObjectFactory *factory = ecmaVm->GetFactory();
149     JSHandle<JSFunction> ctor(env->GetObjectFunction());
150     JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
151 
152     // 4. For each row of Table 9, except the header row, in table order, do
153     //  Let p be the Property value of the current row.
154     //  Let v be the value of lf's internal slot whose name is the Internal Slot value of the current row.
155     //  Assert: v is not undefined.
156     //  Perform ! CreateDataPropertyOrThrow(options, p, v).
157     JSHandle<JSListFormat> listFormat = JSHandle<JSListFormat>::Cast(thisValue);
158     JSListFormat::ResolvedOptions(thread, listFormat, options);
159     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
160 
161     // 5. Return options.
162     return options.GetTaggedValue();
163 }
164 }  // namespace panda::ecmascript::builtins