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 |
11 #include "bindings/core/v8/DOMDataStore.h" | 11 #include "bindings/core/v8/DOMDataStore.h" |
| 12 #include "bindings/core/v8/ScriptState.h" |
12 #include "bindings/core/v8/ScriptValue.h" | 13 #include "bindings/core/v8/ScriptValue.h" |
13 #include "bindings/core/v8/ScriptWrappable.h" | 14 #include "bindings/core/v8/ScriptWrappable.h" |
14 #include "bindings/core/v8/V8Binding.h" | 15 #include "bindings/core/v8/V8Binding.h" |
15 #include "core/CoreExport.h" | 16 #include "core/CoreExport.h" |
16 #include "platform/heap/Handle.h" | 17 #include "platform/heap/Handle.h" |
17 #include "wtf/Forward.h" | 18 #include "wtf/Forward.h" |
| 19 #include <utility> |
18 #include <v8.h> | 20 #include <v8.h> |
19 | 21 |
20 namespace blink { | 22 namespace blink { |
21 | 23 |
22 class DOMWindow; | 24 class DOMWindow; |
23 class Dictionary; | 25 class Dictionary; |
24 class EventTarget; | 26 class EventTarget; |
25 class WorkerOrWorkletGlobalScope; | 27 class WorkerOrWorkletGlobalScope; |
26 | 28 |
27 // ScriptWrappable | 29 // ScriptWrappable |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 for (unsigned i = 0; i < value.size(); ++i) { | 236 for (unsigned i = 0; i < value.size(); ++i) { |
235 v8::Local<v8::Value> v8Value = toV8(value[i].second, creationContext, is
olate); | 237 v8::Local<v8::Value> v8Value = toV8(value[i].second, creationContext, is
olate); |
236 if (v8Value.IsEmpty()) | 238 if (v8Value.IsEmpty()) |
237 v8Value = v8::Undefined(isolate); | 239 v8Value = v8::Undefined(isolate); |
238 if (!v8CallBoolean(object->Set(isolate->GetCurrentContext(), v8String(is
olate, value[i].first), v8Value))) | 240 if (!v8CallBoolean(object->Set(isolate->GetCurrentContext(), v8String(is
olate, value[i].first), v8Value))) |
239 return v8::Local<v8::Value>(); | 241 return v8::Local<v8::Value>(); |
240 } | 242 } |
241 return object; | 243 return object; |
242 } | 244 } |
243 | 245 |
| 246 // In all cases allow script state instead of creation context + isolate. |
| 247 // Use this function only if the call site does not otherwise need the global, |
| 248 // since v8::Context::Global is heavy. |
| 249 template<typename T> |
| 250 inline v8::Local<v8::Value> toV8(T&& value, ScriptState* scriptState) |
| 251 { |
| 252 return toV8(std::forward<T>(value), scriptState->context()->Global(), script
State->isolate()); |
| 253 } |
| 254 |
244 // Only declare toV8(void*,...) for checking function overload mismatch. | 255 // Only declare toV8(void*,...) for checking function overload mismatch. |
245 // This toV8(void*,...) should be never used. So we will find mismatch | 256 // This toV8(void*,...) should be never used. So we will find mismatch |
246 // because of "unresolved external symbol". | 257 // because of "unresolved external symbol". |
247 // Without toV8(void*, ...), call to toV8 with T* will match with | 258 // Without toV8(void*, ...), call to toV8 with T* will match with |
248 // toV8(bool, ...) if T is not a subclass of ScriptWrappable or if T is | 259 // toV8(bool, ...) if T is not a subclass of ScriptWrappable or if T is |
249 // declared but not defined (so it's not clear that T is a subclass of | 260 // declared but not defined (so it's not clear that T is a subclass of |
250 // ScriptWrappable). | 261 // ScriptWrappable). |
251 // This hack helps detect such unwanted implicit conversions from T* to bool. | 262 // This hack helps detect such unwanted implicit conversions from T* to bool. |
252 v8::Local<v8::Value> toV8(void* value, v8::Local<v8::Object> creationContext, v8
::Isolate*) = delete; | 263 v8::Local<v8::Value> toV8(void* value, v8::Local<v8::Object> creationContext, v8
::Isolate*) = delete; |
253 | 264 |
254 } // namespace blink | 265 } // namespace blink |
255 | 266 |
256 #endif // ToV8_h | 267 #endif // ToV8_h |
OLD | NEW |