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); | |
jsbell
2013/06/13 16:51:31
These two lines can be simplified to just: return
do-not-use
2013/06/13 19:11:20
AFAIK, modulus behavior is only guaranteed for uns
jsbell
2013/06/13 19:14:23
Right, sorry. Carry on!
| |
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); | |
jsbell
2013/06/13 16:51:31
Alternately, this line could be: return static_cas
| |
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 return static_cast<uint8_t>(result % 256); // 2^8. | |
jsbell
2013/06/13 16:51:31
The modulus should not be necessary here.
do-not-use
2013/06/13 19:11:20
Yes, here it is not technically required since the
| |
191 } | |
192 | |
193 // Can the value be converted to a number? | |
194 v8::Local<v8::Number> numberObject = value->ToNumber(); | |
195 if (numberObject.IsEmpty()) { | |
196 ok = false; | |
197 return 0; | |
198 } | |
199 | |
200 if (configuration == EnforceRange) | |
201 return enforceRange(numberObject->Value(), 0, kMaxUInt8, ok); | |
202 | |
203 // Does the value convert to nan or to an infinity? | |
204 double numberValue = numberObject->Value(); | |
205 if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue) | |
206 return 0; | |
207 | |
208 numberValue = numberValue < 0 ? -floor(abs(numberValue)) : floor(abs(numberV alue)); | |
209 return static_cast<uint8_t>(fmod(numberValue, 256)); // 2^8. | |
210 } | |
211 | |
137 int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf iguration, bool& ok) | 212 int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf iguration, bool& ok) |
138 { | 213 { |
139 ok = true; | 214 ok = true; |
140 | 215 |
141 // Fast case. The value is already a 32-bit integer. | 216 // Fast case. The value is already a 32-bit integer. |
142 if (value->IsInt32()) | 217 if (value->IsInt32()) |
143 return value->Int32Value(); | 218 return value->Int32Value(); |
144 | 219 |
145 // Can the value be converted to a number? | 220 // Can the value be converted to a number? |
146 v8::Local<v8::Number> numberObject = value->ToNumber(); | 221 v8::Local<v8::Number> numberObject = value->ToNumber(); |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
427 if (!DOMWrapperWorld::isolatedWorldsExist()) | 502 if (!DOMWrapperWorld::isolatedWorldsExist()) |
428 return MainWorld; | 503 return MainWorld; |
429 ASSERT(!v8::Context::GetEntered().IsEmpty()); | 504 ASSERT(!v8::Context::GetEntered().IsEmpty()); |
430 DOMWrapperWorld* isolatedWorld = DOMWrapperWorld::isolatedWorld(v8::Context: :GetEntered()); | 505 DOMWrapperWorld* isolatedWorld = DOMWrapperWorld::isolatedWorld(v8::Context: :GetEntered()); |
431 if (isolatedWorld) | 506 if (isolatedWorld) |
432 return IsolatedWorld; | 507 return IsolatedWorld; |
433 return MainWorld; | 508 return MainWorld; |
434 } | 509 } |
435 | 510 |
436 } // namespace WebCore | 511 } // namespace WebCore |
OLD | NEW |