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

Unified Diff: lib/compiler/implementation/lib/math.dartp

Issue 10696147: Patch methods in classes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 5 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: lib/compiler/implementation/lib/math.dartp
diff --git a/lib/compiler/implementation/lib/math.dartp b/lib/compiler/implementation/lib/math.dartp
index af061bff0c7e2bee778958b74b8a8715f8d2213d..d1c6f68742cb06997c59d57ea1bd2ec9cffc348e 100644
--- a/lib/compiler/implementation/lib/math.dartp
+++ b/lib/compiler/implementation/lib/math.dartp
@@ -4,10 +4,94 @@
// Patch file for dart:math library.
-// Imports the MathNatives class used by the library as well as checkNum used
-// below.
+// Imports checkNum etc. used below.
#import("js_helper.dart");
-// Override the sqrt function, as proof-of-concept.
+patch int parseInt(str) {
+ checkString(str);
+ if (!JS('bool',
+ @'/^\s*[+-]?(?:0[xX][abcdefABCDEF0-9]+|\d+)\s*$/.test(#)',
+ str)) {
+ throw new BadNumberFormatException(str);
+ }
+ var trimmed = str.trim();
+ var base = 10;;
+ if ((trimmed.length > 2 && (trimmed[1] == 'x' || trimmed[1] == 'X')) ||
+ (trimmed.length > 3 && (trimmed[2] == 'x' || trimmed[2] == 'X'))) {
+ base = 16;
+ }
+ var ret = JS('num', @'parseInt(#, #)', trimmed, base);
+ if (ret.isNaN()) throw new BadNumberFormatException(str);
+ return ret;
+}
+
+patch double parseDouble(String str) {
+ checkString(str);
+ var ret = JS('num', @'parseFloat(#)', str);
+ if (ret == 0 && (str.startsWith("0x") || str.startsWith("0X"))) {
+ // TODO(ahe): This is unspecified, but tested by co19.
+ ret = JS('num', @'parseInt(#)', str);
+ }
+ if (ret.isNaN() && str != 'NaN' && str != '-NaN') {
+ throw new BadNumberFormatException(str);
+ }
+ return ret;
+}
+
patch double sqrt(num value)
=> JS('double', @'Math.sqrt(#)', checkNum(value));
+
+patch double sin(num value)
+ => JS('double', @'Math.sin(#)', checkNum(value));
+
+patch double cos(num value)
+ => JS('double', @'Math.cos(#)', checkNum(value));
+
+patch double tan(num value)
+ => JS('double', @'Math.tan(#)', checkNum(value));
+
+patch double acos(num value)
+ => JS('double', @'Math.acos(#)', checkNum(value));
+
+patch double asin(num value)
+ => JS('double', @'Math.asin(#)', checkNum(value));
+
+patch double atan(num value)
+ => JS('double', @'Math.atan(#)', checkNum(value));
+
+patch double atan2(num a, num b)
+ => JS('double', @'Math.atan2(#, #)', checkNum(a), checkNum(b));
+
+patch double exp(num value)
+ => JS('double', @'Math.exp(#)', checkNum(value));
+
+patch double log(num value)
+ => JS('double', @'Math.log(#)', checkNum(value));
+
+patch num pow(num value, num exponent) {
+ checkNum(value);
+ checkNum(exponent);
+ return JS('num', @'Math.pow(#, #)', value, exponent);
+}
+
+patch class _Random {
+ // The Dart2JS implementation of Random doesn't use the seed.
+ patch _Random([int seed]);
+
+ patch int nextInt(int max) {
+ if (max < 0) throw new IllegalArgumentException("negative max: $max");
+ if (max > 0x100000000) max = 0x100000000;
+ return JS("int", "Math.floor(Math.random() * #)", max);
+ }
+
+ /**
+ * Generates a positive random floating point value uniformly distributed on
+ * the range from 0.0, inclusive, to 1.0, exclusive.
+ */
+ patch double nextDouble() => JS("double", "Math.random()");
+
+ /**
+ * Generates a random boolean value.
+ */
+ patch bool nextBool() => JS("bool", "Math.random() < 0.5");
+}

Powered by Google App Engine
This is Rietveld 408576698