fixes #855; fix partially inspired by satyr/coco 3e37cf32

This commit is contained in:
Michael Ficarra 2010-11-26 02:08:25 -05:00
parent c0cb0c35e2
commit 63cbb64341
4 changed files with 46 additions and 39 deletions

View File

@ -208,7 +208,7 @@
return regex.length; return regex.length;
}; };
Lexer.prototype.heregexToken = function(match) { Lexer.prototype.heregexToken = function(match) {
var body, flags, heregex, re, tag, tokens, value, _i, _len, _ref, _ref2, _ref3, _this; var body, flags, heregex, re, tag, tokens, value, _i, _len, _ref, _ref2, _ref3;
heregex = match[0], body = match[1], flags = match[2]; heregex = match[0], body = match[1], flags = match[2];
if (0 > body.indexOf('#{')) { if (0 > body.indexOf('#{')) {
re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/'); re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/');
@ -238,7 +238,7 @@
if (((_ref3 = tokens[0]) != null ? _ref3[0] : void 0) !== 'STRING') { if (((_ref3 = tokens[0]) != null ? _ref3[0] : void 0) !== 'STRING') {
this.tokens.push(['STRING', '""'], ['+', '+']); this.tokens.push(['STRING', '""'], ['+', '+']);
} }
(_this = this.tokens).push.apply(_this, tokens); (_ref = this.tokens).push.apply(_ref, tokens);
if (flags) { if (flags) {
this.tokens.push([',', ','], ['STRING', '"' + flags + '"']); this.tokens.push([',', ','], ['STRING', '"' + flags + '"']);
} }
@ -478,7 +478,7 @@
throw new Error("unterminated " + (stack.pop()[0]) + " on line " + (this.line + 1)); throw new Error("unterminated " + (stack.pop()[0]) + " on line " + (this.line + 1));
}; };
Lexer.prototype.interpolateString = function(str, options) { Lexer.prototype.interpolateString = function(str, options) {
var expr, heredoc, i, inner, interpolated, letter, nested, pi, regex, tag, tokens, value, _len, _ref, _ref2, _this; var expr, heredoc, i, inner, interpolated, letter, nested, pi, regex, tag, tokens, value, _len, _ref, _ref2;
if (options == null) { if (options == null) {
options = {}; options = {};
} }
@ -537,7 +537,7 @@
this.token('+', '+'); this.token('+', '+');
} }
if (tag === 'TOKENS') { if (tag === 'TOKENS') {
(_this = this.tokens).push.apply(_this, value); (_ref = this.tokens).push.apply(_ref, value);
} else { } else {
this.token('STRING', this.makeString(value, '"', heredoc)); this.token('STRING', this.makeString(value, '"', heredoc));
} }

View File

@ -606,7 +606,7 @@
return _results; return _results;
}.call(this)).join(', '); }.call(this)).join(', ');
if (this.isSuper) { if (this.isSuper) {
return this.compileSuper(args, o); return this.superReference(o) + (".call(this" + (args && ', ' + args) + ")");
} else { } else {
return (this.isNew ? 'new ' : '') + this.variable.compile(o, LEVEL_ACCESS) + ("(" + args + ")"); return (this.isNew ? 'new ' : '') + this.variable.compile(o, LEVEL_ACCESS) + ("(" + args + ")");
} }
@ -619,21 +619,24 @@
if (this.isSuper) { if (this.isSuper) {
return "" + (this.superReference(o)) + ".apply(this, " + splatArgs + ")"; return "" + (this.superReference(o)) + ".apply(this, " + splatArgs + ")";
} }
if (!this.isNew) { if (this.isNew) {
idt = this.tab + TAB;
return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + this.tab + "})(" + (this.variable.compile(o, LEVEL_LIST)) + ", " + splatArgs + ", function() {})";
}
base = new Value(this.variable); base = new Value(this.variable);
if ((name = base.properties.pop()) && base.isComplex()) { if ((name = base.properties.pop()) && base.isComplex()) {
ref = o.scope.freeVariable('this'); ref = o.scope.temporary('ref');
fun = "(" + ref + " = " + (base.compile(o, LEVEL_LIST)) + ")" + (name.compile(o)); fun = "(" + ref + " = " + (base.compile(o, LEVEL_LIST)) + ")" + (name.compile(o));
} else { } else {
fun = ref = base.compile(o, LEVEL_ACCESS); fun = base.compile(o, LEVEL_ACCESS);
if (name) { if (name) {
ref = fun;
fun += name.compile(o); fun += name.compile(o);
} else {
ref = 'null';
} }
} }
return "" + fun + ".apply(" + ref + ", " + splatArgs + ")"; return "" + fun + ".apply(" + ref + ", " + splatArgs + ")";
}
idt = this.tab + TAB;
return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + this.tab + "})(" + (this.variable.compile(o, LEVEL_LIST)) + ", " + splatArgs + ", function() {})";
}; };
return Call; return Call;
}(); }();
@ -1227,7 +1230,7 @@
return !!this.ctor; return !!this.ctor;
}; };
Code.prototype.compileNode = function(o) { Code.prototype.compileNode = function(o) {
var code, exprs, i, idt, lit, p, param, ref, scope, sharedScope, splats, v, val, vars, wasEmpty, _i, _j, _k, _len, _len2, _len3, _len4, _ref, _ref2, _ref3, _results, _this; var code, exprs, i, idt, lit, p, param, ref, scope, sharedScope, splats, v, val, vars, wasEmpty, _i, _j, _k, _len, _len2, _len3, _len4, _ref, _ref2, _ref3, _results;
sharedScope = del(o, 'sharedScope'); sharedScope = del(o, 'sharedScope');
o.scope = scope = sharedScope || new Scope(o.scope, this.body, this); o.scope = scope = sharedScope || new Scope(o.scope, this.body, this);
o.indent += TAB; o.indent += TAB;
@ -1278,7 +1281,7 @@
exprs.unshift(splats); exprs.unshift(splats);
} }
if (exprs.length) { if (exprs.length) {
(_this = this.body.expressions).unshift.apply(_this, exprs); (_ref = this.body.expressions).unshift.apply(_ref, exprs);
} }
if (!splats) { if (!splats) {
for (i = 0, _len4 = vars.length; i < _len4; i++) { for (i = 0, _len4 = vars.length; i < _len4; i++) {

View File

@ -65,7 +65,7 @@
if (tuple.length < 3) { if (tuple.length < 3) {
tuple.unshift(null); tuple.unshift(null);
} }
_results.push(buildRule.apply(buildRule, tuple)); _results.push(buildRule.apply(null, tuple));
} }
return _results; return _results;
}; };

