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 18 matching lines...) Expand all Loading... | |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "bindings/v8/custom/V8PromiseCustom.h" | 32 #include "bindings/v8/custom/V8PromiseCustom.h" |
33 | 33 |
34 #include "V8Promise.h" | 34 #include "V8Promise.h" |
35 #include "bindings/v8/ScopedPersistent.h" | 35 #include "bindings/v8/ScopedPersistent.h" |
36 #include "bindings/v8/ScriptFunctionCall.h" | 36 #include "bindings/v8/ScriptFunctionCall.h" |
37 #include "bindings/v8/ScriptState.h" | 37 #include "bindings/v8/ScriptState.h" |
38 #include "bindings/v8/V8Binding.h" | 38 #include "bindings/v8/V8Binding.h" |
39 #include "bindings/v8/V8HiddenPropertyName.h" | |
39 #include "bindings/v8/V8PerIsolateData.h" | 40 #include "bindings/v8/V8PerIsolateData.h" |
40 #include "bindings/v8/V8ScriptRunner.h" | 41 #include "bindings/v8/V8ScriptRunner.h" |
41 #include "bindings/v8/WrapperTypeInfo.h" | 42 #include "bindings/v8/WrapperTypeInfo.h" |
42 #include "core/dom/Document.h" | 43 #include "core/dom/Document.h" |
43 #include "core/page/DOMWindow.h" | 44 #include "core/page/DOMWindow.h" |
44 #include "core/platform/Task.h" | 45 #include "core/platform/Task.h" |
45 #include "core/workers/WorkerGlobalScope.h" | 46 #include "core/workers/WorkerGlobalScope.h" |
46 #include "wtf/Functional.h" | 47 #include "wtf/Functional.h" |
47 #include "wtf/PassOwnPtr.h" | 48 #include "wtf/PassOwnPtr.h" |
48 #include <v8.h> | 49 #include <v8.h> |
(...skipping 10 matching lines...) Expand all Loading... | |
59 if (!functionDescriptor.IsEmpty()) | 60 if (!functionDescriptor.IsEmpty()) |
60 return functionDescriptor->InstanceTemplate(); | 61 return functionDescriptor->InstanceTemplate(); |
61 | 62 |
62 functionDescriptor = v8::FunctionTemplate::New(); | 63 functionDescriptor = v8::FunctionTemplate::New(); |
63 v8::Local<v8::ObjectTemplate> instanceTemplate = functionDescriptor->Instanc eTemplate(); | 64 v8::Local<v8::ObjectTemplate> instanceTemplate = functionDescriptor->Instanc eTemplate(); |
64 instanceTemplate->SetInternalFieldCount(internalFieldCount); | 65 instanceTemplate->SetInternalFieldCount(internalFieldCount); |
65 data->setPrivateTemplate(currentWorldType, privateTemplateUniqueKey, functio nDescriptor); | 66 data->setPrivateTemplate(currentWorldType, privateTemplateUniqueKey, functio nDescriptor); |
66 return instanceTemplate; | 67 return instanceTemplate; |
67 } | 68 } |
68 | 69 |
69 v8::Local<v8::ObjectTemplate> wrapperCallbackEnvironmentObjectTemplate(v8::Isola te* isolate) | |
70 { | |
71 // This is only for getting a unique pointer which we can pass to privateTem plate. | |
72 static int privateTemplateUniqueKey = 0; | |
73 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Wrap perCallbackEnvironmentFieldCount, isolate); | |
74 } | |
75 | |
76 v8::Local<v8::ObjectTemplate> promiseAllEnvironmentObjectTemplate(v8::Isolate* i solate) | 70 v8::Local<v8::ObjectTemplate> promiseAllEnvironmentObjectTemplate(v8::Isolate* i solate) |
77 { | 71 { |
78 // This is only for getting a unique pointer which we can pass to privateTem plate. | 72 // This is only for getting a unique pointer which we can pass to privateTem plate. |
79 static int privateTemplateUniqueKey = 0; | 73 static int privateTemplateUniqueKey = 0; |
80 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Prom iseAllEnvironmentFieldCount, isolate); | 74 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Prom iseAllEnvironmentFieldCount, isolate); |
81 } | 75 } |
82 | 76 |
83 v8::Local<v8::ObjectTemplate> primitiveWrapperObjectTemplate(v8::Isolate* isolat e) | 77 v8::Local<v8::ObjectTemplate> primitiveWrapperObjectTemplate(v8::Isolate* isolat e) |
84 { | 78 { |
85 // This is only for getting a unique pointer which we can pass to privateTem plate. | 79 // This is only for getting a unique pointer which we can pass to privateTem plate. |
86 static int privateTemplateUniqueKey = 0; | 80 static int privateTemplateUniqueKey = 0; |
87 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Prim itiveWrapperFieldCount, isolate); | 81 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Prim itiveWrapperFieldCount, isolate); |
88 } | 82 } |
89 | 83 |
90 v8::Local<v8::ObjectTemplate> internalObjectTemplate(v8::Isolate* isolate) | 84 v8::Local<v8::ObjectTemplate> internalObjectTemplate(v8::Isolate* isolate) |
91 { | 85 { |
92 // This is only for getting a unique pointer which we can pass to privateTem plate. | 86 // This is only for getting a unique pointer which we can pass to privateTem plate. |
93 static int privateTemplateUniqueKey = 0; | 87 static int privateTemplateUniqueKey = 0; |
94 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Inte rnalFieldCount, isolate); | 88 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Inte rnalFieldCount, isolate); |
95 } | 89 } |
96 | 90 |
97 class PromiseTask : public ScriptExecutionContext::Task { | |
98 public: | |
99 PromiseTask(v8::Handle<v8::Function> callback, v8::Handle<v8::Object> receiv er, v8::Handle<v8::Value> result, v8::Isolate* isolate) | |
100 : m_callback(isolate, callback) | |
101 , m_receiver(isolate, receiver) | |
102 , m_result(isolate, result) | |
103 { | |
104 ASSERT(!m_callback.isEmpty()); | |
105 ASSERT(!m_receiver.isEmpty()); | |
106 ASSERT(!m_result.isEmpty()); | |
107 } | |
108 virtual ~PromiseTask() { } | |
109 | |
110 virtual void performTask(ScriptExecutionContext*) OVERRIDE; | |
111 | |
112 private: | |
113 ScopedPersistent<v8::Function> m_callback; | |
114 ScopedPersistent<v8::Object> m_receiver; | |
115 ScopedPersistent<v8::Value> m_result; | |
116 }; | |
117 | |
118 void PromiseTask::performTask(ScriptExecutionContext* context) | |
119 { | |
120 ASSERT(context); | |
121 if (context->activeDOMObjectsAreStopped()) | |
122 return; | |
123 | |
124 ScriptState* state = 0; | |
125 if (context->isDocument()) { | |
126 state = mainWorldScriptState(static_cast<Document*>(context)->frame()); | |
127 } else { | |
128 ASSERT(context->isWorkerGlobalScope()); | |
129 state = scriptStateFromWorkerGlobalScope(toWorkerGlobalScope(context)); | |
130 } | |
131 ASSERT(state); | |
132 | |
133 v8::Isolate* isolate = state->isolate(); | |
134 v8::HandleScope handleScope(isolate); | |
135 v8::Handle<v8::Context> v8Context = state->context(); | |
136 v8::Context::Scope scope(v8Context); | |
137 v8::Handle<v8::Value> args[] = { m_result.newLocal(isolate) }; | |
138 V8ScriptRunner::callFunction(m_callback.newLocal(isolate), context, m_receiv er.newLocal(isolate), WTF_ARRAY_LENGTH(args), args, isolate); | |
139 }; | |
140 | |
141 v8::Handle<v8::Value> postTask(v8::Handle<v8::Function> callback, v8::Handle<v8: :Object> receiver, v8::Handle<v8::Value> value, v8::Isolate* isolate) | |
142 { | |
143 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext() ; | |
144 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextThread()); | |
145 scriptExecutionContext->postTask(adoptPtr(new PromiseTask(callback, receiver , value, isolate))); | |
146 return v8::Undefined(isolate); | |
147 } | |
148 | |
149 void wrapperCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | |
150 { | |
151 v8::Isolate* isolate = args.GetIsolate(); | |
152 ASSERT(!args.Data().IsEmpty()); | |
153 v8::Local<v8::Object> environment = args.Data().As<v8::Object>(); | |
154 v8::Local<v8::Value> result = v8::Undefined(isolate); | |
155 if (args.Length() > 0) | |
156 result = args[0]; | |
157 | |
158 v8::Local<v8::Object> promise = environment->GetInternalField(V8PromiseCusto m::WrapperCallbackEnvironmentPromiseIndex).As<v8::Object>(); | |
159 v8::Local<v8::Function> callback = environment->GetInternalField(V8PromiseCu stom::WrapperCallbackEnvironmentCallbackIndex).As<v8::Function>(); | |
160 | |
161 v8::Local<v8::Value> argv[] = { | |
162 result, | |
163 }; | |
164 v8::TryCatch trycatch; | |
165 result = V8ScriptRunner::callFunction(callback, getScriptExecutionContext(), promise, WTF_ARRAY_LENGTH(argv), argv, isolate); | |
166 if (result.IsEmpty()) { | |
167 V8PromiseCustom::reject(promise, trycatch.Exception(), V8PromiseCustom:: Synchronous, isolate); | |
168 return; | |
169 } | |
170 V8PromiseCustom::resolve(promise, result, V8PromiseCustom::Synchronous, isol ate); | |
171 } | |
172 | |
173 v8::Local<v8::Object> wrapperCallbackEnvironment(v8::Handle<v8::Object> promise, v8::Handle<v8::Function> callback, v8::Isolate* isolate) | |
174 { | |
175 v8::Local<v8::ObjectTemplate> objectTemplate = wrapperCallbackEnvironmentObj ectTemplate(isolate); | |
176 v8::Local<v8::Object> environment = objectTemplate->NewInstance(); | |
177 environment->SetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentPro miseIndex, promise); | |
178 environment->SetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentCal lbackIndex, callback); | |
179 return environment; | |
180 } | |
181 | |
182 void promiseFulfillCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | |
183 { | |
184 ASSERT(!args.Data().IsEmpty()); | |
185 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | |
186 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); | |
187 if (args.Length() > 0) | |
188 result = args[0]; | |
189 | |
190 V8PromiseCustom::fulfill(promise, result, V8PromiseCustom::Synchronous, args .GetIsolate()); | |
191 } | |
192 | |
193 void promiseResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | 91 void promiseResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
194 { | 92 { |
195 ASSERT(!args.Data().IsEmpty()); | 93 ASSERT(!args.Data().IsEmpty()); |
196 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | 94 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); |
197 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); | 95 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); |
198 if (args.Length() > 0) | 96 if (args.Length() > 0) |
199 result = args[0]; | 97 result = args[0]; |
200 | 98 |
201 V8PromiseCustom::resolve(promise, result, V8PromiseCustom::Synchronous, args .GetIsolate()); | 99 V8PromiseCustom::resolve(promise, result, args.GetIsolate()); |
202 } | 100 } |
203 | 101 |
204 void promiseRejectCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | 102 void promiseRejectCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
205 { | 103 { |
206 ASSERT(!args.Data().IsEmpty()); | 104 ASSERT(!args.Data().IsEmpty()); |
207 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | 105 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); |
208 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); | 106 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); |
209 if (args.Length() > 0) | 107 if (args.Length() > 0) |
210 result = args[0]; | 108 result = args[0]; |
211 | 109 |
212 V8PromiseCustom::reject(promise, result, V8PromiseCustom::Synchronous, args. GetIsolate()); | 110 V8PromiseCustom::reject(promise, result, args.GetIsolate()); |
213 } | |
214 | |
215 void callCallbacks(v8::Handle<v8::Array> callbacks, v8::Handle<v8::Value> result , V8PromiseCustom::SynchronousMode mode, v8::Isolate* isolate) | |
216 { | |
217 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); | |
218 for (uint32_t i = 0, length = callbacks->Length(); i < length; ++i) { | |
219 v8::Local<v8::Value> value = callbacks->Get(i); | |
220 v8::Local<v8::Function> callback = value.As<v8::Function>(); | |
221 V8PromiseCustom::call(callback, global, result, mode, isolate); | |
222 } | |
223 } | 111 } |
224 | 112 |
225 void promiseAllFulfillCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | 113 void promiseAllFulfillCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
226 { | 114 { |
227 v8::Isolate* isolate = args.GetIsolate(); | 115 v8::Isolate* isolate = args.GetIsolate(); |
228 ASSERT(!args.Data().IsEmpty()); | 116 ASSERT(!args.Data().IsEmpty()); |
229 v8::Local<v8::Object> environment = args.Data().As<v8::Object>(); | 117 v8::Local<v8::Object> environment = args.Data().As<v8::Object>(); |
230 v8::Local<v8::Value> result = v8::Undefined(isolate); | 118 v8::Local<v8::Value> result = v8::Undefined(isolate); |
231 if (args.Length() > 0) | 119 if (args.Length() > 0) |
232 result = args[0]; | 120 result = args[0]; |
233 | 121 |
234 v8::Local<v8::Object> promise = environment->GetInternalField(V8PromiseCusto m::PromiseAllEnvironmentPromiseIndex).As<v8::Object>(); | 122 v8::Local<v8::Object> promise = environment->GetInternalField(V8PromiseCusto m::PromiseAllEnvironmentPromiseIndex).As<v8::Object>(); |
235 v8::Local<v8::Object> countdownWrapper = environment->GetInternalField(V8Pro miseCustom::PromiseAllEnvironmentCountdownIndex).As<v8::Object>(); | 123 v8::Local<v8::Object> countdownWrapper = environment->GetInternalField(V8Pro miseCustom::PromiseAllEnvironmentCountdownIndex).As<v8::Object>(); |
236 v8::Local<v8::Integer> index = environment->GetInternalField(V8PromiseCustom ::PromiseAllEnvironmentIndexIndex).As<v8::Integer>(); | 124 v8::Local<v8::Integer> index = environment->GetInternalField(V8PromiseCustom ::PromiseAllEnvironmentIndexIndex).As<v8::Integer>(); |
237 v8::Local<v8::Array> results = environment->GetInternalField(V8PromiseCustom ::PromiseAllEnvironmentResultsIndex).As<v8::Array>(); | 125 v8::Local<v8::Array> results = environment->GetInternalField(V8PromiseCustom ::PromiseAllEnvironmentResultsIndex).As<v8::Array>(); |
238 | 126 |
239 results->Set(index->Value(), result); | 127 results->Set(index->Value(), result); |
240 | 128 |
241 v8::Local<v8::Integer> countdown = countdownWrapper->GetInternalField(V8Prom iseCustom::PrimitiveWrapperPrimitiveIndex).As<v8::Integer>(); | 129 v8::Local<v8::Integer> countdown = countdownWrapper->GetInternalField(V8Prom iseCustom::PrimitiveWrapperPrimitiveIndex).As<v8::Integer>(); |
242 ASSERT(countdown->Value() >= 1); | 130 ASSERT(countdown->Value() >= 1); |
243 if (countdown->Value() == 1) { | 131 if (countdown->Value() == 1) { |
244 V8PromiseCustom::resolve(promise, results, V8PromiseCustom::Synchronous, isolate); | 132 V8PromiseCustom::resolve(promise, results, isolate); |
245 return; | 133 return; |
246 } | 134 } |
247 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(countdown->Value() - 1, isolate)); | 135 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(countdown->Value() - 1, isolate)); |
248 } | 136 } |
249 | 137 |
250 v8::Local<v8::Object> promiseAllEnvironment(v8::Handle<v8::Object> promise, v8:: Handle<v8::Object> countdownWrapper, int index, v8::Handle<v8::Array> results, v 8::Isolate* isolate) | 138 v8::Local<v8::Object> promiseAllEnvironment(v8::Handle<v8::Object> promise, v8:: Handle<v8::Object> countdownWrapper, int index, v8::Handle<v8::Array> results, v 8::Isolate* isolate) |
251 { | 139 { |
252 v8::Local<v8::ObjectTemplate> objectTemplate = promiseAllEnvironmentObjectTe mplate(isolate); | 140 v8::Local<v8::ObjectTemplate> objectTemplate = promiseAllEnvironmentObjectTe mplate(isolate); |
253 v8::Local<v8::Object> environment = objectTemplate->NewInstance(); | 141 v8::Local<v8::Object> environment = objectTemplate->NewInstance(); |
254 | 142 |
255 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentPromiseI ndex, promise); | 143 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentPromiseI ndex, promise); |
256 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentCountdow nIndex, countdownWrapper); | 144 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentCountdow nIndex, countdownWrapper); |
257 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentIndexInd ex, v8::Integer::New(index, isolate)); | 145 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentIndexInd ex, v8::Integer::New(index, isolate)); |
258 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentResultsI ndex, results); | 146 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentResultsI ndex, results); |
259 return environment; | 147 return environment; |
260 } | 148 } |
261 | 149 |
262 void promiseResolve(const v8::FunctionCallbackInfo<v8::Value>& args) | 150 // Clear |internal|'s derived array. |
151 void clearDerived(v8::Handle<v8::Object> internal) | |
263 { | 152 { |
264 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | 153 internal->SetInternalField(V8PromiseCustom::InternalFulfillCallbackIndex, v8 ::Array::New()); |
265 ASSERT(!promise.IsEmpty()); | 154 internal->SetInternalField(V8PromiseCustom::InternalRejectCallbackIndex, v8: :Array::New()); |
266 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); | 155 internal->SetInternalField(V8PromiseCustom::InternalDerivedPromiseIndex, v8: :Array::New()); |
267 if (V8PromiseCustom::getState(internal) != V8PromiseCustom::Pending) | |
268 return; | |
269 v8::Isolate* isolate = args.GetIsolate(); | |
270 V8PromiseCustom::setState(V8PromiseCustom::getInternal(promise), V8PromiseCu stom::Following, isolate); | |
271 | |
272 v8::Local<v8::Value> result = v8::Undefined(isolate); | |
273 if (args.Length() > 0) | |
274 result = args[0]; | |
275 V8PromiseCustom::resolve(promise, result, V8PromiseCustom::Asynchronous, iso late); | |
276 } | 156 } |
277 | 157 |
278 void promiseReject(const v8::FunctionCallbackInfo<v8::Value>& args) | 158 // Add a tuple (|derivedPromise|, |onFulfilled|, |onRejected|) to |
159 // |internal|'s derived array. | |
160 // |internal| must be a Promise internal object. | |
161 // |derivedPromise| must be a Promise instance. | |
162 // |onFulfilled| and |onRejected| can be an empty value respectively. | |
163 void addToDerived(v8::Handle<v8::Object> internal, v8::Handle<v8::Object> derive dPromise, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejec ted, v8::Isolate* isolate) | |
279 { | 164 { |
280 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | 165 v8::Local<v8::Array> fulfillCallbacks = internal->GetInternalField(V8Promise Custom::InternalFulfillCallbackIndex).As<v8::Array>(); |
281 ASSERT(!promise.IsEmpty()); | 166 v8::Local<v8::Array> rejectCallbacks = internal->GetInternalField(V8PromiseC ustom::InternalRejectCallbackIndex).As<v8::Array>(); |
282 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); | 167 v8::Local<v8::Array> derivedPromises = internal->GetInternalField(V8PromiseC ustom::InternalDerivedPromiseIndex).As<v8::Array>(); |
283 if (V8PromiseCustom::getState(internal) != V8PromiseCustom::Pending) | 168 |
169 if (onFulfilled.IsEmpty()) { | |
170 fulfillCallbacks->Set(fulfillCallbacks->Length(), v8::Undefined(isolate) ); | |
171 } else { | |
172 fulfillCallbacks->Set(fulfillCallbacks->Length(), onFulfilled); | |
173 } | |
174 | |
175 if (onRejected.IsEmpty()) { | |
176 rejectCallbacks->Set(rejectCallbacks->Length(), v8::Undefined(isolate)); | |
177 } else { | |
178 rejectCallbacks->Set(rejectCallbacks->Length(), onRejected); | |
179 } | |
180 | |
181 ASSERT(!derivedPromise.IsEmpty()); | |
182 derivedPromises->Set(derivedPromises->Length(), derivedPromise); | |
183 | |
184 // Since they are treated as a tuple, | |
185 // we need to guaranteed that the length of these arrays are same. | |
186 ASSERT(fulfillCallbacks->Length() == rejectCallbacks->Length() && rejectCall backs->Length() == derivedPromises->Length()); | |
187 } | |
188 | |
189 class CallHandlerTask : public ScriptExecutionContext::Task { | |
190 public: | |
191 CallHandlerTask(v8::Handle<v8::Object> promise, v8::Handle<v8::Function> han dler, v8::Handle<v8::Value> argument, v8::Isolate* isolate) | |
192 : m_promise(isolate, promise) | |
193 , m_handler(isolate, handler) | |
194 , m_argument(isolate, argument) | |
195 { | |
196 ASSERT(!m_promise.isEmpty()); | |
197 ASSERT(!m_handler.isEmpty()); | |
198 ASSERT(!m_argument.isEmpty()); | |
199 } | |
200 virtual ~CallHandlerTask() { } | |
201 | |
202 virtual void performTask(ScriptExecutionContext*) OVERRIDE; | |
203 | |
204 private: | |
205 ScopedPersistent<v8::Object> m_promise; | |
206 ScopedPersistent<v8::Function> m_handler; | |
207 ScopedPersistent<v8::Value> m_argument; | |
208 }; | |
209 | |
210 void CallHandlerTask::performTask(ScriptExecutionContext* context) | |
211 { | |
212 ASSERT(context); | |
213 if (context->activeDOMObjectsAreStopped()) | |
284 return; | 214 return; |
285 v8::Isolate* isolate = args.GetIsolate(); | |
286 V8PromiseCustom::setState(V8PromiseCustom::getInternal(promise), V8PromiseCu stom::Following, isolate); | |
287 | 215 |
288 v8::Local<v8::Value> result = v8::Undefined(isolate); | 216 ScriptState* state = 0; |
289 if (args.Length() > 0) | 217 if (context->isDocument()) { |
290 result = args[0]; | 218 state = mainWorldScriptState(static_cast<Document*>(context)->frame()); |
291 V8PromiseCustom::reject(promise, result, V8PromiseCustom::Asynchronous, isol ate); | 219 } else { |
220 ASSERT(context->isWorkerGlobalScope()); | |
221 state = scriptStateFromWorkerGlobalScope(toWorkerGlobalScope(context)); | |
222 } | |
223 ASSERT(state); | |
224 | |
225 v8::Isolate* isolate = state->isolate(); | |
226 v8::HandleScope handleScope(isolate); | |
227 v8::Handle<v8::Context> v8Context = state->context(); | |
228 v8::Context::Scope scope(v8Context); | |
229 v8::Handle<v8::Value> args[] = { m_argument.newLocal(isolate) }; | |
230 v8::TryCatch trycatch; | |
231 // FIXME: Receiver should be undefined. | |
232 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); | |
233 v8::Local<v8::Value> value = V8ScriptRunner::callFunction(m_handler.newLocal (isolate), context, global, WTF_ARRAY_LENGTH(args), args, isolate); | |
234 if (value.IsEmpty()) { | |
235 V8PromiseCustom::reject(m_promise.newLocal(isolate), trycatch.Exception( ), isolate); | |
236 } else { | |
237 V8PromiseCustom::resolve(m_promise.newLocal(isolate), value, isolate); | |
238 } | |
239 } | |
240 | |
241 class UpdateDerivedTask : public ScriptExecutionContext::Task { | |
242 public: | |
243 UpdateDerivedTask(v8::Handle<v8::Object> promise, v8::Handle<v8::Function> o nFulfilled, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Object> originat orObject, v8::Isolate* isolate) | |
244 : m_promise(isolate, promise) | |
245 , m_onFulfilled(isolate, onFulfilled) | |
246 , m_onRejected(isolate, onRejected) | |
247 , m_originatorObject(isolate, originatorObject) | |
248 { | |
249 ASSERT(!m_promise.isEmpty()); | |
250 ASSERT(!m_onFulfilled.isEmpty()); | |
251 ASSERT(!m_onRejected.isEmpty()); | |
252 ASSERT(!m_originatorObject.isEmpty()); | |
253 } | |
254 virtual ~UpdateDerivedTask() { } | |
255 | |
256 virtual void performTask(ScriptExecutionContext*) OVERRIDE; | |
257 | |
258 private: | |
259 ScopedPersistent<v8::Object> m_promise; | |
260 ScopedPersistent<v8::Function> m_onFulfilled; | |
261 ScopedPersistent<v8::Function> m_onRejected; | |
262 ScopedPersistent<v8::Object> m_originatorObject; | |
263 }; | |
264 | |
265 void UpdateDerivedTask::performTask(ScriptExecutionContext* context) | |
266 { | |
267 ASSERT(context); | |
268 if (context->activeDOMObjectsAreStopped()) | |
269 return; | |
270 | |
271 ScriptState* state = 0; | |
272 if (context->isDocument()) { | |
273 state = mainWorldScriptState(static_cast<Document*>(context)->frame()); | |
274 } else { | |
275 ASSERT(context->isWorkerGlobalScope()); | |
276 state = scriptStateFromWorkerGlobalScope(toWorkerGlobalScope(context)); | |
277 } | |
278 ASSERT(state); | |
279 | |
280 v8::Isolate* isolate = state->isolate(); | |
281 v8::HandleScope handleScope(isolate); | |
282 v8::Handle<v8::Context> v8Context = state->context(); | |
283 v8::Context::Scope scope(v8Context); | |
284 | |
285 v8::Local<v8::Object> originatorObject = m_originatorObject.newLocal(isolate ); | |
286 v8::Local<v8::Value> coercedAlready = originatorObject->GetHiddenValue(V8Hid denPropertyName::thenableHiddenPromise(isolate)); | |
287 if (!coercedAlready.IsEmpty() && coercedAlready->IsObject()) { | |
288 ASSERT(V8PromiseCustom::isPromise(coercedAlready.As<v8::Object>(), isola te)); | |
289 V8PromiseCustom::updateDerivedFromPromise(m_promise.newLocal(isolate), m _onFulfilled.newLocal(isolate), m_onRejected.newLocal(isolate), coercedAlready.A s<v8::Object>(), isolate); | |
290 return; | |
291 } | |
292 | |
293 v8::Local<v8::Value> then; | |
294 v8::TryCatch trycatch; | |
295 then = originatorObject->Get(v8::String::NewSymbol("then")); | |
296 if (then.IsEmpty()) { | |
297 // If calling the [[Get]] internal method threw an exception, catch it a nd run updateDerivedFromReason. | |
298 V8PromiseCustom::updateDerivedFromReason(m_promise.newLocal(isolate), m_ onRejected.newLocal(isolate), trycatch.Exception(), isolate); | |
299 return; | |
300 } | |
301 | |
302 if (then->IsFunction()) { | |
303 ASSERT(then->IsObject()); | |
304 v8::Local<v8::Object> coerced = V8PromiseCustom::coerceThenable(originat orObject, then.As<v8::Function>(), isolate); | |
305 V8PromiseCustom::updateDerivedFromPromise(m_promise.newLocal(isolate), m _onFulfilled.newLocal(isolate), m_onRejected.newLocal(isolate), coerced, isolate ); | |
306 return; | |
307 } | |
308 | |
309 V8PromiseCustom::updateDerivedFromValue(m_promise.newLocal(isolate), m_onFul filled.newLocal(isolate), originatorObject, isolate); | |
292 } | 310 } |
293 | 311 |
294 } // namespace | 312 } // namespace |
295 | 313 |
296 void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) | 314 void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) |
297 { | 315 { |
298 v8SetReturnValue(args, v8::Local<v8::Value>()); | 316 v8SetReturnValue(args, v8::Local<v8::Value>()); |
299 v8::Isolate* isolate = args.GetIsolate(); | 317 v8::Isolate* isolate = args.GetIsolate(); |
300 if (!args.Length() || !args[0]->IsFunction()) { | 318 if (!args.Length() || !args[0]->IsFunction()) { |
301 throwTypeError("Promise constructor takes a function argument", isolate) ; | 319 throwTypeError("Promise constructor takes a function argument", isolate) ; |
302 return; | 320 return; |
303 } | 321 } |
304 v8::Local<v8::Function> init = args[0].As<v8::Function>(); | 322 v8::Local<v8::Function> init = args[0].As<v8::Function>(); |
305 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 323 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
306 v8::Handle<v8::Value> argv[] = { | 324 v8::Handle<v8::Value> argv[] = { |
307 createClosure(promiseResolve, promise, isolate), | 325 createClosure(promiseResolveCallback, promise, isolate), |
308 createClosure(promiseReject, promise, isolate) | 326 createClosure(promiseRejectCallback, promise, isolate) |
309 }; | 327 }; |
310 v8::TryCatch trycatch; | 328 v8::TryCatch trycatch; |
311 if (V8ScriptRunner::callFunction(init, getScriptExecutionContext(), promise, WTF_ARRAY_LENGTH(argv), argv, isolate).IsEmpty()) { | 329 // FIXME: Receiver should be undefined. |
330 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); | |
331 if (V8ScriptRunner::callFunction(init, getScriptExecutionContext(), global, WTF_ARRAY_LENGTH(argv), argv, isolate).IsEmpty()) { | |
312 // An exception is thrown. Reject the promise if its resolved flag is un set. | 332 // An exception is thrown. Reject the promise if its resolved flag is un set. |
313 if (V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise)) == V8PromiseCustom::Pending) | 333 V8PromiseCustom::reject(promise, trycatch.Exception(), isolate); |
314 V8PromiseCustom::reject(promise, trycatch.Exception(), V8PromiseCust om::Asynchronous, isolate); | |
315 } | 334 } |
316 v8SetReturnValue(args, promise); | 335 v8SetReturnValue(args, promise); |
317 return; | 336 return; |
318 } | 337 } |
319 | 338 |
320 void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) | 339 void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) |
321 { | 340 { |
322 v8::Isolate* isolate = args.GetIsolate(); | 341 v8::Isolate* isolate = args.GetIsolate(); |
323 v8::Local<v8::Function> fulfillWrapper, rejectWrapper; | 342 v8::Local<v8::Function> onFulfilled, onRejected; |
324 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 343 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
325 if (args.Length() > 0 && !args[0]->IsUndefined()) { | 344 if (args.Length() > 0 && !args[0]->IsUndefined()) { |
326 if (!args[0]->IsFunction()) { | 345 if (!args[0]->IsFunction()) { |
327 v8SetReturnValue(args, throwTypeError("fulfillCallback must be a fun ction or undefined", isolate)); | 346 v8SetReturnValue(args, throwTypeError("onFulfilled must be a functio n or undefined", isolate)); |
328 return; | 347 return; |
329 } | 348 } |
330 fulfillWrapper = createClosure(wrapperCallback, wrapperCallbackEnvironme nt(promise, args[0].As<v8::Function>(), isolate), isolate); | 349 onFulfilled = args[0].As<v8::Function>(); |
331 } else { | |
332 fulfillWrapper = createClosure(promiseFulfillCallback, promise, isolate) ; | |
333 } | 350 } |
334 if (args.Length() > 1 && !args[1]->IsUndefined()) { | 351 if (args.Length() > 1 && !args[1]->IsUndefined()) { |
335 if (!args[1]->IsFunction()) { | 352 if (!args[1]->IsFunction()) { |
336 v8SetReturnValue(args, throwTypeError("rejectCallback must be a func tion or undefined", isolate)); | 353 v8SetReturnValue(args, throwTypeError("onRejected must be a function or undefined", isolate)); |
337 return; | 354 return; |
338 } | 355 } |
339 rejectWrapper = createClosure(wrapperCallback, wrapperCallbackEnvironmen t(promise, args[1].As<v8::Function>(), isolate), isolate); | 356 onRejected = args[1].As<v8::Function>(); |
340 } else { | |
341 rejectWrapper = createClosure(promiseRejectCallback, promise, isolate); | |
342 } | 357 } |
343 V8PromiseCustom::append(args.Holder(), fulfillWrapper, rejectWrapper, isolat e); | 358 v8SetReturnValue(args, V8PromiseCustom::then(args.Holder(), onFulfilled, onR ejected, isolate)); |
344 v8SetReturnValue(args, promise); | |
345 } | 359 } |
346 | 360 |
347 void V8Promise::castMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) | 361 void V8Promise::castMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) |
348 { | 362 { |
349 v8::Isolate* isolate = args.GetIsolate(); | 363 v8::Isolate* isolate = args.GetIsolate(); |
350 v8::Local<v8::Value> result = v8::Undefined(isolate); | 364 v8::Local<v8::Value> result = v8::Undefined(isolate); |
351 if (args.Length() > 0) | 365 if (args.Length() > 0) |
352 result = args[0]; | 366 result = args[0]; |
353 | 367 |
354 v8SetReturnValue(args, V8PromiseCustom::toPromise(result, isolate)); | 368 v8SetReturnValue(args, V8PromiseCustom::toPromise(result, isolate)); |
355 } | 369 } |
356 | 370 |
357 void V8Promise::catchMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) | 371 void V8Promise::catchMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) |
358 { | 372 { |
359 v8::Isolate* isolate = args.GetIsolate(); | 373 v8::Isolate* isolate = args.GetIsolate(); |
360 v8::Local<v8::Function> fulfillWrapper, rejectWrapper; | 374 v8::Local<v8::Function> onFulfilled, onRejected; |
361 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | |
362 | 375 |
363 if (args.Length() > 0 && !args[0]->IsUndefined()) { | 376 if (args.Length() > 0 && !args[0]->IsUndefined()) { |
364 if (!args[0]->IsFunction()) { | 377 if (!args[0]->IsFunction()) { |
365 v8SetReturnValue(args, throwTypeError("rejectCallback must be a func tion or undefined", isolate)); | 378 v8SetReturnValue(args, throwTypeError("onRejected must be a function or undefined", isolate)); |
366 return; | 379 return; |
367 } | 380 } |
368 rejectWrapper = createClosure(wrapperCallback, wrapperCallbackEnvironmen t(promise, args[0].As<v8::Function>(), isolate), isolate); | 381 onRejected = args[0].As<v8::Function>(); |
369 } else { | |
370 rejectWrapper = createClosure(promiseRejectCallback, promise, isolate); | |
371 } | 382 } |
372 fulfillWrapper = createClosure(promiseFulfillCallback, promise, isolate); | 383 v8SetReturnValue(args, V8PromiseCustom::then(args.Holder(), onFulfilled, onR ejected, isolate)); |
373 V8PromiseCustom::append(args.Holder(), fulfillWrapper, rejectWrapper, isolat e); | |
374 v8SetReturnValue(args, promise); | |
375 } | 384 } |
376 | 385 |
377 void V8Promise::resolveMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& a rgs) | 386 void V8Promise::resolveMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& a rgs) |
378 { | 387 { |
379 v8::Isolate* isolate = args.GetIsolate(); | 388 v8::Isolate* isolate = args.GetIsolate(); |
380 v8::Local<v8::Value> result = v8::Undefined(isolate); | 389 v8::Local<v8::Value> result = v8::Undefined(isolate); |
381 if (args.Length() > 0) | 390 if (args.Length() > 0) |
382 result = args[0]; | 391 result = args[0]; |
383 | 392 |
384 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 393 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
385 V8PromiseCustom::resolve(promise, result, V8PromiseCustom::Asynchronous, iso late); | 394 V8PromiseCustom::resolve(promise, result, isolate); |
386 v8SetReturnValue(args, promise); | 395 v8SetReturnValue(args, promise); |
387 } | 396 } |
388 | 397 |
389 void V8Promise::rejectMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& ar gs) | 398 void V8Promise::rejectMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& ar gs) |
390 { | 399 { |
391 v8::Isolate* isolate = args.GetIsolate(); | 400 v8::Isolate* isolate = args.GetIsolate(); |
392 v8::Local<v8::Value> result = v8::Undefined(isolate); | 401 v8::Local<v8::Value> result = v8::Undefined(isolate); |
393 if (args.Length() > 0) | 402 if (args.Length() > 0) |
394 result = args[0]; | 403 result = args[0]; |
395 | 404 |
396 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 405 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
397 V8PromiseCustom::reject(promise, result, V8PromiseCustom::Asynchronous, isol ate); | 406 V8PromiseCustom::reject(promise, result, isolate); |
398 v8SetReturnValue(args, promise); | 407 v8SetReturnValue(args, promise); |
399 } | 408 } |
400 | 409 |
401 void V8Promise::raceMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) | 410 void V8Promise::raceMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) |
402 { | 411 { |
403 v8::Isolate* isolate = args.GetIsolate(); | 412 v8::Isolate* isolate = args.GetIsolate(); |
404 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 413 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
405 | 414 |
406 if (!args.Length() || !args[0]->IsArray()) { | 415 if (!args.Length() || !args[0]->IsArray()) { |
407 v8SetReturnValue(args, promise); | 416 v8SetReturnValue(args, promise); |
408 return; | 417 return; |
409 } | 418 } |
410 | 419 |
411 // FIXME: Now we limit the iterable type to the Array type. | 420 // FIXME: Now we limit the iterable type to the Array type. |
412 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); | 421 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); |
413 v8::Local<v8::Function> fulfillCallback = createClosure(promiseResolveCallba ck, promise, isolate); | 422 v8::Local<v8::Function> onFulfilled = createClosure(promiseResolveCallback, promise, isolate); |
414 v8::Local<v8::Function> rejectCallback = createClosure(promiseRejectCallback , promise, isolate); | 423 v8::Local<v8::Function> onRejected = createClosure(promiseRejectCallback, pr omise, isolate); |
415 | 424 |
416 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { | 425 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { |
417 // Array-holes should not be skipped by for-of iteration semantics. | 426 // Array-holes should not be skipped by for-of iteration semantics. |
418 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); | 427 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); |
419 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); | 428 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); |
420 V8PromiseCustom::append(nextPromise, fulfillCallback, rejectCallback, is olate); | 429 V8PromiseCustom::then(nextPromise, onFulfilled, onRejected, isolate); |
421 } | 430 } |
422 v8SetReturnValue(args, promise); | 431 v8SetReturnValue(args, promise); |
423 } | 432 } |
424 | 433 |
425 void V8Promise::allMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) | 434 void V8Promise::allMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
426 { | 435 { |
427 v8::Isolate* isolate = args.GetIsolate(); | 436 v8::Isolate* isolate = args.GetIsolate(); |
428 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 437 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
429 v8::Local<v8::Array> results = v8::Array::New(); | 438 v8::Local<v8::Array> results = v8::Array::New(); |
430 | 439 |
431 if (!args.Length() || !args[0]->IsArray()) { | 440 if (!args.Length() || !args[0]->IsArray()) { |
432 V8PromiseCustom::resolve(promise, results, V8PromiseCustom::Asynchronous , isolate); | 441 V8PromiseCustom::resolve(promise, results, isolate); |
433 v8SetReturnValue(args, promise); | 442 v8SetReturnValue(args, promise); |
434 return; | 443 return; |
435 } | 444 } |
436 | 445 |
437 // FIXME: Now we limit the iterable type to the Array type. | 446 // FIXME: Now we limit the iterable type to the Array type. |
438 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); | 447 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); |
439 | 448 |
440 if (!iterable->Length()) { | 449 if (!iterable->Length()) { |
441 V8PromiseCustom::resolve(promise, results, V8PromiseCustom::Asynchronous , isolate); | 450 V8PromiseCustom::resolve(promise, results, isolate); |
442 v8SetReturnValue(args, promise); | 451 v8SetReturnValue(args, promise); |
443 return; | 452 return; |
444 } | 453 } |
445 | 454 |
446 v8::Local<v8::ObjectTemplate> objectTemplate = primitiveWrapperObjectTemplat e(isolate); | 455 v8::Local<v8::ObjectTemplate> objectTemplate = primitiveWrapperObjectTemplat e(isolate); |
447 v8::Local<v8::Object> countdownWrapper = objectTemplate->NewInstance(); | 456 v8::Local<v8::Object> countdownWrapper = objectTemplate->NewInstance(); |
448 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(iterable->Length(), isolate)); | 457 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(iterable->Length(), isolate)); |
449 | 458 |
450 v8::Local<v8::Function> rejectCallback = createClosure(promiseRejectCallback , promise, isolate); | 459 v8::Local<v8::Function> onRejected = createClosure(promiseRejectCallback, pr omise, isolate); |
451 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { | 460 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { |
452 // Array-holes should not be skipped by for-of iteration semantics. | 461 // Array-holes should not be skipped by for-of iteration semantics. |
453 v8::Local<v8::Object> environment = promiseAllEnvironment(promise, count downWrapper, i, results, isolate); | 462 v8::Local<v8::Object> environment = promiseAllEnvironment(promise, count downWrapper, i, results, isolate); |
454 v8::Local<v8::Function> fulfillCallback = createClosure(promiseAllFulfil lCallback, environment, isolate); | 463 v8::Local<v8::Function> onFulfilled = createClosure(promiseAllFulfillCal lback, environment, isolate); |
455 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); | 464 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); |
456 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); | 465 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); |
457 V8PromiseCustom::append(nextPromise, fulfillCallback, rejectCallback, is olate); | 466 V8PromiseCustom::then(nextPromise, onFulfilled, onRejected, isolate); |
458 } | 467 } |
459 v8SetReturnValue(args, promise); | 468 v8SetReturnValue(args, promise); |
460 } | 469 } |
461 | 470 |
462 // | 471 // |
463 // -- V8PromiseCustom -- | 472 // -- V8PromiseCustom -- |
464 v8::Local<v8::Object> V8PromiseCustom::createPromise(v8::Handle<v8::Object> crea tionContext, v8::Isolate* isolate) | 473 v8::Local<v8::Object> V8PromiseCustom::createPromise(v8::Handle<v8::Object> crea tionContext, v8::Isolate* isolate) |
465 { | 474 { |
466 v8::Local<v8::ObjectTemplate> internalTemplate = internalObjectTemplate(isol ate); | 475 v8::Local<v8::ObjectTemplate> internalTemplate = internalObjectTemplate(isol ate); |
467 v8::Local<v8::Object> internal = internalTemplate->NewInstance(); | 476 v8::Local<v8::Object> internal = internalTemplate->NewInstance(); |
468 v8::Local<v8::Object> promise = V8DOMWrapper::createWrapper(creationContext, &V8Promise::info, 0, isolate); | 477 v8::Local<v8::Object> promise = V8DOMWrapper::createWrapper(creationContext, &V8Promise::info, 0, isolate); |
469 | 478 |
470 clearInternal(internal, V8PromiseCustom::Pending, v8::Undefined(isolate), is olate); | 479 clearDerived(internal); |
480 setState(internal, Pending, v8::Undefined(isolate), isolate); | |
471 | 481 |
472 promise->SetInternalField(v8DOMWrapperObjectIndex, internal); | 482 promise->SetInternalField(v8DOMWrapperObjectIndex, internal); |
473 return promise; | 483 return promise; |
474 } | 484 } |
475 | 485 |
476 void V8PromiseCustom::fulfill(v8::Handle<v8::Object> promise, v8::Handle<v8::Val ue> result, SynchronousMode mode, v8::Isolate* isolate) | |
477 { | |
478 v8::Local<v8::Object> internal = getInternal(promise); | |
479 PromiseState state = getState(internal); | |
480 if (state == Fulfilled || state == Rejected) | |
481 return; | |
482 | |
483 ASSERT(state == Pending || state == Following); | |
484 v8::Local<v8::Array> callbacks = internal->GetInternalField(V8PromiseCustom: :InternalFulfillCallbackIndex).As<v8::Array>(); | |
485 clearInternal(internal, Fulfilled, result, isolate); | |
486 | |
487 callCallbacks(callbacks, result, mode, isolate); | |
488 } | |
489 | |
490 void V8PromiseCustom::resolve(v8::Handle<v8::Object> promise, v8::Handle<v8::Val ue> result, SynchronousMode mode, v8::Isolate* isolate) | |
491 { | |
492 ASSERT(!result.IsEmpty()); | |
493 | |
494 v8::Local<v8::Value> then; | |
495 if (result->IsObject()) { | |
496 v8::TryCatch trycatch; | |
497 then = result.As<v8::Object>()->Get(v8::String::NewSymbol("then")); | |
498 if (then.IsEmpty()) { | |
499 // If calling the [[Get]] internal method threw an exception, catch it and run reject. | |
500 reject(promise, trycatch.Exception(), mode, isolate); | |
501 return; | |
502 } | |
503 } | |
504 | |
505 if (!then.IsEmpty() && then->IsFunction()) { | |
506 ASSERT(result->IsObject()); | |
507 v8::TryCatch trycatch; | |
508 v8::Handle<v8::Value> argv[] = { | |
509 createClosure(promiseResolveCallback, promise, isolate), | |
510 createClosure(promiseRejectCallback, promise, isolate), | |
511 }; | |
512 if (V8ScriptRunner::callFunction(then.As<v8::Function>(), getScriptExecu tionContext(), result.As<v8::Object>(), WTF_ARRAY_LENGTH(argv), argv, isolate).I sEmpty()) | |
513 reject(promise, trycatch.Exception(), mode, isolate); | |
514 return; | |
515 } | |
516 | |
517 fulfill(promise, result, mode, isolate); | |
518 } | |
519 | |
520 void V8PromiseCustom::reject(v8::Handle<v8::Object> promise, v8::Handle<v8::Valu e> result, SynchronousMode mode, v8::Isolate* isolate) | |
521 { | |
522 v8::Local<v8::Object> internal = getInternal(promise); | |
523 PromiseState state = getState(internal); | |
524 if (state == Fulfilled || state == Rejected) | |
525 return; | |
526 | |
527 ASSERT(state == Pending || state == Following); | |
528 v8::Local<v8::Array> callbacks = internal->GetInternalField(V8PromiseCustom: :InternalRejectCallbackIndex).As<v8::Array>(); | |
529 clearInternal(internal, Rejected, result, isolate); | |
530 | |
531 callCallbacks(callbacks, result, mode, isolate); | |
532 } | |
533 | |
534 void V8PromiseCustom::append(v8::Handle<v8::Object> promise, v8::Handle<v8::Func tion> fulfillCallback, v8::Handle<v8::Function> rejectCallback, v8::Isolate* iso late) | |
535 { | |
536 // fulfillCallback and rejectCallback can be empty. | |
537 v8::Local<v8::Object> internal = getInternal(promise); | |
538 | |
539 PromiseState state = getState(internal); | |
540 if (state == Fulfilled) { | |
541 if (!fulfillCallback.IsEmpty()) { | |
542 v8::Local<v8::Value> result = internal->GetInternalField(V8PromiseCu stom::InternalResultIndex); | |
543 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global( ); | |
544 call(fulfillCallback, global, result, Asynchronous, isolate); | |
545 } | |
546 return; | |
547 } | |
548 if (state == Rejected) { | |
549 if (!rejectCallback.IsEmpty()) { | |
550 v8::Local<v8::Value> result = internal->GetInternalField(V8PromiseCu stom::InternalResultIndex); | |
551 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global( ); | |
552 call(rejectCallback, global, result, Asynchronous, isolate); | |
553 } | |
554 return; | |
555 } | |
556 | |
557 ASSERT(state == Pending || state == Following); | |
558 if (!fulfillCallback.IsEmpty()) { | |
559 v8::Local<v8::Array> callbacks = internal->GetInternalField(InternalFulf illCallbackIndex).As<v8::Array>(); | |
560 callbacks->Set(callbacks->Length(), fulfillCallback); | |
561 } | |
562 if (!rejectCallback.IsEmpty()) { | |
563 v8::Local<v8::Array> callbacks = internal->GetInternalField(InternalReje ctCallbackIndex).As<v8::Array>(); | |
564 callbacks->Set(callbacks->Length(), rejectCallback); | |
565 } | |
566 } | |
567 | |
568 v8::Local<v8::Object> V8PromiseCustom::getInternal(v8::Handle<v8::Object> promis e) | 486 v8::Local<v8::Object> V8PromiseCustom::getInternal(v8::Handle<v8::Object> promis e) |
569 { | 487 { |
570 v8::Local<v8::Value> value = promise->GetInternalField(v8DOMWrapperObjectInd ex); | 488 v8::Local<v8::Value> value = promise->GetInternalField(v8DOMWrapperObjectInd ex); |
571 return value.As<v8::Object>(); | 489 return value.As<v8::Object>(); |
572 } | 490 } |
573 | 491 |
574 void V8PromiseCustom::clearInternal(v8::Handle<v8::Object> internal, PromiseStat e state, v8::Handle<v8::Value> value, v8::Isolate* isolate) | |
575 { | |
576 setState(internal, state, isolate); | |
577 internal->SetInternalField(V8PromiseCustom::InternalResultIndex, value); | |
578 internal->SetInternalField(V8PromiseCustom::InternalFulfillCallbackIndex, v8 ::Array::New()); | |
579 internal->SetInternalField(V8PromiseCustom::InternalRejectCallbackIndex, v8: :Array::New()); | |
580 } | |
581 | |
582 V8PromiseCustom::PromiseState V8PromiseCustom::getState(v8::Handle<v8::Object> i nternal) | 492 V8PromiseCustom::PromiseState V8PromiseCustom::getState(v8::Handle<v8::Object> i nternal) |
583 { | 493 { |
584 v8::Handle<v8::Value> value = internal->GetInternalField(V8PromiseCustom::In ternalStateIndex); | 494 v8::Handle<v8::Value> value = internal->GetInternalField(V8PromiseCustom::In ternalStateIndex); |
585 bool ok = false; | 495 bool ok = false; |
586 uint32_t number = toInt32(value, ok); | 496 uint32_t number = toInt32(value, ok); |
587 ASSERT(ok && (number == Pending || number == Fulfilled || number == Rejected || number == Following)); | 497 ASSERT(ok && (number == Pending || number == Fulfilled || number == Rejected || number == Following)); |
588 return static_cast<PromiseState>(number); | 498 return static_cast<PromiseState>(number); |
589 } | 499 } |
590 | 500 |
591 void V8PromiseCustom::setState(v8::Handle<v8::Object> internal, PromiseState sta te, v8::Isolate* isolate) | 501 void V8PromiseCustom::setState(v8::Handle<v8::Object> internal, PromiseState sta te, v8::Handle<v8::Value> value, v8::Isolate* isolate) |
592 { | 502 { |
503 ASSERT(!result.IsEmpty()); | |
593 ASSERT(state == Pending || state == Fulfilled || state == Rejected || state == Following); | 504 ASSERT(state == Pending || state == Fulfilled || state == Rejected || state == Following); |
594 internal->SetInternalField(V8PromiseCustom::InternalStateIndex, v8::Integer: :New(state, isolate)); | 505 internal->SetInternalField(InternalStateIndex, v8::Integer::New(state, isola te)); |
595 } | 506 internal->SetInternalField(InternalResultIndex, value); |
596 | |
597 void V8PromiseCustom::call(v8::Handle<v8::Function> function, v8::Handle<v8::Obj ect> receiver, v8::Handle<v8::Value> result, SynchronousMode mode, v8::Isolate* isolate) | |
598 { | |
599 if (mode == Synchronous) { | |
600 v8::Context::Scope scope(isolate->GetCurrentContext()); | |
601 // If an exception is thrown, catch it and do nothing. | |
602 v8::TryCatch trycatch; | |
603 v8::Handle<v8::Value> args[] = { result }; | |
604 V8ScriptRunner::callFunction(function, getScriptExecutionContext(), rece iver, WTF_ARRAY_LENGTH(args), args, isolate); | |
605 } else { | |
606 ASSERT(mode == Asynchronous); | |
607 postTask(function, receiver, result, isolate); | |
608 } | |
609 } | 507 } |
610 | 508 |
611 bool V8PromiseCustom::isPromise(v8::Handle<v8::Value> maybePromise, v8::Isolate* isolate) | 509 bool V8PromiseCustom::isPromise(v8::Handle<v8::Value> maybePromise, v8::Isolate* isolate) |
612 { | 510 { |
613 WrapperWorldType currentWorldType = worldType(isolate); | 511 WrapperWorldType currentWorldType = worldType(isolate); |
614 return V8Promise::GetTemplate(isolate, currentWorldType)->HasInstance(maybeP romise); | 512 return V8Promise::GetTemplate(isolate, currentWorldType)->HasInstance(maybeP romise); |
615 } | 513 } |
616 | 514 |
617 v8::Local<v8::Object> V8PromiseCustom::toPromise(v8::Handle<v8::Value> maybeProm ise, v8::Isolate* isolate) | 515 v8::Local<v8::Object> V8PromiseCustom::toPromise(v8::Handle<v8::Value> maybeProm ise, v8::Isolate* isolate) |
618 { | 516 { |
619 // FIXME: Currently we dones't check [[PromiseConstructor]] since we limits | 517 // FIXME: Currently we dones't check [[PromiseConstructor]] since we limits |
620 // the creation of the promise objects only from the Blink Promise | 518 // the creation of the promise objects only from the Blink Promise |
621 // constructor. | 519 // constructor. |
622 if (isPromise(maybePromise, isolate)) | 520 if (isPromise(maybePromise, isolate)) |
623 return maybePromise.As<v8::Object>(); | 521 return maybePromise.As<v8::Object>(); |
624 | 522 |
625 v8::Local<v8::Object> promise = createPromise(v8::Handle<v8::Object>(), isol ate); | 523 v8::Local<v8::Object> promise = createPromise(v8::Handle<v8::Object>(), isol ate); |
626 resolve(promise, maybePromise, Asynchronous, isolate); | 524 resolve(promise, maybePromise, isolate); |
627 return promise; | 525 return promise; |
628 } | 526 } |
629 | 527 |
528 void V8PromiseCustom::resolve(v8::Handle<v8::Object> promise, v8::Handle<v8::Val ue> result, v8::Isolate* isolate) | |
529 { | |
530 ASSERT(!result.IsEmpty()); | |
531 v8::Local<v8::Object> internal = getInternal(promise); | |
532 PromiseState state = getState(internal); | |
533 if (state != Pending) | |
534 return; | |
535 | |
536 if (isPromise(result, isolate)) { | |
537 v8::Local<v8::Object> valuePromise = result.As<v8::Object>(); | |
538 v8::Local<v8::Object> valueInternal = getInternal(valuePromise); | |
539 PromiseState valueState = getState(valueInternal); | |
540 if (promise->SameValue(valuePromise)) { | |
yhirano
2013/10/01 08:12:24
We should have a test checking this behavior.
yusukesuzuki
2013/10/02 00:20:56
Done.
| |
541 v8::Local<v8::Value> reason = V8ThrowException::createTypeError("Res olve a promise with itself", isolate); | |
542 setReason(promise, reason, isolate); | |
543 } else if (valueState == Following) { | |
544 v8::Local<v8::Object> valuePromiseFollowing = valueInternal->GetInte rnalField(InternalResultIndex).As<v8::Object>(); | |
545 setState(internal, Following, valuePromiseFollowing, isolate); | |
546 addToDerived(getInternal(valuePromiseFollowing), promise, v8::Handle <v8::Function>(), v8::Handle<v8::Function>(), isolate); | |
547 } else if (valueState == Fulfilled) { | |
548 setValue(promise, valueInternal->GetInternalField(InternalResultInde x), isolate); | |
549 } else if (valueState == Rejected) { | |
550 setReason(promise, valueInternal->GetInternalField(InternalResultInd ex), isolate); | |
551 } else { | |
552 ASSERT(valueState == Pending); | |
553 setState(internal, Following, valuePromise, isolate); | |
554 addToDerived(valueInternal, promise, v8::Handle<v8::Function>(), v8: :Handle<v8::Function>(), isolate); | |
555 } | |
556 } else { | |
557 setValue(promise, result, isolate); | |
558 } | |
559 } | |
560 | |
561 void V8PromiseCustom::reject(v8::Handle<v8::Object> promise, v8::Handle<v8::Valu e> reason, v8::Isolate* isolate) | |
562 { | |
563 v8::Local<v8::Object> internal = getInternal(promise); | |
564 PromiseState state = getState(internal); | |
565 if (state != Pending) | |
566 return; | |
567 setReason(promise, reason, isolate); | |
568 } | |
569 | |
570 v8::Local<v8::Object> V8PromiseCustom::then(v8::Handle<v8::Object> promise, v8:: Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Isola te* isolate) | |
571 { | |
572 v8::Handle<v8::Object> internal = getInternal(promise); | |
573 if (getState(internal) == Following) | |
574 return then(internal->GetInternalField(InternalResultIndex).As<v8::Objec t>(), onFulfilled, onRejected, isolate); | |
yhirano
2013/10/01 08:12:24
Doesn't this function call cause a stack overflow?
yusukesuzuki
2013/10/02 00:20:56
Done. Fixed. Lookup the promise by while statement
| |
575 // FIXME: Currently we dones't lookup "constructor" property since we limits | |
576 // the creation of the promise objects only from the Blink Promise | |
577 // constructor. | |
578 v8::Local<v8::Object> derivedPromise = createPromise(v8::Handle<v8::Object>( ), isolate); | |
579 updateDerivedFromPromise(derivedPromise, onFulfilled, onRejected, promise, i solate); | |
580 return derivedPromise; | |
581 } | |
582 | |
583 void V8PromiseCustom::setValue(v8::Handle<v8::Object> promise, v8::Handle<v8::Va lue> value, v8::Isolate* isolate) | |
584 { | |
585 v8::Local<v8::Object> internal = getInternal(promise); | |
586 ASSERT(getState(internal) != Fulfilled && getState(internal) != Rejected); | |
587 setState(internal, Fulfilled, value, isolate); | |
588 propagateToDerived(promise, isolate); | |
589 } | |
590 | |
591 void V8PromiseCustom::setReason(v8::Handle<v8::Object> promise, v8::Handle<v8::V alue> reason, v8::Isolate* isolate) | |
592 { | |
593 v8::Local<v8::Object> internal = getInternal(promise); | |
594 ASSERT(getState(internal) != Fulfilled && getState(internal) != Rejected); | |
595 setState(internal, Rejected, reason, isolate); | |
596 propagateToDerived(promise, isolate); | |
597 } | |
598 | |
599 void V8PromiseCustom::propagateToDerived(v8::Handle<v8::Object> promise, v8::Iso late* isolate) | |
600 { | |
601 v8::Local<v8::Object> internal = getInternal(promise); | |
602 ASSERT(getState(internal) == Fulfilled || getState(internal) == Rejected); | |
603 v8::Local<v8::Array> fulfillCallbacks = internal->GetInternalField(InternalF ulfillCallbackIndex).As<v8::Array>(); | |
604 v8::Local<v8::Array> rejectCallbacks = internal->GetInternalField(InternalRe jectCallbackIndex).As<v8::Array>(); | |
605 v8::Local<v8::Array> derivedPromises = internal->GetInternalField(InternalDe rivedPromiseIndex).As<v8::Array>(); | |
yhirano
2013/10/01 08:12:24
Let's assert that all arrays are of the same lengt
yusukesuzuki
2013/10/02 00:20:56
Done in PromisePropagator::propagateToDerived.
| |
606 for (uint32_t i = 0, length = derivedPromises->Length(); i < length; ++i) { | |
607 v8::Local<v8::Object> derivedPromise = derivedPromises->Get(i).As<v8::Ob ject>(); | |
608 | |
609 v8::Local<v8::Function> onFulfilled, onRejected; | |
610 v8::Local<v8::Value> onFulfilledValue = fulfillCallbacks->Get(i); | |
611 if (onFulfilledValue->IsFunction()) { | |
612 onFulfilled = onFulfilledValue.As<v8::Function>(); | |
613 } | |
614 v8::Local<v8::Value> onRejectedValue = rejectCallbacks->Get(i); | |
615 if (onRejectedValue->IsFunction()) { | |
616 onRejected = onRejectedValue.As<v8::Function>(); | |
617 } | |
618 | |
619 updateDerived(derivedPromise, onFulfilled, onRejected, promise, isolate) ; | |
620 } | |
621 clearDerived(internal); | |
622 } | |
623 | |
624 void V8PromiseCustom::updateDerived(v8::Handle<v8::Object> derivedPromise, v8::H andle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Handle <v8::Object> originator, v8::Isolate* isolate) | |
yhirano
2013/10/01 08:12:24
I think stack overflow is introduced, such as:
U
yusukesuzuki
2013/10/02 00:20:56
Right.
So I've introduced PromisePropagator. It ho
| |
625 { | |
626 v8::Local<v8::Object> originatorInternal = getInternal(originator); | |
627 ASSERT(getState(originatorInternal) == Fulfilled || getState(originatorInter nal) == Rejected); | |
628 PromiseState originatorState = getState(originatorInternal); | |
629 v8::Local<v8::Value> originatorValue = originatorInternal->GetInternalField( InternalResultIndex); | |
630 if (originatorState == Fulfilled) { | |
631 if (originatorValue->IsObject()) { | |
632 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionC ontext(); | |
633 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextTh read()); | |
634 scriptExecutionContext->postTask(adoptPtr(new UpdateDerivedTask(deri vedPromise, onFulfilled, onRejected, originatorValue.As<v8::Object>(), isolate)) ); | |
635 } else { | |
636 updateDerivedFromValue(derivedPromise, onFulfilled, originatorValue, isolate); | |
637 } | |
638 } else { | |
639 updateDerivedFromReason(derivedPromise, onRejected, originatorValue, iso late); | |
640 } | |
641 } | |
642 | |
643 void V8PromiseCustom::updateDerivedFromValue(v8::Handle<v8::Object> derivedPromi se, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Value> value, v8::Isola te* isolate) | |
644 { | |
645 if (!onFulfilled.IsEmpty()) { | |
646 callHandler(derivedPromise, onFulfilled, value, isolate); | |
647 } else { | |
648 setValue(derivedPromise, value, isolate); | |
649 } | |
650 } | |
651 | |
652 void V8PromiseCustom::updateDerivedFromReason(v8::Handle<v8::Object> derivedProm ise, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Value> reason, v8::Isol ate* isolate) | |
653 { | |
654 if (!onRejected.IsEmpty()) { | |
655 callHandler(derivedPromise, onRejected, reason, isolate); | |
656 } else { | |
657 setReason(derivedPromise, reason, isolate); | |
658 } | |
659 } | |
660 | |
661 void V8PromiseCustom::updateDerivedFromPromise(v8::Handle<v8::Object> derivedPro mise, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Object> promise, v8::Isolate* isolate) | |
662 { | |
663 v8::Local<v8::Object> internal = getInternal(promise); | |
664 PromiseState state = getState(internal); | |
665 if (state == Fulfilled || state == Rejected) { | |
666 updateDerived(derivedPromise, onFulfilled, onRejected, promise, isolate) ; | |
667 } else { | |
668 addToDerived(internal, derivedPromise, onFulfilled, onRejected, isolate) ; | |
669 } | |
670 } | |
671 | |
672 v8::Local<v8::Object> V8PromiseCustom::coerceThenable(v8::Handle<v8::Object> the nable, v8::Handle<v8::Function> then, v8::Isolate* isolate) | |
673 { | |
674 ASSERT(!thenable.IsEmpty()); | |
675 ASSERT(!then.IsEmpty()); | |
676 v8::Local<v8::Object> promise = createPromise(v8::Handle<v8::Object>(), isol ate); | |
677 v8::Handle<v8::Value> argv[] = { | |
678 createClosure(promiseResolveCallback, promise, isolate), | |
679 createClosure(promiseRejectCallback, promise, isolate) | |
680 }; | |
681 v8::TryCatch trycatch; | |
682 if (V8ScriptRunner::callFunction(then, getScriptExecutionContext(), thenable , WTF_ARRAY_LENGTH(argv), argv, isolate).IsEmpty()) { | |
683 reject(promise, trycatch.Exception(), isolate); | |
684 } | |
685 thenable->SetHiddenValue(V8HiddenPropertyName::thenableHiddenPromise(isolate ), promise); | |
686 return promise; | |
687 } | |
688 | |
689 void V8PromiseCustom::callHandler(v8::Handle<v8::Object> promise, v8::Handle<v8: :Function> handler, v8::Handle<v8::Value> argument, v8::Isolate* isolate) | |
690 { | |
691 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext() ; | |
692 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextThread()); | |
693 scriptExecutionContext->postTask(adoptPtr(new CallHandlerTask(promise, handl er, argument, isolate))); | |
694 } | |
695 | |
630 } // namespace WebCore | 696 } // namespace WebCore |
OLD | NEW |