| 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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 compiler.internalError(NO_LOCATION_SPANNABLE, | 193 compiler.internalError(NO_LOCATION_SPANNABLE, |
| 194 "The function constant does not need specific JS code."); | 194 "The function constant does not need specific JS code."); |
| 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 static final _exponentialRE = new RegExp( |
| 204 '^' |
| 205 '\([-+]?\)' // 1: sign |
| 206 '\([0-9]+\)' // 2: leading digit(s) |
| 207 '\(\.\([0-9]*\)\)?' // 4: fraction digits |
| 208 'e\([-+]?[0-9]+\)' // 5: exponent with sign |
| 209 r'$'); |
| 210 |
| 203 /// Reduces the size of exponential representations when minification is | 211 /// Reduces the size of exponential representations when minification is |
| 204 /// enabled. | 212 /// enabled. |
| 205 /// | 213 /// |
| 206 /// Removes the "+" after the exponential sign, and removes the "." before the | 214 /// Removes the "+" after the exponential sign, and removes the "." before the |
| 207 /// "e". For example `1.23e+5` is changed to `123e3`. | 215 /// "e". For example `1.23e+5` is changed to `123e3`. |
| 208 String _shortenExponentialRepresentation(String numberString) { | 216 String _shortenExponentialRepresentation(String numberString) { |
| 209 if (numberString.length < 4) return numberString; | 217 Match match = _exponentialRE.firstMatch(numberString); |
| 210 if (numberString[1] == "e" && numberString[2] == "+") { | 218 if (match == null) return numberString; |
| 211 // For example: "1e+5". Remove the "+". | 219 String sign = match[1]; |
| 212 return "${numberString[0]}e${numberString.substring(2)}"; | 220 String leadingDigits = match[2]; |
| 213 } | 221 String fractionDigits = match[4]; |
| 214 if (numberString[1] != ".") return numberString; | 222 int exponent = int.parse(match[5]); |
| 215 int digitsAfterDotCount = 0; | 223 if (fractionDigits == null) fractionDigits = ''; |
| 216 int pos = 2; | 224 exponent -= fractionDigits.length; |
| 217 while (pos < numberString.length && numberString[pos] != "e") { | 225 String result = '${sign}${leadingDigits}${fractionDigits}e${exponent}'; |
| 218 pos++; | |
| 219 digitsAfterDotCount++; | |
| 220 } | |
| 221 if (pos >= numberString.length) return numberString; | |
| 222 int exponent = int.parse(numberString.substring(pos + 1)); | |
| 223 if (exponent <= digitsAfterDotCount) return numberString; | |
| 224 String digitsAfterDot = numberString.substring(2, pos); | |
| 225 int shiftedExponent = exponent - digitsAfterDotCount; | |
| 226 String result = "${numberString[0]}${digitsAfterDot}e$shiftedExponent"; | |
| 227 assert(double.parse(result) == double.parse(numberString)); | 226 assert(double.parse(result) == double.parse(numberString)); |
| 228 return result; | 227 return result; |
| 229 } | 228 } |
| 230 | 229 |
| 231 @override | 230 @override |
| 232 jsAst.Expression visitInt(IntConstantValue constant, [_]) { | 231 jsAst.Expression visitInt(IntConstantValue constant, [_]) { |
| 233 int primitiveValue = constant.primitiveValue; | 232 int primitiveValue = constant.primitiveValue; |
| 234 // Since we are in JavaScript we can shorten long integers to their | 233 // Since we are in JavaScript we can shorten long integers to their shorter |
| 235 // shorter exponential representation. | 234 // exponential representation, for example: "1e4" is shorter than "10000". |
| 236 // For example: "1e+4" is shorter than "10000". | |
| 237 // | 235 // |
| 238 // Note that this shortening apparently loses precision for big numbers | 236 // Note that this shortening apparently loses precision for big numbers |
| 239 // (like 1234567890123456789012345 which becomes 12345678901234568e8). | 237 // (like 1234567890123456789012345 which becomes 12345678901234568e8). |
| 240 // However, since JavaScript engines represent all numbers as doubles, | 238 // However, since JavaScript engines represent all numbers as doubles, these |
| 241 // these digits are lost anyway. | 239 // digits are lost anyway. |
| 242 int cutOffValue = compiler.enableMinification ? 10000 : 1e20.toInt(); | 240 String representation = primitiveValue.toString(); |
| 243 if (primitiveValue.abs() >= cutOffValue) { | 241 String alternative = null; |
| 244 String exponential = _shortenExponentialRepresentation( | 242 |
| 243 int cutoff = compiler.enableMinification ? 10000 : 1e10.toInt(); |
| 244 if (primitiveValue.abs() >= cutoff) { |
| 245 alternative = _shortenExponentialRepresentation( |
| 245 primitiveValue.toStringAsExponential()); | 246 primitiveValue.toStringAsExponential()); |
| 246 String decimal = primitiveValue.toString(); | |
| 247 return new jsAst.LiteralNumber( | |
| 248 (exponential.length < decimal.length) ? exponential : decimal); | |
| 249 } | 247 } |
| 250 return new jsAst.LiteralNumber('$primitiveValue'); | 248 if (alternative != null && alternative.length < representation.length) { |
| 249 representation = alternative; |
| 250 } |
| 251 return new jsAst.LiteralNumber(representation); |
| 251 } | 252 } |
| 252 | 253 |
| 253 @override | 254 @override |
| 254 jsAst.Expression visitDouble(DoubleConstantValue constant, [_]) { | 255 jsAst.Expression visitDouble(DoubleConstantValue constant, [_]) { |
| 255 double value = constant.primitiveValue; | 256 double value = constant.primitiveValue; |
| 256 if (value.isNaN) { | 257 if (value.isNaN) { |
| 257 return js("0/0"); | 258 return js("0/0"); |
| 258 } else if (value == double.INFINITY) { | 259 } else if (value == double.INFINITY) { |
| 259 return js("1/0"); | 260 return js("1/0"); |
| 260 } else if (value == -double.INFINITY) { | 261 } else if (value == -double.INFINITY) { |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 [value, argumentList]); | 445 [value, argumentList]); |
| 445 } | 446 } |
| 446 return value; | 447 return value; |
| 447 } | 448 } |
| 448 | 449 |
| 449 @override | 450 @override |
| 450 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) { | 451 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) { |
| 451 return constantEmitter.reference(constant.referenced); | 452 return constantEmitter.reference(constant.referenced); |
| 452 } | 453 } |
| 453 } | 454 } |
| OLD | NEW |