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