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 <utility> | 11 #include <utility> |
12 | 12 |
13 #include "bindings/core/v8/DOMDataStore.h" | 13 #include "bindings/core/v8/DOMDataStore.h" |
14 #include "bindings/core/v8/IDLDictionaryBase.h" | 14 #include "bindings/core/v8/IDLDictionaryBase.h" |
15 #include "bindings/core/v8/ScriptState.h" | 15 #include "bindings/core/v8/ScriptState.h" |
16 #include "bindings/core/v8/ScriptValue.h" | 16 #include "bindings/core/v8/ScriptValue.h" |
17 #include "bindings/core/v8/ScriptWrappable.h" | 17 #include "bindings/core/v8/ScriptWrappable.h" |
18 #include "bindings/core/v8/V8Binding.h" | 18 #include "bindings/core/v8/V8Binding.h" |
19 #include "core/CoreExport.h" | 19 #include "core/CoreExport.h" |
20 #include "core/dom/NotShared.h" | |
21 #include "platform/heap/Handle.h" | 20 #include "platform/heap/Handle.h" |
22 #include "platform/wtf/Forward.h" | 21 #include "platform/wtf/Forward.h" |
23 #include "v8/include/v8.h" | 22 #include "v8/include/v8.h" |
24 | 23 |
25 namespace blink { | 24 namespace blink { |
26 | 25 |
27 class DOMWindow; | |
28 class Dictionary; | |
29 class EventTarget; | |
30 | |
31 // ScriptWrappable | 26 // ScriptWrappable |
32 | 27 |
33 inline v8::Local<v8::Value> ToV8(ScriptWrappable* impl, | 28 inline v8::Local<v8::Value> ToV8(ScriptWrappable* impl, |
34 v8::Local<v8::Object> creation_context, | 29 v8::Local<v8::Object> creation_context, |
35 v8::Isolate* isolate) { | 30 v8::Isolate* isolate) { |
36 if (UNLIKELY(!impl)) | 31 if (UNLIKELY(!impl)) |
37 return v8::Null(isolate); | 32 return v8::Null(isolate); |
38 v8::Local<v8::Value> wrapper = DOMDataStore::GetWrapper(impl, isolate); | 33 v8::Local<v8::Value> wrapper = DOMDataStore::GetWrapper(impl, isolate); |
39 if (!wrapper.IsEmpty()) | 34 if (!wrapper.IsEmpty()) |
40 return wrapper; | 35 return wrapper; |
41 | 36 |
42 wrapper = impl->Wrap(isolate, creation_context); | 37 wrapper = impl->Wrap(isolate, creation_context); |
43 DCHECK(!wrapper.IsEmpty()); | 38 DCHECK(!wrapper.IsEmpty()); |
44 return wrapper; | 39 return wrapper; |
45 } | 40 } |
46 | 41 |
47 inline v8::Local<v8::Value> ToV8(Node* impl, | |
48 v8::Local<v8::Object> creation_context, | |
49 v8::Isolate* isolate) { | |
50 return ToV8(ScriptWrappable::FromNode(impl), creation_context, isolate); | |
51 } | |
52 | |
53 // Special versions for DOMWindow and EventTarget | |
54 | |
55 CORE_EXPORT v8::Local<v8::Value> ToV8(DOMWindow*, | |
56 v8::Local<v8::Object> creation_context, | |
57 v8::Isolate*); | |
58 CORE_EXPORT v8::Local<v8::Value> ToV8(EventTarget*, | |
59 v8::Local<v8::Object> creation_context, | |
60 v8::Isolate*); | |
61 | |
62 // Primitives | 42 // Primitives |
63 | 43 |
64 inline v8::Local<v8::Value> ToV8(const String& value, | 44 inline v8::Local<v8::Value> ToV8(const String& value, |
65 v8::Local<v8::Object> creation_context, | 45 v8::Local<v8::Object> creation_context, |
66 v8::Isolate* isolate) { | 46 v8::Isolate* isolate) { |
67 return V8String(isolate, value); | 47 return V8String(isolate, value); |
68 } | 48 } |
69 | 49 |
70 inline v8::Local<v8::Value> ToV8(const char* value, | 50 inline v8::Local<v8::Value> ToV8(const char* value, |
71 v8::Local<v8::Object> creation_context, | 51 v8::Local<v8::Object> creation_context, |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 inline v8::Local<v8::Value> ToV8(const ScriptValue& value, | 168 inline v8::Local<v8::Value> ToV8(const ScriptValue& value, |
189 v8::Local<v8::Object> creation_context, | 169 v8::Local<v8::Object> creation_context, |
190 v8::Isolate* isolate) { | 170 v8::Isolate* isolate) { |
191 if (value.IsEmpty()) | 171 if (value.IsEmpty()) |
192 return v8::Undefined(isolate); | 172 return v8::Undefined(isolate); |
193 return value.V8Value(); | 173 return value.V8Value(); |
194 } | 174 } |
195 | 175 |
196 // Dictionary | 176 // Dictionary |
197 | 177 |
198 inline v8::Local<v8::Value> ToV8(const Dictionary& value, | |
199 v8::Local<v8::Object> creation_context, | |
200 v8::Isolate* isolate) { | |
201 NOTREACHED(); | |
202 return v8::Undefined(isolate); | |
203 } | |
204 | |
205 inline v8::Local<v8::Value> ToV8(const IDLDictionaryBase& value, | 178 inline v8::Local<v8::Value> ToV8(const IDLDictionaryBase& value, |
206 v8::Local<v8::Object> creation_context, | 179 v8::Local<v8::Object> creation_context, |
207 v8::Isolate* isolate) { | 180 v8::Isolate* isolate) { |
208 return value.ToV8Impl(creation_context, isolate); | 181 return value.ToV8Impl(creation_context, isolate); |
209 } | 182 } |
210 | 183 |
211 // Array | 184 // Array |
212 | 185 |
213 // Declare the function here but define it later so it can call the ToV8() | 186 // Declare the function here but define it later so it can call the ToV8() |
214 // overloads below. | 187 // overloads below. |
215 template <typename Sequence> | 188 template <typename Sequence> |
216 inline v8::Local<v8::Value> ToV8SequenceInternal( | 189 inline v8::Local<v8::Value> ToV8SequenceInternal( |
217 const Sequence& sequence, | 190 const Sequence&, |
218 v8::Local<v8::Object> creation_context, | 191 v8::Local<v8::Object> creation_context, |
219 v8::Isolate*); | 192 v8::Isolate*); |
220 | 193 |
221 template <typename T, size_t inlineCapacity> | 194 template <typename T, size_t inlineCapacity> |
222 inline v8::Local<v8::Value> ToV8(const Vector<T, inlineCapacity>& value, | 195 inline v8::Local<v8::Value> ToV8(const Vector<T, inlineCapacity>& value, |
223 v8::Local<v8::Object> creation_context, | 196 v8::Local<v8::Object> creation_context, |
224 v8::Isolate* isolate) { | 197 v8::Isolate* isolate) { |
225 return ToV8SequenceInternal(value, creation_context, isolate); | 198 return ToV8SequenceInternal(value, creation_context, isolate); |
226 } | 199 } |
227 | 200 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 if (v8_value.IsEmpty()) | 242 if (v8_value.IsEmpty()) |
270 v8_value = v8::Undefined(isolate); | 243 v8_value = v8::Undefined(isolate); |
271 if (!V8CallBoolean(object->CreateDataProperty( | 244 if (!V8CallBoolean(object->CreateDataProperty( |
272 isolate->GetCurrentContext(), V8String(isolate, value[i].first), | 245 isolate->GetCurrentContext(), V8String(isolate, value[i].first), |
273 v8_value))) | 246 v8_value))) |
274 return v8::Local<v8::Value>(); | 247 return v8::Local<v8::Value>(); |
275 } | 248 } |
276 return object; | 249 return object; |
277 } | 250 } |
278 | 251 |
279 template <typename T> | |
280 inline v8::Local<v8::Value> ToV8(NotShared<T> value, | |
281 v8::Local<v8::Object> creation_context, | |
282 v8::Isolate* isolate) { | |
283 return ToV8(value.View(), creation_context, isolate); | |
284 } | |
285 | |
286 template <typename Sequence> | 252 template <typename Sequence> |
287 inline v8::Local<v8::Value> ToV8SequenceInternal( | 253 inline v8::Local<v8::Value> ToV8SequenceInternal( |
288 const Sequence& sequence, | 254 const Sequence& sequence, |
289 v8::Local<v8::Object> creation_context, | 255 v8::Local<v8::Object> creation_context, |
290 v8::Isolate* isolate) { | 256 v8::Isolate* isolate) { |
291 v8::Local<v8::Array> array; | 257 v8::Local<v8::Array> array; |
292 { | 258 { |
293 v8::Context::Scope context_scope(creation_context->CreationContext()); | 259 v8::Context::Scope context_scope(creation_context->CreationContext()); |
294 array = v8::Array::New(isolate, sequence.size()); | 260 array = v8::Array::New(isolate, sequence.size()); |
295 } | 261 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 | 296 |
331 // Cannot define in ScriptValue because of the circular dependency between toV8 | 297 // Cannot define in ScriptValue because of the circular dependency between toV8 |
332 // and ScriptValue | 298 // and ScriptValue |
333 template <typename T> | 299 template <typename T> |
334 inline ScriptValue ScriptValue::From(ScriptState* script_state, T&& value) { | 300 inline ScriptValue ScriptValue::From(ScriptState* script_state, T&& value) { |
335 return ScriptValue(script_state, ToV8(std::forward<T>(value), script_state)); | 301 return ScriptValue(script_state, ToV8(std::forward<T>(value), script_state)); |
336 } | 302 } |
337 | 303 |
338 } // namespace blink | 304 } // namespace blink |
339 | 305 |
340 #endif // ToV8_h | 306 #endif // ToV8ForPlatform_h |
OLD | NEW |