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