OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "V8Promise.h" |
| 33 |
| 34 #include "V8PromiseResolver.h" |
| 35 #include "bindings/v8/V8Binding.h" |
| 36 #include "bindings/v8/V8PromiseUtilities.h" |
| 37 #include "bindings/v8/V8ScriptRunner.h" |
| 38 #include <v8.h> |
| 39 |
| 40 namespace WebCore { |
| 41 void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& arg
s) |
| 42 { |
| 43 if (args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsFunction()) { |
| 44 args.GetReturnValue().Set(V8ThrowException::throwError(v8TypeError, "Pro
mise constructor takes exact one function parameter.", args.GetIsolate())); |
| 45 return; |
| 46 } |
| 47 args.GetReturnValue().Set(v8::Handle<v8::Value>()); |
| 48 v8::Handle<v8::Function> init = args[0].As<v8::Function>(); |
| 49 v8::Handle<v8::Function> promiseConstructor = V8PromiseUtilities::promiseCon
structor(args.GetIsolate()); |
| 50 |
| 51 if (promiseConstructor.IsEmpty()) |
| 52 return; |
| 53 v8::Handle<v8::Function> promiseResolverConstructor = V8PromiseUtilities::pr
omiseResolverConstructor(args.GetIsolate()); |
| 54 if (promiseResolverConstructor.IsEmpty()) |
| 55 return; |
| 56 |
| 57 // Ignore the constructor parameter. Promise internal constructor doen't tak
e a parameter. |
| 58 v8::Handle<v8::Value> promise = promiseConstructor->CallAsConstructor(0, 0); |
| 59 if (promise.IsEmpty() || !promise->IsObject()) |
| 60 return; |
| 61 v8::Handle<v8::Object> promiseWrapper = V8DOMWrapper::createWrapper(args.Hol
der(), &V8Promise::info, 0, args.GetIsolate()); |
| 62 promiseWrapper->SetInternalField(v8DOMWrapperObjectIndex, promise); |
| 63 |
| 64 v8::Handle<v8::Value> associated = promise.As<v8::Object>()->Get(v8::String:
:NewSymbol("associated")); |
| 65 if (associated.IsEmpty()) |
| 66 return; |
| 67 v8::Handle<v8::Value> resolver = promiseResolverConstructor->CallAsConstruct
or(1, &associated); |
| 68 if (resolver.IsEmpty()) |
| 69 return; |
| 70 v8::Handle<v8::Object> resolverWrapper = V8DOMWrapper::createWrapper(args.Ho
lder(), &V8PromiseResolver::info, 0, args.GetIsolate()); |
| 71 resolverWrapper->SetInternalField(v8DOMWrapperObjectIndex, resolver); |
| 72 |
| 73 v8::Handle<v8::Value> initArgs[] = { |
| 74 resolverWrapper |
| 75 }; |
| 76 |
| 77 v8::TryCatch trycatch; |
| 78 v8::Handle<v8::Value> r = init->Call(promiseWrapper, sizeof(initArgs) / size
of(initArgs[0]), &initArgs[0]); |
| 79 if (r.IsEmpty()) { |
| 80 fprintf(stderr, "An exception is thrown.\n"); |
| 81 // An exception is thrown. |
| 82 v8::Handle<v8::Value> exception = trycatch.Exception(); |
| 83 V8PromiseUtilities::callUnwrappedMethod("reject", resolverWrapper, 1, &e
xception, |
| 84 args.GetIsolate(), V8PromiseUtilities::promiseResolverPrototype(args
.GetIsolate())); |
| 85 } |
| 86 args.GetReturnValue().Set(promiseWrapper); |
| 87 } |
| 88 |
| 89 void V8Promise::fulfillMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& a
rgs) |
| 90 { |
| 91 v8SetReturnValue(args, V8PromiseUtilities::callStatic("fulfill", args, V8Pro
miseUtilities::promiseConstructor(args.GetIsolate()))); |
| 92 } |
| 93 |
| 94 void V8Promise::resolveMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& a
rgs) |
| 95 { |
| 96 v8SetReturnValue(args, V8PromiseUtilities::callStatic("resolve", args, V8Pro
miseUtilities::promiseConstructor(args.GetIsolate()))); |
| 97 } |
| 98 |
| 99 void V8Promise::rejectMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& ar
gs) |
| 100 { |
| 101 v8SetReturnValue(args, V8PromiseUtilities::callStatic("reject", args, V8Prom
iseUtilities::promiseConstructor(args.GetIsolate()))); |
| 102 } |
| 103 |
| 104 void V8Promise::anyMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
| 105 { |
| 106 v8SetReturnValue(args, V8PromiseUtilities::callStatic("any", args, V8Promise
Utilities::promiseConstructor(args.GetIsolate()))); |
| 107 } |
| 108 |
| 109 void V8Promise::everyMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& arg
s) |
| 110 { |
| 111 v8SetReturnValue(args, V8PromiseUtilities::callStatic("every", args, V8Promi
seUtilities::promiseConstructor(args.GetIsolate()))); |
| 112 } |
| 113 |
| 114 void V8Promise::someMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args
) |
| 115 { |
| 116 v8SetReturnValue(args, V8PromiseUtilities::callStatic("some", args, V8Promis
eUtilities::promiseConstructor(args.GetIsolate()))); |
| 117 } |
| 118 |
| 119 void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args
) |
| 120 { |
| 121 v8SetReturnValue(args, V8PromiseUtilities::callUnwrappedMethod("then", args,
V8PromiseUtilities::promisePrototype(args.GetIsolate()))); |
| 122 } |
| 123 |
| 124 void V8Promise::catchMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& arg
s) |
| 125 { |
| 126 v8SetReturnValue(args, V8PromiseUtilities::callUnwrappedMethod("catch", args
, V8PromiseUtilities::promisePrototype(args.GetIsolate()))); |
| 127 } |
| 128 |
| 129 } // namespace WebCore |
| 130 |
OLD | NEW |