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@1619: function divInt(x, y) { if (y == 0) er("Division by zero"); var n = x / y; return n < 0 ? Math.ceil(n) : Math.floor(n); } adamc@970: function mod(x, y) { return x % y; } adam@1619: function modInt(x, y) { if (y == 0) er("Division by zero"); 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'); } adam@1927: function ord(c) { return c.charCodeAt(0); } adam@1927: function isPrint(c) { return ord(c) > 31 && ord(c) < 127; } adamc@1177: function toLower(c) { return c.toLowerCase(); } adamc@1177: function toUpper(c) { return c.toUpperCase(); } 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) adam@1643: 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@2110: function pow(n, m) { adam@2110: return Math.pow(n, m); adam@2110: } adam@2110: adam@1571: adam@1599: // Time, represented as counts of microseconds since the epoch adam@1487: adam@1629: var time_format = "%c"; adam@1629: adam@1487: function showTime(tm) { adam@1629: return strftime(time_format, tm); adam@1629: } adam@1629: adam@1629: function showTimeHtml(tm) { adam@1629: return eh(showTime(tm)); 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@1685: function diffInMilliseconds(tm1, tm2) { adam@1685: return Math.round((tm2 - tm1) / 1000); adam@1685: } adam@1685: adam@1514: function toSeconds(tm) { adam@1514: return Math.round(tm / 1000000); adam@1514: } adam@1514: adam@1685: function toMilliseconds(tm) { adam@1685: return Math.round(tm / 1000); adam@1685: } adam@1685: adam@2187: function fromMilliseconds(tm) { adam@2187: return tm * 1000; adam@2187: } adam@2187: adam@1518: function addSeconds(tm, n) { adam@1518: return tm + n * 1000000; adam@1518: } adam@1518: adam@1599: function stringToTime_error(string) { adam@1599: var t = Date.parse(string); adam@1599: if (isNaN(t)) adam@1632: er("Invalid date string: " + string); adam@1599: else adam@1599: return t * 1000; adam@1599: } adam@1599: adam@1599: function stringToTime(string) { adam@1599: try { adam@1599: var t = Date.parse(string); adam@1599: if (isNaN(t)) adam@1599: return null; adam@1599: else adam@1599: return t * 1000; adam@1599: } catch (e) { adam@1599: return null; adam@1599: } rmbruijn@1597: } rmbruijn@1597: adam@1609: /* adam@1615: strftime() implementation from: adam@1615: YUI 3.4.1 (build 4118) adam@1615: Copyright 2011 Yahoo! Inc. All rights reserved. adam@1615: Licensed under the BSD License. adam@1615: http://yuilibrary.com/license/ adam@1615: */ adam@1609: adam@1615: var xPad=function (x, pad, r) adam@1615: { adam@1615: if(typeof r === "undefined") adam@1615: { adam@1615: r=10; adam@1615: } adam@1615: pad = pad.toString(); adam@1615: for( ; parseInt(x, 10)1; r/=10) { adam@1615: x = pad + x; adam@1615: } adam@1615: return x.toString(); adam@1615: }; adam@1609: adam@1615: var YDateEn = { adam@1615: a: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], adam@1615: A: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], adam@1615: b: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], adam@1615: B: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], adam@1615: c: "%a %d %b %Y %T %Z", adam@1615: p: ["AM", "PM"], adam@1615: P: ["am", "pm"], adam@1615: r: "%I:%M:%S %p", adam@1615: x: "%d/%m/%y", adam@1615: X: "%T" adam@1615: }; adam@1609: adam@1615: var Dt = { adam@1615: formats: { adam@1615: a: function (d, l) { return l.a[d.getDay()]; }, adam@1615: A: function (d, l) { return l.A[d.getDay()]; }, adam@1615: b: function (d, l) { return l.b[d.getMonth()]; }, adam@1615: B: function (d, l) { return l.B[d.getMonth()]; }, adam@1615: C: function (d) { return xPad(parseInt(d.getFullYear()/100, 10), 0); }, adam@1615: d: ["getDate", "0"], adam@1615: e: ["getDate", " "], adam@1615: g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); }, adam@1615: G: function (d) { adam@1615: var y = d.getFullYear(); adam@1615: var V = parseInt(Dt.formats.V(d), 10); adam@1615: var W = parseInt(Dt.formats.W(d), 10); phurst@1986: adam@1615: if(W > V) { adam@1615: y++; adam@1615: } else if(W===0 && V>=52) { adam@1615: y--; adam@1615: } phurst@1986: adam@1615: return y; adam@1615: }, adam@1615: H: ["getHours", "0"], adam@1615: I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); }, adam@1615: j: function (d) { adam@1615: var gmd_1 = new Date("" + d.getFullYear() + "/1/1 GMT"); adam@1615: var gmdate = new Date("" + d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " GMT"); adam@1615: var ms = gmdate - gmd_1; adam@1615: var doy = parseInt(ms/60000/60/24, 10)+1; adam@1615: return xPad(doy, 0, 100); adam@1615: }, adam@1615: k: ["getHours", " "], adam@1615: l: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, " "); }, adam@1615: m: function (d) { return xPad(d.getMonth()+1, 0); }, adam@1615: M: ["getMinutes", "0"], adam@1615: p: function (d, l) { return l.p[d.getHours() >= 12 ? 1 : 0 ]; }, adam@1615: P: function (d, l) { return l.P[d.getHours() >= 12 ? 1 : 0 ]; }, adam@1615: s: function (d, l) { return parseInt(d.getTime()/1000, 10); }, adam@1615: S: ["getSeconds", "0"], adam@1615: u: function (d) { var dow = d.getDay(); return dow===0?7:dow; }, adam@1615: U: function (d) { adam@1615: var doy = parseInt(Dt.formats.j(d), 10); adam@1615: var rdow = 6-d.getDay(); adam@1615: var woy = parseInt((doy+rdow)/7, 10); adam@1615: return xPad(woy, 0); adam@1615: }, adam@1615: V: function (d) { adam@1615: var woy = parseInt(Dt.formats.W(d), 10); adam@1615: var dow1_1 = (new Date("" + d.getFullYear() + "/1/1")).getDay(); adam@1615: var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1); adam@1615: if(idow === 53 && (new Date("" + d.getFullYear() + "/12/31")).getDay() < 4) adam@1615: { adam@1615: idow = 1; adam@1615: } adam@1615: else if(idow === 0) adam@1615: { adam@1615: idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31")); adam@1615: } phurst@1986: adam@1615: return xPad(idow, 0); adam@1615: }, adam@1615: w: "getDay", adam@1615: W: function (d) { adam@1615: var doy = parseInt(Dt.formats.j(d), 10); adam@1615: var rdow = 7-Dt.formats.u(d); adam@1615: var woy = parseInt((doy+rdow)/7, 10); adam@1615: return xPad(woy, 0, 10); adam@1615: }, adam@1615: y: function (d) { return xPad(d.getFullYear()%100, 0); }, adam@1615: Y: "getFullYear", adam@1615: z: function (d) { adam@1615: var o = d.getTimezoneOffset(); adam@1615: var H = xPad(parseInt(Math.abs(o/60), 10), 0); adam@1615: var M = xPad(Math.abs(o%60), 0); adam@1615: return (o>0?"-":"+") + H + M; adam@1615: }, adam@1615: Z: function (d) { adam@1615: var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, "$2").replace(/[a-z ]/g, ""); adam@1615: if(tz.length > 4) { adam@1615: tz = Dt.formats.z(d); adam@1615: } adam@1615: return tz; adam@1615: }, adam@1615: "%": function (d) { return "%"; } adam@1615: }, adam@1615: adam@1615: aggregates: { adam@1615: c: "locale", adam@1615: D: "%m/%d/%y", adam@1615: F: "%Y-%m-%d", adam@1615: h: "%b", adam@1615: n: "\n", adam@1615: r: "%I:%M:%S %p", adam@1615: R: "%H:%M", adam@1615: t: "\t", adam@1615: T: "%H:%M:%S", adam@1615: x: "locale", adam@1615: X: "locale" adam@1615: }, adam@1615: adam@1615: format : function (oDate, format) { adam@1615: var replace_aggs = function (m0, m1) { adam@1615: var f = Dt.aggregates[m1]; adam@1615: return (f === "locale" ? YDateEn[m1] : f); adam@1615: }; adam@1615: adam@1615: var replace_formats = function (m0, m1) { adam@1615: var f = Dt.formats[m1]; adam@1615: switch(typeof f) { adam@1615: case "string": adam@1615: return oDate[f](); adam@1615: case "function": adam@1615: return f.call(oDate, oDate, YDateEn); adam@1615: case "array": adam@1615: case "object": adam@1615: if(typeof(f[0]) === "string") adam@1615: return xPad(oDate[f[0]](), f[1]); adam@1615: default: adam@1615: return m1; adam@1615: } adam@1615: }; adam@1615: adam@1615: while(format.match(/%[cDFhnrRtTxX]/)) { adam@1615: format = format.replace(/%([cDFhnrRtTxX])/g, replace_aggs); adam@1609: } adam@1615: adam@1615: var str = format.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, replace_formats); adam@1615: adam@1615: replace_aggs = replace_formats = undefined; adam@1615: adam@1615: return str; adam@1615: } adam@1609: }; adam@1609: adam@1615: // End of YUI code adam@1609: adam@1609: function strftime(fmt, thisTime) adam@1609: { adam@1609: var thisDate = new Date(); adam@1622: thisDate.setTime(Math.floor(thisTime / 1000)); adam@1615: return Dt.format(thisDate, fmt); phurst@1986: }; phurst@1986: phurst@1986: function fromDatetime(year, month, date, hour, minute, second) { phurst@1986: return (new Date(year, month, date, hour, minute, second)).getTime() * 1000; phurst@1986: }; phurst@1986: phurst@1986: function datetimeYear(t) { phurst@1986: return (new Date(t / 1000)).getYear() + 1900; phurst@1986: }; phurst@1986: phurst@1986: function datetimeMonth(t) { phurst@1986: return (new Date(t / 1000)).getMonth(); phurst@1986: }; phurst@1986: phurst@1986: function datetimeDay(t) { phurst@1986: return (new Date(t / 1000)).getDate(); phurst@1986: }; phurst@1986: phurst@1986: function datetimeHour(t) { phurst@1986: return (new Date(t / 1000)).getHours(); phurst@1986: }; phurst@1986: phurst@1986: function datetimeMinute(t) { phurst@1986: return (new Date(t / 1000)).getMinutes(); phurst@1986: }; phurst@1986: phurst@1986: function datetimeSecond(t) { phurst@1986: return (new Date(t / 1000)).getSeconds(); phurst@1986: }; phurst@1986: phurst@1986: function datetimeDayOfWeek(t) { phurst@1986: return (new Date(t / 1000)).getDay(); phurst@1986: }; adam@1609: adam@1487: adamc@729: // Error handling adamc@729: adam@1625: function uw_debug(msg) { adam@1625: try { adam@1625: console.debug(msg); adam@1625: } catch (e) { adam@1625: alert("DEBUG: " + msg); adam@1625: } adam@1625: adam@1625: return 0; adam@1625: } adam@1625: 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@1890: 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: adam@1890: function flift0(v) { adam@1890: return {c:"c", v:v}; adam@1890: } adam@1890: adamc@729: function onError(f) { adam@1890: errorHandlers = cons(flift0(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) { adam@1890: failHandlers = cons(flift0(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: adam@2188: function conn(msg) { adam@2188: var rx = /(.*)((.|\n|\r)*)<\/body>(.*)/g; adam@2188: var arr = rx.exec(msg); adam@2188: msg = (arr && arr.length >= 3) ? arr[2] : msg; adam@2188: runHandlers("RPC failure", connectHandlers, msg); adamc@729: } adamc@729: adamc@729: var serverHandlers = null; adamc@729: adamc@729: function onServerError(f) { adam@1890: serverHandlers = cons(flift0(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@1783: // Key and mouse events adam@1555: adam@1555: var uw_event = null; adam@1555: adam@1783: function uw_getEvent() { adam@1783: return window.event ? window.event : uw_event; adam@1555: } adam@1555: adam@1783: function firstGood(x, y) { adam@1783: if (x == undefined || x == 0) adam@1783: return y; adam@1783: else adam@1783: return x; adam@1783: } adam@1783: adam@1783: function uw_mouseEvent() { adam@1783: var ev = uw_getEvent(); adam@1783: adam@1783: return {_ScreenX : firstGood(ev.screenX, 0), adam@1783: _ScreenY : firstGood(ev.screenY, 0), adam@1783: _ClientX : firstGood(ev.clientX, 0), adam@1783: _ClientY : firstGood(ev.clientY, 0), adam@1783: _CtrlKey : firstGood(ev.ctrlKey, false), adam@1783: _ShiftKey : firstGood(ev.shiftKey, false), adam@1783: _AltKey : firstGood(ev.altKey, false), adam@1783: _MetaKey : firstGood(ev.metaKey, false), adam@1783: _Button : ev.button == 2 ? "Right" : ev.button == 1 ? "Middle" : "Left"}; adam@1783: } adam@1783: adam@1783: function uw_keyEvent() { adam@1783: var ev = uw_getEvent(); adam@1783: adam@1783: return {_KeyCode : firstGood(ev.keyCode, ev.which), adam@1783: _CtrlKey : firstGood(ev.ctrlKey, false), adam@1783: _ShiftKey : firstGood(ev.shiftKey, false), adam@1783: _AltKey : firstGood(ev.altKey, false), adam@1783: _MetaKey : firstGood(ev.metaKey, false)}; adam@1783: } adam@1783: adam@1783: adam@1783: 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@1783: document[name] = function(event) { uw_event = event; execF(execF(f, uw_mouseEvent())); }; adam@1555: else adam@1783: document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_mouseEvent())); }; adam@1555: } adam@1555: adam@1555: function uw_onClick(f) { adam@1555: uw_handler("onclick", f); adam@1555: } adam@1555: ziv@2130: function uw_onContextmenu(f) { ziv@2130: uw_handler("oncontextmenu", f); ziv@2130: } ziv@2130: 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: ziv@2130: function uw_onMouseenter(f) { ziv@2130: uw_handler("onmouseenter", f); ziv@2130: } ziv@2130: ziv@2130: function uw_onMouseleave(f) { ziv@2130: uw_handler("onmouseleave", f); ziv@2130: } ziv@2130: adam@1791: function uw_onMousemove(f) { adam@1791: uw_handler("onmousemove", f); adam@1791: } adam@1791: adam@1791: function uw_onMouseout(f) { adam@1791: uw_handler("onmouseout", f); adam@1791: } adam@1791: adam@1791: function uw_onMouseover(f) { adam@1791: uw_handler("onmouseover", f); adam@1791: } adam@1791: 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@1783: document[name] = function(event) { uw_event = event; execF(execF(f, uw_keyEvent())); }; adam@1555: else adam@1783: document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_keyEvent())); }; 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: adam@2129: function flattenAcc(a, cls, trs) { adam@2129: while (trs) { adam@2129: var tr = trs.data; adam@2129: trs = trs.next; adam@2129: adam@2129: if (tr.cat1 != null) { adam@2129: trs = cons(tr.cat1, cons(tr.cat2, trs)); adam@2129: } else if (tr.closure != null) { adam@2129: var cl = newClosure(tr.closure); adam@2129: cls.v = cons(cl, cls.v); adam@2129: a.push("cr(", cl.toString(), ")"); adam@2129: } else adam@2129: a.push(tr); adam@2129: } vshabanoff@1813: } vshabanoff@1813: vshabanoff@1813: function flatten(cls, tr) { vshabanoff@1813: var a = []; adam@2129: flattenAcc(a, cls, cons(tr, null)); vshabanoff@1813: return a.join(""); 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: // 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: adam@1803: var thisScript = null; adam@1803: adamc@604: function addNode(node) { adam@1678: if (thisScript) { adam@1678: if (thisScript.parentNode) adam@1678: thisScript.parentNode.replaceChild(node, thisScript); adam@1678: } else adamc@1260: lastParent().appendChild(node); adamc@603: } adamc@603: adam@1803: function runScripts(node) { adam@1803: if (node.tagName == "SCRIPT") { adam@1803: var savedScript = thisScript; adam@1803: thisScript = node; phurst@1986: adam@1803: try { adam@1803: eval(thisScript.text); adam@1803: } catch (v) { adam@1803: doExn(v); adam@1803: } adam@1803: if (thisScript.parentNode) adam@1803: thisScript.parentNode.removeChild(thisScript); adamc@690: adam@1803: thisScript = savedScript; adam@1803: } else 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