• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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_number_format.h"
17 
18 #include "ecmascript/js_function.h"
19 #include "ecmascript/js_number_format.h"
20 
21 namespace panda::ecmascript::builtins {
22 // 13.2.1 Intl.NumberFormat  ( [ locales [ , options ] ] )
NumberFormatConstructor(EcmaRuntimeCallInfo * argv)23 JSTaggedValue BuiltinsNumberFormat::NumberFormatConstructor(EcmaRuntimeCallInfo *argv)
24 {
25     JSThread *thread = argv->GetThread();
26     BUILTINS_API_TRACE(thread, NumberFormat, Constructor);
27     [[maybe_unused]] EcmaHandleScope scope(thread);
28     EcmaVM *ecmaVm = thread->GetEcmaVM();
29     JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
30     ObjectFactory *factory = ecmaVm->GetFactory();
31 
32     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
33     JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
34     JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
35     if (newTarget->IsUndefined()) {
36         newTarget = constructor;
37     }
38 
39     // Let numberFormat be ? OrdinaryCreateFromConstructor(newTarget, "%NumberFormatPrototype%",
40     // « [[InitializedNumberFormat]], [[Locale]], [[DataLocale]], [[NumberingSystem]], [[Style]], [[Unit]],
41     // [[UnitDisplay]], [[Currency]], [[CurrencyDisplay]], [[CurrencySign]], [[MinimumIntegerDigits]],
42     // [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]],
43     // [[RoundingType]], [[Notation]], [[CompactDisplay]], [[UseGrouping]], [[SignDisplay]], [[BoundFormat]] »).
44     JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
45     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
46     JSHandle<JSNumberFormat> numberFormat = JSHandle<JSNumberFormat>::Cast(newObject);
47 
48     // 3. Perform ? InitializeNumberFormat(numberFormat, locales, options).
49     JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
50     JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
51     JSNumberFormat::InitializeNumberFormat(thread, numberFormat, locales, options);
52     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
53 
54     // 4. Let this be the this value.
55     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
56 
57     // 5. If NewTarget is undefined and Type(this) is Object and ? InstanceofOperator(this, %NumberFormat%) is true,
58     //    then
59     //    a. Perform ? DefinePropertyOrThrow(this, %Intl%.[[FallbackSymbol]], PropertyDescriptor{
60     //       [[Value]]: numberFormat, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
61     //    b. Return this.
62     if (GetNewTarget(argv)->IsUndefined() && thisValue->IsJSObject()) {
63         bool isInstanceOf = JSFunction::OrdinaryHasInstance(thread, env->GetNumberFormatFunction(), thisValue);
64         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
65         if (isInstanceOf) {
66             PropertyDescriptor descriptor(thread, JSHandle<JSTaggedValue>::Cast(numberFormat), false, false, false);
67             JSHandle<JSTaggedValue> key(thread,
68                 JSHandle<JSIntl>::Cast(env->GetIntlFunction())->GetFallbackSymbol(thread));
69             JSTaggedValue::DefinePropertyOrThrow(thread, thisValue, key, descriptor);
70             RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
71             return thisValue.GetTaggedValue();
72         }
73     }
74 
75     // 6. Return numberFormat.
76     return numberFormat.GetTaggedValue();
77 }
78 
79 // 13.3.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] )
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)80 JSTaggedValue BuiltinsNumberFormat::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
81 {
82     JSThread *thread = argv->GetThread();
83     BUILTINS_API_TRACE(thread, NumberFormat, SupportedLocalesOf);
84     [[maybe_unused]] EcmaHandleScope scope(thread);
85     // 1. Let availableLocales be %NumberFormat%.[[AvailableLocales]].
86     JSHandle<TaggedArray> availableLocales = JSNumberFormat::GetAvailableLocales(thread);
87 
88     // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
89     JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
90     JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
91     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
92 
93     // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
94     JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
95     JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
96     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
97     return result.GetTaggedValue();
98 }
99 
100 // 13.4.3 get Intl.NumberFormat.prototype.format
Format(EcmaRuntimeCallInfo * argv)101 JSTaggedValue BuiltinsNumberFormat::Format(EcmaRuntimeCallInfo *argv)
102 {
103     JSThread *thread = argv->GetThread();
104     BUILTINS_API_TRACE(thread, NumberFormat, Format);
105     [[maybe_unused]] EcmaHandleScope scope(thread);
106 
107     // 1. Let nf be this value.
108     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
109     // 2. If Type(nf) is not Object, throw a TypeError exception.
110     if (!thisValue->IsJSObject()) {
111         THROW_TYPE_ERROR_AND_RETURN(thread, "nf is not object", JSTaggedValue::Exception());
112     }
113     // 3. Let nf be ? UnwrapNumberFormat(nf).
114     JSHandle<JSTaggedValue> nf = JSNumberFormat::UnwrapNumberFormat(thread, thisValue);
115     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
116     if (nf->IsUndefined()) {
117         THROW_TYPE_ERROR_AND_RETURN(thread, "nf is not object", JSTaggedValue::Exception());
118     }
119 
120     JSHandle<JSNumberFormat> typpedNf = JSHandle<JSNumberFormat>::Cast(nf);
121     JSHandle<JSTaggedValue> boundFunc(thread, typpedNf->GetBoundFormat(thread));
122     // 4. If nf.[[BoundFormat]] is undefined, then
123     //      a. Let F be a new built-in function object as defined in Number Format Functions (12.1.4).
124     //      b. Set F.[[NumberFormat]] to nf.
125     //      c. Set nf.[[BoundFormat]] to F.
126     if (boundFunc->IsUndefined()) {
127         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
128         JSHandle<JSIntlBoundFunction> intlBoundFunc = factory->NewJSIntlBoundFunction(
129             MethodIndex::BUILTINS_NUMBER_FORMAT_NUMBER_FORMAT_INTERNAL_FORMAT_NUMBER);
130         intlBoundFunc->SetNumberFormat(thread, typpedNf);
131         typpedNf->SetBoundFormat(thread, intlBoundFunc);
132     }
133     return typpedNf->GetBoundFormat(thread);
134 }
135 
136 // 13.4.4 Intl.NumberFormat.prototype.formatToParts ( date )
FormatToParts(EcmaRuntimeCallInfo * argv)137 JSTaggedValue BuiltinsNumberFormat::FormatToParts(EcmaRuntimeCallInfo *argv)
138 {
139     JSThread *thread = argv->GetThread();
140     BUILTINS_API_TRACE(thread, NumberFormat, FormatToParts);
141     [[maybe_unused]] EcmaHandleScope scope(thread);
142     // 1. Let nf be the this value.
143     JSHandle<JSTaggedValue> nf = GetThis(argv);
144     // 2. Perform ? RequireInternalSlot(nf, [[InitializedNumberFormat]]).
145     if (!nf->IsJSNumberFormat()) {
146         THROW_TYPE_ERROR_AND_RETURN(thread, "Is not JSNumberFormat", JSTaggedValue::Exception());
147     }
148     // 3. Let x be ? ToNumeric(value).
149     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
150     JSHandle<JSTaggedValue> x = JSTaggedValue::ToNumeric(thread, value);
151     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
152     JSHandle<JSArray> result =
153         JSNumberFormat::FormatNumericToParts(thread, JSHandle<JSNumberFormat>::Cast(nf), x.GetTaggedValue());
154     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
155     return result.GetTaggedValue();
156 }
157 // 13.4.5 Intl.NumberFormat.prototype.resolvedOptions ()
ResolvedOptions(EcmaRuntimeCallInfo * argv)158 JSTaggedValue BuiltinsNumberFormat::ResolvedOptions(EcmaRuntimeCallInfo *argv)
159 {
160     JSThread *thread = argv->GetThread();
161     BUILTINS_API_TRACE(thread, NumberFormat, ResolvedOptions);
162     [[maybe_unused]] EcmaHandleScope scope(thread);
163     // 1. Let nf be this value.
164     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
165     // 2. If Type(nf) is not Object, throw a TypeError exception.
166     if (!thisValue->IsJSObject()) {
167         THROW_TYPE_ERROR_AND_RETURN(thread, "this is not object", JSTaggedValue::Exception());
168     }
169     // 3. Let nf be ? UnwrapNumberFormat(nf).
170     JSHandle<JSTaggedValue> nf = JSNumberFormat::UnwrapNumberFormat(thread, thisValue);
171     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
172     // Perform ? RequireInternalSlot(nf, [[InitializedNumberFormat]]).
173     if (!nf->IsJSNumberFormat()) {
174         THROW_TYPE_ERROR_AND_RETURN(thread, "nf is not JSNumberFormat", JSTaggedValue::Exception());
175     }
176     // 4. Let options be ! ObjectCreate(%ObjectPrototype%).
177     auto ecmaVm = thread->GetEcmaVM();
178     JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
179     ObjectFactory *factory = ecmaVm->GetFactory();
180     JSHandle<JSFunction> ctor(env->GetObjectFunction());
181     JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
182 
183     // 5. For each row of Table 5, except the header row, in table order, do
184     //  Let p be the Property value of the current row.
185     //  Let v be the value of nf's internal slot whose name is the Internal Slot value of the current row.
186     //  If v is not undefined, then
187     //  Perform ! CreateDataPropertyOrThrow(options, p, v).
188     JSNumberFormat::ResolvedOptions(thread, JSHandle<JSNumberFormat>::Cast(nf), options);
189     return options.GetTaggedValue();
190 }
191 
NumberFormatInternalFormatNumber(EcmaRuntimeCallInfo * argv)192 JSTaggedValue BuiltinsNumberFormat::NumberFormatInternalFormatNumber(EcmaRuntimeCallInfo *argv)
193 {
194     JSThread *thread = argv->GetThread();
195     [[maybe_unused]] EcmaHandleScope scope(thread);
196     JSHandle<JSIntlBoundFunction> intlBoundFunc = JSHandle<JSIntlBoundFunction>::Cast(GetConstructor(argv));
197 
198     // 1. Let nf be F.[[NumberFormat]].
199     JSHandle<JSTaggedValue> nf(thread, intlBoundFunc->GetNumberFormat(thread));
200     // 2. Assert: Type(nf) is Object and nf has an [[InitializedNumberFormat]] internal slot.
201     ASSERT(nf->IsJSObject() && nf->IsJSNumberFormat());
202     // 3. If value is not provided, let value be undefined.
203     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
204     // 4 Let x be ? ToNumeric(value).
205     JSHandle<JSTaggedValue> x = JSTaggedValue::ToNumeric(thread, value);
206     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
207     // 5 Return ? FormatNumeric(nf, x).
208     JSHandle<JSTaggedValue> result =
209         JSNumberFormat::FormatNumeric(thread, JSHandle<JSNumberFormat>::Cast(nf), x.GetTaggedValue());
210     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
211     return result.GetTaggedValue();
212 }
213 }  // namespace panda::ecmascript::builtins