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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 182 |
183 | 183 |
184 #if defined(TARGET_OS_MACOS) | 184 #if defined(TARGET_OS_MACOS) |
185 // MAC OSX math library produces old style cast warning. | 185 // MAC OSX math library produces old style cast warning. |
186 #pragma GCC diagnostic ignored "-Wold-style-cast" | 186 #pragma GCC diagnostic ignored "-Wold-style-cast" |
187 #endif | 187 #endif |
188 | 188 |
189 DEFINE_NATIVE_ENTRY(Double_toInt, 1) { | 189 DEFINE_NATIVE_ENTRY(Double_toInt, 1) { |
190 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); | 190 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
191 if (isinf(arg.value()) || isnan(arg.value())) { | 191 if (isinf(arg.value()) || isnan(arg.value())) { |
192 GrowableArray<const Object*> args; | 192 const Array& args = Array::Handle(Array::New(1)); |
193 args.Add(&String::ZoneHandle(String::New( | 193 args.SetAt(0, String::Handle(String::New("Infinity or NaN toInt"))); |
194 "Infinity or NaN toInt"))); | |
195 Exceptions::ThrowByType(Exceptions::kFormat, args); | 194 Exceptions::ThrowByType(Exceptions::kFormat, args); |
196 } | 195 } |
197 double result = trunc(arg.value()); | 196 double result = trunc(arg.value()); |
198 if ((Smi::kMinValue <= result) && (result <= Smi::kMaxValue)) { | 197 if ((Smi::kMinValue <= result) && (result <= Smi::kMaxValue)) { |
199 return Smi::New(static_cast<intptr_t>(result)); | 198 return Smi::New(static_cast<intptr_t>(result)); |
200 } else if ((Mint::kMinValue <= result) && (result <= Mint::kMaxValue)) { | 199 } else if ((Mint::kMinValue <= result) && (result <= Mint::kMaxValue)) { |
201 return Mint::New(static_cast<int64_t>(result)); | 200 return Mint::New(static_cast<int64_t>(result)); |
202 } else { | 201 } else { |
203 return BigintOperations::NewFromDouble(result); | 202 return BigintOperations::NewFromDouble(result); |
204 } | 203 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 return Double::New(NAN); | 245 return Double::New(NAN); |
247 } | 246 } |
248 if (number_string->Equals("Infinity")) { | 247 if (number_string->Equals("Infinity")) { |
249 if (is_positive) { | 248 if (is_positive) { |
250 return Double::New(INFINITY); | 249 return Double::New(INFINITY); |
251 } | 250 } |
252 return Double::New(-INFINITY); | 251 return Double::New(-INFINITY); |
253 } | 252 } |
254 } | 253 } |
255 | 254 |
256 GrowableArray<const Object*> args; | 255 const Array& args = Array::Handle(Array::New(1)); |
257 args.Add(&value); | 256 args.SetAt(0, value); |
258 Exceptions::ThrowByType(Exceptions::kFormat, args); | 257 Exceptions::ThrowByType(Exceptions::kFormat, args); |
259 return Object::null(); | 258 return Object::null(); |
260 } | 259 } |
261 | 260 |
262 | 261 |
263 DEFINE_NATIVE_ENTRY(Double_toStringAsFixed, 2) { | 262 DEFINE_NATIVE_ENTRY(Double_toStringAsFixed, 2) { |
264 // The boundaries are exclusive. | 263 // The boundaries are exclusive. |
265 static const double kLowerBoundary = -1e21; | 264 static const double kLowerBoundary = -1e21; |
266 static const double kUpperBoundary = 1e21; | 265 static const double kUpperBoundary = 1e21; |
267 | 266 |
268 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); | 267 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
269 GET_NON_NULL_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->NativeArgAt(1)); | 268 GET_NON_NULL_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->NativeArgAt(1)); |
270 double d = arg.value(); | 269 double d = arg.value(); |
271 intptr_t fraction_digits_value = fraction_digits.Value(); | 270 intptr_t fraction_digits_value = fraction_digits.Value(); |
272 if (0 <= fraction_digits_value && fraction_digits_value <= 20 | 271 if (0 <= fraction_digits_value && fraction_digits_value <= 20 |
273 && kLowerBoundary < d && d < kUpperBoundary) { | 272 && kLowerBoundary < d && d < kUpperBoundary) { |
274 return DoubleToStringAsFixed(d, static_cast<int>(fraction_digits_value)); | 273 return DoubleToStringAsFixed(d, static_cast<int>(fraction_digits_value)); |
275 } else { | 274 } else { |
276 GrowableArray<const Object*> args; | 275 const Array& args = Array::Handle(Array::New(1)); |
277 args.Add(&String::ZoneHandle(String::New( | 276 args.SetAt(0, String::Handle( |
278 "Illegal arguments to double.toStringAsFixed"))); | 277 String::New("Illegal arguments to double.toStringAsFixed"))); |
279 Exceptions::ThrowByType(Exceptions::kArgument, args); | 278 Exceptions::ThrowByType(Exceptions::kArgument, args); |
280 return Object::null(); | 279 return Object::null(); |
281 } | 280 } |
282 } | 281 } |
283 | 282 |
284 | 283 |
285 DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 2) { | 284 DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 2) { |
286 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); | 285 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
287 GET_NON_NULL_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->NativeArgAt(1)); | 286 GET_NON_NULL_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->NativeArgAt(1)); |
288 double d = arg.value(); | 287 double d = arg.value(); |
289 intptr_t fraction_digits_value = fraction_digits.Value(); | 288 intptr_t fraction_digits_value = fraction_digits.Value(); |
290 if (-1 <= fraction_digits_value && fraction_digits_value <= 20) { | 289 if (-1 <= fraction_digits_value && fraction_digits_value <= 20) { |
291 return DoubleToStringAsExponential( | 290 return DoubleToStringAsExponential( |
292 d, static_cast<int>(fraction_digits_value)); | 291 d, static_cast<int>(fraction_digits_value)); |
293 } else { | 292 } else { |
294 GrowableArray<const Object*> args; | 293 const Array& args = Array::Handle(Array::New(1)); |
295 args.Add(&String::ZoneHandle(String::New( | 294 args.SetAt(0, String::Handle( |
296 "Illegal arguments to double.toStringAsExponential"))); | 295 String::New("Illegal arguments to double.toStringAsExponential"))); |
297 Exceptions::ThrowByType(Exceptions::kArgument, args); | 296 Exceptions::ThrowByType(Exceptions::kArgument, args); |
298 return Object::null(); | 297 return Object::null(); |
299 } | 298 } |
300 } | 299 } |
301 | 300 |
302 | 301 |
303 DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 2) { | 302 DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 2) { |
304 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); | 303 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
305 GET_NON_NULL_NATIVE_ARGUMENT(Smi, precision, arguments->NativeArgAt(1)); | 304 GET_NON_NULL_NATIVE_ARGUMENT(Smi, precision, arguments->NativeArgAt(1)); |
306 double d = arg.value(); | 305 double d = arg.value(); |
307 intptr_t precision_value = precision.Value(); | 306 intptr_t precision_value = precision.Value(); |
308 if (1 <= precision_value && precision_value <= 21) { | 307 if (1 <= precision_value && precision_value <= 21) { |
309 return DoubleToStringAsPrecision(d, static_cast<int>(precision_value)); | 308 return DoubleToStringAsPrecision(d, static_cast<int>(precision_value)); |
310 } else { | 309 } else { |
311 GrowableArray<const Object*> args; | 310 const Array& args = Array::Handle(Array::New(1)); |
312 args.Add(&String::ZoneHandle(String::New( | 311 args.SetAt(0, String::Handle( |
313 "Illegal arguments to double.toStringAsPrecision"))); | 312 String::New("Illegal arguments to double.toStringAsPrecision"))); |
314 Exceptions::ThrowByType(Exceptions::kArgument, args); | 313 Exceptions::ThrowByType(Exceptions::kArgument, args); |
315 return Object::null(); | 314 return Object::null(); |
316 } | 315 } |
317 } | 316 } |
318 | 317 |
319 | 318 |
320 DEFINE_NATIVE_ENTRY(Double_getIsInfinite, 1) { | 319 DEFINE_NATIVE_ENTRY(Double_getIsInfinite, 1) { |
321 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); | 320 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
322 return Bool::Get(isinf(arg.value())); | 321 return Bool::Get(isinf(arg.value())); |
323 } | 322 } |
324 | 323 |
325 | 324 |
326 DEFINE_NATIVE_ENTRY(Double_getIsNaN, 1) { | 325 DEFINE_NATIVE_ENTRY(Double_getIsNaN, 1) { |
327 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); | 326 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
328 return Bool::Get(isnan(arg.value())); | 327 return Bool::Get(isnan(arg.value())); |
329 } | 328 } |
330 | 329 |
331 | 330 |
332 DEFINE_NATIVE_ENTRY(Double_getIsNegative, 1) { | 331 DEFINE_NATIVE_ENTRY(Double_getIsNegative, 1) { |
333 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); | 332 const Double& arg = Double::CheckedHandle(arguments->NativeArgAt(0)); |
334 // Include negative zero, infinity. | 333 // Include negative zero, infinity. |
335 return Bool::Get(signbit(arg.value()) && !isnan(arg.value())); | 334 return Bool::Get(signbit(arg.value()) && !isnan(arg.value())); |
336 } | 335 } |
337 | 336 |
338 // Add here only functions using/referring to old-style casts. | 337 // Add here only functions using/referring to old-style casts. |
339 | 338 |
340 } // namespace dart | 339 } // namespace dart |
OLD | NEW |