OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 | 28 |
29 "use strict"; | 29 "use strict"; |
30 | 30 |
31 // This file relies on the fact that the following declaration has been made | 31 // This file relies on the fact that the following declaration has been made |
32 // in runtime.js: | 32 // in runtime.js: |
33 // var $Object = global.Object | 33 // var $Object = global.Object |
34 // var $WeakMap = global.WeakMap | 34 // var $WeakMap = global.WeakMap |
35 | 35 |
36 | 36 |
37 var $Promise = function Promise(resolver) { | 37 var $Promise = Promise; |
38 if (resolver === promiseRaw) return; | |
39 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); | |
40 if (typeof resolver !== 'function') | |
41 throw MakeTypeError('resolver_not_a_function', [resolver]); | |
42 var promise = PromiseInit(this); | |
43 try { | |
44 resolver(function(x) { PromiseResolve(promise, x) }, | |
45 function(r) { PromiseReject(promise, r) }); | |
46 } catch (e) { | |
47 PromiseReject(promise, e); | |
48 } | |
49 } | |
50 | 38 |
51 | 39 |
52 //------------------------------------------------------------------- | 40 //------------------------------------------------------------------- |
53 | 41 |
54 // Core functionality. | 42 // Core functionality. |
55 | 43 |
56 // Status values: 0 = pending, +1 = resolved, -1 = rejected | 44 // Status values: 0 = pending, +1 = resolved, -1 = rejected |
57 var promiseStatus = GLOBAL_PRIVATE("Promise#status"); | 45 var promiseStatus = GLOBAL_PRIVATE("Promise#status"); |
58 var promiseValue = GLOBAL_PRIVATE("Promise#value"); | 46 var promiseValue = GLOBAL_PRIVATE("Promise#value"); |
59 var promiseOnResolve = GLOBAL_PRIVATE("Promise#onResolve"); | 47 var promiseOnResolve = GLOBAL_PRIVATE("Promise#onResolve"); |
60 var promiseOnReject = GLOBAL_PRIVATE("Promise#onReject"); | 48 var promiseOnReject = GLOBAL_PRIVATE("Promise#onReject"); |
61 var promiseRaw = GLOBAL_PRIVATE("Promise#raw"); | 49 var promiseRaw = GLOBAL_PRIVATE("Promise#raw"); |
62 | 50 |
63 function IsPromise(x) { | 51 function IsPromise(x) { |
64 return IS_SPEC_OBJECT(x) && %HasLocalProperty(x, promiseStatus); | 52 return IS_SPEC_OBJECT(x) && %HasLocalProperty(x, promiseStatus); |
65 } | 53 } |
66 | 54 |
| 55 function Promise(resolver) { |
| 56 if (resolver === promiseRaw) return; |
| 57 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); |
| 58 if (typeof resolver !== 'function') |
| 59 throw MakeTypeError('resolver_not_a_function', [resolver]); |
| 60 var promise = PromiseInit(this); |
| 61 try { |
| 62 resolver(function(x) { PromiseResolve(promise, x) }, |
| 63 function(r) { PromiseReject(promise, r) }); |
| 64 } catch (e) { |
| 65 PromiseReject(promise, e); |
| 66 } |
| 67 } |
| 68 |
67 function PromiseSet(promise, status, value, onResolve, onReject) { | 69 function PromiseSet(promise, status, value, onResolve, onReject) { |
68 SET_PRIVATE(promise, promiseStatus, status); | 70 SET_PRIVATE(promise, promiseStatus, status); |
69 SET_PRIVATE(promise, promiseValue, value); | 71 SET_PRIVATE(promise, promiseValue, value); |
70 SET_PRIVATE(promise, promiseOnResolve, onResolve); | 72 SET_PRIVATE(promise, promiseOnResolve, onResolve); |
71 SET_PRIVATE(promise, promiseOnReject, onReject); | 73 SET_PRIVATE(promise, promiseOnReject, onReject); |
72 return promise; | 74 return promise; |
73 } | 75 } |
74 | 76 |
75 function PromiseInit(promise) { | 77 function PromiseInit(promise) { |
76 return PromiseSet(promise, 0, UNDEFINED, new InternalArray, new InternalArray) | 78 return PromiseSet(promise, 0, UNDEFINED, new InternalArray, new InternalArray) |
(...skipping 13 matching lines...) Expand all Loading... |
90 function PromiseReject(promise, r) { | 92 function PromiseReject(promise, r) { |
91 PromiseDone(promise, -1, r, promiseOnReject) | 93 PromiseDone(promise, -1, r, promiseOnReject) |
92 } | 94 } |
93 | 95 |
94 | 96 |
95 // For API. | 97 // For API. |
96 | 98 |
97 function PromiseNopResolver() {} | 99 function PromiseNopResolver() {} |
98 | 100 |
99 function PromiseCreate() { | 101 function PromiseCreate() { |
100 return new $Promise(PromiseNopResolver) | 102 return new Promise(PromiseNopResolver) |
101 } | 103 } |
102 | 104 |
103 | 105 |
104 // Convenience. | 106 // Convenience. |
105 | 107 |
106 function PromiseDeferred() { | 108 function PromiseDeferred() { |
107 if (this === $Promise) { | 109 if (this === $Promise) { |
108 // Optimized case, avoid extra closure. | 110 // Optimized case, avoid extra closure. |
109 var promise = PromiseInit(new $Promise(promiseRaw)); | 111 var promise = PromiseInit(new Promise(promiseRaw)); |
110 return { | 112 return { |
111 promise: promise, | 113 promise: promise, |
112 resolve: function(x) { PromiseResolve(promise, x) }, | 114 resolve: function(x) { PromiseResolve(promise, x) }, |
113 reject: function(r) { PromiseReject(promise, r) } | 115 reject: function(r) { PromiseReject(promise, r) } |
114 }; | 116 }; |
115 } else { | 117 } else { |
116 var result = {}; | 118 var result = {}; |
117 result.promise = new this(function(resolve, reject) { | 119 result.promise = new this(function(resolve, reject) { |
118 result.resolve = resolve; | 120 result.resolve = resolve; |
119 result.reject = reject; | 121 result.reject = reject; |
120 }) | 122 }) |
121 return result; | 123 return result; |
122 } | 124 } |
123 } | 125 } |
124 | 126 |
125 function PromiseResolved(x) { | 127 function PromiseResolved(x) { |
126 if (this === $Promise) { | 128 if (this === $Promise) { |
127 // Optimized case, avoid extra closure. | 129 // Optimized case, avoid extra closure. |
128 return PromiseSet(new $Promise(promiseRaw), +1, x); | 130 return PromiseSet(new Promise(promiseRaw), +1, x); |
129 } else { | 131 } else { |
130 return new this(function(resolve, reject) { resolve(x) }); | 132 return new this(function(resolve, reject) { resolve(x) }); |
131 } | 133 } |
132 } | 134 } |
133 | 135 |
134 function PromiseRejected(r) { | 136 function PromiseRejected(r) { |
135 if (this === $Promise) { | 137 if (this === $Promise) { |
136 // Optimized case, avoid extra closure. | 138 // Optimized case, avoid extra closure. |
137 return PromiseSet(new $Promise(promiseRaw), -1, r); | 139 return PromiseSet(new Promise(promiseRaw), -1, r); |
138 } else { | 140 } else { |
139 return new this(function(resolve, reject) { reject(r) }); | 141 return new this(function(resolve, reject) { reject(r) }); |
140 } | 142 } |
141 } | 143 } |
142 | 144 |
143 | 145 |
144 // Simple chaining. | 146 // Simple chaining. |
145 | 147 |
146 function PromiseIdResolveHandler(x) { return x } | 148 function PromiseIdResolveHandler(x) { return x } |
147 function PromiseIdRejectHandler(r) { throw r } | 149 function PromiseIdRejectHandler(r) { throw r } |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 "resolve", PromiseCast | 319 "resolve", PromiseCast |
318 ]); | 320 ]); |
319 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 321 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
320 "chain", PromiseChain, | 322 "chain", PromiseChain, |
321 "then", PromiseThen, | 323 "then", PromiseThen, |
322 "catch", PromiseCatch | 324 "catch", PromiseCatch |
323 ]); | 325 ]); |
324 } | 326 } |
325 | 327 |
326 SetUpPromise(); | 328 SetUpPromise(); |
OLD | NEW |