| 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 // Only used by async-await.js | 72 // Only used by async-await.js |
| 73 function RejectPromise(promise, reason, debugEvent) { | 73 function RejectPromise(promise, reason, debugEvent) { |
| 74 %PromiseReject(promise, reason, debugEvent); | 74 %PromiseReject(promise, reason, debugEvent); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Export to bindings | 77 // Export to bindings |
| 78 function DoRejectPromise(promise, reason) { | 78 function DoRejectPromise(promise, reason) { |
| 79 %PromiseReject(promise, reason, true); | 79 %PromiseReject(promise, reason, true); |
| 80 } | 80 } |
| 81 | 81 |
| 82 // ES#sec-newpromisecapability | |
| 83 // NewPromiseCapability ( C ) | |
| 84 function NewPromiseCapability(C, debugEvent) { | |
| 85 if (C === GlobalPromise) { | |
| 86 // Optimized case, avoid extra closure. | |
| 87 var promise = %promise_internal_constructor(UNDEFINED); | |
| 88 // TODO(gsathya): Remove container for callbacks when this is | |
| 89 // moved to CPP/TF. | |
| 90 var callbacks = %create_resolving_functions(promise, debugEvent); | |
| 91 return { | |
| 92 promise: promise, | |
| 93 resolve: callbacks[kResolveCallback], | |
| 94 reject: callbacks[kRejectCallback] | |
| 95 }; | |
| 96 } | |
| 97 | |
| 98 var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED }; | |
| 99 result.promise = new C((resolve, reject) => { | |
| 100 if (!IS_UNDEFINED(result.resolve) || !IS_UNDEFINED(result.reject)) | |
| 101 throw %make_type_error(kPromiseExecutorAlreadyInvoked); | |
| 102 result.resolve = resolve; | |
| 103 result.reject = reject; | |
| 104 }); | |
| 105 | |
| 106 if (!IS_CALLABLE(result.resolve) || !IS_CALLABLE(result.reject)) | |
| 107 throw %make_type_error(kPromiseNonCallable); | |
| 108 | |
| 109 return result; | |
| 110 } | |
| 111 | |
| 112 // ES#sec-promise.reject | 82 // ES#sec-promise.reject |
| 113 // Promise.reject ( x ) | 83 // Promise.reject ( x ) |
| 114 function PromiseReject(r) { | 84 function PromiseReject(r) { |
| 115 if (!IS_RECEIVER(this)) { | 85 if (!IS_RECEIVER(this)) { |
| 116 throw %make_type_error(kCalledOnNonObject, PromiseReject); | 86 throw %make_type_error(kCalledOnNonObject, PromiseReject); |
| 117 } | 87 } |
| 118 if (this === GlobalPromise) { | 88 if (this === GlobalPromise) { |
| 119 // Optimized case, avoid extra closure. | 89 // Optimized case, avoid extra closure. |
| 120 var promise = %promise_create_and_set(kRejected, r); | 90 var promise = %promise_create_and_set(kRejected, r); |
| 121 // Trigger debug events if the debugger is on, as Promise.reject is | 91 // Trigger debug events if the debugger is on, as Promise.reject is |
| 122 // equivalent to throwing an exception directly. | 92 // equivalent to throwing an exception directly. |
| 123 %PromiseRejectEventFromStack(promise, r); | 93 %PromiseRejectEventFromStack(promise, r); |
| 124 return promise; | 94 return promise; |
| 125 } else { | 95 } else { |
| 126 var promiseCapability = NewPromiseCapability(this, true); | 96 var promiseCapability = %new_promise_capability(this, true); |
| 127 %_Call(promiseCapability.reject, UNDEFINED, r); | 97 %_Call(promiseCapability.reject, UNDEFINED, r); |
| 128 return promiseCapability.promise; | 98 return promiseCapability.promise; |
| 129 } | 99 } |
| 130 } | 100 } |
| 131 | 101 |
| 132 // Combinators. | 102 // Combinators. |
| 133 | 103 |
| 134 // ES#sec-promise.resolve | 104 // ES#sec-promise.resolve |
| 135 // Promise.resolve ( x ) | 105 // Promise.resolve ( x ) |
| 136 function PromiseResolve(x) { | 106 function PromiseResolve(x) { |
| 137 if (!IS_RECEIVER(this)) { | 107 if (!IS_RECEIVER(this)) { |
| 138 throw %make_type_error(kCalledOnNonObject, PromiseResolve); | 108 throw %make_type_error(kCalledOnNonObject, PromiseResolve); |
| 139 } | 109 } |
| 140 if (%is_promise(x) && x.constructor === this) return x; | 110 if (%is_promise(x) && x.constructor === this) return x; |
| 141 | 111 |
| 142 // Avoid creating resolving functions. | 112 // Avoid creating resolving functions. |
| 143 if (this === GlobalPromise) { | 113 if (this === GlobalPromise) { |
| 144 var promise = %promise_internal_constructor(UNDEFINED); | 114 var promise = %promise_internal_constructor(UNDEFINED); |
| 145 %promise_resolve(promise, x); | 115 %promise_resolve(promise, x); |
| 146 return promise; | 116 return promise; |
| 147 } | 117 } |
| 148 | 118 |
| 149 // debugEvent is not so meaningful here as it will be resolved | 119 // debugEvent is not so meaningful here as it will be resolved |
| 150 var promiseCapability = NewPromiseCapability(this, true); | 120 var promiseCapability = %new_promise_capability(this, true); |
| 151 %_Call(promiseCapability.resolve, UNDEFINED, x); | 121 %_Call(promiseCapability.resolve, UNDEFINED, x); |
| 152 return promiseCapability.promise; | 122 return promiseCapability.promise; |
| 153 } | 123 } |
| 154 | 124 |
| 155 // ES#sec-promise.all | 125 // ES#sec-promise.all |
| 156 // Promise.all ( iterable ) | 126 // Promise.all ( iterable ) |
| 157 function PromiseAll(iterable) { | 127 function PromiseAll(iterable) { |
| 158 if (!IS_RECEIVER(this)) { | 128 if (!IS_RECEIVER(this)) { |
| 159 throw %make_type_error(kCalledOnNonObject, "Promise.all"); | 129 throw %make_type_error(kCalledOnNonObject, "Promise.all"); |
| 160 } | 130 } |
| 161 | 131 |
| 162 // false debugEvent so that forwarding the rejection through all does not | 132 // false debugEvent so that forwarding the rejection through all does not |
| 163 // trigger redundant ExceptionEvents | 133 // trigger redundant ExceptionEvents |
| 164 var deferred = NewPromiseCapability(this, false); | 134 var deferred = %new_promise_capability(this, false); |
| 165 var resolutions = new InternalArray(); | 135 var resolutions = new InternalArray(); |
| 166 var count; | 136 var count; |
| 167 | 137 |
| 168 // For catch prediction, don't treat the .then calls as handling it; | 138 // For catch prediction, don't treat the .then calls as handling it; |
| 169 // instead, recurse outwards. | 139 // instead, recurse outwards. |
| 170 var instrumenting = DEBUG_IS_ACTIVE; | 140 var instrumenting = DEBUG_IS_ACTIVE; |
| 171 if (instrumenting) { | 141 if (instrumenting) { |
| 172 SET_PRIVATE(deferred.reject, promiseForwardingHandlerSymbol, true); | 142 SET_PRIVATE(deferred.reject, promiseForwardingHandlerSymbol, true); |
| 173 } | 143 } |
| 174 | 144 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 188 |
| 219 // ES#sec-promise.race | 189 // ES#sec-promise.race |
| 220 // Promise.race ( iterable ) | 190 // Promise.race ( iterable ) |
| 221 function PromiseRace(iterable) { | 191 function PromiseRace(iterable) { |
| 222 if (!IS_RECEIVER(this)) { | 192 if (!IS_RECEIVER(this)) { |
| 223 throw %make_type_error(kCalledOnNonObject, PromiseRace); | 193 throw %make_type_error(kCalledOnNonObject, PromiseRace); |
| 224 } | 194 } |
| 225 | 195 |
| 226 // false debugEvent so that forwarding the rejection through race does not | 196 // false debugEvent so that forwarding the rejection through race does not |
| 227 // trigger redundant ExceptionEvents | 197 // trigger redundant ExceptionEvents |
| 228 var deferred = NewPromiseCapability(this, false); | 198 var deferred = %new_promise_capability(this, false); |
| 229 | 199 |
| 230 // For catch prediction, don't treat the .then calls as handling it; | 200 // For catch prediction, don't treat the .then calls as handling it; |
| 231 // instead, recurse outwards. | 201 // instead, recurse outwards. |
| 232 var instrumenting = DEBUG_IS_ACTIVE; | 202 var instrumenting = DEBUG_IS_ACTIVE; |
| 233 if (instrumenting) { | 203 if (instrumenting) { |
| 234 SET_PRIVATE(deferred.reject, promiseForwardingHandlerSymbol, true); | 204 SET_PRIVATE(deferred.reject, promiseForwardingHandlerSymbol, true); |
| 235 } | 205 } |
| 236 | 206 |
| 237 try { | 207 try { |
| 238 for (var value of iterable) { | 208 for (var value of iterable) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 263 "race", PromiseRace, | 233 "race", PromiseRace, |
| 264 "resolve", PromiseResolve | 234 "resolve", PromiseResolve |
| 265 ]); | 235 ]); |
| 266 | 236 |
| 267 %InstallToContext([ | 237 %InstallToContext([ |
| 268 "promise_create", PromiseCreate, | 238 "promise_create", PromiseCreate, |
| 269 "promise_reject", DoRejectPromise, | 239 "promise_reject", DoRejectPromise, |
| 270 // TODO(gsathya): Remove this once we update the promise builtin. | 240 // TODO(gsathya): Remove this once we update the promise builtin. |
| 271 "promise_internal_reject", RejectPromise, | 241 "promise_internal_reject", RejectPromise, |
| 272 "promise_debug_get_info", PromiseDebugGetInfo, | 242 "promise_debug_get_info", PromiseDebugGetInfo, |
| 273 "new_promise_capability", NewPromiseCapability, | |
| 274 "promise_id_resolve_handler", PromiseIdResolveHandler, | 243 "promise_id_resolve_handler", PromiseIdResolveHandler, |
| 275 "promise_id_reject_handler", PromiseIdRejectHandler | 244 "promise_id_reject_handler", PromiseIdRejectHandler |
| 276 ]); | 245 ]); |
| 277 | 246 |
| 278 // This allows extras to create promises quickly without building extra | 247 // This allows extras to create promises quickly without building extra |
| 279 // resolve/reject closures, and allows them to later resolve and reject any | 248 // resolve/reject closures, and allows them to later resolve and reject any |
| 280 // promise without having to hold on to those closures forever. | 249 // promise without having to hold on to those closures forever. |
| 281 utils.InstallFunctions(extrasUtils, 0, [ | 250 utils.InstallFunctions(extrasUtils, 0, [ |
| 282 "createPromise", PromiseCreate, | 251 "createPromise", PromiseCreate, |
| 283 "rejectPromise", DoRejectPromise, | 252 "rejectPromise", DoRejectPromise, |
| 284 "markPromiseAsHandled", MarkPromiseAsHandled | 253 "markPromiseAsHandled", MarkPromiseAsHandled |
| 285 ]); | 254 ]); |
| 286 | 255 |
| 287 utils.Export(function(to) { | 256 utils.Export(function(to) { |
| 288 to.PromiseCreate = PromiseCreate; | 257 to.PromiseCreate = PromiseCreate; |
| 289 | |
| 290 to.RejectPromise = RejectPromise; | 258 to.RejectPromise = RejectPromise; |
| 291 }); | 259 }); |
| 292 | 260 |
| 293 }) | 261 }) |
| OLD | NEW |