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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 for (size_t i = 0; i < length; ++i) | 108 for (size_t i = 0; i < length; ++i) |
109 result.append(args[i]); | 109 result.append(args[i]); |
110 return result; | 110 return result; |
111 } | 111 } |
112 | 112 |
113 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value> callback) | 113 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value> callback) |
114 { | 114 { |
115 return NodeFilter::create(V8NodeFilterCondition::create(callback)); | 115 return NodeFilter::create(V8NodeFilterCondition::create(callback)); |
116 } | 116 } |
117 | 117 |
| 118 static const int8_t kMaxInt8 = 127; |
| 119 static const int8_t kMinInt8 = -128; |
| 120 static const uint8_t kMaxUInt8 = 255; |
118 const int32_t kMaxInt32 = 0x7fffffff; | 121 const int32_t kMaxInt32 = 0x7fffffff; |
119 const int32_t kMinInt32 = -kMaxInt32 - 1; | 122 const int32_t kMinInt32 = -kMaxInt32 - 1; |
120 const uint32_t kMaxUInt32 = 0xffffffff; | 123 const uint32_t kMaxUInt32 = 0xffffffff; |
121 const int64_t kJSMaxInteger = 0x20000000000000LL - 1; // 2^53 - 1, maximum integ
er exactly representable in ECMAScript. | 124 const int64_t kJSMaxInteger = 0x20000000000000LL - 1; // 2^53 - 1, maximum integ
er exactly representable in ECMAScript. |
122 | 125 |
123 static double enforceRange(double x, double minimum, double maximum, bool& ok) | 126 static double enforceRange(double x, double minimum, double maximum, bool& ok) |
124 { | 127 { |
125 if (std::isnan(x) || std::isinf(x)) { | 128 if (std::isnan(x) || std::isinf(x)) { |
126 ok = false; | 129 ok = false; |
127 return 0; | 130 return 0; |
128 } | 131 } |
129 x = trunc(x); | 132 x = trunc(x); |
130 if (x < minimum || x > maximum) { | 133 if (x < minimum || x > maximum) { |
131 ok = false; | 134 ok = false; |
132 return 0; | 135 return 0; |
133 } | 136 } |
134 return x; | 137 return x; |
135 } | 138 } |
136 | 139 |
| 140 int8_t toInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration config
uration, bool& ok) |
| 141 { |
| 142 ok = true; |
| 143 |
| 144 // Fast case. The value is already a 32-bit integer in the right range. |
| 145 if (value->IsInt32()) { |
| 146 int32_t result = value->Int32Value(); |
| 147 if (result >= kMinInt8 && result <= kMaxInt8) |
| 148 return static_cast<int8_t>(result); |
| 149 if (configuration == EnforceRange) { |
| 150 ok = false; |
| 151 return 0; |
| 152 } |
| 153 result %= 256; // 2^8. |
| 154 return static_cast<int8_t>(result > kMaxInt8 ? result - 256 : result); |
| 155 } |
| 156 |
| 157 // Can the value be converted to a number? |
| 158 v8::Local<v8::Number> numberObject = value->ToNumber(); |
| 159 if (numberObject.IsEmpty()) { |
| 160 ok = false; |
| 161 return 0; |
| 162 } |
| 163 |
| 164 if (configuration == EnforceRange) |
| 165 return enforceRange(numberObject->Value(), kMinInt8, kMaxInt8, ok); |
| 166 |
| 167 double numberValue = numberObject->Value(); |
| 168 if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue) |
| 169 return 0; |
| 170 |
| 171 numberValue = numberValue < 0 ? -floor(abs(numberValue)) : floor(abs(numberV
alue)); |
| 172 numberValue = fmod(numberValue, 256); // 2^8. |
| 173 |
| 174 return static_cast<int8_t>(numberValue > kMaxInt8 ? numberValue - 256 : numb
erValue); |
| 175 } |
| 176 |
| 177 uint8_t toUInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf
iguration, bool& ok) |
| 178 { |
| 179 ok = true; |
| 180 |
| 181 // Fast case. The value is a 32-bit signed integer - possibly positive? |
| 182 if (value->IsInt32()) { |
| 183 int32_t result = value->Int32Value(); |
| 184 if (result >= 0 && result <= kMaxUInt8) |
| 185 return static_cast<uint8_t>(result); |
| 186 if (configuration == EnforceRange) { |
| 187 ok = false; |
| 188 return 0; |
| 189 } |
| 190 // Converting to uint8_t will cause the resulting value to be the value
modulo 2^8. |
| 191 return static_cast<uint8_t>(result); |
| 192 } |
| 193 |
| 194 // Can the value be converted to a number? |
| 195 v8::Local<v8::Number> numberObject = value->ToNumber(); |
| 196 if (numberObject.IsEmpty()) { |
| 197 ok = false; |
| 198 return 0; |
| 199 } |
| 200 |
| 201 if (configuration == EnforceRange) |
| 202 return enforceRange(numberObject->Value(), 0, kMaxUInt8, ok); |
| 203 |
| 204 // Does the value convert to nan or to an infinity? |
| 205 double numberValue = numberObject->Value(); |
| 206 if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue) |
| 207 return 0; |
| 208 |
| 209 numberValue = numberValue < 0 ? -floor(abs(numberValue)) : floor(abs(numberV
alue)); |
| 210 return static_cast<uint8_t>(fmod(numberValue, 256)); // 2^8. |
| 211 } |
| 212 |
137 int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf
iguration, bool& ok) | 213 int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf
iguration, bool& ok) |
138 { | 214 { |
139 ok = true; | 215 ok = true; |
140 | 216 |
141 // Fast case. The value is already a 32-bit integer. | 217 // Fast case. The value is already a 32-bit integer. |
142 if (value->IsInt32()) | 218 if (value->IsInt32()) |
143 return value->Int32Value(); | 219 return value->Int32Value(); |
144 | 220 |
145 // Can the value be converted to a number? | 221 // Can the value be converted to a number? |
146 v8::Local<v8::Number> numberObject = value->ToNumber(); | 222 v8::Local<v8::Number> numberObject = value->ToNumber(); |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 if (!DOMWrapperWorld::isolatedWorldsExist()) | 503 if (!DOMWrapperWorld::isolatedWorldsExist()) |
428 return MainWorld; | 504 return MainWorld; |
429 ASSERT(!v8::Context::GetEntered().IsEmpty()); | 505 ASSERT(!v8::Context::GetEntered().IsEmpty()); |
430 DOMWrapperWorld* isolatedWorld = DOMWrapperWorld::isolatedWorld(v8::Context:
:GetEntered()); | 506 DOMWrapperWorld* isolatedWorld = DOMWrapperWorld::isolatedWorld(v8::Context:
:GetEntered()); |
431 if (isolatedWorld) | 507 if (isolatedWorld) |
432 return IsolatedWorld; | 508 return IsolatedWorld; |
433 return MainWorld; | 509 return MainWorld; |
434 } | 510 } |
435 | 511 |
436 } // namespace WebCore | 512 } // namespace WebCore |
OLD | NEW |