adamc@1036: // Detect browser quirks that we should be aware of. adamc@1036: adamc@1036: function needsDynPrefix() { adamc@1260: var span = document.createElement("span"); adamc@1260: span.innerHTML = ""; adamc@1260: var scripts = span.getElementsByTagName("script"); adamc@1260: return scripts.length == 0; adamc@1036: } adamc@1036: adamc@1036: var dynPrefix = needsDynPrefix() ? "A" : ""; adamc@1036: adamc@970: // Function versions of operators adamc@970: adamc@970: function not(x) { return !x; } adamc@970: function neg(x) { return -x; } adamc@970: adamc@970: function eq(x, y) { return x == y; } adamc@970: function plus(x, y) { return x + y; } adamc@970: function minus(x, y) { return x - y; } adamc@970: function times(x, y) { return x * y; } adamc@970: function div(x, y) { return x / y; } adam@1360: function divInt(x, y) { var n = x / y; return n < 0 ? Math.ceil(n) : Math.floor(n); } adamc@970: function mod(x, y) { return x % y; } adam@1360: function modInt(x, y) { var n = x % y; return n < 0 ? Math.ceil(n) : Math.floor(n); } adamc@970: function lt(x, y) { return x < y; } adamc@970: function le(x, y) { return x <= y; } adamc@970: adamc@1061: // Characters adamc@1061: adamc@1061: function isLower(c) { return c >= 'a' && c <= 'z'; } adamc@1061: function isUpper(c) { return c >= 'A' && c <= 'Z'; } adamc@1061: function isAlpha(c) { return isLower(c) || isUpper(c); } adamc@1061: function isDigit(c) { return c >= '0' && c <= '9'; } adamc@1061: function isAlnum(c) { return isAlpha(c) || isDigit(c); } adamc@1061: function isBlank(c) { return c == ' ' || c == '\t'; } adamc@1061: function isSpace(c) { return isBlank(c) || c == '\r' || c == '\n'; } adamc@1061: function isXdigit(c) { return isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); } adamc@1177: function toLower(c) { return c.toLowerCase(); } adamc@1177: function toUpper(c) { return c.toUpperCase(); } adamc@1061: adamc@1061: adamc@692: // Lists adamc@692: adamc@580: function cons(v, ls) { adamc@1260: return { next : ls, data : v }; adamc@690: } adamc@969: function rev(ls) { adamc@1260: var acc = null; adamc@1260: for (; ls; ls = ls.next) adamc@1260: acc = cons(ls.data, acc); adamc@1260: return acc; adamc@969: } adamc@690: function concat(ls1, ls2) { adamc@1260: var acc = ls2; adamc@1260: ls1 = rev(ls1); adamc@1260: for (; ls1; ls1 = ls1.next) adamc@1260: acc = cons(ls1.data, acc); adamc@1260: return acc; adamc@690: } adamc@690: function member(x, ls) { adamc@1260: for (; ls; ls = ls.next) adamc@1260: if (ls.data == x) adamc@1260: return true; adamc@1260: return false; adamc@690: } adamc@690: function remove(x, ls) { adamc@1260: var acc = null; adamc@969: adamc@1260: for (; ls; ls = ls.next) adamc@1260: if (ls.data == x) adamc@1260: return concat(acc, ls.next); adamc@1260: else adamc@1260: acc = cons(ls.data, acc); adamc@969: adamc@1260: return ls; adamc@690: } adamc@690: function union(ls1, ls2) { adamc@1260: var acc = ls2; adamc@969: adamc@1260: for (; ls1; ls1 = ls1.next) adamc@1260: if (!member(ls1.data, ls2)) adamc@1260: acc = cons(ls1.data, acc); adamc@969: adamc@1260: return acc; adamc@580: } adamc@703: function length(ls) { adamc@1260: var acc = 0; adamc@969: adamc@1260: for (; ls; ls = ls.next) adamc@1260: ++acc; adamc@969: adamc@1260: return acc; adamc@703: } adamc@670: adamc@690: adam@1571: // Floats adam@1571: adam@1571: function float(n) { adam@1571: return n; adam@1571: } adam@1571: adam@1571: function trunc(n) { adam@1571: return ~~n; adam@1571: } adam@1571: adam@1571: function ceil(n) { adam@1571: return Math.ceil(n); adam@1571: } adam@1571: adam@1571: function round(n) { adam@1571: return Math.round(n); adam@1571: } adam@1571: adam@1571: adam@1487: // Time adam@1487: adam@1487: function showTime(tm) { adam@1487: var newDate = new Date(); adam@1487: newDate.setTime(tm / 1000); adam@1487: return newDate.toUTCString(); adam@1487: } adam@1487: adam@1487: function now() { adam@1487: return (new Date()).getTime() * 1000; adam@1487: } adam@1487: adam@1514: function diffInSeconds(tm1, tm2) { adam@1514: return Math.round((tm2 - tm1) / 1000000); adam@1514: } adam@1514: adam@1514: function toSeconds(tm) { adam@1514: return Math.round(tm / 1000000); adam@1514: } adam@1514: adam@1518: function addSeconds(tm, n) { adam@1518: return tm + n * 1000000; adam@1518: } adam@1518: adam@1487: adamc@729: // Error handling adamc@729: adamc@729: function whine(msg) { adamc@1260: alert(msg); adamc@1260: throw msg; adamc@729: } adamc@729: adamc@810: function pf(loc) { adamc@1260: throw ("Pattern match failure (" + loc + ")"); adamc@729: } adamc@729: adam@1553: var lameDuck = false; adam@1553: adamc@798: function runHandlers(kind, ls, arg) { adam@1553: if (!lameDuck) { adam@1553: if (ls == null) adam@1553: alert(kind + ": " + arg); adam@1553: for (; ls; ls = ls.next) adam@1553: try { adam@1553: exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}}); adam@1553: } catch (v) { } adam@1553: } adamc@729: } adamc@729: adamc@729: var errorHandlers = null; adamc@729: adamc@729: function onError(f) { adamc@1260: errorHandlers = cons(f, errorHandlers); adamc@729: } adamc@729: adamc@729: function er(s) { adamc@1260: runHandlers("Error", errorHandlers, s); adamc@1260: throw {uw_error: s}; adamc@729: } adamc@729: adamc@729: var failHandlers = null; adamc@729: adamc@729: function onFail(f) { adamc@1260: failHandlers = cons(f, failHandlers); adamc@729: } adamc@729: adamc@729: function doExn(v) { adamc@1260: if (v == null || v.uw_error == null) { adamc@1260: var s = (v == null ? "null" : v.message ? v.message : v.toString()); adamc@1260: if (v != null && v.fileName && v.lineNumber) adamc@1260: s += " (" + v.fileName + ":" + v.lineNumber + ")"; adamc@1260: runHandlers("Fail", failHandlers, s); adamc@1260: } adamc@729: } adamc@729: adamc@729: var disconnectHandlers = null; adamc@729: adamc@976: function flift(f) { kkallio@1484: return {c: "c", v:{env:cons(f,null), body:{c:"v", n:1}}}; adamc@976: } adamc@976: adamc@729: function onDisconnect(f) { adamc@1260: disconnectHandlers = cons(flift(f), disconnectHandlers); adamc@729: } adamc@729: adamc@729: function discon() { adamc@1260: runHandlers("Disconnect", disconnectHandlers, null); adamc@729: } adamc@729: adamc@729: var connectHandlers = null; adamc@729: adamc@729: function onConnectFail(f) { adamc@1260: connectHandlers = cons(flift(f), connectHandlers); adamc@729: } adamc@729: adamc@729: function conn() { adamc@1260: runHandlers("Connect", connectHandlers, null); adamc@729: } adamc@729: adamc@729: var serverHandlers = null; adamc@729: adamc@729: function onServerError(f) { adamc@1260: serverHandlers = cons(f, serverHandlers); adamc@729: } adamc@729: adamc@729: function servErr(s) { adamc@1260: window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0); adamc@729: } adamc@729: adam@1555: // Key events adam@1555: adam@1555: var uw_event = null; adam@1555: adam@1555: function kc() { adam@1555: return window.event ? event.keyCode : (uw_event ? uw_event.which : 0); adam@1555: } adam@1555: adam@1555: // Document events adam@1555: adam@1555: function uw_handler(name, f) { adam@1555: var old = document[name]; adam@1555: if (old == undefined) adam@1559: document[name] = function(event) { uw_event = event; execF(f); }; adam@1555: else adam@1559: document[name] = function(event) { uw_event = event; old(); execF(f); }; adam@1555: } adam@1555: adam@1555: function uw_onClick(f) { adam@1555: uw_handler("onclick", f); adam@1555: } adam@1555: adam@1555: function uw_onDblclick(f) { adam@1555: uw_handler("ondblclick", f); adam@1555: } adam@1555: adam@1555: function uw_onMousedown(f) { adam@1555: uw_handler("onmousedown", f); adam@1555: } adam@1555: adam@1555: function uw_onMouseup(f) { adam@1555: uw_handler("onmouseup", f); adam@1555: } adam@1555: adam@1555: function uw_keyHandler(name, f) { adam@1555: var old = document[name]; adam@1555: if (old == undefined) adam@1559: document[name] = function(event) { uw_event = event; execF(execF(f, kc())); }; adam@1555: else adam@1559: document[name] = function(event) { uw_event = event; old(); execF(execF(f, kc())); }; adam@1555: } adam@1555: adam@1555: function uw_onKeydown(f) { adam@1555: uw_keyHandler("onkeydown", f); adam@1555: } adam@1555: adam@1555: function uw_onKeypress(f) { adam@1555: uw_keyHandler("onkeypress", f); adam@1555: } adam@1555: adam@1555: function uw_onKeyup(f) { adam@1555: uw_keyHandler("onkeyup", f); adam@1555: } adam@1555: adam@1559: // Cancelling of further event processing adam@1559: adam@1559: function uw_preventDefault() { adam@1559: var e = window.event ? window.event : uw_event; adam@1559: e.returnValue = false; adam@1559: if (e.preventDefault) e.preventDefault(); adam@1559: } adam@1559: adam@1559: function uw_stopPropagation() { adam@1559: var e = window.event ? window.event : uw_event; adam@1559: e.cancelBubble = true; adam@1559: if (e.stopPropagation) e.stopPropagation(); adam@1559: } adamc@729: adamc@692: // Embedding closures in XML strings adamc@692: adamc@693: function cs(f) { adamc@1260: return {closure: f}; adamc@693: } adamc@693: adamc@693: function isWeird(v) { adamc@1260: return v.closure != null || v.cat1 != null; adamc@693: } adamc@693: adamc@692: function cat(s1, s2) { adamc@1260: if (isWeird(s1) || isWeird(s2)) adamc@1260: return {cat1: s1, cat2: s2}; adamc@1260: else adamc@1260: return s1 + s2; adamc@692: } adamc@692: adamc@692: var closures = []; adamc@703: var freeClosures = null; adamc@692: adamc@692: function newClosure(f) { adamc@1260: var n; adamc@1260: if (freeClosures == null) { adamc@1260: n = closures.length; adamc@1260: } else { adamc@1260: n = freeClosures.data; adamc@1260: freeClosures = freeClosures.next; adamc@1260: } adamc@1260: closures[n] = f; adamc@1260: return n; adamc@692: } adamc@692: adamc@703: function freeClosure(n) { adamc@1260: closures[n] = null; adamc@1260: freeClosures = cons(n, freeClosures); adamc@703: } adamc@703: adamc@692: function cr(n) { adamc@1260: return closures[n]; adamc@692: } adamc@692: adamc@703: function flatten(cls, tr) { adamc@1260: if (tr.cat1 != null) adamc@1260: return flatten(cls, tr.cat1) + flatten(cls, tr.cat2); adamc@1260: else if (tr.closure != null) { adamc@1260: var cl = newClosure(tr.closure); adamc@1260: cls.v = cons(cl, cls.v); adamc@1260: return "cr(" + cl + ")"; adamc@1260: } else adamc@1260: return tr; adamc@692: } adamc@692: adamc@728: function flattenLocal(s) { adamc@1260: var cls = {v : null}; adamc@1260: var r = flatten(cls, s); adamc@1260: for (cl = cls.v; cl != null; cl = cl.next) adamc@1260: freeClosure(cl.data); adamc@1260: return r; adamc@728: } adamc@728: adamc@692: adamc@692: adamc@692: // Dynamic tree management adamc@692: adamc@690: function populate(node) { adamc@1260: var s = node.signal; adamc@1260: var oldSources = node.sources; adamc@1260: try { adamc@1260: var sr = execF(s, null); adamc@1260: var newSources = sr._sources; adamc@690: adamc@1260: for (var sp = oldSources; sp; sp = sp.next) adamc@1260: if (!member(sp.data, newSources)) adamc@1260: sp.data.dyns = remove(node, sp.data.dyns); adamc@690: adamc@1260: for (var sp = newSources; sp; sp = sp.next) adamc@1260: if (!member(sp.data, oldSources)) adamc@1260: sp.data.dyns = cons(node, sp.data.dyns); adamc@690: adamc@1260: node.sources = newSources; adamc@1260: node.recreate(sr._data); adamc@1260: } catch (v) { adamc@1260: doExn(v); adamc@1260: } adamc@579: } adamc@574: adamc@580: function sc(v) { adamc@1260: return {data : v, dyns : null}; adamc@580: } adamc@580: function sv(s, v) { adam@1491: if (s.data != v) { adam@1491: s.data = v; adamc@967: adam@1491: for (var ls = s.dyns; ls; ls = ls.next) adam@1491: if (!ls.dead) adam@1491: populate(ls.data); adam@1491: } adamc@580: } adamc@601: function sg(s) { adamc@1260: return s.data; adamc@601: } adamc@579: adamc@580: function ss(s) { adamc@1260: return {env:cons(s, null), body:{c:"r", l: adamc@1260: cons({n:"sources", v:{c:"c", v:cons(s, null)}}, adamc@1260: cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}}; adamc@580: } adamc@580: function sr(v) { adamc@1260: return {env:null, body:{c:"c", v:{_sources : null, _data : v}}}; adamc@580: } adamc@580: function sb(x,y) { adamc@1260: return {env:cons(y,cons(x,null)), adamc@1260: body:{c:"=", adamc@1260: e1:{c:"a", f:{c:"v", n:2}, x:{c:"c", v:null}}, adamc@1260: e2:{c:"=", adamc@1260: e1:{c:"a", adamc@1260: f:{c:"a", f:{c:"v", n:2}, x:{c:".", r:{c:"v", n:0}, f:"data"}}, adamc@1260: x:{c:"c", v:null}}, adamc@1260: e2:{c:"r", l:cons( adamc@1260: {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"}, adamc@1260: cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}}, adamc@1260: cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}}; adamc@580: } adamc@841: function scur(s) { adamc@1260: return execF(s, null)._data; adamc@841: } adamc@571: adamc@604: function lastParent() { adamc@1260: var pos = document.body; adamc@604: adamc@1260: while (pos.lastChild && pos.lastChild.nodeType == 1) adamc@1260: pos = pos.lastChild; adamc@600: adamc@1260: pos = pos.parentNode; adamc@1039: adamc@1260: return pos; adamc@600: } adamc@600: adamc@898: function parent() { adamc@1260: return thisScript ? thisScript.parentNode : lastParent(); adamc@898: } adamc@898: adamc@604: function addNode(node) { adamc@1260: if (thisScript) adamc@1260: thisScript.parentNode.replaceChild(node, thisScript); adamc@1260: else adamc@1260: lastParent().appendChild(node); adamc@603: } adamc@603: adamc@690: var thisScript = null; adamc@690: adamc@604: function runScripts(node) { adamc@1260: if (node.getElementsByTagName) { adamc@1260: var savedScript = thisScript; adamc@603: adamc@1260: var scripts = node.getElementsByTagName("script"), scriptsCopy = []; adamc@1260: var len = scripts.length; adamc@1260: for (var i = 0; i < len; ++i) adamc@1260: scriptsCopy[i] = scripts[i]; adamc@1260: for (var i = 0; i < len; ++i) { adamc@1260: thisScript = scriptsCopy[i]; adamc@1039: adamc@1260: try { adamc@1260: eval(thisScript.text); adamc@1260: } catch (v) { adamc@1260: doExn(v); adamc@1260: } adamc@1260: if (thisScript.parentNode) adamc@1260: thisScript.parentNode.removeChild(thisScript); adamc@1260: } adamc@1260: adamc@1260: thisScript = savedScript; adamc@729: } adamc@603: } adamc@603: adamc@603: adamc@692: // Dynamic tree entry points adamc@692: adamc@967: function killScript(scr) { adamc@1260: scr.dead = true; adamc@1260: for (var ls = scr.sources; ls; ls = ls.next) adamc@1260: ls.data.dyns = remove(scr, ls.data.dyns); adamc@1260: for (var ls = scr.closures; ls; ls = ls.next) adamc@1260: freeClosure(ls.data); adamc@967: } adamc@967: adam@1538: // Sometimes we wind up with tables that contain