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

Side by Side Diff: Source/bindings/v8/V8Binding.h

Issue 351423002: Moved files under Source/bindings/v8 to Source/bindings/core/v8. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 5 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/bindings/v8/V8AbstractEventListener.cpp ('k') | Source/bindings/v8/V8Binding.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2012 Ericsson AB. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef V8Binding_h
33 #define V8Binding_h
34
35 #include "bindings/v8/DOMWrapperWorld.h"
36 #include "bindings/v8/ExceptionMessages.h"
37 #include "bindings/v8/ScriptValue.h"
38 #include "bindings/v8/V8BindingMacros.h"
39 #include "bindings/v8/V8PerIsolateData.h"
40 #include "bindings/v8/V8StringResource.h"
41 #include "bindings/v8/V8ThrowException.h"
42 #include "bindings/v8/V8ValueCache.h"
43 #include "platform/heap/Heap.h"
44 #include "wtf/MathExtras.h"
45 #include "wtf/text/AtomicString.h"
46 #include <v8.h>
47
48 namespace WebCore {
49
50 class LocalDOMWindow;
51 class Document;
52 class EventListener;
53 class ExecutionContext;
54 class ExceptionState;
55 class LocalFrame;
56 class NodeFilter;
57 class ScriptWrappable;
58 class XPathNSResolver;
59
60 namespace TraceEvent {
61 class ConvertableToTraceFormat;
62 }
63
64 const int kMaxRecursionDepth = 22;
65
66 // Schedule a JavaScript error to be thrown.
67 v8::Handle<v8::Value> throwError(V8ErrorType, const String&, v8::Isolate*);
68
69 // Schedule a JavaScript error to be thrown.
70 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*);
71
72 // A helper for throwing JavaScript TypeError.
73 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*);
74
75 // Helpers for throwing JavaScript TypeErrors for arity mismatches.
76 void throwArityTypeErrorForMethod(const char* method, const char* type, const ch ar* valid, unsigned provided, v8::Isolate*);
77 void throwArityTypeErrorForConstructor(const char* type, const char* valid, unsi gned provided, v8::Isolate*);
78 void throwArityTypeError(ExceptionState&, const char* valid, unsigned provided);
79 void throwMinimumArityTypeErrorForMethod(const char* method, const char* type, u nsigned expected, unsigned providedLeastNumMandatoryParams, v8::Isolate*);
80 void throwMinimumArityTypeErrorForConstructor(const char* type, unsigned expecte d, unsigned providedLeastNumMandatoryParams, v8::Isolate*);
81 void throwMinimumArityTypeError(ExceptionState&, unsigned expected, unsigned pro videdLeastNumMandatoryParams);
82
83 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator();
84
85 template<typename CallbackInfo, typename V>
86 inline void v8SetReturnValue(const CallbackInfo& info, V v)
87 {
88 info.GetReturnValue().Set(v);
89 }
90
91 template<typename CallbackInfo>
92 inline void v8SetReturnValueBool(const CallbackInfo& info, bool v)
93 {
94 info.GetReturnValue().Set(v);
95 }
96
97 template<typename CallbackInfo>
98 inline void v8SetReturnValueInt(const CallbackInfo& info, int v)
99 {
100 info.GetReturnValue().Set(v);
101 }
102
103 template<typename CallbackInfo>
104 inline void v8SetReturnValueUnsigned(const CallbackInfo& info, unsigned v)
105 {
106 info.GetReturnValue().Set(v);
107 }
108
109 template<typename CallbackInfo>
110 inline void v8SetReturnValueNull(const CallbackInfo& info)
111 {
112 info.GetReturnValue().SetNull();
113 }
114
115 template<typename CallbackInfo>
116 inline void v8SetReturnValueUndefined(const CallbackInfo& info)
117 {
118 info.GetReturnValue().SetUndefined();
119 }
120
121 template<typename CallbackInfo>
122 inline void v8SetReturnValueEmptyString(const CallbackInfo& info)
123 {
124 info.GetReturnValue().SetEmptyString();
125 }
126
127 template <class CallbackInfo>
128 inline void v8SetReturnValueString(const CallbackInfo& info, const String& strin g, v8::Isolate* isolate)
129 {
130 if (string.isNull()) {
131 v8SetReturnValueEmptyString(info);
132 return;
133 }
134 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf o.GetReturnValue(), string.impl());
135 }
136
137 template <class CallbackInfo>
138 inline void v8SetReturnValueStringOrNull(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
139 {
140 if (string.isNull()) {
141 v8SetReturnValueNull(info);
142 return;
143 }
144 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf o.GetReturnValue(), string.impl());
145 }
146
147 template <class CallbackInfo>
148 inline void v8SetReturnValueStringOrUndefined(const CallbackInfo& info, const St ring& string, v8::Isolate* isolate)
149 {
150 if (string.isNull()) {
151 v8SetReturnValueUndefined(info);
152 return;
153 }
154 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf o.GetReturnValue(), string.impl());
155 }
156
157 // Convert v8::String to a WTF::String. If the V8 string is not already
158 // an external string then it is transformed into an external string at this
159 // point to avoid repeated conversions.
160 inline String toCoreString(v8::Handle<v8::String> value)
161 {
162 return v8StringToWebCoreString<String>(value, Externalize);
163 }
164
165 inline String toCoreStringWithNullCheck(v8::Handle<v8::String> value)
166 {
167 if (value.IsEmpty() || value->IsNull())
168 return String();
169 return toCoreString(value);
170 }
171
172 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::String> value)
173 {
174 if (value.IsEmpty() || value->IsNull() || value->IsUndefined())
175 return String();
176 return toCoreString(value);
177 }
178
179 inline AtomicString toCoreAtomicString(v8::Handle<v8::String> value)
180 {
181 return v8StringToWebCoreString<AtomicString>(value, Externalize);
182 }
183
184 // This method will return a null String if the v8::Value does not contain a v8: :String.
185 // It will not call ToString() on the v8::Value. If you want ToString() to be ca lled,
186 // please use the TONATIVE_FOR_V8STRINGRESOURCE_*() macros instead.
187 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::Value> value)
188 {
189 if (value.IsEmpty() || !value->IsString())
190 return String();
191 return toCoreString(value.As<v8::String>());
192 }
193
194 // Convert a string to a V8 string.
195 // Return a V8 external string that shares the underlying buffer with the given
196 // WebCore string. The reference counting mechanism is used to keep the
197 // underlying buffer alive while the string is still live in the V8 engine.
198 inline v8::Handle<v8::String> v8String(v8::Isolate* isolate, const String& strin g)
199 {
200 if (string.isNull())
201 return v8::String::Empty(isolate);
202 return V8PerIsolateData::from(isolate)->stringCache()->v8ExternalString(stri ng.impl(), isolate);
203 }
204
205 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* s tr)
206 {
207 ASSERT(isolate);
208 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString , strlen(str));
209 }
210
211 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* s tr, size_t length)
212 {
213 ASSERT(isolate);
214 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString , length);
215 }
216
217 inline v8::Handle<v8::Value> v8Undefined()
218 {
219 return v8::Handle<v8::Value>();
220 }
221
222 // Converts a DOM object to a v8 value.
223 // This is a no-inline version of toV8(). If you want to call toV8()
224 // without creating #include cycles, you can use this function instead.
225 // Each specialized implementation will be generated.
226 template<typename T>
227 v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationConte xt, v8::Isolate*);
228
229 template <typename T>
230 struct V8ValueTraits {
231 typedef typename WTF::RemovePointer<T>::Type TypeWithoutPointer;
232 static v8::Handle<v8::Value> toV8Value(TypeWithoutPointer* const& value, v8: :Handle<v8::Object> creationContext, v8::Isolate* isolate)
233 {
234 return toV8NoInline(value, creationContext, isolate);
235 }
236
237 typedef typename WTF::RemoveTemplate<T, RawPtr>::Type TypeWithoutRawPtr;
238 static v8::Handle<v8::Value> toV8Value(const RawPtr<TypeWithoutRawPtr>& valu e, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
239 {
240 return toV8NoInline(value.get(), creationContext, isolate);
241 }
242
243 // HeapVector<RefPtr> requires the following method:
244 typedef typename WTF::RemoveTemplate<T, RefPtr>::Type TypeWithoutRefPtr;
245 static v8::Handle<v8::Value> toV8Value(const RefPtr<TypeWithoutRefPtr>& valu e, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
246 {
247 return toV8NoInline(value.get(), creationContext, isolate);
248 }
249
250 typedef typename WTF::RemoveTemplate<T, PassRefPtr>::Type TypeWithoutPassRef Ptr;
251 static v8::Handle<v8::Value> toV8Value(const PassRefPtr<TypeWithoutPassRefPt r>& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
252 {
253 return toV8NoInline(value.get(), creationContext, isolate);
254 }
255
256 typedef typename WTF::RemoveTemplate<T, Member>::Type TypeWithoutMember;
257 static v8::Handle<v8::Value> toV8Value(const Member<TypeWithoutMember>& valu e, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
258 {
259 return toV8NoInline(value.get(), creationContext, isolate);
260 }
261 };
262
263 template <typename T, size_t inlineCapacity, typename Allocator>
264 struct V8ValueTraits<WTF::Vector<T, inlineCapacity, Allocator> > {
265 static v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity, Alloc ator>& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
266 {
267 return v8ArrayNoInline(value, creationContext, isolate);
268 }
269 };
270
271 template <typename T, size_t inlineCapacity>
272 struct V8ValueTraits<HeapVector<T, inlineCapacity> > {
273 static v8::Handle<v8::Value> toV8Value(const HeapVector<T, inlineCapacity>& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
274 {
275 return v8ArrayNoInline(value, creationContext, isolate);
276 }
277 };
278
279 template<>
280 struct V8ValueTraits<String> {
281 static inline v8::Handle<v8::Value> toV8Value(const String& value, v8::Handl e<v8::Object>, v8::Isolate* isolate)
282 {
283 return v8String(isolate, value);
284 }
285 };
286
287 template<>
288 struct V8ValueTraits<AtomicString> {
289 static inline v8::Handle<v8::Value> toV8Value(const AtomicString& value, v8: :Handle<v8::Object>, v8::Isolate* isolate)
290 {
291 return v8String(isolate, value);
292 }
293 };
294
295 template<size_t n>
296 struct V8ValueTraits<char[n]> {
297 static inline v8::Handle<v8::Value> toV8Value(char const (&value)[n], v8::Ha ndle<v8::Object>, v8::Isolate* isolate)
298 {
299 return v8String(isolate, value);
300 }
301 };
302
303 template<>
304 struct V8ValueTraits<const char*> {
305 static inline v8::Handle<v8::Value> toV8Value(const char* const& value, v8:: Handle<v8::Object>, v8::Isolate* isolate)
306 {
307 return v8String(isolate, value);
308 }
309 };
310
311 template<>
312 struct V8ValueTraits<int> {
313 static inline v8::Handle<v8::Value> toV8Value(const int& value, v8::Handle<v 8::Object>, v8::Isolate* isolate)
314 {
315 return v8::Integer::New(isolate, value);
316 }
317 };
318
319 template<>
320 struct V8ValueTraits<long> {
321 static inline v8::Handle<v8::Value> toV8Value(const long& value, v8::Handle< v8::Object>, v8::Isolate* isolate)
322 {
323 return v8::Integer::New(isolate, value);
324 }
325 };
326
327 template<>
328 struct V8ValueTraits<unsigned> {
329 static inline v8::Handle<v8::Value> toV8Value(const unsigned& value, v8::Han dle<v8::Object>, v8::Isolate* isolate)
330 {
331 return v8::Integer::NewFromUnsigned(isolate, value);
332 }
333 };
334
335 template<>
336 struct V8ValueTraits<unsigned long> {
337 static inline v8::Handle<v8::Value> toV8Value(const unsigned long& value, v8 ::Handle<v8::Object>, v8::Isolate* isolate)
338 {
339 return v8::Integer::NewFromUnsigned(isolate, value);
340 }
341 };
342
343 template<>
344 struct V8ValueTraits<float> {
345 static inline v8::Handle<v8::Value> toV8Value(const float& value, v8::Handle <v8::Object>, v8::Isolate* isolate)
346 {
347 return v8::Number::New(isolate, value);
348 }
349 };
350
351 template<>
352 struct V8ValueTraits<double> {
353 static inline v8::Handle<v8::Value> toV8Value(const double& value, v8::Handl e<v8::Object>, v8::Isolate* isolate)
354 {
355 return v8::Number::New(isolate, value);
356 }
357 };
358
359 template<>
360 struct V8ValueTraits<bool> {
361 static inline v8::Handle<v8::Value> toV8Value(const bool& value, v8::Handle< v8::Object>, v8::Isolate* isolate)
362 {
363 return v8::Boolean::New(isolate, value);
364 }
365 };
366
367 // V8NullType and V8UndefinedType are used only for the value conversion.
368 class V8NullType { };
369 class V8UndefinedType { };
370
371 template<>
372 struct V8ValueTraits<V8NullType> {
373 static inline v8::Handle<v8::Value> toV8Value(const V8NullType&, v8::Handle< v8::Object>, v8::Isolate* isolate)
374 {
375 return v8::Null(isolate);
376 }
377 };
378
379 template<>
380 struct V8ValueTraits<V8UndefinedType> {
381 static inline v8::Handle<v8::Value> toV8Value(const V8UndefinedType&, v8::Ha ndle<v8::Object>, v8::Isolate* isolate)
382 {
383 return v8::Undefined(isolate);
384 }
385 };
386
387 template<>
388 struct V8ValueTraits<ScriptValue> {
389 static inline v8::Handle<v8::Value> toV8Value(const ScriptValue& value, v8:: Handle<v8::Object>, v8::Isolate*)
390 {
391 return value.v8Value();
392 }
393 };
394
395 template<>
396 struct V8ValueTraits<v8::Handle<v8::Value> > {
397 static inline v8::Handle<v8::Value> toV8Value(const v8::Handle<v8::Value>& v alue, v8::Handle<v8::Object>, v8::Isolate*)
398 {
399 return value;
400 }
401 };
402
403 template<>
404 struct V8ValueTraits<v8::Local<v8::Value> > {
405 static inline v8::Handle<v8::Value> toV8Value(const v8::Local<v8::Value>& va lue, v8::Handle<v8::Object>, v8::Isolate*)
406 {
407 return value;
408 }
409 };
410
411 template<typename T, size_t inlineCapacity>
412 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Han dle<v8::Object> creationContext, v8::Isolate* isolate)
413 {
414 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
415 int index = 0;
416 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end();
417 typedef V8ValueTraits<T> TraitsType;
418 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi n(); iter != end; ++iter)
419 result->Set(v8::Integer::New(isolate, index++), TraitsType::toV8Value(*i ter, creationContext, isolate));
420 return result;
421 }
422
423 template<typename T, size_t inlineCapacity>
424 v8::Handle<v8::Value> v8Array(const HeapVector<T, inlineCapacity>& iterator, v8: :Handle<v8::Object> creationContext, v8::Isolate* isolate)
425 {
426 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
427 int index = 0;
428 typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.end();
429 typedef V8ValueTraits<T> TraitsType;
430 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = iterator. begin(); iter != end; ++iter)
431 result->Set(v8::Integer::New(isolate, index++), TraitsType::toV8Value(*i ter, creationContext, isolate));
432 return result;
433 }
434
435 template<typename T, size_t inlineCapacity>
436 v8::Handle<v8::Value> v8ArrayNoInline(const Vector<T, inlineCapacity>& iterator, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
437 {
438 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
439 int index = 0;
440 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end();
441 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi n(); iter != end; ++iter)
442 result->Set(v8::Integer::New(isolate, index++), toV8NoInline(WTF::getPtr (*iter), creationContext, isolate));
443 return result;
444 }
445
446 template<typename T, size_t inlineCapacity>
447 v8::Handle<v8::Value> v8ArrayNoInline(const HeapVector<T, inlineCapacity>& itera tor, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
448 {
449 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
450 int index = 0;
451 typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.end();
452 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = iterator. begin(); iter != end; ++iter)
453 result->Set(v8::Integer::New(isolate, index++), toV8NoInline(WTF::getPtr (*iter), creationContext, isolate));
454 return result;
455 }
456
457 // Conversion flags, used in toIntXX/toUIntXX.
458 enum IntegerConversionConfiguration {
459 NormalConversion,
460 EnforceRange,
461 Clamp
462 };
463
464 // Convert a value to a 8-bit signed integer. The conversion fails if the
465 // value cannot be converted to a number or the range violated per WebIDL:
466 // http://www.w3.org/TR/WebIDL/#es-byte
467 int8_t toInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionSt ate&);
468 inline int8_t toInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState )
469 {
470 return toInt8(value, NormalConversion, exceptionState);
471 }
472
473 // Convert a value to a 8-bit integer assuming the conversion cannot fail.
474 int8_t toInt8(v8::Handle<v8::Value>);
475
476 // Convert a value to a 8-bit unsigned integer. The conversion fails if the
477 // value cannot be converted to a number or the range violated per WebIDL:
478 // http://www.w3.org/TR/WebIDL/#es-octet
479 uint8_t toUInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception State&);
480 inline uint8_t toUInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionSta te)
481 {
482 return toUInt8(value, NormalConversion, exceptionState);
483 }
484
485 // Convert a value to a 8-bit unsigned integer assuming the conversion cannot fa il.
486 uint8_t toUInt8(v8::Handle<v8::Value>);
487
488 // Convert a value to a 16-bit signed integer. The conversion fails if the
489 // value cannot be converted to a number or the range violated per WebIDL:
490 // http://www.w3.org/TR/WebIDL/#es-short
491 int16_t toInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception State&);
492 inline int16_t toInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionSta te)
493 {
494 return toInt16(value, NormalConversion, exceptionState);
495 }
496
497 // Convert a value to a 16-bit integer assuming the conversion cannot fail.
498 int16_t toInt16(v8::Handle<v8::Value>);
499
500 // Convert a value to a 16-bit unsigned integer. The conversion fails if the
501 // value cannot be converted to a number or the range violated per WebIDL:
502 // http://www.w3.org/TR/WebIDL/#es-unsigned-short
503 uint16_t toUInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti onState&);
504 inline uint16_t toUInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionS tate)
505 {
506 return toUInt16(value, NormalConversion, exceptionState);
507 }
508
509 // Convert a value to a 16-bit unsigned integer assuming the conversion cannot f ail.
510 uint16_t toUInt16(v8::Handle<v8::Value>);
511
512 // Convert a value to a 32-bit signed integer. The conversion fails if the
513 // value cannot be converted to a number or the range violated per WebIDL:
514 // http://www.w3.org/TR/WebIDL/#es-long
515 int32_t toInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception State&);
516 inline int32_t toInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionSta te)
517 {
518 return toInt32(value, NormalConversion, exceptionState);
519 }
520
521 // Convert a value to a 32-bit integer assuming the conversion cannot fail.
522 int32_t toInt32(v8::Handle<v8::Value>);
523
524 // Convert a value to a 32-bit unsigned integer. The conversion fails if the
525 // value cannot be converted to a number or the range violated per WebIDL:
526 // http://www.w3.org/TR/WebIDL/#es-unsigned-long
527 uint32_t toUInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti onState&);
528 inline uint32_t toUInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionS tate)
529 {
530 return toUInt32(value, NormalConversion, exceptionState);
531 }
532
533 // Convert a value to a 32-bit unsigned integer assuming the conversion cannot f ail.
534 uint32_t toUInt32(v8::Handle<v8::Value>);
535
536 // Convert a value to a 64-bit signed integer. The conversion fails if the
537 // value cannot be converted to a number or the range violated per WebIDL:
538 // http://www.w3.org/TR/WebIDL/#es-long-long
539 int64_t toInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception State&);
540 inline int64_t toInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionSta te)
541 {
542 return toInt64(value, NormalConversion, exceptionState);
543 }
544
545 // Convert a value to a 64-bit integer assuming the conversion cannot fail.
546 int64_t toInt64(v8::Handle<v8::Value>);
547
548 // Convert a value to a 64-bit unsigned integer. The conversion fails if the
549 // value cannot be converted to a number or the range violated per WebIDL:
550 // http://www.w3.org/TR/WebIDL/#es-unsigned-long-long
551 uint64_t toUInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti onState&);
552 inline uint64_t toUInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionS tate)
553 {
554 return toUInt64(value, NormalConversion, exceptionState);
555 }
556
557 // Convert a value to a 64-bit unsigned integer assuming the conversion cannot f ail.
558 uint64_t toUInt64(v8::Handle<v8::Value>);
559
560 // Convert a value to a single precision float, which might fail.
561 float toFloat(v8::Handle<v8::Value>, ExceptionState&);
562
563 // Convert a value to a single precision float assuming the conversion cannot fa il.
564 inline float toFloat(v8::Local<v8::Value> value)
565 {
566 return static_cast<float>(value->NumberValue());
567 }
568
569 // Converts a value to a String, throwing if any code unit is outside 0-255.
570 String toByteString(v8::Handle<v8::Value>, ExceptionState&);
571
572 // Converts a value to a String, replacing unmatched UTF-16 surrogates with repl acement characters.
573 String toScalarValueString(v8::Handle<v8::Value>, ExceptionState&);
574
575 inline v8::Handle<v8::Boolean> v8Boolean(bool value, v8::Isolate* isolate)
576 {
577 return value ? v8::True(isolate) : v8::False(isolate);
578 }
579
580 inline double toCoreDate(v8::Handle<v8::Value> object)
581 {
582 if (object->IsDate())
583 return v8::Handle<v8::Date>::Cast(object)->ValueOf();
584 if (object->IsNumber())
585 return object->NumberValue();
586 return std::numeric_limits<double>::quiet_NaN();
587 }
588
589 inline v8::Handle<v8::Value> v8DateOrNaN(double value, v8::Isolate* isolate)
590 {
591 ASSERT(isolate);
592 return v8::Date::New(isolate, std::isfinite(value) ? value : std::numeric_li mits<double>::quiet_NaN());
593 }
594
595 // FIXME: Remove the special casing for NodeFilter and XPathNSResolver.
596 PassRefPtrWillBeRawPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Handl e<v8::Object>, ScriptState*);
597 PassRefPtrWillBeRawPtr<XPathNSResolver> toXPathNSResolver(v8::Handle<v8::Value>, v8::Isolate*);
598
599 template<class T> struct NativeValueTraits;
600
601 template<>
602 struct NativeValueTraits<String> {
603 static inline String nativeValue(const v8::Handle<v8::Value>& value, v8::Iso late* isolate)
604 {
605 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, String());
606 return stringValue;
607 }
608 };
609
610 template<>
611 struct NativeValueTraits<unsigned> {
612 static inline unsigned nativeValue(const v8::Handle<v8::Value>& value, v8::I solate* isolate)
613 {
614 return toUInt32(value);
615 }
616 };
617
618 template<>
619 struct NativeValueTraits<float> {
620 static inline float nativeValue(const v8::Handle<v8::Value>& value, v8::Isol ate* isolate)
621 {
622 return static_cast<float>(value->NumberValue());
623 }
624 };
625
626 template<>
627 struct NativeValueTraits<double> {
628 static inline double nativeValue(const v8::Handle<v8::Value>& value, v8::Iso late* isolate)
629 {
630 return static_cast<double>(value->NumberValue());
631 }
632 };
633
634 template<>
635 struct NativeValueTraits<v8::Handle<v8::Value> > {
636 static inline v8::Handle<v8::Value> nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
637 {
638 return value;
639 }
640 };
641
642 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length, v8:: Isolate*);
643
644 // Converts a JavaScript value to an array as per the Web IDL specification:
645 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
646 template <class T, class V8T>
647 Vector<RefPtr<T> > toRefPtrNativeArrayUnchecked(v8::Local<v8::Value> v8Value, ui nt32_t length, v8::Isolate* isolate, bool* success = 0)
648 {
649 Vector<RefPtr<T> > result;
650 result.reserveInitialCapacity(length);
651 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
652 for (uint32_t i = 0; i < length; ++i) {
653 v8::Handle<v8::Value> element = object->Get(i);
654 if (V8T::hasInstance(element, isolate)) {
655 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast( element);
656 result.uncheckedAppend(V8T::toNative(elementObject));
657 } else {
658 if (success)
659 *success = false;
660 throwTypeError("Invalid Array element type", isolate);
661 return Vector<RefPtr<T> >();
662 }
663 }
664 return result;
665 }
666
667 template <class T, class V8T>
668 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argument Index, v8::Isolate* isolate, bool* success = 0)
669 {
670 if (success)
671 *success = true;
672
673 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
674 uint32_t length = 0;
675 if (value->IsArray()) {
676 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
677 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
678 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argument Index), isolate);
679 return Vector<RefPtr<T> >();
680 }
681 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, succes s);
682 }
683
684 template <class T, class V8T>
685 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const String & propertyName, v8::Isolate* isolate, bool* success = 0)
686 {
687 if (success)
688 *success = true;
689
690 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
691 uint32_t length = 0;
692 if (value->IsArray()) {
693 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
694 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
695 throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyName) , isolate);
696 return Vector<RefPtr<T> >();
697 }
698 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, succes s);
699 }
700
701 template <class T, class V8T>
702 WillBeHeapVector<RefPtrWillBeMember<T> > toRefPtrWillBeMemberNativeArray(v8::Han dle<v8::Value> value, int argumentIndex, v8::Isolate* isolate, bool* success = 0 )
703 {
704 if (success)
705 *success = true;
706
707 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
708 uint32_t length = 0;
709 if (value->IsArray()) {
710 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
711 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
712 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argument Index), isolate);
713 return WillBeHeapVector<RefPtrWillBeMember<T> >();
714 }
715
716 WillBeHeapVector<RefPtrWillBeMember<T> > result;
717 result.reserveInitialCapacity(length);
718 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
719 for (uint32_t i = 0; i < length; ++i) {
720 v8::Handle<v8::Value> element = object->Get(i);
721 if (V8T::hasInstance(element, isolate)) {
722 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast( element);
723 result.uncheckedAppend(V8T::toNative(elementObject));
724 } else {
725 if (success)
726 *success = false;
727 throwTypeError("Invalid Array element type", isolate);
728 return WillBeHeapVector<RefPtrWillBeMember<T> >();
729 }
730 }
731 return result;
732 }
733
734 template <class T, class V8T>
735 WillBeHeapVector<RefPtrWillBeMember<T> > toRefPtrWillBeMemberNativeArray(v8::Han dle<v8::Value> value, const String& propertyName, v8::Isolate* isolate, bool* su ccess = 0)
736 {
737 if (success)
738 *success = true;
739
740 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
741 uint32_t length = 0;
742 if (value->IsArray()) {
743 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
744 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
745 throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyName) , isolate);
746 return WillBeHeapVector<RefPtrWillBeMember<T> >();
747 }
748
749 WillBeHeapVector<RefPtrWillBeMember<T> > result;
750 result.reserveInitialCapacity(length);
751 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
752 for (uint32_t i = 0; i < length; ++i) {
753 v8::Handle<v8::Value> element = object->Get(i);
754 if (V8T::hasInstance(element, isolate)) {
755 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast( element);
756 result.uncheckedAppend(V8T::toNative(elementObject));
757 } else {
758 if (success)
759 *success = false;
760 throwTypeError("Invalid Array element type", isolate);
761 return WillBeHeapVector<RefPtrWillBeMember<T> >();
762 }
763 }
764 return result;
765 }
766
767 template <class T, class V8T>
768 HeapVector<Member<T> > toMemberNativeArray(v8::Handle<v8::Value> value, int argu mentIndex, v8::Isolate* isolate, bool* success = 0)
769 {
770 if (success)
771 *success = true;
772
773 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
774 uint32_t length = 0;
775 if (value->IsArray()) {
776 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
777 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
778 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argument Index), isolate);
779 return HeapVector<Member<T> >();
780 }
781
782 HeapVector<Member<T> > result;
783 result.reserveInitialCapacity(length);
784 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
785 for (uint32_t i = 0; i < length; ++i) {
786 v8::Handle<v8::Value> element = object->Get(i);
787 if (V8T::hasInstance(element, isolate)) {
788 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast( element);
789 result.uncheckedAppend(V8T::toNative(elementObject));
790 } else {
791 if (success)
792 *success = false;
793 throwTypeError("Invalid Array element type", isolate);
794 return HeapVector<Member<T> >();
795 }
796 }
797 return result;
798 }
799
800 // Converts a JavaScript value to an array as per the Web IDL specification:
801 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
802 template <class T>
803 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isol ate* isolate)
804 {
805 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
806 uint32_t length = 0;
807 if (value->IsArray()) {
808 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
809 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
810 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argument Index), isolate);
811 return Vector<T>();
812 }
813
814 Vector<T> result;
815 result.reserveInitialCapacity(length);
816 typedef NativeValueTraits<T> TraitsType;
817 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
818 for (uint32_t i = 0; i < length; ++i)
819 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i), isolate)) ;
820 return result;
821 }
822
823 template <class T>
824 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int startIndex)
825 {
826 ASSERT(startIndex <= info.Length());
827 Vector<T> result;
828 typedef NativeValueTraits<T> TraitsType;
829 int length = info.Length();
830 result.reserveInitialCapacity(length);
831 for (int i = startIndex; i < length; ++i)
832 result.uncheckedAppend(TraitsType::nativeValue(info[i], info.GetIsolate( )));
833 return result;
834 }
835
836 // Validates that the passed object is a sequence type per WebIDL spec
837 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence
838 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint32_t& length, v8::Isolate* isolate)
839 {
840 // Attempt converting to a sequence if the value is not already an array but is
841 // any kind of object except for a native Date object or a native RegExp obj ect.
842 ASSERT(!value->IsArray());
843 // FIXME: Do we really need to special case Date and RegExp object?
844 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806
845 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) {
846 // The caller is responsible for reporting a TypeError.
847 return v8Undefined();
848 }
849
850 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
851 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
852 v8::Local<v8::String> lengthSymbol = v8AtomicString(isolate, "length");
853
854 // FIXME: The specification states that the length property should be used a s fallback, if value
855 // is not a platform object that supports indexed properties. If it supports indexed properties,
856 // length should actually be one greater than value’s maximum indexed proper ty index.
857 TONATIVE_EXCEPTION(v8::Local<v8::Value>, lengthValue, object->Get(lengthSymb ol));
858
859 if (lengthValue->IsUndefined() || lengthValue->IsNull()) {
860 // The caller is responsible for reporting a TypeError.
861 return v8Undefined();
862 }
863
864 TONATIVE_EXCEPTION(uint32_t, sequenceLength, lengthValue->Int32Value());
865 length = sequenceLength;
866 return v8Value;
867 }
868
869 v8::Isolate* toIsolate(ExecutionContext*);
870 v8::Isolate* toIsolate(LocalFrame*);
871
872 LocalDOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*);
873 LocalDOMWindow* toDOMWindow(v8::Handle<v8::Context>);
874 LocalDOMWindow* enteredDOMWindow(v8::Isolate*);
875 LocalDOMWindow* currentDOMWindow(v8::Isolate*);
876 LocalDOMWindow* callingDOMWindow(v8::Isolate*);
877 ExecutionContext* toExecutionContext(v8::Handle<v8::Context>);
878 ExecutionContext* currentExecutionContext(v8::Isolate*);
879 ExecutionContext* callingExecutionContext(v8::Isolate*);
880
881 // Returns a V8 context associated with a ExecutionContext and a DOMWrapperWorld .
882 // This method returns an empty context if there is no frame or the frame is alr eady detached.
883 v8::Local<v8::Context> toV8Context(ExecutionContext*, DOMWrapperWorld&);
884 // Returns a V8 context associated with a LocalFrame and a DOMWrapperWorld.
885 // This method returns an empty context if the frame is already detached.
886 v8::Local<v8::Context> toV8Context(LocalFrame*, DOMWrapperWorld&);
887
888 // Returns the frame object of the window object associated with
889 // a context, if the window is currently being displayed in the LocalFrame.
890 LocalFrame* toFrameIfNotDetached(v8::Handle<v8::Context>);
891
892 // If the current context causes out of memory, JavaScript setting
893 // is disabled and it returns true.
894 bool handleOutOfMemory();
895 v8::Local<v8::Value> handleMaxRecursionDepthExceeded(v8::Isolate*);
896 void crashIfV8IsDead();
897
898 inline bool isUndefinedOrNull(v8::Handle<v8::Value> value)
899 {
900 return value->IsNull() || value->IsUndefined();
901 }
902 v8::Handle<v8::Function> getBoundFunction(v8::Handle<v8::Function>);
903
904 // Attaches |environment| to |function| and returns it.
905 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function, v8:: Handle<v8::Value> environment, v8::Isolate* isolate)
906 {
907 return v8::Function::New(isolate, function, environment);
908 }
909
910 // FIXME: This will be soon embedded in the generated code.
911 template<class Collection> static void indexedPropertyEnumerator(const v8::Prope rtyCallbackInfo<v8::Array>& info)
912 {
913 Collection* collection = reinterpret_cast<Collection*>(info.Holder()->GetAli gnedPointerFromInternalField(v8DOMWrapperObjectIndex));
914 int length = collection->length();
915 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), length) ;
916 for (int i = 0; i < length; ++i) {
917 // FIXME: Do we need to check that the item function returns a non-null value for this index?
918 v8::Handle<v8::Integer> integer = v8::Integer::New(info.GetIsolate(), i) ;
919 properties->Set(integer, integer);
920 }
921 v8SetReturnValue(info, properties);
922 }
923
924 // These methods store hidden values into an array that is stored in the interna l field of a DOM wrapper.
925 void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cac heIndex, v8::Isolate*);
926 void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, in t cacheIndex, v8::Isolate*);
927 void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* oldVal ue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*);
928
929 PassRefPtr<JSONValue> v8ToJSONValue(v8::Isolate*, v8::Handle<v8::Value>, int);
930
931 // Result values for platform object 'deleter' methods,
932 // http://www.w3.org/TR/WebIDL/#delete
933 enum DeleteResult {
934 DeleteSuccess,
935 DeleteReject,
936 DeleteUnknownProperty
937 };
938
939 class V8IsolateInterruptor : public ThreadState::Interruptor {
940 public:
941 explicit V8IsolateInterruptor(v8::Isolate* isolate) : m_isolate(isolate) { }
942
943 static void onInterruptCallback(v8::Isolate* isolate, void* data)
944 {
945 reinterpret_cast<V8IsolateInterruptor*>(data)->onInterrupted();
946 }
947
948 virtual void requestInterrupt() OVERRIDE
949 {
950 m_isolate->RequestInterrupt(&onInterruptCallback, this);
951 }
952
953 virtual void clearInterrupt() OVERRIDE
954 {
955 m_isolate->ClearInterrupt();
956 }
957
958 private:
959 v8::Isolate* m_isolate;
960 };
961
962 class V8TestingScope {
963 public:
964 explicit V8TestingScope(v8::Isolate*);
965 ScriptState* scriptState() const;
966 v8::Isolate* isolate() const;
967 ~V8TestingScope();
968
969 private:
970 v8::HandleScope m_handleScope;
971 v8::Context::Scope m_contextScope;
972 RefPtr<ScriptState> m_scriptState;
973 };
974
975 void GetDevToolsFunctionInfo(v8::Handle<v8::Function>, v8::Isolate*, int& script Id, String& resourceName, int& lineNumber);
976 PassRefPtr<TraceEvent::ConvertableToTraceFormat> devToolsTraceEventData(Executio nContext*, v8::Handle<v8::Function>, v8::Isolate*);
977
978 class V8RethrowTryCatchScope FINAL {
979 public:
980 explicit V8RethrowTryCatchScope(v8::TryCatch& block) : m_block(block) { }
981 ~V8RethrowTryCatchScope()
982 {
983 // ReThrow() is a no-op if no exception has been caught, so always call.
984 m_block.ReThrow();
985 }
986
987 private:
988 v8::TryCatch& m_block;
989 };
990
991 class V8ResetTryCatchScope FINAL {
992 public:
993 explicit V8ResetTryCatchScope(v8::TryCatch& block) : m_block(block) { }
994 ~V8ResetTryCatchScope()
995 {
996 m_block.Reset();
997 }
998
999 private:
1000 v8::TryCatch& m_block;
1001 };
1002
1003 } // namespace WebCore
1004
1005 #endif // V8Binding_h
OLDNEW
« no previous file with comments | « Source/bindings/v8/V8AbstractEventListener.cpp ('k') | Source/bindings/v8/V8Binding.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698