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

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

Issue 68533014: Improve Blob constructor error messages. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 1 month 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
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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*); 67 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*);
68 68
69 // A helper for throwing JavaScript TypeError. 69 // A helper for throwing JavaScript TypeError.
70 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*); 70 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*);
71 71
72 // FIXME: Remove this once we kill its callers. 72 // FIXME: Remove this once we kill its callers.
73 v8::Handle<v8::Value> throwUninformativeAndGenericTypeError(v8::Isolate*); 73 v8::Handle<v8::Value> throwUninformativeAndGenericTypeError(v8::Isolate*);
74 74
75 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator(); 75 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator();
76 76
77 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length, bool& notASequence, v8::Isolate*); 77 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length, v8::Isolate*);
78 78
79 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v 8::Value>& info, int index) 79 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v 8::Value>& info, int index)
80 { 80 {
81 return index >= info.Length() ? v8::Local<v8::Value>() : info[index]; 81 return index >= info.Length() ? v8::Local<v8::Value>() : info[index];
82 } 82 }
83 83
84 // Since v8::Null(isolate) crashes if we pass a null isolate, 84 // Since v8::Null(isolate) crashes if we pass a null isolate,
85 // we need to use v8NullWithCheck(isolate) if an isolate can be null. 85 // we need to use v8NullWithCheck(isolate) if an isolate can be null.
86 // 86 //
87 // FIXME: Remove all null isolates from V8 bindings, and remove v8NullWithCh eck(isolate). 87 // FIXME: Remove all null isolates from V8 bindings, and remove v8NullWithCh eck(isolate).
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 } 486 }
487 487
488 template <class T, class V8T> 488 template <class T, class V8T>
489 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argu mentIndex, v8::Isolate* isolate, bool* success = 0) 489 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argu mentIndex, v8::Isolate* isolate, bool* success = 0)
490 { 490 {
491 if (success) 491 if (success)
492 *success = true; 492 *success = true;
493 493
494 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); 494 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
495 uint32_t length = 0; 495 uint32_t length = 0;
496 bool notASequence = false;
497 if (value->IsArray()) { 496 if (value->IsArray()) {
498 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); 497 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
499 } else if (toV8Sequence(value, length, notASequence, isolate).IsEmpty()) { 498 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
500 if (notASequence) 499 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argu mentIndex), isolate);
501 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue( argumentIndex), isolate);
502 return Vector<RefPtr<T> >(); 500 return Vector<RefPtr<T> >();
503 } 501 }
504 502
505 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su ccess); 503 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su ccess);
506 } 504 }
507 505
508 template <class T, class V8T> 506 template <class T, class V8T>
509 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const St ring& propertyName, v8::Isolate* isolate, bool* success = 0) 507 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const St ring& propertyName, v8::Isolate* isolate, bool* success = 0)
510 { 508 {
511 if (success) 509 if (success)
512 *success = true; 510 *success = true;
513 511
514 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); 512 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
515 uint32_t length = 0; 513 uint32_t length = 0;
516 bool notASequence = false;
517 if (value->IsArray()) { 514 if (value->IsArray()) {
518 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); 515 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
519 } else if (toV8Sequence(value, length, notASequence, isolate).IsEmpty()) { 516 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
520 if (notASequence) 517 throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyN ame), isolate);
521 throwTypeError(ExceptionMessages::notASequenceTypeProperty(prope rtyName), isolate);
522 return Vector<RefPtr<T> >(); 518 return Vector<RefPtr<T> >();
523 } 519 }
524 520
525 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su ccess); 521 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su ccess);
526 } 522 }
527 523
528 // Converts a JavaScript value to an array as per the Web IDL specification: 524 // Converts a JavaScript value to an array as per the Web IDL specification:
529 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array 525 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
530 template <class T> 526 template <class T>
531 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8:: Isolate* isolate) 527 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8:: Isolate* isolate)
532 { 528 {
533 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); 529 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
534 uint32_t length = 0; 530 uint32_t length = 0;
535 bool notASequence = false;
536 if (value->IsArray()) { 531 if (value->IsArray()) {
537 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); 532 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
538 } else if (toV8Sequence(value, length, notASequence, isolate).IsEmpty()) { 533 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
539 if (notASequence) 534 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argu mentIndex), isolate);
540 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue( argumentIndex), isolate);
541 return Vector<T>(); 535 return Vector<T>();
542 } 536 }
543 537
544 Vector<T> result; 538 Vector<T> result;
545 result.reserveInitialCapacity(length); 539 result.reserveInitialCapacity(length);
546 typedef NativeValueTraits<T> TraitsType; 540 typedef NativeValueTraits<T> TraitsType;
547 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); 541 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
548 for (uint32_t i = 0; i < length; ++i) 542 for (uint32_t i = 0; i < length; ++i)
549 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i))); 543 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i)));
550 return result; 544 return result;
551 } 545 }
552 546
553 template <class T> 547 template <class T>
554 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int startIndex) 548 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int startIndex)
555 { 549 {
556 ASSERT(startIndex <= info.Length()); 550 ASSERT(startIndex <= info.Length());
557 Vector<T> result; 551 Vector<T> result;
558 typedef NativeValueTraits<T> TraitsType; 552 typedef NativeValueTraits<T> TraitsType;
559 int length = info.Length(); 553 int length = info.Length();
560 result.reserveInitialCapacity(length); 554 result.reserveInitialCapacity(length);
561 for (int i = startIndex; i < length; ++i) 555 for (int i = startIndex; i < length; ++i)
562 result.uncheckedAppend(TraitsType::nativeValue(info[i])); 556 result.uncheckedAppend(TraitsType::nativeValue(info[i]));
563 return result; 557 return result;
564 } 558 }
565 559
566 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac kInfo<v8::Value>&); 560 Vector<v8::Handle<v8::Value> > toVectorOfArguments(const v8::FunctionCallbac kInfo<v8::Value>&);
567 561
568 // Validates that the passed object is a sequence type per WebIDL spec 562 // Validates that the passed object is a sequence type per WebIDL spec
569 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence 563 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence
570 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3 2_t& length, bool& notASequence, v8::Isolate* isolate) 564 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3 2_t& length, v8::Isolate* isolate)
571 { 565 {
572 // Attempt converting to a sequence if the value is not already an array but is 566 // Attempt converting to a sequence if the value is not already an array but is
573 // any kind of object except for a native Date object or a native RegExp object. 567 // any kind of object except for a native Date object or a native RegExp object.
574 ASSERT(!value->IsArray()); 568 ASSERT(!value->IsArray());
575 // FIXME: Do we really need to special case Date and RegExp object? 569 // FIXME: Do we really need to special case Date and RegExp object?
576 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 570 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806
577 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { 571 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) {
578 // Signal that the caller must handle the type error. 572 // The caller is responsible for reporting a TypeError.
579 notASequence = true;
580 return v8Undefined(); 573 return v8Undefined();
581 } 574 }
582 575
583 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); 576 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
584 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); 577 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
585 578
586 // FIXME: The specification states that the length property should be us ed as fallback, if value 579 // FIXME: The specification states that the length property should be us ed as fallback, if value
587 // is not a platform object that supports indexed properties. If it supp orts indexed properties, 580 // is not a platform object that supports indexed properties. If it supp orts indexed properties,
588 // length should actually be one greater than value’s maximum indexed pr operty index. 581 // length should actually be one greater than value’s maximum indexed pr operty index.
589 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(v8::String::Ne wSymbol("length"))); 582 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(v8::String::Ne wSymbol("length")));
590 583
591 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { 584 if (lengthValue->IsUndefined() || lengthValue->IsNull()) {
592 notASequence = true; 585 // The caller is responsible for reporting a TypeError.
593 return v8Undefined(); 586 return v8Undefined();
594 } 587 }
595 588
596 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); 589 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value());
597 length = sequenceLength; 590 length = sequenceLength;
598 591
599 return v8Value; 592 return v8Value;
600 } 593 }
601 594
602 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*); 595 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 706
714 v8::Isolate* mainThreadIsolate(); 707 v8::Isolate* mainThreadIsolate();
715 v8::Isolate* toIsolate(ExecutionContext*); 708 v8::Isolate* toIsolate(ExecutionContext*);
716 v8::Isolate* toIsolate(Frame*); 709 v8::Isolate* toIsolate(Frame*);
717 710
718 // Can only be called by blink::initialize 711 // Can only be called by blink::initialize
719 void setMainThreadIsolate(v8::Isolate*); 712 void setMainThreadIsolate(v8::Isolate*);
720 } // namespace WebCore 713 } // namespace WebCore
721 714
722 #endif // V8Binding_h 715 #endif // V8Binding_h
OLDNEW
« no previous file with comments | « LayoutTests/fast/files/script-tests/blob-constructor.js ('k') | Source/bindings/v8/V8Utilities.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698