| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 function PromiseSet(promise, status, value, onResolve, onReject) { | 69 function PromiseSet(promise, status, value, onResolve, onReject) { |
| 70 SET_PRIVATE(promise, promiseStatus, status); | 70 SET_PRIVATE(promise, promiseStatus, status); |
| 71 SET_PRIVATE(promise, promiseValue, value); | 71 SET_PRIVATE(promise, promiseValue, value); |
| 72 SET_PRIVATE(promise, promiseOnResolve, onResolve); | 72 SET_PRIVATE(promise, promiseOnResolve, onResolve); |
| 73 SET_PRIVATE(promise, promiseOnReject, onReject); | 73 SET_PRIVATE(promise, promiseOnReject, onReject); |
| 74 return promise; | 74 return promise; |
| 75 } | 75 } |
| 76 | 76 |
| 77 function PromiseInit(promise) { | 77 function PromiseInit(promise) { |
| 78 return PromiseSet(promise, 0, UNDEFINED, new InternalArray, new InternalArray) | 78 return PromiseSet( |
| 79 promise, 0, UNDEFINED, new InternalArray, new InternalArray); |
| 79 } | 80 } |
| 80 | 81 |
| 81 function PromiseDone(promise, status, value, promiseQueue) { | 82 function PromiseDone(promise, status, value, promiseQueue) { |
| 82 if (GET_PRIVATE(promise, promiseStatus) === 0) { | 83 if (GET_PRIVATE(promise, promiseStatus) === 0) { |
| 83 PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue)); | 84 PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue)); |
| 84 PromiseSet(promise, status, value); | 85 PromiseSet(promise, status, value); |
| 85 } | 86 } |
| 86 } | 87 } |
| 87 | 88 |
| 88 function PromiseResolve(promise, x) { | 89 function PromiseResolve(promise, x) { |
| 89 PromiseDone(promise, +1, x, promiseOnResolve) | 90 PromiseDone(promise, +1, x, promiseOnResolve) |
| 90 } | 91 } |
| 91 | 92 |
| 92 function PromiseReject(promise, r) { | 93 function PromiseReject(promise, r) { |
| 93 PromiseDone(promise, -1, r, promiseOnReject) | 94 PromiseDone(promise, -1, r, promiseOnReject) |
| 94 } | 95 } |
| 95 | 96 |
| 96 | 97 |
| 97 // For API. | 98 // For API. |
| 98 | 99 |
| 99 function PromiseNopResolver() {} | 100 function PromiseNopResolver() {} |
| 100 | 101 |
| 101 function PromiseCreate() { | 102 function PromiseCreate() { |
| 102 return new Promise(PromiseNopResolver) | 103 return new $Promise(PromiseNopResolver) |
| 103 } | 104 } |
| 104 | 105 |
| 105 | 106 |
| 106 // Convenience. | 107 // Convenience. |
| 107 | 108 |
| 108 function PromiseDeferred() { | 109 function PromiseDeferred() { |
| 109 if (this === $Promise) { | 110 if (this === $Promise) { |
| 110 // Optimized case, avoid extra closure. | 111 // Optimized case, avoid extra closure. |
| 111 var promise = PromiseInit(new Promise(promiseRaw)); | 112 var promise = PromiseInit(new $Promise(promiseRaw)); |
| 112 return { | 113 return { |
| 113 promise: promise, | 114 promise: promise, |
| 114 resolve: function(x) { PromiseResolve(promise, x) }, | 115 resolve: function(x) { PromiseResolve(promise, x) }, |
| 115 reject: function(r) { PromiseReject(promise, r) } | 116 reject: function(r) { PromiseReject(promise, r) } |
| 116 }; | 117 }; |
| 117 } else { | 118 } else { |
| 118 var result = {}; | 119 var result = {}; |
| 119 result.promise = new this(function(resolve, reject) { | 120 result.promise = new this(function(resolve, reject) { |
| 120 result.resolve = resolve; | 121 result.resolve = resolve; |
| 121 result.reject = reject; | 122 result.reject = reject; |
| 122 }) | 123 }); |
| 123 return result; | 124 return result; |
| 124 } | 125 } |
| 125 } | 126 } |
| 126 | 127 |
| 127 function PromiseResolved(x) { | 128 function PromiseResolved(x) { |
| 128 if (this === $Promise) { | 129 if (this === $Promise) { |
| 129 // Optimized case, avoid extra closure. | 130 // Optimized case, avoid extra closure. |
| 130 return PromiseSet(new Promise(promiseRaw), +1, x); | 131 return PromiseSet(new $Promise(promiseRaw), +1, x); |
| 131 } else { | 132 } else { |
| 132 return new this(function(resolve, reject) { resolve(x) }); | 133 return new this(function(resolve, reject) { resolve(x) }); |
| 133 } | 134 } |
| 134 } | 135 } |
| 135 | 136 |
| 136 function PromiseRejected(r) { | 137 function PromiseRejected(r) { |
| 137 if (this === $Promise) { | 138 if (this === $Promise) { |
| 138 // Optimized case, avoid extra closure. | 139 // Optimized case, avoid extra closure. |
| 139 return PromiseSet(new Promise(promiseRaw), -1, r); | 140 return PromiseSet(new $Promise(promiseRaw), -1, r); |
| 140 } else { | 141 } else { |
| 141 return new this(function(resolve, reject) { reject(r) }); | 142 return new this(function(resolve, reject) { reject(r) }); |
| 142 } | 143 } |
| 143 } | 144 } |
| 144 | 145 |
| 145 | 146 |
| 146 // Simple chaining. | 147 // Simple chaining. |
| 147 | 148 |
| 148 function PromiseIdResolveHandler(x) { return x } | 149 function PromiseIdResolveHandler(x) { return x } |
| 149 function PromiseIdRejectHandler(r) { throw r } | 150 function PromiseIdRejectHandler(r) { throw r } |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 "resolve", PromiseCast | 318 "resolve", PromiseCast |
| 318 ]); | 319 ]); |
| 319 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 320 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
| 320 "chain", PromiseChain, | 321 "chain", PromiseChain, |
| 321 "then", PromiseThen, | 322 "then", PromiseThen, |
| 322 "catch", PromiseCatch | 323 "catch", PromiseCatch |
| 323 ]); | 324 ]); |
| 324 } | 325 } |
| 325 | 326 |
| 326 SetUpPromise(); | 327 SetUpPromise(); |
| OLD | NEW |