From 3a20d7dacbe797f5eab01b4cbbc24772789f4c80 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 12 Sep 2010 11:08:05 -0400 Subject: [PATCH] Partial fix for 653. --- lib/nodes.js | 23 ++++++++++++++--------- src/nodes.coffee | 2 ++ test/test_operations.coffee | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index 9d9e60a2..7a0904ec 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -357,20 +357,25 @@ return this.base instanceof LiteralNode && this.base.value.match(NUMBER); }; ValueNode.prototype.cacheIndexes = function(o) { - var _b, _c, _d, copy, i; + var _b, _c, _d, _e, copy, i; copy = new ValueNode(this.base, this.properties.slice(0)); - _c = copy.properties; - for (_b = 0, _d = _c.length; _b < _d; _b++) { + if (this.base instanceof CallNode) { + _b = this.base.compileReference(o); + this.base = _b[0]; + copy.base = _b[1]; + } + _d = copy.properties; + for (_c = 0, _e = _d.length; _c < _e; _c++) { (function() { - var _e, index, indexVar; - var i = _b; - var prop = _c[_b]; + var _f, index, indexVar; + var i = _c; + var prop = _d[_c]; if (prop instanceof IndexNode && prop.contains(function(n) { return n instanceof CallNode; })) { - _e = prop.index.compileReference(o); - index = _e[0]; - indexVar = _e[1]; + _f = prop.index.compileReference(o); + index = _f[0]; + indexVar = _f[1]; this.properties[i] = new IndexNode(index); return (copy.properties[i] = new IndexNode(indexVar)); } diff --git a/src/nodes.coffee b/src/nodes.coffee index 9e993939..6b7c7826 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -339,6 +339,8 @@ exports.ValueNode = class ValueNode extends BaseNode # the value of the indexes. cacheIndexes: (o) -> copy = new ValueNode @base, @properties[0..] + if @base instanceof CallNode + [@base, copy.base] = @base.compileReference o for prop, i in copy.properties if prop instanceof IndexNode and prop.contains((n) -> n instanceof CallNode) [index, indexVar] = prop.index.compileReference o diff --git a/test/test_operations.coffee b/test/test_operations.coffee index 5e98ba4d..357ea581 100644 --- a/test/test_operations.coffee +++ b/test/test_operations.coffee @@ -95,6 +95,21 @@ count = 0 list[key()] ?= 100 ok list.join(' ') is '0 100 5 10' +count = 0 +key = -> + count += 1 + key + +key().val or= 100 + +ok key.val is 100 +ok count is 1 + +key().val ?= 200 + +ok key.val is 100 +ok count is 2 + # Ensure that RHS is treated as a group. a = b = false