Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

got a lot of the Deferral logic working as it does in regular tame

  • Loading branch information...
commit 9b0965c2cd148368e9d63facf83904871f33c465 1 parent b3e2297
@maxtaco authored
View
118 lib/coffee-script/nodes.js
@@ -663,6 +663,10 @@
Value.prototype.children = ['base', 'properties'];
+ Value.prototype.copy = function() {
+ return new Value(this.base, this.properties);
+ };
+
Value.prototype.add = function(props) {
this.properties = this.properties.concat(props);
return this;
@@ -728,11 +732,12 @@
};
Value.prototype.toSlot = function() {
- var props, slotLoc;
- slotLoc = null;
- props = this.properties;
- if (props && props.length) slotLoc = props.pop();
- return new Slot(this.base, props, slotLoc);
+ var sufffix, suffix;
+ sufffix = null;
+ if (this.properties && this.properties.length) {
+ suffix = this.properties.pop();
+ }
+ return new Slot(this, suffix);
};
Value.prototype.cacheReference = function(o) {
@@ -1722,7 +1727,10 @@
code = 'function';
if (this.ctor) code += ' ' + this.name;
code += '(' + vars.join(', ') + ') {';
- if (this.tameNodeFlag) code += ' /* TAMED */ ';
+ if (this.tameNodeFlag) {
+ code += ' /* TAMED */ ';
+ o.tamed_scope = o.scope;
+ }
if (!this.body.isEmpty()) {
code += "\n" + (this.body.compileWithDeclarations(o)) + "\n" + this.tab;
}
@@ -2201,14 +2209,12 @@
__extends(Slot, Base);
- function Slot(base, props, loc) {
- this.base = base;
- this.props = props;
- this.loc = loc;
- console.log("at Last! " + loc);
+ function Slot(value, suffix) {
+ this.value = value;
+ this.suffix = suffix;
}
- Slot.prototype.children = ['base', 'props', 'loc'];
+ Slot.prototype.children = ['value', 'suffix'];
return Slot;
@@ -2219,21 +2225,87 @@
__extends(Defer, Base);
function Defer(args) {
- var a, _i, _len;
- for (_i = 0, _len = args.length; _i < _len; _i++) {
- a = args[_i];
- this.slots = a.toSlot();
- }
+ var a;
+ this.slots = (function() {
+ var _i, _len, _results;
+ _results = [];
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
+ a = args[_i];
+ _results.push(a.toSlot());
+ }
+ return _results;
+ })();
+ this.params = [];
+ this.vars = [];
}
Defer.prototype.children = ['slots'];
- Defer.prototype.compileNode = function(p) {
- var call, meth;
- call = new Value(new Literal(tame["const"].deferrals));
+ Defer.prototype.newParam = function() {
+ var l, v;
+ l = "" + tame["const"].slot + "_" + (this.params.length + 1);
+ v = new Value(new Literal(l));
+ this.params.push(v.copy());
+ return v;
+ };
+
+ Defer.prototype.makeAssignFn = function(o) {
+ var a, args, assign, assignments, block, call, i, inner_fn, outer_block, outer_fn, prop, s, slot, _i, _len, _ref2;
+ assignments = [];
+ args = [];
+ i = 0;
+ _ref2 = this.slots;
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
+ s = _ref2[_i];
+ a = new Value(new Literal("arguments"));
+ a.add(new Index(new Value(new Literal(i))));
+ if (!s.suffix) {
+ slot = s.value;
+ this.vars.push(slot);
+ } else {
+ args.push(s.value);
+ slot = this.newParam();
+ if (s.suffix instanceof Index) {
+ prop = new Index(this.newParam());
+ args.push(s.suffix.index);
+ } else {
+ prop = s.suffix;
+ }
+ slot.add(prop);
+ }
+ assign = new Assign(slot, a);
+ assignments.push(assign);
+ i++;
+ }
+ block = new Block(assignments);
+ inner_fn = new Code([], block);
+ outer_block = new Block([new Return(inner_fn)]);
+ outer_fn = new Code(this.params, outer_block);
+ return call = new Call(outer_fn, args);
+ };
+
+ Defer.prototype.transform = function() {
+ var assign_assign, assign_fn, fn, meth, o;
+ fn = new Value(new Literal(tame["const"].deferrals));
meth = new Value(new Literal(tame["const"].defer_method));
- call.add(new Access(meth));
- return "defer()";
+ fn.add(new Access(meth));
+ assign_fn = this.makeAssignFn();
+ assign_assign = new Assign(new Value(new Literal(tame["const"].assign_fn)), assign_fn, "object");
+ o = new Obj([assign_assign]);
+ return this.call = new Call(fn, [new Value(o)]);
+ };
+
+ Defer.prototype.compileNode = function(o) {
+ var name, scope, v, _i, _len, _ref2;
+ this.transform();
+ _ref2 = this.vars;
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
+ v = _ref2[_i];
+ name = v.compile(o, LEVEL_LIST);
+ scope = o.tamed_scope || o.scope;
+ scope.add(name, 'var');
+ }
+ return this.call.compile(o);
};
return Defer;
@@ -2259,7 +2331,7 @@
assign = new Assign(lhs, rhs);
body.unshift(assign);
if (k = this.needsDummyContinuation()) body.unshift(k);
- meth = lhs.add(new Access(new Value(new Literal(tame["const"].fulfill))));
+ meth = lhs.copy().add(new Access(new Value(new Literal(tame["const"].fulfill))));
call = new Call(meth, []);
body.push(call);
return this.body = body;
View
4 lib/coffee-script/tame.js
@@ -27,7 +27,9 @@
b_while: "_break",
t_while: "_while",
c_while: "_continue",
- defer_method: "defer"
+ defer_method: "defer",
+ slot: "__slot",
+ assign_fn: "assign_fn"
};
}).call(this);
View
63 src/nodes.coffee
@@ -548,6 +548,9 @@ exports.Value = class Value extends Base
children: ['base', 'properties']
+ copy : ->
+ return new Value @base, @properties
+
# Add a property (or *properties* ) `Access` to the list.
add: (props) ->
@properties = @properties.concat props
@@ -1390,7 +1393,9 @@ exports.Code = class Code extends Base
code = 'function'
code += ' ' + @name if @ctor
code += '(' + params.join(', ') + ') {'
- code += ' /* TAMED */ ' if @tameNodeFlag
+ if @tameNodeFlag
+ code += ' /* TAMED */ '
+ o.tamed_scope = o.scope
code += "\n#{ @body.compileWithDeclarations o }\n#{@tab}" unless @body.isEmpty()
code += '}'
return @tab + code if @ctor
@@ -1797,43 +1802,36 @@ exports.Slot = class Slot extends Base
children : [ 'value', 'suffix' ]
- pieces: (o) ->
- ret = []
- if suffix
- ret.push @value.compile o
- ret.push @suffix.compile o
- ret
-
#### Defer
exports.Defer = class Defer extends Base
constructor : (args) ->
- @slots = a.toSlot() for a in args
+ @slots = for a in args
+ a.toSlot()
@params = []
+ @vars = []
- children : ['slots']
+ children : ['slots' ]
# return a copy so we don't have problems in making our function
newParam : ->
- n = "#{tame.const.slot}#{@vars.length + 1}"
- @param.push new Value new Literal n
- new Value new Literal n
+ l = "#{tame.const.slot}_#{@params.length + 1}"
+ v = new Value new Literal l
+ @params.push v.copy()
+ v
makeAssignFn : (o) ->
- pieces = []
- for s in @slots pieces.concat s.pieces o
- i = 1
assignments = []
args = []
- i = 1
+ i = 0
for s in @slots
a = new Value new Literal "arguments"
- a.add new Index i
+ a.add new Index new Value new Literal i
if not s.suffix
slot = s.value
+ @vars.push slot
else
args.push s.value
- params.push p
slot = @newParam()
if s.suffix instanceof Index
prop = new Index @newParam()
@@ -1846,26 +1844,27 @@ exports.Defer = class Defer extends Base
i++
block = new Block assignments
inner_fn = new Code [], block
- outer_block = new Block new Return inner_fn
+ outer_block = new Block [ new Return inner_fn ]
outer_fn = new Code @params, outer_block
call = new Call outer_fn, args
- compileNode : (o) ->
+ transform : ->
fn = new Value new Literal tame.const.deferrals
meth = new Value new Literal tame.const.defer_method
fn.add new Access meth
assign_fn = @makeAssignFn()
- args =
- assign_fn : assign_fn
- call = new Call fn, [ args ]
- call.compile o
+ assign_assign = new Assign(new Value(new Literal(tame.const.assign_fn)),
+ assign_fn, "object")
+ o = new Obj [ assign_assign ]
+ @call = new Call fn, [ new Value o ]
-##### Call or Defer
-#
-#exports.CallOrDefer = class CallOrDefer
-# wrap: (val, args, opt) ->
-# if val == 'defer' new Defer args
-# else new Call val, args, opts
+ compileNode : (o) ->
+ @transform()
+ for v in @vars
+ name = v.compile o, LEVEL_LIST
+ scope = o.tamed_scope || o.scope
+ scope.add name, 'var'
+ @call.compile o
#### Await
@@ -1884,7 +1883,7 @@ exports.Await = class Await extends Base
body.unshift assign
if k = @needsDummyContinuation()
body.unshift (k)
- meth = lhs.add new Access new Value new Literal tame.const.fulfill
+ meth = lhs.copy().add new Access new Value new Literal tame.const.fulfill
call = new Call meth, []
body.push (call)
@body = body
View
1  src/tame.coffee
@@ -18,3 +18,4 @@ exports.const =
c_while : "_continue"
defer_method : "defer"
slot : "__slot"
+ assign_fn : "assign_fn"
Please sign in to comment.
Something went wrong with that request. Please try again.