| 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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 { | 197 { |
| 198 RELEASE_NOTREACHED(); | 198 RELEASE_NOTREACHED(); |
| 199 return v8::Local<v8::Value>(); | 199 return v8::Local<v8::Value>(); |
| 200 } | 200 } |
| 201 | 201 |
| 202 // Array | 202 // Array |
| 203 | 203 |
| 204 template<typename Sequence> | 204 template<typename Sequence> |
| 205 inline v8::Local<v8::Value> toV8SequenceInternal(const Sequence& sequence, v8::L
ocal<v8::Object> creationContext, v8::Isolate* isolate) | 205 inline v8::Local<v8::Value> toV8SequenceInternal(const Sequence& sequence, v8::L
ocal<v8::Object> creationContext, v8::Isolate* isolate) |
| 206 { | 206 { |
| 207 v8::Local<v8::Array> array = v8::Array::New(isolate, sequence.size()); | 207 v8::Local<v8::Array> array; |
| 208 { |
| 209 v8::Context::Scope contextScope(creationContext->CreationContext()); |
| 210 array = v8::Array::New(isolate, sequence.size()); |
| 211 } |
| 208 uint32_t index = 0; | 212 uint32_t index = 0; |
| 209 typename Sequence::const_iterator end = sequence.end(); | 213 typename Sequence::const_iterator end = sequence.end(); |
| 210 for (typename Sequence::const_iterator iter = sequence.begin(); iter != end;
++iter) { | 214 for (typename Sequence::const_iterator iter = sequence.begin(); iter != end;
++iter) { |
| 211 v8::Local<v8::Value> value = toV8(*iter, creationContext, isolate); | 215 v8::Local<v8::Value> value = toV8(*iter, array, isolate); |
| 212 if (value.IsEmpty()) | 216 if (value.IsEmpty()) |
| 213 value = v8::Undefined(isolate); | 217 value = v8::Undefined(isolate); |
| 214 if (!v8CallBoolean(array->Set(isolate->GetCurrentContext(), v8::Integer:
:New(isolate, index++), value))) | 218 if (!v8CallBoolean(array->Set(isolate->GetCurrentContext(), v8::Integer:
:New(isolate, index++), value))) |
| 215 return v8::Local<v8::Value>(); | 219 return v8::Local<v8::Value>(); |
| 216 } | 220 } |
| 217 return array; | 221 return array; |
| 218 } | 222 } |
| 219 | 223 |
| 220 template<typename T, size_t inlineCapacity> | 224 template<typename T, size_t inlineCapacity> |
| 221 inline v8::Local<v8::Value> toV8(const Vector<T, inlineCapacity>& value, v8::Loc
al<v8::Object> creationContext, v8::Isolate* isolate) | 225 inline v8::Local<v8::Value> toV8(const Vector<T, inlineCapacity>& value, v8::Loc
al<v8::Object> creationContext, v8::Isolate* isolate) |
| 222 { | 226 { |
| 223 return toV8SequenceInternal(value, creationContext, isolate); | 227 return toV8SequenceInternal(value, creationContext, isolate); |
| 224 } | 228 } |
| 225 | 229 |
| 226 template<typename T, size_t inlineCapacity> | 230 template<typename T, size_t inlineCapacity> |
| 227 inline v8::Local<v8::Value> toV8(const HeapVector<T, inlineCapacity>& value, v8:
:Local<v8::Object> creationContext, v8::Isolate* isolate) | 231 inline v8::Local<v8::Value> toV8(const HeapVector<T, inlineCapacity>& value, v8:
:Local<v8::Object> creationContext, v8::Isolate* isolate) |
| 228 { | 232 { |
| 229 return toV8SequenceInternal(value, creationContext, isolate); | 233 return toV8SequenceInternal(value, creationContext, isolate); |
| 230 } | 234 } |
| 231 | 235 |
| 232 template<typename T> | 236 template<typename T> |
| 233 inline v8::Local<v8::Value> toV8(const Vector<std::pair<String, T>>& value, v8::
Local<v8::Object> creationContext, v8::Isolate* isolate) | 237 inline v8::Local<v8::Value> toV8(const Vector<std::pair<String, T>>& value, v8::
Local<v8::Object> creationContext, v8::Isolate* isolate) |
| 234 { | 238 { |
| 235 v8::Local<v8::Object> object = v8::Object::New(isolate); | 239 v8::Local<v8::Object> object; |
| 240 { |
| 241 v8::Context::Scope contextScope(creationContext->CreationContext()); |
| 242 object = v8::Object::New(isolate); |
| 243 } |
| 236 for (unsigned i = 0; i < value.size(); ++i) { | 244 for (unsigned i = 0; i < value.size(); ++i) { |
| 237 v8::Local<v8::Value> v8Value = toV8(value[i].second, creationContext, is
olate); | 245 v8::Local<v8::Value> v8Value = toV8(value[i].second, object, isolate); |
| 238 if (v8Value.IsEmpty()) | 246 if (v8Value.IsEmpty()) |
| 239 v8Value = v8::Undefined(isolate); | 247 v8Value = v8::Undefined(isolate); |
| 240 if (!v8CallBoolean(object->Set(isolate->GetCurrentContext(), v8String(is
olate, value[i].first), v8Value))) | 248 if (!v8CallBoolean(object->Set(isolate->GetCurrentContext(), v8String(is
olate, value[i].first), v8Value))) |
| 241 return v8::Local<v8::Value>(); | 249 return v8::Local<v8::Value>(); |
| 242 } | 250 } |
| 243 return object; | 251 return object; |
| 244 } | 252 } |
| 245 | 253 |
| 246 // In all cases allow script state instead of creation context + isolate. | 254 // 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, | 255 // Use this function only if the call site does not otherwise need the global, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 265 // 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 |
| 266 template<typename T> | 274 template<typename T> |
| 267 inline ScriptValue ScriptValue::from(ScriptState* scriptState, T&& value) | 275 inline ScriptValue ScriptValue::from(ScriptState* scriptState, T&& value) |
| 268 { | 276 { |
| 269 return ScriptValue(scriptState, toV8(std::forward<T>(value), scriptState)); | 277 return ScriptValue(scriptState, toV8(std::forward<T>(value), scriptState)); |
| 270 } | 278 } |
| 271 | 279 |
| 272 } // namespace blink | 280 } // namespace blink |
| 273 | 281 |
| 274 #endif // ToV8_h | 282 #endif // ToV8_h |
| OLD | NEW |