From 9eff443032cc7c9f1cd3d6a1265e73da3fe2106e Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 16 Jan 2010 12:52:26 -0500 Subject: [PATCH] arguments no longer is just a find-and-replace -- it'll fix the arguments variable at the top of scope if you use it in a function body --- lib/coffee_script/nodes.rb | 16 +++++++++------- test/fixtures/execution/test_arguments.coffee | 8 ++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 62c586d3..8c856388 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -149,8 +149,11 @@ module CoffeeScript # at the top. def compile_with_declarations(o={}) code = compile_node(o) - code = "#{idt}var #{o[:scope].compiled_assignments};\n#{code}" if o[:scope].assignments?(self) - code = "#{idt}var #{o[:scope].compiled_declarations};\n#{code}" if o[:scope].declarations?(self) + args = self.contains? {|n| n.is_a?(LiteralNode) && n.arguments? } + argv = args && o[:scope].check('arguments') ? '' : 'var ' + code = "#{idt}#{argv}arguments = Array.prototype.slice.call(arguments, 0);\n#{code}" if args + code = "#{idt}var #{o[:scope].compiled_assignments};\n#{code}" if o[:scope].assignments?(self) + code = "#{idt}var #{o[:scope].compiled_declarations};\n#{code}" if o[:scope].declarations?(self) write(code) end @@ -182,10 +185,6 @@ module CoffeeScript # sense returning or assigning them. STATEMENTS = ['break', 'continue'] - # If we get handed a literal reference to an arguments object, convert - # it to an array. - ARG_ARRAY = 'Array.prototype.slice.call(arguments, 0)' - # Wrap up a compiler-generated string as a LiteralNode. def self.wrap(string) self.new(Value.new(string)) @@ -200,8 +199,11 @@ module CoffeeScript end alias_method :statement_only?, :statement? + def arguments? + @value.to_s == 'arguments' + end + def compile_node(o) - @value = ARG_ARRAY if @value.to_s.to_sym == :arguments indent = statement? ? idt : '' ending = statement? ? ';' : '' write "#{indent}#{@value}#{ending}" diff --git a/test/fixtures/execution/test_arguments.coffee b/test/fixtures/execution/test_arguments.coffee index d5c94909..ecaf118a 100644 --- a/test/fixtures/execution/test_arguments.coffee +++ b/test/fixtures/execution/test_arguments.coffee @@ -22,3 +22,11 @@ curried: => print(area.apply(this, arguments.concat(20, 20)) is 100) curried(10, 10) + + +# Arguments is not a special keyword -- it can be assigned to: +func: => + arguments: 25 + arguments + +print(func(100) is 25)