Chromium Code Reviews| 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 v8::Local<v8::Value> value = V8ScriptRunner::callFunction(m_handler.newLocal (isolate), context, v8::Undefined(isolate), WTF_ARRAY_LENGTH(args), args, isolat e); | |
|
yusukesuzuki
2013/10/01 03:44:03
Since V8-side change[1] and Blink-side change[2] a
| |
| 232 if (value.IsEmpty()) { | |
| 233 V8PromiseCustom::reject(m_promise.newLocal(isolate), trycatch.Exception( ), isolate); | |
| 234 } else { | |
| 235 V8PromiseCustom::resolve(m_promise.newLocal(isolate), value, isolate); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 class UpdateDerivedTask : public ScriptExecutionContext::Task { | |
| 240 public: | |
| 241 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) | |
| 242 : m_promise(isolate, promise) | |
| 243 , m_onFulfilled(isolate, onFulfilled) | |
| 244 , m_onRejected(isolate, onRejected) | |
| 245 , m_originatorObject(isolate, originatorObject) | |
| 246 { | |
| 247 ASSERT(!m_promise.isEmpty()); | |
| 248 ASSERT(!m_onFulfilled.isEmpty()); | |
| 249 ASSERT(!m_onRejected.isEmpty()); | |
| 250 ASSERT(!m_originatorObject.isEmpty()); | |
| 251 } | |
| 252 virtual ~UpdateDerivedTask() { } | |
| 253 | |
| 254 virtual void performTask(ScriptExecutionContext*) OVERRIDE; | |
| 255 | |
| 256 private: | |
| 257 ScopedPersistent<v8::Object> m_promise; | |
| 258 ScopedPersistent<v8::Function> m_onFulfilled; | |
| 259 ScopedPersistent<v8::Function> m_onRejected; | |
| 260 ScopedPersistent<v8::Object> m_originatorObject; | |
| 261 }; | |
| 262 | |
| 263 void UpdateDerivedTask::performTask(ScriptExecutionContext* context) | |
| 264 { | |
| 265 ASSERT(context); | |
| 266 if (context->activeDOMObjectsAreStopped()) | |
| 267 return; | |
| 268 | |
| 269 ScriptState* state = 0; | |
| 270 if (context->isDocument()) { | |
| 271 state = mainWorldScriptState(static_cast<Document*>(context)->frame()); | |
| 272 } else { | |
| 273 ASSERT(context->isWorkerGlobalScope()); | |
| 274 state = scriptStateFromWorkerGlobalScope(toWorkerGlobalScope(context)); | |
| 275 } | |
| 276 ASSERT(state); | |
| 277 | |
| 278 v8::Isolate* isolate = state->isolate(); | |
| 279 v8::HandleScope handleScope(isolate); | |
| 280 v8::Handle<v8::Context> v8Context = state->context(); | |
| 281 v8::Context::Scope scope(v8Context); | |
| 282 | |
| 283 v8::Local<v8::Object> originatorObject = m_originatorObject.newLocal(isolate ); | |
| 284 v8::Local<v8::Value> coercedAlready = originatorObject->GetHiddenValue(V8Hid denPropertyName::thenableHiddenPromise(isolate)); | |
| 285 if (!coercedAlready.IsEmpty() && coercedAlready->IsObject()) { | |
| 286 ASSERT(V8PromiseCustom::isPromise(coercedAlready.As<v8::Object>(), isola te)); | |
| 287 V8PromiseCustom::updateDerivedFromPromise(m_promise.newLocal(isolate), m _onFulfilled.newLocal(isolate), m_onRejected.newLocal(isolate), coercedAlready.A s<v8::Object>(), isolate); | |
| 288 return; | |
| 289 } | |
| 290 | |
| 291 v8::Local<v8::Value> then; | |
| 292 v8::TryCatch trycatch; | |
| 293 then = originatorObject->Get(v8::String::NewSymbol("then")); | |
| 294 if (then.IsEmpty()) { | |
| 295 // If calling the [[Get]] internal method threw an exception, catch it a nd run updateDerivedFromReason. | |
| 296 V8PromiseCustom::updateDerivedFromReason(m_promise.newLocal(isolate), m_ onRejected.newLocal(isolate), trycatch.Exception(), isolate); | |
| 297 return; | |
| 298 } | |
| 299 | |
| 300 if (then->IsFunction()) { | |
| 301 ASSERT(then->IsObject()); | |
| 302 v8::Local<v8::Object> coerced = V8PromiseCustom::coerceThenable(originat orObject, then.As<v8::Function>(), isolate); | |
| 303 V8PromiseCustom::updateDerivedFromPromise(m_promise.newLocal(isolate), m _onFulfilled.newLocal(isolate), m_onRejected.newLocal(isolate), coerced, isolate ); | |
| 304 return; | |
| 305 } | |
| 306 | |
| 307 V8PromiseCustom::updateDerivedFromValue(m_promise.newLocal(isolate), m_onFul filled.newLocal(isolate), originatorObject, isolate); | |
| 292 } | 308 } |
| 293 | 309 |
| 294 } // namespace | 310 } // namespace |
| 295 | 311 |
| 296 void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) | 312 void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) |
| 297 { | 313 { |
| 298 v8SetReturnValue(args, v8::Local<v8::Value>()); | 314 v8SetReturnValue(args, v8::Local<v8::Value>()); |
| 299 v8::Isolate* isolate = args.GetIsolate(); | 315 v8::Isolate* isolate = args.GetIsolate(); |
| 300 if (!args.Length() || !args[0]->IsFunction()) { | 316 if (!args.Length() || !args[0]->IsFunction()) { |
| 301 throwTypeError("Promise constructor takes a function argument", isolate) ; | 317 throwTypeError("Promise constructor takes a function argument", isolate) ; |
| 302 return; | 318 return; |
| 303 } | 319 } |
| 304 v8::Local<v8::Function> init = args[0].As<v8::Function>(); | 320 v8::Local<v8::Function> init = args[0].As<v8::Function>(); |
| 305 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 321 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 306 v8::Handle<v8::Value> argv[] = { | 322 v8::Handle<v8::Value> argv[] = { |
| 307 createClosure(promiseResolve, promise, isolate), | 323 createClosure(promiseResolveCallback, promise, isolate), |
| 308 createClosure(promiseReject, promise, isolate) | 324 createClosure(promiseRejectCallback, promise, isolate) |
| 309 }; | 325 }; |
| 310 v8::TryCatch trycatch; | 326 v8::TryCatch trycatch; |
| 311 if (V8ScriptRunner::callFunction(init, getScriptExecutionContext(), promise, WTF_ARRAY_LENGTH(argv), argv, isolate).IsEmpty()) { | 327 if (V8ScriptRunner::callFunction(init, getScriptExecutionContext(), v8::Unde fined(isolate), WTF_ARRAY_LENGTH(argv), argv, isolate).IsEmpty()) { |
| 312 // An exception is thrown. Reject the promise if its resolved flag is un set. | 328 // An exception is thrown. Reject the promise if its resolved flag is un set. |
| 313 if (V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise)) == V8PromiseCustom::Pending) | 329 V8PromiseCustom::reject(promise, trycatch.Exception(), isolate); |
| 314 V8PromiseCustom::reject(promise, trycatch.Exception(), V8PromiseCust om::Asynchronous, isolate); | |
| 315 } | 330 } |
| 316 v8SetReturnValue(args, promise); | 331 v8SetReturnValue(args, promise); |
| 317 return; | 332 return; |
| 318 } | 333 } |
| 319 | 334 |
| 320 void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) | 335 void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) |
| 321 { | 336 { |
| 322 v8::Isolate* isolate = args.GetIsolate(); | 337 v8::Isolate* isolate = args.GetIsolate(); |
| 323 v8::Local<v8::Function> fulfillWrapper, rejectWrapper; | 338 v8::Local<v8::Function> onFulfilled, onRejected; |
| 324 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 339 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 325 if (args.Length() > 0 && !args[0]->IsUndefined()) { | 340 if (args.Length() > 0 && !args[0]->IsUndefined()) { |
| 326 if (!args[0]->IsFunction()) { | 341 if (!args[0]->IsFunction()) { |
| 327 v8SetReturnValue(args, throwTypeError("fulfillCallback must be a fun ction or undefined", isolate)); | 342 v8SetReturnValue(args, throwTypeError("onFulfilled must be a functio n or undefined", isolate)); |
| 328 return; | 343 return; |
| 329 } | 344 } |
| 330 fulfillWrapper = createClosure(wrapperCallback, wrapperCallbackEnvironme nt(promise, args[0].As<v8::Function>(), isolate), isolate); | 345 onFulfilled = args[0].As<v8::Function>(); |
| 331 } else { | |
| 332 fulfillWrapper = createClosure(promiseFulfillCallback, promise, isolate) ; | |
| 333 } | 346 } |
| 334 if (args.Length() > 1 && !args[1]->IsUndefined()) { | 347 if (args.Length() > 1 && !args[1]->IsUndefined()) { |
| 335 if (!args[1]->IsFunction()) { | 348 if (!args[1]->IsFunction()) { |
| 336 v8SetReturnValue(args, throwTypeError("rejectCallback must be a func tion or undefined", isolate)); | 349 v8SetReturnValue(args, throwTypeError("onRejected must be a function or undefined", isolate)); |
| 337 return; | 350 return; |
| 338 } | 351 } |
| 339 rejectWrapper = createClosure(wrapperCallback, wrapperCallbackEnvironmen t(promise, args[1].As<v8::Function>(), isolate), isolate); | 352 onRejected = args[1].As<v8::Function>(); |
| 340 } else { | |
| 341 rejectWrapper = createClosure(promiseRejectCallback, promise, isolate); | |
| 342 } | 353 } |
| 343 V8PromiseCustom::append(args.Holder(), fulfillWrapper, rejectWrapper, isolat e); | 354 v8SetReturnValue(args, V8PromiseCustom::then(args.Holder(), onFulfilled, onR ejected, isolate)); |
| 344 v8SetReturnValue(args, promise); | |
| 345 } | 355 } |
| 346 | 356 |
| 347 void V8Promise::castMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) | 357 void V8Promise::castMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) |
| 348 { | 358 { |
| 349 v8::Isolate* isolate = args.GetIsolate(); | 359 v8::Isolate* isolate = args.GetIsolate(); |
| 350 v8::Local<v8::Value> result = v8::Undefined(isolate); | 360 v8::Local<v8::Value> result = v8::Undefined(isolate); |
| 351 if (args.Length() > 0) | 361 if (args.Length() > 0) |
| 352 result = args[0]; | 362 result = args[0]; |
| 353 | 363 |
| 354 v8SetReturnValue(args, V8PromiseCustom::toPromise(result, isolate)); | 364 v8SetReturnValue(args, V8PromiseCustom::toPromise(result, isolate)); |
| 355 } | 365 } |
| 356 | 366 |
| 357 void V8Promise::catchMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) | 367 void V8Promise::catchMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) |
| 358 { | 368 { |
| 359 v8::Isolate* isolate = args.GetIsolate(); | 369 v8::Isolate* isolate = args.GetIsolate(); |
| 360 v8::Local<v8::Function> fulfillWrapper, rejectWrapper; | 370 v8::Local<v8::Function> onFulfilled, onRejected; |
| 361 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | |
| 362 | 371 |
| 363 if (args.Length() > 0 && !args[0]->IsUndefined()) { | 372 if (args.Length() > 0 && !args[0]->IsUndefined()) { |
| 364 if (!args[0]->IsFunction()) { | 373 if (!args[0]->IsFunction()) { |
| 365 v8SetReturnValue(args, throwTypeError("rejectCallback must be a func tion or undefined", isolate)); | 374 v8SetReturnValue(args, throwTypeError("onRejected must be a function or undefined", isolate)); |
| 366 return; | 375 return; |
| 367 } | 376 } |
| 368 rejectWrapper = createClosure(wrapperCallback, wrapperCallbackEnvironmen t(promise, args[0].As<v8::Function>(), isolate), isolate); | 377 onRejected = args[0].As<v8::Function>(); |
| 369 } else { | |
| 370 rejectWrapper = createClosure(promiseRejectCallback, promise, isolate); | |
| 371 } | 378 } |
| 372 fulfillWrapper = createClosure(promiseFulfillCallback, promise, isolate); | 379 v8SetReturnValue(args, V8PromiseCustom::then(args.Holder(), onFulfilled, onR ejected, isolate)); |
| 373 V8PromiseCustom::append(args.Holder(), fulfillWrapper, rejectWrapper, isolat e); | |
| 374 v8SetReturnValue(args, promise); | |
| 375 } | 380 } |
| 376 | 381 |
| 377 void V8Promise::resolveMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& a rgs) | 382 void V8Promise::resolveMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& a rgs) |
| 378 { | 383 { |
| 379 v8::Isolate* isolate = args.GetIsolate(); | 384 v8::Isolate* isolate = args.GetIsolate(); |
| 380 v8::Local<v8::Value> result = v8::Undefined(isolate); | 385 v8::Local<v8::Value> result = v8::Undefined(isolate); |
| 381 if (args.Length() > 0) | 386 if (args.Length() > 0) |
| 382 result = args[0]; | 387 result = args[0]; |
| 383 | 388 |
| 384 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 389 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 385 V8PromiseCustom::resolve(promise, result, V8PromiseCustom::Asynchronous, iso late); | 390 V8PromiseCustom::resolve(promise, result, isolate); |
| 386 v8SetReturnValue(args, promise); | 391 v8SetReturnValue(args, promise); |
| 387 } | 392 } |
| 388 | 393 |
| 389 void V8Promise::rejectMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& ar gs) | 394 void V8Promise::rejectMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& ar gs) |
| 390 { | 395 { |
| 391 v8::Isolate* isolate = args.GetIsolate(); | 396 v8::Isolate* isolate = args.GetIsolate(); |
| 392 v8::Local<v8::Value> result = v8::Undefined(isolate); | 397 v8::Local<v8::Value> result = v8::Undefined(isolate); |
| 393 if (args.Length() > 0) | 398 if (args.Length() > 0) |
| 394 result = args[0]; | 399 result = args[0]; |
| 395 | 400 |
| 396 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 401 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 397 V8PromiseCustom::reject(promise, result, V8PromiseCustom::Asynchronous, isol ate); | 402 V8PromiseCustom::reject(promise, result, isolate); |
| 398 v8SetReturnValue(args, promise); | 403 v8SetReturnValue(args, promise); |
| 399 } | 404 } |
| 400 | 405 |
| 401 void V8Promise::raceMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) | 406 void V8Promise::raceMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) |
| 402 { | 407 { |
| 403 v8::Isolate* isolate = args.GetIsolate(); | 408 v8::Isolate* isolate = args.GetIsolate(); |
| 404 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 409 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 405 | 410 |
| 406 if (!args.Length() || !args[0]->IsArray()) { | 411 if (!args.Length() || !args[0]->IsArray()) { |
| 407 v8SetReturnValue(args, promise); | 412 v8SetReturnValue(args, promise); |
| 408 return; | 413 return; |
| 409 } | 414 } |
| 410 | 415 |
| 411 // FIXME: Now we limit the iterable type to the Array type. | 416 // FIXME: Now we limit the iterable type to the Array type. |
| 412 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); | 417 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); |
| 413 v8::Local<v8::Function> fulfillCallback = createClosure(promiseResolveCallba ck, promise, isolate); | 418 v8::Local<v8::Function> onFulfilled = createClosure(promiseResolveCallback, promise, isolate); |
| 414 v8::Local<v8::Function> rejectCallback = createClosure(promiseRejectCallback , promise, isolate); | 419 v8::Local<v8::Function> onRejected = createClosure(promiseRejectCallback, pr omise, isolate); |
| 415 | 420 |
| 416 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { | 421 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { |
| 417 // Array-holes should not be skipped by for-of iteration semantics. | 422 // Array-holes should not be skipped by for-of iteration semantics. |
| 418 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); | 423 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); |
| 419 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); | 424 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); |
| 420 V8PromiseCustom::append(nextPromise, fulfillCallback, rejectCallback, is olate); | 425 V8PromiseCustom::then(nextPromise, onFulfilled, onRejected, isolate); |
| 421 } | 426 } |
| 422 v8SetReturnValue(args, promise); | 427 v8SetReturnValue(args, promise); |
| 423 } | 428 } |
| 424 | 429 |
| 425 void V8Promise::allMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) | 430 void V8Promise::allMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
| 426 { | 431 { |
| 427 v8::Isolate* isolate = args.GetIsolate(); | 432 v8::Isolate* isolate = args.GetIsolate(); |
| 428 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 433 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 429 v8::Local<v8::Array> results = v8::Array::New(); | 434 v8::Local<v8::Array> results = v8::Array::New(); |
| 430 | 435 |
| 431 if (!args.Length() || !args[0]->IsArray()) { | 436 if (!args.Length() || !args[0]->IsArray()) { |
| 432 V8PromiseCustom::resolve(promise, results, V8PromiseCustom::Asynchronous , isolate); | 437 V8PromiseCustom::resolve(promise, results, isolate); |
| 433 v8SetReturnValue(args, promise); | 438 v8SetReturnValue(args, promise); |
| 434 return; | 439 return; |
| 435 } | 440 } |
| 436 | 441 |
| 437 // FIXME: Now we limit the iterable type to the Array type. | 442 // FIXME: Now we limit the iterable type to the Array type. |
| 438 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); | 443 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); |
| 439 | 444 |
| 440 if (!iterable->Length()) { | 445 if (!iterable->Length()) { |
| 441 V8PromiseCustom::resolve(promise, results, V8PromiseCustom::Asynchronous , isolate); | 446 V8PromiseCustom::resolve(promise, results, isolate); |
| 442 v8SetReturnValue(args, promise); | 447 v8SetReturnValue(args, promise); |
| 443 return; | 448 return; |
| 444 } | 449 } |
| 445 | 450 |
| 446 v8::Local<v8::ObjectTemplate> objectTemplate = primitiveWrapperObjectTemplat e(isolate); | 451 v8::Local<v8::ObjectTemplate> objectTemplate = primitiveWrapperObjectTemplat e(isolate); |
| 447 v8::Local<v8::Object> countdownWrapper = objectTemplate->NewInstance(); | 452 v8::Local<v8::Object> countdownWrapper = objectTemplate->NewInstance(); |
| 448 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(iterable->Length(), isolate)); | 453 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(iterable->Length(), isolate)); |
| 449 | 454 |
| 450 v8::Local<v8::Function> rejectCallback = createClosure(promiseRejectCallback , promise, isolate); | 455 v8::Local<v8::Function> onRejected = createClosure(promiseRejectCallback, pr omise, isolate); |
| 451 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { | 456 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { |
| 452 // Array-holes should not be skipped by for-of iteration semantics. | 457 // Array-holes should not be skipped by for-of iteration semantics. |
| 453 v8::Local<v8::Object> environment = promiseAllEnvironment(promise, count downWrapper, i, results, isolate); | 458 v8::Local<v8::Object> environment = promiseAllEnvironment(promise, count downWrapper, i, results, isolate); |
| 454 v8::Local<v8::Function> fulfillCallback = createClosure(promiseAllFulfil lCallback, environment, isolate); | 459 v8::Local<v8::Function> onFulfilled = createClosure(promiseAllFulfillCal lback, environment, isolate); |
| 455 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); | 460 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); |
| 456 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); | 461 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); |
| 457 V8PromiseCustom::append(nextPromise, fulfillCallback, rejectCallback, is olate); | 462 V8PromiseCustom::then(nextPromise, onFulfilled, onRejected, isolate); |
| 458 } | 463 } |
| 459 v8SetReturnValue(args, promise); | 464 v8SetReturnValue(args, promise); |
| 460 } | 465 } |
| 461 | 466 |
| 462 // | 467 // |
| 463 // -- V8PromiseCustom -- | 468 // -- V8PromiseCustom -- |
| 464 v8::Local<v8::Object> V8PromiseCustom::createPromise(v8::Handle<v8::Object> crea tionContext, v8::Isolate* isolate) | 469 v8::Local<v8::Object> V8PromiseCustom::createPromise(v8::Handle<v8::Object> crea tionContext, v8::Isolate* isolate) |
| 465 { | 470 { |
| 466 v8::Local<v8::ObjectTemplate> internalTemplate = internalObjectTemplate(isol ate); | 471 v8::Local<v8::ObjectTemplate> internalTemplate = internalObjectTemplate(isol ate); |
| 467 v8::Local<v8::Object> internal = internalTemplate->NewInstance(); | 472 v8::Local<v8::Object> internal = internalTemplate->NewInstance(); |
| 468 v8::Local<v8::Object> promise = V8DOMWrapper::createWrapper(creationContext, &V8Promise::info, 0, isolate); | 473 v8::Local<v8::Object> promise = V8DOMWrapper::createWrapper(creationContext, &V8Promise::info, 0, isolate); |
| 469 | 474 |
| 470 clearInternal(internal, V8PromiseCustom::Pending, v8::Undefined(isolate), is olate); | 475 clearDerived(internal); |
| 476 setState(internal, Pending, v8::Undefined(isolate), isolate); | |
| 471 | 477 |
| 472 promise->SetInternalField(v8DOMWrapperObjectIndex, internal); | 478 promise->SetInternalField(v8DOMWrapperObjectIndex, internal); |
| 473 return promise; | 479 return promise; |
| 474 } | 480 } |
| 475 | 481 |
| 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) | 482 v8::Local<v8::Object> V8PromiseCustom::getInternal(v8::Handle<v8::Object> promis e) |
| 569 { | 483 { |
| 570 v8::Local<v8::Value> value = promise->GetInternalField(v8DOMWrapperObjectInd ex); | 484 v8::Local<v8::Value> value = promise->GetInternalField(v8DOMWrapperObjectInd ex); |
| 571 return value.As<v8::Object>(); | 485 return value.As<v8::Object>(); |
| 572 } | 486 } |
| 573 | 487 |
| 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) | 488 V8PromiseCustom::PromiseState V8PromiseCustom::getState(v8::Handle<v8::Object> i nternal) |
| 583 { | 489 { |
| 584 v8::Handle<v8::Value> value = internal->GetInternalField(V8PromiseCustom::In ternalStateIndex); | 490 v8::Handle<v8::Value> value = internal->GetInternalField(V8PromiseCustom::In ternalStateIndex); |
| 585 bool ok = false; | 491 bool ok = false; |
| 586 uint32_t number = toInt32(value, ok); | 492 uint32_t number = toInt32(value, ok); |
| 587 ASSERT(ok && (number == Pending || number == Fulfilled || number == Rejected || number == Following)); | 493 ASSERT(ok && (number == Pending || number == Fulfilled || number == Rejected || number == Following)); |
| 588 return static_cast<PromiseState>(number); | 494 return static_cast<PromiseState>(number); |
| 589 } | 495 } |
| 590 | 496 |
| 591 void V8PromiseCustom::setState(v8::Handle<v8::Object> internal, PromiseState sta te, v8::Isolate* isolate) | 497 void V8PromiseCustom::setState(v8::Handle<v8::Object> internal, PromiseState sta te, v8::Handle<v8::Value> value, v8::Isolate* isolate) |
| 592 { | 498 { |
| 499 ASSERT(!value.IsEmpty()); | |
| 593 ASSERT(state == Pending || state == Fulfilled || state == Rejected || state == Following); | 500 ASSERT(state == Pending || state == Fulfilled || state == Rejected || state == Following); |
| 594 internal->SetInternalField(V8PromiseCustom::InternalStateIndex, v8::Integer: :New(state, isolate)); | 501 internal->SetInternalField(InternalStateIndex, v8::Integer::New(state, isola te)); |
| 595 } | 502 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 } | 503 } |
| 610 | 504 |
| 611 bool V8PromiseCustom::isPromise(v8::Handle<v8::Value> maybePromise, v8::Isolate* isolate) | 505 bool V8PromiseCustom::isPromise(v8::Handle<v8::Value> maybePromise, v8::Isolate* isolate) |
| 612 { | 506 { |
| 613 WrapperWorldType currentWorldType = worldType(isolate); | 507 WrapperWorldType currentWorldType = worldType(isolate); |
| 614 return V8Promise::GetTemplate(isolate, currentWorldType)->HasInstance(maybeP romise); | 508 return V8Promise::GetTemplate(isolate, currentWorldType)->HasInstance(maybeP romise); |
| 615 } | 509 } |
| 616 | 510 |
| 617 v8::Local<v8::Object> V8PromiseCustom::toPromise(v8::Handle<v8::Value> maybeProm ise, v8::Isolate* isolate) | 511 v8::Local<v8::Object> V8PromiseCustom::toPromise(v8::Handle<v8::Value> maybeProm ise, v8::Isolate* isolate) |
| 618 { | 512 { |
| 619 // FIXME: Currently we dones't check [[PromiseConstructor]] since we limits | 513 // FIXME: Currently we dones't check [[PromiseConstructor]] since we limits |
| 620 // the creation of the promise objects only from the Blink Promise | 514 // the creation of the promise objects only from the Blink Promise |
| 621 // constructor. | 515 // constructor. |
| 622 if (isPromise(maybePromise, isolate)) | 516 if (isPromise(maybePromise, isolate)) |
| 623 return maybePromise.As<v8::Object>(); | 517 return maybePromise.As<v8::Object>(); |
| 624 | 518 |
| 625 v8::Local<v8::Object> promise = createPromise(v8::Handle<v8::Object>(), isol ate); | 519 v8::Local<v8::Object> promise = createPromise(v8::Handle<v8::Object>(), isol ate); |
| 626 resolve(promise, maybePromise, Asynchronous, isolate); | 520 resolve(promise, maybePromise, isolate); |
| 627 return promise; | 521 return promise; |
| 628 } | 522 } |
| 629 | 523 |
| 524 void V8PromiseCustom::resolve(v8::Handle<v8::Object> promise, v8::Handle<v8::Val ue> result, v8::Isolate* isolate) | |
| 525 { | |
| 526 ASSERT(!result.IsEmpty()); | |
| 527 v8::Local<v8::Object> internal = getInternal(promise); | |
| 528 PromiseState state = getState(internal); | |
| 529 if (state != Pending) | |
| 530 return; | |
| 531 | |
| 532 if (isPromise(result, isolate)) { | |
| 533 v8::Local<v8::Object> valuePromise = result.As<v8::Object>(); | |
| 534 v8::Local<v8::Object> valueInternal = getInternal(valuePromise); | |
| 535 PromiseState valueState = getState(valueInternal); | |
| 536 if (promise->SameValue(valuePromise)) { | |
| 537 v8::Local<v8::Value> reason = V8ThrowException::createTypeError("Res olve a promise with itself", isolate); | |
| 538 setReason(promise, reason, isolate); | |
| 539 } else if (valueState == Following) { | |
| 540 v8::Local<v8::Object> valuePromiseFollowing = valueInternal->GetInte rnalField(InternalResultIndex).As<v8::Object>(); | |
| 541 setState(internal, Following, valuePromiseFollowing, isolate); | |
| 542 addToDerived(getInternal(valuePromiseFollowing), promise, v8::Handle <v8::Function>(), v8::Handle<v8::Function>(), isolate); | |
| 543 } else if (valueState == Fulfilled) { | |
| 544 setValue(promise, valueInternal->GetInternalField(InternalResultInde x), isolate); | |
| 545 } else if (valueState == Rejected) { | |
| 546 setReason(promise, valueInternal->GetInternalField(InternalResultInd ex), isolate); | |
| 547 } else { | |
| 548 ASSERT(valueState == Pending); | |
| 549 setState(internal, Following, valuePromise, isolate); | |
| 550 addToDerived(valueInternal, promise, v8::Handle<v8::Function>(), v8: :Handle<v8::Function>(), isolate); | |
| 551 } | |
| 552 } else { | |
| 553 setValue(promise, result, isolate); | |
| 554 } | |
| 555 } | |
| 556 | |
| 557 void V8PromiseCustom::reject(v8::Handle<v8::Object> promise, v8::Handle<v8::Valu e> reason, v8::Isolate* isolate) | |
| 558 { | |
| 559 v8::Local<v8::Object> internal = getInternal(promise); | |
| 560 PromiseState state = getState(internal); | |
| 561 if (state != Pending) | |
| 562 return; | |
| 563 setReason(promise, reason, isolate); | |
| 564 } | |
| 565 | |
| 566 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) | |
| 567 { | |
| 568 v8::Handle<v8::Object> internal = getInternal(promise); | |
| 569 if (getState(internal) == Following) | |
| 570 return then(internal->GetInternalField(InternalResultIndex).As<v8::Objec t>(), onFulfilled, onRejected, isolate); | |
| 571 // FIXME: Currently we dones't lookup "constructor" property since we limits | |
| 572 // the creation of the promise objects only from the Blink Promise | |
| 573 // constructor. | |
| 574 v8::Local<v8::Object> derivedPromise = createPromise(v8::Handle<v8::Object>( ), isolate); | |
| 575 updateDerivedFromPromise(derivedPromise, onFulfilled, onRejected, promise, i solate); | |
| 576 return derivedPromise; | |
| 577 } | |
| 578 | |
| 579 void V8PromiseCustom::setValue(v8::Handle<v8::Object> promise, v8::Handle<v8::Va lue> value, v8::Isolate* isolate) | |
| 580 { | |
| 581 v8::Local<v8::Object> internal = getInternal(promise); | |
| 582 ASSERT(getState(internal) != Fulfilled && getState(internal) != Rejected); | |
| 583 setState(internal, Fulfilled, value, isolate); | |
| 584 propagateToDerived(promise, isolate); | |
| 585 } | |
| 586 | |
| 587 void V8PromiseCustom::setReason(v8::Handle<v8::Object> promise, v8::Handle<v8::V alue> reason, v8::Isolate* isolate) | |
| 588 { | |
| 589 v8::Local<v8::Object> internal = getInternal(promise); | |
| 590 ASSERT(getState(internal) != Fulfilled && getState(internal) != Rejected); | |
| 591 setState(internal, Rejected, reason, isolate); | |
| 592 propagateToDerived(promise, isolate); | |
| 593 } | |
| 594 | |
| 595 void V8PromiseCustom::propagateToDerived(v8::Handle<v8::Object> promise, v8::Iso late* isolate) | |
| 596 { | |
| 597 v8::Local<v8::Object> internal = getInternal(promise); | |
| 598 ASSERT(getState(internal) == Fulfilled || getState(internal) == Rejected); | |
| 599 v8::Local<v8::Array> fulfillCallbacks = internal->GetInternalField(InternalF ulfillCallbackIndex).As<v8::Array>(); | |
| 600 v8::Local<v8::Array> rejectCallbacks = internal->GetInternalField(InternalRe jectCallbackIndex).As<v8::Array>(); | |
| 601 v8::Local<v8::Array> derivedPromises = internal->GetInternalField(InternalDe rivedPromiseIndex).As<v8::Array>(); | |
| 602 for (uint32_t i = 0, length = derivedPromises->Length(); i < length; ++i) { | |
| 603 v8::Local<v8::Object> derivedPromise = derivedPromises->Get(i).As<v8::Ob ject>(); | |
| 604 | |
| 605 v8::Local<v8::Function> onFulfilled, onRejected; | |
| 606 v8::Local<v8::Value> onFulfilledValue = fulfillCallbacks->Get(i); | |
| 607 if (onFulfilledValue->IsFunction()) { | |
| 608 onFulfilled = onFulfilledValue.As<v8::Function>(); | |
| 609 } | |
| 610 v8::Local<v8::Value> onRejectedValue = rejectCallbacks->Get(i); | |
| 611 if (onRejectedValue->IsFunction()) { | |
| 612 onRejected = onRejectedValue.As<v8::Function>(); | |
| 613 } | |
| 614 | |
| 615 updateDerived(derivedPromise, onFulfilled, onRejected, promise, isolate) ; | |
| 616 } | |
| 617 clearDerived(internal); | |
| 618 } | |
| 619 | |
| 620 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) | |
| 621 { | |
| 622 v8::Local<v8::Object> originatorInternal = getInternal(originator); | |
| 623 ASSERT(getState(originatorInternal) == Fulfilled || getState(originatorInter nal) == Rejected); | |
| 624 PromiseState originatorState = getState(originatorInternal); | |
| 625 v8::Local<v8::Value> originatorValue = originatorInternal->GetInternalField( InternalResultIndex); | |
| 626 if (originatorState == Fulfilled) { | |
| 627 if (originatorValue->IsObject()) { | |
| 628 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionC ontext(); | |
| 629 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextTh read()); | |
| 630 scriptExecutionContext->postTask(adoptPtr(new UpdateDerivedTask(deri vedPromise, onFulfilled, onRejected, originatorValue.As<v8::Object>(), isolate)) ); | |
| 631 } else { | |
| 632 updateDerivedFromValue(derivedPromise, onFulfilled, originatorValue, isolate); | |
| 633 } | |
| 634 } else { | |
| 635 updateDerivedFromReason(derivedPromise, onRejected, originatorValue, iso late); | |
| 636 } | |
| 637 } | |
| 638 | |
| 639 void V8PromiseCustom::updateDerivedFromValue(v8::Handle<v8::Object> derivedPromi se, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Value> value, v8::Isola te* isolate) | |
| 640 { | |
| 641 if (!onFulfilled.IsEmpty()) { | |
| 642 callHandler(derivedPromise, onFulfilled, value, isolate); | |
| 643 } else { | |
| 644 setValue(derivedPromise, value, isolate); | |
| 645 } | |
| 646 } | |
| 647 | |
| 648 void V8PromiseCustom::updateDerivedFromReason(v8::Handle<v8::Object> derivedProm ise, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Value> reason, v8::Isol ate* isolate) | |
| 649 { | |
| 650 if (!onRejected.IsEmpty()) { | |
| 651 callHandler(derivedPromise, onRejected, reason, isolate); | |
| 652 } else { | |
| 653 setReason(derivedPromise, reason, isolate); | |
| 654 } | |
| 655 } | |
| 656 | |
| 657 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) | |
| 658 { | |
| 659 v8::Local<v8::Object> internal = getInternal(promise); | |
| 660 PromiseState state = getState(internal); | |
| 661 if (state == Fulfilled || state == Rejected) { | |
| 662 updateDerived(derivedPromise, onFulfilled, onRejected, promise, isolate) ; | |
| 663 } else { | |
| 664 addToDerived(internal, derivedPromise, onFulfilled, onRejected, isolate) ; | |
| 665 } | |
| 666 } | |
| 667 | |
| 668 v8::Local<v8::Object> V8PromiseCustom::coerceThenable(v8::Handle<v8::Object> the nable, v8::Handle<v8::Function> then, v8::Isolate* isolate) | |
| 669 { | |
| 670 ASSERT(!thenable.IsEmpty()); | |
| 671 ASSERT(!then.IsEmpty()); | |
| 672 v8::Local<v8::Object> promise = createPromise(v8::Handle<v8::Object>(), isol ate); | |
| 673 v8::Handle<v8::Value> argv[] = { | |
| 674 createClosure(promiseResolveCallback, promise, isolate), | |
| 675 createClosure(promiseRejectCallback, promise, isolate) | |
| 676 }; | |
| 677 v8::TryCatch trycatch; | |
| 678 if (V8ScriptRunner::callFunction(then, getScriptExecutionContext(), thenable , WTF_ARRAY_LENGTH(argv), argv, isolate).IsEmpty()) { | |
| 679 reject(promise, trycatch.Exception(), isolate); | |
| 680 } | |
| 681 thenable->SetHiddenValue(V8HiddenPropertyName::thenableHiddenPromise(isolate ), promise); | |
| 682 return promise; | |
| 683 } | |
| 684 | |
| 685 void V8PromiseCustom::callHandler(v8::Handle<v8::Object> promise, v8::Handle<v8: :Function> handler, v8::Handle<v8::Value> argument, v8::Isolate* isolate) | |
| 686 { | |
| 687 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext() ; | |
| 688 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextThread()); | |
| 689 scriptExecutionContext->postTask(adoptPtr(new CallHandlerTask(promise, handl er, argument, isolate))); | |
| 690 } | |
| 691 | |
| 630 } // namespace WebCore | 692 } // namespace WebCore |
| OLD | NEW |