| Index: src/conversions-inl.h
|
| diff --git a/src/conversions-inl.h b/src/conversions-inl.h
|
| index cb7dbf88d2a32d7f132fcc5e9e76da60102b461e..eed64cd26f89668b86122c4963a261a2947d9475 100644
|
| --- a/src/conversions-inl.h
|
| +++ b/src/conversions-inl.h
|
| @@ -1,4 +1,4 @@
|
| -// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
| +// Copyright 2011 the V8 project authors. All rights reserved.
|
| // Redistribution and use in source and binary forms, with or without
|
| // modification, are permitted provided that the following conditions are
|
| // met:
|
| @@ -77,6 +77,20 @@ static inline double DoubleToInteger(double x) {
|
| }
|
|
|
|
|
| +uint8_t ClampToUInt8(double double_value) {
|
| + if (!(double_value > 0)) {
|
| + // NaN and less than zero clamp to zero.
|
| + return 0;
|
| + } else if (double_value > 255) {
|
| + // Greater than 255 clamp to 255.
|
| + return 255;
|
| + } else {
|
| + // Other doubles are rounded to the nearest integer.
|
| + return static_cast<uint8_t>(double_value + 0.5);
|
| + }
|
| +}
|
| +
|
| +
|
| int32_t NumberToInt32(Object* number) {
|
| if (number->IsSmi()) return Smi::cast(number)->value();
|
| return DoubleToInt32(number->Number());
|
| @@ -89,6 +103,28 @@ uint32_t NumberToUint32(Object* number) {
|
| }
|
|
|
|
|
| +uint8_t NumberToClampedUInt8(Object* number)
|
| +{
|
| + if (number->IsSmi()) {
|
| + int32_t value = Smi::cast(number)->value();
|
| + if (value < 0) {
|
| + return 0;
|
| + } else if (value > 255) {
|
| + return 255;
|
| + } else {
|
| + return static_cast<uint8_t>(value);
|
| + }
|
| + } else if (number->IsHeapNumber()) {
|
| + return ClampToUInt8(number->Number());
|
| + } else {
|
| + // Clamp undefined to zero (default). All other types have been
|
| + // converted to a number type further up in the call chain.
|
| + ASSERT(number->IsUndefined());
|
| + return 0;
|
| + }
|
| +}
|
| +
|
| +
|
| int32_t DoubleToInt32(double x) {
|
| int32_t i = FastD2I(x);
|
| if (FastI2D(i) == x) return i;
|
|
|