Skip to content

Commit

Permalink
Merge pull request #1999 from karalabe/javascript-timer-argcheck
Browse files Browse the repository at this point in the history
jrse: fix #1082, fail if setTimeout/setInterval lack callback
  • Loading branch information
obscuren committed Nov 26, 2015
2 parents b9db5b3 + f27e826 commit de75d54
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions jsre/jsre.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func (self *JSRE) runEventLoop() {
ready := make(chan *jsTimer)

newTimer := func(call otto.FunctionCall, interval bool) (*jsTimer, otto.Value) {

delay, _ := call.Argument(1).ToInteger()
if 0 >= delay {
delay = 1
Expand All @@ -105,7 +104,6 @@ func (self *JSRE) runEventLoop() {
if err != nil {
panic(err)
}

return timer, value
}

Expand All @@ -127,8 +125,20 @@ func (self *JSRE) runEventLoop() {
}
return otto.UndefinedValue()
}
vm.Set("setTimeout", setTimeout)
vm.Set("setInterval", setInterval)
vm.Set("_setTimeout", setTimeout)
vm.Set("_setInterval", setInterval)
vm.Run(`var setTimeout = function(args) {
if (arguments.length < 1) {
throw TypeError("Failed to execute 'setTimeout': 1 argument required, but only 0 present.");
}
return _setTimeout.apply(this, arguments);
}`)
vm.Run(`var setInterval = function(args) {
if (arguments.length < 1) {
throw TypeError("Failed to execute 'setInterval': 1 argument required, but only 0 present.");
}
return _setInterval.apply(this, arguments);
}`)
vm.Set("clearTimeout", clearTimeout)
vm.Set("clearInterval", clearTimeout)

Expand All @@ -154,7 +164,7 @@ loop:
if err != nil {
fmt.Println("js error:", err, arguments)
}

_, inreg := registry[timer] // when clearInterval is called from within the callback don't reset it
if timer.interval && inreg {
timer.timer.Reset(timer.duration)
Expand Down

0 comments on commit de75d54

Please sign in to comment.