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

Unified Diff: pkg/compiler/lib/src/js_backend/constant_emitter.dart

Issue 944863002: dart2js: don't emit big integers as integer, but instead use their exponential representation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 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: pkg/compiler/lib/src/js_backend/constant_emitter.dart
diff --git a/pkg/compiler/lib/src/js_backend/constant_emitter.dart b/pkg/compiler/lib/src/js_backend/constant_emitter.dart
index 2773144dbc285b195e1ca13073a83b28f6c2610b..d4e57fb4717df427d4bd7143626ba143052935b1 100644
--- a/pkg/compiler/lib/src/js_backend/constant_emitter.dart
+++ b/pkg/compiler/lib/src/js_backend/constant_emitter.dart
@@ -202,7 +202,22 @@ class ConstantLiteralEmitter
@override
jsAst.Expression visitInt(IntConstantValue constant, [_]) {
- return new jsAst.LiteralNumber('${constant.primitiveValue}');
+ int primitiveValue = constant.primitiveValue;
+ // Since we are in JavaScript we can shorten long integers to their
+ // shorter exponential representation.
+ // For example: "1e+4" is shorter than "10000".
sra1 2015/02/20 17:02:54 The + is not necessary, making "1e3" shorter than
floitsch 2015/02/20 20:23:20 https://codereview.chromium.org/938323003
+ //
+ // Note that this shortening apparently loses precision for big numbers
+ // (like 1234567890123456789012345 which becomes 1.2345678901234568e+24).
+ // However, since JavaScript engines implicitly convert to double, these
sra1 2015/02/20 17:02:54 nit. They don't 'implicitly convert to' so much as
floitsch 2015/02/20 20:23:20 done in https://codereview.chromium.org/938323003
+ // digits are lost anyway.
+ if (primitiveValue.abs() >= 10000) {
+ String exponential = primitiveValue.toStringAsExponential();
+ String decimal = primitiveValue.toString();
+ return new jsAst.LiteralNumber(
+ (exponential.length < decimal.length) ? exponential : decimal);
+ }
+ return new jsAst.LiteralNumber('$primitiveValue');
}
@override
« no previous file with comments | « no previous file | tests/compiler/dart2js/number_output_test.dart » ('j') | tests/compiler/dart2js/number_output_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698