tweaking existential compilation.

This commit is contained in:
Jeremy Ashkenas
2010-08-14 11:29:10 -04:00
parent 1eebbfe2bc
commit ead9b1041c
3 changed files with 12 additions and 27 deletions

View File

@@ -288,8 +288,6 @@ exports.ReturnNode = class ReturnNode extends BaseNode
# or vanilla.
exports.ValueNode = class ValueNode extends BaseNode
SOAK: " == undefined ? undefined : "
class: 'ValueNode'
children: ['base', 'properties']
@@ -331,13 +329,6 @@ exports.ValueNode = class ValueNode extends BaseNode
isNumber: ->
@base instanceof LiteralNode and @base.value.match NUMBER
# Works out if the value is the start of a chain.
isStart: (o) ->
return true if this is o.chainRoot and @properties[0] instanceof AccessorNode
node = o.chainRoot.base or o.chainRoot.variable
while node instanceof CallNode then node = node.variable
node is this
# If the value node has indexes containing function calls, and the value node
# needs to be used twice, in compound assignment ... then we need to cache
# the value of the indexes.
@@ -373,8 +364,11 @@ exports.ValueNode = class ValueNode extends BaseNode
if @base instanceof CallNode or @base.contains((n) -> n instanceof CallNode) and i is 0
temp = o.scope.freeVariable()
complete = "(#{ baseline = temp } = (#{complete}))"
complete = "typeof #{complete} === \"undefined\" || #{baseline}" if i is 0 and @isStart(o)
complete += @SOAK + (baseline += prop.compile(o))
complete = if i is 0
"(typeof #{complete} === \"undefined\" || #{baseline} === null) ? undefined : "
else
"#{complete} == undefined ? undefined : "
complete += (baseline += prop.compile(o))
else
part = prop.compile(o)
baseline += part