Index: mojo/public/bindings/js/v8_conversions.h |
diff --git a/mojo/public/bindings/js/v8_conversions.h b/mojo/public/bindings/js/v8_conversions.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..232fdddc66dcbbc17e49d41bdf170dc4739d7962 |
--- /dev/null |
+++ b/mojo/public/bindings/js/v8_conversions.h |
@@ -0,0 +1,45 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MOJO_PUBLIC_BINDINGS_JS_V8_CONVERSIONS_H_ |
Aaron Boodman
2013/11/09 08:26:02
I don't understand why this isn't just another Wra
abarth-chromium
2013/11/09 08:52:37
I'll take a closer look.
|
+#define MOJO_PUBLIC_BINDINGS_JS_V8_CONVERSIONS_H_ |
+ |
+#include <vector> |
+ |
+#include "v8/include/v8.h" |
+ |
+namespace mojo { |
+namespace js { |
+ |
+template<typename T> |
+bool ToNativeArray(v8::Handle<v8::Array> array, std::vector<T>* result) { |
+ uint32_t length = array->Length(); |
+ result->reserve(length); |
+ for (uint32_t i = 0; i < length; ++i) { |
+ v8::Handle<v8::Value> value = array->Get(i); |
+ if (!value->IsObject()) |
+ return false; |
+ v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); |
+ if (!Wrapper<T>::HasInstance(object)) |
+ return false; |
+ result->push_back(Wrapper<T>::ToNative(object)); |
+ } |
+ return true; |
+} |
+ |
+template<typename T> |
+v8::Handle<v8::Array> ToObjectArray(v8::Isolate* isolate, |
+ const std::vector<T>& native) { |
+ uint32_t length = native.size(); |
+ v8::Handle<v8::Array> result = v8::Array::New(length); |
+ for (uint32_t i = 0; i < length; ++i) { |
+ result->Set(i, Wrapper<T>::ToObject(isolate, native[i])); |
+ } |
+ return result; |
+} |
+ |
+} // js |
+} // mojo |
+ |
+#endif // MOJO_PUBLIC_BINDINGS_JS_V8_CONVERSIONS_H_ |