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

Side by Side Diff: sky/engine/bindings/core/v8/V8Binding.h

Issue 922053002: Remove unused V8 integration code in Sky (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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
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 SKY_ENGINE_BINDINGS_CORE_V8_V8BINDING_H_
33 #define SKY_ENGINE_BINDINGS_CORE_V8_V8BINDING_H_
34
35 #include "sky/engine/bindings/core/v8/DOMWrapperWorld.h"
36 #include "sky/engine/bindings/core/v8/ExceptionMessages.h"
37 #include "sky/engine/bindings/core/v8/ScriptValue.h"
38 #include "sky/engine/bindings/core/v8/ScriptWrappable.h"
39 #include "sky/engine/bindings/core/v8/V8BindingMacros.h"
40 #include "sky/engine/bindings/core/v8/V8PerIsolateData.h"
41 #include "sky/engine/bindings/core/v8/V8StringResource.h"
42 #include "sky/engine/bindings/core/v8/V8ThrowException.h"
43 #include "sky/engine/bindings/core/v8/V8ValueCache.h"
44 #include "sky/engine/wtf/GetPtr.h"
45 #include "sky/engine/wtf/MathExtras.h"
46 #include "sky/engine/wtf/text/AtomicString.h"
47 #include "v8/include/v8.h"
48
49 namespace blink {
50
51 class LocalDOMWindow;
52 class Document;
53 class EventListener;
54 class ExecutionContext;
55 class ExceptionState;
56 class LocalFrame;
57
58 namespace TraceEvent {
59 class ConvertableToTraceFormat;
60 }
61
62 const int kMaxRecursionDepth = 22;
63
64 // Helpers for throwing JavaScript TypeErrors for arity mismatches.
65 void setArityTypeError(ExceptionState&, const char* valid, unsigned provided);
66 v8::Local<v8::Value> createMinimumArityTypeErrorForMethod(const char* method, co nst char* type, unsigned expected, unsigned provided, v8::Isolate*);
67 v8::Local<v8::Value> createMinimumArityTypeErrorForConstructor(const char* type, unsigned expected, unsigned provided, v8::Isolate*);
68 void setMinimumArityTypeError(ExceptionState&, unsigned expected, unsigned provi ded);
69
70 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator();
71
72 template<typename CallbackInfo, typename V>
73 inline void v8SetReturnValue(const CallbackInfo& info, V v)
74 {
75 info.GetReturnValue().Set(v);
76 }
77
78 template<typename CallbackInfo>
79 inline void v8SetReturnValueBool(const CallbackInfo& info, bool v)
80 {
81 info.GetReturnValue().Set(v);
82 }
83
84 template<typename CallbackInfo>
85 inline void v8SetReturnValueInt(const CallbackInfo& info, int v)
86 {
87 info.GetReturnValue().Set(v);
88 }
89
90 template<typename CallbackInfo>
91 inline void v8SetReturnValueUnsigned(const CallbackInfo& info, unsigned v)
92 {
93 info.GetReturnValue().Set(v);
94 }
95
96 template<typename CallbackInfo>
97 inline void v8SetReturnValueNull(const CallbackInfo& info)
98 {
99 info.GetReturnValue().SetNull();
100 }
101
102 template<typename CallbackInfo>
103 inline void v8SetReturnValueUndefined(const CallbackInfo& info)
104 {
105 info.GetReturnValue().SetUndefined();
106 }
107
108 template<typename CallbackInfo>
109 inline void v8SetReturnValueEmptyString(const CallbackInfo& info)
110 {
111 info.GetReturnValue().SetEmptyString();
112 }
113
114 template <class CallbackInfo>
115 inline void v8SetReturnValueString(const CallbackInfo& info, const String& strin g, v8::Isolate* isolate)
116 {
117 if (string.isNull()) {
118 v8SetReturnValueEmptyString(info);
119 return;
120 }
121 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf o.GetReturnValue(), string.impl());
122 }
123
124 template <class CallbackInfo>
125 inline void v8SetReturnValueStringOrNull(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
126 {
127 if (string.isNull()) {
128 v8SetReturnValueNull(info);
129 return;
130 }
131 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf o.GetReturnValue(), string.impl());
132 }
133
134 template <class CallbackInfo>
135 inline void v8SetReturnValueStringOrUndefined(const CallbackInfo& info, const St ring& string, v8::Isolate* isolate)
136 {
137 if (string.isNull()) {
138 v8SetReturnValueUndefined(info);
139 return;
140 }
141 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf o.GetReturnValue(), string.impl());
142 }
143
144 // Convert v8::String to a WTF::String. If the V8 string is not already
145 // an external string then it is transformed into an external string at this
146 // point to avoid repeated conversions.
147 inline String toCoreString(v8::Handle<v8::String> value)
148 {
149 return v8StringToWebCoreString<String>(value, Externalize);
150 }
151
152 inline String toCoreStringWithNullCheck(v8::Handle<v8::String> value)
153 {
154 if (value.IsEmpty() || value->IsNull())
155 return String();
156 return toCoreString(value);
157 }
158
159 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::String> value)
160 {
161 if (value.IsEmpty() || value->IsNull() || value->IsUndefined())
162 return String();
163 return toCoreString(value);
164 }
165
166 inline AtomicString toCoreAtomicString(v8::Handle<v8::String> value)
167 {
168 return v8StringToWebCoreString<AtomicString>(value, Externalize);
169 }
170
171 // This method will return a null String if the v8::Value does not contain a v8: :String.
172 // It will not call ToString() on the v8::Value. If you want ToString() to be ca lled,
173 // please use the TONATIVE_FOR_V8STRINGRESOURCE_*() macros instead.
174 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::Value> value)
175 {
176 if (value.IsEmpty() || !value->IsString())
177 return String();
178 return toCoreString(value.As<v8::String>());
179 }
180
181 // Convert a string to a V8 string.
182 // Return a V8 external string that shares the underlying buffer with the given
183 // WebCore string. The reference counting mechanism is used to keep the
184 // underlying buffer alive while the string is still live in the V8 engine.
185 inline v8::Handle<v8::String> v8String(v8::Isolate* isolate, const String& strin g)
186 {
187 if (string.isNull())
188 return v8::String::Empty(isolate);
189 return V8PerIsolateData::from(isolate)->stringCache()->v8ExternalString(stri ng.impl(), isolate);
190 }
191
192 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* s tr)
193 {
194 ASSERT(isolate);
195 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString , strlen(str));
196 }
197
198 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* s tr, size_t length)
199 {
200 ASSERT(isolate);
201 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString , length);
202 }
203
204 inline v8::Handle<v8::Value> v8Undefined()
205 {
206 return v8::Handle<v8::Value>();
207 }
208
209 // Converts a DOM object to a v8 value.
210 // This is a no-inline version of toV8(). If you want to call toV8()
211 // without creating #include cycles, you can use this function instead.
212 // Each specialized implementation will be generated.
213 template<typename T>
214 v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationConte xt, v8::Isolate*);
215
216 template <typename T>
217 struct V8ValueTraits {
218 static v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Object > creationContext, v8::Isolate* isolate)
219 {
220 if (!WTF::getPtr(value))
221 return v8::Null(isolate);
222 return toV8NoInline(WTF::getPtr(value), creationContext, isolate);
223 }
224 };
225
226 template<>
227 struct V8ValueTraits<String> {
228 static inline v8::Handle<v8::Value> toV8Value(const String& value, v8::Handl e<v8::Object>, v8::Isolate* isolate)
229 {
230 return v8String(isolate, value);
231 }
232 };
233
234 template<>
235 struct V8ValueTraits<AtomicString> {
236 static inline v8::Handle<v8::Value> toV8Value(const AtomicString& value, v8: :Handle<v8::Object>, v8::Isolate* isolate)
237 {
238 return v8String(isolate, value);
239 }
240 };
241
242 template<size_t n>
243 struct V8ValueTraits<char[n]> {
244 static inline v8::Handle<v8::Value> toV8Value(char const (&value)[n], v8::Ha ndle<v8::Object>, v8::Isolate* isolate)
245 {
246 return v8String(isolate, value);
247 }
248 };
249
250 template<size_t n>
251 struct V8ValueTraits<char const[n]> {
252 static inline v8::Handle<v8::Value> toV8Value(char const (&value)[n], v8::Ha ndle<v8::Object>, v8::Isolate* isolate)
253 {
254 return v8String(isolate, value);
255 }
256 };
257
258 template<>
259 struct V8ValueTraits<const char*> {
260 static inline v8::Handle<v8::Value> toV8Value(const char* const& value, v8:: Handle<v8::Object>, v8::Isolate* isolate)
261 {
262 if (!value) {
263 // We return an empty string, not null, in order to align
264 // with v8String(isolate, String()).
265 return v8::String::Empty(isolate);
266 }
267 return v8String(isolate, value);
268 }
269 };
270
271 template<>
272 struct V8ValueTraits<char*> {
273 static inline v8::Handle<v8::Value> toV8Value(char* const& value, v8::Handle <v8::Object> object, v8::Isolate* isolate)
274 {
275 return V8ValueTraits<const char*>::toV8Value(value, object, isolate);
276 }
277 };
278
279 template<>
280 struct V8ValueTraits<int> {
281 static inline v8::Handle<v8::Value> toV8Value(const int& value, v8::Handle<v 8::Object>, v8::Isolate* isolate)
282 {
283 return v8::Integer::New(isolate, value);
284 }
285 };
286
287 template<>
288 struct V8ValueTraits<long> {
289 static inline v8::Handle<v8::Value> toV8Value(const long& value, v8::Handle< v8::Object>, v8::Isolate* isolate)
290 {
291 return v8::Integer::New(isolate, value);
292 }
293 };
294
295 template<>
296 struct V8ValueTraits<unsigned> {
297 static inline v8::Handle<v8::Value> toV8Value(const unsigned& value, v8::Han dle<v8::Object>, v8::Isolate* isolate)
298 {
299 return v8::Integer::NewFromUnsigned(isolate, value);
300 }
301 };
302
303 template<>
304 struct V8ValueTraits<unsigned long> {
305 static inline v8::Handle<v8::Value> toV8Value(const unsigned long& value, v8 ::Handle<v8::Object>, v8::Isolate* isolate)
306 {
307 return v8::Integer::NewFromUnsigned(isolate, value);
308 }
309 };
310
311 template<>
312 struct V8ValueTraits<float> {
313 static inline v8::Handle<v8::Value> toV8Value(const float& value, v8::Handle <v8::Object>, v8::Isolate* isolate)
314 {
315 return v8::Number::New(isolate, value);
316 }
317 };
318
319 template<>
320 struct V8ValueTraits<double> {
321 static inline v8::Handle<v8::Value> toV8Value(const double& value, v8::Handl e<v8::Object>, v8::Isolate* isolate)
322 {
323 return v8::Number::New(isolate, value);
324 }
325 };
326
327 template<>
328 struct V8ValueTraits<bool> {
329 static inline v8::Handle<v8::Value> toV8Value(const bool& value, v8::Handle< v8::Object>, v8::Isolate* isolate)
330 {
331 return v8::Boolean::New(isolate, value);
332 }
333 };
334
335 // V8NullType and V8UndefinedType are used only for the value conversion.
336 class V8NullType { };
337 class V8UndefinedType { };
338
339 template<>
340 struct V8ValueTraits<V8NullType> {
341 static inline v8::Handle<v8::Value> toV8Value(const V8NullType&, v8::Handle< v8::Object>, v8::Isolate* isolate)
342 {
343 return v8::Null(isolate);
344 }
345 };
346
347 template<>
348 struct V8ValueTraits<V8UndefinedType> {
349 static inline v8::Handle<v8::Value> toV8Value(const V8UndefinedType&, v8::Ha ndle<v8::Object>, v8::Isolate* isolate)
350 {
351 return v8::Undefined(isolate);
352 }
353 };
354
355 template<>
356 struct V8ValueTraits<ScriptValue> {
357 static inline v8::Handle<v8::Value> toV8Value(const ScriptValue& value, v8:: Handle<v8::Object>, v8::Isolate*)
358 {
359 return value.v8Value();
360 }
361 };
362
363 template<>
364 struct V8ValueTraits<v8::Handle<v8::Value> > {
365 static inline v8::Handle<v8::Value> toV8Value(const v8::Handle<v8::Value>& v alue, v8::Handle<v8::Object>, v8::Isolate*)
366 {
367 return value;
368 }
369 };
370
371 template<>
372 struct V8ValueTraits<v8::Local<v8::Value> > {
373 static inline v8::Handle<v8::Value> toV8Value(const v8::Local<v8::Value>& va lue, v8::Handle<v8::Object>, v8::Isolate*)
374 {
375 return value;
376 }
377 };
378
379 template<typename T, size_t inlineCapacity>
380 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Han dle<v8::Object> creationContext, v8::Isolate* isolate)
381 {
382 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
383 int index = 0;
384 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end();
385 typedef V8ValueTraits<T> TraitsType;
386 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi n(); iter != end; ++iter)
387 result->Set(v8::Integer::New(isolate, index++), TraitsType::toV8Value(*i ter, creationContext, isolate));
388 return result;
389 }
390
391 template <typename T, size_t inlineCapacity, typename Allocator>
392 struct V8ValueTraits<WTF::Vector<T, inlineCapacity, Allocator> > {
393 static v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity, Alloc ator>& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
394 {
395 return v8Array(value, creationContext, isolate);
396 }
397 };
398
399 template <typename T, size_t inlineCapacity>
400 struct V8ValueTraits<Vector<T, inlineCapacity> > {
401 static v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity>& valu e, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
402 {
403 return v8Array(value, creationContext, isolate);
404 }
405 };
406
407 // Conversion flags, used in toIntXX/toUIntXX.
408 enum IntegerConversionConfiguration {
409 NormalConversion,
410 EnforceRange,
411 Clamp
412 };
413
414 // Convert a value to a 8-bit signed integer. The conversion fails if the
415 // value cannot be converted to a number or the range violated per WebIDL:
416 // http://www.w3.org/TR/WebIDL/#es-byte
417 int8_t toInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionSt ate&);
418 inline int8_t toInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState )
419 {
420 return toInt8(value, NormalConversion, exceptionState);
421 }
422
423 // Convert a value to a 8-bit integer assuming the conversion cannot fail.
424 int8_t toInt8(v8::Handle<v8::Value>);
425
426 // Convert a value to a 8-bit unsigned integer. The conversion fails if the
427 // value cannot be converted to a number or the range violated per WebIDL:
428 // http://www.w3.org/TR/WebIDL/#es-octet
429 uint8_t toUInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception State&);
430 inline uint8_t toUInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionSta te)
431 {
432 return toUInt8(value, NormalConversion, exceptionState);
433 }
434
435 // Convert a value to a 8-bit unsigned integer assuming the conversion cannot fa il.
436 uint8_t toUInt8(v8::Handle<v8::Value>);
437
438 // Convert a value to a 16-bit signed integer. The conversion fails if the
439 // value cannot be converted to a number or the range violated per WebIDL:
440 // http://www.w3.org/TR/WebIDL/#es-short
441 int16_t toInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception State&);
442 inline int16_t toInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionSta te)
443 {
444 return toInt16(value, NormalConversion, exceptionState);
445 }
446
447 // Convert a value to a 16-bit integer assuming the conversion cannot fail.
448 int16_t toInt16(v8::Handle<v8::Value>);
449
450 // Convert a value to a 16-bit unsigned integer. The conversion fails if the
451 // value cannot be converted to a number or the range violated per WebIDL:
452 // http://www.w3.org/TR/WebIDL/#es-unsigned-short
453 uint16_t toUInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti onState&);
454 inline uint16_t toUInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionS tate)
455 {
456 return toUInt16(value, NormalConversion, exceptionState);
457 }
458
459 // Convert a value to a 16-bit unsigned integer assuming the conversion cannot f ail.
460 uint16_t toUInt16(v8::Handle<v8::Value>);
461
462 // Convert a value to a 32-bit signed integer. The conversion fails if the
463 // value cannot be converted to a number or the range violated per WebIDL:
464 // http://www.w3.org/TR/WebIDL/#es-long
465 int32_t toInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception State&);
466 inline int32_t toInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionSta te)
467 {
468 return toInt32(value, NormalConversion, exceptionState);
469 }
470
471 // Convert a value to a 32-bit integer assuming the conversion cannot fail.
472 int32_t toInt32(v8::Handle<v8::Value>);
473
474 // Convert a value to a 32-bit unsigned integer. The conversion fails if the
475 // value cannot be converted to a number or the range violated per WebIDL:
476 // http://www.w3.org/TR/WebIDL/#es-unsigned-long
477 uint32_t toUInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti onState&);
478 inline uint32_t toUInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionS tate)
479 {
480 return toUInt32(value, NormalConversion, exceptionState);
481 }
482
483 // Convert a value to a 32-bit unsigned integer assuming the conversion cannot f ail.
484 uint32_t toUInt32(v8::Handle<v8::Value>);
485
486 // Convert a value to a 64-bit signed integer. The conversion fails if the
487 // value cannot be converted to a number or the range violated per WebIDL:
488 // http://www.w3.org/TR/WebIDL/#es-long-long
489 int64_t toInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception State&);
490 inline int64_t toInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionSta te)
491 {
492 return toInt64(value, NormalConversion, exceptionState);
493 }
494
495 // Convert a value to a 64-bit integer assuming the conversion cannot fail.
496 int64_t toInt64(v8::Handle<v8::Value>);
497
498 // Convert a value to a 64-bit unsigned integer. The conversion fails if the
499 // value cannot be converted to a number or the range violated per WebIDL:
500 // http://www.w3.org/TR/WebIDL/#es-unsigned-long-long
501 uint64_t toUInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti onState&);
502 inline uint64_t toUInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionS tate)
503 {
504 return toUInt64(value, NormalConversion, exceptionState);
505 }
506
507 // Convert a value to a 64-bit unsigned integer assuming the conversion cannot f ail.
508 uint64_t toUInt64(v8::Handle<v8::Value>);
509
510 // Convert a value to a single precision float, which might fail.
511 float toFloat(v8::Handle<v8::Value>, ExceptionState&);
512
513 // Convert a value to a single precision float assuming the conversion cannot fa il.
514 inline float toFloat(v8::Local<v8::Value> value)
515 {
516 return static_cast<float>(value->NumberValue());
517 }
518
519 // Converts a value to a String, throwing if any code unit is outside 0-255.
520 String toByteString(v8::Handle<v8::Value>, ExceptionState&);
521
522 // Converts a value to a String, replacing unmatched UTF-16 surrogates with repl acement characters.
523 String toScalarValueString(v8::Handle<v8::Value>, ExceptionState&);
524
525 inline v8::Handle<v8::Boolean> v8Boolean(bool value, v8::Isolate* isolate)
526 {
527 return value ? v8::True(isolate) : v8::False(isolate);
528 }
529
530 inline double toCoreDate(v8::Handle<v8::Value> object)
531 {
532 if (object->IsDate())
533 return v8::Handle<v8::Date>::Cast(object)->ValueOf();
534 if (object->IsNumber())
535 return object->NumberValue();
536 return std::numeric_limits<double>::quiet_NaN();
537 }
538
539 inline v8::Handle<v8::Value> v8DateOrNaN(double value, v8::Isolate* isolate)
540 {
541 ASSERT(isolate);
542 return v8::Date::New(isolate, std::isfinite(value) ? value : std::numeric_li mits<double>::quiet_NaN());
543 }
544
545 template<class T> struct NativeValueTraits;
546
547 template<>
548 struct NativeValueTraits<String> {
549 static inline String nativeValue(const v8::Handle<v8::Value>& value, v8::Iso late* isolate)
550 {
551 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, String());
552 return stringValue;
553 }
554 };
555
556 template<>
557 struct NativeValueTraits<unsigned> {
558 static inline unsigned nativeValue(const v8::Handle<v8::Value>& value, v8::I solate* isolate)
559 {
560 return toUInt32(value);
561 }
562 };
563
564 template<>
565 struct NativeValueTraits<float> {
566 static inline float nativeValue(const v8::Handle<v8::Value>& value, v8::Isol ate* isolate)
567 {
568 return static_cast<float>(value->NumberValue());
569 }
570 };
571
572 template<>
573 struct NativeValueTraits<double> {
574 static inline double nativeValue(const v8::Handle<v8::Value>& value, v8::Iso late* isolate)
575 {
576 return static_cast<double>(value->NumberValue());
577 }
578 };
579
580 template<>
581 struct NativeValueTraits<v8::Handle<v8::Value> > {
582 static inline v8::Handle<v8::Value> nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
583 {
584 return value;
585 }
586 };
587
588 template<>
589 struct NativeValueTraits<ScriptValue> {
590 static inline ScriptValue nativeValue(const v8::Handle<v8::Value>& value, v8 ::Isolate* isolate)
591 {
592 return ScriptValue(ScriptState::current(isolate), value);
593 }
594 };
595
596 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length, v8:: Isolate*);
597
598 // Converts a JavaScript value to an array as per the Web IDL specification:
599 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
600 template <class T>
601 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isol ate* isolate)
602 {
603 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
604 uint32_t length = 0;
605 if (value->IsArray()) {
606 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
607 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
608 V8ThrowException::throwTypeError(ExceptionMessages::notAnArrayTypeArgume ntOrValue(argumentIndex), isolate);
609 return Vector<T>();
610 }
611
612 Vector<T> result;
613 result.reserveInitialCapacity(length);
614 typedef NativeValueTraits<T> TraitsType;
615 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
616 for (uint32_t i = 0; i < length; ++i)
617 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i), isolate)) ;
618 return result;
619 }
620
621 template <class T>
622 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int startIndex)
623 {
624 ASSERT(startIndex <= info.Length());
625 Vector<T> result;
626 typedef NativeValueTraits<T> TraitsType;
627 int length = info.Length();
628 result.reserveInitialCapacity(length);
629 for (int i = startIndex; i < length; ++i)
630 result.uncheckedAppend(TraitsType::nativeValue(info[i], info.GetIsolate( )));
631 return result;
632 }
633
634 // Validates that the passed object is a sequence type per WebIDL spec
635 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence
636 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint32_t& length, v8::Isolate* isolate)
637 {
638 // Attempt converting to a sequence if the value is not already an array but is
639 // any kind of object except for a native Date object or a native RegExp obj ect.
640 ASSERT(!value->IsArray());
641 // FIXME: Do we really need to special case Date and RegExp object?
642 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806
643 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) {
644 // The caller is responsible for reporting a TypeError.
645 return v8Undefined();
646 }
647
648 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
649 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
650 v8::Local<v8::String> lengthSymbol = v8AtomicString(isolate, "length");
651
652 // FIXME: The specification states that the length property should be used a s fallback, if value
653 // is not a platform object that supports indexed properties. If it supports indexed properties,
654 // length should actually be one greater than value’s maximum indexed proper ty index.
655 TONATIVE_EXCEPTION(v8::Local<v8::Value>, lengthValue, object->Get(lengthSymb ol));
656
657 if (lengthValue->IsUndefined() || lengthValue->IsNull()) {
658 // The caller is responsible for reporting a TypeError.
659 return v8Undefined();
660 }
661
662 TONATIVE_EXCEPTION(uint32_t, sequenceLength, lengthValue->Int32Value());
663 length = sequenceLength;
664 return v8Value;
665 }
666
667 v8::Isolate* toIsolate(ExecutionContext*);
668 v8::Isolate* toIsolate(LocalFrame*);
669
670 LocalDOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*);
671 LocalDOMWindow* toDOMWindow(v8::Handle<v8::Context>);
672 LocalDOMWindow* enteredDOMWindow(v8::Isolate*);
673 LocalDOMWindow* currentDOMWindow(v8::Isolate*);
674 LocalDOMWindow* callingDOMWindow(v8::Isolate*);
675 ExecutionContext* toExecutionContext(v8::Handle<v8::Context>);
676 ExecutionContext* currentExecutionContext(v8::Isolate*);
677 ExecutionContext* callingExecutionContext(v8::Isolate*);
678
679 // Returns a V8 context associated with a ExecutionContext and a DOMWrapperWorld .
680 // This method returns an empty context if there is no frame or the frame is alr eady detached.
681 v8::Local<v8::Context> toV8Context(ExecutionContext*, DOMWrapperWorld&);
682 // Returns a V8 context associated with a LocalFrame and a DOMWrapperWorld.
683 // This method returns an empty context if the frame is already detached.
684 v8::Local<v8::Context> toV8Context(LocalFrame*, DOMWrapperWorld&);
685
686 // Returns the frame object of the window object associated with
687 // a context, if the window is currently being displayed in the LocalFrame.
688 LocalFrame* toFrameIfNotDetached(v8::Handle<v8::Context>);
689
690 // If the current context causes out of memory, JavaScript setting
691 // is disabled and it returns true.
692 bool handleOutOfMemory();
693 void crashIfV8IsDead();
694
695 inline bool isUndefinedOrNull(v8::Handle<v8::Value> value)
696 {
697 return value->IsNull() || value->IsUndefined();
698 }
699 v8::Handle<v8::Function> getBoundFunction(v8::Handle<v8::Function>);
700
701 // Attaches |environment| to |function| and returns it.
702 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function, v8:: Handle<v8::Value> environment, v8::Isolate* isolate)
703 {
704 return v8::Function::New(isolate, function, environment);
705 }
706
707 // FIXME: This will be soon embedded in the generated code.
708 template<class Collection> static void indexedPropertyEnumerator(const v8::Prope rtyCallbackInfo<v8::Array>& info)
709 {
710 Collection* collection = toScriptWrappableBase(info.Holder())->toImpl<Collec tion>();
711 int length = collection->length();
712 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), length) ;
713 for (int i = 0; i < length; ++i) {
714 // FIXME: Do we need to check that the item function returns a non-null value for this index?
715 v8::Handle<v8::Integer> integer = v8::Integer::New(info.GetIsolate(), i) ;
716 properties->Set(integer, integer);
717 }
718 v8SetReturnValue(info, properties);
719 }
720
721 // These methods store hidden values into an array that is stored in the interna l field of a DOM wrapper.
722 void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cac heIndex, v8::Isolate*);
723 void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, in t cacheIndex, v8::Isolate*);
724 void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* oldVal ue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*);
725
726 PassRefPtr<JSONValue> v8ToJSONValue(v8::Isolate*, v8::Handle<v8::Value>, int);
727
728 // Result values for platform object 'deleter' methods,
729 // http://www.w3.org/TR/WebIDL/#delete
730 enum DeleteResult {
731 DeleteSuccess,
732 DeleteReject,
733 DeleteUnknownProperty
734 };
735
736 class V8TestingScope {
737 public:
738 explicit V8TestingScope(v8::Isolate*);
739 ScriptState* scriptState() const;
740 v8::Isolate* isolate() const;
741 ~V8TestingScope();
742
743 private:
744 v8::HandleScope m_handleScope;
745 v8::Context::Scope m_contextScope;
746 RefPtr<ScriptState> m_scriptState;
747 };
748
749 class V8RethrowTryCatchScope final {
750 public:
751 explicit V8RethrowTryCatchScope(v8::TryCatch& block) : m_block(block) { }
752 ~V8RethrowTryCatchScope()
753 {
754 // ReThrow() is a no-op if no exception has been caught, so always call.
755 m_block.ReThrow();
756 }
757
758 private:
759 v8::TryCatch& m_block;
760 };
761
762 } // namespace blink
763
764 #endif // SKY_ENGINE_BINDINGS_CORE_V8_V8BINDING_H_
OLDNEW
« no previous file with comments | « sky/engine/bindings/core/v8/V8AbstractEventListener.cpp ('k') | sky/engine/bindings/core/v8/V8Binding.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698