| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // its destruction. | 78 // its destruction. |
| 79 // A ScriptPromiseResolver can be destructed safely without | 79 // A ScriptPromiseResolver can be destructed safely without |
| 80 // entering a v8 context. | 80 // entering a v8 context. |
| 81 ~ScriptPromiseResolver(); | 81 ~ScriptPromiseResolver(); |
| 82 | 82 |
| 83 // Returns the underlying Promise. | 83 // Returns the underlying Promise. |
| 84 // Note that the underlying Promise is cleared when |resolve| or |reject| | 84 // Note that the underlying Promise is cleared when |resolve| or |reject| |
| 85 // is called. | 85 // is called. |
| 86 ScriptPromise promise(); | 86 ScriptPromise promise(); |
| 87 | 87 |
| 88 // To use following template methods, T must be a DOM class. | 88 template <typename T> |
| 89 template<typename T> | 89 void resolve(T value) |
| 90 void resolve(T* value, v8::Handle<v8::Object> creationContext) { resolve(toV
8NoInline(value, creationContext, m_isolate)); } | 90 { |
| 91 template<typename T> | 91 ASSERT(m_isolate->InContext()); |
| 92 void reject(T* value, v8::Handle<v8::Object> creationContext) { reject(toV8N
oInline(value, creationContext, m_isolate)); } | 92 resolve(value, v8::Object::New(m_isolate)); |
| 93 } |
| 94 template <typename T> |
| 95 inline void reject(T value) |
| 96 { |
| 97 ASSERT(m_isolate->InContext()); |
| 98 reject(value, v8::Object::New(m_isolate)); |
| 99 } |
| 93 | 100 |
| 94 template<typename T> | 101 template <typename T> |
| 95 void resolve(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) {
resolve(value.get(), creationContext); } | 102 inline void resolve(T value, ExecutionContext* context) |
| 96 template<typename T> | 103 { |
| 97 void resolve(RawPtr<T> value, v8::Handle<v8::Object> creationContext) { reso
lve(value.get(), creationContext); } | 104 ASSERT(m_isolate->InContext()); |
| 98 template<typename T> | 105 resolve(toV8Value(value, context)); |
| 99 void reject(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { r
eject(value.get(), creationContext); } | 106 } |
| 100 template<typename T> | 107 template <typename T> |
| 101 void reject(RawPtr<T> value, v8::Handle<v8::Object> creationContext) { rejec
t(value.get(), creationContext); } | 108 inline void reject(T value, ExecutionContext* context) |
| 102 | 109 { |
| 103 template<typename T> | 110 ASSERT(m_isolate->InContext()); |
| 104 inline void resolve(T* value, ExecutionContext*); | 111 reject(toV8Value(value, context)); |
| 105 template<typename T> | 112 } |
| 106 inline void reject(T* value, ExecutionContext*); | 113 template <typename T> |
| 107 | 114 inline void resolve(T value, v8::Handle<v8::Object> creationContext) |
| 108 template<typename T> | 115 { |
| 109 void resolve(PassRefPtr<T> value, ExecutionContext* context) { resolve(value
.get(), context); } | 116 ASSERT(m_isolate->InContext()); |
| 110 template<typename T> | 117 resolve(toV8Value(value, creationContext)); |
| 111 void resolve(RawPtr<T> value, ExecutionContext* context) { resolve(value.get
(), context); } | 118 } |
| 112 template<typename T> | 119 template <typename T> |
| 113 void reject(PassRefPtr<T> value, ExecutionContext* context) { reject(value.g
et(), context); } | 120 inline void reject(T value, v8::Handle<v8::Object> creationContext) |
| 114 template<typename T> | 121 { |
| 115 void reject(RawPtr<T> value, ExecutionContext* context) { reject(value.get()
, context); } | 122 ASSERT(m_isolate->InContext()); |
| 116 | 123 reject(toV8Value(value, creationContext)); |
| 117 template<typename T> | 124 } |
| 118 inline void resolve(T* value); | |
| 119 template<typename T> | |
| 120 inline void reject(T* value); | |
| 121 | |
| 122 template<typename T, size_t inlineCapacity> | |
| 123 void resolve(const Vector<T, inlineCapacity>& iterator) { resolve(v8ArrayNoI
nline(iterator, m_isolate)); } | |
| 124 template<typename T, size_t inlineCapacity> | |
| 125 void reject(const Vector<T, inlineCapacity>& iterator) { reject(v8ArrayNoInl
ine(iterator, m_isolate)); } | |
| 126 | |
| 127 template<typename T> | |
| 128 void resolve(PassRefPtr<T> value) { resolve(value.get()); } | |
| 129 template<typename T> | |
| 130 void resolve(RawPtr<T> value) { resolve(value.get()); } | |
| 131 template<typename T> | |
| 132 void reject(PassRefPtr<T> value) { reject(value.get()); } | |
| 133 template<typename T> | |
| 134 void reject(RawPtr<T> value) { reject(value.get()); } | |
| 135 | |
| 136 void resolve(ScriptValue); | |
| 137 void reject(ScriptValue); | |
| 138 | |
| 139 v8::Isolate* isolate() const { return m_isolate; } | |
| 140 | 125 |
| 141 void resolve(v8::Handle<v8::Value>); | 126 void resolve(v8::Handle<v8::Value>); |
| 142 void reject(v8::Handle<v8::Value>); | 127 void reject(v8::Handle<v8::Value>); |
| 143 | 128 |
| 129 v8::Isolate* isolate() const { return m_isolate; } |
| 130 |
| 144 private: | 131 private: |
| 132 |
| 133 template <typename T, bool toV8ValueNeedsCreationContext> |
| 134 struct ToV8ValueHelper; |
| 135 template <typename T> |
| 136 struct ToV8ValueHelper<T, true> { |
| 137 static v8::Handle<v8::Value> toV8Value(const T& value, ExecutionContext*
context, v8::Isolate* isolate) |
| 138 { |
| 139 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperW
orld::current(isolate)); |
| 140 return V8ValueTraits<T>::toV8Value(value, v8Context->Global(), isola
te); |
| 141 } |
| 142 static v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Ob
ject> creationContext, v8::Isolate* isolate) |
| 143 { |
| 144 return V8ValueTraits<T>::toV8Value(value, creationContext, isolate); |
| 145 } |
| 146 }; |
| 147 template <typename T> |
| 148 struct ToV8ValueHelper<T, false> { |
| 149 static v8::Handle<v8::Value> toV8Value(const T& value, ExecutionContext*
, v8::Isolate* isolate) |
| 150 { |
| 151 return V8ValueTraits<T>::toV8Value(value, isolate); |
| 152 } |
| 153 static v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Ob
ject>, v8::Isolate* isolate) |
| 154 { |
| 155 return V8ValueTraits<T>::toV8Value(value, isolate); |
| 156 } |
| 157 }; |
| 158 |
| 159 template <typename T> |
| 160 v8::Handle<v8::Value> toV8Value(const T& value, ExecutionContext* context) |
| 161 { |
| 162 return ToV8ValueHelper<T, V8ValueTraits<T>::toV8ValueNeedsCreationContex
t>::toV8Value(value, context, m_isolate); |
| 163 } |
| 164 template <typename T> |
| 165 v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Object> creat
ionContext) |
| 166 { |
| 167 return ToV8ValueHelper<T, V8ValueTraits<T>::toV8ValueNeedsCreationContex
t>::toV8Value(value, creationContext, m_isolate); |
| 168 } |
| 169 |
| 145 ScriptPromiseResolver(ExecutionContext*); | 170 ScriptPromiseResolver(ExecutionContext*); |
| 146 ScriptPromiseResolver(v8::Isolate*); | 171 ScriptPromiseResolver(v8::Isolate*); |
| 147 | 172 |
| 148 v8::Isolate* m_isolate; | 173 v8::Isolate* m_isolate; |
| 149 // Used when scriptPromiseOnV8Promise is disabled. | 174 // Used when scriptPromiseOnV8Promise is disabled. |
| 150 ScriptPromise m_promise; | 175 ScriptPromise m_promise; |
| 151 // Used when scriptPromiseOnV8Promise is enabled. | 176 // Used when scriptPromiseOnV8Promise is enabled. |
| 152 ScriptValue m_resolver; | 177 ScriptValue m_resolver; |
| 153 }; | 178 }; |
| 154 | 179 |
| 155 template<typename T> | |
| 156 void ScriptPromiseResolver::resolve(T* value, ExecutionContext* context) | |
| 157 { | |
| 158 ASSERT(m_isolate->InContext()); | |
| 159 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu
rrent(m_isolate)); | |
| 160 resolve(value, v8Context->Global()); | |
| 161 } | |
| 162 | |
| 163 template<typename T> | |
| 164 void ScriptPromiseResolver::reject(T* value, ExecutionContext* context) | |
| 165 { | |
| 166 ASSERT(m_isolate->InContext()); | |
| 167 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu
rrent(m_isolate)); | |
| 168 reject(value, v8Context->Global()); | |
| 169 } | |
| 170 | |
| 171 template<typename T> | |
| 172 void ScriptPromiseResolver::resolve(T* value) | |
| 173 { | |
| 174 ASSERT(m_isolate->InContext()); | |
| 175 resolve(value, v8::Object::New(m_isolate)); | |
| 176 } | |
| 177 | |
| 178 template<typename T> | |
| 179 void ScriptPromiseResolver::reject(T* value) | |
| 180 { | |
| 181 ASSERT(m_isolate->InContext()); | |
| 182 reject(value, v8::Object::New(m_isolate)); | |
| 183 } | |
| 184 | |
| 185 } // namespace WebCore | 180 } // namespace WebCore |
| 186 | 181 |
| 187 | 182 |
| 188 #endif // ScriptPromiseResolver_h | 183 #endif // ScriptPromiseResolver_h |
| OLD | NEW |