diff --git a/lib/nodes.js b/lib/nodes.js index e473bfdd..cf23115d 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1734,19 +1734,19 @@ }; Closure = { wrap: function(expressions, statement, noReturn) { - var args, call, func, mentionsArgs; + var args, call, func, mentionsArgs, meth; if (expressions.containsPureStatement()) { return expressions; } - mentionsArgs = expressions.contains(this.literalArgs); - func = new Parens(new Code((mentionsArgs ? [new Literal('arguments')] : []), Expressions.wrap([expressions]))); + func = new Parens(new Code([], Expressions.wrap([expressions]))); args = []; - if (mentionsArgs || expressions.contains(this.literalThis)) { + if ((mentionsArgs = expressions.contains(this.literalArgs)) || (expressions.contains(this.literalThis))) { + meth = new Literal(mentionsArgs ? 'apply' : 'call'); args = [new Literal('this')]; if (mentionsArgs) { args.push(new Literal('arguments')); } - func = new Value(func, [new Accessor(new Literal('call'))]); + func = new Value(func, [new Accessor(meth)]); func.noReturn = noReturn; } call = new Call(func, args); diff --git a/src/nodes.coffee b/src/nodes.coffee index b6baeb10..31cb04bd 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1480,13 +1480,14 @@ Closure = # then make sure that the closure wrapper preserves the original values. wrap: (expressions, statement, noReturn) -> return expressions if expressions.containsPureStatement() - mentionsArgs = expressions.contains @literalArgs - func = new Parens new Code (if mentionsArgs then [new Literal 'arguments'] else []), Expressions.wrap [expressions] + func = new Parens new Code [], Expressions.wrap [expressions] args = [] - if mentionsArgs or expressions.contains @literalThis + if (mentionsArgs = expressions.contains @literalArgs) or + ( expressions.contains @literalThis) + meth = new Literal if mentionsArgs then 'apply' else 'call' args = [new Literal 'this'] args.push new Literal 'arguments' if mentionsArgs - func = new Value func, [new Accessor new Literal 'call'] + func = new Value func, [new Accessor meth] func.noReturn = noReturn call = new Call func, args if statement then Expressions.wrap [call] else call diff --git a/test/test_comprehensions.coffee b/test/test_comprehensions.coffee index 042f7601..cebe40d7 100644 --- a/test/test_comprehensions.coffee +++ b/test/test_comprehensions.coffee @@ -70,9 +70,7 @@ ok result.join(' ') is '6 4 2' # Closure-wrapped comprehensions that refer to the "arguments" object. expr = -> - result = for item in arguments - ok arguments.callee is expr - item * item + result = item * item for item in arguments ok expr(2, 4, 8).join(' ') is '4 16 64'