Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 } |
| OLD | NEW |