simplifying generated output for common-case splices.

This commit is contained in:
Jeremy Ashkenas 2010-12-04 15:06:21 -05:00
parent 85521f88b2
commit c7a9801db7
2 changed files with 23 additions and 11 deletions

View File

@ -1163,15 +1163,23 @@
return new Op(this.context.slice(0, -1), left, new Assign(rite, this.value, '=')).compile(o);
};
Assign.prototype.compileSplice = function(o) {
var from, name, plus, range, ref, to, val;
var from, name, plus, range, to, val;
range = this.variable.properties.pop().range;
name = this.variable.compile(o);
plus = range.exclusive ? '' : ' + 1';
from = range.from ? range.from.compile(o) : '0';
to = range.to ? range.to.compile(o) + ' - ' + from + plus : "" + name + ".length";
ref = o.scope.freeVariable('ref');
if (!range.to) {
to = "" + name + ".length";
}
if (!to) {
if (range.from && range.from.isSimpleNumber() && range.to.isSimpleNumber()) {
to = (+range.to.compile(o)) - +from + +plus;
} else {
to = range.to.compile(o) + ' - ' + from + plus;
}
}
val = this.value.compile(o);
return "([].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + ref + " = " + val + ")), " + ref + ")";
return "[].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + val + "))";
};
return Assign;
}();

View File

@ -944,13 +944,17 @@ exports.Assign = class Assign extends Base
# `Array#splice` method.
compileSplice: (o) ->
{range} = @variable.properties.pop()
name = @variable.compile o
plus = if range.exclusive then '' else ' + 1'
from = if range.from then range.from.compile(o) else '0'
to = if range.to then range.to.compile(o) + ' - ' + from + plus else "#{name}.length"
ref = o.scope.freeVariable 'ref'
val = @value.compile(o)
"([].splice.apply(#{name}, [#{from}, #{to}].concat(#{ref} = #{val})), #{ref})"
name = @variable.compile o
plus = if range.exclusive then '' else ' + 1'
from = if range.from then range.from.compile(o) else '0'
to = "#{name}.length" unless range.to
unless to
if range.from and range.from.isSimpleNumber() and range.to.isSimpleNumber()
to = (+range.to.compile(o)) - +from + +plus
else
to = range.to.compile(o) + ' - ' + from + plus
val = @value.compile(o)
"[].splice.apply(#{name}, [#{from}, #{to}].concat(#{val}))"
#### Code