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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of js_backend; 5 part of js_backend;
6 6
7 class ConstantEmitter { 7 class ConstantEmitter {
8 ConstantReferenceEmitter _referenceEmitter; 8 ConstantReferenceEmitter _referenceEmitter;
9 ConstantLiteralEmitter _literalEmitter; 9 ConstantLiteralEmitter _literalEmitter;
10 10
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 return null; 195 return null;
196 } 196 }
197 197
198 @override 198 @override
199 jsAst.Expression visitNull(NullConstantValue constant, [_]) { 199 jsAst.Expression visitNull(NullConstantValue constant, [_]) {
200 return new jsAst.LiteralNull(); 200 return new jsAst.LiteralNull();
201 } 201 }
202 202
203 @override 203 @override
204 jsAst.Expression visitInt(IntConstantValue constant, [_]) { 204 jsAst.Expression visitInt(IntConstantValue constant, [_]) {
205 return new jsAst.LiteralNumber('${constant.primitiveValue}'); 205 int primitiveValue = constant.primitiveValue;
206 // Since we are in JavaScript we can shorten long integers to their
207 // shorter exponential representation.
208 // 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
209 //
210 // Note that this shortening apparently loses precision for big numbers
211 // (like 1234567890123456789012345 which becomes 1.2345678901234568e+24).
212 // 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
213 // digits are lost anyway.
214 if (primitiveValue.abs() >= 10000) {
215 String exponential = primitiveValue.toStringAsExponential();
216 String decimal = primitiveValue.toString();
217 return new jsAst.LiteralNumber(
218 (exponential.length < decimal.length) ? exponential : decimal);
219 }
220 return new jsAst.LiteralNumber('$primitiveValue');
206 } 221 }
207 222
208 @override 223 @override
209 jsAst.Expression visitDouble(DoubleConstantValue constant, [_]) { 224 jsAst.Expression visitDouble(DoubleConstantValue constant, [_]) {
210 double value = constant.primitiveValue; 225 double value = constant.primitiveValue;
211 if (value.isNaN) { 226 if (value.isNaN) {
212 return js("0/0"); 227 return js("0/0");
213 } else if (value == double.INFINITY) { 228 } else if (value == double.INFINITY) {
214 return js("1/0"); 229 return js("1/0");
215 } else if (value == -double.INFINITY) { 230 } else if (value == -double.INFINITY) {
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 [value, argumentList]); 413 [value, argumentList]);
399 } 414 }
400 return value; 415 return value;
401 } 416 }
402 417
403 @override 418 @override
404 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) { 419 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) {
405 return constantEmitter.reference(constant.referenced); 420 return constantEmitter.reference(constant.referenced);
406 } 421 }
407 } 422 }
OLDNEW
« 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