Altered bound functions with do to just use call(this) rather than binding.

This commit is contained in:
Timothy Jones
2010-10-25 00:18:54 +13:00
parent d096f69c78
commit 9619fa66eb
5 changed files with 23 additions and 2 deletions

View File

@@ -108,7 +108,7 @@
], ],
Do: [ Do: [
o("DO Code", function() { o("DO Code", function() {
return new Call($2, $2.params); return $2["do"]();
}) })
], ],
Code: [ Code: [

View File

@@ -1048,6 +1048,14 @@
Code.prototype.traverseChildren = function(crossScope, func) { Code.prototype.traverseChildren = function(crossScope, func) {
return crossScope ? Code.__super__.traverseChildren.call(this, crossScope, func) : undefined; return crossScope ? Code.__super__.traverseChildren.call(this, crossScope, func) : undefined;
}; };
Code.prototype["do"] = function() {
if (this.bound) {
this.bound = false;
return new Call(new Value(this, [new Accessor(new Literal('call'))]), [new Literal('this')].concat(this.params));
} else {
return new Call(this, this.params);
}
};
return Code; return Code;
})(); })();
exports.Param = (function() { exports.Param = (function() {

View File

@@ -172,7 +172,7 @@ grammar =
] ]
Do: [ Do: [
o "DO Code", -> new Call $2, $2.params o "DO Code", -> $2.do()
] ]
# The **Code** node is the function literal. It's defined by an indented block # The **Code** node is the function literal. It's defined by an indented block

View File

@@ -889,6 +889,14 @@ exports.Code = class Code extends Base
# Short-circuit `traverseChildren` method to prevent it from crossing scope boundaries # Short-circuit `traverseChildren` method to prevent it from crossing scope boundaries
# unless `crossScope` is `true`. # unless `crossScope` is `true`.
traverseChildren: (crossScope, func) -> super(crossScope, func) if crossScope traverseChildren: (crossScope, func) -> super(crossScope, func) if crossScope
# Automatically calls the defined function.
do: ->
if @bound
@bound = no
new Call(new Value this, [new Accessor new Literal 'call']
[new Literal 'this'].concat this.params)
else new Call this, this.params
#### Param #### Param

View File

@@ -355,3 +355,8 @@ do (v1) ->
v2 = 4 v2 = 4
ok v1 is 1 ok v1 is 1
ok v2 is 4 ok v2 is 4
cxt = {}
val = null
(-> do => val = this).call cxt
ok val is cxt