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 - possibly in right rang e? | |
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 unsigned integer - possibly in right ran ge? | |
182 if (value->IsUint32()) { | |
haraken
2013/06/13 14:45:30
I'd remove this branch. The range that IsInt32() c
| |
183 uint32_t result = value->Uint32Value(); | |
184 if (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. | |
191 } | |
192 | |
193 // Fast case. The value is a 32-bit signed integer - possibly positive? | |
194 if (value->IsInt32()) { | |
195 int32_t result = value->Int32Value(); | |
196 if (result >= 0 && result <= kMaxUInt8) | |
197 return static_cast<uint8_t>(result); | |
198 if (configuration == EnforceRange) { | |
199 ok = false; | |
200 return 0; | |
201 } | |
202 return static_cast<uint8_t>(result % 256); // 2^8. | |
203 } | |
204 | |
205 // Can the value be converted to a number? | |
206 v8::Local<v8::Number> numberObject = value->ToNumber(); | |
207 if (numberObject.IsEmpty()) { | |
208 ok = false; | |
209 return 0; | |
210 } | |
211 | |
212 if (configuration == EnforceRange) | |
213 return enforceRange(numberObject->Value(), 0, kMaxUInt8, ok); | |
214 | |
215 // Does the value convert to nan or to an infinity? | |
216 double numberValue = numberObject->Value(); | |
217 if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue) | |
218 return 0; | |
219 | |
220 numberValue = numberValue < 0 ? -floor(abs(numberValue)) : floor(abs(numberV alue)); | |
221 return static_cast<uint8_t>(fmod(numberValue, 256)); // 2^8. | |
222 } | |
223 | |
137 int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf iguration, bool& ok) | 224 int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration conf iguration, bool& ok) |
138 { | 225 { |
139 ok = true; | 226 ok = true; |
140 | 227 |
141 // Fast case. The value is already a 32-bit integer. | 228 // Fast case. The value is already a 32-bit integer. |
142 if (value->IsInt32()) | 229 if (value->IsInt32()) |
143 return value->Int32Value(); | 230 return value->Int32Value(); |
144 | 231 |
145 // Can the value be converted to a number? | 232 // Can the value be converted to a number? |
146 v8::Local<v8::Number> numberObject = value->ToNumber(); | 233 v8::Local<v8::Number> numberObject = value->ToNumber(); |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
427 if (!DOMWrapperWorld::isolatedWorldsExist()) | 514 if (!DOMWrapperWorld::isolatedWorldsExist()) |
428 return MainWorld; | 515 return MainWorld; |
429 ASSERT(!v8::Context::GetEntered().IsEmpty()); | 516 ASSERT(!v8::Context::GetEntered().IsEmpty()); |
430 DOMWrapperWorld* isolatedWorld = DOMWrapperWorld::isolatedWorld(v8::Context: :GetEntered()); | 517 DOMWrapperWorld* isolatedWorld = DOMWrapperWorld::isolatedWorld(v8::Context: :GetEntered()); |
431 if (isolatedWorld) | 518 if (isolatedWorld) |
432 return IsolatedWorld; | 519 return IsolatedWorld; |
433 return MainWorld; | 520 return MainWorld; |
434 } | 521 } |
435 | 522 |
436 } // namespace WebCore | 523 } // namespace WebCore |
OLD | NEW |