removing the constructor safety check -- it wasn't safe enough (Issue 36)

This commit is contained in:
Jeremy Ashkenas 2010-02-09 07:59:48 -05:00
parent 522df2a355
commit c6a6788694
4 changed files with 22 additions and 14 deletions

View File

@ -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"

View File

@ -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

View File

@ -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.

View File

@ -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