OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 ToV8_h | 5 #ifndef ToV8_h |
6 #define ToV8_h | 6 #define ToV8_h |
7 | 7 |
8 // toV8() provides C++ -> V8 conversion. Note that toV8() can return an empty | 8 // toV8() provides C++ -> V8 conversion. Note that toV8() can return an empty |
9 // handle. Call sites must check IsEmpty() before using return value. | 9 // handle. Call sites must check IsEmpty() before using return value. |
10 | 10 |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 for (unsigned i = 0; i < value.size(); ++i) { | 238 for (unsigned i = 0; i < value.size(); ++i) { |
239 v8::Local<v8::Value> v8Value = toV8(value[i].second, object, isolate); | 239 v8::Local<v8::Value> v8Value = toV8(value[i].second, object, isolate); |
240 if (v8Value.IsEmpty()) | 240 if (v8Value.IsEmpty()) |
241 v8Value = v8::Undefined(isolate); | 241 v8Value = v8::Undefined(isolate); |
242 if (!v8CallBoolean(object->CreateDataProperty(isolate->GetCurrentContext
(), v8String(isolate, value[i].first), v8Value))) | 242 if (!v8CallBoolean(object->CreateDataProperty(isolate->GetCurrentContext
(), v8String(isolate, value[i].first), v8Value))) |
243 return v8::Local<v8::Value>(); | 243 return v8::Local<v8::Value>(); |
244 } | 244 } |
245 return object; | 245 return object; |
246 } | 246 } |
247 | 247 |
| 248 template <typename T> |
| 249 inline v8::Local<v8::Value> toV8(const HeapVector<std::pair<String, T>>& value,
v8::Local<v8::Object> creationContext, v8::Isolate* isolate) |
| 250 { |
| 251 v8::Local<v8::Object> object; |
| 252 { |
| 253 v8::Context::Scope contextScope(creationContext->CreationContext()); |
| 254 object = v8::Object::New(isolate); |
| 255 } |
| 256 for (unsigned i = 0; i < value.size(); ++i) { |
| 257 v8::Local<v8::Value> v8Value = toV8(value[i].second, object, isolate); |
| 258 if (v8Value.IsEmpty()) |
| 259 v8Value = v8::Undefined(isolate); |
| 260 if (!v8CallBoolean(object->CreateDataProperty(isolate->GetCurrentContext
(), v8String(isolate, value[i].first), v8Value))) |
| 261 return v8::Local<v8::Value>(); |
| 262 } |
| 263 return object; |
| 264 } |
| 265 |
248 // In all cases allow script state instead of creation context + isolate. | 266 // In all cases allow script state instead of creation context + isolate. |
249 // Use this function only if the call site does not otherwise need the global, | 267 // Use this function only if the call site does not otherwise need the global, |
250 // since v8::Context::Global is heavy. | 268 // since v8::Context::Global is heavy. |
251 template<typename T> | 269 template<typename T> |
252 inline v8::Local<v8::Value> toV8(T&& value, ScriptState* scriptState) | 270 inline v8::Local<v8::Value> toV8(T&& value, ScriptState* scriptState) |
253 { | 271 { |
254 return toV8(std::forward<T>(value), scriptState->context()->Global(), script
State->isolate()); | 272 return toV8(std::forward<T>(value), scriptState->context()->Global(), script
State->isolate()); |
255 } | 273 } |
256 | 274 |
257 // Only declare toV8(void*,...) for checking function overload mismatch. | 275 // Only declare toV8(void*,...) for checking function overload mismatch. |
258 // This toV8(void*,...) should be never used. So we will find mismatch | 276 // This toV8(void*,...) should be never used. So we will find mismatch |
259 // because of "unresolved external symbol". | 277 // because of "unresolved external symbol". |
260 // Without toV8(void*, ...), call to toV8 with T* will match with | 278 // Without toV8(void*, ...), call to toV8 with T* will match with |
261 // toV8(bool, ...) if T is not a subclass of ScriptWrappable or if T is | 279 // toV8(bool, ...) if T is not a subclass of ScriptWrappable or if T is |
262 // declared but not defined (so it's not clear that T is a subclass of | 280 // declared but not defined (so it's not clear that T is a subclass of |
263 // ScriptWrappable). | 281 // ScriptWrappable). |
264 // This hack helps detect such unwanted implicit conversions from T* to bool. | 282 // This hack helps detect such unwanted implicit conversions from T* to bool. |
265 v8::Local<v8::Value> toV8(void* value, v8::Local<v8::Object> creationContext, v8
::Isolate*) = delete; | 283 v8::Local<v8::Value> toV8(void* value, v8::Local<v8::Object> creationContext, v8
::Isolate*) = delete; |
266 | 284 |
267 // Cannot define in ScriptValue because of the circular dependency between toV8
and ScriptValue | 285 // Cannot define in ScriptValue because of the circular dependency between toV8
and ScriptValue |
268 template<typename T> | 286 template<typename T> |
269 inline ScriptValue ScriptValue::from(ScriptState* scriptState, T&& value) | 287 inline ScriptValue ScriptValue::from(ScriptState* scriptState, T&& value) |
270 { | 288 { |
271 return ScriptValue(scriptState, toV8(std::forward<T>(value), scriptState)); | 289 return ScriptValue(scriptState, toV8(std::forward<T>(value), scriptState)); |
272 } | 290 } |
273 | 291 |
274 } // namespace blink | 292 } // namespace blink |
275 | 293 |
276 #endif // ToV8_h | 294 #endif // ToV8_h |
OLD | NEW |