@@ -985,9 +985,19 @@ exports.Class = class Class extends Base
985
985
# Ensure that all functions bound to the instance are proxied in the
986
986
# constructor.
987
987
addBoundFunctions : (o ) ->
988
- for bvar in @boundFuncs
989
- lhs = (new Value (new Literal " this" ), [new Access bvar]).compile o
990
- @ctor .body .unshift new Literal " #{ lhs} = #{ utility ' bind' } (#{ lhs} , this)"
988
+ if @boundFuncs .length
989
+ # Use a temporary Scope just to create some free variables.
990
+ # TODO: This is ugly. This should be the @ctor's scope.
991
+ scope = new Scope o .scope , @ctor .body
992
+ for [name, func] in @boundFuncs
993
+ varName = scope .freeVariable if IDENTIFIER .test (name .value ) then name .value else ' bound'
994
+ varDecl = new Assign new Literal (varName), new Value (new Literal (" this" ), [new Access name])
995
+ lhs = new Value (new Literal " this" ), [new Access name]
996
+ body = new Block [new Return new Literal " #{ varName} .apply(_this, arguments)" ]
997
+ rhs = new Code func .params , body, ' boundfunc'
998
+ bound = new Assign lhs, rhs
999
+ @ctor .body .unshift bound
1000
+ @ctor .body .unshift varDecl
991
1001
return
992
1002
993
1003
# Merge the properties from a top-level object as prototypal properties
@@ -1017,7 +1027,7 @@ exports.Class = class Class extends Base
1017
1027
else
1018
1028
assign .variable = new Value (new Literal (name), [(new Access new Literal ' prototype' ), new Access base ])
1019
1029
if func instanceof Code and func .bound
1020
- @boundFuncs .push base
1030
+ @boundFuncs .push [ base, func]
1021
1031
func .bound = no
1022
1032
assign
1023
1033
compact exprs
@@ -1292,6 +1302,8 @@ exports.Code = class Code extends Base
1292
1302
delete o .isExistentialEquals
1293
1303
params = []
1294
1304
exprs = []
1305
+ # Clean params references in case the params where used in other Code nodes.
1306
+ delete param .reference for param in @params
1295
1307
@eachParamName (name ) -> # this step must be performed before the others
1296
1308
unless o .scope .check name then o .scope .parameter name
1297
1309
for param in @params when param .splat
@@ -2115,11 +2127,6 @@ UTILITIES =
2115
2127
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; }
2116
2128
"""
2117
2129
2118
- # Create a function bound to the current value of "this".
2119
- bind : -> '''
2120
- function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
2121
- '''
2122
-
2123
2130
# Discover if an item is in an array.
2124
2131
indexOf : -> """
2125
2132
[].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }
0 commit comments