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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/V8Binding.h

Issue 2316263003: Add toImplSequence() (Closed)
Patch Set: style Created 4 years, 3 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
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 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 result.reserveInitialCapacity(length - startIndex); 721 result.reserveInitialCapacity(length - startIndex);
722 for (int i = startIndex; i < length; ++i) { 722 for (int i = startIndex; i < length; ++i) {
723 result.uncheckedAppend(TraitsType::nativeValue(info.GetIsolate(), in fo[i], exceptionState)); 723 result.uncheckedAppend(TraitsType::nativeValue(info.GetIsolate(), in fo[i], exceptionState));
724 if (exceptionState.hadException()) 724 if (exceptionState.hadException())
725 return VectorType(); 725 return VectorType();
726 } 726 }
727 } 727 }
728 return result; 728 return result;
729 } 729 }
730 730
731 // Gets an iterator from an Object.
732 v8::MaybeLocal<v8::Object> getIterator(v8::Local<v8::Object>, v8::Isolate*, Exce ptionState&);
haraken 2016/09/08 09:32:08 I don't have any strong opinion but might prefer r
bashi 2016/09/09 00:54:22 I think using MaybeLocal has two advantages: 1. Ca
733
734 // Converts a V8 value to an array (an IDL sequence) as per the WebIDL
735 // specification: http://heycam.github.io/webidl/#es-sequence
736 template <typename VectorType>
737 VectorType toImplSequence(v8::Local<v8::Value> value, v8::Isolate* isolate, Exce ptionState& exceptionState)
Yuki 2016/09/08 08:32:21 Could you make v8::Isolate* the first argument?
bashi 2016/09/09 00:54:23 Done. Having said that it's inconsistent with othe
738 {
739 typedef typename VectorType::ValueType ValueType;
Yuki 2016/09/08 08:32:21 I guess using VectorType::ValueType; does the
bashi 2016/09/09 00:54:22 Done.
740
741 if (!value->IsObject() || value->IsRegExp()) {
742 exceptionState.throwTypeError("The provided value cannot be converted to a sequence.");
743 return VectorType();
744 }
745
746 v8::Local<v8::Object> iterator;
747 if (!getIterator(value.As<v8::Object>(), isolate, exceptionState).ToLocal(&i terator))
748 return VectorType();
749
750 v8::Local<v8::String> nextKey = v8String(isolate, "next");
751 v8::Local<v8::String> valueKey = v8String(isolate, "value");
752 v8::Local<v8::String> doneKey = v8String(isolate, "done");
753 v8::Local<v8::Context> context = isolate->GetCurrentContext();
754 VectorType result;
755 while (true) {
756 v8::Local<v8::Value> next;
757 if (!iterator->Get(context, nextKey).ToLocal(&next))
758 return VectorType();
759 if (!next->IsObject() || !next.As<v8::Object>()->IsCallable()) {
760 exceptionState.throwTypeError("The next property should be callable. ");
Yuki 2016/09/08 08:32:21 I think "Iterator.next should be callable." is e
bashi 2016/09/09 00:54:23 Done.
761 return VectorType();
762 }
763 v8::Local<v8::Value> nextResult;
764 if (!next.As<v8::Object>()->CallAsFunction(context, iterator, 0, nullptr ).ToLocal(&nextResult))
765 return VectorType();
haraken 2016/09/08 09:32:08 Don't we need to throw an exception?
bashi 2016/09/09 00:54:23 No, as described in another reply.
766 if (!nextResult->IsObject()) {
767 exceptionState.throwTypeError("Iterator.next() does not return an ob ject.");
Yuki 2016/09/08 08:32:21 s/does/did/
bashi 2016/09/09 00:54:23 Done.
768 return VectorType();
769 }
770 v8::Local<v8::Object> resultObject = nextResult.As<v8::Object>();
771 v8::Local<v8::Value> element;
772 v8::Local<v8::Value> done;
773 if (!resultObject->Get(context, valueKey).ToLocal(&element)
774 || !resultObject->Get(context, doneKey).ToLocal(&done))
775 return VectorType();
haraken 2016/09/08 09:32:08 Ditto.
776 v8::Local<v8::Boolean> doneBoolean;
777 if (!done->ToBoolean(context).ToLocal(&doneBoolean))
778 return VectorType();
haraken 2016/09/08 09:32:08 Ditto.
779 result.uncheckedAppend(NativeValueTraits<ValueType>::nativeValue(isolate , element, exceptionState));
780 if (doneBoolean->Value())
781 break;
782 }
783 return result;
784 }
785
731 // Validates that the passed object is a sequence type per WebIDL spec 786 // Validates that the passed object is a sequence type per WebIDL spec
732 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence 787 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence
733 inline bool toV8Sequence(v8::Local<v8::Value> value, uint32_t& length, v8::Isola te* isolate, ExceptionState& exceptionState) 788 inline bool toV8Sequence(v8::Local<v8::Value> value, uint32_t& length, v8::Isola te* isolate, ExceptionState& exceptionState)
734 { 789 {
735 // Attempt converting to a sequence if the value is not already an array but is 790 // Attempt converting to a sequence if the value is not already an array but is
736 // any kind of object except for a native Date object or a native RegExp obj ect. 791 // any kind of object except for a native Date object or a native RegExp obj ect.
737 ASSERT(!value->IsArray()); 792 ASSERT(!value->IsArray());
738 // FIXME: Do we really need to special case Date and RegExp object? 793 // FIXME: Do we really need to special case Date and RegExp object?
739 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 794 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806
740 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { 795 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) {
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 typedef void (*InstallTemplateFunction)(v8::Isolate*, const DOMWrapperWorld&, v8 ::Local<v8::FunctionTemplate> interfaceTemplate); 1017 typedef void (*InstallTemplateFunction)(v8::Isolate*, const DOMWrapperWorld&, v8 ::Local<v8::FunctionTemplate> interfaceTemplate);
963 1018
964 // Freeze a V8 object. The type of the first parameter and the return value is 1019 // Freeze a V8 object. The type of the first parameter and the return value is
965 // intentionally v8::Value so that this function can wrap toV8(). 1020 // intentionally v8::Value so that this function can wrap toV8().
966 // If the argument isn't an object, this will crash. 1021 // If the argument isn't an object, this will crash.
967 CORE_EXPORT v8::Local<v8::Value> freezeV8Object(v8::Local<v8::Value>, v8::Isolat e*); 1022 CORE_EXPORT v8::Local<v8::Value> freezeV8Object(v8::Local<v8::Value>, v8::Isolat e*);
968 1023
969 } // namespace blink 1024 } // namespace blink
970 1025
971 #endif // V8Binding_h 1026 #endif // V8Binding_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698