Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #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.
| |
| 6 #define MOJO_PUBLIC_BINDINGS_JS_V8_CONVERSIONS_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "v8/include/v8.h" | |
| 11 | |
| 12 namespace mojo { | |
| 13 namespace js { | |
| 14 | |
| 15 template<typename T> | |
| 16 bool ToNativeArray(v8::Handle<v8::Array> array, std::vector<T>* result) { | |
| 17 uint32_t length = array->Length(); | |
| 18 result->reserve(length); | |
| 19 for (uint32_t i = 0; i < length; ++i) { | |
| 20 v8::Handle<v8::Value> value = array->Get(i); | |
| 21 if (!value->IsObject()) | |
| 22 return false; | |
| 23 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); | |
| 24 if (!Wrapper<T>::HasInstance(object)) | |
| 25 return false; | |
| 26 result->push_back(Wrapper<T>::ToNative(object)); | |
| 27 } | |
| 28 return true; | |
| 29 } | |
| 30 | |
| 31 template<typename T> | |
| 32 v8::Handle<v8::Array> ToObjectArray(v8::Isolate* isolate, | |
| 33 const std::vector<T>& native) { | |
| 34 uint32_t length = native.size(); | |
| 35 v8::Handle<v8::Array> result = v8::Array::New(length); | |
| 36 for (uint32_t i = 0; i < length; ++i) { | |
| 37 result->Set(i, Wrapper<T>::ToObject(isolate, native[i])); | |
| 38 } | |
| 39 return result; | |
| 40 } | |
| 41 | |
| 42 } // js | |
| 43 } // mojo | |
| 44 | |
| 45 #endif // MOJO_PUBLIC_BINDINGS_JS_V8_CONVERSIONS_H_ | |
| OLD | NEW |