| 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 = Promise; | 37 var $Promise = function Promise(resolver) { |
| 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 } |
| 38 | 50 |
| 39 | 51 |
| 40 //------------------------------------------------------------------- | 52 //------------------------------------------------------------------- |
| 41 | 53 |
| 42 // Core functionality. | 54 // Core functionality. |
| 43 | 55 |
| 44 // Status values: 0 = pending, +1 = resolved, -1 = rejected | 56 // Status values: 0 = pending, +1 = resolved, -1 = rejected |
| 45 var promiseStatus = NEW_PRIVATE("Promise#status"); | 57 var promiseStatus = NEW_PRIVATE("Promise#status"); |
| 46 var promiseValue = NEW_PRIVATE("Promise#value"); | 58 var promiseValue = NEW_PRIVATE("Promise#value"); |
| 47 var promiseOnResolve = NEW_PRIVATE("Promise#onResolve"); | 59 var promiseOnResolve = NEW_PRIVATE("Promise#onResolve"); |
| 48 var promiseOnReject = NEW_PRIVATE("Promise#onReject"); | 60 var promiseOnReject = NEW_PRIVATE("Promise#onReject"); |
| 49 var promiseRaw = NEW_PRIVATE("Promise#raw"); | 61 var promiseRaw = NEW_PRIVATE("Promise#raw"); |
| 50 | 62 |
| 51 function IsPromise(x) { | 63 function IsPromise(x) { |
| 52 return IS_SPEC_OBJECT(x) && %HasLocalProperty(x, promiseStatus); | 64 return IS_SPEC_OBJECT(x) && %HasLocalProperty(x, promiseStatus); |
| 53 } | 65 } |
| 54 | 66 |
| 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 | |
| 69 function PromiseSet(promise, status, value, onResolve, onReject) { | 67 function PromiseSet(promise, status, value, onResolve, onReject) { |
| 70 SET_PRIVATE(promise, promiseStatus, status); | 68 SET_PRIVATE(promise, promiseStatus, status); |
| 71 SET_PRIVATE(promise, promiseValue, value); | 69 SET_PRIVATE(promise, promiseValue, value); |
| 72 SET_PRIVATE(promise, promiseOnResolve, onResolve); | 70 SET_PRIVATE(promise, promiseOnResolve, onResolve); |
| 73 SET_PRIVATE(promise, promiseOnReject, onReject); | 71 SET_PRIVATE(promise, promiseOnReject, onReject); |
| 74 return promise; | 72 return promise; |
| 75 } | 73 } |
| 76 | 74 |
| 77 function PromiseInit(promise) { | 75 function PromiseInit(promise) { |
| 78 return PromiseSet(promise, 0, UNDEFINED, new InternalArray, new InternalArray) | 76 return PromiseSet(promise, 0, UNDEFINED, new InternalArray, new InternalArray) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 92 function PromiseReject(promise, r) { | 90 function PromiseReject(promise, r) { |
| 93 PromiseDone(promise, -1, r, promiseOnReject) | 91 PromiseDone(promise, -1, r, promiseOnReject) |
| 94 } | 92 } |
| 95 | 93 |
| 96 | 94 |
| 97 // For API. | 95 // For API. |
| 98 | 96 |
| 99 function PromiseNopResolver() {} | 97 function PromiseNopResolver() {} |
| 100 | 98 |
| 101 function PromiseCreate() { | 99 function PromiseCreate() { |
| 102 return new Promise(PromiseNopResolver) | 100 return new $Promise(PromiseNopResolver) |
| 103 } | 101 } |
| 104 | 102 |
| 105 | 103 |
| 106 // Convenience. | 104 // Convenience. |
| 107 | 105 |
| 108 function PromiseDeferred() { | 106 function PromiseDeferred() { |
| 109 if (this === $Promise) { | 107 if (this === $Promise) { |
| 110 // Optimized case, avoid extra closure. | 108 // Optimized case, avoid extra closure. |
| 111 var promise = PromiseInit(new Promise(promiseRaw)); | 109 var promise = PromiseInit(new $Promise(promiseRaw)); |
| 112 return { | 110 return { |
| 113 promise: promise, | 111 promise: promise, |
| 114 resolve: function(x) { PromiseResolve(promise, x) }, | 112 resolve: function(x) { PromiseResolve(promise, x) }, |
| 115 reject: function(r) { PromiseReject(promise, r) } | 113 reject: function(r) { PromiseReject(promise, r) } |
| 116 }; | 114 }; |
| 117 } else { | 115 } else { |
| 118 var result = {}; | 116 var result = {}; |
| 119 result.promise = new this(function(resolve, reject) { | 117 result.promise = new this(function(resolve, reject) { |
| 120 result.resolve = resolve; | 118 result.resolve = resolve; |
| 121 result.reject = reject; | 119 result.reject = reject; |
| 122 }) | 120 }) |
| 123 return result; | 121 return result; |
| 124 } | 122 } |
| 125 } | 123 } |
| 126 | 124 |
| 127 function PromiseResolved(x) { | 125 function PromiseResolved(x) { |
| 128 if (this === $Promise) { | 126 if (this === $Promise) { |
| 129 // Optimized case, avoid extra closure. | 127 // Optimized case, avoid extra closure. |
| 130 return PromiseSet(new Promise(promiseRaw), +1, x); | 128 return PromiseSet(new $Promise(promiseRaw), +1, x); |
| 131 } else { | 129 } else { |
| 132 return new this(function(resolve, reject) { resolve(x) }); | 130 return new this(function(resolve, reject) { resolve(x) }); |
| 133 } | 131 } |
| 134 } | 132 } |
| 135 | 133 |
| 136 function PromiseRejected(r) { | 134 function PromiseRejected(r) { |
| 137 if (this === $Promise) { | 135 if (this === $Promise) { |
| 138 // Optimized case, avoid extra closure. | 136 // Optimized case, avoid extra closure. |
| 139 return PromiseSet(new Promise(promiseRaw), -1, r); | 137 return PromiseSet(new $Promise(promiseRaw), -1, r); |
| 140 } else { | 138 } else { |
| 141 return new this(function(resolve, reject) { reject(r) }); | 139 return new this(function(resolve, reject) { reject(r) }); |
| 142 } | 140 } |
| 143 } | 141 } |
| 144 | 142 |
| 145 | 143 |
| 146 // Simple chaining. | 144 // Simple chaining. |
| 147 | 145 |
| 148 function PromiseIdResolveHandler(x) { return x } | 146 function PromiseIdResolveHandler(x) { return x } |
| 149 function PromiseIdRejectHandler(r) { throw r } | 147 function PromiseIdRejectHandler(r) { throw r } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 } | 297 } |
| 300 } catch (e) { | 298 } catch (e) { |
| 301 deferred.reject(e) | 299 deferred.reject(e) |
| 302 } | 300 } |
| 303 return deferred.promise; | 301 return deferred.promise; |
| 304 } | 302 } |
| 305 | 303 |
| 306 //------------------------------------------------------------------- | 304 //------------------------------------------------------------------- |
| 307 | 305 |
| 308 function SetUpPromise() { | 306 function SetUpPromise() { |
| 309 %CheckIsBootstrapping() | 307 %CheckIsBootstrapping(); |
| 310 var global_receiver = %GlobalReceiver(global); | 308 %SetProperty(global, "Promise", $Promise, NONE); |
| 311 global_receiver.Promise = $Promise; | |
| 312 InstallFunctions($Promise, DONT_ENUM, [ | 309 InstallFunctions($Promise, DONT_ENUM, [ |
| 313 "defer", PromiseDeferred, | 310 "defer", PromiseDeferred, |
| 314 "accept", PromiseResolved, | 311 "accept", PromiseResolved, |
| 315 "reject", PromiseRejected, | 312 "reject", PromiseRejected, |
| 316 "all", PromiseAll, | 313 "all", PromiseAll, |
| 317 "race", PromiseOne, | 314 "race", PromiseOne, |
| 318 "resolve", PromiseCast | 315 "resolve", PromiseCast |
| 319 ]); | 316 ]); |
| 320 InstallFunctions($Promise.prototype, DONT_ENUM, [ | 317 InstallFunctions($Promise.prototype, DONT_ENUM, [ |
| 321 "chain", PromiseChain, | 318 "chain", PromiseChain, |
| 322 "then", PromiseThen, | 319 "then", PromiseThen, |
| 323 "catch", PromiseCatch | 320 "catch", PromiseCatch |
| 324 ]); | 321 ]); |
| 325 } | 322 } |
| 326 | 323 |
| 327 SetUpPromise(); | 324 SetUpPromise(); |
| OLD | NEW |