1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 var util = require('util');
23 var EventEmitter = require('events');
24 var inherits = util.inherits;
26 // communicate with events module, but don't require that
27 // module to have to load this one, since this module has
28 // a few side effects.
29 EventEmitter.usingDomains = true;
31 // overwrite process.domain with a getter/setter that will allow for more
32 // effective optimizations
34 Object.defineProperty(process, 'domain', {
40 return _domain[0] = arg;
44 // objects with external array data are excellent ways to communicate state
45 // between js and c++ w/o much overhead
46 var _domain_flag = {};
48 // let the process know we're using domains
49 process._setupDomainUse(_domain, _domain_flag);
51 exports.Domain = Domain;
53 exports.create = exports.createDomain = function() {
57 // it's possible to enter one domain while already inside
58 // another one. the stack is each entered domain.
60 exports._stack = stack;
61 // the active domain is always the one that we're currently in.
62 exports.active = null;
65 inherits(Domain, EventEmitter);
68 EventEmitter.call(this);
73 Domain.prototype.members = undefined;
74 Domain.prototype._disposed = undefined;
77 // Called by process._fatalException in case an error was thrown.
78 Domain.prototype._errorHandler = function errorHandler(er) {
80 // ignore errors on disposed domains.
82 // XXX This is a bit stupid. We should probably get rid of
83 // domain.dispose() altogether. It's almost always a terrible
89 er.domainThrown = true;
90 // wrap this in a try/catch so we don't get infinite throwing
92 // One of three things will happen here.
94 // 1. There is a handler, caught = true
95 // 2. There is no handler, caught = false
96 // 3. It throws, caught = false
98 // If caught is false after this, then there's no need to exit()
99 // the domain, because we're going to crash the process anyway.
100 caught = this.emit('error', er);
102 // Exit all domains on the stack. Uncaught exceptions end the
103 // current tick and no domains should be left on the stack
106 exports.active = process.domain = null;
108 // The domain error handler threw! oh no!
109 // See if another domain can catch THIS error,
110 // or else crash on the original one.
111 // If the user already exited it, then don't double-exit.
112 if (this === exports.active) {
116 exports.active = process.domain = stack[stack.length - 1];
117 caught = process._fatalException(er2);
127 Domain.prototype.enter = function() {
128 if (this._disposed) return;
130 // note that this might be a no-op, but we still need
131 // to push it onto the stack so that we can pop it later.
132 exports.active = process.domain = this;
134 _domain_flag[0] = stack.length;
138 Domain.prototype.exit = function() {
139 if (this._disposed) return;
141 // exit all domains until this one.
142 var index = stack.lastIndexOf(this);
147 _domain_flag[0] = stack.length;
149 exports.active = stack[stack.length - 1];
150 process.domain = exports.active;
154 // note: this works for timers as well.
155 Domain.prototype.add = function(ee) {
156 // If the domain is disposed or already added, then nothing left to do.
157 if (this._disposed || ee.domain === this)
160 // has a domain already - remove it first.
162 ee.domain.remove(ee);
164 // check for circular Domain->Domain links.
165 // This causes bad insanity!
168 // var d = domain.create();
169 // var e = domain.create();
172 // e.emit('error', er); // RangeError, stack overflow!
173 if (this.domain && (ee instanceof Domain)) {
174 for (var d = this.domain; d; d = d.domain) {
175 if (ee === d) return;
180 this.members.push(ee);
184 Domain.prototype.remove = function(ee) {
186 var index = this.members.indexOf(ee);
188 this.members.splice(index, 1);
192 Domain.prototype.run = function(fn) {
196 var ret = fn.call(this);
202 function intercepted(_this, self, cb, fnargs) {
206 if (fnargs[0] && fnargs[0] instanceof Error) {
213 self.emit('error', er);
217 var len = fnargs.length;
222 if (fnargs.length > 1) {
223 for (i = 1; i < fnargs.length; i++)
224 args.push(fnargs[i]);
225 ret = cb.apply(_this, args);
227 ret = cb.call(_this);
235 Domain.prototype.intercept = function(cb) {
238 function runIntercepted() {
239 return intercepted(this, self, cb, arguments);
242 return runIntercepted;
246 function bound(_this, self, cb, fnargs) {
250 var len = fnargs.length;
255 if (fnargs.length > 0)
256 ret = cb.apply(_this, fnargs);
258 ret = cb.call(_this);
265 Domain.prototype.bind = function(cb) {
268 function runBound() {
269 return bound(this, self, cb, arguments);
272 runBound.domain = this;
278 Domain.prototype.dispose = util.deprecate(function() {
279 if (this._disposed) return;
281 // if we're the active domain, then get out now.
284 // remove from parent domain, if there is one.
285 if (this.domain) this.domain.remove(this);
287 // kill the references so that they can be properly gc'ed.
288 this.members.length = 0;
290 // mark this domain as 'no longer relevant'
291 // so that it can't be entered or activated.
292 this._disposed = true;