Skip to content

Commit

Permalink
Implement jump out.
Browse files Browse the repository at this point in the history
Closes #194.
  • Loading branch information
flatheadmill committed Feb 23, 2014
1 parent efb9ea3 commit c68a2a7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
30 changes: 23 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ function cadence () {
return createCallback(frame, callback, 0)
}

function Label (step) {
function Label (step, offset) {
this.step = step
this.offset = offset
}

function createCadence (frame, callback) {
Expand Down Expand Up @@ -236,7 +237,7 @@ function cadence () {
createCallback(frame, callback, 0).apply(null, [null].concat(vargs))
}

return new Label(callback.steps[0])
return new Label(callback.steps[0], callback.steps.length)
}
}

Expand Down Expand Up @@ -319,7 +320,7 @@ function cadence () {

function invoke (cadence, index, previous, denouement) {
var callbacks = previous.callbacks, vargs = [], arg = 0
var catcher, finalizers, errors, callback, arity, i, j, k, result, hold
var catcher, finalizers, errors, callback, arity, i, j, k, result, hold, jump

if (previous.errors.length) {
catcher = cadence.catchers[index - 1]
Expand All @@ -340,14 +341,29 @@ function cadence () {
return
}

var results = callbacks[0].results[0]
if (results.length == 2 && Array.isArray(results[1])) {
callbacks[0].results[0] = results = [ invoke ].concat(results[1])
}

if (results[1] instanceof Label) {
var iterator = previous
var label = results.splice(1, 1)[0]
while (iterator.args) {
if (iterator.args[0].steps[0] === label.step) {
iterator.args[1] = label.offset
iterator.args[2].callbacks = previous.callbacks
return invoke.apply(this, iterator.args)
}
iterator.args[1] = iterator.args[0].steps.length
iterator = iterator.caller
}
}

// One in callbacks means that there were no callbacks created, we're
// going to use the return value.
if (callbacks.length == 1) {
i = 0, j = 1
var results = callbacks[0].results[0]
if (results.length == 2 && Array.isArray(results[1])) {
callbacks[0].results[0] = [ invoke ].concat(results[1])
}
} else {
i = 1, j = 0
}
Expand Down
1 change: 1 addition & 0 deletions release.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
### Issue by Issue

* Implement jump out. #194.
* Variable arguments last to counted or each loops. #193.
22 changes: 21 additions & 1 deletion t/cadence/loop.t.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

require('proof')(19, function (step, equal, deepEqual) {
require('proof')(20, function (step, equal, deepEqual) {
var fs = require('fs')
var cadence = require('../..')

Expand Down Expand Up @@ -140,4 +140,24 @@ require('proof')(19, function (step, equal, deepEqual) {
if (error) throw error
equal(result, 10, 'loop continue callbacked')
})

cadence(function (step) {
var count = 0
var outer = step(function () {
return 1
}, function (value) {
var inner = step(function () {
return [ value, 2 ]
}, function (one, two) {
return [ outer, one, two ]
}, function () {
throw new Error('should not be thrown')
})(1)
}, function (one, two) {
return [ one, two ]
})(1)
})(function (error, one, two) {
if (error) throw error
deepEqual([ one, two ], [ 1, 2 ], 'jump out')
})
})

0 comments on commit c68a2a7

Please sign in to comment.