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/logging.h" | |
11 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
12 #include "gin/gin_export.h" | 13 #include "gin/gin_export.h" |
13 #include "v8/include/v8.h" | 14 #include "v8/include/v8.h" |
14 | 15 |
15 namespace gin { | 16 namespace gin { |
16 | 17 |
18 template<typename KeyType> | |
19 bool SetProperty(v8::Isolate* isolate, | |
20 v8::Local<v8::Object> object, | |
21 KeyType key, | |
22 v8::Local<v8::Value> value) { | |
23 auto maybe = object->Set(isolate->GetCurrentContext(), key, value); | |
24 return !maybe.IsNothing() && maybe.FromJust(); | |
25 } | |
26 | |
17 template<typename T, typename Enable = void> | 27 template<typename T, typename Enable = void> |
18 struct Converter {}; | 28 struct Converter {}; |
19 | 29 |
20 template<> | 30 template<> |
21 struct GIN_EXPORT Converter<bool> { | 31 struct GIN_EXPORT Converter<bool> { |
22 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 32 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
23 bool val); | 33 bool val); |
24 static bool FromV8(v8::Isolate* isolate, | 34 static bool FromV8(v8::Isolate* isolate, |
25 v8::Local<v8::Value> val, | 35 v8::Local<v8::Value> val, |
26 bool* out); | 36 bool* out); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 struct GIN_EXPORT Converter<double> { | 87 struct GIN_EXPORT Converter<double> { |
78 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 88 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
79 double val); | 89 double val); |
80 static bool FromV8(v8::Isolate* isolate, | 90 static bool FromV8(v8::Isolate* isolate, |
81 v8::Local<v8::Value> val, | 91 v8::Local<v8::Value> val, |
82 double* out); | 92 double* out); |
83 }; | 93 }; |
84 | 94 |
85 template<> | 95 template<> |
86 struct GIN_EXPORT Converter<base::StringPiece> { | 96 struct GIN_EXPORT Converter<base::StringPiece> { |
97 // This crashes when val.size() > v8::String::kMaxLength. | |
87 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 98 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
88 const base::StringPiece& val); | 99 const base::StringPiece& val); |
89 // No conversion out is possible because StringPiece does not contain storage. | 100 // No conversion out is possible because StringPiece does not contain storage. |
90 }; | 101 }; |
91 | 102 |
92 template<> | 103 template<> |
93 struct GIN_EXPORT Converter<std::string> { | 104 struct GIN_EXPORT Converter<std::string> { |
105 // This crashes when val.size() > v8::String::kMaxLength. | |
94 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 106 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
95 const std::string& val); | 107 const std::string& val); |
96 static bool FromV8(v8::Isolate* isolate, | 108 static bool FromV8(v8::Isolate* isolate, |
97 v8::Local<v8::Value> val, | 109 v8::Local<v8::Value> val, |
98 std::string* out); | 110 std::string* out); |
99 }; | 111 }; |
100 | 112 |
101 template<> | 113 template<> |
102 struct GIN_EXPORT Converter<v8::Local<v8::Function> > { | 114 struct GIN_EXPORT Converter<v8::Local<v8::Function> > { |
103 static bool FromV8(v8::Isolate* isolate, | 115 static bool FromV8(v8::Isolate* isolate, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 struct GIN_EXPORT Converter<v8::Local<v8::Value> > { | 148 struct GIN_EXPORT Converter<v8::Local<v8::Value> > { |
137 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 149 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
138 v8::Local<v8::Value> val); | 150 v8::Local<v8::Value> val); |
139 static bool FromV8(v8::Isolate* isolate, | 151 static bool FromV8(v8::Isolate* isolate, |
140 v8::Local<v8::Value> val, | 152 v8::Local<v8::Value> val, |
141 v8::Local<v8::Value>* out); | 153 v8::Local<v8::Value>* out); |
142 }; | 154 }; |
143 | 155 |
144 template<typename T> | 156 template<typename T> |
145 struct Converter<std::vector<T> > { | 157 struct Converter<std::vector<T> > { |
158 // DEPRECATED. Use Maybe version. | |
jochen (gone - plz use gerrit)
2015/05/15 15:40:39
how many callsites are there?
bashi
2015/05/19 02:09:08
- Some testing code use this ToV8()
- Mojo uses th
| |
146 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 159 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
147 const std::vector<T>& val) { | 160 const std::vector<T>& val) { |
148 v8::Local<v8::Array> result( | 161 v8::Local<v8::Array> result( |
149 v8::Array::New(isolate, static_cast<int>(val.size()))); | 162 v8::Array::New(isolate, static_cast<int>(val.size()))); |
150 for (size_t i = 0; i < val.size(); ++i) { | 163 for (size_t i = 0; i < val.size(); ++i) { |
151 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i])); | 164 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i])); |
152 } | 165 } |
153 return result; | 166 return result; |
154 } | 167 } |
168 static v8::MaybeLocal<v8::Value> ToV8(v8::Local<v8::Context> context, | |
169 const std::vector<T>& val) { | |
170 v8::Isolate* isolate = context->GetIsolate(); | |
171 v8::Local<v8::Array> result( | |
172 v8::Array::New(isolate, static_cast<int>(val.size()))); | |
173 for (size_t i = 0; i < val.size(); ++i) { | |
174 auto maybe = result->Set(context, i, Converter<T>::ToV8(isolate, val[i])); | |
175 if (maybe.IsNothing() || !maybe.FromJust()) | |
176 return v8::MaybeLocal<v8::Value>(); | |
177 } | |
178 return result; | |
179 } | |
155 | 180 |
156 static bool FromV8(v8::Isolate* isolate, | 181 static bool FromV8(v8::Isolate* isolate, |
157 v8::Local<v8::Value> val, | 182 v8::Local<v8::Value> val, |
158 std::vector<T>* out) { | 183 std::vector<T>* out) { |
159 if (!val->IsArray()) | 184 if (!val->IsArray()) |
160 return false; | 185 return false; |
161 | 186 |
162 std::vector<T> result; | 187 std::vector<T> result; |
163 v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val)); | 188 v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val)); |
164 uint32_t length = array->Length(); | 189 uint32_t length = array->Length(); |
165 for (uint32_t i = 0; i < length; ++i) { | 190 for (uint32_t i = 0; i < length; ++i) { |
191 v8::Local<v8::Value> v8_item; | |
192 if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&v8_item)) | |
193 return false; | |
166 T item; | 194 T item; |
167 if (!Converter<T>::FromV8(isolate, array->Get(i), &item)) | 195 if (!Converter<T>::FromV8(isolate, v8_item, &item)) |
168 return false; | 196 return false; |
169 result.push_back(item); | 197 result.push_back(item); |
170 } | 198 } |
171 | 199 |
172 out->swap(result); | 200 out->swap(result); |
173 return true; | 201 return true; |
174 } | 202 } |
175 }; | 203 }; |
176 | 204 |
177 // Convenience functions that deduce T. | 205 // Convenience functions that deduce T. |
178 template<typename T> | 206 template<typename T> |
179 v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T input) { | 207 v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T input) { |
180 return Converter<T>::ToV8(isolate, input); | 208 return Converter<T>::ToV8(isolate, input); |
181 } | 209 } |
182 | 210 |
211 // This crashes when input.size() > v8::String::kMaxLength. | |
183 GIN_EXPORT inline v8::Local<v8::String> StringToV8( | 212 GIN_EXPORT inline v8::Local<v8::String> StringToV8( |
184 v8::Isolate* isolate, | 213 v8::Isolate* isolate, |
185 const base::StringPiece& input) { | 214 const base::StringPiece& input) { |
186 return ConvertToV8(isolate, input).As<v8::String>(); | 215 return ConvertToV8(isolate, input).As<v8::String>(); |
187 } | 216 } |
188 | 217 |
218 // This crashes when input.size() > v8::String::kMaxLength. | |
189 GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate, | 219 GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate, |
190 const base::StringPiece& val); | 220 const base::StringPiece& val); |
191 | 221 |
192 template<typename T> | 222 template<typename T> |
193 bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input, | 223 bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input, |
194 T* result) { | 224 T* result) { |
195 return Converter<T>::FromV8(isolate, input, result); | 225 return Converter<T>::FromV8(isolate, input, result); |
196 } | 226 } |
197 | 227 |
198 GIN_EXPORT std::string V8ToString(v8::Local<v8::Value> value); | 228 GIN_EXPORT std::string V8ToString(v8::Local<v8::Value> value); |
199 | 229 |
200 } // namespace gin | 230 } // namespace gin |
201 | 231 |
202 #endif // GIN_CONVERTER_H_ | 232 #endif // GIN_CONVERTER_H_ |
OLD | NEW |