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_number_format.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_array.h"
20 #include "ecmascript/js_intl.h"
21 #include "ecmascript/js_locale.h"
22 #include "ecmascript/js_number_format.h"
23 #include "ecmascript/js_tagged_value.h"
24 #include "ecmascript/object_factory.h"
25
26 namespace panda::ecmascript::builtins {
27 // 13.2.1 Intl.NumberFormat ( [ locales [ , options ] ] )
NumberFormatConstructor(EcmaRuntimeCallInfo * argv)28 JSTaggedValue BuiltinsNumberFormat::NumberFormatConstructor(EcmaRuntimeCallInfo *argv)
29 {
30 JSThread *thread = argv->GetThread();
31 [[maybe_unused]] EcmaHandleScope scope(thread);
32 EcmaVM *ecmaVm = thread->GetEcmaVM();
33 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
34 ObjectFactory *factory = ecmaVm->GetFactory();
35
36 // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
37 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
38 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
39 if (newTarget->IsUndefined()) {
40 newTarget = constructor;
41 }
42
43 // Let numberFormat be ? OrdinaryCreateFromConstructor(newTarget, "%NumberFormatPrototype%",
44 // « [[InitializedNumberFormat]], [[Locale]], [[DataLocale]], [[NumberingSystem]], [[Style]], [[Unit]],
45 // [[UnitDisplay]], [[Currency]], [[CurrencyDisplay]], [[CurrencySign]], [[MinimumIntegerDigits]],
46 // [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]],
47 // [[RoundingType]], [[Notation]], [[CompactDisplay]], [[UseGrouping]], [[SignDisplay]], [[BoundFormat]] »).
48 JSHandle<JSNumberFormat> numberFormat =
49 JSHandle<JSNumberFormat>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget));
50 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
51
52 // 3. Perform ? InitializeNumberFormat(numberFormat, locales, options).
53 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
54 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
55 JSNumberFormat::InitializeNumberFormat(thread, numberFormat, locales, options);
56 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
57
58 // 4. Let this be the this value.
59 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
60
61 // 5. If NewTarget is undefined and Type(this) is Object and ? InstanceofOperator(this, %NumberFormat%) is true,
62 // then
63 // a. Perform ? DefinePropertyOrThrow(this, %Intl%.[[FallbackSymbol]], PropertyDescriptor{
64 // [[Value]]: numberFormat, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
65 // b. Return this.
66 bool isInstanceOf = JSObject::InstanceOf(thread, thisValue, env->GetNumberFormatFunction());
67 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
68 if (newTarget->IsUndefined() && thisValue->IsJSObject() && isInstanceOf) {
69 PropertyDescriptor descriptor(thread, JSHandle<JSTaggedValue>::Cast(numberFormat), false, false, false);
70 JSHandle<JSTaggedValue> key(thread, JSHandle<JSIntl>::Cast(env->GetIntlFunction())->GetFallbackSymbol());
71 JSTaggedValue::DefinePropertyOrThrow(thread, thisValue, key, descriptor);
72 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
73 return thisValue.GetTaggedValue();
74 }
75
76 // 6. Return numberFormat.
77 return numberFormat.GetTaggedValue();
78 }
79
80 // 13.3.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] )
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)81 JSTaggedValue BuiltinsNumberFormat::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
82 {
83 JSThread *thread = argv->GetThread();
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 = JSLocale::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 [[maybe_unused]] EcmaHandleScope scope(thread);
105
106 // 1. Let nf be this value.
107 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
108 // 2. If Type(nf) is not Object, throw a TypeError exception.
109 if (!thisValue->IsJSObject()) {
110 THROW_TYPE_ERROR_AND_RETURN(thread, "nf is not object", JSTaggedValue::Exception());
111 }
112 // 3. Let nf be ? UnwrapNumberFormat(nf).
113 JSHandle<JSTaggedValue> nf = JSNumberFormat::UnwrapNumberFormat(thread, thisValue);
114 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
115 if (nf->IsUndefined()) {
116 THROW_TYPE_ERROR_AND_RETURN(thread, "nf is not object", JSTaggedValue::Exception());
117 }
118
119 JSHandle<JSNumberFormat> typpedNf = JSHandle<JSNumberFormat>::Cast(nf);
120 JSHandle<JSTaggedValue> boundFunc(thread, typpedNf->GetBoundFormat());
121 // 4. If nf.[[BoundFormat]] is undefined, then
122 // a. Let F be a new built-in function object as defined in Number Format Functions (12.1.4).
123 // b. Set F.[[NumberFormat]] to nf.
124 // c. Set nf.[[BoundFormat]] to F.
125 if (boundFunc->IsUndefined()) {
126 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
127 JSHandle<JSIntlBoundFunction> intlBoundFunc = factory->NewJSIntlBoundFunction(
128 reinterpret_cast<void *>(BuiltinsNumberFormat::NumberFormatInternalFormatNumber));
129 intlBoundFunc->SetNumberFormat(thread, typpedNf);
130 typpedNf->SetBoundFormat(thread, intlBoundFunc);
131 }
132 return typpedNf->GetBoundFormat();
133 }
134
135 // 13.4.4 Intl.NumberFormat.prototype.formatToParts ( date )
FormatToParts(EcmaRuntimeCallInfo * argv)136 JSTaggedValue BuiltinsNumberFormat::FormatToParts(EcmaRuntimeCallInfo *argv)
137 {
138 JSThread *thread = argv->GetThread();
139 [[maybe_unused]] EcmaHandleScope scope(thread);
140 // 1. Let nf be the this value.
141 JSHandle<JSTaggedValue> nf = GetThis(argv);
142 // 2. Perform ? RequireInternalSlot(nf, [[InitializedNumberFormat]]).
143 if (!nf->IsJSNumberFormat()) {
144 THROW_TYPE_ERROR_AND_RETURN(thread, "", JSTaggedValue::Exception());
145 }
146 // 3. Let x be ? ToNumeric(value).
147 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
148 JSTaggedNumber x = JSTaggedValue::ToNumber(thread, value);
149 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
150
151 JSHandle<JSArray> result = JSNumberFormat::FormatNumericToParts(thread, JSHandle<JSNumberFormat>::Cast(nf), x);
152 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
153
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<JSTaggedValue> ctor = env->GetObjectFunction();
175 JSHandle<JSObject> options(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), 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 JSTaggedNumber x = JSTaggedValue::ToNumber(thread, value);
200 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
201 // 5 Return ? FormatNumeric(nf, x).
202 JSHandle<JSTaggedValue> result = JSNumberFormat::FormatNumeric(thread, JSHandle<JSNumberFormat>::Cast(nf), x);
203 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
204 return result.GetTaggedValue();
205 }
206 } // namespace panda::ecmascript::builtins