[JSC] Merge PromiseReactions
[WebKit-https.git] / Source / JavaScriptCore / builtins / PromiseOperations.js
1 /*
2  * Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>.
3  * Copyright (C) 2016 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 // @internal
28
29 @globalPrivate
30 function isPromise(promise)
31 {
32     "use strict";
33
34     return @isObject(promise) && !!promise.@promiseState;
35 }
36
37 @globalPrivate
38 function newPromiseReaction(capability, onFulfilled, onRejected)
39 {
40     "use strict";
41
42     return {
43         @capabilities: capability,
44         @onFulfilled: onFulfilled,
45         @onRejected: onRejected,
46     };
47 }
48
49 @globalPrivate
50 function newPromiseCapability(constructor)
51 {
52     "use strict";
53
54     if (!@isConstructor(constructor))
55         @throwTypeError("promise capability requires a constructor function");
56
57     var promiseCapability = {
58         @promise: @undefined,
59         @resolve: @undefined,
60         @reject: @undefined
61     };
62
63     function executor(resolve, reject)
64     {
65         if (promiseCapability.@resolve !== @undefined)
66             @throwTypeError("resolve function is already set");
67         if (promiseCapability.@reject !== @undefined)
68             @throwTypeError("reject function is already set");
69
70         promiseCapability.@resolve = resolve;
71         promiseCapability.@reject = reject;
72     }
73
74     var promise = new constructor(executor);
75
76     if (typeof promiseCapability.@resolve !== "function")
77         @throwTypeError("executor did not take a resolve function");
78
79     if (typeof promiseCapability.@reject !== "function")
80         @throwTypeError("executor did not take a reject function");
81
82     promiseCapability.@promise = promise;
83
84     return promiseCapability;
85 }
86
87 @globalPrivate
88 function triggerPromiseReactions(state, reactions, argument)
89 {
90     "use strict";
91
92     for (var index = 0, length = reactions.length; index < length; ++index)
93         @enqueueJob(@promiseReactionJob, [state, reactions[index], argument]);
94 }
95
96 @globalPrivate
97 function rejectPromise(promise, reason)
98 {
99     "use strict";
100
101     var reactions = promise.@promiseReactions;
102     promise.@promiseResult = reason;
103     promise.@promiseReactions = @undefined;
104     promise.@promiseState = @promiseStateRejected;
105
106     @InspectorInstrumentation.promiseRejected(promise, reason, reactions);
107
108     @triggerPromiseReactions(@promiseStateRejected, reactions, reason);
109 }
110
111 @globalPrivate
112 function fulfillPromise(promise, value)
113 {
114     "use strict";
115
116     var reactions = promise.@promiseReactions;
117     promise.@promiseResult = value;
118     promise.@promiseReactions = @undefined;
119     promise.@promiseState = @promiseStateFulfilled;
120
121     @InspectorInstrumentation.promiseFulfilled(promise, value, reactions);
122
123     @triggerPromiseReactions(@promiseStateFulfilled, reactions, value);
124 }
125
126 @globalPrivate
127 function createResolvingFunctions(promise)
128 {
129     "use strict";
130
131     var alreadyResolved = false;
132
133     var resolve = function (resolution) {
134         if (alreadyResolved)
135             return @undefined;
136         alreadyResolved = true;
137
138         if (resolution === promise)
139             return @rejectPromise(promise, new @TypeError("Resolve a promise with itself"));
140
141         if (!@isObject(resolution))
142             return @fulfillPromise(promise, resolution);
143
144         var then;
145         try {
146             then = resolution.then;
147         } catch (error) {
148             return @rejectPromise(promise, error);
149         }
150
151         if (typeof then !== 'function')
152             return @fulfillPromise(promise, resolution);
153
154         @enqueueJob(@promiseResolveThenableJob, [promise, resolution, then]);
155
156         return @undefined;
157     };
158
159     var reject = function (reason) {
160         if (alreadyResolved)
161             return @undefined;
162         alreadyResolved = true;
163
164         return @rejectPromise(promise, reason);
165     };
166
167     return {
168         @resolve: resolve,
169         @reject: reject
170     };
171 }
172
173 @globalPrivate
174 function promiseReactionJob(state, reaction, argument)
175 {
176     "use strict";
177
178     var promiseCapability = reaction.@capabilities;
179
180     var result;
181     var handler = (state === @promiseStateFulfilled) ? reaction.@onFulfilled: reaction.@onRejected;
182     try {
183         result = handler(argument);
184     } catch (error) {
185         return promiseCapability.@reject.@call(@undefined, error);
186     }
187
188     return promiseCapability.@resolve.@call(@undefined, result);
189 }
190
191 @globalPrivate
192 function promiseResolveThenableJob(promiseToResolve, thenable, then)
193 {
194     "use strict";
195
196     var resolvingFunctions = @createResolvingFunctions(promiseToResolve);
197
198     try {
199         return then.@call(thenable, resolvingFunctions.@resolve, resolvingFunctions.@reject);
200     } catch (error) {
201         return resolvingFunctions.@reject.@call(@undefined, error);
202     }
203 }
204
205 @globalPrivate
206 function initializePromise(executor)
207 {
208     "use strict";
209
210     if (typeof executor !== 'function')
211         @throwTypeError("Promise constructor takes a function argument");
212
213     this.@promiseState = @promiseStatePending;
214     this.@promiseReactions = [];
215
216     var resolvingFunctions = @createResolvingFunctions(this);
217     try {
218         executor(resolvingFunctions.@resolve, resolvingFunctions.@reject);
219     } catch (error) {
220         return resolvingFunctions.@reject.@call(@undefined, error);
221     }
222
223     return this;
224 }