OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function(global, utils, extrasUtils) { | 5 (function(global, utils, extrasUtils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 // trigger redundant ExceptionEvents | 42 // trigger redundant ExceptionEvents |
43 var deferred = %new_promise_capability(this, false); | 43 var deferred = %new_promise_capability(this, false); |
44 var resolutions = new InternalArray(); | 44 var resolutions = new InternalArray(); |
45 var count; | 45 var count; |
46 | 46 |
47 // For catch prediction, don't treat the .then calls as handling it; | 47 // For catch prediction, don't treat the .then calls as handling it; |
48 // instead, recurse outwards. | 48 // instead, recurse outwards. |
49 var instrumenting = DEBUG_IS_ACTIVE; | 49 var instrumenting = DEBUG_IS_ACTIVE; |
50 if (instrumenting) { | 50 if (instrumenting) { |
51 SET_PRIVATE(deferred.reject, promiseForwardingHandlerSymbol, true); | 51 SET_PRIVATE(deferred.reject, promiseForwardingHandlerSymbol, true); |
52 %DebugSetNonBreakableAsyncEvents(true); | |
Yang
2017/01/30 13:30:14
This is not particular elegant. Especially if Prom
kozy
2017/01/30 16:58:00
Done.
| |
52 } | 53 } |
53 | 54 |
54 function CreateResolveElementFunction(index, values, promiseCapability) { | 55 function CreateResolveElementFunction(index, values, promiseCapability) { |
55 var alreadyCalled = false; | 56 var alreadyCalled = false; |
56 return (x) => { | 57 return (x) => { |
57 if (alreadyCalled === true) return; | 58 if (alreadyCalled === true) return; |
58 alreadyCalled = true; | 59 alreadyCalled = true; |
59 values[index] = x; | 60 values[index] = x; |
60 if (--count === 0) { | 61 if (--count === 0) { |
61 var valuesArray = []; | 62 var valuesArray = []; |
(...skipping 23 matching lines...) Expand all Loading... | |
85 // 6.d | 86 // 6.d |
86 if (--count === 0) { | 87 if (--count === 0) { |
87 var valuesArray = []; | 88 var valuesArray = []; |
88 %MoveArrayContents(resolutions, valuesArray); | 89 %MoveArrayContents(resolutions, valuesArray); |
89 %_Call(deferred.resolve, UNDEFINED, valuesArray); | 90 %_Call(deferred.resolve, UNDEFINED, valuesArray); |
90 } | 91 } |
91 | 92 |
92 } catch (e) { | 93 } catch (e) { |
93 %_Call(deferred.reject, UNDEFINED, e); | 94 %_Call(deferred.reject, UNDEFINED, e); |
94 } | 95 } |
96 if (instrumenting) { | |
97 %DebugSetNonBreakableAsyncEvents(false); | |
98 } | |
95 return deferred.promise; | 99 return deferred.promise; |
96 } | 100 } |
97 | 101 |
98 // ES#sec-promise.race | 102 // ES#sec-promise.race |
99 // Promise.race ( iterable ) | 103 // Promise.race ( iterable ) |
100 function PromiseRace(iterable) { | 104 function PromiseRace(iterable) { |
101 if (!IS_RECEIVER(this)) { | 105 if (!IS_RECEIVER(this)) { |
102 throw %make_type_error(kCalledOnNonObject, PromiseRace); | 106 throw %make_type_error(kCalledOnNonObject, PromiseRace); |
103 } | 107 } |
104 | 108 |
105 // false debugEvent so that forwarding the rejection through race does not | 109 // false debugEvent so that forwarding the rejection through race does not |
106 // trigger redundant ExceptionEvents | 110 // trigger redundant ExceptionEvents |
107 var deferred = %new_promise_capability(this, false); | 111 var deferred = %new_promise_capability(this, false); |
108 | 112 |
109 // For catch prediction, don't treat the .then calls as handling it; | 113 // For catch prediction, don't treat the .then calls as handling it; |
110 // instead, recurse outwards. | 114 // instead, recurse outwards. |
111 var instrumenting = DEBUG_IS_ACTIVE; | 115 var instrumenting = DEBUG_IS_ACTIVE; |
112 if (instrumenting) { | 116 if (instrumenting) { |
113 SET_PRIVATE(deferred.reject, promiseForwardingHandlerSymbol, true); | 117 SET_PRIVATE(deferred.reject, promiseForwardingHandlerSymbol, true); |
118 %DebugSetNonBreakableAsyncEvents(true); | |
114 } | 119 } |
115 | 120 |
116 try { | 121 try { |
117 for (var value of iterable) { | 122 for (var value of iterable) { |
118 var throwawayPromise = this.resolve(value).then(deferred.resolve, | 123 var throwawayPromise = this.resolve(value).then(deferred.resolve, |
119 deferred.reject); | 124 deferred.reject); |
120 // For catch prediction, mark that rejections here are semantically | 125 // For catch prediction, mark that rejections here are semantically |
121 // handled by the combined Promise. | 126 // handled by the combined Promise. |
122 if (instrumenting && %is_promise(throwawayPromise)) { | 127 if (instrumenting && %is_promise(throwawayPromise)) { |
123 SET_PRIVATE(throwawayPromise, promiseHandledBySymbol, deferred.promise); | 128 SET_PRIVATE(throwawayPromise, promiseHandledBySymbol, deferred.promise); |
124 } | 129 } |
125 } | 130 } |
126 } catch (e) { | 131 } catch (e) { |
127 %_Call(deferred.reject, UNDEFINED, e); | 132 %_Call(deferred.reject, UNDEFINED, e); |
128 } | 133 } |
134 if (instrumenting) { | |
135 %DebugSetNonBreakableAsyncEvents(false); | |
136 } | |
129 return deferred.promise; | 137 return deferred.promise; |
130 } | 138 } |
131 | 139 |
132 // ------------------------------------------------------------------- | 140 // ------------------------------------------------------------------- |
133 // Install exported functions. | 141 // Install exported functions. |
134 | 142 |
135 utils.InstallFunctions(GlobalPromise, DONT_ENUM, [ | 143 utils.InstallFunctions(GlobalPromise, DONT_ENUM, [ |
136 "all", PromiseAll, | 144 "all", PromiseAll, |
137 "race", PromiseRace, | 145 "race", PromiseRace, |
138 ]); | 146 ]); |
139 | 147 |
140 %InstallToContext([ | 148 %InstallToContext([ |
141 "promise_id_resolve_handler", PromiseIdResolveHandler, | 149 "promise_id_resolve_handler", PromiseIdResolveHandler, |
142 "promise_id_reject_handler", PromiseIdRejectHandler | 150 "promise_id_reject_handler", PromiseIdRejectHandler |
143 ]); | 151 ]); |
144 | 152 |
145 }) | 153 }) |
OLD | NEW |