tweaking compiled tempvar names to satisfaction.

This commit is contained in:
Jeremy Ashkenas
2010-09-20 21:56:18 -04:00
parent 5d82dddfb1
commit f7fce3cd68
13 changed files with 472 additions and 465 deletions

View File

@@ -70,7 +70,7 @@ exports.BaseNode = class BaseNode
else if this instanceof ValueNode and options.assignment
this.cacheIndexes(o)
else
reference = literal o.scope.freeVariable 'cache'
reference = literal o.scope.freeVariable 'ref'
compiled = new AssignNode reference, this
[compiled, reference]
return [pair[0].compile(o), pair[1].compile(o)] if options.precompile
@@ -374,7 +374,7 @@ exports.ValueNode = class ValueNode extends BaseNode
@source = baseline
if prop.soakNode
if @base.containsType(CallNode) and i is 0
temp = o.scope.freeVariable 'cache'
temp = o.scope.freeVariable 'ref'
complete = "(#{ baseline = temp } = (#{complete}))"
complete = if i is 0
"(typeof #{complete} === \"undefined\" || #{baseline} === null) ? undefined : "
@@ -488,7 +488,7 @@ exports.CallNode = class CallNode extends BaseNode
meth = @meth or @superReference(o)
obj = @variable and @variable.source or 'this'
if obj.match(/\(/)
temp = o.scope.freeVariable 'cache'
temp = o.scope.freeVariable 'ref'
obj = temp
meth = "(#{temp} = #{ @variable.source })#{ @variable.last }"
if @isNew
@@ -496,9 +496,9 @@ exports.CallNode = class CallNode extends BaseNode
for arg in @args
arg.contains (n) -> mentionsArgs or= n instanceof LiteralNode and (n.value is 'arguments')
utility 'extends'
a = o.scope.freeVariable 'klass'
b = o.scope.freeVariable 'cache'
c = o.scope.freeVariable 'cache'
a = o.scope.freeVariable 'ctor'
b = o.scope.freeVariable 'instance'
c = o.scope.freeVariable 'result'
"""
#{@first}(function() {
#{@idt(1)}var ctor = function(){};
@@ -624,7 +624,7 @@ exports.RangeNode = class RangeNode extends BaseNode
range = [+@fromNum..+@toNum]
range.pop() if @exclusive
return "[#{ range.join(', ') }]"
i = o.scope.freeVariable 'index'
i = o.scope.freeVariable 'i'
result = o.scope.freeVariable 'result'
pre = "\n#{idt}#{result} = []; #{vars}"
if @fromNum and @toNum
@@ -743,7 +743,7 @@ exports.ClassNode = class ClassNode extends BaseNode
# equivalent syntax tree and compile that, in pieces. You can see the
# constructor, property assignments, and inheritance getting built out below.
compileNode: (o) ->
@variable = literal o.scope.freeVariable 'klass' if @variable is '__temp__'
@variable = literal o.scope.freeVariable 'ctor' if @variable is '__temp__'
extension = @parent and new ExtendsNode(@variable, @parent)
props = new Expressions
o.top = true
@@ -775,7 +775,7 @@ exports.ClassNode = class ClassNode extends BaseNode
else
func.bound = false
constScope or= new Scope(o.scope, constructor.body, constructor)
me or= constScope.freeVariable 'self'
me or= constScope.freeVariable 'this'
pname = pvar.compile(o)
constructor.body.push new ReturnNode literal 'this' if constructor.body.empty()
constructor.body.unshift literal "this.#{pname} = function(){ return #{className}.prototype.#{pname}.apply(#{me}, arguments); }"
@@ -851,7 +851,7 @@ exports.AssignNode = class AssignNode extends BaseNode
# See the [ECMAScript Harmony Wiki](http://wiki.ecmascript.org/doku.php?id=harmony:destructuring)
# for details.
compilePatternMatch: (o) ->
valVar = o.scope.freeVariable 'cache'
valVar = o.scope.freeVariable 'ref'
value = if @value.isStatement(o) then ClosureNode.wrap(@value) else @value
assigns = ["#{@tab}#{valVar} = #{ value.compile(o) };"]
o.top = true
@@ -1011,7 +1011,7 @@ exports.SplatNode = class SplatNode extends BaseNode
o.scope.find name
end = ''
if @trailings.length
len = o.scope.freeVariable 'cache'
len = o.scope.freeVariable 'ref'
o.scope.assign len, "arguments.length"
variadic = o.scope.freeVariable 'result'
o.scope.assign variadic, len + ' >= ' + @arglength
@@ -1227,7 +1227,7 @@ exports.InNode = class InNode extends BaseNode
compileLoopTest: (o) ->
[@arr1, @arr2] = @array.compileReference o, precompile: yes
[i, l] = [o.scope.freeVariable('index'), o.scope.freeVariable('cache')]
[i, l] = [o.scope.freeVariable('i'), o.scope.freeVariable('l')]
prefix = if @obj1 isnt @obj2 then @obj1 + '; ' else ''
"(function(){ #{prefix}for (var #{i}=0, #{l}=#{@arr1}.length; #{i}<#{l}; #{i}++) { if (#{@arr2}[#{i}] === #{@obj2}) return true; } return false; }).call(this)"
@@ -1385,12 +1385,12 @@ exports.ForNode = class ForNode extends BaseNode
source = if range then @source.base else @source
codeInBody = @body.contains (n) -> n instanceof CodeNode
scope = o.scope
name = (@name and @name.compile(o)) or scope.freeVariable 'index'
name = (@name and @name.compile(o)) or scope.freeVariable 'i'
index = @index and @index.compile o
scope.find(name, immediate: yes) if name and not @pattern and (range or not codeInBody)
scope.find(index, immediate: yes) if index
rvar = scope.freeVariable 'result' unless topLevel
ivar = if codeInBody then scope.freeVariable 'index' else if range then name else index or scope.freeVariable 'index'
ivar = if codeInBody then scope.freeVariable 'i' else if range then name else index or scope.freeVariable 'i'
varPart = ''
guardPart = ''
body = Expressions.wrap([@body])
@@ -1398,14 +1398,14 @@ exports.ForNode = class ForNode extends BaseNode
sourcePart = source.compileVariables(o)
forPart = source.compile merge o, index: ivar, step: @step
else
svar = scope.freeVariable 'cache'
svar = scope.freeVariable 'ref'
sourcePart = "#{svar} = #{ @source.compile(o) };"
if @pattern
namePart = new AssignNode(@name, literal("#{svar}[#{ivar}]")).compile(merge o, {indent: @idt(1), top: true}) + '\n'
else
namePart = "#{name} = #{svar}[#{ivar}]" if name
unless @object
lvar = scope.freeVariable 'cache'
lvar = scope.freeVariable 'l'
stepPart = if @step then "#{ivar} += #{ @step.compile(o) }" else "#{ivar}++"
forPart = "#{ivar} = 0, #{lvar} = #{svar}.length; #{ivar} < #{lvar}; #{stepPart}"
sourcePart = (if rvar then "#{rvar} = []; " else '') + sourcePart