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

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

Issue 2316263003: Add toImplSequence() (Closed)
Patch Set: comments 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/core/v8/V8Binding.h
diff --git a/third_party/WebKit/Source/bindings/core/v8/V8Binding.h b/third_party/WebKit/Source/bindings/core/v8/V8Binding.h
index 3d5ea9e20f91f19ec507c2cf2548f142e2cfce26..edae174289e05b186734c56e38d12edfe2c969ba 100644
--- a/third_party/WebKit/Source/bindings/core/v8/V8Binding.h
+++ b/third_party/WebKit/Source/bindings/core/v8/V8Binding.h
@@ -728,6 +728,9 @@ VectorType toImplArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int
return result;
}
+// Gets an iterator from an Object.
+CORE_EXPORT v8::MaybeLocal<v8::Object> getEsIterator(v8::Isolate*, v8::Local<v8::Object>, ExceptionState&);
+
// Validates that the passed object is a sequence type per WebIDL spec
// http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence
inline bool toV8Sequence(v8::Local<v8::Value> value, uint32_t& length, v8::Isolate* isolate, ExceptionState& exceptionState)
@@ -880,6 +883,59 @@ CORE_EXPORT EventTarget* toEventTarget(v8::Isolate*, v8::Local<v8::Value>);
// to allocate it using alloca() in the callers stack frame.
CORE_EXPORT void toFlexibleArrayBufferView(v8::Isolate*, v8::Local<v8::Value>, FlexibleArrayBufferView&, void* storage = nullptr);
+// Converts a V8 value to an array (an IDL sequence) as per the WebIDL
+// specification: http://heycam.github.io/webidl/#es-sequence
+template <typename VectorType>
+VectorType toImplSequence(v8::Isolate* isolate, v8::Local<v8::Value> value, ExceptionState& exceptionState)
bashi 2016/09/09 00:54:23 Moved below toExecutionContext(). We can't call ca
+{
+ using ValueType = typename VectorType::ValueType;
+
+ if (!value->IsObject() || value->IsRegExp()) {
+ exceptionState.throwTypeError("The provided value cannot be converted to a sequence.");
+ return VectorType();
+ }
+
+ v8::Local<v8::Object> iterator;
+ if (!getEsIterator(isolate, value.As<v8::Object>(), exceptionState).ToLocal(&iterator))
+ return VectorType();
+
+ v8::Local<v8::String> nextKey = v8String(isolate, "next");
+ v8::Local<v8::String> valueKey = v8String(isolate, "value");
+ v8::Local<v8::String> doneKey = v8String(isolate, "done");
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
+ VectorType result;
+ while (true) {
haraken 2016/09/09 01:38:42 Maybe we want to share this code with DictionaryIt
+ v8::Local<v8::Value> next;
+ if (!iterator->Get(context, nextKey).ToLocal(&next))
+ return VectorType();
+ // TODO(bashi): Support callable objects.
+ if (!next->IsObject() || !next.As<v8::Object>()->IsFunction()) {
+ exceptionState.throwTypeError("Iterator.next should be callable.");
+ return VectorType();
+ }
+ v8::Local<v8::Value> nextResult;
+ if (!V8ScriptRunner::callFunction(next.As<v8::Function>(), toExecutionContext(context), iterator, 0, nullptr, isolate).ToLocal(&nextResult))
+ return VectorType();
+ if (!nextResult->IsObject()) {
+ exceptionState.throwTypeError("Iterator.next() did not return an object.");
+ return VectorType();
+ }
+ v8::Local<v8::Object> resultObject = nextResult.As<v8::Object>();
+ v8::Local<v8::Value> element;
+ v8::Local<v8::Value> done;
+ if (!resultObject->Get(context, valueKey).ToLocal(&element)
+ || !resultObject->Get(context, doneKey).ToLocal(&done))
+ return VectorType();
+ v8::Local<v8::Boolean> doneBoolean;
+ if (!done->ToBoolean(context).ToLocal(&doneBoolean))
+ return VectorType();
+ if (doneBoolean->Value())
bashi 2016/09/09 00:54:23 Don't append when |done| is true, as |value| is `u
+ break;
+ result.append(NativeValueTraits<ValueType>::nativeValue(isolate, element, exceptionState));
+ }
+ return result;
+}
+
// Installs all of the origin-trial-enabled V8 bindings for the given context
// and world, based on the trial tokens which have been added to the
// ExecutionContext. This should be called after the V8 context has been

Powered by Google App Engine
This is Rietveld 408576698