f4a09d415ac93dd675fbb01218eac6bca73bd1b8
[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, handler)
39 {
40     "use strict";
41
42     return {
43         @capabilities: capability,
44         @handler: handler
45     };
46 }
47
48 @globalPrivate
49 function newPromiseCapability(constructor)
50 {
51     "use strict";
52
53     if (!@isConstructor(constructor))
54         @throwTypeError("promise capability requires a constructor function");
55
56     var promiseCapability = {
57         @promise: @undefined,
58         @resolve: @undefined,
59         @reject: @undefined
60     };
61
62     function executor(resolve, reject)
63     {
64         if (promiseCapability.@resolve !== @undefined)
65             @throwTypeError("resolve function is already set");
66         if (promiseCapability.@reject !== @undefined)
67             @throwTypeError("reject function is already set");
68
69         promiseCapability.@resolve = resolve;
70         promiseCapability.@reject = reject;
71     }
72
73     var promise = new constructor(executor);
74
75     if (typeof promiseCapability.@resolve !== "function")
76         @throwTypeError("executor did not take a resolve function");
77
78     if (typeof promiseCapability.@reject !== "function")
79         @throwTypeError("executor did not take a reject function");
80
81     promiseCapability.@promise = promise;
82
83     return promiseCapability;
84 }
85
86 @globalPrivate
87 function triggerPromiseReactions(reactions, argument)
88 {
89     "use strict";
90
91     for (var index = 0, length = reactions.length; index < length; ++index)
92         @enqueueJob(@promiseReactionJob, [reactions[index], argument]);
93 }
94
95 @globalPrivate
96 function rejectPromise(promise, reason)
97 {
98     "use strict";
99
100     var reactions = promise.@promiseRejectReactions;
101     promise.@promiseResult = reason;
102     promise.@promiseFulfillReactions = @undefined;
103     promise.@promiseRejectReactions = @undefined;
104     promise.@promiseState = @promiseStateRejected;
105
106     @InspectorInstrumentation.promiseRejected(promise, reason, reactions);
107
108     @triggerPromiseReactions(reactions, reason);
109 }
110
111 @globalPrivate
112 function fulfillPromise(promise, value)
113 {
114     "use strict";
115
116     var reactions = promise.@promiseFulfillReactions;
117     promise.@promiseResult = value;
118     promise.@promiseFulfillReactions = @undefined;
119     promise.@promiseRejectReactions = @undefined;
120     promise.@promiseState = @promiseStateFulfilled;
121
122     @InspectorInstrumentation.promiseFulfilled(promise, value, reactions);
123
124     @triggerPromiseReactions(reactions, value);
125 }
126
127 @globalPrivate
128 function createResolvingFunctions(promise)
129 {
130     "use strict";
131
132     var alreadyResolved = false;
133
134     var resolve = function (resolution) {
135         if (alreadyResolved)
136             return @undefined;
137         alreadyResolved = true;
138
139         if (resolution === promise)
140             return @rejectPromise(promise, new @TypeError("Resolve a promise with itself"));
141
142         if (!@isObject(resolution))
143             return @fulfillPromise(promise, resolution);
144
145         var then;
146         try {
147             then = resolution.then;
148         } catch (error) {
149             return @rejectPromise(promise, error);
150         }
151
152         if (typeof then !== 'function')
153             return @fulfillPromise(promise, resolution);
154
155         @enqueueJob(@promiseResolveThenableJob, [promise, resolution, then]);
156
157         return @undefined;
158     };
159
160     var reject = function (reason) {
161         if (alreadyResolved)
162             return @undefined;
163         alreadyResolved = true;
164
165         return @rejectPromise(promise, reason);
166     };
167
168     return {
169         @resolve: resolve,
170         @reject: reject
171     };
172 }
173
174 @globalPrivate
175 function promiseReactionJob(reaction, argument)
176 {
177     "use strict";
178
179     var promiseCapability = reaction.@capabilities;
180
181     var result;
182     try {
183         result = reaction.@handler.@call(@undefined, 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.@promiseFulfillReactions = [];
215     this.@promiseRejectReactions = [];
216
217     var resolvingFunctions = @createResolvingFunctions(this);
218     try {
219         executor(resolvingFunctions.@resolve, resolvingFunctions.@reject);
220     } catch (error) {
221         return resolvingFunctions.@reject.@call(@undefined, error);
222     }
223
224     return this;
225 }