Skip to content

Commit 67de35f

Browse files
committed
Fixes #2781, fixes #2782. Reverting to old method of instance method binding.
1 parent 774ee6a commit 67de35f

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

lib/coffee-script/nodes.js

+10-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

+9-9
Original file line numberDiff line numberDiff line change
@@ -983,14 +983,9 @@ exports.Class = class Class extends Base
983983
# Ensure that all functions bound to the instance are proxied in the
984984
# constructor.
985985
addBoundFunctions: (o) ->
986-
if @boundFuncs.length
987-
o.scope.assign '_this', 'this'
988-
for [name, func] in @boundFuncs
989-
lhs = new Value (new Literal "this"), [new Access name]
990-
body = new Block [new Return new Literal "#{@ctor.name}.prototype.#{name.value}.apply(_this, arguments)"]
991-
rhs = new Code func.params, body, 'boundfunc'
992-
bound = new Assign lhs, rhs
993-
@ctor.body.push bound
986+
for bvar in @boundFuncs
987+
lhs = (new Value (new Literal "this"), [new Access bvar]).compile o
988+
@ctor.body.unshift new Literal "#{lhs} = #{utility 'bind'}(#{lhs}, this)"
994989
return
995990

996991
# Merge the properties from a top-level object as prototypal properties
@@ -1020,7 +1015,7 @@ exports.Class = class Class extends Base
10201015
else
10211016
assign.variable = new Value(new Literal(name), [(new Access new Literal 'prototype'), new Access base ])
10221017
if func instanceof Code and func.bound
1023-
@boundFuncs.push [base, func]
1018+
@boundFuncs.push base
10241019
func.bound = no
10251020
assign
10261021
compact exprs
@@ -2122,6 +2117,11 @@ UTILITIES =
21222117
function(child, parent) { for (var key in parent) { if (#{utility 'hasProp'}.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }
21232118
"""
21242119

2120+
# Create a function bound to the current value of "this".
2121+
bind: -> '''
2122+
function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
2123+
'''
2124+
21252125
# Discover if an item is in an array.
21262126
indexOf: -> """
21272127
[].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }

test/classes.coffee

+18-14
Original file line numberDiff line numberDiff line change
@@ -730,23 +730,27 @@ test "#2359: extending native objects that use other typed constructors requires
730730
eq 'yes!', workingArray.method()
731731

732732

733-
test "#2489: removing __bind", ->
734-
735-
class Thing
736-
foo: (a, b, c) ->
737-
bar: (a, b, c) =>
733+
test "#2782: non-alphanumeric-named bound functions", ->
734+
class A
735+
'b:c': =>
736+
'd'
738737

739-
thing = new Thing
738+
eq (new A)['b:c'](), 'd'
740739

741-
eq thing.foo.length, 3
742-
eq thing.bar.length, 3
743740

741+
test "#2781: overriding bound functions", ->
742+
class A
743+
a: ->
744+
@b()
745+
b: =>
746+
1
744747

745-
test "#2773: overriding bound functions", ->
746-
class Foo
747-
method: => 'Foo'
748+
class B extends A
749+
b: =>
750+
2
748751

749-
class Bar extends Foo
750-
method: => 'Bar'
752+
b = (new A).b
753+
eq b(), 1
751754

752-
eq (new Bar).method(), 'Bar'
755+
b = (new B).b
756+
eq b(), 2

0 commit comments

Comments
 (0)