Skip to content

Commit cf11a57

Browse files
committed
Fixes #2489, fixes #1819, fixes #1821 -- remove the __bind helper.
1 parent 9fe0e71 commit cf11a57

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

lib/coffee-script/nodes.js

Lines changed: 13 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -868,9 +868,22 @@ exports.Class = class Class extends Base
868868
# Ensure that all functions bound to the instance are proxied in the
869869
# constructor.
870870
addBoundFunctions: (o) ->
871-
for bvar in @boundFuncs
872-
lhs = (new Value (new Literal "this"), [new Access bvar]).compile o
873-
@ctor.body.unshift new Literal "#{lhs} = #{utility 'bind'}(#{lhs}, this)"
871+
if @boundFuncs.length
872+
o.scope.assign '_this', 'this'
873+
for [name, func] in @boundFuncs
874+
lhs = new Value (new Literal "this"), [new Access name]
875+
body = new Block [new Return new Literal "#{@ctor.name}.prototype.#{name.value}.apply(_this, arguments)"]
876+
rhs = new Code func.params, body, 'boundfunc'
877+
bound = new Assign lhs, rhs
878+
879+
@ctor.body.unshift bound
880+
881+
# {base} = assign.variable
882+
# lhs = (new Value (new Literal "this"), [new Access base]).compile o
883+
# @ctor.body.unshift new Literal """#{lhs} = function() {
884+
# #{o.indent} return #{@ctor.name}.prototype.#{base.value}.apply(_this, arguments);
885+
# #{o.indent}}\n
886+
# """
874887
return
875888

876889
# Merge the properties from a top-level object as prototypal properties
@@ -900,7 +913,7 @@ exports.Class = class Class extends Base
900913
else
901914
assign.variable = new Value(new Literal(name), [(new Access new Literal 'prototype'), new Access base ])
902915
if func instanceof Code and func.bound
903-
@boundFuncs.push base
916+
@boundFuncs.push [base, func]
904917
func.bound = no
905918
assign
906919
compact exprs
@@ -1960,11 +1973,6 @@ UTILITIES =
19601973
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; }
19611974
"""
19621975

1963-
# Create a function bound to the current value of "this".
1964-
bind: -> '''
1965-
function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
1966-
'''
1967-
19681976
# Discover if an item is in an array.
19691977
indexOf: -> """
19701978
[].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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,3 +728,15 @@ test "#2359: extending native objects that use other typed constructors requires
728728
workingArray = new WorkingArray
729729
ok workingArray instanceof WorkingArray
730730
eq 'yes!', workingArray.method()
731+
732+
733+
test "#2489: removing __bind", ->
734+
735+
class Thing
736+
foo: (a, b, c) ->
737+
bar: (a, b, c) =>
738+
739+
thing = new Thing
740+
741+
eq thing.foo.length, 3
742+
eq thing.bar.length, 3

0 commit comments

Comments
 (0)