Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Unified Diff: Source/bindings/v8/custom/V8PromiseCustom.cpp

Issue 17993004: Implement PromiseResolver.prototype.{fulfill, reject} (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/bindings/v8/custom/V8PromiseCustom.cpp
diff --git a/Source/bindings/v8/custom/V8PromiseCustom.cpp b/Source/bindings/v8/custom/V8PromiseCustom.cpp
index c9da71b9e89383867155271fdcf99798551f24dd..68817b7baab308faeccf96356a989a27207559b1 100644
--- a/Source/bindings/v8/custom/V8PromiseCustom.cpp
+++ b/Source/bindings/v8/custom/V8PromiseCustom.cpp
@@ -33,13 +33,76 @@
#include "V8Promise.h"
#include "V8PromiseResolver.h"
+#include "bindings/v8/ScopedPersistent.h"
+#include "bindings/v8/ScriptFunctionCall.h"
#include "bindings/v8/V8Binding.h"
+#include "bindings/v8/V8PerIsolateData.h"
#include "bindings/v8/V8ScriptRunner.h"
#include "bindings/v8/WrapperTypeInfo.h"
+#include "core/dom/Document.h"
+#include "core/dom/ExceptionCode.h"
+#include "core/page/DOMWindow.h"
+#include "core/platform/Task.h"
+#include "wtf/Functional.h"
+#include "wtf/PassOwnPtr.h"
#include <v8.h>
namespace WebCore {
+namespace {
+
+class PromiseTask : public ScriptExecutionContext::Task {
+public:
+ PromiseTask(v8::Handle<v8::Function> callback, v8::Handle<v8::Object> receiver, v8::Handle<v8::Value> result)
+ : m_callback(callback)
+ , m_receiver(receiver)
+ , m_result(result)
+ {
+ ASSERT(!callback.IsEmpty());
+ ASSERT(!receiver.IsEmpty());
+ ASSERT(!result.IsEmpty());
abarth-chromium 2013/06/27 06:34:01 You can check the m_ versions of these, if you lik
yhirano 2013/06/27 09:29:23 Done.
+ }
+ virtual ~PromiseTask() { }
+
+ virtual void performTask(ScriptExecutionContext*) OVERRIDE;
+
+private:
+ ScopedPersistent<v8::Function> m_callback;
+ ScopedPersistent<v8::Object> m_receiver;
+ ScopedPersistent<v8::Value> m_result;
+};
+
+void PromiseTask::performTask(ScriptExecutionContext* context)
+{
+ if (context->activeDOMObjectsAreStopped())
+ return;
+ ASSERT(context->isDocument());
haraken 2013/06/27 06:55:56 Nit: You can move this check to the head of this m
yhirano 2013/06/27 09:29:23 Done.
+ ScriptState* state = mainWorldScriptState(static_cast<Document*>(context)->frame());
+ v8::HandleScope handleScope;
+ ASSERT(state);
+ v8::Handle<v8::Context> v8Context = state->context();
+ ASSERT(!v8Context.IsEmpty());
+ v8::Context::Scope scope(v8Context);
+ v8::Isolate* isolate = v8Context->GetIsolate();
+ ASSERT(!m_callback.newLocal(isolate).IsEmpty());
+ ASSERT(!m_receiver.newLocal(isolate).IsEmpty());
+ ASSERT(!m_result.newLocal(isolate).IsEmpty());
abarth-chromium 2013/06/27 06:34:01 No need to call newLocal here. ScopedPersistent h
yhirano 2013/06/27 09:29:23 I deleted the ASSERTs.
+ v8::Handle<v8::Value> result = m_result.newLocal(isolate);
+ V8ScriptRunner::callFunction(m_callback.newLocal(isolate), context, m_receiver.newLocal(isolate), 1, &result);
haraken 2013/06/27 06:55:56 Nit: We normally write this as follows: v8::Han
yhirano 2013/06/27 09:29:23 Done.
+};
+
+v8::Handle<v8::Value> postTask(v8::Handle<v8::Function> callback, v8::Handle<v8::Object> receiver, v8::Handle<v8::Value> value, v8::Isolate* isolate)
+{
+ DOMWindow* window = activeDOMWindow();
+ ASSERT(window);
+ Document* document = window->document();
+ ASSERT(document);
+ document->postTask(adoptPtr(new PromiseTask(callback, receiver, value)));
+ return v8::Undefined(isolate);
+}
+
+} // namespace
+
void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8SetReturnValue(args, v8::Local<v8::Value>());
@@ -48,30 +111,186 @@ void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& arg
throwTypeError("Promise constructor takes a function argument", isolate);
return;
}
- v8::Handle<v8::Function> init = args[0].As<v8::Function>();
+ v8::Local<v8::Function> init = args[0].As<v8::Function>();
+ v8::Local<v8::Object> promise, resolver;
+ V8PromiseCustom::createPromise(args.Holder(), &promise, &resolver, isolate);
+ v8::Handle<v8::Value> argv[] = {
+ resolver,
+ };
+ v8::TryCatch trycatch;
+ if (V8ScriptRunner::callFunction(init, getScriptExecutionContext(), promise, WTF_ARRAY_LENGTH(argv), argv).IsEmpty()) {
+ // An exception is thrown. Reject the promise.
+ V8PromiseCustom::rejectResolver(resolver, trycatch.Exception(), V8PromiseCustom::Asynchronous, isolate);
+ }
+ v8SetReturnValue(args, promise);
+ return;
+}
+
+//
+// -- V8PromiseCustom --
+void V8PromiseCustom::createPromise(v8::Handle<v8::Object> creationContext, v8::Local<v8::Object>* promise, v8::Local<v8::Object>* resolver, v8::Isolate* isolate)
+{
+ // FIXME: v8::ObjectTemplate::New should be cached.
v8::Local<v8::ObjectTemplate> internalTemplate = v8::ObjectTemplate::New();
- internalTemplate->SetInternalFieldCount(V8PromiseCustom::InternalFieldCount);
+ internalTemplate->SetInternalFieldCount(InternalFieldCount);
v8::Local<v8::Object> internal = internalTemplate->NewInstance();
- v8::Local<v8::Object> promise = V8DOMWrapper::createWrapper(args.Holder(), &V8Promise::info, 0, isolate);
- v8::Local<v8::Object> promiseResolver = V8DOMWrapper::createWrapper(args.Holder(), &V8PromiseResolver::info, 0, isolate);
+ *promise = V8DOMWrapper::createWrapper(creationContext, &V8Promise::info, 0, isolate);
+ *resolver = V8DOMWrapper::createWrapper(creationContext, &V8PromiseResolver::info, 0, isolate);
+
+ clearInternal(internal, V8PromiseCustom::Pending, v8::Undefined(isolate));
+
+ (*promise)->SetInternalField(v8DOMWrapperObjectIndex, internal);
+ (*resolver)->SetInternalField(v8DOMWrapperObjectIndex, internal);
+}
+
+void V8PromiseCustom::fulfillResolver(v8::Handle<v8::Object> resolver, v8::Handle<v8::Value> result, SynchronousMode mode, v8::Isolate* isolate)
+{
+ ASSERT(!resolver.IsEmpty());
+ ASSERT(!result.IsEmpty());
+ v8::Local<v8::Object> internal = getInternal(resolver);
+ if (internal.IsEmpty() || getState(internal) != Pending) {
+ // The resolver is already fulfilled or rejected.
+ return;
+ }
+
+ v8::Local<v8::Value> value;
+ value = internal->GetInternalField(V8PromiseCustom::InternalFulfillCallbackIndex);
abarth-chromium 2013/06/27 06:34:01 You can combine these lines.
yhirano 2013/06/27 09:29:23 Done.
+ ASSERT(!value.IsEmpty());
+ ASSERT(value->IsArray());
+ v8::Local<v8::Array> callbacks = value.As<v8::Array>();
+ clearInternal(internal, Fulfilled, result);
+ // The internal object is no more needed in this PromiseResolver.
+ resolver->SetInternalField(v8DOMWrapperObjectIndex, v8::Null(isolate));
+
+ v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global();
+ for (uint32_t i = 0, length = callbacks->Length(); i < length; ++i) {
+ value = callbacks->Get(i);
+ ASSERT(!value.IsEmpty());
+ ASSERT(value->IsFunction());
+ v8::Local<v8::Function> callback = value.As<v8::Function>();
+ call(callback, global, result, mode, isolate);
abarth-chromium 2013/06/27 06:34:01 Do we need to check for exceptions?
yhirano 2013/06/27 09:29:23 V8PromiseCustom::call doesn't throws an exception,
+ }
+}
+
+void V8PromiseCustom::rejectResolver(v8::Handle<v8::Object> resolver, v8::Handle<v8::Value> result, SynchronousMode mode, v8::Isolate* isolate)
abarth-chromium 2013/06/27 06:34:01 Can we share more code between this function and f
yhirano 2013/06/27 09:29:23 Done.
+{
+ ASSERT(!resolver.IsEmpty());
+ ASSERT(!result.IsEmpty());
+ v8::Local<v8::Object> internal = getInternal(resolver);
+ if (internal.IsEmpty() || getState(internal) != Pending) {
+ // The resolver is already fulfilled or rejected.
+ return;
+ }
+
+ v8::Local<v8::Value> value;
+ value = internal->GetInternalField(V8PromiseCustom::InternalRejectCallbackIndex);
+ ASSERT(!value.IsEmpty());
+ ASSERT(value->IsArray());
+ v8::Local<v8::Array> callbacks = value.As<v8::Array>();
+ clearInternal(internal, Rejected, result);
+ // The internal object is no more needed in this PromiseResolver.
+ resolver->SetInternalField(v8DOMWrapperObjectIndex, v8::Null(isolate));
+
+ v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global();
+ for (uint32_t i = 0, length = callbacks->Length(); i < length; ++i) {
+ value = callbacks->Get(i);
+ ASSERT(!value.IsEmpty());
+ ASSERT(value->IsFunction());
+ v8::Local<v8::Function> callback = value.As<v8::Function>();
+ call(callback, global, result, mode, isolate);
+ }
+}
+
+void V8PromiseCustom::append(v8::Handle<v8::Object> promise, v8::Handle<v8::Function> fulfillCallback, v8::Handle<v8::Function> rejectCallback, v8::Isolate* isolate)
+{
+ v8::Local<v8::Object> internal = getInternal(promise);
+ ASSERT(!internal.IsEmpty());
+
+ PromiseState state = getState(internal);
+ if (state == Fulfilled) {
+ if (!fulfillCallback.IsEmpty()) {
+ v8::Local<v8::Value> result = internal->GetInternalField(V8PromiseCustom::InternalResultIndex);
+ v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global();
+ call(fulfillCallback, global, result, Asynchronous, isolate);
+ }
+ return;
+ }
+ if (state == Rejected) {
+ if (!rejectCallback.IsEmpty()) {
+ v8::Local<v8::Value> result = internal->GetInternalField(V8PromiseCustom::InternalResultIndex);
+ v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global();
+ call(rejectCallback, global, result, Asynchronous, isolate);
+ }
+ return;
+ }
- internal->SetInternalField(V8PromiseCustom::InternalStateIndex, v8::NumberObject::New(V8PromiseCustom::Pending));
- internal->SetInternalField(V8PromiseCustom::InternalResultIndex, v8::Undefined());
+ ASSERT(state == Pending);
+ if (!fulfillCallback.IsEmpty()) {
+ v8::Local<v8::Value> value = internal->GetInternalField(InternalFulfillCallbackIndex);
+ ASSERT(!value.IsEmpty());
+ ASSERT(value->IsArray());
+ v8::Local<v8::Array> callbacks = value.As<v8::Array>();
+ callbacks->Set(callbacks->Length(), fulfillCallback);
+ }
+ if (!rejectCallback.IsEmpty()) {
+ v8::Local<v8::Value> value = internal->GetInternalField(InternalRejectCallbackIndex);
+ ASSERT(!value.IsEmpty());
+ ASSERT(value->IsArray());
+ v8::Local<v8::Array> callbacks = value.As<v8::Array>();
+ callbacks->Set(callbacks->Length(), rejectCallback);
+ }
+}
+
+v8::Local<v8::Object> V8PromiseCustom::getInternal(v8::Handle<v8::Object> promiseOrPromiseResolver)
+{
+ ASSERT(!promiseOrPromiseResolver.IsEmpty());
+ v8::Local<v8::Value> value = promiseOrPromiseResolver->GetInternalField(v8DOMWrapperObjectIndex);
+ ASSERT(!value.IsEmpty());
+ if (value->IsNull())
haraken 2013/06/27 06:55:56 What if value is undefined?
yhirano 2013/06/27 09:29:23 Null was the special value that indicates the inte
+ return v8::Local<v8::Object>();
+ ASSERT(value->IsObject());
+ return value.As<v8::Object>();
+}
+
+void V8PromiseCustom::clearInternal(v8::Handle<v8::Object> internal, PromiseState state, v8::Handle<v8::Value> value)
+{
+ ASSERT(!internal.IsEmpty());
+
+ setState(internal, state);
+ internal->SetInternalField(V8PromiseCustom::InternalResultIndex, value);
internal->SetInternalField(V8PromiseCustom::InternalFulfillCallbackIndex, v8::Array::New());
internal->SetInternalField(V8PromiseCustom::InternalRejectCallbackIndex, v8::Array::New());
+}
- promise->SetInternalField(v8DOMWrapperObjectIndex, internal);
- promiseResolver->SetInternalField(v8DOMWrapperObjectIndex, internal);
+V8PromiseCustom::PromiseState V8PromiseCustom::getState(v8::Handle<v8::Object> internal)
+{
+ ASSERT(!internal.IsEmpty());
+ v8::Handle<v8::Value> value = internal->GetInternalField(V8PromiseCustom::InternalStateIndex);
+ ASSERT(!value.IsEmpty());
+ ASSERT(value->IsNumber());
+ double number = value.As<v8::Number>()->Value();
abarth-chromium 2013/06/27 06:34:01 Why not use a v8::Integer ? We shouldn't have to
haraken 2013/06/27 06:55:56 You can use toInt32() or some utility methods in V
yhirano 2013/06/27 09:29:23 Done.
+ ASSERT(number == Pending || number == Fulfilled || number == Rejected);
+ return static_cast<PromiseState>(number);
+}
- v8::Handle<v8::Value> argv[] = {
- promiseResolver,
- };
- v8::TryCatch trycatch;
- if (V8ScriptRunner::callFunction(init, getScriptExecutionContext(), promise, WTF_ARRAY_LENGTH(argv), argv).IsEmpty()) {
- // FIXME: An exception is thrown. Reject the promise.
+void V8PromiseCustom::setState(v8::Handle<v8::Object> internal, PromiseState state)
+{
+ ASSERT(!internal.IsEmpty());
+ ASSERT(state == Pending || state == Fulfilled || state == Rejected);
+ internal->SetInternalField(V8PromiseCustom::InternalStateIndex, v8::Number::New(state));
+}
+
+void V8PromiseCustom::call(v8::Handle<v8::Function> function, v8::Handle<v8::Object> receiver, v8::Handle<v8::Value> result, SynchronousMode mode, v8::Isolate* isolate)
+{
+ ASSERT(!function.IsEmpty());
+ ASSERT(!receiver.IsEmpty());
+ if (mode == Synchronous) {
+ v8::TryCatch trycatch;
abarth-chromium 2013/06/27 06:34:01 I see. We catch the exception and ignore it.
yhirano 2013/06/27 09:29:23 I added a comment.
+ V8ScriptRunner::callFunction(function, getScriptExecutionContext(), receiver, 1, &result);
haraken 2013/06/27 06:55:56 Nit: WTF_ARRAY_LENGTH(args).
yhirano 2013/06/27 09:29:23 Done.
+ } else {
+ ASSERT(mode == Asynchronous);
+ postTask(function, receiver, result, isolate);
}
- v8SetReturnValue(args, promise);
- return;
}
} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698