2 module.exports = function(Promise, INTERNAL, tryConvertToPromise, debug) {
3 var calledBind = false;
4 var rejectThis = function(_, e) {
8 var targetRejected = function(e, context) {
9 context.promiseRejectionQueued = true;
10 context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
13 var bindingResolved = function(thisArg, context) {
14 if (((this._bitField & 50397184) === 0)) {
15 this._resolveCallback(context.target);
19 var bindingRejected = function(e, context) {
20 if (!context.promiseRejectionQueued) this._reject(e);
23 Promise.prototype.bind = function (thisArg) {
26 Promise.prototype._propagateFrom = debug.propagateFromFunction();
27 Promise.prototype._boundValue = debug.boundValueFunction();
29 var maybePromise = tryConvertToPromise(thisArg);
30 var ret = new Promise(INTERNAL);
31 ret._propagateFrom(this, 1);
32 var target = this._target();
33 ret._setBoundTo(maybePromise);
34 if (maybePromise instanceof Promise) {
36 promiseRejectionQueued: false,
39 bindingPromise: maybePromise
41 target._then(INTERNAL, targetRejected, undefined, ret, context);
43 bindingResolved, bindingRejected, undefined, ret, context);
44 ret._setOnCancel(maybePromise);
46 ret._resolveCallback(target);
51 Promise.prototype._setBoundTo = function (obj) {
52 if (obj !== undefined) {
53 this._bitField = this._bitField | 2097152;
56 this._bitField = this._bitField & (~2097152);
60 Promise.prototype._isBound = function () {
61 return (this._bitField & 2097152) === 2097152;
64 Promise.bind = function (thisArg, value) {
65 return Promise.resolve(value).bind(thisArg);