View File

@ -487,7 +487,7 @@ exports.Call = class Call extends Base
return @compileSplat o, code return @compileSplat o, code
args = (arg.compile o, LEVEL_LIST for arg in @args).join ', ' args = (arg.compile o, LEVEL_LIST for arg in @args).join ', '
if @isSuper if @isSuper
@compileSuper args, o @superReference(o) + ".call(this#{ args and ', ' + args })"
else else
(if @isNew then 'new ' else '') + @variable.compile(o, LEVEL_ACCESS) + "(#{args})" (if @isNew then 'new ' else '') + @variable.compile(o, LEVEL_ACCESS) + "(#{args})"
@ -502,23 +502,27 @@ exports.Call = class Call extends Base
# inner constructor in order to be able to pass the varargs. # inner constructor in order to be able to pass the varargs.
compileSplat: (o, splatArgs) -> compileSplat: (o, splatArgs) ->
return "#{ @superReference o }.apply(this, #{splatArgs})" if @isSuper return "#{ @superReference o }.apply(this, #{splatArgs})" if @isSuper
unless @isNew if @isNew
base = new Value @variable
if (name = base.properties.pop()) and base.isComplex()
ref = o.scope.freeVariable 'this'
fun = "(#{ref} = #{ base.compile o, LEVEL_LIST })#{ name.compile o }"
else
fun = ref = base.compile o, LEVEL_ACCESS
fun += name.compile o if name
return "#{fun}.apply(#{ref}, #{splatArgs})"
idt = @tab + TAB idt = @tab + TAB
""" return """
(function(func, args, ctor) { (function(func, args, ctor) {
#{idt}ctor.prototype = func.prototype; #{idt}ctor.prototype = func.prototype;
#{idt}var child = new ctor, result = func.apply(child, args); #{idt}var child = new ctor, result = func.apply(child, args);
#{idt}return typeof result === "object" ? result : child; #{idt}return typeof result === "object" ? result : child;
#{@tab}})(#{ @variable.compile o, LEVEL_LIST }, #{splatArgs}, function() {}) #{@tab}})(#{ @variable.compile o, LEVEL_LIST }, #{splatArgs}, function() {})
""" """
base = new Value @variable
if (name = base.properties.pop()) and base.isComplex()
ref = o.scope.temporary 'ref'
fun = "(#{ref} = #{ base.compile o, LEVEL_LIST })#{ name.compile o }"
else
fun = base.compile o, LEVEL_ACCESS
if name
ref = fun
fun += name.compile o
else
ref = 'null'
"#{fun}.apply(#{ref}, #{splatArgs})"
#### Extends #### Extends