ServerEval = {
	results: function() {
		return ServerEval._collection.find({}, {
			sort: {
				eval_time: -1
			}
		});
	},
	eval: function(expr) {
		Meteor.call('serverEval/eval', expr);
	},
	clear: function() {
		Meteor.call('serverEval/clear');
	}
};

if (Meteor.isClient) {
	ServerEval._collection = new Meteor.Collection("eval-results");

	Meteor.subscribe("eval-results");
}

if (Meteor.isServer) {
	ServerEval._collection = new Meteor.Collection("eval-results", {
		connection: null
	});

	Meteor.publish("eval-results", function() {
		return ServerEval.results();
	});

	var prettyJSON = function(obj) {
		var cache = [];
		var json = JSON.stringify(obj, function(key, value) {
			if (_.isFunction(value)) {
				value = "[Function]";
			} else if (_.isObject(value)) {
				if (cache.indexOf(value) !== -1) {
					// Circular reference found, discard key
					return;
				}
				// Store value in our collection
				cache.push(value);
			}
			return value;
		});
		cache = null; // Enable garbage collection TODO investigate
		return json;
	};

	Meteor.methods({
		'serverEval/eval': function(expr) {
			if (!expr || expr.length === 0) return;
			var eval_time = Date.now();
			try {
				var result_raw = eval(expr);
				ServerEval._collection.insert({
					eval_time: eval_time,
					expr: expr,
					type: typeof result_raw,
					result: JSON.parse(prettyJSON(result_raw))
				});
			} catch (e) {
				ServerEval._collection.insert({
					eval_time: eval_time,
					expr: expr,
					error: true,
					result: e.toString()
				});
			}
		},
		'serverEval/clear': function() {
			ServerEval._collection.remove({});
		}
	});
}
@@ -687,71 +687,28 @@
 %0A%09%09%09
-if (_.isFunction(value)) %7B%0A%09%09%09%09value = %22%5BFunction%5D%22;%0A%09%09%09%7D else 
+var val = value;%0A%09%09%09
 if (
@@ -832,16 +832,123 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%09if (_.isFunction(value)) %7B%0A%09%09%09%09%09val = _.extend(%7B%7D, value);%0A%09%09%09%09%09val.____TYPE____ = %22%5BFunction%5D%22;%0A%09%09%09%09%7D%0A
 %09%09%09%09// S
@@ -1013,26 +1013,24 @@
 %09%09return val
-ue
 ;%0A%09%09%7D);%0A%09%09ca
@@ -1246,16 +1246,16 @@
 %09%09try %7B%0A
-
 %09%09%09%09var 
@@ -1279,16 +1279,47 @@
 (expr);%0A
+%09%09%09%09//console.log(result_raw);%0A
 %09%09%09%09Serv

add Function object properties to tree