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 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #ifndef V8PromiseCustom_h 26 #define V8PromiseCustom_h 27 28 #include "bindings/v8/WrapperTypeInfo.h" 29 30 #include <v8.h> 31 32 namespace WebCore { 33 34 class V8PromiseCustom { 35 public: 36 enum InternalFieldIndex { 37 InternalStateIndex, 38 InternalResultIndex, 39 InternalFulfillCallbackIndex, 40 InternalRejectCallbackIndex, 41 InternalDerivedPromiseIndex, 42 InternalFieldCount, // This entry must always be at the bottom. 43 }; 44 45 enum PromiseAllEnvironmentFieldIndex { 46 PromiseAllEnvironmentPromiseIndex, 47 PromiseAllEnvironmentCountdownIndex, 48 PromiseAllEnvironmentIndexIndex, 49 PromiseAllEnvironmentResultsIndex, 50 PromiseAllEnvironmentFieldCount, // This entry must always be at the bottom. 51 }; 52 53 enum PrimitiveWrapperFieldIndex { 54 PrimitiveWrapperPrimitiveIndex, 55 PrimitiveWrapperFieldCount, // This entry must always be at the bottom. 56 }; 57 58 enum PromiseState { 59 Pending, 60 Fulfilled, 61 Rejected, 62 Following, 63 }; 64 65 static v8::Local<v8::Object> createPromise(v8::Handle<v8::Object> creationContext, v8::Isolate*); 66 67 // |promise| must be a Promise instance. 68 static v8::Local<v8::Object> getInternal(v8::Handle<v8::Object> promise); 69 70 // |internal| must be a Promise internal object. 71 static PromiseState getState(v8::Handle<v8::Object> internal); 72 73 // |internal| must be a Promise internal object. 74 // Set a |promise|'s state and result that correspond to the state. 75 static void setState(v8::Handle<v8::Object> internal, PromiseState, v8::Handle<v8::Value>, v8::Isolate*); 76 77 // Return true if |maybePromise| is a Promise instance. 78 static bool isPromise(v8::Handle<v8::Value> maybePromise, v8::Isolate*); 79 80 // Coerces |maybePromise| to a Promise instance. 81 static v8::Local<v8::Object> toPromise(v8::Handle<v8::Value> maybePromise, v8::Isolate*); 82 83 // |promise| must be a Promise instance. 84 static void resolve(v8::Handle<v8::Object> promise, v8::Handle<v8::Value> result, v8::Isolate*); 85 86 // |promise| must be a Promise instance. 87 static void reject(v8::Handle<v8::Object> promise, v8::Handle<v8::Value> result, v8::Isolate*); 88 89 // |promise| must be a Promise instance. 90 // |onFulfilled| and |onRejected| can be an empty value respectively. 91 // Appends |onFulfilled| and/or |onRejected| handlers to |promise|. 92 static v8::Local<v8::Object> then(v8::Handle<v8::Object> promise, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Isolate*); 93 94 // |promise| must be a Promise instance. 95 // Set a |promise|'s value and propagate it to derived promises. 96 static void setValue(v8::Handle<v8::Object> promise, v8::Handle<v8::Value>, v8::Isolate*); 97 98 // |promise| must be a Promise instance. 99 // Set a |promise|'s failure reason and propagate it to derived promises. 100 static void setReason(v8::Handle<v8::Object> promise, v8::Handle<v8::Value>, v8::Isolate*); 101 102 // |promise| must be a Promise instance. 103 // Propagate a |promise|'s value or reason to all of its derived promies. 104 static void propagateToDerived(v8::Handle<v8::Object> promise, v8::Isolate*); 105 106 // |derivedPromise| and |originator| must be a Promise instance. 107 // |onFulfilled| and |onRejected| can be an empty value respectively. 108 // Propagate |originator|'s state to |derivedPromise|. 109 static void updateDerived(v8::Handle<v8::Object> derivedPromise, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Object> originator, v8::Isolate*); 110 111 // |derivedPromise| must be a Promise instance. 112 // Propagate a value to |derivedPromise|. 113 static void updateDerivedFromValue(v8::Handle<v8::Object> derivedPromise, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Value>, v8::Isolate*); 114 115 // |derivedPromise| must be a Promise instance. 116 // Propagate a failure reason to |derivedPromise|. 117 static void updateDerivedFromReason(v8::Handle<v8::Object> derivedPromise, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Value>, v8::Isolate*); 118 119 // |derivedPromise| and |promise| must be a Promise instance. 120 // |onFulfilled| and |onRejected| can be an empty value respectively. 121 // Propagate |promise|'s state to |derivedPromise|. 122 static void updateDerivedFromPromise(v8::Handle<v8::Object> derivedPromise, v8::Handle<v8::Function> onFulfilled, v8::Handle<v8::Function> onRejected, v8::Handle<v8::Object> promise, v8::Isolate*); 123 124 // Returns a Promise instance that will be fulfilled or rejected by 125 // |thenable|'s result. 126 static v8::Local<v8::Object> coerceThenable(v8::Handle<v8::Object> thenable, v8::Handle<v8::Function> then, v8::Isolate*); 127 128 // |promise| must be a Promise instance. 129 // Applies a transformation to an argument and use it to update derived 130 // promies. 131 static void callHandler(v8::Handle<v8::Object> promise, v8::Handle<v8::Function> handler, v8::Handle<v8::Value> argument, v8::Isolate*); 132 }; 133 134 } // namespace WebCore 135 136 #endif // V8PromiseCustom_h 137