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

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 | tests/compiler/dart2js_extra/constant_javascript_semantics4_test.dart » ('j') | 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] fits into a double without losing 180 * Returns true if [value] will turn into NaN or infinity
181 * precision. 181 * at runtime.
182 */ 182 */
183 bool integerFitsIntoDouble(int value) { 183 bool integerBecomesNanOrInfinity(int value) {
184 int absValue = value.abs(); 184 double doubleValue = value.toDouble();
185 double doubleValue = absValue.toDouble(); 185 return doubleValue.isNaN || doubleValue.isInfinite;
186 if (doubleValue.isNaN || doubleValue.isInfinite) return false;
187 return value.toDouble().floor().toInt() == value;
188 } 186 }
189 187
190 NumConstant convertToJavaScriptConstant(NumConstant constant) { 188 NumConstant convertToJavaScriptConstant(NumConstant constant) {
191 if (constant.isInt()) { 189 if (constant.isInt()) {
192 IntConstant intConstant = constant; 190 IntConstant intConstant = constant;
193 int intValue = intConstant.value; 191 int intValue = intConstant.value;
194 if (!integerFitsIntoDouble(intValue)) { 192 if (integerBecomesNanOrInfinity(intValue)) {
195 return new DoubleConstant(intValue.toDouble()); 193 return new DoubleConstant(intValue.toDouble());
196 } 194 }
195 // If the integer loses precision with JavaScript numbers, use
196 // the floored version JavaScript will use.
197 int floorValue = intValue.toDouble().floor().toInt();
198 if (floorValue != intValue) {
199 return new IntConstant(floorValue);
200 }
197 } else if (constant.isDouble()) { 201 } else if (constant.isDouble()) {
198 DoubleConstant doubleResult = constant; 202 DoubleConstant doubleResult = constant;
199 double doubleValue = doubleResult.value; 203 double doubleValue = doubleResult.value;
200 if (!doubleValue.isInfinite && !doubleValue.isNaN && 204 if (!doubleValue.isInfinite && !doubleValue.isNaN &&
201 !constant.isMinusZero()) { 205 !constant.isMinusZero()) {
202 int intValue = doubleValue.truncate(); 206 int intValue = doubleValue.truncate();
203 if (intValue == doubleValue && integerFitsIntoDouble(intValue)) { 207 if (intValue == doubleValue) {
204 return new IntConstant(intValue); 208 return new IntConstant(intValue);
205 } 209 }
206 } 210 }
207 } 211 }
208 return constant; 212 return constant;
209 } 213 }
210 214
211 NumConstant createInt(int i) 215 NumConstant createInt(int i)
212 => convertToJavaScriptConstant(new IntConstant(i)); 216 => convertToJavaScriptConstant(new IntConstant(i));
213 NumConstant createInt32(int i) => new IntConstant(i & BITS32); 217 NumConstant createInt32(int i) => new IntConstant(i & BITS32);
(...skipping 16 matching lines...) Expand all
230 // At runtime, an integer is both an integer and a double: the 234 // At runtime, an integer is both an integer and a double: the
231 // integer type check is Math.floor, which will return true only 235 // integer type check is Math.floor, which will return true only
232 // for real integers, and our double type check is 'typeof number' 236 // for real integers, and our double type check is 'typeof number'
233 // which will return true for both integers and doubles. 237 // which will return true for both integers and doubles.
234 if (s.element == compiler.intClass && t.element == compiler.doubleClass) { 238 if (s.element == compiler.intClass && t.element == compiler.doubleClass) {
235 return true; 239 return true;
236 } 240 }
237 return compiler.types.isSubtype(s, t); 241 return compiler.types.isSubtype(s, t);
238 } 242 }
239 } 243 }
OLDNEW
« no previous file with comments | « no previous file | tests/compiler/dart2js_extra/constant_javascript_semantics4_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698