| 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 "bindings/core/v8/DOMDataStore.h" | 11 #include "bindings/core/v8/DOMDataStore.h" |
| 12 #include "bindings/core/v8/ScriptValue.h" | 12 #include "bindings/core/v8/ScriptValue.h" |
| 13 #include "bindings/core/v8/ScriptWrappable.h" | 13 #include "bindings/core/v8/ScriptWrappable.h" |
| 14 #include "bindings/core/v8/V8Binding.h" | 14 #include "bindings/core/v8/V8Binding.h" |
| 15 #include "core/CoreExport.h" | 15 #include "core/CoreExport.h" |
| 16 #include "platform/heap/Handle.h" | 16 #include "platform/heap/Handle.h" |
| 17 #include "wtf/Forward.h" | 17 #include "wtf/Forward.h" |
| 18 #include <v8.h> | 18 #include <v8.h> |
| 19 | 19 |
| 20 namespace blink { | 20 namespace blink { |
| 21 | 21 |
| 22 class DOMWindow; | 22 class DOMWindow; |
| 23 class Dictionary; | 23 class Dictionary; |
| 24 class EventTarget; | 24 class EventTarget; |
| 25 class WorkerGlobalScope; | 25 class WorkerGlobalScope; |
| 26 | 26 |
| 27 // ScriptWrappable | 27 // ScriptWrappable |
| 28 | 28 |
| 29 inline v8::Handle<v8::Value> toV8(ScriptWrappable* impl, v8::Handle<v8::Object>
creationContext, v8::Isolate* isolate) | 29 inline v8::Local<v8::Value> toV8(ScriptWrappable* impl, v8::Local<v8::Object> cr
eationContext, v8::Isolate* isolate) |
| 30 { | 30 { |
| 31 if (UNLIKELY(!impl)) | 31 if (UNLIKELY(!impl)) |
| 32 return v8::Null(isolate); | 32 return v8::Null(isolate); |
| 33 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate); | 33 v8::Local<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate); |
| 34 if (!wrapper.IsEmpty()) | 34 if (!wrapper.IsEmpty()) |
| 35 return wrapper; | 35 return wrapper; |
| 36 | 36 |
| 37 return impl->wrap(creationContext, isolate); | 37 return impl->wrap(creationContext, isolate); |
| 38 } | 38 } |
| 39 | 39 |
| 40 inline v8::Handle<v8::Value> toV8(Node* impl, v8::Handle<v8::Object> creationCon
text, v8::Isolate* isolate) | 40 inline v8::Local<v8::Value> toV8(Node* impl, v8::Local<v8::Object> creationConte
xt, v8::Isolate* isolate) |
| 41 { | 41 { |
| 42 if (UNLIKELY(!impl)) | 42 if (UNLIKELY(!impl)) |
| 43 return v8::Null(isolate); | 43 return v8::Null(isolate); |
| 44 v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate); | 44 v8::Local<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate); |
| 45 if (!wrapper.IsEmpty()) | 45 if (!wrapper.IsEmpty()) |
| 46 return wrapper; | 46 return wrapper; |
| 47 | 47 |
| 48 return ScriptWrappable::fromNode(impl)->wrap(creationContext, isolate); | 48 return ScriptWrappable::fromNode(impl)->wrap(creationContext, isolate); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Special versions for DOMWindow, WorkerGlobalScope and EventTarget | 51 // Special versions for DOMWindow, WorkerGlobalScope and EventTarget |
| 52 | 52 |
| 53 CORE_EXPORT v8::Handle<v8::Value> toV8(DOMWindow*, v8::Handle<v8::Object> creati
onContext, v8::Isolate*); | 53 CORE_EXPORT v8::Local<v8::Value> toV8(DOMWindow*, v8::Local<v8::Object> creation
Context, v8::Isolate*); |
| 54 CORE_EXPORT v8::Handle<v8::Value> toV8(EventTarget*, v8::Handle<v8::Object> crea
tionContext, v8::Isolate*); | 54 CORE_EXPORT v8::Local<v8::Value> toV8(EventTarget*, v8::Local<v8::Object> creati
onContext, v8::Isolate*); |
| 55 v8::Handle<v8::Value> toV8(WorkerGlobalScope*, v8::Handle<v8::Object> creationCo
ntext, v8::Isolate*); | 55 v8::Local<v8::Value> toV8(WorkerGlobalScope*, v8::Local<v8::Object> creationCont
ext, v8::Isolate*); |
| 56 | 56 |
| 57 // PassRefPtr, RawPtr and RefPtr | 57 // PassRefPtr, RawPtr and RefPtr |
| 58 | 58 |
| 59 template<typename T> | 59 template<typename T> |
| 60 inline v8::Handle<v8::Value> toV8(PassRefPtr<T> impl, v8::Handle<v8::Object> cre
ationContext, v8::Isolate* isolate) | 60 inline v8::Local<v8::Value> toV8(PassRefPtr<T> impl, v8::Local<v8::Object> creat
ionContext, v8::Isolate* isolate) |
| 61 { | 61 { |
| 62 return toV8(impl.get(), creationContext, isolate); | 62 return toV8(impl.get(), creationContext, isolate); |
| 63 } | 63 } |
| 64 | 64 |
| 65 template<typename T> | 65 template<typename T> |
| 66 inline v8::Handle<v8::Value> toV8(RawPtr<T> impl, v8::Handle<v8::Object> creatio
nContext, v8::Isolate* isolate) | 66 inline v8::Local<v8::Value> toV8(RawPtr<T> impl, v8::Local<v8::Object> creationC
ontext, v8::Isolate* isolate) |
| 67 { | 67 { |
| 68 return toV8(impl.get(), creationContext, isolate); | 68 return toV8(impl.get(), creationContext, isolate); |
| 69 } | 69 } |
| 70 | 70 |
| 71 template<typename T> | 71 template<typename T> |
| 72 inline v8::Handle<v8::Value> toV8(const RefPtr<T>& impl, v8::Handle<v8::Object>
creationContext, v8::Isolate* isolate) | 72 inline v8::Local<v8::Value> toV8(const RefPtr<T>& impl, v8::Local<v8::Object> cr
eationContext, v8::Isolate* isolate) |
| 73 { | 73 { |
| 74 return toV8(impl.get(), creationContext, isolate); | 74 return toV8(impl.get(), creationContext, isolate); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Primitives | 77 // Primitives |
| 78 | 78 |
| 79 inline v8::Handle<v8::Value> toV8(const String& value, v8::Handle<v8::Object> cr
eationContext, v8::Isolate* isolate) | 79 inline v8::Local<v8::Value> toV8(const String& value, v8::Local<v8::Object> crea
tionContext, v8::Isolate* isolate) |
| 80 { | 80 { |
| 81 return v8String(isolate, value); | 81 return v8String(isolate, value); |
| 82 } | 82 } |
| 83 | 83 |
| 84 inline v8::Handle<v8::Value> toV8(const char* value, v8::Handle<v8::Object> crea
tionContext, v8::Isolate* isolate) | 84 inline v8::Local<v8::Value> toV8(const char* value, v8::Local<v8::Object> creati
onContext, v8::Isolate* isolate) |
| 85 { | 85 { |
| 86 return v8String(isolate, value); | 86 return v8String(isolate, value); |
| 87 } | 87 } |
| 88 | 88 |
| 89 template<size_t sizeOfValue> | 89 template<size_t sizeOfValue> |
| 90 inline v8::Handle<v8::Value> toV8SignedIntegerInternal(int64_t value, v8::Isolat
e*); | 90 inline v8::Local<v8::Value> toV8SignedIntegerInternal(int64_t value, v8::Isolate
*); |
| 91 | 91 |
| 92 template<> | 92 template<> |
| 93 inline v8::Handle<v8::Value> toV8SignedIntegerInternal<4>(int64_t value, v8::Iso
late* isolate) | 93 inline v8::Local<v8::Value> toV8SignedIntegerInternal<4>(int64_t value, v8::Isol
ate* isolate) |
| 94 { | 94 { |
| 95 return v8::Integer::New(isolate, static_cast<int32_t>(value)); | 95 return v8::Integer::New(isolate, static_cast<int32_t>(value)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 template<> | 98 template<> |
| 99 inline v8::Handle<v8::Value> toV8SignedIntegerInternal<8>(int64_t value, v8::Iso
late* isolate) | 99 inline v8::Local<v8::Value> toV8SignedIntegerInternal<8>(int64_t value, v8::Isol
ate* isolate) |
| 100 { | 100 { |
| 101 int32_t valueIn32Bit = static_cast<int32_t>(value); | 101 int32_t valueIn32Bit = static_cast<int32_t>(value); |
| 102 if (valueIn32Bit == value) | 102 if (valueIn32Bit == value) |
| 103 return v8::Integer::New(isolate, value); | 103 return v8::Integer::New(isolate, value); |
| 104 // V8 doesn't have a 64-bit integer implementation. | 104 // V8 doesn't have a 64-bit integer implementation. |
| 105 return v8::Number::New(isolate, value); | 105 return v8::Number::New(isolate, value); |
| 106 } | 106 } |
| 107 | 107 |
| 108 template<size_t sizeOfValue> | 108 template<size_t sizeOfValue> |
| 109 inline v8::Handle<v8::Value> toV8UnsignedIntegerInternal(uint64_t value, v8::Iso
late*); | 109 inline v8::Local<v8::Value> toV8UnsignedIntegerInternal(uint64_t value, v8::Isol
ate*); |
| 110 | 110 |
| 111 template<> | 111 template<> |
| 112 inline v8::Handle<v8::Value> toV8UnsignedIntegerInternal<4>(uint64_t value, v8::
Isolate* isolate) | 112 inline v8::Local<v8::Value> toV8UnsignedIntegerInternal<4>(uint64_t value, v8::I
solate* isolate) |
| 113 { | 113 { |
| 114 return v8::Integer::NewFromUnsigned(isolate, static_cast<uint32_t>(value)); | 114 return v8::Integer::NewFromUnsigned(isolate, static_cast<uint32_t>(value)); |
| 115 } | 115 } |
| 116 | 116 |
| 117 template<> | 117 template<> |
| 118 inline v8::Handle<v8::Value> toV8UnsignedIntegerInternal<8>(uint64_t value, v8::
Isolate* isolate) | 118 inline v8::Local<v8::Value> toV8UnsignedIntegerInternal<8>(uint64_t value, v8::I
solate* isolate) |
| 119 { | 119 { |
| 120 uint32_t valueIn32Bit = static_cast<uint32_t>(value); | 120 uint32_t valueIn32Bit = static_cast<uint32_t>(value); |
| 121 if (valueIn32Bit == value) | 121 if (valueIn32Bit == value) |
| 122 return v8::Integer::NewFromUnsigned(isolate, value); | 122 return v8::Integer::NewFromUnsigned(isolate, value); |
| 123 // V8 doesn't have a 64-bit integer implementation. | 123 // V8 doesn't have a 64-bit integer implementation. |
| 124 return v8::Number::New(isolate, value); | 124 return v8::Number::New(isolate, value); |
| 125 } | 125 } |
| 126 | 126 |
| 127 inline v8::Handle<v8::Value> toV8(int value, v8::Handle<v8::Object> creationCont
ext, v8::Isolate* isolate) | 127 inline v8::Local<v8::Value> toV8(int value, v8::Local<v8::Object> creationContex
t, v8::Isolate* isolate) |
| 128 { | 128 { |
| 129 return toV8SignedIntegerInternal<sizeof value>(value, isolate); | 129 return toV8SignedIntegerInternal<sizeof value>(value, isolate); |
| 130 } | 130 } |
| 131 | 131 |
| 132 inline v8::Handle<v8::Value> toV8(long value, v8::Handle<v8::Object> creationCon
text, v8::Isolate* isolate) | 132 inline v8::Local<v8::Value> toV8(long value, v8::Local<v8::Object> creationConte
xt, v8::Isolate* isolate) |
| 133 { | 133 { |
| 134 return toV8SignedIntegerInternal<sizeof value>(value, isolate); | 134 return toV8SignedIntegerInternal<sizeof value>(value, isolate); |
| 135 } | 135 } |
| 136 | 136 |
| 137 inline v8::Handle<v8::Value> toV8(long long value, v8::Handle<v8::Object> creati
onContext, v8::Isolate* isolate) | 137 inline v8::Local<v8::Value> toV8(long long value, v8::Local<v8::Object> creation
Context, v8::Isolate* isolate) |
| 138 { | 138 { |
| 139 return toV8SignedIntegerInternal<sizeof value>(value, isolate); | 139 return toV8SignedIntegerInternal<sizeof value>(value, isolate); |
| 140 } | 140 } |
| 141 | 141 |
| 142 inline v8::Handle<v8::Value> toV8(unsigned value, v8::Handle<v8::Object> creatio
nContext, v8::Isolate* isolate) | 142 inline v8::Local<v8::Value> toV8(unsigned value, v8::Local<v8::Object> creationC
ontext, v8::Isolate* isolate) |
| 143 { | 143 { |
| 144 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate); | 144 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate); |
| 145 } | 145 } |
| 146 | 146 |
| 147 inline v8::Handle<v8::Value> toV8(unsigned long value, v8::Handle<v8::Object> cr
eationContext, v8::Isolate* isolate) | 147 inline v8::Local<v8::Value> toV8(unsigned long value, v8::Local<v8::Object> crea
tionContext, v8::Isolate* isolate) |
| 148 { | 148 { |
| 149 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate); | 149 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate); |
| 150 } | 150 } |
| 151 | 151 |
| 152 inline v8::Handle<v8::Value> toV8(unsigned long long value, v8::Handle<v8::Objec
t> creationContext, v8::Isolate* isolate) | 152 inline v8::Local<v8::Value> toV8(unsigned long long value, v8::Local<v8::Object>
creationContext, v8::Isolate* isolate) |
| 153 { | 153 { |
| 154 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate); | 154 return toV8UnsignedIntegerInternal<sizeof value>(value, isolate); |
| 155 } | 155 } |
| 156 | 156 |
| 157 inline v8::Handle<v8::Value> toV8(double value, v8::Handle<v8::Object> creationC
ontext, v8::Isolate* isolate) | 157 inline v8::Local<v8::Value> toV8(double value, v8::Local<v8::Object> creationCon
text, v8::Isolate* isolate) |
| 158 { | 158 { |
| 159 return v8::Number::New(isolate, value); | 159 return v8::Number::New(isolate, value); |
| 160 } | 160 } |
| 161 | 161 |
| 162 inline v8::Handle<v8::Value> toV8(bool value, v8::Handle<v8::Object> creationCon
text, v8::Isolate* isolate) | 162 inline v8::Local<v8::Value> toV8(bool value, v8::Local<v8::Object> creationConte
xt, v8::Isolate* isolate) |
| 163 { | 163 { |
| 164 return v8::Boolean::New(isolate, value); | 164 return v8::Boolean::New(isolate, value); |
| 165 } | 165 } |
| 166 | 166 |
| 167 // Identity operator | 167 // Identity operator |
| 168 | 168 |
| 169 inline v8::Handle<v8::Value> toV8(v8::Handle<v8::Value> value, v8::Handle<v8::Ob
ject> creationContext, v8::Isolate*) | 169 inline v8::Local<v8::Value> toV8(v8::Local<v8::Value> value, v8::Local<v8::Objec
t> creationContext, v8::Isolate*) |
| 170 { | 170 { |
| 171 return value; | 171 return value; |
| 172 } | 172 } |
| 173 | 173 |
| 174 // Undefined | 174 // Undefined |
| 175 | 175 |
| 176 struct ToV8UndefinedGenerator { }; // Used only for having toV8 return v8::Undef
ined. | 176 struct ToV8UndefinedGenerator { }; // Used only for having toV8 return v8::Undef
ined. |
| 177 | 177 |
| 178 inline v8::Handle<v8::Value> toV8(const ToV8UndefinedGenerator& value, v8::Hand
le<v8::Object> creationContext, v8::Isolate* isolate) | 178 inline v8::Local<v8::Value> toV8(const ToV8UndefinedGenerator& value, v8::Local
<v8::Object> creationContext, v8::Isolate* isolate) |
| 179 { | 179 { |
| 180 return v8::Undefined(isolate); | 180 return v8::Undefined(isolate); |
| 181 } | 181 } |
| 182 | 182 |
| 183 // ScriptValue | 183 // ScriptValue |
| 184 | 184 |
| 185 inline v8::Handle<v8::Value> toV8(const ScriptValue& value, v8::Handle<v8::Objec
t> creationContext, v8::Isolate*) | 185 inline v8::Local<v8::Value> toV8(const ScriptValue& value, v8::Local<v8::Object>
creationContext, v8::Isolate*) |
| 186 { | 186 { |
| 187 return value.v8Value(); | 187 return value.v8Value(); |
| 188 } | 188 } |
| 189 | 189 |
| 190 // Dictionary | 190 // Dictionary |
| 191 | 191 |
| 192 inline v8::Handle<v8::Value> toV8(const Dictionary& value, v8::Handle<v8::Object
> creationContext, v8::Isolate*) | 192 inline v8::Local<v8::Value> toV8(const Dictionary& value, v8::Local<v8::Object>
creationContext, v8::Isolate*) |
| 193 { | 193 { |
| 194 RELEASE_ASSERT_NOT_REACHED(); | 194 RELEASE_ASSERT_NOT_REACHED(); |
| 195 return v8::Handle<v8::Value>(); | 195 return v8::Local<v8::Value>(); |
| 196 } | 196 } |
| 197 | 197 |
| 198 // Array | 198 // Array |
| 199 | 199 |
| 200 template<typename Sequence> | 200 template<typename Sequence> |
| 201 inline v8::Handle<v8::Value> toV8SequenceInternal(const Sequence& sequence, v8::
Handle<v8::Object> creationContext, v8::Isolate* isolate) | 201 inline v8::Local<v8::Value> toV8SequenceInternal(const Sequence& sequence, v8::L
ocal<v8::Object> creationContext, v8::Isolate* isolate) |
| 202 { | 202 { |
| 203 v8::Local<v8::Array> array = v8::Array::New(isolate, sequence.size()); | 203 v8::Local<v8::Array> array = v8::Array::New(isolate, sequence.size()); |
| 204 uint32_t index = 0; | 204 uint32_t index = 0; |
| 205 typename Sequence::const_iterator end = sequence.end(); | 205 typename Sequence::const_iterator end = sequence.end(); |
| 206 for (typename Sequence::const_iterator iter = sequence.begin(); iter != end;
++iter) { | 206 for (typename Sequence::const_iterator iter = sequence.begin(); iter != end;
++iter) { |
| 207 v8::Local<v8::Value> value = toV8(*iter, creationContext, isolate); | 207 v8::Local<v8::Value> value = toV8(*iter, creationContext, isolate); |
| 208 if (value.IsEmpty()) | 208 if (value.IsEmpty()) |
| 209 value = v8::Undefined(isolate); | 209 value = v8::Undefined(isolate); |
| 210 array->Set(v8::Integer::New(isolate, index++), value); | 210 array->Set(v8::Integer::New(isolate, index++), value); |
| 211 } | 211 } |
| 212 return array; | 212 return array; |
| 213 } | 213 } |
| 214 | 214 |
| 215 template<typename T, size_t inlineCapacity> | 215 template<typename T, size_t inlineCapacity> |
| 216 inline v8::Handle<v8::Value> toV8(const Vector<T, inlineCapacity>& value, v8::Ha
ndle<v8::Object> creationContext, v8::Isolate* isolate) | 216 inline v8::Local<v8::Value> toV8(const Vector<T, inlineCapacity>& value, v8::Loc
al<v8::Object> creationContext, v8::Isolate* isolate) |
| 217 { | 217 { |
| 218 return toV8SequenceInternal(value, creationContext, isolate); | 218 return toV8SequenceInternal(value, creationContext, isolate); |
| 219 } | 219 } |
| 220 | 220 |
| 221 template<typename T, size_t inlineCapacity> | 221 template<typename T, size_t inlineCapacity> |
| 222 inline v8::Handle<v8::Value> toV8(const HeapVector<T, inlineCapacity>& value, v8
::Handle<v8::Object> creationContext, v8::Isolate* isolate) | 222 inline v8::Local<v8::Value> toV8(const HeapVector<T, inlineCapacity>& value, v8:
:Local<v8::Object> creationContext, v8::Isolate* isolate) |
| 223 { | 223 { |
| 224 return toV8SequenceInternal(value, creationContext, isolate); | 224 return toV8SequenceInternal(value, creationContext, isolate); |
| 225 } | 225 } |
| 226 | 226 |
| 227 template<typename T> | 227 template<typename T> |
| 228 inline v8::Handle<v8::Value> toV8(const Vector<std::pair<String, T>>& value, v8:
:Handle<v8::Object> creationContext, v8::Isolate* isolate) | 228 inline v8::Local<v8::Value> toV8(const Vector<std::pair<String, T>>& value, v8::
Local<v8::Object> creationContext, v8::Isolate* isolate) |
| 229 { | 229 { |
| 230 v8::Local<v8::Object> object = v8::Object::New(isolate); | 230 v8::Local<v8::Object> object = v8::Object::New(isolate); |
| 231 for (unsigned i = 0; i < value.size(); ++i) { | 231 for (unsigned i = 0; i < value.size(); ++i) { |
| 232 v8::Local<v8::Value> v8Value = toV8(value[i].second, creationContext, is
olate); | 232 v8::Local<v8::Value> v8Value = toV8(value[i].second, creationContext, is
olate); |
| 233 if (v8Value.IsEmpty()) | 233 if (v8Value.IsEmpty()) |
| 234 v8Value = v8::Undefined(isolate); | 234 v8Value = v8::Undefined(isolate); |
| 235 object->Set(v8String(isolate, value[i].first), v8Value); | 235 object->Set(v8String(isolate, value[i].first), v8Value); |
| 236 } | 236 } |
| 237 return object; | 237 return object; |
| 238 } | 238 } |
| 239 | 239 |
| 240 } // namespace blink | 240 } // namespace blink |
| 241 | 241 |
| 242 #endif // ToV8_h | 242 #endif // ToV8_h |
| OLD | NEW |