Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GIN_CONVERTER_H_ | 5 #ifndef GIN_CONVERTER_H_ |
| 6 #define GIN_CONVERTER_H_ | 6 #define GIN_CONVERTER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 #include "gin/gin_export.h" | 12 #include "gin/gin_export.h" |
| 13 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
| 14 | 14 |
| 15 namespace gin { | 15 namespace gin { |
| 16 | 16 |
| 17 template<typename KeyType> | |
| 18 bool SetProperty(v8::Isolate* isolate, | |
| 19 v8::Local<v8::Object> object, | |
| 20 KeyType key, | |
| 21 v8::Local<v8::Value> value) { | |
| 22 auto maybe = object->Set(isolate->GetCurrentContext(), key, value); | |
| 23 return !maybe.IsNothing() && maybe.FromJust(); | |
| 24 } | |
| 25 | |
| 17 template<typename T, typename Enable = void> | 26 template<typename T, typename Enable = void> |
| 18 struct Converter {}; | 27 struct Converter {}; |
| 19 | 28 |
| 20 template<> | 29 template<> |
| 21 struct GIN_EXPORT Converter<bool> { | 30 struct GIN_EXPORT Converter<bool> { |
| 22 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 31 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 23 bool val); | 32 bool val); |
| 24 static bool FromV8(v8::Isolate* isolate, | 33 static bool FromV8(v8::Isolate* isolate, |
| 25 v8::Handle<v8::Value> val, | 34 v8::Handle<v8::Value> val, |
| 26 bool* out); | 35 bool* out); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 struct GIN_EXPORT Converter<v8::Handle<v8::Value> > { | 147 struct GIN_EXPORT Converter<v8::Handle<v8::Value> > { |
| 139 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 148 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 140 v8::Handle<v8::Value> val); | 149 v8::Handle<v8::Value> val); |
| 141 static bool FromV8(v8::Isolate* isolate, | 150 static bool FromV8(v8::Isolate* isolate, |
| 142 v8::Handle<v8::Value> val, | 151 v8::Handle<v8::Value> val, |
| 143 v8::Handle<v8::Value>* out); | 152 v8::Handle<v8::Value>* out); |
| 144 }; | 153 }; |
| 145 | 154 |
| 146 template<typename T> | 155 template<typename T> |
| 147 struct Converter<std::vector<T> > { | 156 struct Converter<std::vector<T> > { |
| 157 // If this function fails to set an element due to an exception, this returns | |
| 158 // an empty handle. | |
| 148 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 159 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 149 const std::vector<T>& val) { | 160 const std::vector<T>& val) { |
| 150 v8::Handle<v8::Array> result( | 161 v8::Handle<v8::Array> result( |
| 151 v8::Array::New(isolate, static_cast<int>(val.size()))); | 162 v8::Array::New(isolate, static_cast<int>(val.size()))); |
| 152 for (size_t i = 0; i < val.size(); ++i) { | 163 for (size_t i = 0; i < val.size(); ++i) { |
| 153 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i])); | 164 if (!SetProperty(isolate, result, static_cast<int>(i), |
| 165 Converter<T>::ToV8(isolate, val[i]))) | |
| 166 return v8::Handle<v8::Value>(); | |
|
jochen (gone - plz use gerrit)
2015/04/28 11:14:20
we shouldn't return empty handles but MaybeLocal<>
bashi
2015/05/01 03:50:35
Changing return type requires 3-way CLs.
1. Add Ma
| |
| 154 } | 167 } |
| 155 return result; | 168 return result; |
| 156 } | 169 } |
| 157 | 170 |
| 158 static bool FromV8(v8::Isolate* isolate, | 171 static bool FromV8(v8::Isolate* isolate, |
| 159 v8::Handle<v8::Value> val, | 172 v8::Handle<v8::Value> val, |
| 160 std::vector<T>* out) { | 173 std::vector<T>* out) { |
| 161 if (!val->IsArray()) | 174 if (!val->IsArray()) |
| 162 return false; | 175 return false; |
| 163 | 176 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input, | 213 bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input, |
| 201 T* result) { | 214 T* result) { |
| 202 return Converter<T>::FromV8(isolate, input, result); | 215 return Converter<T>::FromV8(isolate, input, result); |
| 203 } | 216 } |
| 204 | 217 |
| 205 GIN_EXPORT std::string V8ToString(v8::Handle<v8::Value> value); | 218 GIN_EXPORT std::string V8ToString(v8::Handle<v8::Value> value); |
| 206 | 219 |
| 207 } // namespace gin | 220 } // namespace gin |
| 208 | 221 |
| 209 #endif // GIN_CONVERTER_H_ | 222 #endif // GIN_CONVERTER_H_ |
| OLD | NEW |