Changing implementation of bound functions to mirror sethaurus' suggestion for cleaner output. Issue #530. Tests pass.

This commit is contained in:
Jeremy Ashkenas
2010-07-23 22:34:56 -07:00
parent de9fb7777b
commit e41abe2d83
3 changed files with 67 additions and 78 deletions

View File

@@ -203,6 +203,11 @@
this.expressions[idx] = last.makeReturn(); this.expressions[idx] = last.makeReturn();
return this; return this;
}; };
Expressions.prototype.rewriteThis = function() {
return this.traverseChildren(false, function(child) {
return child instanceof ValueNode && child.base.value === 'this' ? (child.base = literal('_this')) : null;
});
};
Expressions.prototype.compile = function(o) { Expressions.prototype.compile = function(o) {
o = o || {}; o = o || {};
return o.scope ? Expressions.__superClass__.compile.call(this, o) : this.compileRoot(o); return o.scope ? Expressions.__superClass__.compile.call(this, o) : this.compileRoot(o);
@@ -942,7 +947,7 @@
CodeNode.prototype['class'] = 'CodeNode'; CodeNode.prototype['class'] = 'CodeNode';
CodeNode.prototype.children = ['params', 'body']; CodeNode.prototype.children = ['params', 'body'];
CodeNode.prototype.compileNode = function(o) { CodeNode.prototype.compileNode = function(o) {
var _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, code, func, i, inner, param, params, sharedScope, splat, top; var _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, code, func, i, param, params, sharedScope, splat, top;
sharedScope = del(o, 'sharedScope'); sharedScope = del(o, 'sharedScope');
top = del(o, 'top'); top = del(o, 'top');
o.scope = sharedScope || new Scope(o.scope, this.body, this); o.scope = sharedScope || new Scope(o.scope, this.body, this);
@@ -978,6 +983,9 @@
return _e; return _e;
})(); })();
this.body.makeReturn(); this.body.makeReturn();
if (this.bound) {
this.body.rewriteThis();
}
_j = params; _j = params;
for (_i = 0, _k = _j.length; _i < _k; _i++) { for (_i = 0, _k = _j.length; _i < _k; _i++) {
param = _j[_i]; param = _j[_i];
@@ -991,8 +999,7 @@
if (!(this.bound)) { if (!(this.bound)) {
return func; return func;
} }
inner = ("(function() {\n" + (this.idt(2)) + "return __func.apply(__this, arguments);\n" + (this.idt(1)) + "});"); return "(function(_this) {\n" + (this.idt(1)) + "return " + func + "\n" + this.tab + "})(this)";
return "(function(__this) {\n" + (this.idt(1)) + "var __func = " + func + ";\n" + (this.idt(1)) + "return " + inner + "\n" + this.tab + "})(this)";
}; };
CodeNode.prototype.topSensitive = function() { CodeNode.prototype.topSensitive = function() {
return true; return true;

View File

@@ -37,32 +37,29 @@
return true; return true;
}; };
Rewriter.prototype.adjustComments = function() { Rewriter.prototype.adjustComments = function() {
return this.scanTokens((function(__this) { return this.scanTokens((function(_this) {
var __func = function(prev, token, post, i) { return function(prev, token, post, i) {
var _c, _d, after, before; var _c, _d, after, before;
if (!(token[0] === 'HERECOMMENT')) { if (!(token[0] === 'HERECOMMENT')) {
return 1; return 1;
} }
_c = [this.tokens[i - 2], this.tokens[i + 2]]; _c = [_this.tokens[i - 2], _this.tokens[i + 2]];
before = _c[0]; before = _c[0];
after = _c[1]; after = _c[1];
if (after && after[0] === 'INDENT') { if (after && after[0] === 'INDENT') {
this.tokens.splice(i + 2, 1); _this.tokens.splice(i + 2, 1);
before && before[0] === 'OUTDENT' && post && (prev[0] === post[0]) && (post[0] === 'TERMINATOR') ? this.tokens.splice(i - 2, 1) : this.tokens.splice(i, 0, after); before && before[0] === 'OUTDENT' && post && (prev[0] === post[0]) && (post[0] === 'TERMINATOR') ? _this.tokens.splice(i - 2, 1) : _this.tokens.splice(i, 0, after);
} else if (prev && !('TERMINATOR' === (_d = prev[0]) || 'INDENT' === _d || 'OUTDENT' === _d)) { } else if (prev && !('TERMINATOR' === (_d = prev[0]) || 'INDENT' === _d || 'OUTDENT' === _d)) {
if (post && post[0] === 'TERMINATOR' && after && after[0] === 'OUTDENT') { if (post && post[0] === 'TERMINATOR' && after && after[0] === 'OUTDENT') {
this.tokens.splice.apply(this.tokens, [i + 2, 0].concat(this.tokens.splice(i, 2))); _this.tokens.splice.apply(_this.tokens, [i + 2, 0].concat(_this.tokens.splice(i, 2)));
this.tokens[i + 2][0] !== 'TERMINATOR' ? this.tokens.splice(i + 2, 0, ['TERMINATOR', "\n", prev[2]]) : null; _this.tokens[i + 2][0] !== 'TERMINATOR' ? _this.tokens.splice(i + 2, 0, ['TERMINATOR', "\n", prev[2]]) : null;
} else { } else {
this.tokens.splice(i, 0, ['TERMINATOR', "\n", prev[2]]); _this.tokens.splice(i, 0, ['TERMINATOR', "\n", prev[2]]);
} }
return 2; return 2;
} }
return 1; return 1;
}; }
return (function() {
return __func.apply(__this, arguments);
});
})(this)); })(this));
}; };
Rewriter.prototype.removeLeadingNewlines = function() { Rewriter.prototype.removeLeadingNewlines = function() {
@@ -74,25 +71,22 @@
return _c; return _c;
}; };
Rewriter.prototype.removeMidExpressionNewlines = function() { Rewriter.prototype.removeMidExpressionNewlines = function() {
return this.scanTokens((function(__this) { return this.scanTokens((function(_this) {
var __func = function(prev, token, post, i) { return function(prev, token, post, i) {
if (!(post && include(EXPRESSION_CLOSE, post[0]) && token[0] === 'TERMINATOR')) { if (!(post && include(EXPRESSION_CLOSE, post[0]) && token[0] === 'TERMINATOR')) {
return 1; return 1;
} }
this.tokens.splice(i, 1); _this.tokens.splice(i, 1);
return 0; return 0;
}; }
return (function() {
return __func.apply(__this, arguments);
});
})(this)); })(this));
}; };
Rewriter.prototype.closeOpenCallsAndIndexes = function() { Rewriter.prototype.closeOpenCallsAndIndexes = function() {
var brackets, parens; var brackets, parens;
parens = [0]; parens = [0];
brackets = [0]; brackets = [0];
return this.scanTokens((function(__this) { return this.scanTokens((function(_this) {
var __func = function(prev, token, post, i) { return function(prev, token, post, i) {
var _c; var _c;
if ((_c = token[0]) === 'CALL_START') { if ((_c = token[0]) === 'CALL_START') {
parens.push(0); parens.push(0);
@@ -118,32 +112,26 @@
} }
} }
return 1; return 1;
}; }
return (function() {
return __func.apply(__this, arguments);
});
})(this)); })(this));
}; };
Rewriter.prototype.addImplicitParentheses = function() { Rewriter.prototype.addImplicitParentheses = function() {
var closeCalls, stack; var closeCalls, stack;
stack = [0]; stack = [0];
closeCalls = (function(__this) { closeCalls = (function(_this) {
var __func = function(i) { return function(i) {
var _c, size, tmp; var _c, size, tmp;
_c = stack[stack.length - 1]; _c = stack[stack.length - 1];
for (tmp = 0; (0 <= _c ? tmp < _c : tmp > _c); (0 <= _c ? tmp += 1 : tmp -= 1)) { for (tmp = 0; (0 <= _c ? tmp < _c : tmp > _c); (0 <= _c ? tmp += 1 : tmp -= 1)) {
this.tokens.splice(i, 0, ['CALL_END', ')', this.tokens[i][2]]); _this.tokens.splice(i, 0, ['CALL_END', ')', _this.tokens[i][2]]);
} }
size = stack[stack.length - 1] + 1; size = stack[stack.length - 1] + 1;
stack[stack.length - 1] = 0; stack[stack.length - 1] = 0;
return size; return size;
}; }
return (function() {
return __func.apply(__this, arguments);
});
})(this); })(this);
return this.scanTokens((function(__this) { return this.scanTokens((function(_this) {
var __func = function(prev, token, post, i) { return function(prev, token, post, i) {
var _c, _d, j, nx, open, size, tag; var _c, _d, j, nx, open, size, tag;
tag = token[0]; tag = token[0];
if (tag === 'OUTDENT') { if (tag === 'OUTDENT') {
@@ -151,7 +139,7 @@
} }
open = stack[stack.length - 1] > 0; open = stack[stack.length - 1] > 0;
if (prev && prev.spaced && include(IMPLICIT_FUNC, prev[0]) && include(IMPLICIT_CALL, tag) && !(tag === '!' && (('IN' === (_c = post[0]) || 'OF' === _c)))) { if (prev && prev.spaced && include(IMPLICIT_FUNC, prev[0]) && include(IMPLICIT_CALL, tag) && !(tag === '!' && (('IN' === (_c = post[0]) || 'OF' === _c)))) {
this.tokens.splice(i, 0, ['CALL_START', '(', token[2]]); _this.tokens.splice(i, 0, ['CALL_START', '(', token[2]]);
stack[stack.length - 1] += 1; stack[stack.length - 1] += 1;
if (include(EXPRESSION_START, tag)) { if (include(EXPRESSION_START, tag)) {
stack.push(0); stack.push(0);
@@ -169,12 +157,12 @@
} }
if (open && !token.generated && prev[0] !== ',' && (!post || include(IMPLICIT_END, tag))) { if (open && !token.generated && prev[0] !== ',' && (!post || include(IMPLICIT_END, tag))) {
j = 1; j = 1;
while ((typeof (_d = (nx = this.tokens[i + j])) !== "undefined" && _d !== null) && include(IMPLICIT_END, nx[0])) { while ((typeof (_d = (nx = _this.tokens[i + j])) !== "undefined" && _d !== null) && include(IMPLICIT_END, nx[0])) {
j++; j++;
} }
if ((typeof nx !== "undefined" && nx !== null) && nx[0] === ',' && this.tokens[i + j - 1][0] === 'OUTDENT') { if ((typeof nx !== "undefined" && nx !== null) && nx[0] === ',' && _this.tokens[i + j - 1][0] === 'OUTDENT') {
if (tag === 'TERMINATOR') { if (tag === 'TERMINATOR') {
this.tokens.splice(i, 1); _this.tokens.splice(i, 1);
} }
} else { } else {
size = closeCalls(i); size = closeCalls(i);
@@ -189,42 +177,39 @@
return 1; return 1;
} }
return 1; return 1;
}; }
return (function() {
return __func.apply(__this, arguments);
});
})(this)); })(this));
}; };
Rewriter.prototype.addImplicitIndentation = function() { Rewriter.prototype.addImplicitIndentation = function() {
return this.scanTokens((function(__this) { return this.scanTokens((function(_this) {
var __func = function(prev, token, post, i) { return function(prev, token, post, i) {
var _c, idx, indent, insertion, outdent, parens, pre, starter, tok; var _c, idx, indent, insertion, outdent, parens, pre, starter, tok;
if (token[0] === 'ELSE' && prev[0] !== 'OUTDENT') { if (token[0] === 'ELSE' && prev[0] !== 'OUTDENT') {
this.tokens.splice.apply(this.tokens, [i, 0].concat(this.indentation(token))); _this.tokens.splice.apply(_this.tokens, [i, 0].concat(_this.indentation(token)));
return 2; return 2;
} }
if (token[0] === 'CATCH' && (this.tokens[i + 2][0] === 'TERMINATOR' || this.tokens[i + 2][0] === 'FINALLY')) { if (token[0] === 'CATCH' && (_this.tokens[i + 2][0] === 'TERMINATOR' || _this.tokens[i + 2][0] === 'FINALLY')) {
this.tokens.splice.apply(this.tokens, [i + 2, 0].concat(this.indentation(token))); _this.tokens.splice.apply(_this.tokens, [i + 2, 0].concat(_this.indentation(token)));
return 4; return 4;
} }
if (!(include(SINGLE_LINERS, token[0]) && post[0] !== 'INDENT' && !(token[0] === 'ELSE' && post[0] === 'IF'))) { if (!(include(SINGLE_LINERS, token[0]) && post[0] !== 'INDENT' && !(token[0] === 'ELSE' && post[0] === 'IF'))) {
return 1; return 1;
} }
starter = token[0]; starter = token[0];
_c = this.indentation(token); _c = _this.indentation(token);
indent = _c[0]; indent = _c[0];
outdent = _c[1]; outdent = _c[1];
indent.generated = (outdent.generated = true); indent.generated = (outdent.generated = true);
this.tokens.splice(i + 1, 0, indent); _this.tokens.splice(i + 1, 0, indent);
idx = i + 1; idx = i + 1;
parens = 0; parens = 0;
while (true) { while (true) {
idx += 1; idx += 1;
tok = this.tokens[idx]; tok = _this.tokens[idx];
pre = this.tokens[idx - 1]; pre = _this.tokens[idx - 1];
if ((!tok || (include(SINGLE_CLOSERS, tok[0]) && tok[1] !== ';' && parens === 0) || (tok[0] === ')' && parens === 0)) && !(tok[0] === 'ELSE' && !('IF' === starter || 'THEN' === starter))) { if ((!tok || (include(SINGLE_CLOSERS, tok[0]) && tok[1] !== ';' && parens === 0) || (tok[0] === ')' && parens === 0)) && !(tok[0] === 'ELSE' && !('IF' === starter || 'THEN' === starter))) {
insertion = pre[0] === "," ? idx - 1 : idx; insertion = pre[0] === "," ? idx - 1 : idx;
this.tokens.splice(insertion, 0, outdent); _this.tokens.splice(insertion, 0, outdent);
break; break;
} }
if (tok[0] === '(') { if (tok[0] === '(') {
@@ -237,20 +222,17 @@
if (!(token[0] === 'THEN')) { if (!(token[0] === 'THEN')) {
return 1; return 1;
} }
this.tokens.splice(i, 1); _this.tokens.splice(i, 1);
return 0; return 0;
}; }
return (function() {
return __func.apply(__this, arguments);
});
})(this)); })(this));
}; };
Rewriter.prototype.ensureBalance = function(pairs) { Rewriter.prototype.ensureBalance = function(pairs) {
var _c, _d, key, levels, line, open, openLine, unclosed, value; var _c, _d, key, levels, line, open, openLine, unclosed, value;
levels = {}; levels = {};
openLine = {}; openLine = {};
this.scanTokens((function(__this) { this.scanTokens((function(_this) {
var __func = function(prev, token, post, i) { return function(prev, token, post, i) {
var _c, _d, _e, _f, close, open, pair; var _c, _d, _e, _f, close, open, pair;
_d = pairs; _d = pairs;
for (_c = 0, _e = _d.length; _c < _e; _c++) { for (_c = 0, _e = _d.length; _c < _e; _c++) {
@@ -273,10 +255,7 @@
} }
} }
return 1; return 1;
}; }
return (function() {
return __func.apply(__this, arguments);
});
})(this)); })(this));
unclosed = (function() { unclosed = (function() {
_c = []; _d = levels; _c = []; _d = levels;
@@ -303,8 +282,8 @@
val = _c[key]; val = _c[key];
(debt[key] = 0); (debt[key] = 0);
} }
return this.scanTokens((function(__this) { return this.scanTokens((function(_this) {
var __func = function(prev, token, post, i) { return function(prev, token, post, i) {
var inv, match, mtag, oppos, tag; var inv, match, mtag, oppos, tag;
tag = token[0]; tag = token[0];
inv = INVERSES[token[0]]; inv = INVERSES[token[0]];
@@ -314,7 +293,7 @@
} else if (include(EXPRESSION_END, tag)) { } else if (include(EXPRESSION_END, tag)) {
if (debt[inv] > 0) { if (debt[inv] > 0) {
debt[inv] -= 1; debt[inv] -= 1;
this.tokens.splice(i, 1); _this.tokens.splice(i, 1);
return 0; return 0;
} else { } else {
match = stack.pop(); match = stack.pop();
@@ -325,21 +304,18 @@
} }
debt[mtag] += 1; debt[mtag] += 1;
val = [oppos, mtag === 'INDENT' ? match[1] : oppos]; val = [oppos, mtag === 'INDENT' ? match[1] : oppos];
if ((this.tokens[i + 2] == undefined ? undefined : this.tokens[i + 2][0]) === mtag) { if ((_this.tokens[i + 2] == undefined ? undefined : _this.tokens[i + 2][0]) === mtag) {
this.tokens.splice(i + 3, 0, val); _this.tokens.splice(i + 3, 0, val);
stack.push(match); stack.push(match);
} else { } else {
this.tokens.splice(i, 0, val); _this.tokens.splice(i, 0, val);
} }
return 1; return 1;
} }
} else { } else {
return 1; return 1;
} }
}; }
return (function() {
return __func.apply(__this, arguments);
});
})(this)); })(this));
}; };
Rewriter.prototype.indentation = function(token) { Rewriter.prototype.indentation = function(token) {

View File

@@ -185,6 +185,12 @@ exports.Expressions: class Expressions extends BaseNode
@expressions[idx]: last.makeReturn() @expressions[idx]: last.makeReturn()
this this
# A bound function uses a local `_this` variable instead of the real `this`.
rewriteThis: ->
@traverseChildren false, (child) ->
if child instanceof ValueNode and child.base.value is 'this'
child.base: literal '_this'
# An **Expressions** is the only node that can serve as the root. # An **Expressions** is the only node that can serve as the root.
compile: (o) -> compile: (o) ->
o: or {} o: or {}
@@ -880,13 +886,13 @@ exports.CodeNode: class CodeNode extends BaseNode
i: + 1 i: + 1
params: (param.compile(o) for param in params) params: (param.compile(o) for param in params)
@body.makeReturn() @body.makeReturn()
@body.rewriteThis() if @bound
(o.scope.parameter(param)) for param in params (o.scope.parameter(param)) for param in params
code: if @body.expressions.length then "\n${ @body.compileWithDeclarations(o) }\n" else '' code: if @body.expressions.length then "\n${ @body.compileWithDeclarations(o) }\n" else ''
func: "function(${ params.join(', ') }) {$code${ code and @idt(if @bound then 1 else 0) }}" func: "function(${ params.join(', ') }) {$code${ code and @idt(if @bound then 1 else 0) }}"
func: "($func)" if top and not @bound func: "($func)" if top and not @bound
return func unless @bound return func unless @bound
inner: "(function() {\n${@idt(2)}return __func.apply(__this, arguments);\n${@idt(1)}});" "(function(_this) {\n${@idt(1)}return $func\n$@tab})(this)"
"(function(__this) {\n${@idt(1)}var __func = $func;\n${@idt(1)}return $inner\n$@tab})(this)"
topSensitive: -> topSensitive: ->
true true