Skip to content
Browse files

ensure that functions are only called once, when chaining comparators

  • Loading branch information...
1 parent 9679fc0 commit 0bc4da2b51eaa467fcee2535bf03898b0ae13747 @jashkenas jashkenas committed Jan 16, 2010
Showing with 18 additions and 2 deletions.
  1. +8 −1 lib/coffee_script/nodes.rb
  2. +10 −1 test/fixtures/execution/test_operations.coffee
View
9 lib/coffee_script/nodes.rb
@@ -544,6 +544,7 @@ def compile_splice(o)
class OpNode < Node
children :first, :second
attr_reader :operator
+ attr_accessor :second
CONVERSIONS = {
:== => "===",
@@ -581,7 +582,13 @@ def compile_node(o)
# Mimic Python's chained comparisons. See:
# http://docs.python.org/reference/expressions.html#notin
def compile_chain(o)
- write("(#{@first.compile(o)}) && (#{@first.unwrap.second.compile(o)} #{@operator} #{@second.compile(o)})")
+ shared = @first.unwrap.second
+ if shared.is_a?(CallNode)
+ temp = o[:scope].free_variable
+ @first.second = ParentheticalNode.new(AssignNode.new(temp, shared))
+ shared = temp
+ end
+ write("(#{@first.compile(o)}) && (#{shared.compile(o)} #{@operator} #{@second.compile(o)})")
end
def compile_conditional(o)
View
11 test/fixtures/execution/test_operations.coffee
@@ -6,4 +6,13 @@ print(true is not false is true is not false)
print(10 < 20 > 10)
-print(50 > 10 > 5 is parseInt('5', 10))
+print(50 > 10 > 5 is parseInt('5', 10))
+
+
+# Make sure that each argument is only evaluated once, even if used
+# more than once.
+
+i: 0
+func: => i++
+
+print(1 > func() < 1)

0 comments on commit 0bc4da2

Please sign in to comment.
Something went wrong with that request. Please try again.