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

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

Issue 19969004: Update toNativeArray() / toRefPtrNativeArray() do not match Web IDL specification (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 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
OLDNEW
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
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
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(value));
haraken 2013/07/24 15:59:30 Please pass an Isolate to Local::New. It's sad th
do-not-use 2013/07/25 08:01:26 Ok, I forgot about this is the preferred way now.
412 return Vector<RefPtr<T> >(); 416 uint32_t length = 0;
417 if (value->IsArray()) {
418 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
419 } else {
arv (Not doing code reviews) 2013/07/24 16:38:13 else if
420 if (toV8Sequence(value, length, isolate).IsEmpty())
arv (Not doing code reviews) 2013/07/24 16:38:13 Are we converting this twice. Once to see if it is
421 return Vector<RefPtr<T> >();
422 }
413 423
414 Vector<RefPtr<T> > result; 424 Vector<RefPtr<T> > result;
415 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value)); 425 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
416 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(v8Value); 426 for (uint32_t i = 0; i < length; ++i) {
haraken 2013/07/24 15:59:30 I'm just curious about why you choose uint32_t ins
arv (Not doing code reviews) 2013/07/24 16:38:13 I feel like I'm missing something here. Where is t
do-not-use 2013/07/25 08:01:26 It is done in the pre-existing toV8Sequence() func
do-not-use 2013/07/25 08:01:26 The reason is that: - v8::Array::Length() returns
417 size_t length = array->Length(); 427 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 428
421 if (V8T::HasInstance(element, isolate, worldType(isolate))) { 429 if (V8T::HasInstance(element, isolate, worldType(isolate))) {
422 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(ele ment); 430 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::C ast(element);
423 result.append(V8T::toNative(object)); 431 result.append(V8T::toNative(elementObject));
424 } else { 432 } else {
425 if (success) 433 if (success)
426 *success = false; 434 *success = false;
427 throwTypeError("Invalid Array element type", isolate); 435 throwTypeError("Invalid Array element type", isolate);
428 return Vector<RefPtr<T> >(); 436 return Vector<RefPtr<T> >();
429 } 437 }
430 } 438 }
431 return result; 439 return result;
432 } 440 }
433 441
442 // Converts a JavaScript value to an array as per the Web IDL specification:
443 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
434 template <class T> 444 template <class T>
435 Vector<T> toNativeArray(v8::Handle<v8::Value> value) 445 Vector<T> toNativeArray(v8::Handle<v8::Value> value, v8::Isolate* isolate)
436 { 446 {
437 if (!value->IsArray()) 447 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value));
haraken 2013/07/24 15:59:30 Please pass an Isolate to Local::New.
do-not-use 2013/07/25 08:01:26 Ok.
438 return Vector<T>(); 448 uint32_t length = 0;
449 if (value->IsArray()) {
450 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
451 } else {
452 if (toV8Sequence(value, length, isolate).IsEmpty())
453 return Vector<T>();
454 }
439 455
440 Vector<T> result; 456 Vector<T> result;
441 typedef NativeValueTraits<T> TraitsType; 457 typedef NativeValueTraits<T> TraitsType;
442 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value)); 458 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
443 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(v8Value); 459 for (uint32_t i = 0; i < length; ++i)
444 size_t length = array->Length(); 460 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; 461 return result;
448 } 462 }
449 463
450 template <class T> 464 template <class T>
451 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& args, int startIndex) 465 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& args, int startIndex)
452 { 466 {
453 ASSERT(startIndex <= args.Length()); 467 ASSERT(startIndex <= args.Length());
454 Vector<T> result; 468 Vector<T> result;
455 typedef NativeValueTraits<T> TraitsType; 469 typedef NativeValueTraits<T> TraitsType;
456 int length = args.Length(); 470 int length = args.Length();
457 for (int i = startIndex; i < length; ++i) 471 for (int i = startIndex; i < length; ++i)
458 result.append(TraitsType::nativeValue(args[i])); 472 result.append(TraitsType::nativeValue(args[i]));
459 return result; 473 return result;
460 } 474 }
461 475
462 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac kInfo<v8::Value>& args); 476 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac kInfo<v8::Value>& args);
463 477
464 // Validates that the passed object is a sequence type per WebIDL spec 478 // Validates that the passed object is a sequence type per WebIDL spec
465 // http://www.w3.org/TR/2012/WD-WebIDL-20120207/#es-sequence 479 // 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) 480 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3 2_t& length, v8::Isolate* isolate)
467 { 481 {
468 if (!value->IsObject()) { 482 // Attempt converting to a sequence if the value is not already an array but is
483 // any kind of object except for a native Date object or a native RegExp object.
484 ASSERT(!value->IsArray());
485 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) {
haraken 2013/07/24 15:59:30 Would you add a test case about this? We might wan
arv (Not doing code reviews) 2013/07/24 16:38:13 Why are you special casing Date and RegExp? What a
do-not-use 2013/07/25 08:01:26 Yes, I will work on a test case for these.
do-not-use 2013/07/25 08:01:26 Because the Web IDL specification is special casin
arv (Not doing code reviews) 2013/07/25 18:24:41 Please link to WebIDL bug that might clarify why/i
469 throwTypeError(isolate); 486 throwTypeError(isolate);
470 return v8Undefined(); 487 return v8Undefined();
471 } 488 }
472 489
473 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value)); 490 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(value));
474 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); 491 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
475 492
493 // FIXME: The specification states that the length property should be us ed as fallback, if value
494 // is not a platform object that supports indexed properties. If it supp orts indexed properties,
495 // 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"))); 496 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(v8::String::Ne wSymbol("length")));
477 497
478 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { 498 if (lengthValue->IsUndefined() || lengthValue->IsNull()) {
479 throwTypeError(isolate); 499 throwTypeError(isolate);
480 return v8Undefined(); 500 return v8Undefined();
481 } 501 }
482 502
483 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); 503 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value());
484 length = sequenceLength; 504 length = sequenceLength;
485 505
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function, v8::Handle<v8::Value> environment) 615 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function, v8::Handle<v8::Value> environment)
596 { 616 {
597 return v8::FunctionTemplate::New(function, environment)->GetFunction(); 617 return v8::FunctionTemplate::New(function, environment)->GetFunction();
598 } 618 }
599 619
600 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, Script Wrappable*, v8::Handle<v8::String> key); 620 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, Script Wrappable*, v8::Handle<v8::String> key);
601 621
602 } // namespace WebCore 622 } // namespace WebCore
603 623
604 #endif // V8Binding_h 624 #endif // V8Binding_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698