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

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

Issue 17505004: Introduce Promises. (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
« no previous file with comments | « Source/bindings/v8/custom/V8PromiseCustom.h ('k') | Source/bindings/v8/custom/V8PromiseResolverCustom.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/bindings/v8/custom/V8PromiseCustom.cpp
diff --git a/Source/bindings/v8/custom/V8PromiseCustom.cpp b/Source/bindings/v8/custom/V8PromiseCustom.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..cd40750a75817c68892153127da2864a84174063
--- /dev/null
+++ b/Source/bindings/v8/custom/V8PromiseCustom.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2013 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "bindings/v8/custom/V8PromiseCustom.h"
+
+#include "V8Promise.h"
+#include "V8PromiseResolver.h"
+#include "bindings/v8/V8Binding.h"
+#include "bindings/v8/V8ScriptRunner.h"
+#include "bindings/v8/WrapperTypeInfo.h"
+#include <v8.h>
+
+namespace WebCore {
+
+void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
+{
+ args.GetReturnValue().Set(v8::Local<v8::Value>());
abarth-chromium 2013/06/21 05:38:46 There isn't a SetReturnValue function?
yhirano 2013/06/21 09:36:53 Thanks, done.
+ v8::Isolate* isolate = args.GetIsolate();
+ if (args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsFunction()) {
haraken 2013/06/21 05:47:58 This should be 'args.Length() < 1'.
yhirano 2013/06/21 09:36:53 Done.
+ throwTypeError("Promise constructor takes exact one function argument", isolate);
+ return;
+ }
+ v8::Handle<v8::Function> init = args[0].As<v8::Function>();
+ v8::Local<v8::ObjectTemplate> associatedTemplate = v8::ObjectTemplate::New();
abarth-chromium 2013/06/21 05:38:46 Normally we create the object template in V8PerIso
yhirano 2013/06/21 09:36:53 We can attach FunctionTemplate data to V8PerIsolat
+ associatedTemplate->SetInternalFieldCount(V8PromiseCustom::AssociatedInternalFieldCount);
+ v8::Local<v8::Object> associated = associatedTemplate->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);
haraken 2013/06/21 05:47:58 Don't you need to store a C++ object pointer to th
abarth-chromium 2013/06/21 05:58:53 The "associated" object isn't a wrapper. It's jus
yhirano 2013/06/21 09:36:53 No, instead, we set a JavaScript object as an inte
+
+ associated->SetInternalField(V8PromiseCustom::AssociatedStatusIndex, v8::NumberObject::New(V8PromiseCustom::Pending));
abarth-chromium 2013/06/21 05:38:46 These plain numbers we can keep on the CPP side.
+ associated->SetInternalField(V8PromiseCustom::AssociatedResultIndex, v8::Undefined());
+ associated->SetInternalField(V8PromiseCustom::AssociatedFulfillCallbackIndex, v8::Array::New());
+ associated->SetInternalField(V8PromiseCustom::AssociatedRejectCallbackIndex, v8::Array::New());
+
+ promise->SetInternalField(v8DOMWrapperObjectIndex, associated);
+ promiseResolver->SetInternalField(v8DOMWrapperObjectIndex, associated);
abarth-chromium 2013/06/21 05:38:46 These two object share the same associated storage
yhirano 2013/06/21 09:36:53 Hmm... There is "state" property in the spec (I ca
+
+ v8::Handle<v8::Value> argv[] = {
+ promiseResolver,
+ };
+ v8::TryCatch trycatch;
+ if (init->Call(promise, sizeof(argv) / sizeof(argv[0]), argv).IsEmpty()) {
abarth-chromium 2013/06/21 05:38:46 WTF_ARRAY_LENGTH(argv)
haraken 2013/06/21 05:47:58 You can use V8ScriptRunner::callFunction().
abarth-chromium 2013/06/21 05:58:53 Ah, sorry I missed that!
yhirano 2013/06/21 09:36:53 Done.
+ // FIXME: An exception is thrown. Reject the promise.
+ }
+ args.GetReturnValue().Set(promise);
+ return;
+}
+
+} // namespace WebCore
« no previous file with comments | « Source/bindings/v8/custom/V8PromiseCustom.h ('k') | Source/bindings/v8/custom/V8PromiseResolverCustom.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698