Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

stop() control and the wait test from stackedy

  • Loading branch information...
commit 5b18344c40ce98df12851e03d17e10fd7e43fd36 1 parent 654a2b1
James Halliday authored
Showing with 65 additions and 4 deletions.
  1. +14 −4 index.js
  2. +7 −0 test/sources/wait.js
  3. +44 −0 test/wait.js
18 index.js
View
@@ -19,7 +19,8 @@ function Fritter (context) {
call : identifier(6),
catcher : identifier(6),
catchVar : identifier(6),
- expr : identifier(6)
+ expr : identifier(6),
+ stopped : identifier(6)
};
this.stack = [];
this.current = undefined;
@@ -35,6 +36,7 @@ Fritter.prototype.defineContext = function () {
var self = this;
var nodes = this.nodes;
var context = this.context;
+ var names = this.names;
function isBuiltin (fn) {
return fn === context.setTimeout
@@ -44,9 +46,10 @@ Fritter.prototype.defineContext = function () {
;
}
- context[self.names.call] = function (ix, fn) {
+ context[names.call] = function (ix, fn) {
if (typeof fn === 'function' && typeof fn.apply === 'function') {
var fn_ = function () {
+ if (self.stopped) throw names.stopped;
self.stack.unshift(nodes[ix]);
var res = fn.apply(undefined, arguments);
self.stack.shift();
@@ -68,7 +71,9 @@ Fritter.prototype.defineContext = function () {
var caught = [];
var throwing = false;
- self.context[self.names.catcher] = function (err) {
+ self.context[names.catcher] = function (err) {
+ if (err === names.stopped) return;
+
if (caught.indexOf(err) < 0) {
caught.push(err);
self.emit('error', err, {
@@ -88,12 +93,17 @@ Fritter.prototype.defineContext = function () {
};
})();
- context[self.names.expr] = function (ix, expr) {
+ context[names.expr] = function (ix, expr) {
+ if (self.stopped) throw names.stopped;
self.current = self.nodes[ix];
return expr;
};
};
+Fritter.prototype.stop = function () {
+ this.stopped = true;
+};
+
Fritter.prototype.include = function (src, opts) {
var self = this;
if (typeof src === 'object') {
7 test/sources/wait.js
View
@@ -0,0 +1,7 @@
+t.ok(true);
+
+exports.times = 0;
+
+wait(function () {
+ exports.times ++;
+});
44 test/wait.js
View
@@ -0,0 +1,44 @@
+var fritter = require('../');
+var test = require('tap').test;
+var vm = require('vm');
+
+var fs = require('fs');
+var src = fs.readFileSync(__dirname + '/sources/wait.js', 'utf8');
+
+test('wait', function (t) {
+ t.plan(4);
+
+ var iv = null;
+ var context = {
+ wait : function (cb) {
+ t.ok(!iv);
+ iv = setInterval(function () {
+ cb();
+ }, 50);
+ },
+ exports : {},
+ t : { ok : t.ok.bind(t) }
+ };
+
+ var fry = fritter(src, context);
+
+ var x0 = null;
+ setTimeout(function () {
+ x0 = context.exports.times;
+ t.ok(x0 >= 3 || x0 <= 6);
+ fry.stop();
+ }, 325);
+
+ setTimeout(function () {
+ t.equal(context.exports.times, x0);
+ }, 500);
+
+ try {
+ vm.runInNewContext(fry.source, fry.context);
+ }
+ catch (err) { console.log(err.stack) }
+
+ t.on('end', function () {
+ clearInterval(iv);
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.