Skip to content
Browse files

futures support in spoon + some spoon unit tests pass

  • Loading branch information...
1 parent a623611 commit abf6982990520b97bb643f2e30f3349d152ad944 @bjouhier bjouhier committed
View
5 lib/compiler/build.sh
@@ -10,6 +10,10 @@ rm builtins.js
cat builtins.js | sed -e "s/\/\/\/ \!doc//" > ../generators/builtins.js
rm builtins.js
+../../bin/_node -lp -v -f --spoon -c builtins._js
+cat builtins.js | sed -e "s/\/\/\/ \!doc//" > ../spoon/builtins.js
+rm builtins.js
+
../../bin/_node -lp -v -f -c compile._js
../../bin/_node -lp -v -f -c ../streams/client/streams._js
@@ -19,5 +23,6 @@ pushd ../../test/common > /dev/null
mv eval-test.js flows-test.js stack-test.js futures-test.js callbacks
../../bin/_node --generators -v -f -c .
mv eval-test.js flows-test.js stack-test.js futures-test.js generators
+../../bin/_node --spoon -v -f -c spoon
popd > /dev/null
popd > /dev/null
View
2 lib/compiler/underscored.js
@@ -3,7 +3,7 @@ var path = require("path");
var fs = require("fs");
function run(options) {
- var transformer = "streamline/lib/" + (options.generators ? "generators" : options.fibers ? "fibers" : "callbacks") + "/transform";
+ var transformer = "streamline/lib/" + (options.generators ? "generators" : options.fibers ? "fibers" : options.spoon ? "spoon" : "callbacks") + "/transform";
var streamline = require(transformer).transform;
function clone(obj) {
View
1,086 lib/spoon/builtins.js
@@ -0,0 +1,1086 @@
+/*** Generated by streamline 0.4.5 (spoon) - DO NOT EDIT ***/var __$rt = require('streamline/lib/spoon/runtime');
+(function(__$callback) {
+var __$i0 = function(exports) {
+ var __$i68;
+ var __$i53;
+ var has;
+ var funnel;
+ var future;
+ var VERSION;
+ "use strict";
+ VERSION = 3;
+ future = function(fn, args, i) {
+ var err, result, done, q, self;
+ q = [];
+ self = this;
+ args = Array.prototype.slice.call(args);
+ args[i] = function(e, r) {
+ var __$i1214;
+ var __$i1204;
+ err = e;
+ result = r;
+ done = true;
+ __$i1204 = q;
+ if (__$i1204) {
+ __$i1214 = q.forEach(function(f) {
+ f.call(self, e, r);
+ });
+ } else {
+ __$i1214 = __$i1204;
+ };
+ q = null;
+ };
+ fn.apply(this, args);
+ return function F(cb) {
+ if (!cb) {
+ return F;
+ } else {
+ if (done) {
+ cb.call(self, err, result);
+ } else {
+ q.push(cb);
+ };
+ };
+ };
+ };
+ exports.funnel = function(max) {
+ var __$i295;
+ var fun;
+ var queue, active, closed;
+ if (max == null) {
+ __$i295 = -1;
+ } else {
+ __$i295 = max;
+ };
+ max = __$i295;
+ if (max === 0) {
+ max = funnel.defaultSize;
+ } else {};
+ if (typeof max !== "number") {
+ throw new Error("bad max number: " + max);
+ } else {
+ queue = [];
+ active = 0;
+ closed = false;
+ fun = function(callback, fn) {
+ var __$i1290;
+ var __$i1279;
+ var __$i1260;
+ var __$i1251;
+ if (callback == null) {
+ return future(fun, arguments, 0);
+ } else {
+ __$i1251 = max < 0;
+ if (__$i1251) {
+ __$i1260 = __$i1251;
+ } else {
+ __$i1260 = max == Infinity;
+ };
+ if (__$i1260) {
+ return fn(callback);
+ } else {
+ queue.push({
+ fn: fn,
+ cb: callback
+ });
+ function _doOne() {
+ var current;
+ current = queue.splice(0, 1)[0];
+ if (!current.cb) {
+ return current.fn();
+ } else {
+ active++;
+ current.fn(function(err, result) {
+ var __$i1593;
+ var __$i1582;
+ active--;
+ if (!closed) {
+ current.cb(err, result);
+ while (true) {
+ __$i1582 = active < max;
+ if (__$i1582) {
+ __$i1593 = queue.length > 0;
+ } else {
+ __$i1593 = __$i1582;
+ };
+ if (!__$i1593) {
+ break;
+ } else {
+ _doOne();
+ };
+ };
+ } else {};
+ });
+ };
+ };
+ while (true) {
+ __$i1279 = active < max;
+ if (__$i1279) {
+ __$i1290 = queue.length > 0;
+ } else {
+ __$i1290 = __$i1279;
+ };
+ if (!__$i1290) {
+ break;
+ } else {
+ _doOne();
+ };
+ };
+ };
+ };
+ };
+ fun.close = function() {
+ queue = [];
+ closed = true;
+ };
+ return fun;
+ };
+ };
+ funnel = exports.funnel;
+ funnel.defaultSize = 4;
+ function _parallel(options) {
+ var __$i363;
+ if (typeof options === "number") {
+ return options;
+ } else {
+ if (typeof options.parallel === "number") {
+ return options.parallel;
+ } else {
+ if (options.parallel) {
+ __$i363 = -1;
+ } else {
+ __$i363 = 1;
+ };
+ return __$i363;
+ };
+ };
+ };
+ __$i53 = Array.prototype.forEach_;
+ if (__$i53) {
+ __$i68 = Array.prototype.forEach_.version_ >= VERSION;
+ } else {
+ __$i68 = __$i53;
+ };
+ if (__$i68) {
+ return;
+ } else {
+ try {
+ Object.defineProperty({}, "x", {});
+ has = Object.prototype.hasOwnProperty;
+ delete Array.prototype.forEach_;
+ Object.defineProperty(Array.prototype, "forEach_", {
+ configurable: true,
+ writable: true,
+ enumerable: false,
+ value: function(__$callback, options, fn, thisObj) {
+ var __$i412;
+ var __$i403;
+ var __$i393;
+ function __$fn386(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < len)) {
+ break;
+ } else {
+ has.call(this, i);
+ return __$fn385.call(this);
+ };
+ };
+ return __$fn383.call(this);
+ };
+ function __$fn385(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ i++;
+ return __$fn386.call(this);
+ };
+ function __$fn384(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$fn385.call(this);
+ };
+ function __$fn383(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, this);
+ };
+ function __$fn382(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$fn383.call(this);
+ };
+ var i;
+ var len;
+ var par;
+ if (typeof options === "function") {
+ thisObj = fn;
+ fn = options;
+ options = 1;
+ } else {};
+ par = _parallel(options);
+ if (thisObj !== undefined) {
+ __$i393 = thisObj;
+ } else {
+ __$i393 = this;
+ };
+ thisObj = __$i393;
+ len = this.length;
+ __$i403 = par === 1;
+ if (__$i403) {
+ __$i412 = __$i403;
+ } else {
+ __$i412 = len <= 1;
+ };
+ if (__$i412) {
+ i = 0;
+ return __$fn386.call(this);
+ } else {
+ return this.map_(__$fn382, par, fn, thisObj);
+ };
+ }
+ });
+ Array.prototype.forEach_.version_ = VERSION;
+ delete Array.prototype.map_;
+ Object.defineProperty(Array.prototype, "map_", {
+ configurable: true,
+ writable: true,
+ enumerable: false,
+ value: function(__$callback, options, fn, thisObj) {
+ var __$i508;
+ var __$i499;
+ var __$i488;
+ function __$fn393(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < len)) {
+ break;
+ } else {
+ has.call(this, i);
+ return __$fn392.call(this);
+ };
+ };
+ return __$fn390.call(this);
+ };
+ function __$fn392(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ i++;
+ return __$fn393.call(this);
+ };
+ function __$fn391(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i581[__$i582] = __$r;
+ return __$fn392.call(this);
+ };
+ function __$fn390(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, result);
+ };
+ function __$fn389(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < len)) {
+ break;
+ } else {
+ has.call(this, i);
+ return __$fn388.call(this);
+ };
+ };
+ return __$fn390.call(this);
+ };
+ function __$fn388(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ i++;
+ return __$fn389.call(this);
+ };
+ function __$fn387(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i533[__$i534] = __$r;
+ return __$fn388.call(this);
+ };
+ var i;
+ var fun;
+ var i;
+ var result;
+ var len;
+ var par;
+ if (typeof options === "function") {
+ thisObj = fn;
+ fn = options;
+ options = 1;
+ } else {};
+ par = _parallel(options);
+ if (thisObj !== undefined) {
+ __$i488 = thisObj;
+ } else {
+ __$i488 = this;
+ };
+ thisObj = __$i488;
+ len = this.length;
+ __$i499 = par === 1;
+ if (__$i499) {
+ __$i508 = __$i499;
+ } else {
+ __$i508 = len <= 1;
+ };
+ if (__$i508) {
+ result = new Array(len);
+ i = 0;
+ return __$fn389.call(this);
+ } else {
+ fun = funnel(par);
+ result = this.map(function(elt, i) {
+ return fun(null, function(__$callback) {
+ function __$fn435(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, __$r);
+ };
+ return fn.call(thisObj, __$fn435, elt, i);
+ });
+ });
+ i = 0;
+ return __$fn393.call(this);
+ };
+ }
+ });
+ delete Array.prototype.filter_;
+ Object.defineProperty(Array.prototype, "filter_", {
+ configurable: true,
+ writable: true,
+ enumerable: false,
+ value: function(__$callback, options, fn, thisObj) {
+ var __$i648;
+ var __$i639;
+ var __$i626;
+ function __$fn398(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < len)) {
+ break;
+ } else {
+ has.call(this, i);
+ return __$fn397.call(this);
+ };
+ };
+ return __$fn395.call(this);
+ };
+ function __$fn397(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ i++;
+ return __$fn398.call(this);
+ };
+ function __$fn396(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$r) {
+ result.push(elt);
+ } else {};
+ return __$fn397.call(this);
+ };
+ function __$fn395(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, result);
+ };
+ function __$fn394(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$fn395.call(this);
+ };
+ var elt;
+ var i;
+ var len;
+ var result;
+ var par;
+ if (typeof options === "function") {
+ thisObj = fn;
+ fn = options;
+ options = 1;
+ } else {};
+ par = _parallel(options);
+ if (thisObj !== undefined) {
+ __$i626 = thisObj;
+ } else {
+ __$i626 = this;
+ };
+ thisObj = __$i626;
+ result = [];
+ len = this.length;
+ __$i639 = par === 1;
+ if (__$i639) {
+ __$i648 = __$i639;
+ } else {
+ __$i648 = len <= 1;
+ };
+ if (__$i648) {
+ i = 0;
+ return __$fn398.call(this);
+ } else {
+ return this.map_(__$fn394, par, function(__$callback, elt) {
+ function __$fn420(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$r) {
+ result.push(elt);
+ } else {};
+ return __$callback.call(this, null);
+ };
+ return fn.call(thisObj, __$fn420, elt);
+ }, thisObj);
+ };
+ }
+ });
+ delete Array.prototype.every_;
+ Object.defineProperty(Array.prototype, "every_", {
+ configurable: true,
+ writable: true,
+ enumerable: false,
+ value: function(__$callback, options, fn, thisObj) {
+ var __$i824;
+ var __$i835;
+ var __$i770;
+ var __$i785;
+ var __$i751;
+ var __$i742;
+ var __$i732;
+ function __$fn405(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < len)) {
+ break;
+ } else {
+ __$i824 = has.call(this, i);
+ if (__$i824) {
+ return futures[i](__$fn403);
+ } else {
+ __$i835 = __$i824;
+ return __$fn404.call(this);
+ };
+ };
+ };
+ return __$fn402.call(this);
+ };
+ function __$fn404(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i835) {
+ fun.close();
+ return __$callback.call(this, null, false);
+ } else {
+ i++;
+ return __$fn405.call(this);
+ };
+ };
+ function __$fn403(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i835 = !__$r;
+ return __$fn404.call(this);
+ };
+ function __$fn402(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, true);
+ };
+ function __$fn401(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < len)) {
+ break;
+ } else {
+ __$i770 = has.call(this, i);
+ if (__$i770) {
+ return fn.call(thisObj, __$fn399, this[i]);
+ } else {
+ __$i785 = __$i770;
+ return __$fn400.call(this);
+ };
+ };
+ };
+ return __$fn402.call(this);
+ };
+ function __$fn400(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i785) {
+ return __$callback.call(this, null, false);
+ } else {
+ i++;
+ return __$fn401.call(this);
+ };
+ };
+ function __$fn399(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i785 = !__$r;
+ return __$fn400.call(this);
+ };
+ var i;
+ var futures;
+ var fun;
+ var i;
+ var len;
+ var par;
+ if (typeof options === "function") {
+ thisObj = fn;
+ fn = options;
+ options = 1;
+ } else {};
+ par = _parallel(options);
+ if (thisObj !== undefined) {
+ __$i732 = thisObj;
+ } else {
+ __$i732 = this;
+ };
+ thisObj = __$i732;
+ len = this.length;
+ __$i742 = par === 1;
+ if (__$i742) {
+ __$i751 = __$i742;
+ } else {
+ __$i751 = len <= 1;
+ };
+ if (__$i751) {
+ i = 0;
+ return __$fn401.call(this);
+ } else {
+ fun = funnel(par);
+ futures = this.map(function(elt) {
+ return fun(null, function(__$callback) {
+ function __$fn436(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, __$r);
+ };
+ return fn.call(thisObj, __$fn436, elt);
+ });
+ });
+ i = 0;
+ return __$fn405.call(this);
+ };
+ }
+ });
+ delete Array.prototype.some_;
+ Object.defineProperty(Array.prototype, "some_", {
+ configurable: true,
+ writable: true,
+ enumerable: false,
+ value: function(__$callback, options, fn, thisObj) {
+ var __$i971;
+ var __$i981;
+ var __$i918;
+ var __$i932;
+ var __$i899;
+ var __$i890;
+ var __$i880;
+ function __$fn412(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < len)) {
+ break;
+ } else {
+ __$i971 = has.call(this, i);
+ if (__$i971) {
+ return futures[i](__$fn410);
+ } else {
+ __$i981 = __$i971;
+ return __$fn411.call(this);
+ };
+ };
+ };
+ return __$fn409.call(this);
+ };
+ function __$fn411(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i981) {
+ fun.close();
+ return __$callback.call(this, null, true);
+ } else {
+ i++;
+ return __$fn412.call(this);
+ };
+ };
+ function __$fn410(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i981 = __$r;
+ return __$fn411.call(this);
+ };
+ function __$fn409(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, false);
+ };
+ function __$fn408(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < len)) {
+ break;
+ } else {
+ __$i918 = has.call(this, i);
+ if (__$i918) {
+ return fn.call(thisObj, __$fn406, this[i]);
+ } else {
+ __$i932 = __$i918;
+ return __$fn407.call(this);
+ };
+ };
+ };
+ return __$fn409.call(this);
+ };
+ function __$fn407(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i932) {
+ return __$callback.call(this, null, true);
+ } else {
+ i++;
+ return __$fn408.call(this);
+ };
+ };
+ function __$fn406(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i932 = __$r;
+ return __$fn407.call(this);
+ };
+ var i;
+ var futures;
+ var fun;
+ var i;
+ var len;
+ var par;
+ if (typeof options === "function") {
+ thisObj = fn;
+ fn = options;
+ options = 1;
+ } else {};
+ par = _parallel(options);
+ if (thisObj !== undefined) {
+ __$i880 = thisObj;
+ } else {
+ __$i880 = this;
+ };
+ thisObj = __$i880;
+ len = this.length;
+ __$i890 = par === 1;
+ if (__$i890) {
+ __$i899 = __$i890;
+ } else {
+ __$i899 = len <= 1;
+ };
+ if (__$i899) {
+ i = 0;
+ return __$fn408.call(this);
+ } else {
+ fun = funnel(par);
+ futures = this.map(function(elt) {
+ return fun(null, function(__$callback) {
+ function __$fn437(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, __$r);
+ };
+ return fn.call(thisObj, __$fn437, elt);
+ });
+ });
+ i = 0;
+ return __$fn412.call(this);
+ };
+ }
+ });
+ delete Array.prototype.reduce_;
+ Object.defineProperty(Array.prototype, "reduce_", {
+ configurable: true,
+ writable: true,
+ enumerable: false,
+ value: function(__$callback, fn, v, thisObj) {
+ var __$i1009;
+ function __$fn415(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < len)) {
+ break;
+ } else {
+ has.call(this, i);
+ return __$fn414.call(this);
+ };
+ };
+ return __$callback.call(this, null, v);
+ };
+ function __$fn414(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ i++;
+ return __$fn415.call(this);
+ };
+ function __$fn413(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ v = __$r;
+ return __$fn414.call(this);
+ };
+ var i;
+ var len;
+ if (thisObj !== undefined) {
+ __$i1009 = thisObj;
+ } else {
+ __$i1009 = this;
+ };
+ thisObj = __$i1009;
+ len = this.length;
+ i = 0;
+ return __$fn415.call(this);
+ }
+ });
+ delete Array.prototype.reduceRight_;
+ Object.defineProperty(Array.prototype, "reduceRight_", {
+ configurable: true,
+ writable: true,
+ enumerable: false,
+ value: function(__$callback, fn, v, thisObj) {
+ var __$i1066;
+ function __$fn418(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i >= 0)) {
+ break;
+ } else {
+ has.call(this, i);
+ return __$fn417.call(this);
+ };
+ };
+ return __$callback.call(this, null, v);
+ };
+ function __$fn417(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ i--;
+ return __$fn418.call(this);
+ };
+ function __$fn416(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ v = __$r;
+ return __$fn417.call(this);
+ };
+ var i;
+ var len;
+ if (thisObj !== undefined) {
+ __$i1066 = thisObj;
+ } else {
+ __$i1066 = this;
+ };
+ thisObj = __$i1066;
+ len = this.length;
+ i = len - 1;
+ return __$fn418.call(this);
+ }
+ });
+ delete Array.prototype.sort_;
+ Object.defineProperty(Array.prototype, "sort_", {
+ configurable: true,
+ writable: true,
+ enumerable: false,
+ value: function(__$callback, compare, beg, end) {
+ var __$i1142;
+ var __$i1125;
+ var __$i1118;
+ function __$fn419(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, array);
+ };
+ var array;
+ array = this;
+ __$i1118 = beg;
+ if (__$i1118) {
+ __$i1125 = __$i1118;
+ } else {
+ __$i1125 = 0;
+ };
+ beg = __$i1125;
+ if (end == null) {
+ __$i1142 = array.length - 1;
+ } else {
+ __$i1142 = end;
+ };
+ end = __$i1142;
+ function _qsort(__$callback, beg, end) {
+ var __$i1440;
+ var __$i1455;
+ var __$i1409;
+ var __$i1424;
+ function __$fn426(__$e, __$r) {
+ function __$fn430(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (nbeg <= nend) {
+ tmp = array[nbeg];
+ array[nbeg] = array[nend];
+ array[nend] = tmp;
+ nbeg++;
+ nend--;
+ } else {};
+ return __$fn425.call(this);
+ };
+ function __$fn429(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ __$i1440 = beg < nend;
+ if (__$i1440) {
+ return compare(__$fn427, o, array[nend]);
+ } else {
+ __$i1455 = __$i1440;
+ return __$fn428.call(this);
+ };
+ };
+ return __$fn430.call(this);
+ };
+ function __$fn428(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (!__$i1455) {
+ return __$fn430.call(this);
+ } else {
+ nend--;
+ return __$fn429.call(this);
+ };
+ };
+ function __$fn427(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i1455 = __$r < 0;
+ return __$fn428.call(this);
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$fn429.call(this);
+ };
+ function __$fn425(__$e, __$r) {
+ function __$fn432(__$e, __$r) {
+ function __$fn434(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null);
+ };
+ function __$fn433(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$fn434.call(this);
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ beg < nend;
+ return __$fn434.call(this);
+ };
+ function __$fn431(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$fn432.call(this);
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(nbeg <= nend)) {
+ break;
+ } else {
+ return __$fn424.call(this);
+ };
+ };
+ nbeg < end;
+ return __$fn432.call(this);
+ };
+ function __$fn424(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ __$i1409 = nbeg < end;
+ if (__$i1409) {
+ return compare(__$fn422, array[nbeg], o);
+ } else {
+ __$i1424 = __$i1409;
+ return __$fn423.call(this);
+ };
+ };
+ return __$fn426.call(this);
+ };
+ function __$fn423(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (!__$i1424) {
+ return __$fn426.call(this);
+ } else {
+ nbeg++;
+ return __$fn424.call(this);
+ };
+ };
+ function __$fn422(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i1424 = __$r < 0;
+ return __$fn423.call(this);
+ };
+ function __$fn421(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$r > 0) {
+ tmp = array[beg];
+ array[beg] = array[end];
+ array[end] = tmp;
+ } else {};
+ return __$callback.call(this, null);
+ };
+ var tmp;
+ var nend;
+ var nbeg;
+ var o;
+ var mid;
+ var tmp;
+ if (beg >= end) {
+ return __$callback.call(this, null);
+ } else {
+ if (end == beg + 1) {
+ return compare(__$fn421, array[beg], array[end]);
+ } else {
+ mid = Math.floor((beg + end) / 2);
+ o = array[mid];
+ nbeg = beg;
+ nend = end;
+ return __$fn425.call(this);
+ };
+ };
+ };
+ return _qsort(__$fn419, beg, end);
+ }
+ });
+ delete Function.prototype.apply_;
+ Object.defineProperty(Function.prototype, "apply_", {
+ configurable: true,
+ writable: true,
+ enumerable: false,
+ value: function(callback, thisObj, args, index) {
+ var __$i1187;
+ var __$i1176;
+ var __$i1163;
+ var __$i1164;
+ var __$i1167;
+ args = Array.prototype.slice.call(args, 0);
+ __$i1163 = args;
+ __$i1164 = "splice";
+ __$i1167 = index != null;
+ if (__$i1167) {
+ __$i1176 = index >= 0;
+ } else {
+ __$i1176 = __$i1167;
+ };
+ if (__$i1176) {
+ __$i1187 = index;
+ } else {
+ __$i1187 = args.length;
+ };
+ __$i1163[__$i1164](__$i1187, 0, callback);
+ return this.apply(thisObj, args);
+ }
+ });
+ } catch (e) {
+ return;
+ };
+ };
+};
+
+if (typeof exports !== "undefined") {
+ __$i25 = exports;
+} else {
+ var __$i9 = Streamline;
+ var __$i10 = "builtins";
+ var __$i13 = Streamline.builtins;
+ if (__$i13) {
+ __$i20 = __$i13;
+ } else {
+ __$i20 = {};
+ };
+ var __$i20;
+ __$i25 = __$i9[__$i10] = __$i20;
+};
+
+var __$i25;
+
+__$i0(__$i25);
+
+return __$callback.call(this, null);
+})(function(__$e) { if (__$e) console.log(__$e); });
View
13 lib/spoon/require-stub.js
@@ -0,0 +1,13 @@
+if (!Object.create || !Object.defineProperty || !Object.defineProperties) alert("Example will fail because your browser does not support ECMAScript 5. Try with another browser!");
+var __filename = "" + window.location;
+
+window.Streamline = { globals: {} };
+
+function require(str) {
+ if (str == "streamline/lib/util/flows") return Streamline.flows;
+ else if (str == "streamline/lib/globals") return Streamline.globals;
+ else if (str == "streamline/lib/spoon/runtime") return Streamline.runtime;
+ else if (str == "streamline/lib/spoon/builtins") return Streamline.builtins;
+ else if (str == "streamline/lib/util/future") return Streamline.future;
+ else alert("cannot require " + str)
+}
View
6 lib/spoon/runtime.js
@@ -0,0 +1,6 @@
+// Copyright (c) 2011 Bruno Jouhier <bruno.jouhier@sage.com>
+"use strict";
+(function(exports) {
+ exports.future = require("streamline/lib/util/future").future;
+})(typeof exports !== 'undefined' ? exports : (Streamline.runtime = Streamline.runtime || {}));
+require("streamline/lib/spoon/builtins");
View
115 lib/spoon/transform.js
@@ -6,22 +6,131 @@ exports.version = "0.4.5 (spoon)";
// hack to fix #123
exports.transform.version = exports.version;
+var THIS = ["name", "this"],
+ ARGUMENTS = ["name", "arguments"],
+ __$CALLBACK = ["name", "__$callback"],
+ __$RT = ["name", "__$rt"];
+
+function deeper(node, fn) {
+ Object.keys(node).forEach(function(key) {
+ var n = node[key];
+ if (n && typeof n === 'object') fn(n);
+ });
+}
function transform(source, options) {
var spoon = require("spoon"),
esprima = require("esprima"),
uglify = require("uglify-js");
+ var async = true;
+
+ function isMarker(node) {
+ return node.type === 'Identifier' && node.name === '_';
+ }
+
+ function isCoffeeClosure(node) {
+ return node.type === 'FunctionExpression' //
+ && node.id === null //
+ && node.params.length === 0
+ }
+
+ function preProcess(node) {
+ if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
+ var oldAsync = async;
+ async = node.params.some(isMarker);
+ deeper(node, preProcess);
+ async = oldAsync;
+ return;
+ } else if (node.type === 'CallExpression') {
+ if (node.arguments.some(isMarker)) {
+ if (!async) throw new Error("async call in invalid context");
+ } else if (node.arguments.length === 0) {
+ // take care of CoffeeScript wrappers:
+ // (function() { ... })()
+ if (isCoffeeClosure(node.callee)) {
+ node.callee.params = [{
+ type: 'Identifier',
+ name: '_'
+ }];
+ node.arguments = [{
+ type: 'Identifier',
+ name: '_'
+ }];
+ return preProcess(node);
+ }
+ } else if (node.arguments.length === 1) {
+ // take care of (function() { ... }).call(this);
+ if (node.callee.type === 'MemberExpression' //
+ && isCoffeeClosure(node.callee.object) //
+ && node.callee.property.name === 'call' //
+ && node.arguments[0].type === 'ThisExpression') {
+ node.callee.object.params = [{
+ type: 'Identifier',
+ name: '_'
+ }];
+ node.arguments.push({
+ type: 'Identifier',
+ name: '_'
+ });
+ }
+ } else if (node.arguments.length === 2) {
+ // take care of (function() { ... }).apply(this, arguments);
+ if (node.callee.type === 'MemberExpression' //
+ && isCoffeeClosure(node.callee.object) //
+ && node.callee.property.name === 'apply' //
+ && node.arguments[0].type === 'ThisExpression' //
+ && node.arguments[1].name === 'arguments') {
+ throw new Error("NIY"); // needs more work
+ node.callee.object.params = [{
+ type: 'Identifier',
+ name: '_'
+ }];
+ node.arguments.push({
+ type: 'Identifier',
+ name: '_'
+ });
+ }
+ }
+ }
+ deeper(node, preProcess);
+ }
+
+ var anonId = 0;
+
+ function postProcess(node) {
+ if ((node[0] === 'defun' || node[0] === 'function') && (!node[1] || node[1].substring(0, 3) !== '__$')) {
+ var marker = node[2].indexOf('__$callback');
+ if (marker >= 0) {
+ if (!node[1]) node[1] = "__$anon" + ++anonId;
+ var name = node[1];
+ node[3].unshift(["if", ["unary-prefix", "!", __$CALLBACK], //
+ ["block", [
+ ["return", ["call", ["dot", ["dot", __$RT, "future"], "call"],
+ [THIS, ["name", name], ARGUMENTS, ["num", marker]]
+ ]]
+ ]]]);
+ }
+ }
+ deeper(node, postProcess);
+ }
+
var ast = esprima.parse(source);
+ preProcess(ast);
var cfg = spoon.construct(ast);
//console.log("CFG=" + cfg);
-
- cfg.asyncify([], { marker: '_' });
+ cfg.asyncify([], {
+ marker: '_'
+ });
var out = spoon.render(cfg);
+
+ postProcess(out);
+
var code = uglify.uglify.gen_code(out, {
beautify: true
});
- return "(function(__$callback) {\n" + code + "\n})(function(__$e) { if (__$e) console.log(__$e); });";
+ return "var __$rt = require('streamline/lib/spoon/runtime');\n" //
+ + "(function(__$callback) {\n" + code + "\n})(function(__$e) { if (__$e) console.log(__$e); });";
}
View
698 test/common/spoon/eval-test._js
@@ -0,0 +1,698 @@
+var module = QUnit.module;
+
+module("streamline evaluation");
+
+function evalTest(f, val) {
+ f(function(err, result) {
+ var str = err ? "ERR: " + err : result;
+ strictEqual(str, val);
+ start();
+ })
+}
+
+function delay(_, val) {
+ setTimeout(_, 0);
+ return val;
+}
+
+function delayFail(_, err) {
+ setTimeout(_, 0);
+ throw err;
+}
+
+function throwError(message) {
+ throw new Error(message);
+}
+
+asyncTest("eval return", 1, function(_) {
+ evalTest(function f(_) {
+ return delay(_, 5);
+ }, 5);
+})
+asyncTest("eval if true", 1, function(_) {
+ evalTest(function f(_) {
+ if (true) return delay(_, 3);
+ return 4;
+ }, 3);
+})
+asyncTest("eval if false", 1, function(_) {
+ evalTest(function f(_) {
+ if (false) return delay(_, 3);
+ return 4;
+ }, 4);
+})
+asyncTest("eval while", 1, function(_) {
+ evalTest(function f(_) {
+ var i = 1,
+ result = 1;
+ while (i < 5) {
+ result = delay(_, i * result);
+ i++;
+ }
+ return result;
+ }, 24);
+})
+/* fails with undefined __$ixxx var
+asyncTest("eval for", 1, function(_) {
+ evalTest(function f(_) {
+ var result = 1;
+ for (var i = 1; i < 5; i++) {
+ result = delay(_, i) * delay(_, result);
+ }
+ return result;
+ }, 24);
+})*/
+/* fails with reference to __$fnxxxx defined in inner scope
+asyncTest("eval for in", 1, function(_) {
+ evalTest(function f(_) {
+ var foo = {
+ a: 1,
+ b: 2,
+ c: 3,
+ d: 5
+ }
+ var result = 1;
+ for (var k in foo) {
+ result = delay(_, foo[delay(_, k)]) * delay(_, result);
+ }
+ return result;
+ }, 30);
+})
+asyncTest("fully async for in", 1, function(_) {
+ evalTest(function f(_) {
+ var result = 1;
+ for (var i = delay(_, 2); i < delay(_, 5); i = delay(_, i) + 1) {
+ result = delay(_, result) * delay(_, i)
+ }
+ return result;
+ }, 24);
+})
+asyncTest("break in loop", 1, function(_) {
+ evalTest(function f(_) {
+ var result = 1;
+ for (var i = 1; i < 10; i++) {
+ if (i == 5) break;
+ result = delay(_, result) * delay(_, i)
+ }
+ return result;
+ }, 24);
+})
+asyncTest("continue", 1, function(_) {
+ evalTest(function f(_) {
+ var result = 1;
+ for (var i = 1; i < 10; i++) {
+ if (i >= 5) continue;
+ result = delay(_, result) * delay(_, i)
+ }
+ return result;
+ }, 24);
+})
+asyncTest("break in while", 1, function(_) {
+ evalTest(function f(_) {
+ var i = 1,
+ result = 1;
+ while (i < 10) {
+ if (i == 5) break;
+ result = delay(_, result) * delay(_, i);
+ i++;
+ }
+ return result;
+ }, 24);
+})
+asyncTest("continue in while", 1, function(_) {
+ evalTest(function f(_) {
+ var i = 1,
+ result = 1;
+ while (i < 10) {
+ i++;
+ if (i >= 5) continue;
+ result = delay(_, result) * delay(_, i);
+ }
+ return result;
+ }, 24);
+})
+
+asyncTest("for (;;)", 1, function(_) {
+ evalTest(function f(_) {
+ var i = 0;
+ for (;;) {
+ if (delay(_, ++i) === 10) return i;
+ }
+ }, 10);
+})
+
+asyncTest("eval lazy", 1, function(_) {
+ evalTest(function f(_) {
+ var result = 1;
+ return delay(_, delay(_, result + 8) < 5) && true ? 2 : 4
+ }, 4);
+})
+asyncTest("eval lazy full async", 1, function(_) {
+ evalTest(function f(_) {
+ var result = 1;
+ return delay(_, delay(_, result + 8) < 5) && true ? delay(_, 2) : delay(_, 4)
+ }, 4);
+})
+*/
+asyncTest("try catch 1", 1, function(_) {
+ evalTest(function f(_) {
+ try {
+ return delay(_, "ok");
+ } catch (ex) {
+ return delay(_, "err");
+ }
+ }, "ok");
+})
+/*
+asyncTest("try catch 2", 1, function(_) {
+ evalTest(function f(_) {
+ try {
+ throw delay(_, "thrown");
+ } catch (ex) {
+ return delay(_, "caught ") + ex;
+ }
+ }, "caught thrown");
+})
+asyncTest("try catch 3", 1, function(_) {
+ evalTest(function f(_) {
+ try {
+ throw delay(_, "thrown");
+ } catch (ex) {
+ return delay(_, "caught ") + ex;
+ }
+ }, "caught thrown");
+})
+asyncTest("try catch 5", 1, function(_) {
+ evalTest(function f(_) {
+ try {
+ delayFail(_, "delay fail");
+ } catch (ex) {
+ return delay(_, "caught ") + ex;
+ }
+ }, "caught delay fail");
+})
+asyncTest("try catch 6", 1, function(_) {
+ evalTest(function f(_) {
+ try {
+ throwError("direct")
+ return delay(_, "ok")
+ } catch (ex) {
+ return delay(_, "caught ") + ex.message;
+ }
+ }, "caught direct");
+})
+asyncTest("try catch 7", 1, function(_) {
+ evalTest(function f(_) {
+ try {
+ var message = delay(_, "indirect");
+ throwError(message)
+ return delay(_, "ok")
+ } catch (ex) {
+ return delay(_, "caught ") + ex.message;
+ }
+ }, "caught indirect");
+})
+asyncTest("try finally 1", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ x += delay(_, "try")
+ } finally {
+ x += delay(_, " finally");
+ }
+ x += " end"
+ return x;
+ }, "try finally end");
+})
+asyncTest("try finally 2", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ x += delay(_, "try")
+ return x;
+ } finally {
+ x += delay(_, " finally");
+ }
+ x += " end"
+ return x;
+ }, "try");
+})
+asyncTest("try finally 3", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ x += delay(_, "try")
+ throw "bad try";
+ } finally {
+ x += delay(_, " finally");
+ }
+ x += " end"
+ return x;
+ }, "ERR: bad try");
+})
+asyncTest("try finally 4", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ x += delay(_, "try")
+ throwError("except");
+ } finally {
+ x += delay(_, " finally");
+ }
+ x += " end"
+ return x;
+ }, "ERR: Error: except");
+})
+asyncTest("try finally 5", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ try {
+ x += delay(_, "try")
+ throwError("except");
+ x += " unreached"
+ } finally {
+ x += delay(_, " finally");
+ }
+ x += " end"
+ return x;
+ } catch (ex) {
+ return x + "/" + ex.message;
+ }
+ }, "try finally/except");
+})
+asyncTest("try catch finally 1", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ try {
+ x += delay(_, "try")
+ throw new Error("except");
+ x += " unreached"
+ } catch (ex) {
+ x += delay(_, " catch " + ex.message);
+ throw ex;
+ } finally {
+ x += delay(_, " finally");
+ }
+ x += " end"
+ return x;
+ } catch (ex) {
+ return x + "/" + ex.message;
+ }
+ }, "try catch except finally/except");
+})
+asyncTest("try catch finally 2", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ try {
+ x += delay(_, "try")
+ throwError("except");
+ x += " unreached"
+ } catch (ex) {
+ x += " catch " + ex.message;
+ throw ex;
+ } finally {
+ x += " finally";
+ }
+ x += " end"
+ return x;
+ } catch (ex) {
+ return x + "/" + ex.message;
+ }
+ }, "try catch except finally/except");
+})
+
+asyncTest("nested try/catch 1", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ try {
+ x += delay(_, "try")
+ } catch (ex) {
+ x += delay(_, " inner catch " + ex.message);
+ }
+ throwError(" except");
+ } catch (ex) {
+ return x + " outer catch" + ex.message;
+ }
+ }, "try outer catch except");
+})
+asyncTest("nested try/catch 2", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ try {
+ x += delay(_, "try")
+ } catch (ex) {
+ x += " inner catch " + ex.message;
+ }
+ throw new Error(" except");
+ } catch (ex) {
+ return x + " outer catch" + ex.message;
+ }
+ }, "try outer catch except");
+})
+asyncTest("nested try/catch 3", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ try {
+ x += delay(_, "try")
+ } catch (ex) {
+ x += delay(_, " inner catch " + ex.message);
+ }
+ throw new Error(" except");
+ } catch (ex) {
+ return x + " outer catch" + ex.message;
+ }
+ }, "try outer catch except");
+})
+
+asyncTest("nested try/finally 1", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ try {
+ x += delay(_, "try")
+ } finally {
+ x += delay(_, " inner finally");
+ }
+ throwError(" except");
+ } catch (ex) {
+ return x + " outer catch" + ex.message;
+ }
+ }, "try inner finally outer catch except");
+})
+asyncTest("nested try/finally 2", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ try {
+ x += delay(_, "try")
+ } finally {
+ x += " inner finally";
+ }
+ throwError(" except");
+ } catch (ex) {
+ return x + " outer catch" + ex.message;
+ }
+ }, "try inner finally outer catch except");
+})
+asyncTest("nested try/finally 3", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "";
+ try {
+ try {
+ x += delay(_, "try")
+ } finally {
+ x += delay(_, " inner finally");
+ }
+ throw new Error(" except");
+ } catch (ex) {
+ return x + " outer catch" + ex.message;
+ }
+ }, "try inner finally outer catch except");
+})
+*/
+asyncTest("and ok", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "<<";
+ if (delay(_, true) && delay(_, true)) x += "T1";
+ else x += "F1"
+ if (delay(_, true) && delay(_, false)) x += "T2";
+ else x += "F2"
+ if (delay(_, false) && delay(_, true)) x += "T3";
+ else x += "F3"
+ if (delay(_, false) && delay(_, false)) x += "T4";
+ else x += "F4"
+ if (delay(_, false) && delayFail(_, "bad")) x += "T5";
+ else x += "F5"
+ x += ">>";
+ return x;
+ }, "<<T1F2F3F4F5>>");
+})
+asyncTest("or ok", 1, function(_) {
+ evalTest(function f(_) {
+ var x = "<<";
+ if (delay(_, true) || delay(_, true)) x += "T1";
+ else x += "F1"
+ if (delay(_, true) || delay(_, false)) x += "T2";
+ else x += "F2"
+ if (delay(_, false) || delay(_, true)) x += "T3";
+ else x += "F3"
+ if (delay(_, false) || delay(_, false)) x += "T4";
+ else x += "F4"
+ if (delay(_, true) || delayFail(_, "bad")) x += "T5";
+ else x += "F5"
+ x += ">>";
+ return x;
+ }, "<<T1T2T3F4T5>>");
+})
+/*
+asyncTest("switch with default", 1, function(_) {
+ evalTest(function f(_) {
+ function g(_, i) {
+ var result = "a"
+ switch (delay(_, i)) {
+ case 1:
+ result = delay(_, "b");
+ break;
+ case 2:
+ return delay(_, "c");
+ case 3:
+ case 4:
+ result = delay(_, "d");
+ break;
+ default:
+ result = delay(_, "e");
+ }
+ return result;
+ }
+
+ return g(_, 0) + g(_, 1) + g(_, 2) + g(_, 3) + g(_, 4) + g(_, 5);
+ }, "ebcdde");
+})
+asyncTest("switch without default", 1, function(_) {
+ evalTest(function f(_) {
+ function g(_, i) {
+ var result = "a"
+ switch (delay(_, i)) {
+ case 1:
+ result = "b";
+ break;
+ case 2:
+ return "c";
+ case 3:
+ case 4:
+ result = "d";
+ break;
+ }
+ return result;
+ }
+
+ return g(_, 0) + g(_, 1) + g(_, 2) + g(_, 3) + g(_, 4) + g(_, 5);
+ }, "abcdda");
+})
+
+asyncTest("this", 5, function(_) {
+ evalTest(function f(_) {
+ function O(x) {
+ this.x = x;
+ }
+
+ O.prototype.test1 = function(_) {
+ var self = this;
+ this.x = delay(_, this.x + 1);
+ strictEqual(this, self);
+ }
+ O.prototype.test2 = function(_) {
+ var self = this;
+ try {
+ this.x = delay(_, this.x + 1);
+ strictEqual(this, self);
+ } catch (ex) {
+ ok(false);
+ }
+ }
+ O.prototype.test3 = function(_) {
+ var self = this;
+ try {
+ this.x = delay(_, this.x + 1);
+ throwError("test3");
+ ok(false);
+ } catch (ex) {
+ strictEqual(this, self);
+ this.x = delay(_, this.x + 1);
+ }
+ }
+
+ function delay2(val, _) {
+ return delay(_, val);
+ }
+
+ O.prototype.test4 = function(_) {
+ var self = this;
+ var v1 = delay2(this.x + 1);
+ var v2 = delay2(1);
+ this.x = v1(_) + v2(_);
+ strictEqual(this, self);
+ }
+ var o = new O(1);
+ o.test1(_);
+ o.test2(_);
+ o.test3(_);
+ o.test4(_);
+ return o.x;
+ }, 7);
+})
+*/
+asyncTest("scoping", 1, function(_) {
+ evalTest(function f(_) {
+ function test(_) {
+ var foo = "abc";
+
+ function bar() {
+ return foo;
+ }
+
+ delay(_);
+ var foo = "xyz";
+ return bar;
+ }
+
+ return test(_)();
+ }, "xyz");
+})
+asyncTest("return undefined", 1, function(_) {
+ evalTest(function f(_) {
+ function test(_) {
+ delay(_);
+ return;
+ }
+
+ return test(_);
+ }, undefined);
+})
+/*
+asyncTest("futures test", 1, function(_) {
+ evalTest(function f(_) {
+ function delay2(val, _) {
+ return delay(_, val);
+ }
+
+ var a = delay2('a');
+ var b = delay2('b');
+ var c = delay2('c');
+ var d = delay2('d');
+ return a(_) + b(_) + d(_) + c(_);
+ }, "abdc");
+})
+
+asyncTest("last case without break", 1, function(_) {
+ evalTest(function f(_) {
+ switch (true) {
+ case true:
+ delay(_);
+ }
+ return 1;
+ }, 1);
+})
+
+asyncTest("async comma operator", 1, function(_) {
+ evalTest(function f(_) {
+ var a;
+ return a = 4, a++, a = delay(_, 2 * a), delay(_, a + 1);
+ }, 11);
+})
+
+// NEXT TEST FAILS BECAUSE OF STREAMLINE
+asyncTest("async constructor", 1, function(_) {
+ evalTest(function f(_) {
+ function Foo(val, _) {
+ delay(_);
+ this.x = val;
+ }
+ Foo.prototype.y = function() {
+ return this.x + 1;
+ }
+ return new Foo(5, _).y();
+ }, 6);
+})
+
+asyncTest("fibo false async", 1, function(_) {
+ evalTest(function f(_) {
+ function fibo(_, n) {
+ return n > 1 ? fibo(_, n - 1) + fibo(_, n - 2) : 1;
+ }
+ return fibo(_, 16);
+ }, 1597);
+})
+*/
+
+asyncTest("coffeescript wrapper 1", 1, function(_) {
+ evalTest(function f(_) {
+ return (function() {
+ return delay(_, "cs1");
+ })();
+ }, "cs1");
+})
+
+asyncTest("coffeescript wrapper 2", 1, function(_) {
+ evalTest(function f(_) {
+ return (function() {
+ return delay(_, "cs2");
+ }).call(this);
+ }, "cs2");
+})
+
+/* THIS ONE NEEDS MORE WORK
+asyncTest("coffeescript wrapper 3", 1, function(_) {
+ evalTest(function f(_) {
+ return (function() {
+ return delay(_, "cs3");
+ }).apply(this, arguments);
+ }, "cs3");
+})
+*/
+
+asyncTest("sync try/catch in async", 1, function(_) {
+ evalTest(function f(_) {
+ try {
+ throw new Error("catch me");
+ } catch (ex) {
+ return "got it";
+ }
+ }, "got it");
+})
+
+asyncTest("sync try/catch inside conditional", 1, function(_) {
+ evalTest(function f(_) {
+ if (true) {
+ try {} catch (ex) {}
+ }
+ }, undefined);
+})
+/*
+asyncTest("labelled break", 1, function(_) {
+ evalTest(function f(_) {
+ var result = '';
+ outer:
+ for (var i = 1; i < 10; i++) {
+ inner:
+ for (var j = 5; j < 10; j++) {
+ result = delay(_, result) + '!'
+ if (i == 1 && j == 7) break;
+ if (i == 2 && j == 7) break inner;
+ if (i == 3 && j == 7) continue inner;
+ if (i == 4 && j == 7) continue outer;
+ if (i == 5 && j == 7) break outer;
+ result = delay(_, result) + delay(_, i) + delay(_, j) + '-';
+ }
+ result += delay(_, '/')
+ }
+ return result;
+ }, '!15-!16-!/!25-!26-!/!35-!36-!!38-!39-/!45-!46-!!55-!56-!');
+})
+*/
View
21 test/common/spoon/eval-test.html
@@ -0,0 +1,21 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+ "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <script src="../../qunit/jquery.js"></script>
+ <link rel="stylesheet" href="../../qunit/qunit.css" type="text/css" media="screen" />
+ <script type="text/javascript" src="../../qunit/qunit.js"></script>
+ <script src="../../../lib/spoon/require-stub.js" type="application/javascript"></script>
+ <script src="../../../lib/util/future.js" type="application/javascript"></script>
+ <script src="../../../lib/spoon/runtime.js" type="application/javascript"></script>
+ <script src="../../../lib/spoon/builtins.js" type="application/javascript"></script>
+ <script src="eval-test.js" type="application/javascript" ></script>
+</head>
+<body>
+ <h1 id="qunit-header">streamline evaluation tests (spoon)</h1>
+ <h2 id="qunit-banner"></h2>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+</body>
+</html>
+
View
680 test/common/spoon/eval-test.js
@@ -0,0 +1,680 @@
+/*** Generated by streamline 0.4.5 (spoon) - DO NOT EDIT ***/var __$rt = require('streamline/lib/spoon/runtime');
+(function(__$callback) {
+var module;
+
+module = QUnit.module;
+
+module("streamline evaluation");
+
+function evalTest(f, val) {
+ f(function(err, result) {
+ var __$i159;
+ var str;
+ if (err) {
+ __$i159 = "ERR: " + err;
+ } else {
+ __$i159 = result;
+ };
+ str = __$i159;
+ strictEqual(str, val);
+ start();
+ });
+};
+
+function delay(__$callback, val) {
+ if (!__$callback) {
+ return __$rt.future.call(this, delay, arguments, 0);
+ }
+ function __$fn148(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, val);
+ };
+ return setTimeout(__$fn148, 0);
+};
+
+function delayFail(__$callback, err) {
+ if (!__$callback) {
+ return __$rt.future.call(this, delayFail, arguments, 0);
+ }
+ function __$fn149(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ throw err;
+ };
+ return setTimeout(__$fn149, 0);
+};
+
+function throwError(message) {
+ throw new Error(message);
+};
+
+asyncTest("eval return", 1, function __$anon1(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, __$anon1, arguments, 0);
+ }
+ evalTest(function f(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, f, arguments, 0);
+ }
+ function __$fn150(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, __$r);
+ };
+ return delay(__$fn150, 5);
+ }, 5);
+ return __$callback.call(this, null);
+});
+
+asyncTest("eval if true", 1, function __$anon2(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, __$anon2, arguments, 0);
+ }
+ evalTest(function f(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, f, arguments, 0);
+ }
+ function __$fn151(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, __$r);
+ };
+ if (true) {
+ return delay(__$fn151, 3);
+ } else {
+ return __$callback.call(this, null, 4);
+ };
+ }, 3);
+ return __$callback.call(this, null);
+});
+
+asyncTest("eval if false", 1, function __$anon3(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, __$anon3, arguments, 0);
+ }
+ evalTest(function f(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, f, arguments, 0);
+ }
+ function __$fn152(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, __$r);
+ };
+ if (false) {
+ return delay(__$fn152, 3);
+ } else {
+ return __$callback.call(this, null, 4);
+ };
+ }, 4);
+ return __$callback.call(this, null);
+});
+
+asyncTest("eval while", 1, function __$anon4(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, __$anon4, arguments, 0);
+ }
+ evalTest(function f(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, f, arguments, 0);
+ }
+ function __$fn154(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ while (true) {
+ if (!(i < 5)) {
+ break;
+ } else {
+ return delay(__$fn153, i * result);
+ };
+ };
+ return __$callback.call(this, null, result);
+ };
+ function __$fn153(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ result = __$r;
+ i++;
+ return __$fn154.call(this);
+ };
+ var i, result;
+ i = 1;
+ result = 1;
+ return __$fn154.call(this);
+ }, 24);
+ return __$callback.call(this, null);
+});
+
+asyncTest("try catch 1", 1, function __$anon5(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, __$anon5, arguments, 0);
+ }
+ evalTest(function f(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, f, arguments, 0);
+ }
+ function __$fn156(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, __$r);
+ };
+ function __$fn155(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ return __$callback.call(this, null, __$r);
+ };
+ try {
+ return delay(__$fn155, "ok");
+ } catch (ex) {
+ return delay(__$fn156, "err");
+ };
+ }, "ok");
+ return __$callback.call(this, null);
+});
+
+asyncTest("and ok", 1, function __$anon6(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, __$anon6, arguments, 0);
+ }
+ evalTest(function f(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, f, arguments, 0);
+ }
+ var __$i341;
+ var __$i318;
+ var __$i295;
+ var __$i272;
+ var __$i249;
+ function __$fn157(__$e, __$r) {
+ function __$fn159(__$e, __$r) {
+ function __$fn160(__$e, __$r) {
+ function __$fn162(__$e, __$r) {
+ function __$fn163(__$e, __$r) {
+ function __$fn165(__$e, __$r) {
+ function __$fn166(__$e, __$r) {
+ function __$fn168(__$e, __$r) {
+ function __$fn169(__$e, __$r) {
+ function __$fn171(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i341) {
+ x += "T5";
+ } else {
+ x += "F5";
+ };
+ x += ">>";
+ return __$callback.call(this, null, x);
+ };
+ function __$fn170(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i341 = __$r;
+ return __$fn171.call(this);
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$r) {
+ return delayFail(__$fn170, "bad");
+ } else {
+ __$i341 = __$r;
+ return __$fn171.call(this);
+ };
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i318) {
+ x += "T4";
+ } else {
+ x += "F4";
+ };
+ return delay(__$fn169, false);
+ };
+ function __$fn167(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i318 = __$r;
+ return __$fn168.call(this);
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$r) {
+ return delay(__$fn167, false);
+ } else {
+ __$i318 = __$r;
+ return __$fn168.call(this);
+ };
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i295) {
+ x += "T3";
+ } else {
+ x += "F3";
+ };
+ return delay(__$fn166, false);
+ };
+ function __$fn164(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i295 = __$r;
+ return __$fn165.call(this);
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$r) {
+ return delay(__$fn164, true);
+ } else {
+ __$i295 = __$r;
+ return __$fn165.call(this);
+ };
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i272) {
+ x += "T2";
+ } else {
+ x += "F2";
+ };
+ return delay(__$fn163, false);
+ };
+ function __$fn161(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i272 = __$r;
+ return __$fn162.call(this);
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$r) {
+ return delay(__$fn161, false);
+ } else {
+ __$i272 = __$r;
+ return __$fn162.call(this);
+ };
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i249) {
+ x += "T1";
+ } else {
+ x += "F1";
+ };
+ return delay(__$fn160, true);
+ };
+ function __$fn158(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i249 = __$r;
+ return __$fn159.call(this);
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$r) {
+ return delay(__$fn158, true);
+ } else {
+ __$i249 = __$r;
+ return __$fn159.call(this);
+ };
+ };
+ var x;
+ x = "<<";
+ return delay(__$fn157, true);
+ }, "<<T1F2F3F4F5>>");
+ return __$callback.call(this, null);
+});
+
+asyncTest("or ok", 1, function __$anon7(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, __$anon7, arguments, 0);
+ }
+ evalTest(function f(__$callback) {
+ if (!__$callback) {
+ return __$rt.future.call(this, f, arguments, 0);
+ }
+ var __$i463;
+ var __$i440;
+ var __$i417;
+ var __$i394;
+ var __$i371;
+ function __$fn172(__$e, __$r) {
+ function __$fn174(__$e, __$r) {
+ function __$fn175(__$e, __$r) {
+ function __$fn177(__$e, __$r) {
+ function __$fn178(__$e, __$r) {
+ function __$fn180(__$e, __$r) {
+ function __$fn181(__$e, __$r) {
+ function __$fn183(__$e, __$r) {
+ function __$fn184(__$e, __$r) {
+ function __$fn186(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$i463) {
+ x += "T5";
+ } else {
+ x += "F5";
+ };
+ x += ">>";
+ return __$callback.call(this, null, x);
+ };
+ function __$fn185(__$e, __$r) {
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ __$i463 = __$r;
+ return __$fn186.call(this);
+ };
+ if (__$e) {
+ return __$callback.call(this, __$e);
+ };
+ if (__$r) {
+ __$i463 = __$r;
+ return __$fn186.call(this);
+ } else {
+ return delayFail(__$fn185, "bad");
+ };
+ };
+ if (__$e) {
+ return __$ca