@ -173,8 +173,9 @@ exports.active = function(item) {
* /
* /
exports . setTimeout = function ( callback , after ) {
exports . setTimeout = function ( callback , after , arg1 , arg2 , arg3 ) {
var timer ;
var timer , i , args ;
var len = arguments . length ;
after *= 1 ; // coalesce to number or NaN
after *= 1 ; // coalesce to number or NaN
@ -184,22 +185,38 @@ exports.setTimeout = function(callback, after) {
timer = new Timeout ( after ) ;
timer = new Timeout ( after ) ;
if ( arguments . length <= 2 ) {
switch ( len ) {
timer . _ onTimeout = callback ;
// fast cases
} else {
case 0 :
/ *
case 1 :
* Sometimes setTimeout is called with arguments , EG
case 2 :
*
timer . _ onTimeout = callback ;
* setTimeout ( callback , 2000 , "hello" , "world" )
break ;
*
case 3 :
* If that ' s the case we need to call the callback with
timer . _ onTimeout = function ( ) {
* those args . The overhead of an extra closure is not
callback . call ( timer , arg1 ) ;
* desired in the normal case .
} ;
* /
break ;
var args = Array . prototype . slice . call ( arguments , 2 ) ;
case 4 :
timer . _ onTimeout = function ( ) {
timer . _ onTimeout = function ( ) {
callback . apply ( timer , args ) ;
callback . call ( timer , arg1 , arg2 ) ;
} ;
} ;
break ;
case 5 :
timer . _ onTimeout = function ( ) {
callback . call ( timer , arg1 , arg2 , arg3 ) ;
} ;
break ;
// slow case
default :
args = new Array ( len - 2 ) ;
for ( i = 2 ; i < len ; i ++ )
args [ i - 2 ] = arguments [ i ] ;
timer . _ onTimeout = function ( ) {
callback . apply ( timer , args ) ;
} ;
break ;
}
}
if ( process . domain ) timer . domain = process . domain ;
if ( process . domain ) timer . domain = process . domain ;
@ -222,17 +239,24 @@ exports.clearTimeout = function(timer) {
} ;
} ;
exports . setInterval = function ( callback , repeat ) {
exports . setInterval = function ( callback , repeat , arg1 , arg2 , arg3 ) {
repeat *= 1 ; // coalesce to number or NaN
repeat *= 1 ; // coalesce to number or NaN
if ( ! ( repeat >= 1 && repeat <= TIMEOUT_MAX ) ) {
if ( ! ( repeat >= 1 && repeat <= TIMEOUT_MAX ) ) {
repeat = 1 ; // schedule on next tick, follows browser behaviour
repeat = 1 ; // schedule on next tick, follows browser behaviour
}
}
var args , i ;
var timer = new Timeout ( repeat ) ;
var timer = new Timeout ( repeat ) ;
var args = Array . prototype . slice . call ( arguments , 2 ) ;
var len = arguments . length - 2 ;
timer . _ onTimeout = wrapper ;
timer . _ onTimeout = wrapper ;
timer . _ repeat = true ;
timer . _ repeat = true ;
// Initialize args once for repeated invocation of slow case below
if ( len > 3 ) {
args = new Array ( len ) ;
for ( i = 0 ; i < len ; i ++ )
args [ i ] = arguments [ i + 2 ] ;
}
if ( process . domain ) timer . domain = process . domain ;
if ( process . domain ) timer . domain = process . domain ;
exports . active ( timer ) ;
exports . active ( timer ) ;
@ -240,7 +264,25 @@ exports.setInterval = function(callback, repeat) {
return timer ;
return timer ;
function wrapper ( ) {
function wrapper ( ) {
callback . apply ( this , args ) ;
switch ( len ) {
// fast cases
case 0 :
callback . call ( this ) ;
break ;
case 1 :
callback . call ( this , arg1 ) ;
break ;
case 2 :
callback . call ( this , arg1 , arg2 ) ;
break ;
case 3 :
callback . call ( this , arg1 , arg2 , arg3 ) ;
break ;
// slow case
default :
callback . apply ( this , args ) ;
break ;
}
// If callback called clearInterval().
// If callback called clearInterval().
if ( timer . _ repeat === false ) return ;
if ( timer . _ repeat === false ) return ;
// If timer is unref'd (or was - it's permanently removed from the list.)
// If timer is unref'd (or was - it's permanently removed from the list.)
@ -361,22 +403,44 @@ Immediate.prototype._idleNext = undefined;
Immediate . prototype . _ idlePrev = undefined ;
Immediate . prototype . _ idlePrev = undefined ;
exports . setImmediate = function ( callback ) {
exports . setImmediate = function ( callback , arg1 , arg2 , arg3 ) {
var i , args ;
var len = arguments . length ;
var immediate = new Immediate ( ) ;
var immediate = new Immediate ( ) ;
var args , index ;
L . init ( immediate ) ;
L . init ( immediate ) ;
immediate . _ onImmediate = callback ;
switch ( len ) {
// fast cases
if ( arguments . length > 1 ) {
case 0 :
args = [ ] ;
case 1 :
for ( index = 1 ; index < arguments . length ; index ++ )
immediate . _ onImmediate = callback ;
args . push ( arguments [ index ] ) ;
break ;
case 2 :
immediate . _ onImmediate = function ( ) {
immediate . _ onImmediate = function ( ) {
callback . apply ( immediate , args ) ;
callback . call ( immediate , arg1 ) ;
} ;
} ;
break ;
case 3 :
immediate . _ onImmediate = function ( ) {
callback . call ( immediate , arg1 , arg2 ) ;
} ;
break ;
case 4 :
immediate . _ onImmediate = function ( ) {
callback . call ( immediate , arg1 , arg2 , arg3 ) ;
} ;
break ;
// slow case
default :
args = new Array ( len - 1 ) ;
for ( i = 1 ; i < len ; i ++ )
args [ i - 1 ] = arguments [ i ] ;
immediate . _ onImmediate = function ( ) {
callback . apply ( immediate , args ) ;
} ;
break ;
}
}
if ( ! process . _ needImmediateCallback ) {
if ( ! process . _ needImmediateCallback ) {