| 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 "use strict"; | 5 "use strict"; |
| 6 | 6 |
| 7 // This file relies on the fact that the following declaration has been made | 7 // This file relies on the fact that the following declaration has been made |
| 8 // in runtime.js: | 8 // in runtime.js: |
| 9 // var $Object = global.Object | 9 // var $Object = global.Object |
| 10 // var $WeakMap = global.WeakMap | 10 // var $WeakMap = global.WeakMap |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // mirror-debugger.js currently uses builtins.promiseStatus. It would be nice | 22 // mirror-debugger.js currently uses builtins.promiseStatus. It would be nice |
| 23 // if we could move these property names into the closure below. | 23 // if we could move these property names into the closure below. |
| 24 // TODO(jkummerow/rossberg/yangguo): Find a better solution. | 24 // TODO(jkummerow/rossberg/yangguo): Find a better solution. |
| 25 | 25 |
| 26 // Status values: 0 = pending, +1 = resolved, -1 = rejected | 26 // Status values: 0 = pending, +1 = resolved, -1 = rejected |
| 27 var promiseStatus = GLOBAL_PRIVATE("Promise#status"); | 27 var promiseStatus = GLOBAL_PRIVATE("Promise#status"); |
| 28 var promiseValue = GLOBAL_PRIVATE("Promise#value"); | 28 var promiseValue = GLOBAL_PRIVATE("Promise#value"); |
| 29 var promiseOnResolve = GLOBAL_PRIVATE("Promise#onResolve"); | 29 var promiseOnResolve = GLOBAL_PRIVATE("Promise#onResolve"); |
| 30 var promiseOnReject = GLOBAL_PRIVATE("Promise#onReject"); | 30 var promiseOnReject = GLOBAL_PRIVATE("Promise#onReject"); |
| 31 var promiseRaw = GLOBAL_PRIVATE("Promise#raw"); | 31 var promiseRaw = GLOBAL_PRIVATE("Promise#raw"); |
| 32 var lastMicrotaskId = 0; |
| 32 | 33 |
| 33 (function() { | 34 (function() { |
| 34 | 35 |
| 35 var $Promise = function Promise(resolver) { | 36 var $Promise = function Promise(resolver) { |
| 36 if (resolver === promiseRaw) return; | 37 if (resolver === promiseRaw) return; |
| 37 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); | 38 if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); |
| 38 if (!IS_SPEC_FUNCTION(resolver)) | 39 if (!IS_SPEC_FUNCTION(resolver)) |
| 39 throw MakeTypeError('resolver_not_a_function', [resolver]); | 40 throw MakeTypeError('resolver_not_a_function', [resolver]); |
| 40 var promise = PromiseInit(this); | 41 var promise = PromiseInit(this); |
| 41 if (DEBUG_IS_ACTIVE) { | 42 if (DEBUG_IS_ACTIVE) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 64 return promise; | 65 return promise; |
| 65 } | 66 } |
| 66 | 67 |
| 67 function PromiseInit(promise) { | 68 function PromiseInit(promise) { |
| 68 return PromiseSet( | 69 return PromiseSet( |
| 69 promise, 0, UNDEFINED, new InternalArray, new InternalArray) | 70 promise, 0, UNDEFINED, new InternalArray, new InternalArray) |
| 70 } | 71 } |
| 71 | 72 |
| 72 function PromiseDone(promise, status, value, promiseQueue) { | 73 function PromiseDone(promise, status, value, promiseQueue) { |
| 73 if (GET_PRIVATE(promise, promiseStatus) === 0) { | 74 if (GET_PRIVATE(promise, promiseStatus) === 0) { |
| 74 PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue)); | 75 PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue), status); |
| 75 PromiseSet(promise, status, value); | 76 PromiseSet(promise, status, value); |
| 76 } | 77 } |
| 77 } | 78 } |
| 78 | 79 |
| 79 function PromiseCoerce(constructor, x) { | 80 function PromiseCoerce(constructor, x) { |
| 80 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { | 81 if (!IsPromise(x) && IS_SPEC_OBJECT(x)) { |
| 81 var then; | 82 var then; |
| 82 try { | 83 try { |
| 83 then = x.then; | 84 then = x.then; |
| 84 } catch(r) { | 85 } catch(r) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 %DebugPromiseHandlePrologue(function() { return deferred.promise }); | 117 %DebugPromiseHandlePrologue(function() { return deferred.promise }); |
| 117 deferred.reject(exception); | 118 deferred.reject(exception); |
| 118 } catch (e) { } finally { | 119 } catch (e) { } finally { |
| 119 %DebugPromiseHandleEpilogue(); | 120 %DebugPromiseHandleEpilogue(); |
| 120 } | 121 } |
| 121 } finally { | 122 } finally { |
| 122 %DebugPromiseHandleEpilogue(); | 123 %DebugPromiseHandleEpilogue(); |
| 123 } | 124 } |
| 124 } | 125 } |
| 125 | 126 |
| 126 function PromiseEnqueue(value, tasks) { | 127 function PromiseEnqueue(value, tasks, status) { |
| 128 var id, name, instrumenting = DEBUG_IS_ACTIVE; |
| 127 %EnqueueMicrotask(function() { | 129 %EnqueueMicrotask(function() { |
| 130 if (instrumenting) { |
| 131 %DebugAsyncTaskEvent({ type: "willHandle", id: id, name: name }); |
| 132 } |
| 128 for (var i = 0; i < tasks.length; i += 2) { | 133 for (var i = 0; i < tasks.length; i += 2) { |
| 129 PromiseHandle(value, tasks[i], tasks[i + 1]) | 134 PromiseHandle(value, tasks[i], tasks[i + 1]) |
| 130 } | 135 } |
| 136 if (instrumenting) { |
| 137 %DebugAsyncTaskEvent({ type: "didHandle", id: id, name: name }); |
| 138 } |
| 131 }); | 139 }); |
| 140 if (instrumenting) { |
| 141 id = ++lastMicrotaskId; |
| 142 name = status > 0 ? "Promise.Resolved" : "Promise.Rejected"; |
| 143 %DebugAsyncTaskEvent({ type: "enqueue", id: id, name: name }); |
| 144 } |
| 132 } | 145 } |
| 133 | 146 |
| 134 function PromiseIdResolveHandler(x) { return x } | 147 function PromiseIdResolveHandler(x) { return x } |
| 135 function PromiseIdRejectHandler(r) { throw r } | 148 function PromiseIdRejectHandler(r) { throw r } |
| 136 | 149 |
| 137 function PromiseNopResolver() {} | 150 function PromiseNopResolver() {} |
| 138 | 151 |
| 139 // ------------------------------------------------------------------- | 152 // ------------------------------------------------------------------- |
| 140 // Define exported functions. | 153 // Define exported functions. |
| 141 | 154 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 // Optimized case, avoid extra closure. | 205 // Optimized case, avoid extra closure. |
| 193 return PromiseSet(new $Promise(promiseRaw), -1, r); | 206 return PromiseSet(new $Promise(promiseRaw), -1, r); |
| 194 } else { | 207 } else { |
| 195 return new this(function(resolve, reject) { reject(r) }); | 208 return new this(function(resolve, reject) { reject(r) }); |
| 196 } | 209 } |
| 197 } | 210 } |
| 198 | 211 |
| 199 // Simple chaining. | 212 // Simple chaining. |
| 200 | 213 |
| 201 PromiseChain = function PromiseChain(onResolve, onReject) { // a.k.a. | 214 PromiseChain = function PromiseChain(onResolve, onReject) { // a.k.a. |
| 202 // flatMap | 215 // flatMap |
| 203 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; | 216 onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; |
| 204 onReject = IS_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; | 217 onReject = IS_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; |
| 205 var deferred = %_CallFunction(this.constructor, PromiseDeferred); | 218 var deferred = %_CallFunction(this.constructor, PromiseDeferred); |
| 206 switch (GET_PRIVATE(this, promiseStatus)) { | 219 switch (GET_PRIVATE(this, promiseStatus)) { |
| 207 case UNDEFINED: | 220 case UNDEFINED: |
| 208 throw MakeTypeError('not_a_promise', [this]); | 221 throw MakeTypeError('not_a_promise', [this]); |
| 209 case 0: // Pending | 222 case 0: // Pending |
| 210 GET_PRIVATE(this, promiseOnResolve).push(onResolve, deferred); | 223 GET_PRIVATE(this, promiseOnResolve).push(onResolve, deferred); |
| 211 GET_PRIVATE(this, promiseOnReject).push(onReject, deferred); | 224 GET_PRIVATE(this, promiseOnReject).push(onReject, deferred); |
| 212 break; | 225 break; |
| 213 case +1: // Resolved | 226 case +1: // Resolved |
| 214 PromiseEnqueue(GET_PRIVATE(this, promiseValue), [onResolve, deferred]); | 227 PromiseEnqueue(GET_PRIVATE(this, promiseValue), |
| 228 [onResolve, deferred], |
| 229 +1); |
| 215 break; | 230 break; |
| 216 case -1: // Rejected | 231 case -1: // Rejected |
| 217 PromiseEnqueue(GET_PRIVATE(this, promiseValue), [onReject, deferred]); | 232 PromiseEnqueue(GET_PRIVATE(this, promiseValue), |
| 233 [onReject, deferred], |
| 234 -1); |
| 218 break; | 235 break; |
| 219 } | 236 } |
| 220 return deferred.promise; | 237 return deferred.promise; |
| 221 } | 238 } |
| 222 | 239 |
| 223 PromiseCatch = function PromiseCatch(onReject) { | 240 PromiseCatch = function PromiseCatch(onReject) { |
| 224 return this.then(UNDEFINED, onReject); | 241 return this.then(UNDEFINED, onReject); |
| 225 } | 242 } |
| 226 | 243 |
| 227 // Multi-unwrapped chaining with thenable coercion. | 244 // Multi-unwrapped chaining with thenable coercion. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 "race", PromiseOne, | 330 "race", PromiseOne, |
| 314 "resolve", PromiseCast | 331 "resolve", PromiseCast |
| 315 ]); | 332 ]); |
| 316 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 333 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
| 317 "chain", PromiseChain, | 334 "chain", PromiseChain, |
| 318 "then", PromiseThen, | 335 "then", PromiseThen, |
| 319 "catch", PromiseCatch | 336 "catch", PromiseCatch |
| 320 ]); | 337 ]); |
| 321 | 338 |
| 322 })(); | 339 })(); |
| OLD | NEW |