OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * Copyright (C) 2012 Ericsson AB. All rights reserved. | 3 * Copyright (C) 2012 Ericsson AB. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 68 |
69 // A helper for throwing JavaScript TypeError. | 69 // A helper for throwing JavaScript TypeError. |
70 v8::Handle<v8::Value> throwTypeError(v8::Isolate*); | 70 v8::Handle<v8::Value> throwTypeError(v8::Isolate*); |
71 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*); | 71 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*); |
72 | 72 |
73 // A helper for throwing JavaScript TypeError for not enough arguments. | 73 // A helper for throwing JavaScript TypeError for not enough arguments. |
74 v8::Handle<v8::Value> throwNotEnoughArgumentsError(v8::Isolate*); | 74 v8::Handle<v8::Value> throwNotEnoughArgumentsError(v8::Isolate*); |
75 | 75 |
76 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator(); | 76 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator(); |
77 | 77 |
| 78 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length,
v8::Isolate*); |
| 79 |
78 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v
8::Value>& args, int index) | 80 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v
8::Value>& args, int index) |
79 { | 81 { |
80 return index >= args.Length() ? v8::Local<v8::Value>() : args[index]; | 82 return index >= args.Length() ? v8::Local<v8::Value>() : args[index]; |
81 } | 83 } |
82 | 84 |
83 // Since v8::Null(isolate) crashes if we pass a null isolate, | 85 // Since v8::Null(isolate) crashes if we pass a null isolate, |
84 // we need to use v8NullWithCheck(isolate) if an isolate can be null. | 86 // we need to use v8NullWithCheck(isolate) if an isolate can be null. |
85 // | 87 // |
86 // FIXME: Remove all null isolates from V8 bindings, and remove v8NullWithCh
eck(isolate). | 88 // FIXME: Remove all null isolates from V8 bindings, and remove v8NullWithCh
eck(isolate). |
87 inline v8::Handle<v8::Value> v8NullWithCheck(v8::Isolate* isolate) | 89 inline v8::Handle<v8::Value> v8NullWithCheck(v8::Isolate* isolate) |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 }; | 397 }; |
396 | 398 |
397 template<> | 399 template<> |
398 struct NativeValueTraits<double> { | 400 struct NativeValueTraits<double> { |
399 static inline double nativeValue(const v8::Handle<v8::Value>& value) | 401 static inline double nativeValue(const v8::Handle<v8::Value>& value) |
400 { | 402 { |
401 return static_cast<double>(value->NumberValue()); | 403 return static_cast<double>(value->NumberValue()); |
402 } | 404 } |
403 }; | 405 }; |
404 | 406 |
| 407 // Converts a JavaScript value to an array as per the Web IDL specification: |
| 408 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array |
405 template <class T, class V8T> | 409 template <class T, class V8T> |
406 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, v8::Isol
ate* isolate, bool* success = 0) | 410 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, v8::Isol
ate* isolate, bool* success = 0) |
407 { | 411 { |
408 if (success) | 412 if (success) |
409 *success = true; | 413 *success = true; |
410 | 414 |
411 if (!value->IsArray()) | 415 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 416 uint32_t length = 0; |
| 417 if (value->IsArray()) |
| 418 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 419 else if (toV8Sequence(value, length, isolate).IsEmpty()) |
412 return Vector<RefPtr<T> >(); | 420 return Vector<RefPtr<T> >(); |
413 | 421 |
414 Vector<RefPtr<T> > result; | 422 Vector<RefPtr<T> > result; |
415 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value)); | 423 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
416 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(v8Value); | 424 for (uint32_t i = 0; i < length; ++i) { |
417 size_t length = array->Length(); | 425 v8::Handle<v8::Value> element = object->Get(i); |
418 for (size_t i = 0; i < length; ++i) { | |
419 v8::Handle<v8::Value> element = array->Get(i); | |
420 | 426 |
421 if (V8T::HasInstance(element, isolate, worldType(isolate))) { | 427 if (V8T::HasInstance(element, isolate, worldType(isolate))) { |
422 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(ele
ment); | 428 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::C
ast(element); |
423 result.append(V8T::toNative(object)); | 429 result.append(V8T::toNative(elementObject)); |
424 } else { | 430 } else { |
425 if (success) | 431 if (success) |
426 *success = false; | 432 *success = false; |
427 throwTypeError("Invalid Array element type", isolate); | 433 throwTypeError("Invalid Array element type", isolate); |
428 return Vector<RefPtr<T> >(); | 434 return Vector<RefPtr<T> >(); |
429 } | 435 } |
430 } | 436 } |
431 return result; | 437 return result; |
432 } | 438 } |
433 | 439 |
| 440 // Converts a JavaScript value to an array as per the Web IDL specification: |
| 441 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array |
434 template <class T> | 442 template <class T> |
435 Vector<T> toNativeArray(v8::Handle<v8::Value> value) | 443 Vector<T> toNativeArray(v8::Handle<v8::Value> value, v8::Isolate* isolate) |
436 { | 444 { |
437 if (!value->IsArray()) | 445 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 446 uint32_t length = 0; |
| 447 if (value->IsArray()) |
| 448 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 449 else if (toV8Sequence(value, length, isolate).IsEmpty()) |
438 return Vector<T>(); | 450 return Vector<T>(); |
439 | 451 |
440 Vector<T> result; | 452 Vector<T> result; |
441 typedef NativeValueTraits<T> TraitsType; | 453 typedef NativeValueTraits<T> TraitsType; |
442 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value)); | 454 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
443 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(v8Value); | 455 for (uint32_t i = 0; i < length; ++i) |
444 size_t length = array->Length(); | 456 result.append(TraitsType::nativeValue(object->Get(i))); |
445 for (size_t i = 0; i < length; ++i) | |
446 result.append(TraitsType::nativeValue(array->Get(i))); | |
447 return result; | 457 return result; |
448 } | 458 } |
449 | 459 |
450 template <class T> | 460 template <class T> |
451 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& args,
int startIndex) | 461 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& args,
int startIndex) |
452 { | 462 { |
453 ASSERT(startIndex <= args.Length()); | 463 ASSERT(startIndex <= args.Length()); |
454 Vector<T> result; | 464 Vector<T> result; |
455 typedef NativeValueTraits<T> TraitsType; | 465 typedef NativeValueTraits<T> TraitsType; |
456 int length = args.Length(); | 466 int length = args.Length(); |
457 for (int i = startIndex; i < length; ++i) | 467 for (int i = startIndex; i < length; ++i) |
458 result.append(TraitsType::nativeValue(args[i])); | 468 result.append(TraitsType::nativeValue(args[i])); |
459 return result; | 469 return result; |
460 } | 470 } |
461 | 471 |
462 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac
kInfo<v8::Value>& args); | 472 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac
kInfo<v8::Value>& args); |
463 | 473 |
464 // Validates that the passed object is a sequence type per WebIDL spec | 474 // Validates that the passed object is a sequence type per WebIDL spec |
465 // http://www.w3.org/TR/2012/WD-WebIDL-20120207/#es-sequence | 475 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence |
466 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3
2_t& length, v8::Isolate* isolate) | 476 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3
2_t& length, v8::Isolate* isolate) |
467 { | 477 { |
468 if (!value->IsObject()) { | 478 // Attempt converting to a sequence if the value is not already an array
but is |
| 479 // any kind of object except for a native Date object or a native RegExp
object. |
| 480 ASSERT(!value->IsArray()); |
| 481 // FIXME: Do we really need to special case Date and RegExp object? |
| 482 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 |
| 483 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { |
469 throwTypeError(isolate); | 484 throwTypeError(isolate); |
470 return v8Undefined(); | 485 return v8Undefined(); |
471 } | 486 } |
472 | 487 |
473 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value)); | 488 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
474 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 489 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
475 | 490 |
| 491 // FIXME: The specification states that the length property should be us
ed as fallback, if value |
| 492 // is not a platform object that supports indexed properties. If it supp
orts indexed properties, |
| 493 // length should actually be one greater than value’s maximum indexed pr
operty index. |
476 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(v8::String::Ne
wSymbol("length"))); | 494 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(v8::String::Ne
wSymbol("length"))); |
477 | 495 |
478 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { | 496 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { |
479 throwTypeError(isolate); | 497 throwTypeError(isolate); |
480 return v8Undefined(); | 498 return v8Undefined(); |
481 } | 499 } |
482 | 500 |
483 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); | 501 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); |
484 length = sequenceLength; | 502 length = sequenceLength; |
485 | 503 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function,
v8::Handle<v8::Value> environment) | 613 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function,
v8::Handle<v8::Value> environment) |
596 { | 614 { |
597 return v8::FunctionTemplate::New(function, environment)->GetFunction(); | 615 return v8::FunctionTemplate::New(function, environment)->GetFunction(); |
598 } | 616 } |
599 | 617 |
600 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, Script
Wrappable*, v8::Handle<v8::String> key); | 618 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, Script
Wrappable*, v8::Handle<v8::String> key); |
601 | 619 |
602 } // namespace WebCore | 620 } // namespace WebCore |
603 | 621 |
604 #endif // V8Binding_h | 622 #endif // V8Binding_h |
OLD | NEW |