fixes #1858: top-level returns should force safety wrapper

This commit is contained in:
Michael Ficarra
2011-12-10 22:06:38 -05:00
parent 63ab886e9f
commit f0728fcf8f
2 changed files with 21 additions and 8 deletions

View File

@@ -302,13 +302,21 @@
};
Block.prototype.compileRoot = function(o) {
var code;
var code, hasReturn;
o.indent = this.tab = o.bare ? '' : TAB;
o.scope = new Scope(null, this, null);
o.level = LEVEL_TOP;
this.spaced = true;
code = this.compileWithDeclarations(o);
if (o.bare || o.scope.variables.length <= 1) return code;
hasReturn = (function(exps) {
var e, _i, _len;
for (_i = 0, _len = exps.length; _i < _len; _i++) {
e = exps[_i];
if (e instanceof Return) return true;
}
return false;
})(this.expressions);
if ((o.bare || o.scope.variables.length <= 1) && !hasReturn) return code;
return "(function() {\n" + code + "\n}).call(this);\n";
};

View File

@@ -243,13 +243,18 @@ exports.Block = class Block extends Base
# It would be better not to generate them in the first place, but for now,
# clean up obvious double-parentheses.
compileRoot: (o) ->
o.indent = @tab = if o.bare then '' else TAB
o.scope = new Scope null, this, null
o.level = LEVEL_TOP
@spaced = yes
code = @compileWithDeclarations o
o.indent = @tab = if o.bare then '' else TAB
o.scope = new Scope null, this, null
o.level = LEVEL_TOP
@spaced = yes
code = @compileWithDeclarations o
hasReturn = ((exps)->
for e in exps
return true if e instanceof Return
false
)(@expressions)
# the `1` below accounts for `arguments`, always "in scope"
return code if o.bare or o.scope.variables.length <= 1
return code if (o.bare or o.scope.variables.length <= 1) and not hasReturn
"(function() {\n#{code}\n}).call(this);\n"
# Compile the expressions body for the contents of a function, with