| 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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 { | 210 { |
| 211 v8::Local<v8::Array> array; | 211 v8::Local<v8::Array> array; |
| 212 { | 212 { |
| 213 v8::Context::Scope contextScope(creationContext->CreationContext()); | 213 v8::Context::Scope contextScope(creationContext->CreationContext()); |
| 214 array = v8::Array::New(isolate, sequence.size()); | 214 array = v8::Array::New(isolate, sequence.size()); |
| 215 } | 215 } |
| 216 uint32_t index = 0; | 216 uint32_t index = 0; |
| 217 typename Sequence::const_iterator end = sequence.end(); | 217 typename Sequence::const_iterator end = sequence.end(); |
| 218 for (typename Sequence::const_iterator iter = sequence.begin(); iter != end;
++iter) { | 218 for (typename Sequence::const_iterator iter = sequence.begin(); iter != end;
++iter) { |
| 219 v8::Local<v8::Value> value = toV8(*iter, array, isolate); | 219 v8::Local<v8::Value> value = toV8(*iter, array, isolate); |
| 220 if (value.IsEmpty()) | |
| 221 value = v8::Undefined(isolate); | |
| 222 if (!v8CallBoolean(array->CreateDataProperty(isolate->GetCurrentContext(
), index++, value))) | 220 if (!v8CallBoolean(array->CreateDataProperty(isolate->GetCurrentContext(
), index++, value))) |
| 223 return v8::Local<v8::Value>(); | 221 return v8::Local<v8::Value>(); |
| 224 } | 222 } |
| 225 return array; | 223 return array; |
| 226 } | 224 } |
| 227 | 225 |
| 228 template<typename T, size_t inlineCapacity> | 226 template<typename T, size_t inlineCapacity> |
| 229 inline v8::Local<v8::Value> toV8(const Vector<T, inlineCapacity>& value, v8::Loc
al<v8::Object> creationContext, v8::Isolate* isolate) | 227 inline v8::Local<v8::Value> toV8(const Vector<T, inlineCapacity>& value, v8::Loc
al<v8::Object> creationContext, v8::Isolate* isolate) |
| 230 { | 228 { |
| 231 return toV8SequenceInternal(value, creationContext, isolate); | 229 return toV8SequenceInternal(value, creationContext, isolate); |
| 232 } | 230 } |
| 233 | 231 |
| 234 template<typename T, size_t inlineCapacity> | 232 template<typename T, size_t inlineCapacity> |
| 235 inline v8::Local<v8::Value> toV8(const HeapVector<T, inlineCapacity>& value, v8:
:Local<v8::Object> creationContext, v8::Isolate* isolate) | 233 inline v8::Local<v8::Value> toV8(const HeapVector<T, inlineCapacity>& value, v8:
:Local<v8::Object> creationContext, v8::Isolate* isolate) |
| 236 { | 234 { |
| 237 return toV8SequenceInternal(value, creationContext, isolate); | 235 return toV8SequenceInternal(value, creationContext, isolate); |
| 238 } | 236 } |
| 239 | 237 |
| 240 template<typename T> | 238 template<typename T> |
| 241 inline v8::Local<v8::Value> toV8(const Vector<std::pair<String, T>>& value, v8::
Local<v8::Object> creationContext, v8::Isolate* isolate) | 239 inline v8::Local<v8::Value> toV8(const Vector<std::pair<String, T>>& value, v8::
Local<v8::Object> creationContext, v8::Isolate* isolate) |
| 242 { | 240 { |
| 243 v8::Local<v8::Object> object; | 241 v8::Local<v8::Object> object; |
| 244 { | 242 { |
| 245 v8::Context::Scope contextScope(creationContext->CreationContext()); | 243 v8::Context::Scope contextScope(creationContext->CreationContext()); |
| 246 object = v8::Object::New(isolate); | 244 object = v8::Object::New(isolate); |
| 247 } | 245 } |
| 248 for (unsigned i = 0; i < value.size(); ++i) { | 246 for (unsigned i = 0; i < value.size(); ++i) { |
| 249 v8::Local<v8::Value> v8Value = toV8(value[i].second, object, isolate); | 247 v8::Local<v8::Value> v8Value = toV8(value[i].second, object, isolate); |
| 250 if (v8Value.IsEmpty()) | |
| 251 v8Value = v8::Undefined(isolate); | |
| 252 if (!v8CallBoolean(object->CreateDataProperty(isolate->GetCurrentContext
(), v8String(isolate, value[i].first), v8Value))) | 248 if (!v8CallBoolean(object->CreateDataProperty(isolate->GetCurrentContext
(), v8String(isolate, value[i].first), v8Value))) |
| 253 return v8::Local<v8::Value>(); | 249 return v8::Local<v8::Value>(); |
| 254 } | 250 } |
| 255 return object; | 251 return object; |
| 256 } | 252 } |
| 257 | 253 |
| 258 // In all cases allow script state instead of creation context + isolate. | 254 // In all cases allow script state instead of creation context + isolate. |
| 259 // Use this function only if the call site does not otherwise need the global, | 255 // Use this function only if the call site does not otherwise need the global, |
| 260 // since v8::Context::Global is heavy. | 256 // since v8::Context::Global is heavy. |
| 261 template<typename T> | 257 template<typename T> |
| (...skipping 15 matching lines...) Expand all Loading... |
| 277 // Cannot define in ScriptValue because of the circular dependency between toV8
and ScriptValue | 273 // Cannot define in ScriptValue because of the circular dependency between toV8
and ScriptValue |
| 278 template<typename T> | 274 template<typename T> |
| 279 inline ScriptValue ScriptValue::from(ScriptState* scriptState, T&& value) | 275 inline ScriptValue ScriptValue::from(ScriptState* scriptState, T&& value) |
| 280 { | 276 { |
| 281 return ScriptValue(scriptState, toV8(std::forward<T>(value), scriptState)); | 277 return ScriptValue(scriptState, toV8(std::forward<T>(value), scriptState)); |
| 282 } | 278 } |
| 283 | 279 |
| 284 } // namespace blink | 280 } // namespace blink |
| 285 | 281 |
| 286 #endif // ToV8_h | 282 #endif // ToV8_h |
| OLD | NEW |