1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Fixes #3259 -- Use placeholders when adding params to scope

Don't register nested variables of complex parameters as parameters of the compiled function. Use the computed placeholder name instead.
This commit is contained in:
Marc Häfner 2013-11-26 06:40:09 +01:00
parent 210376f7a9
commit 873ed071d4
3 changed files with 32 additions and 22 deletions

View file

@ -1813,7 +1813,7 @@
}; };
Code.prototype.compileNode = function(o) { Code.prototype.compileNode = function(o) {
var answer, boundfunc, code, exprs, i, lit, p, param, params, ref, splats, uniqs, val, wasEmpty, wrapper, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref2, _ref3, _ref4, _ref5, _ref6; var answer, boundfunc, code, exprs, i, lit, p, param, params, ref, splats, uniqs, val, wasEmpty, wrapper, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _m, _n, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
if (this.bound && ((_ref2 = o.scope.method) != null ? _ref2.bound : void 0)) { if (this.bound && ((_ref2 = o.scope.method) != null ? _ref2.bound : void 0)) {
this.context = o.scope.method.context; this.context = o.scope.method.context;
} }
@ -1831,20 +1831,20 @@
delete o.isExistentialEquals; delete o.isExistentialEquals;
params = []; params = [];
exprs = []; exprs = [];
this.eachParamName(function(name) {
if (!o.scope.check(name)) {
return o.scope.parameter(name);
}
});
_ref3 = this.params; _ref3 = this.params;
for (_i = 0, _len = _ref3.length; _i < _len; _i++) { for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
param = _ref3[_i]; param = _ref3[_i];
o.scope.parameter(param.asReference(o));
}
_ref4 = this.params;
for (_j = 0, _len1 = _ref4.length; _j < _len1; _j++) {
param = _ref4[_j];
if (!param.splat) { if (!param.splat) {
continue; continue;
} }
_ref4 = this.params; _ref5 = this.params;
for (_j = 0, _len1 = _ref4.length; _j < _len1; _j++) { for (_k = 0, _len2 = _ref5.length; _k < _len2; _k++) {
p = _ref4[_j].name; p = _ref5[_k].name;
if (p["this"]) { if (p["this"]) {
p = p.properties[0].name; p = p.properties[0].name;
} }
@ -1853,20 +1853,20 @@
} }
} }
splats = new Assign(new Value(new Arr((function() { splats = new Assign(new Value(new Arr((function() {
var _k, _len2, _ref5, _results; var _l, _len3, _ref6, _results;
_ref5 = this.params; _ref6 = this.params;
_results = []; _results = [];
for (_k = 0, _len2 = _ref5.length; _k < _len2; _k++) { for (_l = 0, _len3 = _ref6.length; _l < _len3; _l++) {
p = _ref5[_k]; p = _ref6[_l];
_results.push(p.asReference(o)); _results.push(p.asReference(o));
} }
return _results; return _results;
}).call(this))), new Value(new Literal('arguments'))); }).call(this))), new Value(new Literal('arguments')));
break; break;
} }
_ref5 = this.params; _ref6 = this.params;
for (_k = 0, _len2 = _ref5.length; _k < _len2; _k++) { for (_l = 0, _len3 = _ref6.length; _l < _len3; _l++) {
param = _ref5[_k]; param = _ref6[_l];
if (param.isComplex()) { if (param.isComplex()) {
val = ref = param.asReference(o); val = ref = param.asReference(o);
if (param.value) { if (param.value) {
@ -1892,9 +1892,9 @@
exprs.unshift(splats); exprs.unshift(splats);
} }
if (exprs.length) { if (exprs.length) {
(_ref6 = this.body.expressions).unshift.apply(_ref6, exprs); (_ref7 = this.body.expressions).unshift.apply(_ref7, exprs);
} }
for (i = _l = 0, _len3 = params.length; _l < _len3; i = ++_l) { for (i = _m = 0, _len4 = params.length; _m < _len4; i = ++_m) {
p = params[i]; p = params[i];
params[i] = p.compileToFragments(o); params[i] = p.compileToFragments(o);
o.scope.parameter(fragmentsToText(params[i])); o.scope.parameter(fragmentsToText(params[i]));
@ -1915,7 +1915,7 @@
} }
code += '('; code += '(';
answer = [this.makeCode(code)]; answer = [this.makeCode(code)];
for (i = _m = 0, _len4 = params.length; _m < _len4; i = ++_m) { for (i = _n = 0, _len5 = params.length; _n < _len5; i = ++_n) {
p = params[i]; p = params[i];
if (i) { if (i) {
answer.push(this.makeCode(", ")); answer.push(this.makeCode(", "));

View file

@ -1336,8 +1336,8 @@ exports.Code = class Code extends Base
delete o.isExistentialEquals delete o.isExistentialEquals
params = [] params = []
exprs = [] exprs = []
@eachParamName (name) -> # this step must be performed before the others for param in @params
unless o.scope.check name then o.scope.parameter name o.scope.parameter param.asReference o
for param in @params when param.splat for param in @params when param.splat
for {name: p} in @params for {name: p} in @params
if p.this then p = p.properties[0].name if p.this then p = p.properties[0].name

View file

@ -103,4 +103,14 @@ test "#2331: bound super regression", ->
class B extends A class B extends A
method: => super method: => super
eq (new B).method(), 'A' eq (new B).method(), 'A'
test "#3259: leak with @-params within destructured parameters", ->
fn = ({@foo}, [@bar], [{@baz}]) ->
foo = bar = baz = false
fn.call {}, {foo: 'foo'}, ['bar'], [{baz: 'baz'}]
eq 'undefined', typeof foo
eq 'undefined', typeof bar
eq 'undefined', typeof baz