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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/ToV8.h

Issue 2595543003: Rename toV8(...) function in Blink to ToV8(...). (Closed)
Patch Set: Rebasing... Created 3 years, 11 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
OLDNEW
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/IDLDictionaryBase.h" 12 #include "bindings/core/v8/IDLDictionaryBase.h"
13 #include "bindings/core/v8/ScriptState.h" 13 #include "bindings/core/v8/ScriptState.h"
14 #include "bindings/core/v8/ScriptValue.h" 14 #include "bindings/core/v8/ScriptValue.h"
15 #include "bindings/core/v8/ScriptWrappable.h" 15 #include "bindings/core/v8/ScriptWrappable.h"
16 #include "bindings/core/v8/V8Binding.h" 16 #include "bindings/core/v8/V8Binding.h"
17 #include "core/CoreExport.h" 17 #include "core/CoreExport.h"
18 #include "platform/heap/Handle.h" 18 #include "platform/heap/Handle.h"
19 #include "wtf/Forward.h" 19 #include "wtf/Forward.h"
20 #include <utility> 20 #include <utility>
21 #include <v8.h> 21 #include <v8.h>
22 22
23 namespace blink { 23 namespace blink {
24 24
25 class DOMWindow; 25 class DOMWindow;
26 class Dictionary; 26 class Dictionary;
27 class EventTarget; 27 class EventTarget;
28 class WorkerOrWorkletGlobalScope; 28 class WorkerOrWorkletGlobalScope;
29 29
30 // ScriptWrappable 30 // ScriptWrappable
31 31
32 inline v8::Local<v8::Value> toV8(ScriptWrappable* impl, 32 inline v8::Local<v8::Value> ToV8(ScriptWrappable* impl,
33 v8::Local<v8::Object> creationContext, 33 v8::Local<v8::Object> creationContext,
34 v8::Isolate* isolate) { 34 v8::Isolate* isolate) {
35 if (UNLIKELY(!impl)) 35 if (UNLIKELY(!impl))
36 return v8::Null(isolate); 36 return v8::Null(isolate);
37 v8::Local<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate); 37 v8::Local<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate);
38 if (!wrapper.IsEmpty()) 38 if (!wrapper.IsEmpty())
39 return wrapper; 39 return wrapper;
40 40
41 wrapper = impl->wrap(isolate, creationContext); 41 wrapper = impl->wrap(isolate, creationContext);
42 DCHECK(!wrapper.IsEmpty()); 42 DCHECK(!wrapper.IsEmpty());
43 return wrapper; 43 return wrapper;
44 } 44 }
45 45
46 inline v8::Local<v8::Value> toV8(Node* impl, 46 inline v8::Local<v8::Value> ToV8(Node* impl,
47 v8::Local<v8::Object> creationContext, 47 v8::Local<v8::Object> creationContext,
48 v8::Isolate* isolate) { 48 v8::Isolate* isolate) {
49 if (UNLIKELY(!impl)) 49 if (UNLIKELY(!impl))
50 return v8::Null(isolate); 50 return v8::Null(isolate);
51 v8::Local<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate); 51 v8::Local<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate);
52 if (!wrapper.IsEmpty()) 52 if (!wrapper.IsEmpty())
53 return wrapper; 53 return wrapper;
54 54
55 wrapper = ScriptWrappable::fromNode(impl)->wrap(isolate, creationContext); 55 wrapper = ScriptWrappable::fromNode(impl)->wrap(isolate, creationContext);
56 DCHECK(!wrapper.IsEmpty()); 56 DCHECK(!wrapper.IsEmpty());
57 return wrapper; 57 return wrapper;
58 } 58 }
59 59
60 // Special versions for DOMWindow, WorkerOrWorkletGlobalScope and EventTarget 60 // Special versions for DOMWindow, WorkerOrWorkletGlobalScope and EventTarget
61 61
62 CORE_EXPORT v8::Local<v8::Value> toV8(DOMWindow*, 62 CORE_EXPORT v8::Local<v8::Value> ToV8(DOMWindow*,
63 v8::Local<v8::Object> creationContext, 63 v8::Local<v8::Object> creationContext,
64 v8::Isolate*); 64 v8::Isolate*);
65 CORE_EXPORT v8::Local<v8::Value> toV8(EventTarget*, 65 CORE_EXPORT v8::Local<v8::Value> ToV8(EventTarget*,
66 v8::Local<v8::Object> creationContext, 66 v8::Local<v8::Object> creationContext,
67 v8::Isolate*); 67 v8::Isolate*);
68 v8::Local<v8::Value> toV8(WorkerOrWorkletGlobalScope*, 68 v8::Local<v8::Value> ToV8(WorkerOrWorkletGlobalScope*,
69 v8::Local<v8::Object> creationContext, 69 v8::Local<v8::Object> creationContext,
70 v8::Isolate*); 70 v8::Isolate*);
71 71
72 // Primitives 72 // Primitives
73 73
74 inline v8::Local<v8::Value> toV8(const String& value, 74 inline v8::Local<v8::Value> ToV8(const String& value,
75 v8::Local<v8::Object> creationContext, 75 v8::Local<v8::Object> creationContext,
76 v8::Isolate* isolate) { 76 v8::Isolate* isolate) {
77 return v8String(isolate, value); 77 return v8String(isolate, value);
78 } 78 }
79 79
80 inline v8::Local<v8::Value> toV8(const char* value, 80 inline v8::Local<v8::Value> ToV8(const char* value,
81 v8::Local<v8::Object> creationContext, 81 v8::Local<v8::Object> creationContext,
82 v8::Isolate* isolate) { 82 v8::Isolate* isolate) {
83 return v8String(isolate, value); 83 return v8String(isolate, value);
84 } 84 }
85 85
86 template <size_t sizeOfValue> 86 template <size_t sizeOfValue>
87 inline v8::Local<v8::Value> toV8SignedIntegerInternal(int64_t value, 87 inline v8::Local<v8::Value> toV8SignedIntegerInternal(int64_t value,
88 v8::Isolate*); 88 v8::Isolate*);
89 89
90 template <> 90 template <>
(...skipping 27 matching lines...) Expand all
118 inline v8::Local<v8::Value> toV8UnsignedIntegerInternal<8>( 118 inline v8::Local<v8::Value> toV8UnsignedIntegerInternal<8>(
119 uint64_t value, 119 uint64_t value,
120 v8::Isolate* isolate) { 120 v8::Isolate* isolate) {
121 uint32_t valueIn32Bit = static_cast<uint32_t>(value); 121 uint32_t valueIn32Bit = static_cast<uint32_t>(value);
122 if (valueIn32Bit == value) 122 if (valueIn32Bit == value)
123 return v8::Integer::NewFromUnsigned(isolate, value); 123 return v8::Integer::NewFromUnsigned(isolate, value);
124 // V8 doesn't have a 64-bit integer implementation. 124 // V8 doesn't have a 64-bit integer implementation.
125 return v8::Number::New(isolate, value); 125 return v8::Number::New(isolate, value);
126 } 126 }
127 127
128 inline v8::Local<v8::Value> toV8(int value, 128 inline v8::Local<v8::Value> ToV8(int value,
129 v8::Local<v8::Object> creationContext, 129 v8::Local<v8::Object> creationContext,
130 v8::Isolate* isolate) { 130 v8::Isolate* isolate) {
131 return toV8SignedIntegerInternal<sizeof value>(value, isolate); 131 return toV8SignedIntegerInternal<sizeof value>(value, isolate);
132 } 132 }
133 133
134 inline v8::Local<v8::Value> toV8(long value, 134 inline v8::Local<v8::Value> ToV8(long value,
135 v8::Local<v8::Object> creationContext, 135 v8::Local<v8::Object> creationContext,
136 v8::Isolate* isolate) { 136 v8::Isolate* isolate) {
137 return toV8SignedIntegerInternal<sizeof value>(value, isolate); 137 return toV8SignedIntegerInternal<sizeof value>(value, isolate);
138 } 138 }
139 139
140 inline v8::Local<v8::Value> toV8(long long value, 140 inline v8::Local<v8::Value> ToV8(long long value,
141 v8::Local<v8::Object> creationContext, 141 v8::Local<v8::Object> creationContext,
142 v8::Isolate* isolate) { 142 v8::Isolate* isolate) {
143 return toV8SignedIntegerInternal<sizeof value>(value, isolate); 143 return toV8SignedIntegerInternal<sizeof value>(value, isolate);
144 } 144 }
145 145
146 inline v8::Local<v8::Value> toV8(unsigned value, 146 inline v8::Local<v8::Value> ToV8(unsigned value,
147 v8::Local<v8::Object> creationContext, 147 v8::Local<v8::Object> creationContext,
148 v8::Isolate* isolate) { 148 v8::Isolate* isolate) {
149 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate); 149 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate);
150 } 150 }
151 151
152 inline v8::Local<v8::Value> toV8(unsigned long value, 152 inline v8::Local<v8::Value> ToV8(unsigned long value,
153 v8::Local<v8::Object> creationContext, 153 v8::Local<v8::Object> creationContext,
154 v8::Isolate* isolate) { 154 v8::Isolate* isolate) {
155 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate); 155 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate);
156 } 156 }
157 157
158 inline v8::Local<v8::Value> toV8(unsigned long long value, 158 inline v8::Local<v8::Value> ToV8(unsigned long long value,
159 v8::Local<v8::Object> creationContext, 159 v8::Local<v8::Object> creationContext,
160 v8::Isolate* isolate) { 160 v8::Isolate* isolate) {
161 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate); 161 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate);
162 } 162 }
163 163
164 inline v8::Local<v8::Value> toV8(double value, 164 inline v8::Local<v8::Value> ToV8(double value,
165 v8::Local<v8::Object> creationContext, 165 v8::Local<v8::Object> creationContext,
166 v8::Isolate* isolate) { 166 v8::Isolate* isolate) {
167 return v8::Number::New(isolate, value); 167 return v8::Number::New(isolate, value);
168 } 168 }
169 169
170 inline v8::Local<v8::Value> toV8(bool value, 170 inline v8::Local<v8::Value> ToV8(bool value,
171 v8::Local<v8::Object> creationContext, 171 v8::Local<v8::Object> creationContext,
172 v8::Isolate* isolate) { 172 v8::Isolate* isolate) {
173 return v8::Boolean::New(isolate, value); 173 return v8::Boolean::New(isolate, value);
174 } 174 }
175 175
176 // Identity operator 176 // Identity operator
177 177
178 inline v8::Local<v8::Value> toV8(v8::Local<v8::Value> value, 178 inline v8::Local<v8::Value> ToV8(v8::Local<v8::Value> value,
179 v8::Local<v8::Object> creationContext, 179 v8::Local<v8::Object> creationContext,
180 v8::Isolate*) { 180 v8::Isolate*) {
181 return value; 181 return value;
182 } 182 }
183 183
184 // Undefined 184 // Undefined
185 185
186 struct ToV8UndefinedGenerator { 186 struct ToV8UndefinedGenerator {
187 DISALLOW_NEW(); 187 DISALLOW_NEW();
188 }; // Used only for having toV8 return v8::Undefined. 188 }; // Used only for having toV8 return v8::Undefined.
189 189
190 inline v8::Local<v8::Value> toV8(const ToV8UndefinedGenerator& value, 190 inline v8::Local<v8::Value> ToV8(const ToV8UndefinedGenerator& value,
191 v8::Local<v8::Object> creationContext, 191 v8::Local<v8::Object> creationContext,
192 v8::Isolate* isolate) { 192 v8::Isolate* isolate) {
193 return v8::Undefined(isolate); 193 return v8::Undefined(isolate);
194 } 194 }
195 195
196 // ScriptValue 196 // ScriptValue
197 197
198 inline v8::Local<v8::Value> toV8(const ScriptValue& value, 198 inline v8::Local<v8::Value> ToV8(const ScriptValue& value,
199 v8::Local<v8::Object> creationContext, 199 v8::Local<v8::Object> creationContext,
200 v8::Isolate* isolate) { 200 v8::Isolate* isolate) {
201 if (value.isEmpty()) 201 if (value.isEmpty())
202 return v8::Undefined(isolate); 202 return v8::Undefined(isolate);
203 return value.v8Value(); 203 return value.v8Value();
204 } 204 }
205 205
206 // Dictionary 206 // Dictionary
207 207
208 inline v8::Local<v8::Value> toV8(const Dictionary& value, 208 inline v8::Local<v8::Value> ToV8(const Dictionary& value,
209 v8::Local<v8::Object> creationContext, 209 v8::Local<v8::Object> creationContext,
210 v8::Isolate* isolate) { 210 v8::Isolate* isolate) {
211 NOTREACHED(); 211 NOTREACHED();
212 return v8::Undefined(isolate); 212 return v8::Undefined(isolate);
213 } 213 }
214 214
215 inline v8::Local<v8::Value> toV8(const IDLDictionaryBase& value, 215 inline v8::Local<v8::Value> ToV8(const IDLDictionaryBase& value,
216 v8::Local<v8::Object> creationContext, 216 v8::Local<v8::Object> creationContext,
217 v8::Isolate* isolate) { 217 v8::Isolate* isolate) {
218 return value.toV8Impl(creationContext, isolate); 218 return value.toV8Impl(creationContext, isolate);
219 } 219 }
220 220
221 // Array 221 // Array
222 222
223 template <typename Sequence> 223 template <typename Sequence>
224 inline v8::Local<v8::Value> toV8SequenceInternal( 224 inline v8::Local<v8::Value> toV8SequenceInternal(
225 const Sequence& sequence, 225 const Sequence& sequence,
226 v8::Local<v8::Object> creationContext, 226 v8::Local<v8::Object> creationContext,
227 v8::Isolate* isolate) { 227 v8::Isolate* isolate) {
228 v8::Local<v8::Array> array; 228 v8::Local<v8::Array> array;
229 { 229 {
230 v8::Context::Scope contextScope(creationContext->CreationContext()); 230 v8::Context::Scope contextScope(creationContext->CreationContext());
231 array = v8::Array::New(isolate, sequence.size()); 231 array = v8::Array::New(isolate, sequence.size());
232 } 232 }
233 uint32_t index = 0; 233 uint32_t index = 0;
234 typename Sequence::const_iterator end = sequence.end(); 234 typename Sequence::const_iterator end = sequence.end();
235 for (typename Sequence::const_iterator iter = sequence.begin(); iter != end; 235 for (typename Sequence::const_iterator iter = sequence.begin(); iter != end;
236 ++iter) { 236 ++iter) {
237 v8::Local<v8::Value> value = toV8(*iter, array, isolate); 237 v8::Local<v8::Value> value = ToV8(*iter, array, isolate);
238 if (value.IsEmpty()) 238 if (value.IsEmpty())
239 value = v8::Undefined(isolate); 239 value = v8::Undefined(isolate);
240 if (!v8CallBoolean(array->CreateDataProperty(isolate->GetCurrentContext(), 240 if (!v8CallBoolean(array->CreateDataProperty(isolate->GetCurrentContext(),
241 index++, value))) 241 index++, value)))
242 return v8::Local<v8::Value>(); 242 return v8::Local<v8::Value>();
243 } 243 }
244 return array; 244 return array;
245 } 245 }
246 246
247 template <typename T, size_t inlineCapacity> 247 template <typename T, size_t inlineCapacity>
248 inline v8::Local<v8::Value> toV8(const Vector<T, inlineCapacity>& value, 248 inline v8::Local<v8::Value> ToV8(const Vector<T, inlineCapacity>& value,
249 v8::Local<v8::Object> creationContext, 249 v8::Local<v8::Object> creationContext,
250 v8::Isolate* isolate) { 250 v8::Isolate* isolate) {
251 return toV8SequenceInternal(value, creationContext, isolate); 251 return toV8SequenceInternal(value, creationContext, isolate);
252 } 252 }
253 253
254 template <typename T, size_t inlineCapacity> 254 template <typename T, size_t inlineCapacity>
255 inline v8::Local<v8::Value> toV8(const HeapVector<T, inlineCapacity>& value, 255 inline v8::Local<v8::Value> ToV8(const HeapVector<T, inlineCapacity>& value,
256 v8::Local<v8::Object> creationContext, 256 v8::Local<v8::Object> creationContext,
257 v8::Isolate* isolate) { 257 v8::Isolate* isolate) {
258 return toV8SequenceInternal(value, creationContext, isolate); 258 return toV8SequenceInternal(value, creationContext, isolate);
259 } 259 }
260 260
261 template <typename T> 261 template <typename T>
262 inline v8::Local<v8::Value> toV8(const Vector<std::pair<String, T>>& value, 262 inline v8::Local<v8::Value> ToV8(const Vector<std::pair<String, T>>& value,
263 v8::Local<v8::Object> creationContext, 263 v8::Local<v8::Object> creationContext,
264 v8::Isolate* isolate) { 264 v8::Isolate* isolate) {
265 v8::Local<v8::Object> object; 265 v8::Local<v8::Object> object;
266 { 266 {
267 v8::Context::Scope contextScope(creationContext->CreationContext()); 267 v8::Context::Scope contextScope(creationContext->CreationContext());
268 object = v8::Object::New(isolate); 268 object = v8::Object::New(isolate);
269 } 269 }
270 for (unsigned i = 0; i < value.size(); ++i) { 270 for (unsigned i = 0; i < value.size(); ++i) {
271 v8::Local<v8::Value> v8Value = toV8(value[i].second, object, isolate); 271 v8::Local<v8::Value> v8Value = ToV8(value[i].second, object, isolate);
272 if (v8Value.IsEmpty()) 272 if (v8Value.IsEmpty())
273 v8Value = v8::Undefined(isolate); 273 v8Value = v8::Undefined(isolate);
274 if (!v8CallBoolean(object->CreateDataProperty( 274 if (!v8CallBoolean(object->CreateDataProperty(
275 isolate->GetCurrentContext(), v8String(isolate, value[i].first), 275 isolate->GetCurrentContext(), v8String(isolate, value[i].first),
276 v8Value))) 276 v8Value)))
277 return v8::Local<v8::Value>(); 277 return v8::Local<v8::Value>();
278 } 278 }
279 return object; 279 return object;
280 } 280 }
281 281
282 // In all cases allow script state instead of creation context + isolate. 282 // In all cases allow script state instead of creation context + isolate.
283 // Use this function only if the call site does not otherwise need the global, 283 // Use this function only if the call site does not otherwise need the global,
284 // since v8::Context::Global is heavy. 284 // since v8::Context::Global is heavy.
285 template <typename T> 285 template <typename T>
286 inline v8::Local<v8::Value> toV8(T&& value, ScriptState* scriptState) { 286 inline v8::Local<v8::Value> ToV8(T&& value, ScriptState* scriptState) {
287 return toV8(std::forward<T>(value), scriptState->context()->Global(), 287 return ToV8(std::forward<T>(value), scriptState->context()->Global(),
288 scriptState->isolate()); 288 scriptState->isolate());
289 } 289 }
290 290
291 // Only declare toV8(void*,...) for checking function overload mismatch. 291 // Only declare ToV8(void*,...) for checking function overload mismatch.
292 // This toV8(void*,...) should be never used. So we will find mismatch 292 // This ToV8(void*,...) should be never used. So we will find mismatch
293 // because of "unresolved external symbol". 293 // because of "unresolved external symbol".
294 // Without toV8(void*, ...), call to toV8 with T* will match with 294 // Without ToV8(void*, ...), call to toV8 with T* will match with
295 // toV8(bool, ...) if T is not a subclass of ScriptWrappable or if T is 295 // ToV8(bool, ...) if T is not a subclass of ScriptWrappable or if T is
296 // declared but not defined (so it's not clear that T is a subclass of 296 // declared but not defined (so it's not clear that T is a subclass of
297 // ScriptWrappable). 297 // ScriptWrappable).
298 // This hack helps detect such unwanted implicit conversions from T* to bool. 298 // This hack helps detect such unwanted implicit conversions from T* to bool.
299 v8::Local<v8::Value> toV8(void* value, 299 v8::Local<v8::Value> ToV8(void* value,
300 v8::Local<v8::Object> creationContext, 300 v8::Local<v8::Object> creationContext,
301 v8::Isolate*) = delete; 301 v8::Isolate*) = delete;
302 302
303 // Cannot define in ScriptValue because of the circular dependency between toV8 303 // Cannot define in ScriptValue because of the circular dependency between toV8
304 // and ScriptValue 304 // and ScriptValue
305 template <typename T> 305 template <typename T>
306 inline ScriptValue ScriptValue::from(ScriptState* scriptState, T&& value) { 306 inline ScriptValue ScriptValue::from(ScriptState* scriptState, T&& value) {
307 return ScriptValue(scriptState, toV8(std::forward<T>(value), scriptState)); 307 return ScriptValue(scriptState, ToV8(std::forward<T>(value), scriptState));
308 } 308 }
309 309
310 } // namespace blink 310 } // namespace blink
311 311
312 #endif // ToV8_h 312 #endif // ToV8_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698