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..7b1e0b8a6d9ffb1d265a4aa8dd61e38e640516f1 |
--- /dev/null |
+++ b/Source/bindings/v8/custom/V8PromiseCustom.cpp |
@@ -0,0 +1,130 @@ |
+/* |
+ * 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 "V8Promise.h" |
+ |
+#include "V8PromiseResolver.h" |
+#include "bindings/v8/V8Binding.h" |
+#include "bindings/v8/V8PromiseUtilities.h" |
+#include "bindings/v8/V8ScriptRunner.h" |
+#include <v8.h> |
+ |
+namespace WebCore { |
+void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
+{ |
+ if (args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsFunction()) { |
+ args.GetReturnValue().Set(V8ThrowException::throwError(v8TypeError, "Promise constructor takes exact one function parameter.", args.GetIsolate())); |
+ return; |
+ } |
+ args.GetReturnValue().Set(v8::Handle<v8::Value>()); |
+ v8::Handle<v8::Function> init = args[0].As<v8::Function>(); |
+ v8::Handle<v8::Function> promiseConstructor = V8PromiseUtilities::promiseConstructor(args.GetIsolate()); |
+ |
+ if (promiseConstructor.IsEmpty()) |
+ return; |
+ v8::Handle<v8::Function> promiseResolverConstructor = V8PromiseUtilities::promiseResolverConstructor(args.GetIsolate()); |
+ if (promiseResolverConstructor.IsEmpty()) |
+ return; |
+ |
+ // Ignore the constructor parameter. Promise internal constructor doen't take a parameter. |
+ v8::Handle<v8::Value> promise = promiseConstructor->CallAsConstructor(0, 0); |
+ if (promise.IsEmpty() || !promise->IsObject()) |
+ return; |
+ v8::Handle<v8::Object> promiseWrapper = V8DOMWrapper::createWrapper(args.Holder(), &V8Promise::info, 0, args.GetIsolate()); |
+ promiseWrapper->SetInternalField(v8DOMWrapperObjectIndex, promise); |
+ |
+ v8::Handle<v8::Value> associated = promise.As<v8::Object>()->Get(v8::String::NewSymbol("associated")); |
+ if (associated.IsEmpty()) |
+ return; |
+ v8::Handle<v8::Value> resolver = promiseResolverConstructor->CallAsConstructor(1, &associated); |
+ if (resolver.IsEmpty()) |
+ return; |
+ v8::Handle<v8::Object> resolverWrapper = V8DOMWrapper::createWrapper(args.Holder(), &V8PromiseResolver::info, 0, args.GetIsolate()); |
+ resolverWrapper->SetInternalField(v8DOMWrapperObjectIndex, resolver); |
+ |
+ v8::Handle<v8::Value> initArgs[] = { |
+ resolverWrapper |
+ }; |
+ |
+ v8::TryCatch trycatch; |
+ v8::Handle<v8::Value> r = init->Call(promiseWrapper, sizeof(initArgs) / sizeof(initArgs[0]), &initArgs[0]); |
+ if (r.IsEmpty()) { |
+ fprintf(stderr, "An exception is thrown.\n"); |
+ // An exception is thrown. |
+ v8::Handle<v8::Value> exception = trycatch.Exception(); |
+ V8PromiseUtilities::callUnwrappedMethod("reject", resolverWrapper, 1, &exception, |
+ args.GetIsolate(), V8PromiseUtilities::promiseResolverPrototype(args.GetIsolate())); |
+ } |
+ args.GetReturnValue().Set(promiseWrapper); |
+} |
+ |
+void V8Promise::fulfillMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
+{ |
+ v8SetReturnValue(args, V8PromiseUtilities::callStatic("fulfill", args, V8PromiseUtilities::promiseConstructor(args.GetIsolate()))); |
+} |
+ |
+void V8Promise::resolveMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
+{ |
+ v8SetReturnValue(args, V8PromiseUtilities::callStatic("resolve", args, V8PromiseUtilities::promiseConstructor(args.GetIsolate()))); |
+} |
+ |
+void V8Promise::rejectMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
+{ |
+ v8SetReturnValue(args, V8PromiseUtilities::callStatic("reject", args, V8PromiseUtilities::promiseConstructor(args.GetIsolate()))); |
+} |
+ |
+void V8Promise::anyMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
+{ |
+ v8SetReturnValue(args, V8PromiseUtilities::callStatic("any", args, V8PromiseUtilities::promiseConstructor(args.GetIsolate()))); |
+} |
+ |
+void V8Promise::everyMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
+{ |
+ v8SetReturnValue(args, V8PromiseUtilities::callStatic("every", args, V8PromiseUtilities::promiseConstructor(args.GetIsolate()))); |
+} |
+ |
+void V8Promise::someMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
+{ |
+ v8SetReturnValue(args, V8PromiseUtilities::callStatic("some", args, V8PromiseUtilities::promiseConstructor(args.GetIsolate()))); |
+} |
+ |
+void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
+{ |
+ v8SetReturnValue(args, V8PromiseUtilities::callUnwrappedMethod("then", args, V8PromiseUtilities::promisePrototype(args.GetIsolate()))); |
+} |
+ |
+void V8Promise::catchMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
+{ |
+ v8SetReturnValue(args, V8PromiseUtilities::callUnwrappedMethod("catch", args, V8PromiseUtilities::promisePrototype(args.GetIsolate()))); |
+} |
+ |
+} // namespace WebCore |
+ |