js-marketplace-2014/marketplace.js

410 lines
10 KiB
JavaScript
Raw Normal View History

2013-08-12 02:23:24 +00:00
/*---------------------------------------------------------------------------*/
/* Unification */
var __ = new Object(); /* wildcard marker */
__.__ = "__";
function unificationFailed() {
throw {unificationFailed: true};
}
function unify1(a, b) {
var i;
if (a === __) return b;
if (b === __) return a;
if (a === b) return a;
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) unificationFailed();
2013-08-13 17:44:39 +00:00
var result = new Array(a.length);
2013-08-12 02:23:24 +00:00
for (i = 0; i < a.length; i++) {
2013-08-13 17:44:39 +00:00
result[i] = unify1(a[i], b[i]);
2013-08-12 02:23:24 +00:00
}
return result;
}
if (typeof a === "object" && typeof b === "object") {
/* TODO: consider other kinds of matching. I've chosen to
require any field mentioned by either side to be present in
both. Does that make sense? */
var result = ({});
2013-08-13 17:44:24 +00:00
for (i in a) { if (a.hasOwnProperty(i)) result[i] = true; }
for (i in b) { if (b.hasOwnProperty(i)) result[i] = true; }
2013-08-12 02:23:24 +00:00
for (i in result) {
if (result.hasOwnProperty(i)) {
result[i] = unify1(a[i], b[i]);
}
}
return result;
}
unificationFailed();
}
function unify(a, b) {
try {
return {result: unify1(a, b)};
} catch (e) {
if (e.unificationFailed) return undefined;
throw e;
}
}
function anyUnify(aa, bb) {
for (var i = 0; i < aa.length; i++) {
for (var j = 0; j < bb.length; j++) {
if (unify(aa[i], bb[j])) return true;
}
}
return false;
}
/*---------------------------------------------------------------------------*/
2013-09-27 11:14:27 +00:00
/* Events and Actions */
2013-08-12 02:23:24 +00:00
2013-09-27 11:14:27 +00:00
function Route(isSubscription, pattern, metaLevel, level) {
2013-08-12 12:51:45 +00:00
this.isSubscription = isSubscription;
2013-08-12 02:23:24 +00:00
this.pattern = pattern;
2013-09-27 11:14:27 +00:00
this.metaLevel = (metaLevel === undefined) ? 0 : metaLevel;
2013-08-12 12:51:45 +00:00
this.level = (level === undefined) ? 0 : level;
2013-08-12 02:23:24 +00:00
}
2013-09-27 11:14:27 +00:00
Route.prototype.drop = function () {
if (this.metaLevel === 0) { return null; }
return new Route(this.isSubscription, this.pattern, this.metaLevel - 1, this.level);
};
Route.prototype.lift = function () {
return new Route(this.isSubscription, this.pattern, this.metaLevel + 1, this.level);
};
function sub(pattern, metaLevel, level) {
return new Route(true, pattern, metaLevel, level);
2013-08-12 02:23:24 +00:00
}
2013-09-27 11:14:27 +00:00
function pub(pattern, metaLevel, level) {
return new Route(false, pattern, metaLevel, level);
2013-08-12 02:23:24 +00:00
}
2013-09-27 11:14:27 +00:00
function spawn(behavior, initialRoutes) {
return { type: "spawn",
behavior: behavior,
initialRoutes: (initialRoutes === undefined) ? [] : initialRoutes };
2013-08-12 02:23:24 +00:00
}
function updateRoutes(routes) {
return { type: "routes", routes: routes };
}
2013-09-27 11:14:27 +00:00
function sendMessage(m, metaLevel, isFeedback) {
return { type: "send",
metaLevel: (metaLevel === undefined) ? 0 : metaLevel,
message: m,
isFeedback: (isFeedback === undefined) ? false : isFeedback };
}
/*---------------------------------------------------------------------------*/
/* Metafunctions */
function dropRoutes(routes) {
var result = [];
for (var i = 0; i < routes.length; i++) {
var r = routes[i].drop();
if (r) { result.push(r); }
}
return result;
2013-08-12 02:23:24 +00:00
}
2013-09-27 11:14:27 +00:00
function liftRoutes(routes) {
var result = [];
for (var i = 0; i < routes.length; i++) {
result.push(routes[i].lift());
}
return result;
2013-08-12 02:23:24 +00:00
}
2013-09-27 11:14:27 +00:00
function filterEvent(e, routes) {
switch (e.type) {
case "routes":
var result = [];
for (var i = 0; i < e.routes.length; i++) {
for (var j = 0; j < routes.length; j++) {
var ri = e.routes[i];
var rj = routes[j];
if (ri.isSubscription === !rj.isSubscription
&& ri.metaLevel === rj.metaLevel
&& ri.level < rj.level)
{
var u = unify(ri.pattern, rj.pattern);
if (u) {
var rk = new Route(ri.isSubscription, u.result, ri.metaLevel, ri.level);
result.push(rk);
}
}
}
}
return result.length ? result : null;
case "send":
for (var i = 0; i < routes.length; i++) {
var r = routes[i];
if (e.metaLevel === r.metaLevel
&& e.isFeedback === !r.isSubscription
&& unify(e.message, r.pattern))
{
return e;
}
}
return null;
default:
throw { message: "Event type " + e.type + " not filterable",
event: e };
}
}
/*---------------------------------------------------------------------------*/
/* Configurations */
2013-10-17 14:50:51 +00:00
function World(bootFn) {
2013-08-12 02:23:24 +00:00
this.nextPid = 0;
this.eventQueue = [];
this.processTable = {};
this.downwardRoutes = [];
2013-09-27 11:14:27 +00:00
this.processActions = [];
this.activePid = null;
2013-10-17 14:50:51 +00:00
this.stepperId = null;
this.asChild(-1, bootFn);
2013-08-12 02:23:24 +00:00
}
2013-09-27 11:14:27 +00:00
/* Class state / methods */
World.current = null; // parameter
World.send = function (m, metaLevel, isFeedback) {
World.current.enqueueAction(sendMessage(m, metaLevel, isFeedback));
};
World.updateRoutes = function (routes) {
World.current.enqueueAction(updateRoutes(routes));
};
World.spawn = function (behavior, initialRoutes) {
World.current.enqueueAction(spawn(behavior, initialRoutes));
};
2013-10-17 14:50:51 +00:00
World.wrap = function (f) {
return World.current.wrap(f);
};
2013-09-27 11:14:27 +00:00
/* Instance methods */
World.prototype.enqueueAction = function (action) {
this.processActions.push([this.activePid, action]);
};
2013-08-12 02:23:24 +00:00
World.prototype.enqueueActions = function (pid, actions) {
for (var i = 0; i < actions.length; i++) {
2013-09-27 11:14:27 +00:00
this.processActions.push([pid, actions[i]]);
}
};
World.prototype.isQuiescent = function () {
return this.eventQueue.length === 0 && this.processActions.length === 0;
};
World.prototype.step = function () {
this.dispatchEvents();
this.performActions();
return this.stepChildren() || !this.isQuiescent();
};
2013-10-17 14:50:51 +00:00
World.prototype.startStepping = function () {
var self = this;
if (this.stepperId) return;
if (this.step()) {
this.stepperId = setTimeout(function () {
self.stepperId = null;
self.startStepping();
}, 0);
}
};
World.prototype.stopStepping = function () {
if (this.stepperId) {
clearTimeout(this.stepperId);
this.stepperId = null;
}
};
2013-09-27 11:14:27 +00:00
World.prototype.asChild = function (pid, f) {
var oldWorld = World.current;
var result = null;
World.current = this;
this.activePid = pid;
try {
result = f();
} catch (e) {
this.kill(pid, e);
2013-08-12 02:23:24 +00:00
}
2013-09-27 11:14:27 +00:00
this.activePid = null;
World.current = oldWorld;
return result;
2013-08-12 02:23:24 +00:00
};
2013-10-17 14:50:51 +00:00
World.prototype.wrap = function (f) {
var savedWorld = this;
var savedPid = this.activePid;
return function () {
var actuals = arguments;
return savedWorld.asChild(savedPid, function () { return f.apply(null, actuals) });
};
};
2013-09-27 11:14:27 +00:00
World.prototype.kill = function (pid, exn) {
console.log("Killed process " + pid + (exn ? " with reason " + exn.message : ""));
delete this.processTable[pid];
this.issueRoutingUpdate();
};
World.prototype.stepChildren = function () {
var someChildBusy = false;
for (var pid in this.processTable) {
var p = this.processTable[pid];
if (p.behavior.step /* exists, haven't called it yet */) {
2013-10-17 14:50:51 +00:00
var childBusy = this.asChild(pid, function () { return p.behavior.step() });
2013-09-27 11:14:27 +00:00
someChildBusy = someChildBusy || childBusy;
}
}
return someChildBusy;
2013-08-12 02:23:24 +00:00
};
2013-09-27 11:14:27 +00:00
World.prototype.performActions = function () {
var queue = this.processActions;
this.processActions = [];
2013-08-12 02:23:24 +00:00
var item;
while ((item = queue.shift())) {
this.performAction(item[0], item[1]);
}
};
2013-09-27 11:14:27 +00:00
World.prototype.dispatchEvents = function () {
var queue = this.eventQueue;
this.eventQueue = [];
var item;
while ((item = queue.shift())) {
this.dispatchEvent(item);
}
};
2013-08-12 02:23:24 +00:00
World.prototype.performAction = function (pid, action) {
switch (action.type) {
case "spawn":
2013-09-27 11:14:27 +00:00
var pid = this.nextPid++;
this.processTable[pid] = { routes: action.initialRoutes, behavior: action.behavior };
2013-09-27 14:36:51 +00:00
if (action.behavior.boot) { this.asChild(pid, function () { action.behavior.boot() }); }
2013-09-27 11:14:27 +00:00
this.issueRoutingUpdate();
2013-08-12 02:23:24 +00:00
break;
case "routes":
2013-09-27 11:14:27 +00:00
this.processTable[pid].routes = action.routes;
this.issueRoutingUpdate();
2013-08-12 02:23:24 +00:00
break;
case "send":
2013-09-27 11:14:27 +00:00
if (action.metaLevel === 0) {
this.eventQueue.push(action);
} else {
World.send(action.message, action.metaLevel - 1, action.isFeedback);
}
2013-08-12 02:23:24 +00:00
break;
default:
throw { message: "Action type " + action.type + " not understood",
action: action };
}
};
2013-09-27 11:14:27 +00:00
World.prototype.aggregateRoutes = function (base) {
var acc = base.slice();
for (var pid in this.processTable) {
var p = this.processTable[pid];
for (var i = 0; i < p.routes.length; i++) {
acc.push(p.routes[i]);
}
}
2013-10-17 14:50:51 +00:00
return acc;
2013-08-12 02:23:24 +00:00
};
2013-09-27 11:14:27 +00:00
World.prototype.issueRoutingUpdate = function () {
this.eventQueue.push(updateRoutes(this.aggregateRoutes(this.downwardRoutes)));
World.updateRoutes(dropRoutes(this.aggregateRoutes([])));
2013-08-12 02:23:24 +00:00
};
World.prototype.dispatchEvent = function (e) {
2013-09-27 11:14:27 +00:00
for (var pid in this.processTable) {
var p = this.processTable[pid];
var e1 = filterEvent(e, p.routes);
if (e1) { this.asChild(pid, function () { p.behavior.handleEvent(e1) }); }
}
};
World.prototype.handleEvent = function (e) {
2013-08-12 02:23:24 +00:00
switch (e.type) {
case "routes":
2013-09-27 11:14:27 +00:00
this.downwardRoutes = liftRoutes(e.routes);
this.issueRoutingUpdate();
2013-08-12 02:23:24 +00:00
break;
case "send":
2013-09-27 11:14:27 +00:00
this.eventQueue.push(sendMessage(e.message, e.metaLevel + 1, e.isFeedback));
2013-08-12 02:23:24 +00:00
break;
default:
2013-09-27 11:14:27 +00:00
throw { message: "Event type " + e.type + " not understood",
event: e };
2013-08-12 02:23:24 +00:00
}
};
2013-09-27 14:36:51 +00:00
/*---------------------------------------------------------------------------*/
/* Ground interface */
2013-10-17 14:50:51 +00:00
function Ground(bootFn) {
var self = this;
this.stepperId = null;
this.wrap(function () {
self.world = new World(bootFn);
})();
2013-09-27 14:36:51 +00:00
}
2013-10-17 14:50:51 +00:00
Ground.prototype.wrap = function (f) {
var self = this;
return function () {
var oldWorld = World.current;
var result = null;
World.current = self;
try {
result = f();
} catch (e) {
World.current = oldWorld;
throw e;
}
World.current = oldWorld;
return result;
};
};
Ground.prototype.step = function () {
var self = this;
return this.wrap(function () {
return self.world.step();
})();
};
Ground.prototype.startStepping = World.prototype.startStepping;
Ground.prototype.stopStepping = World.prototype.stopStepping;
Ground.prototype.enqueueAction = function (action) {
if (action.type === 'routes') {
// Ignore it. These are generated outside user control so it
// shouldn't be an error.
} else {
console.error("You have sent a message into the outer void.", action);
}
};