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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/js_backend/constant_system_javascript.dart

Issue 19180002: A dart int literal is a dart2js' int unless it will become NaN or infinity. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 5 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 const JAVA_SCRIPT_CONSTANT_SYSTEM = const JavaScriptConstantSystem(); 7 const JAVA_SCRIPT_CONSTANT_SYSTEM = const JavaScriptConstantSystem();
8 8
9 class JavaScriptBitNotOperation extends BitNotOperation { 9 class JavaScriptBitNotOperation extends BitNotOperation {
10 const JavaScriptBitNotOperation(); 10 const JavaScriptBitNotOperation();
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 const JavaScriptBinaryBitOperation(const ShiftLeftOperation()); 170 const JavaScriptBinaryBitOperation(const ShiftLeftOperation());
171 final shiftRight = const JavaScriptShiftRightOperation(); 171 final shiftRight = const JavaScriptShiftRightOperation();
172 final subtract = 172 final subtract =
173 const JavaScriptBinaryArithmeticOperation(const SubtractOperation()); 173 const JavaScriptBinaryArithmeticOperation(const SubtractOperation());
174 final truncatingDivide = const JavaScriptBinaryArithmeticOperation( 174 final truncatingDivide = const JavaScriptBinaryArithmeticOperation(
175 const TruncatingDivideOperation()); 175 const TruncatingDivideOperation());
176 176
177 const JavaScriptConstantSystem(); 177 const JavaScriptConstantSystem();
178 178
179 /** 179 /**
180 * Returns true if the given [value] will turn into NaN or infinity
karlklose 2013/07/15 13:52:08 '... true if [value] will...'?
ngeoffray 2013/07/15 13:53:38 Done.
181 * at runtime.
182 */
183 bool integerBecomesNanOrInfinity(int value) {
184 int absValue = value.abs();
floitsch 2013/07/15 13:25:51 Why do you need the absolute value? isNaN and isIn
ngeoffray 2013/07/15 13:49:27 Done.
185 double doubleValue = absValue.toDouble();
186 return doubleValue.isNaN || doubleValue.isInfinite;
187 }
188
189 /**
180 * Returns true if the given [value] fits into a double without losing 190 * Returns true if the given [value] fits into a double without losing
181 * precision. 191 * precision.
182 */ 192 */
183 bool integerFitsIntoDouble(int value) { 193 bool integerFitsIntoDouble(int value) {
184 int absValue = value.abs(); 194 int absValue = value.abs();
185 double doubleValue = absValue.toDouble(); 195 double doubleValue = absValue.toDouble();
186 if (doubleValue.isNaN || doubleValue.isInfinite) return false; 196 if (doubleValue.isNaN || doubleValue.isInfinite) return false;
187 return value.toDouble().floor().toInt() == value; 197 return value.toDouble().floor().toInt() == value;
188 } 198 }
189 199
200
karlklose 2013/07/15 13:52:08 Remove extra line.
ngeoffray 2013/07/15 13:53:38 Done.
190 NumConstant convertToJavaScriptConstant(NumConstant constant) { 201 NumConstant convertToJavaScriptConstant(NumConstant constant) {
191 if (constant.isInt()) { 202 if (constant.isInt()) {
192 IntConstant intConstant = constant; 203 IntConstant intConstant = constant;
193 int intValue = intConstant.value; 204 int intValue = intConstant.value;
194 if (!integerFitsIntoDouble(intValue)) { 205 if (integerBecomesNanOrInfinity(intValue)) {
195 return new DoubleConstant(intValue.toDouble()); 206 return new DoubleConstant(intValue.toDouble());
196 } 207 }
208 // If the integer looses precision with JavaScript numbers, use
karlklose 2013/07/15 13:52:08 'looses' -> 'loses'.
ngeoffray 2013/07/15 13:53:38 Done.
209 // the floored version JavaScript will use.
210 int floorValue = intValue.toDouble().floor().toInt();
211 if (floorValue != intValue) {
212 return new IntConstant(floorValue);
213 }
197 } else if (constant.isDouble()) { 214 } else if (constant.isDouble()) {
198 DoubleConstant doubleResult = constant; 215 DoubleConstant doubleResult = constant;
199 double doubleValue = doubleResult.value; 216 double doubleValue = doubleResult.value;
200 if (!doubleValue.isInfinite && !doubleValue.isNaN && 217 if (!doubleValue.isInfinite && !doubleValue.isNaN &&
201 !constant.isMinusZero()) { 218 !constant.isMinusZero()) {
202 int intValue = doubleValue.truncate(); 219 int intValue = doubleValue.truncate();
203 if (intValue == doubleValue && integerFitsIntoDouble(intValue)) { 220 if (intValue == doubleValue && integerFitsIntoDouble(intValue)) {
204 return new IntConstant(intValue); 221 return new IntConstant(intValue);
205 } 222 }
206 } 223 }
(...skipping 23 matching lines...) Expand all
230 // At runtime, an integer is both an integer and a double: the 247 // At runtime, an integer is both an integer and a double: the
231 // integer type check is Math.floor, which will return true only 248 // integer type check is Math.floor, which will return true only
232 // for real integers, and our double type check is 'typeof number' 249 // for real integers, and our double type check is 'typeof number'
233 // which will return true for both integers and doubles. 250 // which will return true for both integers and doubles.
234 if (s.element == compiler.intClass && t.element == compiler.doubleClass) { 251 if (s.element == compiler.intClass && t.element == compiler.doubleClass) {
235 return true; 252 return true;
236 } 253 }
237 return compiler.types.isSubtype(s, t); 254 return compiler.types.isSubtype(s, t);
238 } 255 }
239 } 256 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698