1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Fix #3361, make %% coerce right operand only once

Force coercion of right operand once before doing arithmetic with it in the `__modulo` utility function.
This commit is contained in:
Demian Ferreiro 2014-02-08 13:14:16 -03:00
parent 46f55d1bb4
commit 0ad30e9b3f
3 changed files with 8 additions and 2 deletions

View file

@ -3075,7 +3075,7 @@
return "[].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }";
},
modulo: function() {
return "function(a, b) { return (a % b + +b) % b; }";
return "function(a, b) { b = +b; return (a % b + b) % b; }";
},
hasProp: function() {
return '{}.hasOwnProperty';

View file

@ -2194,7 +2194,7 @@ UTILITIES =
"
modulo: -> """
function(a, b) { return (a % b + +b) % b; }
function(a, b) { b = +b; return (a % b + b) % b; }
"""
# Shortcuts to speed up the lookup time for native functions.

View file

@ -357,3 +357,9 @@ test "modulo operator converts arguments to numbers", ->
eq 1, 1 %% '42'
eq 1, '1' %% 42
eq 1, '1' %% '42'
test "#3361: Modulo operator coerces right operand once", ->
count = 0
res = 42 %% valueOf: -> count += 1
eq 1, count
eq 0, res