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

Unified Diff: src/objects-inl.h

Issue 212663005: Fix FixedUint8ClampedArray::SetValue. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/objects.cc ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/objects-inl.h
diff --git a/src/objects-inl.h b/src/objects-inl.h
index c358ee6584525e4415aacce8615efd687acda79e..95f52b194c5cf2f2d4e8dca4541dfe83db78b495 100644
--- a/src/objects-inl.h
+++ b/src/objects-inl.h
@@ -3744,21 +3744,69 @@ MaybeObject* FixedTypedArray<Traits>::get(int index) {
return Traits::ToObject(GetHeap(), get_scalar(index));
}
+
+inline uint8_t clampObjectToUint8(Object* value) {
+ uint8_t clamped_value = 0;
+ if (value->IsSmi()) {
+ int int_value = Smi::cast(value)->value();
+ if (int_value < 0) {
+ clamped_value = 0;
+ } else if (int_value > 255) {
+ clamped_value = 255;
+ } else {
+ clamped_value = static_cast<uint8_t>(int_value);
+ }
+ } else if (value->IsHeapNumber()) {
+ double double_value = HeapNumber::cast(value)->value();
+ if (!(double_value > 0)) {
+ // NaN and less than zero clamp to zero.
+ clamped_value = 0;
+ } else if (double_value > 255) {
+ // Greater than 255 clamp to 255.
+ clamped_value = 255;
+ } else {
+ // Other doubles are rounded to the nearest integer.
+ clamped_value = static_cast<uint8_t>(lrint(double_value));
+ }
+ } else {
+ // Clamp undefined to zero (default). All other types have been
+ // converted to a number type further up in the call chain.
+ ASSERT(value->IsUndefined());
+ }
+ return clamped_value;
+}
+
+
+template<> inline
+uint8_t FixedTypedArray<Uint8ClampedArrayTraits>::castObject(Object* value) {
+ return clampObjectToUint8(value);
+}
+
+
+template <class Traits>
+typename Traits::ElementType FixedTypedArray<Traits>::castObject(
+ Object* value) {
+ ElementType result = Traits::defaultValue();
+ if (value->IsSmi()) {
+ int int_value = Smi::cast(value)->value();
+ result = static_cast<ElementType>(int_value);
+ } else if (value->IsHeapNumber()) {
+ double double_value = HeapNumber::cast(value)->value();
+ result = static_cast<ElementType>(DoubleToInt32(double_value));
+ } else {
+ // Clamp undefined to the default value. All other types have been
+ // converted to a number type further up in the call chain.
+ ASSERT(value->IsUndefined());
+ }
+ return result;
+}
+
+
template <class Traits>
MaybeObject* FixedTypedArray<Traits>::SetValue(uint32_t index, Object* value) {
ElementType cast_value = Traits::defaultValue();
if (index < static_cast<uint32_t>(length())) {
- if (value->IsSmi()) {
- int int_value = Smi::cast(value)->value();
- cast_value = static_cast<ElementType>(int_value);
- } else if (value->IsHeapNumber()) {
- double double_value = HeapNumber::cast(value)->value();
- cast_value = static_cast<ElementType>(DoubleToInt32(double_value));
- } else {
- // Clamp undefined to the default value. All other types have been
- // converted to a number type further up in the call chain.
- ASSERT(value->IsUndefined());
- }
+ cast_value = castObject(value);
set(index, cast_value);
}
return Traits::ToObject(GetHeap(), cast_value);
« no previous file with comments | « src/objects.cc ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698