adamc@1036: // Detect browser quirks that we should be aware of. adamc@1036: adamc@1036: function needsDynPrefix() { adamc@1036: var span = document.createElement("span"); adamc@1036: span.innerHTML = ""; adamc@1036: var scripts = span.getElementsByTagName("script"); adamc@1036: 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; } adamc@970: function mod(x, y) { return x % y; } 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@1061: function toLower(c) { return c.toLowercase(); } adamc@1061: function toUpper(c) { return c.toUppercase(); } adamc@1061: adamc@1061: adamc@692: // Lists adamc@692: adamc@580: function cons(v, ls) { adamc@690: return { next : ls, data : v }; adamc@690: } adamc@969: function rev(ls) { adamc@969: var acc = null; adamc@969: for (; ls; ls = ls.next) adamc@969: acc = cons(ls.data, acc); adamc@969: return acc; adamc@969: } adamc@690: function concat(ls1, ls2) { adamc@969: var acc = ls2; adamc@969: ls1 = rev(ls1); adamc@969: for (; ls1; ls1 = ls1.next) adamc@969: acc = cons(ls1.data, acc); adamc@969: return acc; adamc@690: } adamc@690: function member(x, ls) { adamc@690: for (; ls; ls = ls.next) adamc@690: if (ls.data == x) adamc@690: return true; adamc@690: return false; adamc@690: } adamc@690: function remove(x, ls) { adamc@969: var acc = null; adamc@969: adamc@969: for (; ls; ls = ls.next) adamc@969: if (ls.data == x) adamc@969: return concat(acc, ls.next); adamc@969: else adamc@969: acc = cons(ls.data, acc); adamc@969: adamc@969: return ls; adamc@690: } adamc@690: function union(ls1, ls2) { adamc@969: var acc = ls2; adamc@969: adamc@969: for (; ls1; ls1 = ls1.next) adamc@969: if (!member(ls1.data, ls2)) adamc@969: acc = cons(ls1.data, acc); adamc@969: adamc@969: return acc; adamc@580: } adamc@703: function length(ls) { adamc@969: var acc = 0; adamc@969: adamc@969: for (; ls; ls = ls.next) adamc@969: ++acc; adamc@969: adamc@969: return acc; adamc@703: } adamc@670: adamc@690: adamc@729: // Error handling adamc@729: adamc@729: function whine(msg) { adamc@729: alert(msg); adamc@729: throw msg; adamc@729: } adamc@729: adamc@810: function pf(loc) { adamc@835: throw ("Pattern match failure (" + loc + ")"); adamc@729: } adamc@729: adamc@798: function runHandlers(kind, ls, arg) { adamc@798: if (ls == null) adamc@798: alert(kind + ": " + arg); adamc@729: for (; ls; ls = ls.next) adamc@729: try { adamc@976: exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}}); adamc@729: } catch (v) { } adamc@729: } adamc@729: adamc@729: var errorHandlers = null; adamc@729: adamc@729: function onError(f) { adamc@729: errorHandlers = cons(f, errorHandlers); adamc@729: } adamc@729: adamc@729: function er(s) { adamc@798: runHandlers("Error", errorHandlers, s); adamc@729: throw {uw_error: s}; adamc@729: } adamc@729: adamc@729: var failHandlers = null; adamc@729: adamc@729: function onFail(f) { adamc@729: failHandlers = cons(f, failHandlers); adamc@729: } adamc@729: adamc@729: function doExn(v) { adamc@729: if (v == null || v.uw_error == null) { adamc@1039: var s = (v == null ? "null" : v.message ? v.message : v.toString()); adamc@969: if (v != null && v.fileName && v.lineNumber) adamc@969: s += " (" + v.fileName + ":" + v.lineNumber + ")"; adamc@798: runHandlers("Fail", failHandlers, s); adamc@729: } adamc@729: } adamc@729: adamc@729: var disconnectHandlers = null; adamc@729: adamc@976: function flift(f) { adamc@977: return {env:cons(f,null), body:{c:"v", n:1}}; adamc@976: } adamc@976: adamc@729: function onDisconnect(f) { adamc@976: disconnectHandlers = cons(flift(f), disconnectHandlers); adamc@729: } adamc@729: adamc@729: function discon() { adamc@798: runHandlers("Disconnect", disconnectHandlers, null); adamc@729: } adamc@729: adamc@729: var connectHandlers = null; adamc@729: adamc@729: function onConnectFail(f) { adamc@976: connectHandlers = cons(flift(f), connectHandlers); adamc@729: } adamc@729: adamc@729: function conn() { adamc@798: runHandlers("Connect", connectHandlers, null); adamc@729: } adamc@729: adamc@729: var serverHandlers = null; adamc@729: adamc@729: function onServerError(f) { adamc@729: serverHandlers = cons(f, serverHandlers); adamc@729: } adamc@729: adamc@729: function servErr(s) { adamc@824: 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@693: return {closure: f}; adamc@693: } adamc@693: adamc@693: function isWeird(v) { adamc@693: return v.closure != null || v.cat1 != null; adamc@693: } adamc@693: adamc@692: function cat(s1, s2) { adamc@693: if (isWeird(s1) || isWeird(s2)) adamc@693: return {cat1: s1, cat2: s2}; adamc@693: else adamc@692: return s1 + s2; adamc@692: } adamc@692: adamc@692: var closures = []; adamc@703: var freeClosures = null; adamc@692: adamc@692: function newClosure(f) { adamc@703: var n; adamc@703: if (freeClosures == null) { adamc@703: n = closures.length; adamc@703: } else { adamc@703: n = freeClosures.data; adamc@703: freeClosures = freeClosures.next; adamc@703: } adamc@692: closures[n] = f; adamc@692: return n; adamc@692: } adamc@692: adamc@703: function freeClosure(n) { adamc@703: closures[n] = null; adamc@703: freeClosures = cons(n, freeClosures); adamc@703: } adamc@703: adamc@692: function cr(n) { adamc@975: return closures[n]; adamc@692: } adamc@692: adamc@703: function flatten(cls, tr) { adamc@693: if (tr.cat1 != null) adamc@703: return flatten(cls, tr.cat1) + flatten(cls, tr.cat2); adamc@703: else if (tr.closure != null) { adamc@703: var cl = newClosure(tr.closure); adamc@703: cls.v = cons(cl, cls.v); adamc@703: return "cr(" + cl + ")"; adamc@703: } else adamc@692: return tr; adamc@692: } adamc@692: adamc@728: function flattenLocal(s) { adamc@728: var cls = {v : null}; adamc@728: var r = flatten(cls, s); adamc@728: for (cl = cls.v; cl != null; cl = cl.next) adamc@728: freeClosure(cl.data); adamc@728: return r; adamc@728: } adamc@728: adamc@692: adamc@692: adamc@692: // Dynamic tree management adamc@692: adamc@690: function populate(node) { adamc@690: var s = node.signal; adamc@690: var oldSources = node.sources; adamc@729: try { adamc@977: var sr = execF(s, null); adamc@976: var newSources = sr._sources; adamc@690: adamc@729: for (var sp = oldSources; sp; sp = sp.next) adamc@729: if (!member(sp.data, newSources)) adamc@729: sp.data.dyns = remove(node, sp.data.dyns); adamc@690: adamc@729: for (var sp = newSources; sp; sp = sp.next) adamc@729: if (!member(sp.data, oldSources)) adamc@729: sp.data.dyns = cons(node, sp.data.dyns); adamc@690: adamc@729: node.sources = newSources; adamc@976: node.recreate(sr._data); adamc@729: } catch (v) { adamc@729: doExn(v); adamc@729: } adamc@579: } adamc@574: adamc@580: function sc(v) { adamc@690: return {data : v, dyns : null}; adamc@580: } adamc@580: function sv(s, v) { adamc@690: s.data = v; adamc@967: adamc@690: for (var ls = s.dyns; ls; ls = ls.next) adamc@690: if (!ls.dead) adamc@690: populate(ls.data); adamc@580: } adamc@601: function sg(s) { adamc@690: return s.data; adamc@601: } adamc@579: adamc@580: function ss(s) { adamc@976: return {env:cons(s, null), body:{c:"r", l: adamc@976: cons({n:"sources", v:{c:"c", v:cons(s, null)}}, adamc@976: cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}}; adamc@580: } adamc@580: function sr(v) { adamc@976: return {env:null, body:{c:"c", v:{_sources : null, _data : v}}}; adamc@580: } adamc@580: function sb(x,y) { adamc@976: return {env:cons(y,cons(x,null)), adamc@976: body:{c:"=", adamc@977: e1:{c:"a", f:{c:"v", n:2}, x:{c:"c", v:null}}, adamc@976: e2:{c:"=", adamc@976: e1:{c:"a", adamc@977: f:{c:"a", f:{c:"v", n:2}, x:{c:".", r:{c:"v", n:0}, f:"data"}}, adamc@976: x:{c:"c", v:null}}, adamc@976: e2:{c:"r", l:cons( adamc@976: {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"}, adamc@976: cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}}, adamc@976: cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}}; adamc@580: } adamc@841: function scur(s) { adamc@982: return execF(s, null)._data; adamc@841: } adamc@571: adamc@604: function lastParent() { adamc@604: var pos = document; adamc@604: adamc@600: while (pos.lastChild && pos.lastChild.nodeType == 1) adamc@600: pos = pos.lastChild; adamc@600: adamc@1039: pos = pos.parentNode; adamc@1039: adamc@1039: return pos; adamc@600: } adamc@600: adamc@898: function parent() { adamc@898: return thisScript ? thisScript.parentNode : lastParent(); adamc@898: } adamc@898: adamc@604: function addNode(node) { adamc@899: if (thisScript) adamc@899: thisScript.parentNode.replaceChild(node, thisScript); adamc@899: else adamc@604: lastParent().appendChild(node); adamc@603: } adamc@603: adamc@690: var thisScript = null; adamc@690: adamc@604: function runScripts(node) { adamc@899: if (node.getElementsByTagName) { adamc@899: var savedScript = thisScript; adamc@603: adamc@899: var scripts = node.getElementsByTagName("script"), scriptsCopy = []; adamc@899: var len = scripts.length; adamc@899: for (var i = 0; i < len; ++i) adamc@899: scriptsCopy[i] = scripts[i]; adamc@899: for (var i = 0; i < len; ++i) { adamc@899: thisScript = scriptsCopy[i]; adamc@1039: adamc@899: try { adamc@1036: eval(thisScript.text); adamc@899: } catch (v) { adamc@899: doExn(v); adamc@899: } adamc@899: if (thisScript.parentNode) adamc@901: thisScript.parentNode.removeChild(thisScript); adamc@729: } adamc@899: adamc@899: thisScript = savedScript; adamc@604: } adamc@603: } adamc@603: adamc@603: adamc@692: // Dynamic tree entry points adamc@692: adamc@967: function killScript(scr) { adamc@967: scr.dead = true; adamc@967: for (var ls = scr.sources; ls; ls = ls.next) adamc@967: ls.data.dyns = remove(scr, ls.data.dyns); adamc@967: for (var ls = scr.closures; ls; ls = ls.next) adamc@967: freeClosure(ls.data); adamc@967: } adamc@967: adamc@901: function dyn(pnode, s) { adamc@901: var x = document.createElement("script"); adamc@690: x.dead = false; adamc@690: x.signal = s; adamc@690: x.sources = null; adamc@703: x.closures = null; adamc@899: adamc@902: var firstChild = null; adamc@899: adamc@690: x.recreate = function(v) { adamc@703: for (var ls = x.closures; ls; ls = ls.next) adamc@703: freeClosure(ls.data); adamc@692: adamc@902: var next; adamc@902: for (var child = firstChild; child && child != x; child = next) { adamc@902: next = child.nextSibling; adamc@1039: adamc@967: killScript(child); adamc@901: if (child.getElementsByTagName) { adamc@901: var arr = child.getElementsByTagName("script"); adamc@967: for (var i = 0; i < arr.length; ++i) adamc@967: killScript(arr[i]); adamc@847: } adamc@847: adamc@901: if (child.parentNode) adamc@901: child.parentNode.removeChild(child); adamc@899: } adamc@690: adamc@703: var cls = {v : null}; adamc@1039: var html = flatten(cls, v); adamc@1039: if (pnode != "table" && pnode != "tr") adamc@1039: html = dynPrefix + html; adamc@703: x.closures = cls.v; adamc@899: adamc@901: if (pnode == "table") { adamc@901: var dummy = document.createElement("body"); adamc@901: dummy.innerHTML = "" + html + "
"; adamc@901: runScripts(dummy); adamc@901: var table = x.parentNode; adamc@901: adamc@946: if (table) { adamc@946: firstChild = null; adamc@946: var tbody; adamc@1039: adamc@1039: var arr = dummy.getElementsByTagName("tbody"); adamc@1039: adamc@1039: var tbody; adamc@1039: if (arr.length > 0 && arr[0].parentNode == dummy.firstChild) { adamc@946: tbody = arr[0]; adamc@1039: var next; adamc@1039: for (var node = dummy.firstChild.firstChild; node; node = next) { adamc@1039: next = node.nextSibling; adamc@1039: adamc@1039: if (node.tagName != "TBODY") adamc@1039: tbody.appendChild(node); adamc@1039: } adamc@1039: } else adamc@946: tbody = dummy.firstChild; adamc@945: adamc@946: var next; adamc@946: firstChild = document.createElement("script"); adamc@946: table.insertBefore(firstChild, x); adamc@946: for (var node = tbody.firstChild; node; node = next) { adamc@946: next = node.nextSibling; adamc@946: table.insertBefore(node, x); adamc@946: } adamc@901: } adamc@903: } else if (pnode == "tr") { adamc@903: var dummy = document.createElement("body"); adamc@903: dummy.innerHTML = "" + html + "
"; adamc@903: runScripts(dummy); adamc@903: var table = x.parentNode; adamc@903: adamc@946: if (table) { adamc@946: var arr = dummy.getElementsByTagName("tr"); adamc@946: firstChild = null; adamc@1039: var tr; adamc@946: if (arr.length > 0 && table != null) adamc@1039: tr = arr[0]; adamc@946: else adamc@1039: tr = dummy.firstChild; adamc@946: adamc@946: var next; adamc@903: firstChild = document.createElement("script"); adamc@903: table.insertBefore(firstChild, x); adamc@1039: for (var node = tr.firstChild; node; node = next) { adamc@903: next = node.nextSibling; adamc@903: table.insertBefore(node, x); adamc@903: } adamc@903: } adamc@901: } else { adamc@902: firstChild = document.createElement("span"); adamc@902: firstChild.innerHTML = html; adamc@902: runScripts(firstChild); adamc@900: if (x.parentNode) adamc@902: x.parentNode.insertBefore(firstChild, x); adamc@899: } adamc@690: }; adamc@899: adamc@899: addNode(x); adamc@690: populate(x); adamc@571: } adamc@582: adamc@1040: function input(x, s, recreate, type) { adamc@1039: if (type) x.type = type; adamc@690: x.dead = false; adamc@690: x.signal = ss(s); adamc@690: x.sources = null; adamc@817: x.recreate = recreate(x); adamc@899: addNode(x); adamc@690: populate(x); adamc@816: adamc@816: return x; adamc@816: } adamc@816: adamc@816: function inp(s) { adamc@1040: var x = input(document.createElement("input"), s, adamc@1040: function(x) { return function(v) { if (x.value != v) x.value = v; }; }); adamc@816: x.value = s.data; adamc@816: x.onkeyup = function() { sv(s, x.value) }; adamc@816: adamc@816: return x; adamc@816: } adamc@816: adamc@816: function sel(s, content) { adamc@1040: var dummy = document.createElement("span"); adamc@1040: dummy.innerHTML = ""; adamc@1040: var x = input(dummy.firstChild, s, function(x) { return function(v) { if (x.value != v) x.value = v; }; }); adamc@816: x.value = s.data; adamc@835: if (x.value != s.data) adamc@835: sv(s, x.value); adamc@816: x.onchange = function() { sv(s, x.value) }; adamc@797: adamc@606: return x; adamc@598: } adamc@598: adamc@817: function chk(s) { adamc@1040: var x = input(document.createElement("input"), s, adamc@1040: function(x) { return function(v) { if (x.checked != v) x.checked = v; }; }, "checkbox"); adamc@1041: x.defaultChecked = x.checked = s.data; adamc@1041: x.onclick = function() { sv(s, x.checked) }; adamc@817: adamc@817: return x; adamc@817: } adamc@817: adamc@800: function addOnChange(x, f) { adamc@800: var old = x.onchange; adamc@800: x.onchange = function() { old(); f (); }; adamc@800: } adamc@800: adamc@692: adamc@692: // Basic string operations adamc@692: adamc@597: function eh(x) { adamc@800: if (x == null) adamc@800: return "NULL"; adamc@800: else adamc@800: 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@829: var r = s.indexOf(ch); adamc@829: if (r == -1) adamc@829: return null; adamc@829: else adamc@829: return r; adamc@829: } adamc@831: function sspn(s, chs) { adamc@831: for (var i = 0; i < s.length; ++i) adamc@1038: if (chs.indexOf(s.charAt(i)) != -1) adamc@831: return i; adamc@831: adamc@831: return null; adamc@831: } adamc@829: function schr(s, ch) { adamc@829: var r = s.indexOf(ch); adamc@829: if (r == -1) adamc@829: return null; adamc@829: else adamc@829: return s.substring(r); adamc@829: } adamc@829: function ssub(s, start, len) { adamc@829: return s.substring(start, start+len); adamc@829: } adamc@821: adamc@649: function pi(s) { adamc@649: var r = parseInt(s); adamc@649: if (r.toString() == s) adamc@649: return r; adamc@649: else adamc@729: er("Can't parse int: " + s); adamc@649: } adamc@649: adamc@649: function pfl(s) { adamc@649: var r = parseFloat(s); adamc@649: if (r.toString() == s) adamc@649: return r; adamc@649: else adamc@729: er("Can't parse float: " + s); adamc@649: } adamc@649: adamc@838: function pio(s) { adamc@838: var r = parseInt(s); adamc@838: if (r.toString() == s) adamc@838: return r; adamc@838: else adamc@838: return null; adamc@838: } adamc@838: adamc@838: function pflo(s) { adamc@838: var r = parseFloat(s); adamc@838: if (r.toString() == s) adamc@838: return r; adamc@838: else adamc@838: return null; adamc@838: } adamc@838: adamc@692: function uf(s) { adamc@925: if (s.length == 0) adamc@925: return "_"; adamc@1038: return (s.charAt(0) == '_' ? "_" : "") adamc@925: + escape(s).replace(new RegExp ("/", "g"), "%2F").replace(new RegExp ("\\+", "g"), "%2B"); adamc@691: } adamc@691: adamc@692: function uu(s) { adamc@1038: if (s.length > 0 && s.charAt(0) == '_') { adamc@925: s = s.substring(1); adamc@1038: } else if (s.length >= 3 && s.charAt(0) == '%' && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F')) adamc@927: s = s.substring(3); adamc@905: return unescape(s.replace(new RegExp ("\\+", "g"), " ")); adamc@905: } adamc@905: adamc@912: function ub(b) { adamc@912: return b ? "1" : "0"; adamc@912: } adamc@912: adamc@905: function uul(getToken, getData) { adamc@905: var tok = getToken(); adamc@905: if (tok == "Nil") { adamc@905: return null; adamc@905: } else if (tok == "Cons") { adamc@905: var d = getData(); adamc@905: var l = uul(getToken, getData); adamc@905: return {_1:d, _2:l}; adamc@905: } else adamc@982: whine("Can't unmarshal list (" + tok + ")"); adamc@692: } adamc@692: adamc@961: function strcmp(str1, str2) { adamc@961: 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@609: return new XMLHttpRequest(); adamc@609: } catch (e) { adamc@609: try { adamc@609: return new ActiveXObject("Msxml2.XMLHTTP"); adamc@609: } catch (e) { adamc@609: try { adamc@609: return new ActiveXObject("Microsoft.XMLHTTP"); adamc@609: } catch (e) { adamc@982: whine("Your browser doesn't seem to support AJAX."); adamc@609: } adamc@609: } 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@1042: unloading = true; adamc@1042: adamc@1042: for (; inFlight; inFlight = inFlight.next) { adamc@1042: inFlight.data.abort(); adamc@1042: } adamc@1042: } adamc@1042: adamc@736: function requestUri(xhr, uri, needsSig) { adamc@1042: if (unloading) adamc@1042: return; adamc@1042: adamc@1037: xhr.open("POST", uri, true); adamc@668: adamc@703: if (client_id != null) { adamc@668: xhr.setRequestHeader("UrWeb-Client", client_id.toString()); adamc@668: xhr.setRequestHeader("UrWeb-Pass", client_pass.toString()); adamc@668: } adamc@668: adamc@736: if (needsSig) { adamc@736: if (sig == null) adamc@736: whine("Missing cookie signature!"); adamc@736: adamc@736: xhr.setRequestHeader("UrWeb-Sig", sig); adamc@736: } adamc@736: adamc@1042: inFlight = cons(xhr, inFlight); adamc@668: xhr.send(null); adamc@668: } adamc@668: adamc@1042: function xhrFinished(xhr) { adamc@1042: xhr.abort(); adamc@1042: inFlight = remove(xhr, inFlight); adamc@1042: } adamc@1042: adamc@978: function rc(prefix, uri, parse, k, needsSig) { adamc@978: uri = cat(prefix, uri); adamc@728: uri = flattenLocal(uri); adamc@609: var xhr = getXHR(); adamc@609: adamc@609: xhr.onreadystatechange = function() { adamc@612: if (xhr.readyState == 4) { adamc@612: var isok = false; adamc@612: adamc@612: try { adamc@612: if (xhr.status == 200) adamc@612: isok = true; adamc@612: } catch (e) { } adamc@612: adamc@729: if (isok) { adamc@729: try { adamc@1020: k(parse(xhr.responseText)); adamc@729: } catch (v) { adamc@729: doExn(v); adamc@729: } adamc@729: } else { adamc@729: conn(); adamc@649: } adamc@1042: adamc@1042: xhrFinished(xhr); adamc@612: } adamc@609: }; adamc@609: adamc@736: requestUri(xhr, uri, needsSig); adamc@609: } adamc@667: adamc@667: function path_join(s1, s2) { adamc@1038: if (s1.length > 0 && s1.charAt(s1.length-1) == '/') adamc@667: return s1 + s2; adamc@667: else adamc@667: return s1 + "/" + s2; adamc@667: } adamc@667: adamc@670: var channels = []; adamc@670: adamc@670: function newQueue() { adamc@670: return { front : null, back : null }; adamc@670: } adamc@670: function enqueue(q, v) { adamc@670: if (q.front == null) { adamc@670: q.front = cons(v, null); adamc@670: q.back = q.front; adamc@670: } else { adamc@670: var node = cons(v, null); adamc@690: q.back.next = node; adamc@670: q.back = node; adamc@670: } adamc@670: } adamc@670: function dequeue(q) { adamc@670: if (q.front == null) adamc@670: return null; adamc@670: else { adamc@690: var r = q.front.data; adamc@690: q.front = q.front.next; adamc@670: if (q.front == null) adamc@670: q.back = null; adamc@670: return r; adamc@670: } adamc@670: } adamc@670: adamc@670: function newChannel() { adamc@670: return { msgs : newQueue(), listeners : newQueue() }; adamc@670: } adamc@670: adamc@667: function listener() { adamc@668: var uri = path_join(url_prefix, ".msgs"); adamc@667: var xhr = getXHR(); adamc@673: var tid, orsc, onTimeout; adamc@673: adamc@673: var connect = function () { adamc@673: xhr.onreadystatechange = orsc; adamc@673: tid = window.setTimeout(onTimeout, timeout * 500); adamc@736: requestUri(xhr, uri, false); adamc@673: } adamc@673: adamc@673: orsc = function() { adamc@667: if (xhr.readyState == 4) { adamc@673: window.clearTimeout(tid); adamc@673: adamc@667: var isok = false; adamc@667: adamc@667: try { adamc@667: if (xhr.status == 200) adamc@667: isok = true; adamc@667: } catch (e) { } adamc@667: adamc@668: if (isok) { adamc@889: var text = xhr.responseText adamc@889: if (text == "") adamc@889: return; adamc@889: var lines = text.split("\n"); adamc@889: adamc@729: if (lines.length < 2) { adamc@729: discon(); adamc@729: return; adamc@729: } adamc@669: adamc@669: for (var i = 0; i+1 < lines.length; i += 2) { adamc@670: var chn = lines[i]; adamc@670: var msg = lines[i+1]; adamc@670: adamc@670: if (chn < 0) adamc@670: whine("Out-of-bounds channel in message from remote server"); adamc@670: adamc@670: var ch; adamc@670: adamc@670: if (chn >= channels.length || channels[chn] == null) { adamc@670: ch = newChannel(); adamc@670: channels[chn] = ch; adamc@670: } else adamc@670: ch = channels[chn]; adamc@670: adamc@670: var listener = dequeue(ch.listeners); adamc@670: if (listener == null) { adamc@670: enqueue(ch.msgs, msg); adamc@670: } else { adamc@728: try { adamc@728: listener(msg); adamc@728: } catch (v) { adamc@728: doExn(v); adamc@728: } adamc@670: } adamc@669: } adamc@669: adamc@1042: xhrFinished(xhr); adamc@1038: adamc@673: connect(); adamc@668: } adamc@667: else { adamc@729: try { adamc@824: if (xhr.status != 0) adamc@824: servErr("Error querying remote server for messages: " + xhr.status); adamc@889: } catch (e) { } adamc@667: } adamc@667: } adamc@667: }; adamc@667: adamc@673: onTimeout = function() { adamc@1042: xhrFinished(xhr); adamc@673: connect(); adamc@673: }; adamc@673: adamc@673: connect(); adamc@667: } adamc@670: adamc@670: function rv(chn, parse, k) { adamc@682: if (chn == null) adamc@682: return; adamc@682: adamc@670: if (chn < 0) adamc@670: whine("Out-of-bounds channel receive"); adamc@670: adamc@670: var ch; adamc@670: adamc@670: if (chn >= channels.length || channels[chn] == null) { adamc@670: ch = newChannel(); adamc@670: channels[chn] = ch; adamc@670: } else adamc@670: ch = channels[chn]; adamc@670: adamc@670: var msg = dequeue(ch.msgs); adamc@670: if (msg == null) { adamc@1021: enqueue(ch.listeners, function(msg) { k(parse(msg)); }); adamc@670: } else { adamc@728: try { adamc@1021: k(parse(msg)); adamc@728: } catch (v) { adamc@728: doExn(v); adamc@728: } adamc@670: } adamc@670: } adamc@693: adamc@978: function sl(ms, k) { adamc@1021: window.setTimeout(function() { k(null); }, ms); adamc@1021: } adamc@1021: adamc@1021: function sp(e) { adamc@1021: 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() { adamc@970: return window.event ? uw_event.keyCode : uw_event.which; 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@970: while (env != null) { adamc@970: if (n == 0) adamc@970: return env.data; adamc@970: else { adamc@970: --n; adamc@970: env = env.next; adamc@970: } adamc@970: } adamc@970: adamc@982: whine("Out-of-bounds Ur variable reference"); adamc@970: } adamc@970: adamc@974: function execP(env, p, v) { adamc@974: switch (p.c) { adamc@974: case "w": adamc@974: return env; adamc@974: case "v": adamc@974: return cons(v, env); adamc@974: case "c": adamc@974: if (v == p.v) adamc@974: return env; adamc@974: else adamc@974: return false; adamc@974: case "s": adamc@974: if (v == null) adamc@974: return false; adamc@974: else adamc@974: return execP(env, p.p, p.n ? v.v : v); adamc@974: case "1": adamc@974: if (v.n != p.n) adamc@974: return false; adamc@974: else adamc@974: return execP(env, p.p, v.v); adamc@974: case "r": adamc@974: for (var fs = p.l; fs != null; fs = fs.next) { adamc@974: env = execP(env, fs.data.p, v["_" + fs.data.n]); adamc@974: if (env == false) adamc@974: return false; adamc@974: } adamc@974: return env; adamc@974: default: adamc@982: whine("Unknown Ur pattern kind" + p.c); adamc@974: } adamc@974: } adamc@974: adamc@970: function exec0(env, e) { adamc@1020: return exec1(env, null, e); adamc@1020: } adamc@1020: adamc@1020: function exec1(env, stack, e) { adamc@1020: var stack, usedK = false; adamc@970: adamc@983: var saveEnv = function() { adamc@983: if (stack.next != null && stack.next.data.c != "<") adamc@983: stack = cons({c: "<", env: env}, stack.next); adamc@983: else adamc@983: stack = stack.next; adamc@983: }; adamc@983: adamc@970: while (true) { adamc@970: switch (e.c) { adamc@970: case "c": adamc@970: var v = e.v; adamc@970: if (stack == null) adamc@970: return v; adamc@970: var fr = stack.data; adamc@970: adamc@970: switch (fr.c) { adamc@970: case "s": adamc@970: e = {c: "c", v: {v: v}}; adamc@970: stack = stack.next; adamc@970: break; adamc@970: case "1": adamc@970: e = {c: "c", v: {n: fr.n, v: v}}; adamc@970: stack = stack.next; adamc@970: break; adamc@970: case "f": adamc@970: fr.args[fr.pos++] = v; adamc@970: if (fr.a == null) { adamc@1035: var res; adamc@1020: stack = stack.next; adamc@1035: adamc@1035: if (fr.f.apply) adamc@1035: res = fr.f.apply(null, fr.args); adamc@1035: else if (fr.args.length == 0) adamc@1035: res = fr.f(); adamc@1035: else if (fr.args.length == 1) adamc@1035: res = fr.f(fr.args[0]); adamc@1035: else if (fr.args.length == 2) adamc@1035: res = fr.f(fr.args[0], fr.args[1]); adamc@1035: else if (fr.args.length == 3) adamc@1035: res = fr.f(fr.args[0], fr.args[1], fr.args[2]); adamc@1035: else if (fr.args.length == 4) adamc@1035: res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]); adamc@1035: else if (fr.args.length == 5) adamc@1035: res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]); adamc@1035: else adamc@1035: whine("Native function has " + fr.args.length + " args, but there is no special case for that count."); adamc@1035: adamc@1035: e = {c: "c", v: res}; adamc@1020: if (usedK) return null; adamc@970: } else { adamc@970: e = fr.a.data; adamc@970: fr.a = fr.a.next; adamc@970: } adamc@970: break; adamc@970: case "a1": adamc@970: e = fr.x; adamc@971: stack = cons({c: "a2", f: v}, stack.next); adamc@970: break; adamc@970: case "a2": adamc@971: if (fr.f == null) adamc@982: whine("Ur: applying null function"); adamc@971: else if (fr.f.body) { adamc@983: saveEnv(); adamc@971: env = cons(v, fr.f.env); adamc@971: e = fr.f.body; adamc@971: } else { adamc@971: e = {c: "c", v: fr.f(v)}; adamc@971: stack = stack.next; adamc@971: } adamc@970: break; adamc@978: case "<": adamc@970: env = fr.env; adamc@970: stack = stack.next; adamc@970: break; adamc@970: case "r": adamc@970: fr.fs["_" + fr.n] = v; adamc@970: if (fr.l == null) { adamc@970: e = {c: "c", v: fr.fs}; adamc@970: stack = stack.next; adamc@970: } else { adamc@970: fr.n = fr.l.data.n; adamc@970: e = fr.l.data.v; adamc@970: fr.l = fr.l.next; adamc@970: } adamc@970: break; adamc@970: case ".": adamc@970: e = {c: "c", v: v["_" + fr.f]}; adamc@970: stack = stack.next; adamc@970: break; adamc@970: case ";": adamc@970: e = fr.e2; adamc@970: stack = stack.next; adamc@970: break; adamc@970: case "=": adamc@983: saveEnv(); adamc@970: env = cons(v, env); adamc@970: e = fr.e2; adamc@970: break; adamc@974: case "m": adamc@974: var ps; adamc@974: for (ps = fr.p; ps != null; ps = ps.next) { adamc@974: var r = execP(env, ps.data.p, v); adamc@974: if (r != false) { adamc@983: saveEnv(); adamc@974: env = r; adamc@974: e = ps.data.b; adamc@974: break; adamc@974: } adamc@974: } adamc@974: if (ps == null) adamc@982: whine("Match failure in Ur interpretation"); adamc@974: break; adamc@970: default: adamc@982: whine("Unknown Ur continuation kind " + fr.c); adamc@970: } adamc@970: adamc@970: break; adamc@970: case "v": adamc@970: e = {c: "c", v: lookup(env, e.n)}; adamc@970: break; adamc@970: case "n": adamc@971: e = urfuncs[e.n]; adamc@970: break; adamc@970: case "s": adamc@970: stack = cons({c: "s"}, stack); adamc@970: e = e.v; adamc@970: break; adamc@970: case "1": adamc@970: stack = cons({c: "1", n: e.n}, stack); adamc@970: e = e.v; adamc@970: break; adamc@970: case "f": adamc@970: if (e.a == null) adamc@1057: e = {c: "c", v: (eval(e.f))()}; adamc@970: else { adamc@970: var args = []; adamc@1057: stack = cons({c: "f", f: eval(e.f), args: args, pos: 0, a: e.a.next}, stack); adamc@982: if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data); adamc@970: e = e.a.data; adamc@970: } adamc@970: break; adamc@970: case "l": adamc@970: e = {c: "c", v: {env: env, body: e.b}}; adamc@970: break; adamc@970: case "a": adamc@970: stack = cons({c: "a1", x: e.x}, stack); adamc@970: e = e.f; adamc@970: break; adamc@970: case "r": adamc@970: if (e.l == null) adamc@982: whine("Empty Ur record in interpretation"); adamc@970: var fs = {}; adamc@970: stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack); adamc@973: e = e.l.data.v; adamc@970: break; adamc@970: case ".": adamc@970: stack = cons({c: ".", f: e.f}, stack); adamc@970: e = e.r; adamc@970: break; adamc@970: case ";": adamc@970: stack = cons({c: ";", e2: e.e2}, stack); adamc@970: e = e.e1; adamc@970: break; adamc@970: case "=": adamc@970: stack = cons({c: "=", e2: e.e2}, stack); adamc@970: e = e.e1; adamc@970: break; adamc@974: case "m": adamc@974: stack = cons({c: "m", p: e.p}, stack); adamc@974: e = e.e; adamc@974: break; adamc@970: case "e": adamc@975: e = {c: "c", v: cs({c: "wc", env: env, body: e.e})}; adamc@975: break; adamc@975: case "wc": adamc@975: env = e.env; adamc@975: e = e.body; adamc@970: break; adamc@1020: case "K": adamc@1020: { var savedStack = stack.next, savedEnv = env; adamc@1020: e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };} adamc@1020: usedK = true; adamc@1020: break; adamc@970: default: adamc@982: whine("Unknown Ur expression kind " + e.c); adamc@970: } adamc@970: } adamc@970: } adamc@970: adamc@976: function execD(e) { adamc@976: return exec0(null, e); adamc@976: } adamc@976: adamc@970: function exec(e) { adamc@970: var r = exec0(null, e); adamc@970: adamc@975: if (r != null && r.body != null) adamc@970: return function(v) { return exec0(cons(v, r.env), r.body); }; adamc@970: else adamc@970: return r; adamc@895: } adamc@895: adamc@977: function execF(f, x) { adamc@977: return exec0(cons(x, f.env), f.body); adamc@977: } adamc@977: adamc@895: adamc@693: // App-specific code adamc@693: