2 module.exports = function(Promise, CapturedTrace) {
3 var getDomain = Promise._getDomain;
4 var async = require("./async.js");
5 var Warning = require("./errors.js").Warning;
6 var util = require("./util.js");
7 var canAttachTrace = util.canAttachTrace;
8 var unhandledRejectionHandled;
9 var possiblyUnhandledRejection;
10 var debugging = false || (util.isNode &&
11 (!!process.env["BLUEBIRD_DEBUG"] ||
12 process.env["NODE_ENV"] === "development"));
14 if (util.isNode && process.env["BLUEBIRD_DEBUG"] == 0) debugging = false;
17 async.disableTrampolineIfNecessary();
20 Promise.prototype._ignoreRejections = function() {
21 this._unsetRejectionIsUnhandled();
22 this._bitField = this._bitField | 16777216;
25 Promise.prototype._ensurePossibleRejectionHandled = function () {
26 if ((this._bitField & 16777216) !== 0) return;
27 this._setRejectionIsUnhandled();
28 async.invokeLater(this._notifyUnhandledRejection, this, undefined);
31 Promise.prototype._notifyUnhandledRejectionIsHandled = function () {
32 CapturedTrace.fireRejectionEvent("rejectionHandled",
33 unhandledRejectionHandled, undefined, this);
36 Promise.prototype._notifyUnhandledRejection = function () {
37 if (this._isRejectionUnhandled()) {
38 var reason = this._getCarriedStackTrace() || this._settledValue;
39 this._setUnhandledRejectionIsNotified();
40 CapturedTrace.fireRejectionEvent("unhandledRejection",
41 possiblyUnhandledRejection, reason, this);
45 Promise.prototype._setUnhandledRejectionIsNotified = function () {
46 this._bitField = this._bitField | 524288;
49 Promise.prototype._unsetUnhandledRejectionIsNotified = function () {
50 this._bitField = this._bitField & (~524288);
53 Promise.prototype._isUnhandledRejectionNotified = function () {
54 return (this._bitField & 524288) > 0;
57 Promise.prototype._setRejectionIsUnhandled = function () {
58 this._bitField = this._bitField | 2097152;
61 Promise.prototype._unsetRejectionIsUnhandled = function () {
62 this._bitField = this._bitField & (~2097152);
63 if (this._isUnhandledRejectionNotified()) {
64 this._unsetUnhandledRejectionIsNotified();
65 this._notifyUnhandledRejectionIsHandled();
69 Promise.prototype._isRejectionUnhandled = function () {
70 return (this._bitField & 2097152) > 0;
73 Promise.prototype._setCarriedStackTrace = function (capturedTrace) {
74 this._bitField = this._bitField | 1048576;
75 this._fulfillmentHandler0 = capturedTrace;
78 Promise.prototype._isCarryingStackTrace = function () {
79 return (this._bitField & 1048576) > 0;
82 Promise.prototype._getCarriedStackTrace = function () {
83 return this._isCarryingStackTrace()
84 ? this._fulfillmentHandler0
88 Promise.prototype._captureStackTrace = function () {
90 this._trace = new CapturedTrace(this._peekContext());
95 Promise.prototype._attachExtraTrace = function (error, ignoreSelf) {
96 if (debugging && canAttachTrace(error)) {
97 var trace = this._trace;
98 if (trace !== undefined) {
99 if (ignoreSelf) trace = trace._parent;
101 if (trace !== undefined) {
102 trace.attachExtraTrace(error);
103 } else if (!error.__stackCleaned__) {
104 var parsed = CapturedTrace.parseStackAndMessage(error);
105 util.notEnumerableProp(error, "stack",
106 parsed.message + "\n" + parsed.stack.join("\n"));
107 util.notEnumerableProp(error, "__stackCleaned__", true);
112 Promise.prototype._warn = function(message) {
113 var warning = new Warning(message);
114 var ctx = this._peekContext();
116 ctx.attachExtraTrace(warning);
118 var parsed = CapturedTrace.parseStackAndMessage(warning);
119 warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
121 CapturedTrace.formatAndLogError(warning, "");
124 Promise.onPossiblyUnhandledRejection = function (fn) {
125 var domain = getDomain();
126 possiblyUnhandledRejection =
127 typeof fn === "function" ? (domain === null ? fn : domain.bind(fn))
131 Promise.onUnhandledRejectionHandled = function (fn) {
132 var domain = getDomain();
133 unhandledRejectionHandled =
134 typeof fn === "function" ? (domain === null ? fn : domain.bind(fn))
138 Promise.longStackTraces = function () {
139 if (async.haveItemsQueued() &&
142 throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/DT1qyG\u000a");
144 debugging = CapturedTrace.isSupported();
146 async.disableTrampolineIfNecessary();
150 Promise.hasLongStackTraces = function () {
151 return debugging && CapturedTrace.isSupported();
154 if (!CapturedTrace.isSupported()) {
155 Promise.longStackTraces = function(){};