Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

domain: add arguments for the function in domain.run() #15

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions benchmark/misc/domain-fn-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var common = require('../common.js');
var domain = require('domain');

var bench = common.createBenchmark(main, {
arguments: [0, 1, 2, 3],
n: [10]
});

var bdomain = domain.create();
var gargs = [1, 2, 3];

function main(conf) {

var args, ret, n = +conf.n;
var arguments = gargs.slice(0, conf.arguments);
bench.start();

bdomain.enter();
for (var i = 0; i < n; i++) {
if (arguments.length >= 2) {
args = Array.prototype.slice.call(arguments, 1);
ret = fn.apply(this, args);
} else {
ret = fn.call(this);
}
}
bdomain.exit();

bench.end(n);
}

function fn(a, b, c) {
if (!a)
a = 1;

if (!b)
b = 2;

if (!c)
c = 3;

return a + b + c;
}
5 changes: 3 additions & 2 deletions doc/api/domain.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,14 @@ uncaught exceptions to the active Domain object.
Domain is a child class of [EventEmitter][]. To handle the errors that it
catches, listen to its `error` event.

### domain.run(fn)
### domain.run(fn[, arg][, ...])

* `fn` {Function}

Run the supplied function in the context of the domain, implicitly
binding all event emitters, timers, and lowlevel requests that are
created in that context.
created in that context. Optionally, arguments can be passed to
the function.

This is the most basic way to use a domain.

Expand Down
16 changes: 15 additions & 1 deletion lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,23 @@ Domain.prototype.remove = function(ee) {
Domain.prototype.run = function(fn) {
if (this._disposed)
return;

var args, i, len ret;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing a comma between len and ret. This should cause a syntax error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll handle this in the merge, though!


this.enter();
var ret = fn.call(this);
if (arguments.length >= 2) {
len = arguments.length;
args = new Array(len - 1);

for (i = 1; i < len; i++)
args[i - 1] = arguments[i];

ret = fn.apply(this, args);
} else {
ret = fn.call(this);
}
this.exit();

return ret;
};

Expand Down
7 changes: 7 additions & 0 deletions test/simple/test-domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ var result = d.run(function () {
assert.equal(result, 'return value');


// check if the executed function take in count the applied parameters
result = d.run(function (a, b) {
return a + ' ' + b;
}, 'return', 'value');
assert.equal(result, 'return value');


var fst = fs.createReadStream('stream for nonexistent file')
d.add(fst)
expectCaught++;
Expand Down