mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 03:21:20 -05:00
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
This commit is contained in:
@@ -149,8 +149,11 @@ module CoffeeScript
|
|||||||
# at the top.
|
# at the top.
|
||||||
def compile_with_declarations(o={})
|
def compile_with_declarations(o={})
|
||||||
code = compile_node(o)
|
code = compile_node(o)
|
||||||
code = "#{idt}var #{o[:scope].compiled_assignments};\n#{code}" if o[:scope].assignments?(self)
|
args = self.contains? {|n| n.is_a?(LiteralNode) && n.arguments? }
|
||||||
code = "#{idt}var #{o[:scope].compiled_declarations};\n#{code}" if o[:scope].declarations?(self)
|
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)
|
write(code)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -182,10 +185,6 @@ module CoffeeScript
|
|||||||
# sense returning or assigning them.
|
# sense returning or assigning them.
|
||||||
STATEMENTS = ['break', 'continue']
|
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.
|
# Wrap up a compiler-generated string as a LiteralNode.
|
||||||
def self.wrap(string)
|
def self.wrap(string)
|
||||||
self.new(Value.new(string))
|
self.new(Value.new(string))
|
||||||
@@ -200,8 +199,11 @@ module CoffeeScript
|
|||||||
end
|
end
|
||||||
alias_method :statement_only?, :statement?
|
alias_method :statement_only?, :statement?
|
||||||
|
|
||||||
|
def arguments?
|
||||||
|
@value.to_s == 'arguments'
|
||||||
|
end
|
||||||
|
|
||||||
def compile_node(o)
|
def compile_node(o)
|
||||||
@value = ARG_ARRAY if @value.to_s.to_sym == :arguments
|
|
||||||
indent = statement? ? idt : ''
|
indent = statement? ? idt : ''
|
||||||
ending = statement? ? ';' : ''
|
ending = statement? ? ';' : ''
|
||||||
write "#{indent}#{@value}#{ending}"
|
write "#{indent}#{@value}#{ending}"
|
||||||
|
|||||||
@@ -22,3 +22,11 @@ curried: =>
|
|||||||
print(area.apply(this, arguments.concat(20, 20)) is 100)
|
print(area.apply(this, arguments.concat(20, 20)) is 100)
|
||||||
|
|
||||||
curried(10, 10)
|
curried(10, 10)
|
||||||
|
|
||||||
|
|
||||||
|
# Arguments is not a special keyword -- it can be assigned to:
|
||||||
|
func: =>
|
||||||
|
arguments: 25
|
||||||
|
arguments
|
||||||
|
|
||||||
|
print(func(100) is 25)
|
||||||
|
|||||||
Reference in New Issue
Block a user