Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Unified Diff: src/js/promise.js

Issue 1531073004: [promise] Make Promise.reject match spec, and validate promise capabilities (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Re-add and skip test Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/messages.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/js/promise.js
diff --git a/src/js/promise.js b/src/js/promise.js
index 6fc8728562c03f4e5def5bc4d3dcc72088b90b75..6248654ecc28c75c36eb7afb13e91c9941eb88c4 100644
--- a/src/js/promise.js
+++ b/src/js/promise.js
@@ -227,15 +227,22 @@ function NewPromiseCapability(C) {
resolve: callbacks.resolve,
reject: callbacks.reject
};
- } else {
- var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED };
- result.promise = new C(function(resolve, reject) {
- // TODO(littledan): Check for resolve and reject being not undefined
- result.resolve = resolve;
- result.reject = reject;
- });
- return result;
}
+
+ var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED };
+ result.promise = new C(function(resolve, reject) {
+ if (!IS_UNDEFINED(result.resolve) || !IS_UNDEFINED(result.reject))
+ throw MakeTypeError(kPromiseExecutorAlreadyInvoked);
+ result.resolve = resolve;
+ result.reject = reject;
+ });
+
+ if (!IS_CALLABLE(result.resolve))
+ throw MakeTypeError(kCalledNonCallable, "promiseCapability.[[Resolve]]");
+ if (!IS_CALLABLE(result.reject))
+ throw MakeTypeError(kCalledNonCallable, "promiseCapability.[[Reject]]");
+
+ return result;
}
function PromiseDeferred() {
@@ -250,17 +257,18 @@ function PromiseRejected(r) {
if (!IS_RECEIVER(this)) {
throw MakeTypeError(kCalledOnNonObject, PromiseRejected);
}
- var promise;
if (this === GlobalPromise) {
// Optimized case, avoid extra closure.
- promise = PromiseCreateAndSet(-1, r);
+ var promise = PromiseCreateAndSet(-1, r);
// The debug event for this would always be an uncaught promise reject,
// which is usually simply noise. Do not trigger that debug event.
%PromiseRejectEvent(promise, r, false);
+ return promise;
} else {
- promise = new this(function(resolve, reject) { reject(r) });
+ var promiseCapability = NewPromiseCapability(this);
+ %_Call(promiseCapability.reject, UNDEFINED, r);
adamk 2016/01/05 20:33:09 Any reason to use %_Call here instead of just maki
caitp (gmail) 2016/01/05 20:39:54 The `promiseCapability` object is emulating a Reco
adamk 2016/01/05 20:44:41 You could do: var reject = promiseCapability.reje
+ return promiseCapability.promise;
}
- return promise;
}
// Multi-unwrapped chaining with thenable coercion.
@@ -359,6 +367,10 @@ function PromiseAll(iterable) {
}
function PromiseRace(iterable) {
+ if (!IS_RECEIVER(this)) {
+ throw MakeTypeError(kCalledOnNonObject, PromiseRace);
+ }
+
var deferred = NewPromiseCapability(this);
try {
for (var value of iterable) {
« no previous file with comments | « no previous file | src/messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698