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 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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 const TruncatingDivideOperation()); | 178 const TruncatingDivideOperation()); |
179 | 179 |
180 const JavaScriptConstantSystem(); | 180 const JavaScriptConstantSystem(); |
181 | 181 |
182 /** | 182 /** |
183 * Returns true if the given [value] fits into a double without losing | 183 * Returns true if the given [value] fits into a double without losing |
184 * precision. | 184 * precision. |
185 */ | 185 */ |
186 bool integerFitsIntoDouble(int value) { | 186 bool integerFitsIntoDouble(int value) { |
187 int absValue = value.abs(); | 187 int absValue = value.abs(); |
188 return (absValue & BITS53) == absValue; | 188 return (absValue & BITS53) == absValue; |
Lasse Reichstein Nielsen
2013/01/04 10:29:42
Why not just check (absValue <= BITS53) ?
| |
189 } | 189 } |
190 | 190 |
191 NumConstant convertToJavaScriptConstant(NumConstant constant) { | 191 NumConstant convertToJavaScriptConstant(NumConstant constant) { |
192 if (constant.isInt()) { | 192 if (constant.isInt()) { |
193 IntConstant intConstant = constant; | 193 IntConstant intConstant = constant; |
194 int intValue = intConstant.value; | 194 int intValue = intConstant.value; |
195 if (!integerFitsIntoDouble(intValue)) { | 195 if (!integerFitsIntoDouble(intValue)) { |
196 return new DoubleConstant(intValue.toDouble()); | 196 return new DoubleConstant(intValue.toDouble()); |
197 } | 197 } |
198 } else if (constant.isDouble()) { | 198 } else if (constant.isDouble()) { |
199 DoubleConstant doubleResult = constant; | 199 DoubleConstant doubleResult = constant; |
200 double doubleValue = doubleResult.value; | 200 double doubleValue = doubleResult.value; |
201 if (!doubleValue.isInfinite && !doubleValue.isNaN && | 201 if (!doubleValue.isInfinite && !doubleValue.isNaN && |
202 !constant.isMinusZero()) { | 202 !constant.isMinusZero()) { |
203 int intValue = doubleValue.toInt(); | 203 int intValue = doubleValue.truncate(); |
204 if (intValue == doubleValue && integerFitsIntoDouble(intValue)) { | 204 if (intValue == doubleValue && integerFitsIntoDouble(intValue)) { |
205 return new IntConstant(intValue); | 205 return new IntConstant(intValue); |
206 } | 206 } |
207 } | 207 } |
208 } | 208 } |
209 return constant; | 209 return constant; |
210 } | 210 } |
211 | 211 |
212 NumConstant createInt(int i) | 212 NumConstant createInt(int i) |
213 => convertToJavaScriptConstant(new IntConstant(i)); | 213 => convertToJavaScriptConstant(new IntConstant(i)); |
(...skipping 17 matching lines...) Expand all Loading... | |
231 // At runtime, an integer is both an integer and a double: the | 231 // At runtime, an integer is both an integer and a double: the |
232 // integer type check is Math.floor, which will return true only | 232 // integer type check is Math.floor, which will return true only |
233 // for real integers, and our double type check is 'typeof number' | 233 // for real integers, and our double type check is 'typeof number' |
234 // which will return true for both integers and doubles. | 234 // which will return true for both integers and doubles. |
235 if (s.element == compiler.intClass && t.element == compiler.doubleClass) { | 235 if (s.element == compiler.intClass && t.element == compiler.doubleClass) { |
236 return true; | 236 return true; |
237 } | 237 } |
238 return compiler.types.isSubtype(s, t); | 238 return compiler.types.isSubtype(s, t); |
239 } | 239 } |
240 } | 240 } |
OLD | NEW |