Changeset 91966 in webkit for trunk/Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp
- Timestamp:
- Jul 28, 2011, 5:43:15 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp
r91938 r91966 461 461 { 462 462 JSValue value = exec->argument(0); 463 464 // Optimized case: If the argument is a positive number in the integer 465 // range, and the exponent is 10, then parseInt is equivalent to floor. 463 JSValue radixValue = exec->argument(1); 464 465 // Optimized handling for numbers: 466 // If the argument is 0 or a number in range 10^-6 <= n < INT_MAX+1, then parseInt 467 // results in a truncation to integer. In the case of -0, this is converted to 0. 468 // 469 // This is also a truncation for values in the range INT_MAX+1 <= n < 10^21, 470 // however these values cannot be trivially truncated to int since 10^21 exceeds 471 // even the int64_t range. Negative numbers are a little trickier, the case for 472 // values in the range -10^21 < n <= -1 are similar to those for integer, but 473 // values in the range -1 < n <= -10^-6 need to truncate to -0, not 0. 474 static const double tenToTheMinus6 = 0.000001; 475 static const double intMaxPlusOne = 2147483648.0; 466 476 double n; 467 if (value.getNumber(n) && n >= 0 && n < INT_MAX && exec->argument(1).isUndefined()) 468 return JSValue::encode(jsNumber(static_cast<int>(n))); 469 470 int32_t radix = exec->argument(1).toInt32(exec); 471 472 if (radix != 0 && radix != 10) 473 return JSValue::encode(jsNumber(parseInt(value.toString(exec), radix))); 474 475 if (value.isInt32()) 476 return JSValue::encode(value); 477 478 if (value.isDouble()) { 479 double d = value.asDouble(); 480 if (isfinite(d)) 481 return JSValue::encode(jsNumber((d > 0) ? floor(d) : ceil(d))); 482 return JSValue::encode(jsNaN()); 483 } 484 485 return JSValue::encode(jsNumber(parseInt(value.toString(exec), radix))); 477 if (value.getNumber(n) && ((n < intMaxPlusOne && n >= tenToTheMinus6) || !n) && radixValue.isUndefinedOrNull()) 478 return JSValue::encode(jsNumber(static_cast<int32_t>(n))); 479 480 // If ToString throws, we shouldn't call ToInt32. 481 UString s = value.toString(exec); 482 if (exec->hadException()) 483 return JSValue::encode(jsUndefined()); 484 485 return JSValue::encode(jsNumber(parseInt(s, radixValue.toInt32(exec)))); 486 486 } 487 487
Note:
See TracChangeset
for help on using the changeset viewer.