diff --git a/documentation/coffee/super.coffee b/documentation/coffee/super.coffee index fa4b41ba..25f57985 100644 --- a/documentation/coffee/super.coffee +++ b/documentation/coffee/super.coffee @@ -1,19 +1,28 @@ Animal: -> + Animal::move: (meters) -> alert @name + " moved " + meters + "m." -Snake: (name) -> @name: name -Snake extends Animal +Snake: (name) -> + @name: name + this + Snake::move: -> alert "Slithering..." super 5 -Horse: (name) -> @name: name -Horse extends Animal +Snake extends Animal + +Horse: (name) -> + @name: name + this + Horse::move: -> alert "Galloping..." super 45 +Horse extends Animal + sam: new Snake "Sammy the Python" tom: new Horse "Tommy the Palomino" diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 90da709d..0d160e17 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -171,11 +171,8 @@ module CoffeeScript return "#{stmt ? '' : idt}#{node.compile(o.merge(:top => true))}#{stmt ? '' : ';'}" unless returns # If it's a statement, the node knows how to return itself. return node.compile(o.merge(:return => true)) if node.statement? - # If it's not part of a constructor, we can just return the value of the expression. - return "#{idt}return #{node.compile(o)};" unless o[:scope].function && o[:scope].function.constructor? - # It's the last line of a constructor, add a safety check. - temp = o[:scope].free_variable - "#{idt}#{temp} = #{node.compile(o)};\n#{idt}return #{o[:scope].function.name} === this.constructor ? this : #{temp};" + # Otherwise, we can just return the value of the expression. + return "#{idt}return #{node.compile(o)};" end end diff --git a/src/nodes.coffee b/src/nodes.coffee index 9517b573..be2cad3e 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -220,11 +220,9 @@ Expressions: exports.Expressions: inherit Node, { return (if stmt then '' else @idt()) + node.compile(merge(o, {top: true})) + (if stmt then '' else ';') unless returns # If it's a statement, the node knows how to return itself. return node.compile(merge(o, {returns: true})) if node.is_statement() - # If it's not part of a constructor, we can just return the value of the expression. - return @idt() + 'return ' + node.compile(o) unless o.scope.method?.is_constructor() - # It's the last line of a constructor, add a safety check. - temp: o.scope.free_variable() - @idt() + temp + ' = ' + node.compile(o) + ";\n" + @idt() + "return " + o.scope.method.name + ' === this.constructor ? this : ' + temp + ';' + # Otherwise, we can just return the value of the expression. + return @idt() + 'return ' + node.compile(o) + } # Wrap up a node as an Expressions, unless it already is one. diff --git a/test/fixtures/execution/test_calling_super.coffee b/test/fixtures/execution/test_calling_super.coffee index a72fcdd6..7e4ab2d8 100644 --- a/test/fixtures/execution/test_calling_super.coffee +++ b/test/fixtures/execution/test_calling_super.coffee @@ -14,6 +14,7 @@ SecondChild::func: (string) -> ThirdChild: -> @array: [1, 2, 3] + this ThirdChild extends SecondChild ThirdChild::func: (string) -> super('three/') + string @@ -25,12 +26,15 @@ puts result is 'zero/one/two/three/four' TopClass: (arg) -> @prop: 'top-' + arg + this SuperClass: (arg) -> super 'super-' + arg + this SubClass: -> super 'sub' + this SuperClass extends TopClass SubClass extends SuperClass