Index: src/promise.js |
diff --git a/src/promise.js b/src/promise.js |
index f638f63d26027d24ef11d6d406b87b5098806b3a..fa650eaf0e983d31d988dc7093d11398ea515091 100644 |
--- a/src/promise.js |
+++ b/src/promise.js |
@@ -10,7 +10,22 @@ |
// var $WeakMap = global.WeakMap |
-var $Promise = Promise; |
+var $Promise = function Promise(resolver) { |
+ if (resolver === promiseRaw) return; |
+ if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]); |
+ if (!IS_SPEC_FUNCTION(resolver)) |
+ throw MakeTypeError('resolver_not_a_function', [resolver]); |
+ var promise = PromiseInit(this); |
+ try { |
+ %DebugPromiseHandlePrologue(function() { return promise }); |
+ resolver(function(x) { PromiseResolve(promise, x) }, |
+ function(r) { PromiseReject(promise, r) }); |
+ } catch (e) { |
+ PromiseReject(promise, e); |
+ } finally { |
+ %DebugPromiseHandleEpilogue(); |
+ } |
+} |
//------------------------------------------------------------------- |
@@ -28,23 +43,6 @@ function IsPromise(x) { |
return IS_SPEC_OBJECT(x) && %HasLocalProperty(x, promiseStatus); |
} |
-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 { |
- %DebugPromiseHandlePrologue(function() { return promise }); |
- resolver(function(x) { PromiseResolve(promise, x) }, |
- function(r) { PromiseReject(promise, r) }); |
- } catch (e) { |
- PromiseReject(promise, e); |
- } finally { |
- %DebugPromiseHandleEpilogue(); |
- } |
-} |
- |
function PromiseSet(promise, status, value, onResolve, onReject) { |
SET_PRIVATE(promise, promiseStatus, status); |
SET_PRIVATE(promise, promiseValue, value); |
@@ -78,7 +76,7 @@ function PromiseReject(promise, r) { |
function PromiseNopResolver() {} |
function PromiseCreate() { |
- return new Promise(PromiseNopResolver) |
+ return new $Promise(PromiseNopResolver) |
} |
@@ -87,7 +85,7 @@ function PromiseCreate() { |
function PromiseDeferred() { |
if (this === $Promise) { |
// Optimized case, avoid extra closure. |
- var promise = PromiseInit(new Promise(promiseRaw)); |
+ var promise = PromiseInit(new $Promise(promiseRaw)); |
return { |
promise: promise, |
resolve: function(x) { PromiseResolve(promise, x) }, |
@@ -106,7 +104,7 @@ function PromiseDeferred() { |
function PromiseResolved(x) { |
if (this === $Promise) { |
// Optimized case, avoid extra closure. |
- return PromiseSet(new Promise(promiseRaw), +1, x); |
+ return PromiseSet(new $Promise(promiseRaw), +1, x); |
} else { |
return new this(function(resolve, reject) { resolve(x) }); |
} |
@@ -115,7 +113,7 @@ function PromiseResolved(x) { |
function PromiseRejected(r) { |
if (this === $Promise) { |
// Optimized case, avoid extra closure. |
- return PromiseSet(new Promise(promiseRaw), -1, r); |
+ return PromiseSet(new $Promise(promiseRaw), -1, r); |
} else { |
return new this(function(resolve, reject) { reject(r) }); |
} |
@@ -190,10 +188,8 @@ function PromiseHandle(value, handler, deferred) { |
// Multi-unwrapped chaining with thenable coercion. |
function PromiseThen(onResolve, onReject) { |
- onResolve = |
- IS_NULL_OR_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve; |
- onReject = |
- IS_NULL_OR_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject; |
+ onResolve = IS_SPEC_FUNCTION(onResolve) ? onResolve : PromiseIdResolveHandler; |
+ onReject = IS_SPEC_FUNCTION(onReject) ? onReject : PromiseIdRejectHandler; |
var that = this; |
var constructor = this.constructor; |
return %_CallFunction( |