From c7a9801db70aab8a72dbd8a15413a14847de068a Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 4 Dec 2010 15:06:21 -0500 Subject: [PATCH] simplifying generated output for common-case splices. --- lib/nodes.js | 16 ++++++++++++---- src/nodes.coffee | 18 +++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index 59b3e41a..e2eaaccc 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -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; }(); diff --git a/src/nodes.coffee b/src/nodes.coffee index 06fb8437..05c84311 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -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