Commit 3286bc71 authored by rossberg@chromium.org's avatar rossberg@chromium.org

Promises: some adaptations to spec

- Rename Promise.{resolved,rejected,deferred} to Promise.{resolve,reject,defer}
- Rename Promise.one to Promise.race
- Make all failures asynchronous, EXCEPT type errors for resolver
- Disallow non-construct call to Promise constructor
- Don't make combinators go through public this.defer

Also, don't bother using IsCallable.

R=dslomov@chromium.org, yhirano@chromium.org
BUG=

Review URL: https://codereview.chromium.org/99573002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18515 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 61692bf2
......@@ -109,8 +109,9 @@ var kMessages = {
invalid_argument: ["invalid_argument"],
data_view_not_array_buffer: ["First argument to DataView constructor must be an ArrayBuffer"],
constructor_not_function: ["Constructor ", "%0", " requires 'new'"],
not_a_promise: ["%0", "is not a promise"],
promise_cyclic: ["Chaining cycle detected for promise", "%0"],
not_a_promise: ["%0", " is not a promise"],
resolver_not_a_function: ["Promise resolver ", "%0", " is not a function"],
promise_cyclic: ["Chaining cycle detected for promise ", "%0"],
array_functions_on_frozen: ["Cannot modify frozen array elements"],
array_functions_change_sealed: ["Cannot add/remove sealed array elements"],
// RangeError
......
......@@ -62,11 +62,16 @@ function IsPromise(x) {
function Promise(resolver) {
if (resolver === promiseRaw) return;
if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]);
if (typeof resolver !== 'function')
throw MakeTypeError('resolver_not_a_function', [resolver]);
var promise = PromiseInit(this);
try {
resolver(function(x) { PromiseResolve(promise, x) },
function(r) { PromiseReject(promise, r) });
// TODO(rossberg): current draft makes exception from this call asynchronous,
// but that's probably a mistake.
} catch (e) {
PromiseReject(promise, e);
}
}
function PromiseSet(promise, status, value, onResolve, onReject) {
......@@ -82,9 +87,10 @@ function PromiseInit(promise) {
}
function PromiseDone(promise, status, value, promiseQueue) {
if (GET_PRIVATE(promise, promiseStatus) !== 0) return;
if (GET_PRIVATE(promise, promiseStatus) === 0) {
PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue));
PromiseSet(promise, status, value);
}
}
function PromiseResolve(promise, x) {
......@@ -219,14 +225,13 @@ function PromiseThen(onResolve, onReject) {
PromiseCoerce.table = new $WeakMap;
function PromiseCoerce(constructor, x) {
var then;
if (IsPromise(x)) {
return x;
} else if (!IS_NULL_OR_UNDEFINED(x) && %IsCallable(then = x.then)) {
if (!(IsPromise(x) || IS_NULL_OR_UNDEFINED(x))) {
var then = x.then;
if (typeof then === 'function') {
if (PromiseCoerce.table.has(x)) {
return PromiseCoerce.table.get(x);
} else {
var deferred = constructor.deferred();
var deferred = %_CallFunction(constructor, PromiseDeferred);
PromiseCoerce.table.set(x, deferred.promise);
try {
%_CallFunction(x, deferred.resolve, deferred.reject, then);
......@@ -235,9 +240,9 @@ function PromiseCoerce(constructor, x) {
}
return deferred.promise;
}
} else {
return x;
}
}
return x;
}
......@@ -245,12 +250,13 @@ function PromiseCoerce(constructor, x) {
function PromiseCast(x) {
// TODO(rossberg): cannot do better until we support @@create.
return IsPromise(x) ? x : this.resolved(x);
return IsPromise(x) ? x : this.resolve(x);
}
function PromiseAll(values) {
var deferred = this.deferred();
var deferred = %_CallFunction(this, PromiseDeferred);
var resolutions = [];
try {
var count = values.length;
if (count === 0) {
deferred.resolve(resolutions);
......@@ -261,24 +267,28 @@ function PromiseAll(values) {
resolutions[i] = x;
if (--count === 0) deferred.resolve(resolutions);
}.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available
function(r) {
if (count > 0) { count = 0; deferred.reject(r) }
}
function(r) { deferred.reject(r) }
);
}
}
} catch (e) {
deferred.reject(e)
}
return deferred.promise;
}
function PromiseOne(values) { // a.k.a. race
var deferred = this.deferred();
var done = false;
function PromiseOne(values) {
var deferred = %_CallFunction(this, PromiseDeferred);
try {
for (var i = 0; i < values.length; ++i) {
this.cast(values[i]).chain(
function(x) { if (!done) { done = true; deferred.resolve(x) } },
function(r) { if (!done) { done = true; deferred.reject(r) } }
function(x) { deferred.resolve(x) },
function(r) { deferred.reject(r) }
);
}
} catch (e) {
deferred.reject(e)
}
return deferred.promise;
}
......@@ -288,11 +298,11 @@ function SetUpPromise() {
%CheckIsBootstrapping()
global.Promise = $Promise;
InstallFunctions($Promise, DONT_ENUM, [
"deferred", PromiseDeferred,
"resolved", PromiseResolved,
"rejected", PromiseRejected,
"defer", PromiseDeferred,
"resolve", PromiseResolved,
"reject", PromiseRejected,
"all", PromiseAll,
"one", PromiseOne,
"race", PromiseOne,
"cast", PromiseCast
]);
InstallFunctions($Promise.prototype, DONT_ENUM, [
......
......@@ -2663,14 +2663,6 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_SpecialArrayFunctions) {
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_IsCallable) {
SealHandleScope shs(isolate);
ASSERT(args.length() == 1);
CONVERT_ARG_CHECKED(Object, obj, 0);
return isolate->heap()->ToBoolean(obj->IsCallable());
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_IsClassicModeFunction) {
SealHandleScope shs(isolate);
ASSERT(args.length() == 1);
......
......@@ -64,7 +64,6 @@ namespace internal {
F(ToFastProperties, 1, 1) \
F(FinishArrayPrototypeSetup, 1, 1) \
F(SpecialArrayFunctions, 1, 1) \
F(IsCallable, 1, 1) \
F(IsClassicModeFunction, 1, 1) \
F(GetDefaultReceiver, 1, 1) \
\
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment