@@ -985,9 +985,21 @@ 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
+ exprs = []
993
+ for [name, func] in @boundFuncs
994
+ varName = scope .freeVariable if IDENTIFIER .test (name .value ) then name .value else ' bound'
995
+ varDecl = new Assign new Literal (varName), new Value (new Literal (" this" ), [new Access name])
996
+ exprs .push varDecl
997
+ lhs = new Value (new Literal " this" ), [new Access name]
998
+ body = new Block [new Return new Literal " #{ varName} .apply(_this, arguments)" ]
999
+ rhs = new Code func .params , body, ' boundfunc'
1000
+ bound = new Assign lhs, rhs
1001
+ exprs .push bound
1002
+ @ctor .body .unshift expr for expr in exprs by - 1
991
1003
return
992
1004
993
1005
# Merge the properties from a top-level object as prototypal properties
@@ -1017,7 +1029,7 @@ exports.Class = class Class extends Base
1017
1029
else
1018
1030
assign .variable = new Value (new Literal (name), [(new Access new Literal ' prototype' ), new Access base ])
1019
1031
if func instanceof Code and func .bound
1020
- @boundFuncs .push base
1032
+ @boundFuncs .push [ base, func]
1021
1033
func .bound = no
1022
1034
assign
1023
1035
compact exprs
@@ -1292,6 +1304,8 @@ exports.Code = class Code extends Base
1292
1304
delete o .isExistentialEquals
1293
1305
params = []
1294
1306
exprs = []
1307
+ # Clean params references in case the params where used in other Code nodes.
1308
+ delete param .reference for param in @params
1295
1309
@eachParamName (name ) -> # this step must be performed before the others
1296
1310
unless o .scope .check name then o .scope .parameter name
1297
1311
for param in @params when param .splat
@@ -2115,11 +2129,6 @@ UTILITIES =
2115
2129
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
2130
"""
2117
2131
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
2132
# Discover if an item is in an array.
2124
2133
indexOf : -> """
2125
2134
[].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