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