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@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@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: adamc@798: function runHandlers(kind, ls, arg) { adamc@1260: if (ls == null) adamc@1260: alert(kind + ": " + arg); adamc@1260: for (; ls; ls = ls.next) adamc@1260: try { adamc@1260: exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}}); adamc@1260: } catch (v) { } 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: 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) { adamc@1260: s.data = v; adamc@967: adamc@1260: for (var ls = s.dyns; ls; ls = ls.next) adamc@1260: if (!ls.dead) adamc@1260: populate(ls.data); 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: adamc@901: function dyn(pnode, s) { adamc@1260: var x = document.createElement("script"); adamc@1260: x.dead = false; adamc@1260: x.signal = s; adamc@1260: x.sources = null; adamc@1260: x.closures = null; adamc@899: adamc@1260: var firstChild = null; adamc@899: adamc@1260: x.recreate = function(v) { adamc@1260: for (var ls = x.closures; ls; ls = ls.next) adamc@1260: freeClosure(ls.data); adamc@945: adamc@946: var next; adamc@1260: for (var child = firstChild; child && child != x; child = next) { adamc@1260: next = child.nextSibling; adamc@1260: adamc@1260: killScript(child); adamc@1260: if (child.getElementsByTagName) { adamc@1260: var arr = child.getElementsByTagName("script"); adamc@1260: for (var i = 0; i < arr.length; ++i) adamc@1260: killScript(arr[i]); adamc@1260: } adamc@1260: adamc@1260: if (child.parentNode) adamc@1260: child.parentNode.removeChild(child); adamc@946: } adamc@903: adamc@1260: var cls = {v : null}; adamc@1260: var html = flatten(cls, v); adamc@1260: if (pnode != "table" && pnode != "tr") adamc@1260: html = dynPrefix + html; adamc@1260: x.closures = cls.v; adamc@946: adamc@1260: if (pnode == "table") { adamc@1260: var dummy = document.createElement("body"); adamc@1260: dummy.innerHTML = "" + html + "
"; adamc@1260: runScripts(dummy); adamc@1260: var table = x.parentNode; adamc@1260: adamc@1260: if (table) { adamc@1260: firstChild = null; adamc@1260: var tbody; adamc@1260: adamc@1260: var arr = dummy.getElementsByTagName("tbody"); adamc@1260: adamc@1260: var tbody; adamc@1260: if (arr.length > 0 && arr[0].parentNode == dummy.firstChild) { adamc@1260: tbody = arr[0]; adamc@1260: var next; adamc@1260: for (var node = dummy.firstChild.firstChild; node; node = next) { adamc@1260: next = node.nextSibling; adamc@1260: adamc@1260: if (node.tagName != "TBODY") adamc@1260: tbody.appendChild(node); adamc@1260: } adamc@1260: } else adamc@1260: tbody = dummy.firstChild; adamc@1260: adamc@1260: var next; adamc@1260: firstChild = document.createElement("script"); adamc@1260: table.insertBefore(firstChild, x); adamc@1260: for (var node = tbody.firstChild; node; node = next) { adamc@1260: next = node.nextSibling; adamc@1260: table.insertBefore(node, x); adamc@1260: } adamc@1260: } adamc@1260: } else if (pnode == "tr") { adamc@1260: var dummy = document.createElement("body"); adamc@1260: dummy.innerHTML = "" + html + "
"; adamc@1260: runScripts(dummy); adamc@1260: var table = x.parentNode; adamc@1260: adamc@1260: if (table) { adamc@1260: var arr = dummy.getElementsByTagName("tr"); adamc@1260: firstChild = null; adamc@1260: var tr; adamc@1260: if (arr.length > 0 && table != null) adamc@1260: tr = arr[0]; adamc@1260: else adamc@1260: tr = dummy.firstChild; adamc@1260: adamc@1260: var next; adamc@1260: firstChild = document.createElement("script"); adamc@1260: table.insertBefore(firstChild, x); adamc@1260: for (var node = tr.firstChild; node; node = next) { adamc@1260: next = node.nextSibling; adamc@1260: table.insertBefore(node, x); adamc@1260: } adamc@1260: } adamc@1260: } else { adamc@1260: firstChild = document.createElement("span"); adamc@1260: firstChild.innerHTML = html; adamc@1260: runScripts(firstChild); adamc@1260: if (x.parentNode) adamc@1260: x.parentNode.insertBefore(firstChild, x); adamc@903: } adamc@1260: }; adamc@899: adamc@1260: addNode(x); adamc@1260: populate(x); adamc@571: } adamc@582: adam@1463: function input(x, s, recreate, type, name) { adam@1463: if (name) x.name = name; adamc@1260: if (type) x.type = type; adamc@1260: x.dead = false; adamc@1260: x.signal = ss(s); adamc@1260: x.sources = null; adamc@1260: x.recreate = recreate(x); adamc@1260: addNode(x); adamc@1260: populate(x); adamc@816: adamc@1260: return x; adamc@816: } adamc@816: adam@1463: function inp(s, name) { adamc@1260: var x = input(document.createElement("input"), s, adam@1463: function(x) { return function(v) { if (x.value != v) x.value = v; }; }, null, name); adamc@1260: x.value = s.data; adamc@1260: x.onkeyup = function() { sv(s, x.value) }; adamc@816: adamc@1260: return x; adamc@816: } adamc@816: adam@1479: function selectValue(x) { adam@1479: if (x.options.length == 0) adam@1479: return ""; adam@1479: else adam@1479: return x.options[x.selectedIndex].value; adam@1479: } adam@1479: adam@1479: function setSelectValue(x, v) { adam@1479: for (var i = 0; i < x.options.length; ++i) { adam@1479: if(x.options[i].value == v) { adam@1479: x.selectedIndex = i; adam@1479: return; adam@1479: } adam@1479: } adam@1479: } adam@1479: adamc@816: function sel(s, content) { adamc@1260: var dummy = document.createElement("span"); adamc@1260: dummy.innerHTML = ""; adam@1479: var x = input(dummy.firstChild, s, function(x) { return function(v) { if (selectValue(x) != v) setSelectValue(x, v); }; }); adam@1479: adam@1479: for (var i = 0; i < x.options.length; ++i) { adam@1479: if (x.options[i].value == "") adam@1479: x.options[i].value = x.options[i].text; adam@1479: else adam@1479: x.options[i].value = x.options[i].value.substring(1); adam@1479: } adam@1479: adam@1479: setSelectValue(x, s.data); adam@1479: if (selectValue(x) != s.data) adam@1479: sv(s, selectValue(x)); adam@1479: x.onchange = function() { sv(s, selectValue(x)) }; adamc@797: adamc@1260: return x; adamc@598: } adamc@598: adamc@817: function chk(s) { adamc@1260: var x = input(document.createElement("input"), s, adamc@1260: function(x) { return function(v) { if (x.checked != v) x.checked = v; }; }, "checkbox"); adamc@1260: x.defaultChecked = x.checked = s.data; adamc@1260: x.onclick = function() { sv(s, x.checked) }; adamc@817: adamc@1260: return x; adamc@817: } adamc@817: adamc@1099: function tbx(s) { adamc@1260: var x = input(document.createElement("textarea"), s, adamc@1260: function(x) { return function(v) { if (x.innerHTML != v) x.innerHTML = v; }; }); adamc@1260: x.innerHTML = s.data; adamc@1260: x.onkeyup = function() { sv(s, x.value) }; adamc@1099: adamc@1260: return x; adamc@1099: } adamc@1099: adamc@800: function addOnChange(x, f) { adamc@1260: var old = x.onchange; adam@1290: if (old == null) adam@1290: x.onchange = f; adam@1290: else adam@1290: x.onchange = function() { old(); f(); }; adam@1290: } adam@1290: adam@1290: function addOnKeyUp(x, f) { adam@1290: var old = x.onkeyup; adam@1290: if (old == null) adam@1290: x.onkeyup = f; adam@1290: else adam@1290: x.onkeyup = function(x) { old(x); f(x); }; adamc@800: } adamc@800: adamc@692: adamc@692: // Basic string operations adamc@692: adamc@597: function eh(x) { adamc@1260: if (x == null) adamc@1260: return "NULL"; adamc@1260: else adamc@1260: return x.split("&").join("&").split("<").join("<").split(">").join(">"); adamc@597: } adamc@597: adamc@582: function ts(x) { return x.toString() } adamc@586: function bs(b) { return (b ? "True" : "False") } adamc@586: adamc@1057: function id(x) { return x; } adamc@1038: function sub(s, i) { return s.charAt(i); } adamc@828: function suf(s, i) { return s.substring(i); } adamc@828: function slen(s) { return s.length; } adamc@829: function sidx(s, ch) { adamc@1260: var r = s.indexOf(ch); adamc@1260: if (r == -1) adamc@1260: return null; adamc@1260: else adamc@1260: return r; adamc@829: } adamc@831: function sspn(s, chs) { adamc@1260: for (var i = 0; i < s.length; ++i) adamc@1260: if (chs.indexOf(s.charAt(i)) != -1) adamc@1260: return i; adamc@831: adamc@1260: return null; adamc@831: } adamc@829: function schr(s, ch) { adamc@1260: var r = s.indexOf(ch); adamc@1260: if (r == -1) adamc@1260: return null; adamc@1260: else adamc@1260: return s.substring(r); adamc@829: } adamc@829: function ssub(s, start, len) { adamc@1260: return s.substring(start, start+len); adamc@829: } adamc@821: adamc@649: function pi(s) { adamc@1260: var r = parseInt(s); adamc@1260: if (r.toString() == s) adamc@1260: return r; adamc@1260: else adamc@1260: er("Can't parse int: " + s); adamc@649: } adamc@649: adamc@649: function pfl(s) { adamc@1260: var r = parseFloat(s); adamc@1260: if (r.toString() == s) adamc@1260: return r; adamc@1260: else adamc@1260: er("Can't parse float: " + s); adamc@649: } adamc@649: adamc@838: function pio(s) { adamc@1260: var r = parseInt(s); adamc@1260: if (r.toString() == s) adamc@1260: return r; adamc@1260: else adamc@1260: return null; adamc@838: } adamc@838: adamc@838: function pflo(s) { adamc@1260: var r = parseFloat(s); adamc@1260: if (r.toString() == s) adamc@1260: return r; adamc@1260: else adamc@1260: return null; adamc@838: } adamc@838: adamc@692: function uf(s) { adamc@1260: if (s.length == 0) adamc@1260: return "_"; adamc@1260: s = s.replace(new RegExp ("\\.", "g"), ".2E"); adamc@1260: return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(new RegExp ("%", "g"), "."); adamc@691: } adamc@691: adamc@692: function uu(s) { adamc@1260: if (s.length > 0 && s.charAt(0) == '_') { adamc@1260: s = s.substring(1); adamc@1260: } else if (s.length >= 3 && (s.charAt(0) == '%' || s.charAt(0) == '.') adamc@1260: && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F')) adamc@1260: s = s.substring(3); adamc@1260: s = s.replace(new RegExp ("\\+", "g"), " "); adamc@1260: s = s.replace(new RegExp ("\\.", "g"), "%"); adamc@1260: return decodeURIComponent(s); adamc@905: } adamc@905: adamc@1108: function atr(s) { adamc@1260: return s.replace(new RegExp ("\"", "g"), """).replace(new RegExp ("&", "g"), "&") adamc@1260: } adamc@1108: adamc@912: function ub(b) { adamc@1260: return b ? "1" : "0"; adamc@912: } adamc@912: adamc@905: function uul(getToken, getData) { adamc@1260: var tok = getToken(); adamc@1260: if (tok == "Nil") { adamc@1260: return null; adamc@1260: } else if (tok == "Cons") { adamc@1260: var d = getData(); adamc@1260: var l = uul(getToken, getData); adamc@1260: return {_1:d, _2:l}; adamc@1260: } else adamc@1260: whine("Can't unmarshal list (" + tok + ")"); adamc@692: } adamc@692: adamc@961: function strcmp(str1, str2) { adamc@1260: return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1)); adamc@961: } adamc@692: adamc@603: adamc@692: // Remote calls adamc@609: adamc@703: var client_id = null; adamc@668: var client_pass = 0; adamc@668: var url_prefix = "/"; adamc@673: var timeout = 60; adamc@668: adamc@668: function getXHR(uri) adamc@609: { adamc@609: try { adamc@1260: return new XMLHttpRequest(); adamc@609: } catch (e) { adamc@1260: try { adamc@1260: return new ActiveXObject("Msxml2.XMLHTTP"); adamc@1260: } catch (e) { adamc@1260: try { adamc@1260: return new ActiveXObject("Microsoft.XMLHTTP"); adamc@1260: } catch (e) { adamc@1260: whine("Your browser doesn't seem to support AJAX."); adamc@1260: } adamc@1260: } adamc@609: } adamc@609: } adamc@609: adamc@736: var sig = null; adamc@736: adamc@1042: var unloading = false, inFlight = null; adamc@1042: adamc@1042: function unload() { adamc@1260: unloading = true; adamc@1042: adamc@1260: for (; inFlight; inFlight = inFlight.next) { adamc@1260: inFlight.data.abort(); adamc@1260: } adamc@1042: } adamc@1042: adamc@736: function requestUri(xhr, uri, needsSig) { adamc@1260: if (unloading) adamc@1260: return; adamc@1042: adamc@1260: xhr.open("POST", uri, true); adamc@1260: xhr.setRequestHeader("Content-type", "text/plain"); adamc@1260: xhr.setRequestHeader("Content-length", "0"); adamc@1260: xhr.setRequestHeader("Connection", "close"); adamc@668: adamc@1260: if (client_id != null) { adamc@1260: xhr.setRequestHeader("UrWeb-Client", client_id.toString()); adamc@1260: xhr.setRequestHeader("UrWeb-Pass", client_pass.toString()); adamc@1260: } adamc@668: adamc@1260: if (needsSig) { adamc@1260: if (sig == null) adamc@1260: whine("Missing cookie signature!"); adamc@736: adamc@1260: xhr.setRequestHeader("UrWeb-Sig", sig); adamc@1260: } adamc@736: adamc@1260: inFlight = cons(xhr, inFlight); adamc@1260: xhr.send(null); adamc@668: } adamc@668: adamc@1042: function xhrFinished(xhr) { adamc@1260: xhr.abort(); adamc@1260: inFlight = remove(xhr, inFlight); adamc@1042: } adamc@1042: adamc@1111: function unurlify(parse, s) { adamc@1260: return parse(s); adamc@1111: } adamc@1111: adam@1385: function redirect(s) { adam@1385: window.location = s; adam@1385: } adam@1385: adamc@978: function rc(prefix, uri, parse, k, needsSig) { adamc@1260: uri = cat(prefix, uri); adamc@1260: uri = flattenLocal(uri); adamc@1260: var xhr = getXHR(); adamc@609: adamc@1260: xhr.onreadystatechange = function() { adamc@1260: if (xhr.readyState == 4) { adamc@1260: var isok = false; adamc@612: adamc@1260: try { adamc@1260: if (xhr.status == 200) adamc@1260: isok = true; adamc@1260: } catch (e) { } adamc@612: adamc@1260: if (isok) { adam@1446: var lines = xhr.responseText.split("\n"); adam@1446: if (lines.length != 2) adam@1446: whine("Bad RPC response lines"); adam@1446: adam@1446: eval(lines[0]); adam@1446: adamc@1260: try { adam@1446: k(parse(lines[1])); adamc@1260: } catch (v) { adamc@1260: doExn(v); adamc@1260: } adamc@1260: } else { adamc@1260: conn(); adamc@1260: } adamc@1260: adamc@1260: xhrFinished(xhr); adamc@729: } adamc@1260: }; adamc@1042: adamc@1260: requestUri(xhr, uri, needsSig); adamc@609: } adamc@667: adamc@667: function path_join(s1, s2) { adamc@1260: if (s1.length > 0 && s1.charAt(s1.length-1) == '/') adamc@1260: return s1 + s2; adamc@1260: else adamc@1260: return s1 + "/" + s2; adamc@667: } adamc@667: adamc@670: var channels = []; adamc@670: adamc@670: function newQueue() { adamc@1260: return { front : null, back : null }; adamc@670: } adamc@670: function enqueue(q, v) { adamc@1260: if (q.front == null) { adamc@1260: q.front = cons(v, null); adamc@1260: q.back = q.front; adamc@1260: } else { adamc@1260: var node = cons(v, null); adamc@1260: q.back.next = node; adamc@1260: q.back = node; adamc@1260: } adamc@670: } adamc@670: function dequeue(q) { adamc@670: if (q.front == null) adamc@1260: return null; adamc@1260: else { adamc@1260: var r = q.front.data; adamc@1260: q.front = q.front.next; adamc@1260: if (q.front == null) adamc@1260: q.back = null; adamc@1260: return r; adamc@1260: } adamc@670: } adamc@670: adamc@670: function newChannel() { adamc@1260: return { msgs : newQueue(), listeners : newQueue() }; adamc@670: } adamc@670: adamc@667: function listener() { adamc@1260: var uri = path_join(url_prefix, ".msgs"); adamc@1260: var xhr = getXHR(); adamc@1260: var tid, orsc, onTimeout; adamc@673: adamc@1260: var connect = function () { adamc@1260: xhr.onreadystatechange = orsc; adamc@1260: tid = window.setTimeout(onTimeout, timeout * 500); adamc@1260: requestUri(xhr, uri, false); adamc@1260: } adamc@673: adamc@1260: orsc = function() { adamc@1260: if (xhr.readyState == 4) { adamc@1260: window.clearTimeout(tid); adamc@673: adamc@1260: var isok = false; adamc@667: adamc@1260: try { adamc@1260: if (xhr.status == 200) adamc@1260: isok = true; adamc@1260: } catch (e) { } adamc@667: adamc@1260: if (isok) { adam@1447: var text = xhr.responseText; adamc@1260: if (text == "") adamc@1260: return; adamc@1260: var lines = text.split("\n"); adamc@889: adamc@1260: if (lines.length < 2) { adamc@1260: discon(); adamc@1260: return; adamc@1260: } adamc@1260: adam@1447: var messageReader = function(i) { adam@1447: if (i+1 >= lines.length) { adam@1447: xhrFinished(xhr); adam@1447: connect(); adam@1447: } adam@1447: else { adam@1447: var chn = lines[i]; adam@1447: var msg = lines[i+1]; adamc@1260: adam@1447: if (chn == "E") { adam@1447: eval(msg); adam@1447: window.setTimeout(function() { messageReader(i+2); }, 0); adam@1447: } else { adam@1447: if (chn < 0) adam@1447: whine("Out-of-bounds channel in message from remote server"); adamc@1260: adam@1447: var ch; adamc@1260: adam@1447: if (chn >= channels.length || channels[chn] == null) { adam@1447: ch = newChannel(); adam@1447: channels[chn] = ch; adam@1447: } else adam@1447: ch = channels[chn]; adamc@1260: adam@1447: var listener = dequeue(ch.listeners); adam@1447: if (listener == null) { adam@1447: enqueue(ch.msgs, msg); adam@1447: } else { adam@1447: try { adam@1447: listener(msg); adam@1447: } catch (v) { adam@1447: doExn(v); adam@1447: } adam@1447: } adam@1447: adam@1447: messageReader(i+2); adamc@1260: } adamc@1260: } adamc@1260: } adamc@1260: adam@1447: messageReader(0); adamc@1260: } adamc@1260: else { adamc@1260: try { adamc@1260: if (xhr.status != 0) adamc@1260: servErr("Error querying remote server for messages: " + xhr.status); adamc@1260: } catch (e) { } adamc@1260: } adamc@729: } adamc@1260: }; adamc@669: adamc@1260: onTimeout = function() { adamc@1260: xhrFinished(xhr); adamc@1260: connect(); adamc@1260: }; adamc@670: adamc@673: connect(); adamc@667: } adamc@670: adamc@670: function rv(chn, parse, k) { adamc@1260: if (chn == null) adamc@1260: return; adamc@682: adamc@1260: if (chn < 0) adamc@1260: whine("Out-of-bounds channel receive"); adamc@670: adamc@1260: var ch; adamc@670: adamc@1260: if (chn >= channels.length || channels[chn] == null) { adamc@1260: ch = newChannel(); adamc@1260: channels[chn] = ch; adamc@1260: } else adamc@1260: ch = channels[chn]; adamc@670: adamc@1260: var msg = dequeue(ch.msgs); adamc@1260: if (msg == null) { adamc@1260: enqueue(ch.listeners, function(msg) { k(parse(msg)); }); adamc@1260: } else { adamc@1260: try { adamc@1260: k(parse(msg)); adamc@1260: } catch (v) { adamc@1260: doExn(v); adamc@1260: } adamc@728: } adamc@670: } adamc@693: adamc@978: function sl(ms, k) { adamc@1260: window.setTimeout(function() { k(null); }, ms); adamc@1021: } adamc@1021: adamc@1021: function sp(e) { adamc@1260: execF(e, null); adamc@978: } adamc@978: adamc@693: adamc@895: // Key events adamc@895: adamc@970: var uw_event = null; adamc@970: adamc@970: function kc() { adam@1290: return window.event ? event.keyCode : (uw_event ? uw_event.keyCode : 0); adamc@970: } adamc@970: adamc@970: adamc@970: // The Ur interpreter adamc@970: adamc@970: var urfuncs = []; adamc@970: adamc@970: function lookup(env, n) { adamc@1260: while (env != null) { adamc@1260: if (n == 0) adamc@1260: return env.data; adamc@1260: else { adamc@1260: --n; adamc@1260: env = env.next; adamc@1260: } adamc@970: } adamc@970: adamc@1260: whine("Out-of-bounds Ur variable reference"); adamc@970: } adamc@970: adamc@974: function execP(env, p, v) { adamc@1260: switch (p.c) { adamc@1260: case "w": adamc@1260: return env; adamc@1260: case "v": adamc@1260: return cons(v, env); adamc@1260: case "c": adamc@1260: if (v == p.v) adamc@1260: return env; adamc@1260: else adamc@1260: return false; adamc@1260: case "s": adamc@1260: if (v == null) adamc@1260: return false; adamc@1260: else adamc@1260: return execP(env, p.p, p.n ? v.v : v); adamc@1260: case "1": adamc@1260: if (v.n != p.n) adamc@1260: return false; adamc@1260: else adamc@1260: return execP(env, p.p, v.v); adamc@1260: case "r": adamc@1260: for (var fs = p.l; fs != null; fs = fs.next) { adamc@1260: env = execP(env, fs.data.p, v["_" + fs.data.n]); adamc@1260: if (env == false) adamc@1260: return false; adamc@1260: } adamc@1260: return env; adamc@1260: default: adamc@1260: whine("Unknown Ur pattern kind" + p.c); adamc@974: } adamc@974: } adamc@974: adamc@970: function exec0(env, e) { adamc@1260: return exec1(env, null, e); adamc@1020: } adamc@1020: adamc@1020: function exec1(env, stack, e) { adamc@1260: var stack, usedK = false; adamc@970: adamc@1260: var saveEnv = function() { adamc@1260: if (stack.next != null && stack.next.data.c != "<") adamc@1260: stack = cons({c: "<", env: env}, stack.next); adamc@1260: else adamc@1260: stack = stack.next; adamc@1260: }; adamc@983: adamc@1260: while (true) { adamc@1260: switch (e.c) { adamc@1260: case "c": adamc@1260: var v = e.v; adamc@1260: if (stack == null) adamc@1260: return v; adamc@1260: var fr = stack.data; adamc@970: adamc@1260: switch (fr.c) { adamc@1260: case "s": adamc@1260: e = {c: "c", v: {v: v}}; adamc@1260: stack = stack.next; adamc@1260: break; adamc@1260: case "1": adamc@1260: e = {c: "c", v: {n: fr.n, v: v}}; adamc@1260: stack = stack.next; adamc@1260: break; adamc@1260: case "f": adamc@1260: fr.args[fr.pos++] = v; adamc@1260: if (fr.a == null) { adamc@1260: var res; adamc@1260: stack = stack.next; adamc@1035: adamc@1260: if (fr.f.apply) adamc@1260: res = fr.f.apply(null, fr.args); adamc@1260: else if (fr.args.length == 0) adamc@1260: res = fr.f(); adamc@1260: else if (fr.args.length == 1) adamc@1260: res = fr.f(fr.args[0]); adamc@1260: else if (fr.args.length == 2) adamc@1260: res = fr.f(fr.args[0], fr.args[1]); adamc@1260: else if (fr.args.length == 3) adamc@1260: res = fr.f(fr.args[0], fr.args[1], fr.args[2]); adamc@1260: else if (fr.args.length == 4) adamc@1260: res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]); adamc@1260: else if (fr.args.length == 5) adamc@1260: res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]); adamc@1260: else adamc@1260: whine("Native function has " + fr.args.length + " args, but there is no special case for that count."); adamc@1035: adamc@1260: e = {c: "c", v: res}; adamc@1260: if (usedK) return null; adamc@1260: } else { adamc@1260: e = fr.a.data; adamc@1260: fr.a = fr.a.next; adamc@1260: } adamc@1260: break; adamc@1260: case "a1": adamc@1260: e = fr.x; adamc@1260: stack = cons({c: "a2", f: v}, stack.next); adamc@1260: break; adamc@1260: case "a2": adamc@1260: if (fr.f == null) adamc@1260: whine("Ur: applying null function"); adamc@1260: else if (fr.f.body) { adamc@1260: saveEnv(); adamc@1260: env = cons(v, fr.f.env); adamc@1260: e = fr.f.body; adamc@1260: } else { adamc@1260: e = {c: "c", v: fr.f(v)}; adamc@1260: stack = stack.next; adamc@1260: } adamc@1260: break; adamc@1260: case "<": adamc@1260: env = fr.env; adamc@1260: stack = stack.next; adamc@1260: break; adamc@1260: case "r": adamc@1260: fr.fs["_" + fr.n] = v; adamc@1260: if (fr.l == null) { adamc@1260: e = {c: "c", v: fr.fs}; adamc@1260: stack = stack.next; adamc@1260: } else { adamc@1260: fr.n = fr.l.data.n; adamc@1260: e = fr.l.data.v; adamc@1260: fr.l = fr.l.next; adamc@1260: } adamc@1260: break; adamc@1260: case ".": adamc@1260: e = {c: "c", v: v["_" + fr.f]}; adamc@1260: stack = stack.next; adamc@1260: break; adamc@1260: case ";": adamc@1260: e = fr.e2; adamc@1260: stack = stack.next; adamc@1260: break; adamc@1260: case "=": adamc@1260: saveEnv(); adamc@1260: env = cons(v, env); adamc@1260: e = fr.e2; adamc@1260: break; adamc@1260: case "m": adamc@1260: var ps; adamc@1260: for (ps = fr.p; ps != null; ps = ps.next) { adamc@1260: var r = execP(env, ps.data.p, v); adamc@1260: if (r != false) { adamc@1260: saveEnv(); adamc@1260: env = r; adamc@1260: e = ps.data.b; adamc@1260: break; adamc@1260: } adamc@1260: } adamc@1260: if (ps == null) adamc@1260: whine("Match failure in Ur interpretation"); adamc@1260: break; adamc@1260: default: adamc@1260: whine("Unknown Ur continuation kind " + fr.c); adamc@1260: } adamc@1260: adamc@1260: break; adamc@1260: case "v": adamc@1260: e = {c: "c", v: lookup(env, e.n)}; adamc@1260: break; adamc@1260: case "n": adamc@1260: var idx = e.n; adamc@1260: e = urfuncs[idx]; adamc@1260: if (e.c == "t") adamc@1262: e = urfuncs[idx] = eval("(" + e.f + ")"); adamc@1260: break; adamc@1260: case "s": adamc@1260: stack = cons({c: "s"}, stack); adamc@1260: e = e.v; adamc@1260: break; adamc@1260: case "1": adamc@1260: stack = cons({c: "1", n: e.n}, stack); adamc@1260: e = e.v; adamc@1260: break; adamc@1260: case "f": adamc@1260: if (e.a == null) adamc@1260: e = {c: "c", v: (eval(e.f))()}; adamc@1260: else { adamc@1260: var args = []; adamc@1260: stack = cons({c: "f", f: eval(e.f), args: args, pos: 0, a: e.a.next}, stack); adamc@1260: if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data); adamc@1260: e = e.a.data; adamc@1260: } adamc@1260: break; adamc@1260: case "l": adamc@1260: e = {c: "c", v: {env: env, body: e.b}}; adamc@1260: break; adamc@1260: case "a": adamc@1260: stack = cons({c: "a1", x: e.x}, stack); adamc@1260: e = e.f; adamc@1260: break; adamc@1260: case "r": adamc@1260: if (e.l == null) adamc@1260: whine("Empty Ur record in interpretation"); adamc@1260: var fs = {}; adamc@1260: stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack); adamc@1260: e = e.l.data.v; adamc@1260: break; adamc@1260: case ".": adamc@1260: stack = cons({c: ".", f: e.f}, stack); adamc@1260: e = e.r; adamc@1260: break; adamc@1260: case ";": adamc@1260: stack = cons({c: ";", e2: e.e2}, stack); adamc@1260: e = e.e1; adamc@1260: break; adamc@1260: case "=": adamc@1260: stack = cons({c: "=", e2: e.e2}, stack); adamc@1260: e = e.e1; adamc@1260: break; adamc@1260: case "m": adamc@1260: stack = cons({c: "m", p: e.p}, stack); adamc@1260: e = e.e; adamc@1260: break; adamc@1260: case "e": adamc@1260: e = {c: "c", v: cs({c: "wc", env: env, body: e.e})}; adamc@1260: break; adamc@1260: case "wc": adamc@1260: env = e.env; adamc@1260: e = e.body; adamc@1260: break; adamc@1260: case "K": adamc@1260: { var savedStack = stack.next, savedEnv = env; adamc@1260: e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };} adamc@1260: usedK = true; adamc@1260: break; adamc@1260: default: adamc@1260: whine("Unknown Ur expression kind " + e.c); adamc@970: } adamc@970: } adamc@970: } adamc@970: adamc@976: function execD(e) { adamc@1260: return exec0(null, e); adamc@976: } adamc@976: adamc@970: function exec(e) { adamc@1260: var r = exec0(null, e); adamc@970: adamc@1260: if (r != null && r.body != null) adamc@1260: return function(v) { return exec0(cons(v, r.env), r.body); }; adamc@1260: else adamc@1260: return r; adamc@895: } adamc@895: adamc@977: function execF(f, x) { adamc@1260: return exec0(cons(x, f.env), f.body); adamc@977: } adamc@977: adamc@895: adam@1290: // Wrappers adam@1290: adam@1290: function confrm(s) { adam@1290: return confirm(s) ? true : false; adam@1290: } adam@1290: adam@1290: adamc@1323: // URL blessing adamc@1323: adamc@1323: var urlRules = null; adamc@1323: adamc@1323: function checkUrl(s) { adamc@1323: for (var r = urlRules; r; r = r.next) { adamc@1323: var ru = r.data; adamc@1323: if (ru.prefix ? s.indexOf(ru.pattern) == 0 : s == ru.pattern) adamc@1323: return ru.allow ? s : null; adamc@1323: } adamc@1323: adamc@1323: return null; adamc@1323: } adamc@1323: adamc@1323: function bless(s) { adamc@1323: u = checkUrl(s); adamc@1323: if (u == null) adamc@1323: er("Disallowed URL: " + s); adamc@1323: return u; adamc@1323: } adamc@1323: adamc@693: // App-specific code adamc@693: