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

Side by Side Diff: gin/converter.h

Issue 1152653004: Re-land: gin: Use V8 Maybe APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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 unified diff | Download patch
« no previous file with comments | « gin/arguments.h ('k') | gin/converter.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
27 template<typename T>
28 struct ToV8ReturnsMaybe {
29 static const bool value = false;
30 };
31
17 template<typename T, typename Enable = void> 32 template<typename T, typename Enable = void>
18 struct Converter {}; 33 struct Converter {};
19 34
20 template<> 35 template<>
21 struct GIN_EXPORT Converter<bool> { 36 struct GIN_EXPORT Converter<bool> {
22 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, 37 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
23 bool val); 38 bool val);
24 static bool FromV8(v8::Isolate* isolate, 39 static bool FromV8(v8::Isolate* isolate,
25 v8::Local<v8::Value> val, 40 v8::Local<v8::Value> val,
26 bool* out); 41 bool* out);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 struct GIN_EXPORT Converter<double> { 92 struct GIN_EXPORT Converter<double> {
78 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, 93 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
79 double val); 94 double val);
80 static bool FromV8(v8::Isolate* isolate, 95 static bool FromV8(v8::Isolate* isolate,
81 v8::Local<v8::Value> val, 96 v8::Local<v8::Value> val,
82 double* out); 97 double* out);
83 }; 98 };
84 99
85 template<> 100 template<>
86 struct GIN_EXPORT Converter<base::StringPiece> { 101 struct GIN_EXPORT Converter<base::StringPiece> {
102 // This crashes when val.size() > v8::String::kMaxLength.
87 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, 103 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
88 const base::StringPiece& val); 104 const base::StringPiece& val);
89 // No conversion out is possible because StringPiece does not contain storage. 105 // No conversion out is possible because StringPiece does not contain storage.
90 }; 106 };
91 107
92 template<> 108 template<>
93 struct GIN_EXPORT Converter<std::string> { 109 struct GIN_EXPORT Converter<std::string> {
110 // This crashes when val.size() > v8::String::kMaxLength.
94 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, 111 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
95 const std::string& val); 112 const std::string& val);
96 static bool FromV8(v8::Isolate* isolate, 113 static bool FromV8(v8::Isolate* isolate,
97 v8::Local<v8::Value> val, 114 v8::Local<v8::Value> val,
98 std::string* out); 115 std::string* out);
99 }; 116 };
100 117
101 template<> 118 template<>
102 struct GIN_EXPORT Converter<v8::Local<v8::Function> > { 119 struct GIN_EXPORT Converter<v8::Local<v8::Function> > {
103 static bool FromV8(v8::Isolate* isolate, 120 static bool FromV8(v8::Isolate* isolate,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 struct GIN_EXPORT Converter<v8::Local<v8::Value> > { 153 struct GIN_EXPORT Converter<v8::Local<v8::Value> > {
137 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, 154 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
138 v8::Local<v8::Value> val); 155 v8::Local<v8::Value> val);
139 static bool FromV8(v8::Isolate* isolate, 156 static bool FromV8(v8::Isolate* isolate,
140 v8::Local<v8::Value> val, 157 v8::Local<v8::Value> val,
141 v8::Local<v8::Value>* out); 158 v8::Local<v8::Value>* out);
142 }; 159 };
143 160
144 template<typename T> 161 template<typename T>
145 struct Converter<std::vector<T> > { 162 struct Converter<std::vector<T> > {
146 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, 163 static v8::MaybeLocal<v8::Value> ToV8(v8::Local<v8::Context> context,
147 const std::vector<T>& val) { 164 const std::vector<T>& val) {
165 v8::Isolate* isolate = context->GetIsolate();
148 v8::Local<v8::Array> result( 166 v8::Local<v8::Array> result(
149 v8::Array::New(isolate, static_cast<int>(val.size()))); 167 v8::Array::New(isolate, static_cast<int>(val.size())));
150 for (size_t i = 0; i < val.size(); ++i) { 168 for (uint32_t i = 0; i < val.size(); ++i) {
151 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i])); 169 auto maybe = result->Set(context, i, Converter<T>::ToV8(isolate, val[i]));
170 if (maybe.IsNothing() || !maybe.FromJust())
171 return v8::MaybeLocal<v8::Value>();
152 } 172 }
153 return result; 173 return result;
154 } 174 }
155 175
156 static bool FromV8(v8::Isolate* isolate, 176 static bool FromV8(v8::Isolate* isolate,
157 v8::Local<v8::Value> val, 177 v8::Local<v8::Value> val,
158 std::vector<T>* out) { 178 std::vector<T>* out) {
159 if (!val->IsArray()) 179 if (!val->IsArray())
160 return false; 180 return false;
161 181
162 std::vector<T> result; 182 std::vector<T> result;
163 v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val)); 183 v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
164 uint32_t length = array->Length(); 184 uint32_t length = array->Length();
165 for (uint32_t i = 0; i < length; ++i) { 185 for (uint32_t i = 0; i < length; ++i) {
186 v8::Local<v8::Value> v8_item;
187 if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&v8_item))
188 return false;
166 T item; 189 T item;
167 if (!Converter<T>::FromV8(isolate, array->Get(i), &item)) 190 if (!Converter<T>::FromV8(isolate, v8_item, &item))
168 return false; 191 return false;
169 result.push_back(item); 192 result.push_back(item);
170 } 193 }
171 194
172 out->swap(result); 195 out->swap(result);
173 return true; 196 return true;
174 } 197 }
175 }; 198 };
176 199
200 template<typename T>
201 struct ToV8ReturnsMaybe<std::vector<T>> {
202 static const bool value = true;
203 };
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 template<typename T>
212 v8::MaybeLocal<v8::Value> ConvertToV8(v8::Local<v8::Context> context, T input) {
213 return Converter<T>::ToV8(context, input);
214 }
215
216 template<typename T, bool = ToV8ReturnsMaybe<T>::value> struct ToV8Traits;
217
218 template <typename T>
219 struct ToV8Traits<T, true> {
220 static bool TryConvertToV8(v8::Isolate* isolate,
221 T input,
222 v8::Local<v8::Value>* output) {
223 auto maybe = ConvertToV8(isolate->GetCurrentContext(), input);
224 if (maybe.IsEmpty())
225 return false;
226 *output = maybe.ToLocalChecked();
227 return true;
228 }
229 };
230
231 template <typename T>
232 struct ToV8Traits<T, false> {
233 static bool TryConvertToV8(v8::Isolate* isolate,
234 T input,
235 v8::Local<v8::Value>* output) {
236 *output = ConvertToV8(isolate, input);
237 return true;
238 }
239 };
240
241 template <typename T>
242 bool TryConvertToV8(v8::Isolate* isolate,
243 T input,
244 v8::Local<v8::Value>* output) {
245 return ToV8Traits<T>::TryConvertToV8(isolate, input, output);
246 }
247
248 // This crashes when input.size() > v8::String::kMaxLength.
183 GIN_EXPORT inline v8::Local<v8::String> StringToV8( 249 GIN_EXPORT inline v8::Local<v8::String> StringToV8(
184 v8::Isolate* isolate, 250 v8::Isolate* isolate,
185 const base::StringPiece& input) { 251 const base::StringPiece& input) {
186 return ConvertToV8(isolate, input).As<v8::String>(); 252 return ConvertToV8(isolate, input).As<v8::String>();
187 } 253 }
188 254
255 // This crashes when input.size() > v8::String::kMaxLength.
189 GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate, 256 GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
190 const base::StringPiece& val); 257 const base::StringPiece& val);
191 258
192 template<typename T> 259 template<typename T>
193 bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input, 260 bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input,
194 T* result) { 261 T* result) {
195 return Converter<T>::FromV8(isolate, input, result); 262 return Converter<T>::FromV8(isolate, input, result);
196 } 263 }
197 264
198 GIN_EXPORT std::string V8ToString(v8::Local<v8::Value> value); 265 GIN_EXPORT std::string V8ToString(v8::Local<v8::Value> value);
199 266
200 } // namespace gin 267 } // namespace gin
201 268
202 #endif // GIN_CONVERTER_H_ 269 #endif // GIN_CONVERTER_H_
OLDNEW
« no previous file with comments | « gin/arguments.h ('k') | gin/converter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698