mirror of
https://github.com/nodejs/node.git
synced 2025-05-05 21:04:16 +00:00
timers: remember extra setTimeout() arguments when timeout==0
Fixes #2079.
This commit is contained in:
parent
8082858ee7
commit
098fef6740
@ -144,19 +144,23 @@ exports.active = function(item) {
|
|||||||
|
|
||||||
|
|
||||||
exports.setTimeout = function(callback, after) {
|
exports.setTimeout = function(callback, after) {
|
||||||
var timer, c, args;
|
var timer;
|
||||||
|
|
||||||
if (after <= 0) {
|
if (after <= 0) {
|
||||||
// Use the slow case for after == 0
|
// Use the slow case for after == 0
|
||||||
timer = new Timer();
|
timer = new Timer();
|
||||||
timer.ontimeout = callback;
|
|
||||||
|
|
||||||
args = Array.prototype.slice.call(arguments, 2);
|
if (arguments.length <= 2) {
|
||||||
|
timer._onTimeout = callback;
|
||||||
|
} else {
|
||||||
|
var args = Array.prototype.slice.call(arguments, 2);
|
||||||
timer._onTimeout = function() {
|
timer._onTimeout = function() {
|
||||||
callback.apply(timer, args);
|
callback.apply(timer, args);
|
||||||
timer.close();
|
timer.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
timer.ontimeout = timer._onTimeout;
|
||||||
timer.start(0, 0);
|
timer.start(0, 0);
|
||||||
} else {
|
} else {
|
||||||
timer = { _idleTimeout: after };
|
timer = { _idleTimeout: after };
|
||||||
@ -166,16 +170,7 @@ exports.setTimeout = function(callback, after) {
|
|||||||
if (arguments.length <= 2) {
|
if (arguments.length <= 2) {
|
||||||
timer._onTimeout = callback;
|
timer._onTimeout = callback;
|
||||||
} else {
|
} else {
|
||||||
/*
|
var args = Array.prototype.slice.call(arguments, 2);
|
||||||
* 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);
|
|
||||||
timer._onTimeout = function() {
|
timer._onTimeout = function() {
|
||||||
callback.apply(timer, args);
|
callback.apply(timer, args);
|
||||||
}
|
}
|
||||||
|
58
test/simple/test-timers-zero-timeout.js
Normal file
58
test/simple/test-timers-zero-timeout.js
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
})();
|
Loading…
Reference in New Issue
Block a user