Chromium Code Reviews| Index: src/js/promise.js |
| diff --git a/src/js/promise.js b/src/js/promise.js |
| index bcf826a10151d48a498d40e72f418805a5022f18..03f43cd13faa57ef02be8dbbc725bdae23307298 100644 |
| --- a/src/js/promise.js |
| +++ b/src/js/promise.js |
| @@ -25,10 +25,13 @@ var promiseStatusSymbol = utils.ImportNow("promise_status_symbol"); |
| var promiseValueSymbol = utils.ImportNow("promise_value_symbol"); |
| var SpeciesConstructor; |
| var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
| - |
| +var AsyncFunctionNext; |
| +var AsyncFunctionThrow; |
| utils.Import(function(from) { |
| MakeTypeError = from.MakeTypeError; |
| SpeciesConstructor = from.SpeciesConstructor; |
| + AsyncFunctionNext = from.AsyncFunctionNext; |
| + AsyncFunctionThrow = from.AsyncFunctionThrow; |
| }); |
| // ------------------------------------------------------------------- |
| @@ -268,6 +271,19 @@ function PromiseRejected(r) { |
| } |
| } |
| +// Shortcut Promise.reject and Promise.resolve() implementations, used by |
| +// Async Functions implementation. |
| +function PromiseCreateRejected(r) { |
| + var promise = PromiseCreateAndSet(-1, r); |
| + %PromiseRejectEvent(promise, r, false); |
| + return promise; |
| +} |
| + |
| +function PromiseCreateResolved(x) { |
| + if (IsPromise(x) && x.constructor === GlobalPromise) return x; |
| + return PromiseCreateAndSet(+1, x); |
| +} |
| + |
| // Multi-unwrapped chaining with thenable coercion. |
| function PromiseThen(onResolve, onReject) { |
| @@ -424,6 +440,25 @@ function PromiseHasUserDefinedRejectHandler() { |
| return PromiseHasUserDefinedRejectHandlerRecursive(this); |
| }; |
| +// TODO(caitp): move me to a new file =) |
| +function AsyncFunctionResume(generator, sentValue, resumeFunc) { |
| + try { |
| + var result = resumeFunc(generator, sentValue); |
| + if (result.done) return PromiseCreateResolved(result.value); |
| + } catch (error) { |
| + return PromiseCreateRejected(error); |
| + } |
| + |
| + %GlobalPrint("await...\n"); |
| + return %_Call(PromiseThen, PromiseCreateResolved(result.value), |
| + function(value) { %GlobalPrint("resume normal...\n"); return AsyncFunctionResume(generator, value, AsyncFunctionNext); }, |
|
caitp (gmail)
2016/04/16 18:33:03
phantom edits from some debugging =)
|
| + function(error) { %GlobalPrint("resume throw...\n"); return AsyncFunctionResume(generator, error, AsyncFunctionThrow); }); |
| +} |
| + |
| +function AsyncFunctionStart(generator) { |
| + return AsyncFunctionResume(generator, UNDEFINED, AsyncFunctionNext); |
| +} |
| + |
| // ------------------------------------------------------------------- |
| // Install exported functions. |
| @@ -451,6 +486,9 @@ utils.InstallFunctions(GlobalPromise.prototype, DONT_ENUM, [ |
| "promise_reject", PromiseReject, |
| "promise_resolve", PromiseResolve, |
| "promise_then", PromiseThen, |
| + "promise_create_rejected", PromiseCreateRejected, |
| + "promise_create_resolved", PromiseCreateResolved, |
| + "async_function_start", AsyncFunctionStart |
| ]); |
| // This allows extras to create promises quickly without building extra |