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" |
| 48 #include "wtf/Noncopyable.h" | |
| 47 #include "wtf/PassOwnPtr.h" | 49 #include "wtf/PassOwnPtr.h" |
| 50 #include "wtf/Vector.h" | |
| 48 #include <v8.h> | 51 #include <v8.h> |
| 49 | 52 |
| 50 namespace WebCore { | 53 namespace WebCore { |
| 51 | 54 |
| 52 namespace { | 55 namespace { |
| 53 | 56 |
| 54 v8::Local<v8::ObjectTemplate> cachedObjectTemplate(void* privateTemplateUniqueKe y, int internalFieldCount, v8::Isolate* isolate) | 57 v8::Local<v8::ObjectTemplate> cachedObjectTemplate(void* privateTemplateUniqueKe y, int internalFieldCount, v8::Isolate* isolate) |
| 55 { | 58 { |
| 56 V8PerIsolateData* data = V8PerIsolateData::from(isolate); | 59 V8PerIsolateData* data = V8PerIsolateData::from(isolate); |
| 57 WrapperWorldType currentWorldType = worldType(isolate); | 60 WrapperWorldType currentWorldType = worldType(isolate); |
| 58 v8::Handle<v8::FunctionTemplate> functionDescriptor = data->privateTemplateI fExists(currentWorldType, privateTemplateUniqueKey); | 61 v8::Handle<v8::FunctionTemplate> functionDescriptor = data->privateTemplateI fExists(currentWorldType, privateTemplateUniqueKey); |
| 59 if (!functionDescriptor.IsEmpty()) | 62 if (!functionDescriptor.IsEmpty()) |
| 60 return functionDescriptor->InstanceTemplate(); | 63 return functionDescriptor->InstanceTemplate(); |
| 61 | 64 |
| 62 functionDescriptor = v8::FunctionTemplate::New(); | 65 functionDescriptor = v8::FunctionTemplate::New(); |
| 63 v8::Local<v8::ObjectTemplate> instanceTemplate = functionDescriptor->Instanc eTemplate(); | 66 v8::Local<v8::ObjectTemplate> instanceTemplate = functionDescriptor->Instanc eTemplate(); |
| 64 instanceTemplate->SetInternalFieldCount(internalFieldCount); | 67 instanceTemplate->SetInternalFieldCount(internalFieldCount); |
| 65 data->setPrivateTemplate(currentWorldType, privateTemplateUniqueKey, functio nDescriptor); | 68 data->setPrivateTemplate(currentWorldType, privateTemplateUniqueKey, functio nDescriptor); |
| 66 return instanceTemplate; | 69 return instanceTemplate; |
| 67 } | 70 } |
| 68 | 71 |
| 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) | 72 v8::Local<v8::ObjectTemplate> promiseAllEnvironmentObjectTemplate(v8::Isolate* i solate) |
| 77 { | 73 { |
| 78 // This is only for getting a unique pointer which we can pass to privateTem plate. | 74 // This is only for getting a unique pointer which we can pass to privateTem plate. |
| 79 static int privateTemplateUniqueKey = 0; | 75 static int privateTemplateUniqueKey = 0; |
| 80 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Prom iseAllEnvironmentFieldCount, isolate); | 76 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Prom iseAllEnvironmentFieldCount, isolate); |
| 81 } | 77 } |
| 82 | 78 |
| 83 v8::Local<v8::ObjectTemplate> primitiveWrapperObjectTemplate(v8::Isolate* isolat e) | 79 v8::Local<v8::ObjectTemplate> primitiveWrapperObjectTemplate(v8::Isolate* isolat e) |
| 84 { | 80 { |
| 85 // This is only for getting a unique pointer which we can pass to privateTem plate. | 81 // This is only for getting a unique pointer which we can pass to privateTem plate. |
| 86 static int privateTemplateUniqueKey = 0; | 82 static int privateTemplateUniqueKey = 0; |
| 87 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Prim itiveWrapperFieldCount, isolate); | 83 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Prim itiveWrapperFieldCount, isolate); |
| 88 } | 84 } |
| 89 | 85 |
| 90 v8::Local<v8::ObjectTemplate> internalObjectTemplate(v8::Isolate* isolate) | 86 v8::Local<v8::ObjectTemplate> internalObjectTemplate(v8::Isolate* isolate) |
| 91 { | 87 { |
| 92 // This is only for getting a unique pointer which we can pass to privateTem plate. | 88 // This is only for getting a unique pointer which we can pass to privateTem plate. |
| 93 static int privateTemplateUniqueKey = 0; | 89 static int privateTemplateUniqueKey = 0; |
| 94 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Inte rnalFieldCount, isolate); | 90 return cachedObjectTemplate(&privateTemplateUniqueKey, V8PromiseCustom::Inte rnalFieldCount, isolate); |
| 95 } | 91 } |
| 96 | 92 |
| 97 class PromiseTask : public ScriptExecutionContext::Task { | 93 void promiseResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
| 94 { | |
| 95 ASSERT(!args.Data().IsEmpty()); | |
| 96 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | |
| 97 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); | |
| 98 if (args.Length() > 0) | |
| 99 result = args[0]; | |
| 100 | |
| 101 V8PromiseCustom::resolve(promise, result, args.GetIsolate()); | |
| 102 } | |
| 103 | |
| 104 void promiseRejectCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | |
| 105 { | |
| 106 ASSERT(!args.Data().IsEmpty()); | |
| 107 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | |
| 108 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); | |
| 109 if (args.Length() > 0) | |
| 110 result = args[0]; | |
| 111 | |
| 112 V8PromiseCustom::reject(promise, result, args.GetIsolate()); | |
| 113 } | |
| 114 | |
| 115 void promiseAllFulfillCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | |
| 116 { | |
| 117 v8::Isolate* isolate = args.GetIsolate(); | |
| 118 ASSERT(!args.Data().IsEmpty()); | |
| 119 v8::Local<v8::Object> environment = args.Data().As<v8::Object>(); | |
| 120 v8::Local<v8::Value> result = v8::Undefined(isolate); | |
| 121 if (args.Length() > 0) | |
| 122 result = args[0]; | |
| 123 | |
| 124 v8::Local<v8::Object> promise = environment->GetInternalField(V8PromiseCusto m::PromiseAllEnvironmentPromiseIndex).As<v8::Object>(); | |
| 125 v8::Local<v8::Object> countdownWrapper = environment->GetInternalField(V8Pro miseCustom::PromiseAllEnvironmentCountdownIndex).As<v8::Object>(); | |
| 126 v8::Local<v8::Integer> index = environment->GetInternalField(V8PromiseCustom ::PromiseAllEnvironmentIndexIndex).As<v8::Integer>(); | |
| 127 v8::Local<v8::Array> results = environment->GetInternalField(V8PromiseCustom ::PromiseAllEnvironmentResultsIndex).As<v8::Array>(); | |
| 128 | |
| 129 results->Set(index->Value(), result); | |
| 130 | |
| 131 v8::Local<v8::Integer> countdown = countdownWrapper->GetInternalField(V8Prom iseCustom::PrimitiveWrapperPrimitiveIndex).As<v8::Integer>(); | |
| 132 ASSERT(countdown->Value() >= 1); | |
| 133 if (countdown->Value() == 1) { | |
| 134 V8PromiseCustom::resolve(promise, results, isolate); | |
| 135 return; | |
| 136 } | |
| 137 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(countdown->Value() - 1, isolate)); | |
| 138 } | |
| 139 | |
| 140 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) | |
| 141 { | |
| 142 v8::Local<v8::ObjectTemplate> objectTemplate = promiseAllEnvironmentObjectTe mplate(isolate); | |
| 143 v8::Local<v8::Object> environment = objectTemplate->NewInstance(); | |
| 144 | |
| 145 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentPromiseI ndex, promise); | |
| 146 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentCountdow nIndex, countdownWrapper); | |
| 147 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentIndexInd ex, v8::Integer::New(index, isolate)); | |
| 148 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentResultsI ndex, results); | |
| 149 return environment; | |
| 150 } | |
| 151 | |
| 152 // Clear |internal|'s derived array. | |
| 153 void clearDerived(v8::Handle<v8::Object> internal) | |
| 154 { | |
| 155 internal->SetInternalField(V8PromiseCustom::InternalFulfillCallbackIndex, v8 ::Array::New()); | |
| 156 internal->SetInternalField(V8PromiseCustom::InternalRejectCallbackIndex, v8: :Array::New()); | |
| 157 internal->SetInternalField(V8PromiseCustom::InternalDerivedPromiseIndex, v8: :Array::New()); | |
| 158 } | |
| 159 | |
| 160 // Add a tuple (|derivedPromise|, |onFulfilled|, |onRejected|) to | |
| 161 // |internal|'s derived array. | |
| 162 // |internal| must be a Promise internal object. | |
| 163 // |derivedPromise| must be a Promise instance. | |
| 164 // |onFulfilled| and |onRejected| can be an empty value respectively. | |
| 165 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) | |
| 166 { | |
| 167 v8::Local<v8::Array> fulfillCallbacks = internal->GetInternalField(V8Promise Custom::InternalFulfillCallbackIndex).As<v8::Array>(); | |
| 168 v8::Local<v8::Array> rejectCallbacks = internal->GetInternalField(V8PromiseC ustom::InternalRejectCallbackIndex).As<v8::Array>(); | |
| 169 v8::Local<v8::Array> derivedPromises = internal->GetInternalField(V8PromiseC ustom::InternalDerivedPromiseIndex).As<v8::Array>(); | |
| 170 | |
| 171 if (onFulfilled.IsEmpty()) { | |
| 172 fulfillCallbacks->Set(fulfillCallbacks->Length(), v8::Undefined(isolate) ); | |
| 173 } else { | |
| 174 fulfillCallbacks->Set(fulfillCallbacks->Length(), onFulfilled); | |
| 175 } | |
| 176 | |
| 177 if (onRejected.IsEmpty()) { | |
| 178 rejectCallbacks->Set(rejectCallbacks->Length(), v8::Undefined(isolate)); | |
| 179 } else { | |
| 180 rejectCallbacks->Set(rejectCallbacks->Length(), onRejected); | |
| 181 } | |
| 182 | |
| 183 ASSERT(!derivedPromise.IsEmpty()); | |
| 184 derivedPromises->Set(derivedPromises->Length(), derivedPromise); | |
| 185 | |
| 186 // Since they are treated as a tuple, | |
| 187 // we need to guaranteed that the length of these arrays are same. | |
| 188 ASSERT(fulfillCallbacks->Length() == rejectCallbacks->Length() && rejectCall backs->Length() == derivedPromises->Length()); | |
| 189 } | |
| 190 | |
| 191 class CallHandlerTask : public ScriptExecutionContext::Task { | |
| 98 public: | 192 public: |
| 99 PromiseTask(v8::Handle<v8::Function> callback, v8::Handle<v8::Object> receiv er, v8::Handle<v8::Value> result, v8::Isolate* isolate) | 193 CallHandlerTask(v8::Handle<v8::Object> promise, v8::Handle<v8::Function> han dler, v8::Handle<v8::Value> argument, v8::Isolate* isolate) |
| 100 : m_callback(isolate, callback) | 194 : m_promise(isolate, promise) |
| 101 , m_receiver(isolate, receiver) | 195 , m_handler(isolate, handler) |
| 102 , m_result(isolate, result) | 196 , m_argument(isolate, argument) |
| 103 { | 197 { |
| 104 ASSERT(!m_callback.isEmpty()); | 198 ASSERT(!m_promise.isEmpty()); |
| 105 ASSERT(!m_receiver.isEmpty()); | 199 ASSERT(!m_handler.isEmpty()); |
| 106 ASSERT(!m_result.isEmpty()); | 200 ASSERT(!m_argument.isEmpty()); |
| 107 } | 201 } |
| 108 virtual ~PromiseTask() { } | 202 virtual ~CallHandlerTask() { } |
| 109 | 203 |
| 110 virtual void performTask(ScriptExecutionContext*) OVERRIDE; | 204 virtual void performTask(ScriptExecutionContext*) OVERRIDE; |
| 111 | 205 |
| 112 private: | 206 private: |
| 113 ScopedPersistent<v8::Function> m_callback; | 207 ScopedPersistent<v8::Object> m_promise; |
| 114 ScopedPersistent<v8::Object> m_receiver; | 208 ScopedPersistent<v8::Function> m_handler; |
| 115 ScopedPersistent<v8::Value> m_result; | 209 ScopedPersistent<v8::Value> m_argument; |
| 116 }; | 210 }; |
| 117 | 211 |
| 118 void PromiseTask::performTask(ScriptExecutionContext* context) | 212 void CallHandlerTask::performTask(ScriptExecutionContext* context) |
| 119 { | 213 { |
| 120 ASSERT(context); | 214 ASSERT(context); |
| 121 if (context->activeDOMObjectsAreStopped()) | 215 if (context->activeDOMObjectsAreStopped()) |
| 122 return; | 216 return; |
| 123 | 217 |
| 124 ScriptState* state = 0; | 218 ScriptState* state = 0; |
| 125 if (context->isDocument()) { | 219 if (context->isDocument()) { |
| 126 state = mainWorldScriptState(static_cast<Document*>(context)->frame()); | 220 state = mainWorldScriptState(static_cast<Document*>(context)->frame()); |
| 127 } else { | 221 } else { |
| 128 ASSERT(context->isWorkerGlobalScope()); | 222 ASSERT(context->isWorkerGlobalScope()); |
| 129 state = scriptStateFromWorkerGlobalScope(toWorkerGlobalScope(context)); | 223 state = scriptStateFromWorkerGlobalScope(toWorkerGlobalScope(context)); |
| 130 } | 224 } |
| 131 ASSERT(state); | 225 ASSERT(state); |
| 132 | 226 |
| 133 v8::Isolate* isolate = state->isolate(); | 227 v8::Isolate* isolate = state->isolate(); |
| 134 v8::HandleScope handleScope(isolate); | 228 v8::HandleScope handleScope(isolate); |
| 135 v8::Handle<v8::Context> v8Context = state->context(); | 229 v8::Handle<v8::Context> v8Context = state->context(); |
| 136 v8::Context::Scope scope(v8Context); | 230 v8::Context::Scope scope(v8Context); |
| 137 v8::Handle<v8::Value> args[] = { m_result.newLocal(isolate) }; | 231 v8::Handle<v8::Value> args[] = { m_argument.newLocal(isolate) }; |
| 138 V8ScriptRunner::callFunction(m_callback.newLocal(isolate), context, m_receiv er.newLocal(isolate), WTF_ARRAY_LENGTH(args), args, isolate); | 232 v8::TryCatch trycatch; |
| 233 v8::Local<v8::Value> value = V8ScriptRunner::callFunction(m_handler.newLocal (isolate), context, v8::Undefined(isolate), WTF_ARRAY_LENGTH(args), args, isolat e); | |
| 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_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; | |
|
yhirano
2013/10/03 03:57:36
I think m_originatorValueObject is a better name b
yusukesuzuki
2013/10/03 08:59:42
You're right. Renamed.
| |
| 139 }; | 261 }; |
| 140 | 262 |
| 141 v8::Handle<v8::Value> postTask(v8::Handle<v8::Function> callback, v8::Handle<v8: :Object> receiver, v8::Handle<v8::Value> value, v8::Isolate* isolate) | 263 void UpdateDerivedTask::performTask(ScriptExecutionContext* context) |
| 142 { | 264 { |
| 143 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext() ; | 265 ASSERT(context); |
| 144 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextThread()); | 266 if (context->activeDOMObjectsAreStopped()) |
| 145 scriptExecutionContext->postTask(adoptPtr(new PromiseTask(callback, receiver , value, isolate))); | 267 return; |
| 146 return v8::Undefined(isolate); | 268 |
| 147 } | 269 ScriptState* state = 0; |
| 148 | 270 if (context->isDocument()) { |
| 149 void wrapperCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | 271 state = mainWorldScriptState(static_cast<Document*>(context)->frame()); |
| 150 { | 272 } else { |
| 151 v8::Isolate* isolate = args.GetIsolate(); | 273 ASSERT(context->isWorkerGlobalScope()); |
| 152 ASSERT(!args.Data().IsEmpty()); | 274 state = scriptStateFromWorkerGlobalScope(toWorkerGlobalScope(context)); |
| 153 v8::Local<v8::Object> environment = args.Data().As<v8::Object>(); | 275 } |
| 154 v8::Local<v8::Value> result = v8::Undefined(isolate); | 276 ASSERT(state); |
| 155 if (args.Length() > 0) | 277 |
| 156 result = args[0]; | 278 v8::Isolate* isolate = state->isolate(); |
| 157 | 279 v8::HandleScope handleScope(isolate); |
| 158 v8::Local<v8::Object> promise = environment->GetInternalField(V8PromiseCusto m::WrapperCallbackEnvironmentPromiseIndex).As<v8::Object>(); | 280 v8::Handle<v8::Context> v8Context = state->context(); |
| 159 v8::Local<v8::Function> callback = environment->GetInternalField(V8PromiseCu stom::WrapperCallbackEnvironmentCallbackIndex).As<v8::Function>(); | 281 v8::Context::Scope scope(v8Context); |
| 160 | 282 |
| 161 v8::Local<v8::Value> argv[] = { | 283 v8::Local<v8::Object> originatorObject = m_originatorObject.newLocal(isolate ); |
| 162 result, | 284 v8::Local<v8::Value> coercedAlready = originatorObject->GetHiddenValue(V8Hid denPropertyName::thenableHiddenPromise(isolate)); |
| 163 }; | 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; | |
| 164 v8::TryCatch trycatch; | 292 v8::TryCatch trycatch; |
| 165 result = V8ScriptRunner::callFunction(callback, getScriptExecutionContext(), promise, WTF_ARRAY_LENGTH(argv), argv, isolate); | 293 then = originatorObject->Get(v8::String::NewSymbol("then")); |
| 166 if (result.IsEmpty()) { | 294 if (then.IsEmpty()) { |
| 167 V8PromiseCustom::reject(promise, trycatch.Exception(), V8PromiseCustom:: Synchronous, isolate); | 295 // If calling the [[Get]] internal method threw an exception, catch it a nd run updateDerivedFromReason. |
| 168 return; | 296 V8PromiseCustom::updateDerivedFromReason(m_promise.newLocal(isolate), m_ onRejected.newLocal(isolate), trycatch.Exception(), isolate); |
| 169 } | 297 return; |
| 170 V8PromiseCustom::resolve(promise, result, V8PromiseCustom::Synchronous, isol ate); | 298 } |
| 171 } | 299 |
| 172 | 300 if (then->IsFunction()) { |
| 173 v8::Local<v8::Object> wrapperCallbackEnvironment(v8::Handle<v8::Object> promise, v8::Handle<v8::Function> callback, v8::Isolate* isolate) | 301 ASSERT(then->IsObject()); |
| 174 { | 302 v8::Local<v8::Object> coerced = V8PromiseCustom::coerceThenable(originat orObject, then.As<v8::Function>(), isolate); |
| 175 v8::Local<v8::ObjectTemplate> objectTemplate = wrapperCallbackEnvironmentObj ectTemplate(isolate); | 303 V8PromiseCustom::updateDerivedFromPromise(m_promise.newLocal(isolate), m _onFulfilled.newLocal(isolate), m_onRejected.newLocal(isolate), coerced, isolate ); |
| 176 v8::Local<v8::Object> environment = objectTemplate->NewInstance(); | 304 return; |
| 177 environment->SetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentPro miseIndex, promise); | 305 } |
| 178 environment->SetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentCal lbackIndex, callback); | 306 |
| 179 return environment; | 307 V8PromiseCustom::updateDerivedFromValue(m_promise.newLocal(isolate), m_onFul filled.newLocal(isolate), originatorObject, isolate); |
| 180 } | 308 } |
| 181 | 309 |
| 182 void promiseFulfillCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | 310 class Derived { |
| 183 { | 311 WTF_MAKE_NONCOPYABLE(Derived); |
| 184 ASSERT(!args.Data().IsEmpty()); | 312 public: |
| 185 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | 313 Derived(v8::Handle<v8::Object> promise, v8::Handle<v8::Function> onFulfilled , v8::Handle<v8::Function> onRejected, v8::Handle<v8::Object> originator, v8::Is olate* isolate) |
| 186 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); | 314 : m_promise(isolate, promise) |
| 187 if (args.Length() > 0) | 315 , m_onFulfilled(isolate, onFulfilled) |
| 188 result = args[0]; | 316 , m_onRejected(isolate, onRejected) |
| 189 | 317 , m_originator(isolate, originator) |
| 190 V8PromiseCustom::fulfill(promise, result, V8PromiseCustom::Synchronous, args .GetIsolate()); | 318 { |
| 191 } | 319 ASSERT(!m_promise.isEmpty()); |
| 192 | 320 ASSERT(!m_originator.isEmpty()); |
| 193 void promiseResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | 321 } |
| 194 { | 322 |
| 195 ASSERT(!args.Data().IsEmpty()); | 323 static PassOwnPtr<Derived> create(v8::Handle<v8::Object> promise, v8::Handle <v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Handle<v8:: Object> originator, v8::Isolate* isolate) |
| 196 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | 324 { |
| 197 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); | 325 return adoptPtr(new Derived(promise, onFulfilled, onRejected, originator , isolate)); |
| 198 if (args.Length() > 0) | 326 } |
| 199 result = args[0]; | 327 |
| 200 | 328 v8::Local<v8::Object> promise(v8::Isolate* isolate) const { return m_promise .newLocal(isolate); } |
| 201 V8PromiseCustom::resolve(promise, result, V8PromiseCustom::Synchronous, args .GetIsolate()); | 329 v8::Local<v8::Function> onFulfilled(v8::Isolate* isolate) const { return m_o nFulfilled.newLocal(isolate); } |
| 202 } | 330 v8::Local<v8::Function> onRejected(v8::Isolate* isolate) const { return m_on Rejected.newLocal(isolate); } |
| 203 | 331 v8::Local<v8::Object> originator(v8::Isolate* isolate) const { return m_orig inator.newLocal(isolate); } |
| 204 void promiseRejectCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | 332 |
| 205 { | 333 private: |
| 206 ASSERT(!args.Data().IsEmpty()); | 334 ScopedPersistent<v8::Object> m_promise; |
| 207 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | 335 ScopedPersistent<v8::Function> m_onFulfilled; |
| 208 v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); | 336 ScopedPersistent<v8::Function> m_onRejected; |
| 209 if (args.Length() > 0) | 337 ScopedPersistent<v8::Object> m_originator; |
| 210 result = args[0]; | 338 }; |
| 211 | 339 |
| 212 V8PromiseCustom::reject(promise, result, V8PromiseCustom::Synchronous, args. GetIsolate()); | 340 // Since Promises state propagation routines are executed recursively, they caus e |
| 213 } | 341 // stack overflow. |
| 214 | 342 // (e.g. UpdateDerived -> UpdateDerivedFromValue -> SetValue -> |
| 215 void callCallbacks(v8::Handle<v8::Array> callbacks, v8::Handle<v8::Value> result , V8PromiseCustom::SynchronousMode mode, v8::Isolate* isolate) | 343 // PropagateToDerived -> UpdateDerived) |
| 216 { | 344 // |
| 217 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); | 345 // To fix that, we introduce PromisePropagator. It holds the stack. When |
| 218 for (uint32_t i = 0, length = callbacks->Length(); i < length; ++i) { | 346 // propagating the result to the derived tuples, we append the derived tuples |
| 219 v8::Local<v8::Value> value = callbacks->Get(i); | 347 // to the stack. After that, we drain the stack to propagate the result to |
| 220 v8::Local<v8::Function> callback = value.As<v8::Function>(); | 348 // the stored tuples. |
| 221 V8PromiseCustom::call(callback, global, result, mode, isolate); | 349 class PromisePropagator { |
| 222 } | 350 WTF_MAKE_NONCOPYABLE(PromisePropagator); |
| 223 } | 351 public: |
| 224 | 352 PromisePropagator() { } |
| 225 void promiseAllFulfillCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | 353 |
| 226 { | 354 void performPropagation(v8::Isolate*); |
| 227 v8::Isolate* isolate = args.GetIsolate(); | 355 |
| 228 ASSERT(!args.Data().IsEmpty()); | 356 void setValue(v8::Handle<v8::Object> promise, v8::Handle<v8::Value>, v8::Iso late*); |
| 229 v8::Local<v8::Object> environment = args.Data().As<v8::Object>(); | 357 void setReason(v8::Handle<v8::Object> promise, v8::Handle<v8::Value>, v8::Is olate*); |
| 230 v8::Local<v8::Value> result = v8::Undefined(isolate); | 358 void propagateToDerived(v8::Handle<v8::Object> promise, v8::Isolate*); |
| 231 if (args.Length() > 0) | 359 void updateDerived(v8::Handle<v8::Object> derivedPromise, v8::Handle<v8::Fun ction> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Object> originator, v8::Isolate*); |
| 232 result = args[0]; | 360 void updateDerivedFromValue(v8::Handle<v8::Object> derivedPromise, v8::Handl e<v8::Function> onFulfilled, v8::Handle<v8::Value>, v8::Isolate*); |
| 233 | 361 void updateDerivedFromReason(v8::Handle<v8::Object> derivedPromise, v8::Hand le<v8::Function> onRejected, v8::Handle<v8::Value>, v8::Isolate*); |
| 234 v8::Local<v8::Object> promise = environment->GetInternalField(V8PromiseCusto m::PromiseAllEnvironmentPromiseIndex).As<v8::Object>(); | 362 void updateDerivedFromPromise(v8::Handle<v8::Object> derivedPromise, v8::Han dle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Handle<v 8::Object> promise, v8::Isolate*); |
| 235 v8::Local<v8::Object> countdownWrapper = environment->GetInternalField(V8Pro miseCustom::PromiseAllEnvironmentCountdownIndex).As<v8::Object>(); | 363 private: |
|
yhirano
2013/10/03 03:57:36
Please place a blank line before the access specif
yusukesuzuki
2013/10/03 08:59:42
Done.
| |
| 236 v8::Local<v8::Integer> index = environment->GetInternalField(V8PromiseCustom ::PromiseAllEnvironmentIndexIndex).As<v8::Integer>(); | 364 Vector<OwnPtr<Derived> > m_derivedStack; |
| 237 v8::Local<v8::Array> results = environment->GetInternalField(V8PromiseCustom ::PromiseAllEnvironmentResultsIndex).As<v8::Array>(); | 365 }; |
| 238 | 366 |
| 239 results->Set(index->Value(), result); | 367 void PromisePropagator::performPropagation(v8::Isolate* isolate) |
| 240 | 368 { |
| 241 v8::Local<v8::Integer> countdown = countdownWrapper->GetInternalField(V8Prom iseCustom::PrimitiveWrapperPrimitiveIndex).As<v8::Integer>(); | 369 while (!m_derivedStack.isEmpty()) { |
|
yhirano
2013/10/03 03:57:36
How about creating a HandleScope here?
yusukesuzuki
2013/10/03 08:59:42
Sounds nice. I've added it here
| |
| 242 ASSERT(countdown->Value() >= 1); | 370 OwnPtr<Derived> derived = m_derivedStack.last().release(); |
| 243 if (countdown->Value() == 1) { | 371 m_derivedStack.removeLast(); |
| 244 V8PromiseCustom::resolve(promise, results, V8PromiseCustom::Synchronous, isolate); | 372 updateDerived(derived->promise(isolate), derived->onFulfilled(isolate), derived->onRejected(isolate), derived->originator(isolate), isolate); |
| 245 return; | 373 } |
| 246 } | 374 } |
| 247 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(countdown->Value() - 1, isolate)); | 375 |
| 248 } | 376 void PromisePropagator::setValue(v8::Handle<v8::Object> promise, v8::Handle<v8:: Value> value, v8::Isolate* isolate) |
| 249 | 377 { |
| 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) | 378 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); |
| 251 { | 379 ASSERT(V8PromiseCustom::getState(internal) != V8PromiseCustom::Fulfilled && V8PromiseCustom::getState(internal) != V8PromiseCustom::Rejected); |
| 252 v8::Local<v8::ObjectTemplate> objectTemplate = promiseAllEnvironmentObjectTe mplate(isolate); | 380 V8PromiseCustom::setState(internal, V8PromiseCustom::Fulfilled, value, isola te); |
| 253 v8::Local<v8::Object> environment = objectTemplate->NewInstance(); | 381 propagateToDerived(promise, isolate); |
| 254 | 382 } |
| 255 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentPromiseI ndex, promise); | 383 |
| 256 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentCountdow nIndex, countdownWrapper); | 384 void PromisePropagator::setReason(v8::Handle<v8::Object> promise, v8::Handle<v8: :Value> reason, v8::Isolate* isolate) |
| 257 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentIndexInd ex, v8::Integer::New(index, isolate)); | 385 { |
| 258 environment->SetInternalField(V8PromiseCustom::PromiseAllEnvironmentResultsI ndex, results); | 386 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); |
| 259 return environment; | 387 ASSERT(V8PromiseCustom::getState(internal) != V8PromiseCustom::Fulfilled && V8PromiseCustom::getState(internal) != V8PromiseCustom::Rejected); |
| 260 } | 388 V8PromiseCustom::setState(internal, V8PromiseCustom::Rejected, reason, isola te); |
| 261 | 389 propagateToDerived(promise, isolate); |
| 262 void promiseResolve(const v8::FunctionCallbackInfo<v8::Value>& args) | 390 } |
| 263 { | 391 |
| 264 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | 392 void PromisePropagator::propagateToDerived(v8::Handle<v8::Object> promise, v8::I solate* isolate) |
| 265 ASSERT(!promise.IsEmpty()); | 393 { |
| 266 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); | 394 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); |
| 267 if (V8PromiseCustom::getState(internal) != V8PromiseCustom::Pending) | 395 ASSERT(V8PromiseCustom::getState(internal) == V8PromiseCustom::Fulfilled || V8PromiseCustom::getState(internal) == V8PromiseCustom::Rejected); |
| 268 return; | 396 v8::Local<v8::Array> fulfillCallbacks = internal->GetInternalField(V8Promise Custom::InternalFulfillCallbackIndex).As<v8::Array>(); |
| 269 v8::Isolate* isolate = args.GetIsolate(); | 397 v8::Local<v8::Array> rejectCallbacks = internal->GetInternalField(V8PromiseC ustom::InternalRejectCallbackIndex).As<v8::Array>(); |
| 270 V8PromiseCustom::setState(V8PromiseCustom::getInternal(promise), V8PromiseCu stom::Following, isolate); | 398 v8::Local<v8::Array> derivedPromises = internal->GetInternalField(V8PromiseC ustom::InternalDerivedPromiseIndex).As<v8::Array>(); |
| 271 | 399 // Since they are treated as a tuple, |
| 272 v8::Local<v8::Value> result = v8::Undefined(isolate); | 400 // we need to guaranteed that the length of these arrays are same. |
| 273 if (args.Length() > 0) | 401 ASSERT(fulfillCallbacks->Length() == rejectCallbacks->Length() && rejectCall backs->Length() == derivedPromises->Length()); |
| 274 result = args[0]; | 402 |
| 275 V8PromiseCustom::resolve(promise, result, V8PromiseCustom::Asynchronous, iso late); | 403 // Append Derived tuple to the stack in reverse order. |
| 276 } | 404 for (uint32_t count = 0, length = derivedPromises->Length(); count < length; ++count) { |
| 277 | 405 uint32_t i = length - count - 1; |
| 278 void promiseReject(const v8::FunctionCallbackInfo<v8::Value>& args) | 406 v8::Local<v8::Object> derivedPromise = derivedPromises->Get(i).As<v8::Ob ject>(); |
| 279 { | 407 |
| 280 v8::Local<v8::Object> promise = args.Data().As<v8::Object>(); | 408 v8::Local<v8::Function> onFulfilled, onRejected; |
| 281 ASSERT(!promise.IsEmpty()); | 409 v8::Local<v8::Value> onFulfilledValue = fulfillCallbacks->Get(i); |
| 282 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); | 410 if (onFulfilledValue->IsFunction()) { |
| 283 if (V8PromiseCustom::getState(internal) != V8PromiseCustom::Pending) | 411 onFulfilled = onFulfilledValue.As<v8::Function>(); |
| 284 return; | 412 } |
| 285 v8::Isolate* isolate = args.GetIsolate(); | 413 v8::Local<v8::Value> onRejectedValue = rejectCallbacks->Get(i); |
| 286 V8PromiseCustom::setState(V8PromiseCustom::getInternal(promise), V8PromiseCu stom::Following, isolate); | 414 if (onRejectedValue->IsFunction()) { |
| 287 | 415 onRejected = onRejectedValue.As<v8::Function>(); |
| 288 v8::Local<v8::Value> result = v8::Undefined(isolate); | 416 } |
| 289 if (args.Length() > 0) | 417 |
| 290 result = args[0]; | 418 m_derivedStack.append(Derived::create(derivedPromise, onFulfilled, onRej ected, promise, isolate)); |
| 291 V8PromiseCustom::reject(promise, result, V8PromiseCustom::Asynchronous, isol ate); | 419 } |
| 420 clearDerived(internal); | |
| 421 } | |
| 422 | |
| 423 void PromisePropagator::updateDerivedFromValue(v8::Handle<v8::Object> derivedPro mise, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Value> value, v8::Iso late* isolate) | |
| 424 { | |
| 425 if (!onFulfilled.IsEmpty()) { | |
| 426 V8PromiseCustom::callHandler(derivedPromise, onFulfilled, value, isolate ); | |
| 427 } else { | |
| 428 setValue(derivedPromise, value, isolate); | |
| 429 } | |
| 430 } | |
| 431 | |
| 432 void PromisePropagator::updateDerivedFromReason(v8::Handle<v8::Object> derivedPr omise, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Value> reason, v8::Is olate* isolate) | |
| 433 { | |
| 434 if (!onRejected.IsEmpty()) { | |
| 435 V8PromiseCustom::callHandler(derivedPromise, onRejected, reason, isolate ); | |
| 436 } else { | |
| 437 setReason(derivedPromise, reason, isolate); | |
| 438 } | |
| 439 } | |
| 440 | |
| 441 void PromisePropagator::updateDerived(v8::Handle<v8::Object> derivedPromise, v8: :Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Hand le<v8::Object> originator, v8::Isolate* isolate) | |
| 442 { | |
| 443 v8::Local<v8::Object> originatorInternal = V8PromiseCustom::getInternal(orig inator); | |
| 444 ASSERT(V8PromiseCustom::getState(originatorInternal) == V8PromiseCustom::Ful filled || V8PromiseCustom::getState(originatorInternal) == V8PromiseCustom::Reje cted); | |
|
yhirano
2013/10/03 03:57:36
If you define originatorState before the assert, y
yusukesuzuki
2013/10/03 08:59:42
Done.
| |
| 445 V8PromiseCustom::PromiseState originatorState = V8PromiseCustom::getState(or iginatorInternal); | |
| 446 v8::Local<v8::Value> originatorValue = originatorInternal->GetInternalField( V8PromiseCustom::InternalResultIndex); | |
| 447 if (originatorState == V8PromiseCustom::Fulfilled) { | |
| 448 if (originatorValue->IsObject()) { | |
| 449 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionC ontext(); | |
| 450 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextTh read()); | |
| 451 scriptExecutionContext->postTask(adoptPtr(new UpdateDerivedTask(deri vedPromise, onFulfilled, onRejected, originatorValue.As<v8::Object>(), isolate)) ); | |
| 452 } else { | |
| 453 updateDerivedFromValue(derivedPromise, onFulfilled, originatorValue, isolate); | |
| 454 } | |
| 455 } else { | |
| 456 updateDerivedFromReason(derivedPromise, onRejected, originatorValue, iso late); | |
| 457 } | |
| 458 } | |
| 459 | |
| 460 void PromisePropagator::updateDerivedFromPromise(v8::Handle<v8::Object> derivedP romise, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejecte d, v8::Handle<v8::Object> promise, v8::Isolate* isolate) | |
| 461 { | |
| 462 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); | |
| 463 V8PromiseCustom::PromiseState state = V8PromiseCustom::getState(internal); | |
| 464 if (state == V8PromiseCustom::Fulfilled || state == V8PromiseCustom::Rejecte d) { | |
| 465 updateDerived(derivedPromise, onFulfilled, onRejected, promise, isolate) ; | |
| 466 } else { | |
| 467 addToDerived(internal, derivedPromise, onFulfilled, onRejected, isolate) ; | |
| 468 } | |
| 292 } | 469 } |
| 293 | 470 |
| 294 } // namespace | 471 } // namespace |
| 295 | 472 |
| 296 void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) | 473 void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) |
| 297 { | 474 { |
| 298 v8SetReturnValue(args, v8::Local<v8::Value>()); | 475 v8SetReturnValue(args, v8::Local<v8::Value>()); |
| 299 v8::Isolate* isolate = args.GetIsolate(); | 476 v8::Isolate* isolate = args.GetIsolate(); |
| 300 if (!args.Length() || !args[0]->IsFunction()) { | 477 if (!args.Length() || !args[0]->IsFunction()) { |
| 301 throwTypeError("Promise constructor takes a function argument", isolate) ; | 478 throwTypeError("Promise constructor takes a function argument", isolate) ; |
| 302 return; | 479 return; |
| 303 } | 480 } |
| 304 v8::Local<v8::Function> init = args[0].As<v8::Function>(); | 481 v8::Local<v8::Function> init = args[0].As<v8::Function>(); |
| 305 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 482 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 306 v8::Handle<v8::Value> argv[] = { | 483 v8::Handle<v8::Value> argv[] = { |
| 307 createClosure(promiseResolve, promise, isolate), | 484 createClosure(promiseResolveCallback, promise, isolate), |
| 308 createClosure(promiseReject, promise, isolate) | 485 createClosure(promiseRejectCallback, promise, isolate) |
| 309 }; | 486 }; |
| 310 v8::TryCatch trycatch; | 487 v8::TryCatch trycatch; |
| 311 if (V8ScriptRunner::callFunction(init, getScriptExecutionContext(), promise, WTF_ARRAY_LENGTH(argv), argv, isolate).IsEmpty()) { | 488 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. | 489 // An exception is thrown. Reject the promise if its resolved flag is un set. |
| 313 if (V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise)) == V8PromiseCustom::Pending) | 490 V8PromiseCustom::reject(promise, trycatch.Exception(), isolate); |
| 314 V8PromiseCustom::reject(promise, trycatch.Exception(), V8PromiseCust om::Asynchronous, isolate); | |
| 315 } | 491 } |
| 316 v8SetReturnValue(args, promise); | 492 v8SetReturnValue(args, promise); |
| 317 return; | 493 return; |
| 318 } | 494 } |
| 319 | 495 |
| 320 void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) | 496 void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) |
| 321 { | 497 { |
| 322 v8::Isolate* isolate = args.GetIsolate(); | 498 v8::Isolate* isolate = args.GetIsolate(); |
| 323 v8::Local<v8::Function> fulfillWrapper, rejectWrapper; | 499 v8::Local<v8::Function> onFulfilled, onRejected; |
| 324 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 500 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 325 if (args.Length() > 0 && !args[0]->IsUndefined()) { | 501 if (args.Length() > 0 && !args[0]->IsUndefined()) { |
| 326 if (!args[0]->IsFunction()) { | 502 if (!args[0]->IsFunction()) { |
| 327 v8SetReturnValue(args, throwTypeError("fulfillCallback must be a fun ction or undefined", isolate)); | 503 v8SetReturnValue(args, throwTypeError("onFulfilled must be a functio n or undefined", isolate)); |
| 328 return; | 504 return; |
| 329 } | 505 } |
| 330 fulfillWrapper = createClosure(wrapperCallback, wrapperCallbackEnvironme nt(promise, args[0].As<v8::Function>(), isolate), isolate); | 506 onFulfilled = args[0].As<v8::Function>(); |
| 331 } else { | |
| 332 fulfillWrapper = createClosure(promiseFulfillCallback, promise, isolate) ; | |
| 333 } | 507 } |
| 334 if (args.Length() > 1 && !args[1]->IsUndefined()) { | 508 if (args.Length() > 1 && !args[1]->IsUndefined()) { |
| 335 if (!args[1]->IsFunction()) { | 509 if (!args[1]->IsFunction()) { |
| 336 v8SetReturnValue(args, throwTypeError("rejectCallback must be a func tion or undefined", isolate)); | 510 v8SetReturnValue(args, throwTypeError("onRejected must be a function or undefined", isolate)); |
| 337 return; | 511 return; |
| 338 } | 512 } |
| 339 rejectWrapper = createClosure(wrapperCallback, wrapperCallbackEnvironmen t(promise, args[1].As<v8::Function>(), isolate), isolate); | 513 onRejected = args[1].As<v8::Function>(); |
| 340 } else { | |
| 341 rejectWrapper = createClosure(promiseRejectCallback, promise, isolate); | |
| 342 } | 514 } |
| 343 V8PromiseCustom::append(args.Holder(), fulfillWrapper, rejectWrapper, isolat e); | 515 v8SetReturnValue(args, V8PromiseCustom::then(args.Holder(), onFulfilled, onR ejected, isolate)); |
| 344 v8SetReturnValue(args, promise); | |
| 345 } | 516 } |
| 346 | 517 |
| 347 void V8Promise::castMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) | 518 void V8Promise::castMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) |
| 348 { | 519 { |
| 349 v8::Isolate* isolate = args.GetIsolate(); | 520 v8::Isolate* isolate = args.GetIsolate(); |
| 350 v8::Local<v8::Value> result = v8::Undefined(isolate); | 521 v8::Local<v8::Value> result = v8::Undefined(isolate); |
| 351 if (args.Length() > 0) | 522 if (args.Length() > 0) |
| 352 result = args[0]; | 523 result = args[0]; |
| 353 | 524 |
| 354 v8SetReturnValue(args, V8PromiseCustom::toPromise(result, isolate)); | 525 v8SetReturnValue(args, V8PromiseCustom::toPromise(result, isolate)); |
| 355 } | 526 } |
| 356 | 527 |
| 357 void V8Promise::catchMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) | 528 void V8Promise::catchMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& arg s) |
| 358 { | 529 { |
| 359 v8::Isolate* isolate = args.GetIsolate(); | 530 v8::Isolate* isolate = args.GetIsolate(); |
| 360 v8::Local<v8::Function> fulfillWrapper, rejectWrapper; | 531 v8::Local<v8::Function> onFulfilled, onRejected; |
| 361 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | |
| 362 | 532 |
| 363 if (args.Length() > 0 && !args[0]->IsUndefined()) { | 533 if (args.Length() > 0 && !args[0]->IsUndefined()) { |
| 364 if (!args[0]->IsFunction()) { | 534 if (!args[0]->IsFunction()) { |
| 365 v8SetReturnValue(args, throwTypeError("rejectCallback must be a func tion or undefined", isolate)); | 535 v8SetReturnValue(args, throwTypeError("onRejected must be a function or undefined", isolate)); |
| 366 return; | 536 return; |
| 367 } | 537 } |
| 368 rejectWrapper = createClosure(wrapperCallback, wrapperCallbackEnvironmen t(promise, args[0].As<v8::Function>(), isolate), isolate); | 538 onRejected = args[0].As<v8::Function>(); |
| 369 } else { | |
| 370 rejectWrapper = createClosure(promiseRejectCallback, promise, isolate); | |
| 371 } | 539 } |
| 372 fulfillWrapper = createClosure(promiseFulfillCallback, promise, isolate); | 540 v8SetReturnValue(args, V8PromiseCustom::then(args.Holder(), onFulfilled, onR ejected, isolate)); |
| 373 V8PromiseCustom::append(args.Holder(), fulfillWrapper, rejectWrapper, isolat e); | |
| 374 v8SetReturnValue(args, promise); | |
| 375 } | 541 } |
| 376 | 542 |
| 377 void V8Promise::resolveMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& a rgs) | 543 void V8Promise::resolveMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& a rgs) |
| 378 { | 544 { |
| 379 v8::Isolate* isolate = args.GetIsolate(); | 545 v8::Isolate* isolate = args.GetIsolate(); |
| 380 v8::Local<v8::Value> result = v8::Undefined(isolate); | 546 v8::Local<v8::Value> result = v8::Undefined(isolate); |
| 381 if (args.Length() > 0) | 547 if (args.Length() > 0) |
| 382 result = args[0]; | 548 result = args[0]; |
| 383 | 549 |
| 384 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 550 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 385 V8PromiseCustom::resolve(promise, result, V8PromiseCustom::Asynchronous, iso late); | 551 V8PromiseCustom::resolve(promise, result, isolate); |
| 386 v8SetReturnValue(args, promise); | 552 v8SetReturnValue(args, promise); |
| 387 } | 553 } |
| 388 | 554 |
| 389 void V8Promise::rejectMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& ar gs) | 555 void V8Promise::rejectMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& ar gs) |
| 390 { | 556 { |
| 391 v8::Isolate* isolate = args.GetIsolate(); | 557 v8::Isolate* isolate = args.GetIsolate(); |
| 392 v8::Local<v8::Value> result = v8::Undefined(isolate); | 558 v8::Local<v8::Value> result = v8::Undefined(isolate); |
| 393 if (args.Length() > 0) | 559 if (args.Length() > 0) |
| 394 result = args[0]; | 560 result = args[0]; |
| 395 | 561 |
| 396 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 562 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 397 V8PromiseCustom::reject(promise, result, V8PromiseCustom::Asynchronous, isol ate); | 563 V8PromiseCustom::reject(promise, result, isolate); |
| 398 v8SetReturnValue(args, promise); | 564 v8SetReturnValue(args, promise); |
| 399 } | 565 } |
| 400 | 566 |
| 401 void V8Promise::raceMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) | 567 void V8Promise::raceMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args ) |
| 402 { | 568 { |
| 403 v8::Isolate* isolate = args.GetIsolate(); | 569 v8::Isolate* isolate = args.GetIsolate(); |
| 404 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 570 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 405 | 571 |
| 406 if (!args.Length() || !args[0]->IsArray()) { | 572 if (!args.Length() || !args[0]->IsArray()) { |
| 407 v8SetReturnValue(args, promise); | 573 v8SetReturnValue(args, promise); |
| 408 return; | 574 return; |
| 409 } | 575 } |
| 410 | 576 |
| 411 // FIXME: Now we limit the iterable type to the Array type. | 577 // FIXME: Now we limit the iterable type to the Array type. |
| 412 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); | 578 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); |
| 413 v8::Local<v8::Function> fulfillCallback = createClosure(promiseResolveCallba ck, promise, isolate); | 579 v8::Local<v8::Function> onFulfilled = createClosure(promiseResolveCallback, promise, isolate); |
| 414 v8::Local<v8::Function> rejectCallback = createClosure(promiseRejectCallback , promise, isolate); | 580 v8::Local<v8::Function> onRejected = createClosure(promiseRejectCallback, pr omise, isolate); |
| 415 | 581 |
| 416 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { | 582 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { |
| 417 // Array-holes should not be skipped by for-of iteration semantics. | 583 // Array-holes should not be skipped by for-of iteration semantics. |
| 418 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); | 584 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); |
| 419 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); | 585 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); |
| 420 V8PromiseCustom::append(nextPromise, fulfillCallback, rejectCallback, is olate); | 586 V8PromiseCustom::then(nextPromise, onFulfilled, onRejected, isolate); |
| 421 } | 587 } |
| 422 v8SetReturnValue(args, promise); | 588 v8SetReturnValue(args, promise); |
| 423 } | 589 } |
| 424 | 590 |
| 425 void V8Promise::allMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) | 591 void V8Promise::allMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
| 426 { | 592 { |
| 427 v8::Isolate* isolate = args.GetIsolate(); | 593 v8::Isolate* isolate = args.GetIsolate(); |
| 428 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); | 594 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(args.Holder() , isolate); |
| 429 v8::Local<v8::Array> results = v8::Array::New(); | 595 v8::Local<v8::Array> results = v8::Array::New(); |
| 430 | 596 |
| 431 if (!args.Length() || !args[0]->IsArray()) { | 597 if (!args.Length() || !args[0]->IsArray()) { |
| 432 V8PromiseCustom::resolve(promise, results, V8PromiseCustom::Asynchronous , isolate); | 598 V8PromiseCustom::resolve(promise, results, isolate); |
| 433 v8SetReturnValue(args, promise); | 599 v8SetReturnValue(args, promise); |
| 434 return; | 600 return; |
| 435 } | 601 } |
| 436 | 602 |
| 437 // FIXME: Now we limit the iterable type to the Array type. | 603 // FIXME: Now we limit the iterable type to the Array type. |
| 438 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); | 604 v8::Local<v8::Array> iterable = args[0].As<v8::Array>(); |
| 439 | 605 |
| 440 if (!iterable->Length()) { | 606 if (!iterable->Length()) { |
| 441 V8PromiseCustom::resolve(promise, results, V8PromiseCustom::Asynchronous , isolate); | 607 V8PromiseCustom::resolve(promise, results, isolate); |
| 442 v8SetReturnValue(args, promise); | 608 v8SetReturnValue(args, promise); |
| 443 return; | 609 return; |
| 444 } | 610 } |
| 445 | 611 |
| 446 v8::Local<v8::ObjectTemplate> objectTemplate = primitiveWrapperObjectTemplat e(isolate); | 612 v8::Local<v8::ObjectTemplate> objectTemplate = primitiveWrapperObjectTemplat e(isolate); |
| 447 v8::Local<v8::Object> countdownWrapper = objectTemplate->NewInstance(); | 613 v8::Local<v8::Object> countdownWrapper = objectTemplate->NewInstance(); |
| 448 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(iterable->Length(), isolate)); | 614 countdownWrapper->SetInternalField(V8PromiseCustom::PrimitiveWrapperPrimitiv eIndex, v8::Integer::New(iterable->Length(), isolate)); |
| 449 | 615 |
| 450 v8::Local<v8::Function> rejectCallback = createClosure(promiseRejectCallback , promise, isolate); | 616 v8::Local<v8::Function> onRejected = createClosure(promiseRejectCallback, pr omise, isolate); |
| 451 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { | 617 for (unsigned i = 0, length = iterable->Length(); i < length; ++i) { |
| 452 // Array-holes should not be skipped by for-of iteration semantics. | 618 // Array-holes should not be skipped by for-of iteration semantics. |
| 453 v8::Local<v8::Object> environment = promiseAllEnvironment(promise, count downWrapper, i, results, isolate); | 619 v8::Local<v8::Object> environment = promiseAllEnvironment(promise, count downWrapper, i, results, isolate); |
| 454 v8::Local<v8::Function> fulfillCallback = createClosure(promiseAllFulfil lCallback, environment, isolate); | 620 v8::Local<v8::Function> onFulfilled = createClosure(promiseAllFulfillCal lback, environment, isolate); |
| 455 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); | 621 V8TRYCATCH_VOID(v8::Local<v8::Value>, nextValue, iterable->Get(i)); |
| 456 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); | 622 v8::Local<v8::Object> nextPromise = V8PromiseCustom::toPromise(nextValue , isolate); |
| 457 V8PromiseCustom::append(nextPromise, fulfillCallback, rejectCallback, is olate); | 623 V8PromiseCustom::then(nextPromise, onFulfilled, onRejected, isolate); |
| 458 } | 624 } |
| 459 v8SetReturnValue(args, promise); | 625 v8SetReturnValue(args, promise); |
| 460 } | 626 } |
| 461 | 627 |
| 462 // | 628 // |
| 463 // -- V8PromiseCustom -- | 629 // -- V8PromiseCustom -- |
| 464 v8::Local<v8::Object> V8PromiseCustom::createPromise(v8::Handle<v8::Object> crea tionContext, v8::Isolate* isolate) | 630 v8::Local<v8::Object> V8PromiseCustom::createPromise(v8::Handle<v8::Object> crea tionContext, v8::Isolate* isolate) |
| 465 { | 631 { |
| 466 v8::Local<v8::ObjectTemplate> internalTemplate = internalObjectTemplate(isol ate); | 632 v8::Local<v8::ObjectTemplate> internalTemplate = internalObjectTemplate(isol ate); |
| 467 v8::Local<v8::Object> internal = internalTemplate->NewInstance(); | 633 v8::Local<v8::Object> internal = internalTemplate->NewInstance(); |
| 468 v8::Local<v8::Object> promise = V8DOMWrapper::createWrapper(creationContext, &V8Promise::info, 0, isolate); | 634 v8::Local<v8::Object> promise = V8DOMWrapper::createWrapper(creationContext, &V8Promise::info, 0, isolate); |
| 469 | 635 |
| 470 clearInternal(internal, V8PromiseCustom::Pending, v8::Undefined(isolate), is olate); | 636 clearDerived(internal); |
| 637 setState(internal, Pending, v8::Undefined(isolate), isolate); | |
| 471 | 638 |
| 472 promise->SetInternalField(v8DOMWrapperObjectIndex, internal); | 639 promise->SetInternalField(v8DOMWrapperObjectIndex, internal); |
| 473 return promise; | 640 return promise; |
| 474 } | 641 } |
| 475 | 642 |
| 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) | 643 v8::Local<v8::Object> V8PromiseCustom::getInternal(v8::Handle<v8::Object> promis e) |
| 569 { | 644 { |
| 570 v8::Local<v8::Value> value = promise->GetInternalField(v8DOMWrapperObjectInd ex); | 645 v8::Local<v8::Value> value = promise->GetInternalField(v8DOMWrapperObjectInd ex); |
| 571 return value.As<v8::Object>(); | 646 return value.As<v8::Object>(); |
| 572 } | 647 } |
| 573 | 648 |
| 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) | 649 V8PromiseCustom::PromiseState V8PromiseCustom::getState(v8::Handle<v8::Object> i nternal) |
| 583 { | 650 { |
| 584 v8::Handle<v8::Value> value = internal->GetInternalField(V8PromiseCustom::In ternalStateIndex); | 651 v8::Handle<v8::Value> value = internal->GetInternalField(V8PromiseCustom::In ternalStateIndex); |
| 585 bool ok = false; | 652 bool ok = false; |
| 586 uint32_t number = toInt32(value, ok); | 653 uint32_t number = toInt32(value, ok); |
| 587 ASSERT(ok && (number == Pending || number == Fulfilled || number == Rejected || number == Following)); | 654 ASSERT(ok && (number == Pending || number == Fulfilled || number == Rejected || number == Following)); |
| 588 return static_cast<PromiseState>(number); | 655 return static_cast<PromiseState>(number); |
| 589 } | 656 } |
| 590 | 657 |
| 591 void V8PromiseCustom::setState(v8::Handle<v8::Object> internal, PromiseState sta te, v8::Isolate* isolate) | 658 void V8PromiseCustom::setState(v8::Handle<v8::Object> internal, PromiseState sta te, v8::Handle<v8::Value> value, v8::Isolate* isolate) |
| 592 { | 659 { |
| 660 ASSERT(!value.IsEmpty()); | |
| 593 ASSERT(state == Pending || state == Fulfilled || state == Rejected || state == Following); | 661 ASSERT(state == Pending || state == Fulfilled || state == Rejected || state == Following); |
| 594 internal->SetInternalField(V8PromiseCustom::InternalStateIndex, v8::Integer: :New(state, isolate)); | 662 internal->SetInternalField(InternalStateIndex, v8::Integer::New(state, isola te)); |
| 595 } | 663 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 } | 664 } |
| 610 | 665 |
| 611 bool V8PromiseCustom::isPromise(v8::Handle<v8::Value> maybePromise, v8::Isolate* isolate) | 666 bool V8PromiseCustom::isPromise(v8::Handle<v8::Value> maybePromise, v8::Isolate* isolate) |
| 612 { | 667 { |
| 613 WrapperWorldType currentWorldType = worldType(isolate); | 668 WrapperWorldType currentWorldType = worldType(isolate); |
| 614 return V8Promise::GetTemplate(isolate, currentWorldType)->HasInstance(maybeP romise); | 669 return V8Promise::GetTemplate(isolate, currentWorldType)->HasInstance(maybeP romise); |
| 615 } | 670 } |
| 616 | 671 |
| 617 v8::Local<v8::Object> V8PromiseCustom::toPromise(v8::Handle<v8::Value> maybeProm ise, v8::Isolate* isolate) | 672 v8::Local<v8::Object> V8PromiseCustom::toPromise(v8::Handle<v8::Value> maybeProm ise, v8::Isolate* isolate) |
| 618 { | 673 { |
| 619 // FIXME: Currently we dones't check [[PromiseConstructor]] since we limits | 674 // FIXME: Currently we dones't check [[PromiseConstructor]] since we limits |
| 620 // the creation of the promise objects only from the Blink Promise | 675 // the creation of the promise objects only from the Blink Promise |
| 621 // constructor. | 676 // constructor. |
| 622 if (isPromise(maybePromise, isolate)) | 677 if (isPromise(maybePromise, isolate)) |
| 623 return maybePromise.As<v8::Object>(); | 678 return maybePromise.As<v8::Object>(); |
| 624 | 679 |
| 625 v8::Local<v8::Object> promise = createPromise(v8::Handle<v8::Object>(), isol ate); | 680 v8::Local<v8::Object> promise = createPromise(v8::Handle<v8::Object>(), isol ate); |
| 626 resolve(promise, maybePromise, Asynchronous, isolate); | 681 resolve(promise, maybePromise, isolate); |
| 627 return promise; | 682 return promise; |
| 628 } | 683 } |
| 629 | 684 |
| 685 void V8PromiseCustom::resolve(v8::Handle<v8::Object> promise, v8::Handle<v8::Val ue> result, v8::Isolate* isolate) | |
| 686 { | |
| 687 ASSERT(!result.IsEmpty()); | |
| 688 v8::Local<v8::Object> internal = getInternal(promise); | |
| 689 PromiseState state = getState(internal); | |
| 690 if (state != Pending) | |
| 691 return; | |
| 692 | |
| 693 if (isPromise(result, isolate)) { | |
| 694 v8::Local<v8::Object> valuePromise = result.As<v8::Object>(); | |
| 695 v8::Local<v8::Object> valueInternal = getInternal(valuePromise); | |
| 696 PromiseState valueState = getState(valueInternal); | |
| 697 if (promise->SameValue(valuePromise)) { | |
| 698 v8::Local<v8::Value> reason = V8ThrowException::createTypeError("Res olve a promise with itself", isolate); | |
| 699 setReason(promise, reason, isolate); | |
| 700 } else if (valueState == Following) { | |
| 701 v8::Local<v8::Object> valuePromiseFollowing = valueInternal->GetInte rnalField(InternalResultIndex).As<v8::Object>(); | |
| 702 setState(internal, Following, valuePromiseFollowing, isolate); | |
| 703 addToDerived(getInternal(valuePromiseFollowing), promise, v8::Handle <v8::Function>(), v8::Handle<v8::Function>(), isolate); | |
| 704 } else if (valueState == Fulfilled) { | |
| 705 setValue(promise, valueInternal->GetInternalField(InternalResultInde x), isolate); | |
| 706 } else if (valueState == Rejected) { | |
| 707 setReason(promise, valueInternal->GetInternalField(InternalResultInd ex), isolate); | |
| 708 } else { | |
| 709 ASSERT(valueState == Pending); | |
| 710 setState(internal, Following, valuePromise, isolate); | |
| 711 addToDerived(valueInternal, promise, v8::Handle<v8::Function>(), v8: :Handle<v8::Function>(), isolate); | |
| 712 } | |
| 713 } else { | |
| 714 setValue(promise, result, isolate); | |
| 715 } | |
| 716 } | |
| 717 | |
| 718 void V8PromiseCustom::reject(v8::Handle<v8::Object> promise, v8::Handle<v8::Valu e> reason, v8::Isolate* isolate) | |
| 719 { | |
| 720 v8::Local<v8::Object> internal = getInternal(promise); | |
| 721 PromiseState state = getState(internal); | |
| 722 if (state != Pending) | |
| 723 return; | |
| 724 setReason(promise, reason, isolate); | |
| 725 } | |
| 726 | |
| 727 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) | |
| 728 { | |
| 729 v8::Handle<v8::Object> internal = getInternal(promise); | |
| 730 while (getState(internal) == Following) { | |
| 731 promise = internal->GetInternalField(InternalResultIndex).As<v8::Object> (); | |
| 732 internal = getInternal(promise); | |
| 733 } | |
| 734 // FIXME: Currently we dones't lookup "constructor" property since we limits | |
| 735 // the creation of the promise objects only from the Blink Promise | |
| 736 // constructor. | |
| 737 v8::Local<v8::Object> derivedPromise = createPromise(v8::Handle<v8::Object>( ), isolate); | |
| 738 updateDerivedFromPromise(derivedPromise, onFulfilled, onRejected, promise, i solate); | |
| 739 return derivedPromise; | |
| 740 } | |
| 741 | |
| 742 void V8PromiseCustom::setValue(v8::Handle<v8::Object> promise, v8::Handle<v8::Va lue> value, v8::Isolate* isolate) | |
| 743 { | |
| 744 PromisePropagator propagator; | |
| 745 propagator.setValue(promise, value, isolate); | |
| 746 propagator.performPropagation(isolate); | |
| 747 } | |
| 748 | |
| 749 void V8PromiseCustom::setReason(v8::Handle<v8::Object> promise, v8::Handle<v8::V alue> reason, v8::Isolate* isolate) | |
| 750 { | |
| 751 PromisePropagator propagator; | |
| 752 propagator.setReason(promise, reason, isolate); | |
| 753 propagator.performPropagation(isolate); | |
| 754 } | |
| 755 | |
| 756 void V8PromiseCustom::propagateToDerived(v8::Handle<v8::Object> promise, v8::Iso late* isolate) | |
| 757 { | |
| 758 PromisePropagator propagator; | |
| 759 propagator.propagateToDerived(promise, isolate); | |
| 760 propagator.performPropagation(isolate); | |
| 761 } | |
| 762 | |
| 763 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) | |
| 764 { | |
| 765 PromisePropagator propagator; | |
| 766 propagator.updateDerived(derivedPromise, onFulfilled, onRejected, originator , isolate); | |
| 767 propagator.performPropagation(isolate); | |
| 768 } | |
| 769 | |
| 770 void V8PromiseCustom::updateDerivedFromValue(v8::Handle<v8::Object> derivedPromi se, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Value> value, v8::Isola te* isolate) | |
| 771 { | |
| 772 PromisePropagator propagator; | |
| 773 propagator.updateDerivedFromValue(derivedPromise, onFulfilled, value, isolat e); | |
| 774 propagator.performPropagation(isolate); | |
| 775 } | |
| 776 | |
| 777 void V8PromiseCustom::updateDerivedFromReason(v8::Handle<v8::Object> derivedProm ise, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Value> reason, v8::Isol ate* isolate) | |
| 778 { | |
| 779 PromisePropagator propagator; | |
| 780 propagator.updateDerivedFromReason(derivedPromise, onRejected, reason, isola te); | |
| 781 propagator.performPropagation(isolate); | |
| 782 } | |
| 783 | |
| 784 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) | |
| 785 { | |
| 786 PromisePropagator propagator; | |
| 787 propagator.updateDerivedFromPromise(derivedPromise, onFulfilled, onRejected, promise, isolate); | |
| 788 propagator.performPropagation(isolate); | |
| 789 } | |
| 790 | |
| 791 v8::Local<v8::Object> V8PromiseCustom::coerceThenable(v8::Handle<v8::Object> the nable, v8::Handle<v8::Function> then, v8::Isolate* isolate) | |
| 792 { | |
| 793 ASSERT(!thenable.IsEmpty()); | |
| 794 ASSERT(!then.IsEmpty()); | |
| 795 v8::Local<v8::Object> promise = createPromise(v8::Handle<v8::Object>(), isol ate); | |
| 796 v8::Handle<v8::Value> argv[] = { | |
| 797 createClosure(promiseResolveCallback, promise, isolate), | |
| 798 createClosure(promiseRejectCallback, promise, isolate) | |
| 799 }; | |
| 800 v8::TryCatch trycatch; | |
| 801 if (V8ScriptRunner::callFunction(then, getScriptExecutionContext(), thenable , WTF_ARRAY_LENGTH(argv), argv, isolate).IsEmpty()) { | |
| 802 reject(promise, trycatch.Exception(), isolate); | |
| 803 } | |
| 804 thenable->SetHiddenValue(V8HiddenPropertyName::thenableHiddenPromise(isolate ), promise); | |
| 805 return promise; | |
| 806 } | |
| 807 | |
| 808 void V8PromiseCustom::callHandler(v8::Handle<v8::Object> promise, v8::Handle<v8: :Function> handler, v8::Handle<v8::Value> argument, v8::Isolate* isolate) | |
| 809 { | |
| 810 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext() ; | |
| 811 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextThread()); | |
| 812 scriptExecutionContext->postTask(adoptPtr(new CallHandlerTask(promise, handl er, argument, isolate))); | |
| 813 } | |
| 814 | |
| 630 } // namespace WebCore | 815 } // namespace WebCore |
| OLD | NEW |