Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Unified Diff: runtime/lib/double.cc

Issue 9113043: Implement Double.{toString, toStringAsExponential, toStringAsPrecision} (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Don't use NULL and rebase. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/lib/double.cc
diff --git a/runtime/lib/double.cc b/runtime/lib/double.cc
index 00c68e823af1033d676c8ada5744f8cd8eaa93b8..5097ffdb02709f2c7b94da64bc353524b241e3c3 100644
--- a/runtime/lib/double.cc
+++ b/runtime/lib/double.cc
@@ -186,13 +186,20 @@ DEFINE_NATIVE_ENTRY(Double_toInt, 1) {
DEFINE_NATIVE_ENTRY(Double_toStringAsFixed, 2) {
+ // The boundaries are exclusive.
+ static const double kLowerBoundary = -1e21;
+ static const double kUpperBoundary = 1e21;
+
const Double& arg = Double::CheckedHandle(arguments->At(0));
GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1));
double d = arg.value();
- int fraction_digits_value = fraction_digits.Value();
String& result = String::Handle();
- bool succeeded = DoubleToStringAsFixed(d, fraction_digits_value, result);
- if (!succeeded) {
+ intptr_t fraction_digits_value = fraction_digits.Value();
+ if (0 <= fraction_digits_value && fraction_digits_value <= 20
+ && kLowerBoundary < d && d < kUpperBoundary) {
+ result = DoubleToStringAsFixed(d, static_cast<int>(fraction_digits_value));
+ }
+ if (result.IsNull()) {
Ivan Posva 2012/02/27 14:47:14 I assume this test is here in case the DoubleToStr
floitsch 2012/02/27 19:55:40 Done.
GrowableArray<const Object*> args;
args.Add(&String::ZoneHandle(String::New(
"Illegal arguments to double.toStringAsFixed")));
@@ -203,12 +210,41 @@ DEFINE_NATIVE_ENTRY(Double_toStringAsFixed, 2) {
DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 2) {
- UNIMPLEMENTED();
+ const Double& arg = Double::CheckedHandle(arguments->At(0));
+ GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1));
+ double d = arg.value();
+ String& result = String::Handle();
+ intptr_t fraction_digits_value = fraction_digits.Value();
+ if (-1 <= fraction_digits_value && fraction_digits_value <= 20) {
+ result =
+ DoubleToStringAsExponential(d, static_cast<int>(fraction_digits_value));
Ivan Posva 2012/02/27 14:47:14 Style nit: Generally we break the line after the c
floitsch 2012/02/27 19:55:40 Done.
+ }
+ if (result.IsNull()) {
+ GrowableArray<const Object*> args;
+ args.Add(&String::ZoneHandle(String::New(
+ "Illegal arguments to double.toStringAsExponential")));
+ Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
+ }
+ arguments->SetReturn(result);
}
DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 2) {
- UNIMPLEMENTED();
+ const Double& arg = Double::CheckedHandle(arguments->At(0));
+ GET_NATIVE_ARGUMENT(Smi, precision, arguments->At(1));
+ double d = arg.value();
+ String& result = String::Handle();
+ intptr_t precision_value = precision.Value();
+ if (1 <= precision_value && precision_value <= 21) {
+ result = DoubleToStringAsPrecision(d, static_cast<int>(precision_value));
+ }
+ if (result.IsNull()) {
+ GrowableArray<const Object*> args;
+ args.Add(&String::ZoneHandle(String::New(
+ "Illegal arguments to double.toStringAsPrecision")));
+ Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
+ }
+ arguments->SetReturn(result);
}

Powered by Google App Engine
This is Rietveld 408576698