Skip to content

Commit

Permalink
betteR! gotta run, but close!
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtaco committed Jan 24, 2012
1 parent fd041e1 commit 0795b7d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 48 deletions.
6 changes: 2 additions & 4 deletions lib/coffee-script/cake.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions lib/coffee-script/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 22 additions & 7 deletions src/nodes.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -555,7 +555,13 @@ exports.Literal = class Literal extends Base
code = if @isUndefined code = if @isUndefined
if o.level >= LEVEL_ACCESS then '(void 0)' else 'void 0' if o.level >= LEVEL_ACCESS then '(void 0)' else 'void 0'
else if @value is 'this' else if @value is 'this'
if o.scope.method?.bound then o.scope.method.context else @value if o.scope.method?.bound
o.scope.method.context
else if o.tamed_scope
o.tamed_scope.assign '_this', 'this'
'_this'
else
@value
else if @value.reserved else if @value.reserved
"\"#{@value}\"" "\"#{@value}\""
else if @tameLoopFlag and @tameIsJump() else if @tameLoopFlag and @tameIsJump()
Expand Down Expand Up @@ -1398,6 +1404,7 @@ exports.Code = class Code extends Base
@params = params or [] @params = params or []
@body = body or new Block @body = body or new Block
@bound = tag is 'boundfunc' @bound = tag is 'boundfunc'
@tamegen = tag is 'tamegen'
@context = '_this' if @bound @context = '_this' if @bound


children: ['params', 'body'] children: ['params', 'body']
Expand Down Expand Up @@ -1466,9 +1473,17 @@ exports.Code = class Code extends Base
idt = o.indent idt = o.indent
code = 'function' code = 'function'
code += ' ' + @name if @ctor code += ' ' + @name if @ctor
<<<<<<< HEAD
code += '(' + params.join(', ') + ') {' code += '(' + params.join(', ') + ') {'
if @tameNodeFlag if @tameNodeFlag
o.tamed_scope = o.scope o.tamed_scope = o.scope
=======
code += '(' + vars.join(', ') + ') {'

unless @tamegen
o.tamed_scope = if @tameNodeFlag then o.scope else null

>>>>>>> betteR! gotta run, but close!
code += "\n#{ @body.compileWithDeclarations o }\n#{@tab}" unless @body.isEmpty() code += "\n#{ @body.compileWithDeclarations o }\n#{@tab}" unless @body.isEmpty()
code += '}' code += '}'
return @tab + code if @ctor return @tab + code if @ctor
Expand Down Expand Up @@ -1647,12 +1662,12 @@ exports.While = class While extends Base
continue_id = new Value new Literal tame.const.c_while continue_id = new Value new Literal tame.const.c_while
continue_block = new Block [ new Call top_id, [ k_id ] ] continue_block = new Block [ new Call top_id, [ k_id ] ]
continue_block.unshift d.step if d.step continue_block.unshift d.step if d.step
continue_body = new Code [], continue_block continue_body = new Code [], continue_block, 'tamegen'
continue_assign = new Assign continue_id, continue_body continue_assign = new Assign continue_id, continue_body
cond = new If condition, body cond = new If condition, body
cond.addElse new Block [ new Call break_id, [] ] cond.addElse new Block [ new Call break_id, [] ]
top_body = new Block [ break_assign, continue_assign, cond ] top_body = new Block [ break_assign, continue_assign, cond ]
top_func = new Code [ k_id ], top_body top_func = new Code [ k_id ], top_body, 'tamegen'
top_assign = new Assign top_id, top_func top_assign = new Assign top_id, top_func
top_call = new Call top_id, [ k_id ] top_call = new Call top_id, [ k_id ]
top_statements = [] top_statements = []
Expand Down Expand Up @@ -1961,9 +1976,9 @@ exports.Defer = class Defer extends Base
assignments.push assign assignments.push assign
i++ i++
block = new Block assignments block = new Block assignments
inner_fn = new Code [], block inner_fn = new Code [], block, 'tamegen'
outer_block = new Block [ new Return inner_fn ] outer_block = new Block [ new Return inner_fn ]
outer_fn = new Code @params, outer_block outer_fn = new Code @params, outer_block, 'tamegen'
call = new Call outer_fn, args call = new Call outer_fn, args


transform : -> transform : ->
Expand Down Expand Up @@ -2560,9 +2575,9 @@ Closure =
CpsCascade = CpsCascade =


wrap: (statement, rest) -> wrap: (statement, rest) ->
func = new Code [ new Param new Literal tame.const.k ], (Block.wrap [ statement ]), 'boundfunc' func = new Code [ new Param new Literal tame.const.k ], (Block.wrap [ statement ]), 'tamegen'
block = Block.wrap [ rest ] block = Block.wrap [ rest ]
cont = new Code [], block, 'boundfunc' cont = new Code [], block, 'tamegen'
call = new Call func, [ cont ] call = new Call func, [ cont ]
new Block [ call ] new Block [ call ]


Expand Down
58 changes: 29 additions & 29 deletions test/tame.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -131,20 +131,6 @@ atest "simple autocb operations", (cb) ->
true true
await foo defer b await foo defer b
cb(b, {}) cb(b, {})

atest "test nested serial/parallel", (cb) ->
slots = []
await
for i in [0..10]
( (j, autocb) ->
await delay defer(), 5*Math.random()
await delay defer(), 4*Math.random()
slots[j] = true
)(i, defer())
ok = true
for i in [0..10]
ok = false unless slots[i]
cb(ok, {})


atest "AT variable works in an await (1)", (cb) -> atest "AT variable works in an await (1)", (cb) ->
class MyClass class MyClass
Expand All @@ -159,21 +145,6 @@ atest "AT variable works in an await (1)", (cb) ->
o = new MyClass o = new MyClass
await o.run defer() await o.run defer()
cb(o.getFlag(), {}) cb(o.getFlag(), {})

atest "AT variable works in an await (2)", (cb) ->
class MyClass
constructor : -> @val = 0
inc : -> @val++
chill : (autocb) -> await delay defer()
run : (autocb) ->
await @chill defer()
for i in [0..10]
await @chill defer()
@inc()
getVal : -> @val
o = new MyClass
await o.run defer()
cb(o.getVal() == 10, {})


atest "more advanced autocb test", (cb) -> atest "more advanced autocb test", (cb) ->
bar = -> "yoyo" bar = -> "yoyo"
Expand Down Expand Up @@ -201,3 +172,32 @@ atest "test of autocb in a simple function", (cb) ->
await simple defer() await simple defer()
ok = true ok = true
cb(ok,{}) cb(ok,{})

atest "test nested serial/parallel", (cb) ->
slots = []
await
for i in [0..10]
( (j, autocb) ->
await delay defer(), 5*Math.random()
await delay defer(), 4*Math.random()
slots[j] = true
)(i, defer())
ok = true
for i in [0..10]
ok = false unless slots[i]
cb(ok, {})

atest "AT variable works in an await (2)", (cb) ->
class MyClass
constructor : -> @val = 0
inc : -> @val++
chill : (autocb) -> await delay defer()
run : (autocb) ->
await @chill defer()
for i in [0..10]
await @chill defer()
@inc()
getVal : -> @val
o = new MyClass
await o.run defer()
cb(o.getVal() == 10, {})

0 comments on commit 0795b7d

Please sign in to comment.