OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include <math.h> | 5 #include <math.h> |
6 | 6 |
7 #include "vm/bootstrap_natives.h" | 7 #include "vm/bootstrap_natives.h" |
8 | 8 |
9 #include "vm/bigint_operations.h" | 9 #include "vm/bigint_operations.h" |
10 #include "vm/double_conversion.h" | 10 #include "vm/double_conversion.h" |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 } else if ((Mint::kMinValue <= result) && (result <= Mint::kMaxValue)) { | 179 } else if ((Mint::kMinValue <= result) && (result <= Mint::kMaxValue)) { |
180 arguments->SetReturn(Mint::Handle(Mint::New(static_cast<int64_t>(result)))); | 180 arguments->SetReturn(Mint::Handle(Mint::New(static_cast<int64_t>(result)))); |
181 } else { | 181 } else { |
182 arguments->SetReturn( | 182 arguments->SetReturn( |
183 Bigint::Handle(BigintOperations::NewFromDouble(result))); | 183 Bigint::Handle(BigintOperations::NewFromDouble(result))); |
184 } | 184 } |
185 } | 185 } |
186 | 186 |
187 | 187 |
188 DEFINE_NATIVE_ENTRY(Double_toStringAsFixed, 2) { | 188 DEFINE_NATIVE_ENTRY(Double_toStringAsFixed, 2) { |
| 189 // The boundaries are exclusive. |
| 190 static const double kLowerBoundary = -1e21; |
| 191 static const double kUpperBoundary = 1e21; |
| 192 |
189 const Double& arg = Double::CheckedHandle(arguments->At(0)); | 193 const Double& arg = Double::CheckedHandle(arguments->At(0)); |
190 GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1)); | 194 GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1)); |
191 double d = arg.value(); | 195 double d = arg.value(); |
192 int fraction_digits_value = fraction_digits.Value(); | 196 intptr_t fraction_digits_value = fraction_digits.Value(); |
193 String& result = String::Handle(); | 197 if (0 <= fraction_digits_value && fraction_digits_value <= 20 |
194 bool succeeded = DoubleToStringAsFixed(d, fraction_digits_value, result); | 198 && kLowerBoundary < d && d < kUpperBoundary) { |
195 if (!succeeded) { | 199 String& result = String::Handle(); |
| 200 result = DoubleToStringAsFixed(d, static_cast<int>(fraction_digits_value)); |
| 201 arguments->SetReturn(result); |
| 202 } else { |
196 GrowableArray<const Object*> args; | 203 GrowableArray<const Object*> args; |
197 args.Add(&String::ZoneHandle(String::New( | 204 args.Add(&String::ZoneHandle(String::New( |
198 "Illegal arguments to double.toStringAsFixed"))); | 205 "Illegal arguments to double.toStringAsFixed"))); |
199 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | 206 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
200 } | 207 } |
201 arguments->SetReturn(result); | |
202 } | 208 } |
203 | 209 |
204 | 210 |
205 DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 2) { | 211 DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 2) { |
206 UNIMPLEMENTED(); | 212 const Double& arg = Double::CheckedHandle(arguments->At(0)); |
| 213 GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1)); |
| 214 double d = arg.value(); |
| 215 intptr_t fraction_digits_value = fraction_digits.Value(); |
| 216 if (-1 <= fraction_digits_value && fraction_digits_value <= 20) { |
| 217 String& result = String::Handle(); |
| 218 result = DoubleToStringAsExponential( |
| 219 d, static_cast<int>(fraction_digits_value)); |
| 220 arguments->SetReturn(result); |
| 221 } else { |
| 222 GrowableArray<const Object*> args; |
| 223 args.Add(&String::ZoneHandle(String::New( |
| 224 "Illegal arguments to double.toStringAsExponential"))); |
| 225 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
| 226 } |
207 } | 227 } |
208 | 228 |
209 | 229 |
210 DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 2) { | 230 DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 2) { |
211 UNIMPLEMENTED(); | 231 const Double& arg = Double::CheckedHandle(arguments->At(0)); |
| 232 GET_NATIVE_ARGUMENT(Smi, precision, arguments->At(1)); |
| 233 double d = arg.value(); |
| 234 intptr_t precision_value = precision.Value(); |
| 235 if (1 <= precision_value && precision_value <= 21) { |
| 236 String& result = String::Handle(); |
| 237 result = DoubleToStringAsPrecision(d, static_cast<int>(precision_value)); |
| 238 arguments->SetReturn(result); |
| 239 } else { |
| 240 GrowableArray<const Object*> args; |
| 241 args.Add(&String::ZoneHandle(String::New( |
| 242 "Illegal arguments to double.toStringAsPrecision"))); |
| 243 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
| 244 } |
212 } | 245 } |
213 | 246 |
214 | 247 |
215 DEFINE_NATIVE_ENTRY(Double_isInfinite, 1) { | 248 DEFINE_NATIVE_ENTRY(Double_isInfinite, 1) { |
216 const Double& arg = Double::CheckedHandle(arguments->At(0)); | 249 const Double& arg = Double::CheckedHandle(arguments->At(0)); |
217 if (isinf(arg.value())) { | 250 if (isinf(arg.value())) { |
218 arguments->SetReturn(Bool::Handle(Bool::True())); | 251 arguments->SetReturn(Bool::Handle(Bool::True())); |
219 } else { | 252 } else { |
220 arguments->SetReturn(Bool::Handle(Bool::False())); | 253 arguments->SetReturn(Bool::Handle(Bool::False())); |
221 } | 254 } |
(...skipping 17 matching lines...) Expand all Loading... |
239 arguments->SetReturn(Bool::Handle(Bool::True())); | 272 arguments->SetReturn(Bool::Handle(Bool::True())); |
240 } else { | 273 } else { |
241 arguments->SetReturn(Bool::Handle(Bool::False())); | 274 arguments->SetReturn(Bool::Handle(Bool::False())); |
242 } | 275 } |
243 } | 276 } |
244 | 277 |
245 // Add here only functions using/referring to old-style casts. | 278 // Add here only functions using/referring to old-style casts. |
246 | 279 |
247 } // namespace dart | 280 } // namespace dart |
248 | 281 |
OLD | NEW |