From 098fef674038dcc42ae991205fdc6e5d649c2b6a Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 12 Nov 2011 13:31:26 +0100 Subject: [PATCH] timers: remember extra setTimeout() arguments when timeout==0 Fixes #2079. --- lib/timers.js | 27 +++++------- test/simple/test-timers-zero-timeout.js | 58 +++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 test/simple/test-timers-zero-timeout.js diff --git a/lib/timers.js b/lib/timers.js index 59b016ef3..39eb4d916 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -144,19 +144,23 @@ exports.active = function(item) { exports.setTimeout = function(callback, after) { - var timer, c, args; + var timer; if (after <= 0) { // Use the slow case for after == 0 timer = new Timer(); - timer.ontimeout = callback; - args = Array.prototype.slice.call(arguments, 2); - timer._onTimeout = function() { - callback.apply(timer, args); - timer.close(); + if (arguments.length <= 2) { + timer._onTimeout = callback; + } else { + var args = Array.prototype.slice.call(arguments, 2); + timer._onTimeout = function() { + callback.apply(timer, args); + timer.close(); + } } + timer.ontimeout = timer._onTimeout; timer.start(0, 0); } else { timer = { _idleTimeout: after }; @@ -166,16 +170,7 @@ exports.setTimeout = function(callback, after) { if (arguments.length <= 2) { timer._onTimeout = callback; } else { - /* - * Sometimes setTimeout is called with arguments, EG - * - * setTimeout(callback, 2000, "hello", "world") - * - * If that's the case we need to call the callback with - * those args. The overhead of an extra closure is not - * desired in the normal case. - */ - args = Array.prototype.slice.call(arguments, 2); + var args = Array.prototype.slice.call(arguments, 2); timer._onTimeout = function() { callback.apply(timer, args); } diff --git a/test/simple/test-timers-zero-timeout.js b/test/simple/test-timers-zero-timeout.js new file mode 100644 index 000000000..79ecd9d26 --- /dev/null +++ b/test/simple/test-timers-zero-timeout.js @@ -0,0 +1,58 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var common = require('../common'); +var assert = require('assert'); + +// https://github.com/joyent/node/issues/2079 - zero timeout drops extra args +(function() { + var ncalled = 0; + + setTimeout(f, 0, 'foo', 'bar', 'baz'); + + function f(a, b, c) { + assert.equal(a, 'foo'); + assert.equal(b, 'bar'); + assert.equal(c, 'baz'); + ncalled++; + } + + process.on('exit', function() { + assert.equal(ncalled, 1); + }); +})(); + +(function() { + var ncalled = 0; + + var iv = setInterval(f, 0, 'foo', 'bar', 'baz'); + + function f(a, b, c) { + assert.equal(a, 'foo'); + assert.equal(b, 'bar'); + assert.equal(c, 'baz'); + if (++ncalled == 3) clearTimeout(iv); + } + + process.on('exit', function() { + assert.equal(ncalled, 3); + }); +})();