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

Unified Diff: sdk/lib/_internal/compiler/implementation/lib/js_number.dart

Issue 11348129: Add JSNumber, JSInt and JSDouble for the new interceptor scheme. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 1 month 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: sdk/lib/_internal/compiler/implementation/lib/js_number.dart
===================================================================
--- sdk/lib/_internal/compiler/implementation/lib/js_number.dart (revision 0)
+++ sdk/lib/_internal/compiler/implementation/lib/js_number.dart (revision 0)
@@ -0,0 +1,124 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of _interceptors;
+
+/**
+ * The super interceptor class for [JSInt] and [JSDouble]. The compiler
+ * recognizes this class as an interceptor, and changes references to
+ * [:this:] to actually use the receiver of the method, which is
+ * generated as an extra argument added to each member.
+ */
+class JSNumber {
+ const JSNumber();
+
+ int compareTo(num b) {
+ if (b is! num) throw new ArgumentError(b);
+ if (this < b) {
+ return -1;
+ } else if (this > b) {
+ return 1;
+ } else if (this == b) {
+ if (this == 0) {
+ bool bIsNegative = b.isNegative;
+ if (isNegative == bIsNegative) return 0;
+ if (isNegative) return -1;
+ return 1;
+ }
+ return 0;
+ } else if (isNaN) {
+ if (b.isNaN) {
+ return 0;
+ }
+ return 1;
+ } else {
+ return -1;
+ }
+ }
+
+ bool get isNegative => (this == 0) ? (1 / this) < 0 : this < 0;
+
+ bool get isNaN => JS('bool', r'isNaN(#)', this);
+
+ num remainder(num b) {
+ checkNull(b); // TODO(ngeoffray): This is not specified but co19 tests it.
+ if (b is! num) throw new ArgumentError(b);
+ return JS('num', r'# % #', this, b);
+ }
+
+ num abs() => JS('num', r'Math.abs(#)', this);
+
+ int toInt() {
+ if (isNaN) throw new FormatException('NaN');
+ if (isInfinite) throw new FormatException('Infinity');
+ num truncated = truncate();
+ return JS('bool', r'# == -0.0', truncated) ? 0 : truncated;
+ }
+
+ num ceil() => JS('num', r'Math.ceil(#)', this);
+
+ num floor() => JS('num', r'Math.floor(#)', this);
+
+ bool get isInfinite {
+ return JS('bool', r'# == Infinity', this)
+ || JS('bool', r'# == -Infinity', this);
+ }
+
+ num round() {
+ if (this < 0) {
+ return JS('num', r'-Math.round(-#)', this);
+ } else {
+ return JS('num', r'Math.round(#)', this);
+ }
+ }
+
+ double toDouble() => this;
+
+ num truncate() => this < 0 ? ceil() : floor();
+
+ String toStringAsFixed(int fractionDigits) {
+ checkNum(fractionDigits);
+ String result = JS('String', r'#.toFixed(#)', this, fractionDigits);
+ if (this == 0 && isNegative) return "-$result";
+ return result;
+ }
+
+ String toStringAsExponential(int fractionDigits) {
+ String result;
+ if (fractionDigits != null) {
+ checkNum(fractionDigits);
+ result = JS('String', r'#.toExponential(#)', this, fractionDigits);
+ } else {
+ result = JS('String', r'#.toExponential()', this);
+ }
+ if (this == 0 && isNegative) return "-$result";
+ return result;
+ }
+
+ String toStringAsPrecision(int fractionDigits) {
+ checkNum(fractionDigits);
+ String result = JS('String', r'#.toPrecision(#)',
+ this, fractionDigits);
+ if (this == 0 && isNegative) return "-$result";
+ return result;
+ }
+
+ String toRadixString(int radix) {
+ checkNum(radix);
+ if (radix < 2 || radix > 36) throw new ArgumentError(radix);
+ return JS('String', r'#.toString(#)', this, radix);
+ }
+}
+
+class JSInt extends JSNumber {
+ const JSInt();
+
+ bool get isEven => (this & 1) == 0;
+
+ bool get isOdd => (this & 1) == 1;
+}
+
+class JSDouble extends JSNumber {
+ const JSDouble();
+}

Powered by Google App Engine
This is Rietveld 408576698