Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 | 137 |
| 138 // FIXME: Should pass in appropriate creationContext | 138 // FIXME: Should pass in appropriate creationContext |
| 139 v8::Handle<v8::Object> filterWrapper = toV8(filter, v8::Handle<v8::Object>() , isolate).As<v8::Object>(); | 139 v8::Handle<v8::Object> filterWrapper = toV8(filter, v8::Handle<v8::Object>() , isolate).As<v8::Object>(); |
| 140 | 140 |
| 141 RefPtr<NodeFilterCondition> condition = V8NodeFilterCondition::create(callba ck, filterWrapper, isolate); | 141 RefPtr<NodeFilterCondition> condition = V8NodeFilterCondition::create(callba ck, filterWrapper, isolate); |
| 142 filter->setCondition(condition.release()); | 142 filter->setCondition(condition.release()); |
| 143 | 143 |
| 144 return filter.release(); | 144 return filter.release(); |
| 145 } | 145 } |
| 146 | 146 |
| 147 static const int8_t kMaxInt8 = 127; | |
| 148 static const int8_t kMinInt8 = -128; | |
| 149 static const uint8_t kMaxUInt8 = 255; | |
| 150 const int32_t kMaxInt32 = 0x7fffffff; | 147 const int32_t kMaxInt32 = 0x7fffffff; |
| 151 const int32_t kMinInt32 = -kMaxInt32 - 1; | 148 const int32_t kMinInt32 = -kMaxInt32 - 1; |
| 152 const uint32_t kMaxUInt32 = 0xffffffff; | 149 const uint32_t kMaxUInt32 = 0xffffffff; |
| 153 const int64_t kJSMaxInteger = 0x20000000000000LL - 1; // 2^53 - 1, maximum integ er exactly representable in ECMAScript. | 150 const int64_t kJSMaxInteger = 0x20000000000000LL - 1; // 2^53 - 1, maximum integ er exactly representable in ECMAScript. |
| 154 | 151 |
| 155 static double enforceRange(double x, double minimum, double maximum, bool& ok) | 152 static double enforceRange(double x, double minimum, double maximum, bool& ok) |
| 156 { | 153 { |
| 157 if (std::isnan(x) || std::isinf(x)) { | 154 if (std::isnan(x) || std::isinf(x)) { |
| 158 ok = false; | 155 ok = false; |
| 159 return 0; | 156 return 0; |
| 160 } | 157 } |
| 161 x = trunc(x); | 158 x = trunc(x); |
| 162 if (x < minimum || x > maximum) { | 159 if (x < minimum || x > maximum) { |
| 163 ok = false; | 160 ok = false; |
| 164 return 0; | 161 return 0; |
| 165 } | 162 } |
| 166 return x; | 163 return x; |
| 167 } | 164 } |
| 168 | 165 |
| 169 int8_t toInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration config uration, bool& ok) | 166 template <typename T> |
| 167 struct IntTypeLimits { | |
| 168 }; | |
| 169 | |
| 170 template <> | |
| 171 struct IntTypeLimits<int8_t> { | |
| 172 static const int8_t minValue = -128; | |
| 173 static const int8_t maxValue = 127; | |
| 174 static const unsigned numberOfValues = 256; // 2^8 | |
| 175 }; | |
| 176 | |
| 177 template <> | |
| 178 struct IntTypeLimits<uint8_t> { | |
| 179 static const uint8_t maxValue = 255; | |
| 180 static const unsigned numberOfValues = 256; // 2^8 | |
| 181 }; | |
| 182 | |
| 183 template <> | |
| 184 struct IntTypeLimits<int16_t> { | |
| 185 static const short minValue = -32768; | |
| 186 static const short maxValue = 32767; | |
| 187 static const unsigned numberOfValues = 65536; // 2^16 | |
| 188 }; | |
| 189 | |
| 190 template <> | |
| 191 struct IntTypeLimits<uint16_t> { | |
| 192 static const unsigned short maxValue = 65535; | |
| 193 static const unsigned numberOfValues = 65536; // 2^16 | |
| 194 }; | |
| 195 | |
| 196 template <typename T> | |
| 197 static inline T toSmallerInt(v8::Handle<v8::Value> value, IntegerConversionConfi guration configuration, bool& ok) | |
| 170 { | 198 { |
| 199 typedef IntTypeLimits<T> LimitsTrait; | |
| 171 ok = true; | 200 ok = true; |
| 172 | 201 |
| 173 // Fast case. The value is already a 32-bit integer in the right range. | 202 // Fast case. The value is already a 32-bit integer in the right range. |
| 174 if (value->IsInt32()) { | 203 if (value->IsInt32()) { |
| 175 int32_t result = value->Int32Value(); | 204 int32_t result = value->Int32Value(); |
| 176 if (result >= kMinInt8 && result <= kMaxInt8) | 205 if (result >= LimitsTrait::minValue && result <= LimitsTrait::maxValue) |
| 177 return static_cast<int8_t>(result); | 206 return static_cast<T>(result); |
| 178 if (configuration == EnforceRange) { | 207 if (configuration == EnforceRange) { |
| 179 ok = false; | 208 ok = false; |
| 180 return 0; | 209 return 0; |
| 181 } | 210 } |
| 182 result %= 256; // 2^8. | 211 result %= LimitsTrait::numberOfValues; |
| 183 return static_cast<int8_t>(result > kMaxInt8 ? result - 256 : result); | 212 return static_cast<T>(result > LimitsTrait::maxValue ? result - LimitsTr ait::numberOfValues : result); |
| 184 } | 213 } |
| 185 | 214 |
| 186 // Can the value be converted to a number? | 215 // Can the value be converted to a number? |
| 187 v8::Local<v8::Number> numberObject = value->ToNumber(); | 216 v8::Local<v8::Number> numberObject = value->ToNumber(); |
| 188 if (numberObject.IsEmpty()) { | 217 if (numberObject.IsEmpty()) { |
| 189 ok = false; | 218 ok = false; |
| 190 return 0; | 219 return 0; |
| 191 } | 220 } |
| 192 | 221 |
| 193 if (configuration == EnforceRange) | 222 if (configuration == EnforceRange) |
| 194 return enforceRange(numberObject->Value(), kMinInt8, kMaxInt8, ok); | 223 return enforceRange(numberObject->Value(), LimitsTrait::minValue, Limits Trait::maxValue, ok); |
| 195 | 224 |
| 196 double numberValue = numberObject->Value(); | 225 double numberValue = numberObject->Value(); |
| 197 if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue) | 226 if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue) |
| 198 return 0; | 227 return 0; |
| 199 | 228 |
| 200 numberValue = numberValue < 0 ? -floor(abs(numberValue)) : floor(abs(numberV alue)); | 229 numberValue = numberValue < 0 ? -floor(fabs(numberValue)) : floor(fabs(numbe rValue)); |
|
Inactive
2013/11/04 00:45:59
I actually found a long-standing bug where we were
| |
| 201 numberValue = fmod(numberValue, 256); // 2^8. | 230 numberValue = fmod(numberValue, LimitsTrait::numberOfValues); |
| 202 | 231 |
| 203 return static_cast<int8_t>(numberValue > kMaxInt8 ? numberValue - 256 : numb erValue); | 232 return static_cast<T>(numberValue > LimitsTrait::maxValue ? numberValue - Li mitsTrait::numberOfValues : numberValue); |
| 204 } | 233 } |
| 205 | 234 |
| 206 uint8_t toUInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf iguration, bool& ok) | 235 template <typename T> |
| 236 static inline T toSmallerUInt(v8::Handle<v8::Value> value, IntegerConversionConf iguration configuration, bool& ok) | |
| 207 { | 237 { |
| 238 typedef IntTypeLimits<T> LimitsTrait; | |
| 208 ok = true; | 239 ok = true; |
| 209 | 240 |
| 210 // Fast case. The value is a 32-bit signed integer - possibly positive? | 241 // Fast case. The value is a 32-bit signed integer - possibly positive? |
| 211 if (value->IsInt32()) { | 242 if (value->IsInt32()) { |
| 212 int32_t result = value->Int32Value(); | 243 int32_t result = value->Int32Value(); |
| 213 if (result >= 0 && result <= kMaxUInt8) | 244 if (result >= 0 && result <= LimitsTrait::maxValue) |
| 214 return static_cast<uint8_t>(result); | 245 return static_cast<T>(result); |
| 215 if (configuration == EnforceRange) { | 246 if (configuration == EnforceRange) { |
| 216 ok = false; | 247 ok = false; |
| 217 return 0; | 248 return 0; |
| 218 } | 249 } |
| 219 // Converting to uint8_t will cause the resulting value to be the value modulo 2^8. | 250 return static_cast<T>(result); |
| 220 return static_cast<uint8_t>(result); | |
| 221 } | 251 } |
| 222 | 252 |
| 223 // Can the value be converted to a number? | 253 // Can the value be converted to a number? |
| 224 v8::Local<v8::Number> numberObject = value->ToNumber(); | 254 v8::Local<v8::Number> numberObject = value->ToNumber(); |
| 225 if (numberObject.IsEmpty()) { | 255 if (numberObject.IsEmpty()) { |
| 226 ok = false; | 256 ok = false; |
| 227 return 0; | 257 return 0; |
| 228 } | 258 } |
| 229 | 259 |
| 230 if (configuration == EnforceRange) | 260 if (configuration == EnforceRange) |
| 231 return enforceRange(numberObject->Value(), 0, kMaxUInt8, ok); | 261 return enforceRange(numberObject->Value(), 0, LimitsTrait::maxValue, ok) ; |
| 232 | 262 |
| 233 // Does the value convert to nan or to an infinity? | 263 // Does the value convert to nan or to an infinity? |
| 234 double numberValue = numberObject->Value(); | 264 double numberValue = numberObject->Value(); |
| 235 if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue) | 265 if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue) |
| 236 return 0; | 266 return 0; |
| 237 | 267 |
| 238 numberValue = numberValue < 0 ? -floor(abs(numberValue)) : floor(abs(numberV alue)); | 268 numberValue = numberValue < 0 ? -floor(fabs(numberValue)) : floor(fabs(numbe rValue)); |
| 239 return static_cast<uint8_t>(fmod(numberValue, 256)); // 2^8. | 269 return static_cast<T>(fmod(numberValue, LimitsTrait::numberOfValues)); |
| 270 } | |
| 271 | |
| 272 int8_t toInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration config uration, bool& ok) | |
| 273 { | |
| 274 return toSmallerInt<int8_t>(value, configuration, ok); | |
| 275 } | |
| 276 | |
| 277 uint8_t toUInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf iguration, bool& ok) | |
| 278 { | |
| 279 return toSmallerUInt<uint8_t>(value, configuration, ok); | |
| 280 } | |
| 281 | |
| 282 int16_t toInt16(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf iguration, bool& ok) | |
| 283 { | |
| 284 return toSmallerInt<int16_t>(value, configuration, ok); | |
| 285 } | |
| 286 | |
| 287 uint16_t toUInt16(v8::Handle<v8::Value> value, IntegerConversionConfiguration co nfiguration, bool& ok) | |
| 288 { | |
| 289 return toSmallerUInt<uint16_t>(value, configuration, ok); | |
| 240 } | 290 } |
| 241 | 291 |
| 242 int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf iguration, bool& ok) | 292 int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf iguration, bool& ok) |
| 243 { | 293 { |
| 244 ok = true; | 294 ok = true; |
| 245 | 295 |
| 246 // Fast case. The value is already a 32-bit integer. | 296 // Fast case. The value is already a 32-bit integer. |
| 247 if (value->IsInt32()) | 297 if (value->IsInt32()) |
| 248 return value->Int32Value(); | 298 return value->Int32Value(); |
| 249 | 299 |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 568 return mainThreadIsolate(); | 618 return mainThreadIsolate(); |
| 569 return v8::Isolate::GetCurrent(); | 619 return v8::Isolate::GetCurrent(); |
| 570 } | 620 } |
| 571 | 621 |
| 572 v8::Isolate* toIsolate(Frame* frame) | 622 v8::Isolate* toIsolate(Frame* frame) |
| 573 { | 623 { |
| 574 return frame->script().isolate(); | 624 return frame->script().isolate(); |
| 575 } | 625 } |
| 576 | 626 |
| 577 } // namespace WebCore | 627 } // namespace WebCore |
| OLD | NEW |