annotate lib/js/urweb.js @ 974:b851675a2c3d

Compiled an 'option' pattern-match
author Adam Chlipala <adamc@hcoop.net>
date Tue, 22 Sep 2009 14:15:29 -0400
parents e30c2409c9d0
children 8fe576c0bee9
rev   line source
adamc@970 1 // Function versions of operators
adamc@970 2
adamc@970 3 function not(x) { return !x; }
adamc@970 4 function neg(x) { return -x; }
adamc@970 5
adamc@970 6 function eq(x, y) { return x == y; }
adamc@970 7 function plus(x, y) { return x + y; }
adamc@970 8 function minus(x, y) { return x - y; }
adamc@970 9 function times(x, y) { return x * y; }
adamc@970 10 function div(x, y) { return x / y; }
adamc@970 11 function mod(x, y) { return x % y; }
adamc@970 12 function lt(x, y) { return x < y; }
adamc@970 13 function le(x, y) { return x <= y; }
adamc@970 14
adamc@692 15 // Lists
adamc@692 16
adamc@580 17 function cons(v, ls) {
adamc@690 18 return { next : ls, data : v };
adamc@690 19 }
adamc@969 20 function rev(ls) {
adamc@969 21 var acc = null;
adamc@969 22 for (; ls; ls = ls.next)
adamc@969 23 acc = cons(ls.data, acc);
adamc@969 24 return acc;
adamc@969 25 }
adamc@690 26 function concat(ls1, ls2) {
adamc@969 27 var acc = ls2;
adamc@969 28 ls1 = rev(ls1);
adamc@969 29 for (; ls1; ls1 = ls1.next)
adamc@969 30 acc = cons(ls1.data, acc);
adamc@969 31 return acc;
adamc@690 32 }
adamc@690 33 function member(x, ls) {
adamc@690 34 for (; ls; ls = ls.next)
adamc@690 35 if (ls.data == x)
adamc@690 36 return true;
adamc@690 37 return false;
adamc@690 38 }
adamc@690 39 function remove(x, ls) {
adamc@969 40 var acc = null;
adamc@969 41
adamc@969 42 for (; ls; ls = ls.next)
adamc@969 43 if (ls.data == x)
adamc@969 44 return concat(acc, ls.next);
adamc@969 45 else
adamc@969 46 acc = cons(ls.data, acc);
adamc@969 47
adamc@969 48 return ls;
adamc@690 49 }
adamc@690 50 function union(ls1, ls2) {
adamc@969 51 var acc = ls2;
adamc@969 52
adamc@969 53 for (; ls1; ls1 = ls1.next)
adamc@969 54 if (!member(ls1.data, ls2))
adamc@969 55 acc = cons(ls1.data, acc);
adamc@969 56
adamc@969 57 return acc;
adamc@580 58 }
adamc@703 59 function length(ls) {
adamc@969 60 var acc = 0;
adamc@969 61
adamc@969 62 for (; ls; ls = ls.next)
adamc@969 63 ++acc;
adamc@969 64
adamc@969 65 return acc;
adamc@703 66 }
adamc@670 67
adamc@690 68
adamc@729 69 // Error handling
adamc@729 70
adamc@729 71 function whine(msg) {
adamc@729 72 alert(msg);
adamc@729 73 throw msg;
adamc@729 74 }
adamc@729 75
adamc@810 76 function pf(loc) {
adamc@835 77 throw ("Pattern match failure (" + loc + ")");
adamc@729 78 }
adamc@729 79
adamc@798 80 function runHandlers(kind, ls, arg) {
adamc@798 81 if (ls == null)
adamc@798 82 alert(kind + ": " + arg);
adamc@729 83 for (; ls; ls = ls.next)
adamc@729 84 try {
adamc@729 85 ls.data(arg)(null);
adamc@729 86 } catch (v) { }
adamc@729 87 }
adamc@729 88
adamc@729 89 var errorHandlers = null;
adamc@729 90
adamc@729 91 function onError(f) {
adamc@729 92 errorHandlers = cons(f, errorHandlers);
adamc@729 93 }
adamc@729 94
adamc@729 95 function er(s) {
adamc@798 96 runHandlers("Error", errorHandlers, s);
adamc@729 97 throw {uw_error: s};
adamc@729 98 }
adamc@729 99
adamc@729 100 var failHandlers = null;
adamc@729 101
adamc@729 102 function onFail(f) {
adamc@729 103 failHandlers = cons(f, failHandlers);
adamc@729 104 }
adamc@729 105
adamc@729 106 function doExn(v) {
adamc@729 107 if (v == null || v.uw_error == null) {
adamc@729 108 var s = (v == null ? "null" : v.toString());
adamc@969 109 if (v != null && v.fileName && v.lineNumber)
adamc@969 110 s += " (" + v.fileName + ":" + v.lineNumber + ")";
adamc@798 111 runHandlers("Fail", failHandlers, s);
adamc@729 112 }
adamc@729 113 }
adamc@729 114
adamc@729 115 var disconnectHandlers = null;
adamc@729 116
adamc@729 117 function onDisconnect(f) {
adamc@729 118 disconnectHandlers = cons(function (_){return f}, disconnectHandlers);
adamc@729 119 }
adamc@729 120
adamc@729 121 function discon() {
adamc@798 122 runHandlers("Disconnect", disconnectHandlers, null);
adamc@729 123 }
adamc@729 124
adamc@729 125 var connectHandlers = null;
adamc@729 126
adamc@729 127 function onConnectFail(f) {
adamc@729 128 connectHandlers = cons(function (_){return f}, connectHandlers);
adamc@729 129 }
adamc@729 130
adamc@729 131 function conn() {
adamc@798 132 runHandlers("Connect", connectHandlers, null);
adamc@729 133 }
adamc@729 134
adamc@729 135 var serverHandlers = null;
adamc@729 136
adamc@729 137 function onServerError(f) {
adamc@729 138 serverHandlers = cons(f, serverHandlers);
adamc@729 139 }
adamc@729 140
adamc@729 141 function servErr(s) {
adamc@824 142 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
adamc@729 143 }
adamc@729 144
adamc@729 145
adamc@692 146 // Embedding closures in XML strings
adamc@692 147
adamc@693 148 function cs(f) {
adamc@693 149 return {closure: f};
adamc@693 150 }
adamc@693 151
adamc@693 152 function isWeird(v) {
adamc@693 153 return v.closure != null || v.cat1 != null;
adamc@693 154 }
adamc@693 155
adamc@692 156 function cat(s1, s2) {
adamc@693 157 if (isWeird(s1) || isWeird(s2))
adamc@693 158 return {cat1: s1, cat2: s2};
adamc@693 159 else
adamc@692 160 return s1 + s2;
adamc@692 161 }
adamc@692 162
adamc@692 163 var closures = [];
adamc@703 164 var freeClosures = null;
adamc@692 165
adamc@692 166 function newClosure(f) {
adamc@703 167 var n;
adamc@703 168 if (freeClosures == null) {
adamc@703 169 n = closures.length;
adamc@703 170 } else {
adamc@703 171 n = freeClosures.data;
adamc@703 172 freeClosures = freeClosures.next;
adamc@703 173 }
adamc@692 174 closures[n] = f;
adamc@692 175 return n;
adamc@692 176 }
adamc@692 177
adamc@703 178 function freeClosure(n) {
adamc@703 179 closures[n] = null;
adamc@703 180 freeClosures = cons(n, freeClosures);
adamc@703 181 }
adamc@703 182
adamc@692 183 function cr(n) {
adamc@692 184 return closures[n]();
adamc@692 185 }
adamc@692 186
adamc@703 187 function flatten(cls, tr) {
adamc@693 188 if (tr.cat1 != null)
adamc@703 189 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
adamc@703 190 else if (tr.closure != null) {
adamc@703 191 var cl = newClosure(tr.closure);
adamc@703 192 cls.v = cons(cl, cls.v);
adamc@703 193 return "cr(" + cl + ")";
adamc@703 194 } else
adamc@692 195 return tr;
adamc@692 196 }
adamc@692 197
adamc@728 198 function flattenLocal(s) {
adamc@728 199 var cls = {v : null};
adamc@728 200 var r = flatten(cls, s);
adamc@728 201 for (cl = cls.v; cl != null; cl = cl.next)
adamc@728 202 freeClosure(cl.data);
adamc@728 203 return r;
adamc@728 204 }
adamc@728 205
adamc@692 206
adamc@692 207
adamc@692 208 // Dynamic tree management
adamc@692 209
adamc@690 210 function populate(node) {
adamc@690 211 var s = node.signal;
adamc@690 212 var oldSources = node.sources;
adamc@729 213 try {
adamc@729 214 var sr = s();
adamc@729 215 var newSources = sr.sources;
adamc@690 216
adamc@729 217 for (var sp = oldSources; sp; sp = sp.next)
adamc@729 218 if (!member(sp.data, newSources))
adamc@729 219 sp.data.dyns = remove(node, sp.data.dyns);
adamc@690 220
adamc@729 221 for (var sp = newSources; sp; sp = sp.next)
adamc@729 222 if (!member(sp.data, oldSources))
adamc@729 223 sp.data.dyns = cons(node, sp.data.dyns);
adamc@690 224
adamc@729 225 node.sources = newSources;
adamc@729 226 node.recreate(sr.data);
adamc@729 227 } catch (v) {
adamc@729 228 doExn(v);
adamc@729 229 }
adamc@579 230 }
adamc@574 231
adamc@580 232 function sc(v) {
adamc@690 233 return {data : v, dyns : null};
adamc@580 234 }
adamc@580 235 function sv(s, v) {
adamc@690 236 s.data = v;
adamc@967 237
adamc@690 238 for (var ls = s.dyns; ls; ls = ls.next)
adamc@690 239 if (!ls.dead)
adamc@690 240 populate(ls.data);
adamc@580 241 }
adamc@601 242 function sg(s) {
adamc@690 243 return s.data;
adamc@601 244 }
adamc@579 245
adamc@580 246 function ss(s) {
adamc@690 247 return function() { return {sources : cons(s, null), data : s.data } };
adamc@580 248 }
adamc@580 249 function sr(v) {
adamc@690 250 return function() { return {sources : null, data : v } };
adamc@580 251 }
adamc@580 252 function sb(x,y) {
adamc@690 253 return function() {
adamc@690 254 var xr = x();
adamc@690 255 var yr = y(xr.data)();
adamc@690 256 return {sources : union(xr.sources, yr.sources), data : yr.data};
adamc@690 257 };
adamc@580 258 }
adamc@841 259 function scur(s) {
adamc@841 260 return s().data;
adamc@841 261 }
adamc@571 262
adamc@604 263 function lastParent() {
adamc@604 264 var pos = document;
adamc@604 265
adamc@600 266 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@600 267 pos = pos.lastChild;
adamc@600 268
adamc@600 269 return pos.parentNode;
adamc@600 270 }
adamc@600 271
adamc@898 272 function parent() {
adamc@898 273 return thisScript ? thisScript.parentNode : lastParent();
adamc@898 274 }
adamc@898 275
adamc@604 276 function addNode(node) {
adamc@899 277 if (thisScript)
adamc@899 278 thisScript.parentNode.replaceChild(node, thisScript);
adamc@899 279 else
adamc@604 280 lastParent().appendChild(node);
adamc@603 281 }
adamc@603 282
adamc@690 283 var thisScript = null;
adamc@690 284
adamc@604 285 function runScripts(node) {
adamc@899 286 if (node.getElementsByTagName) {
adamc@899 287 var savedScript = thisScript;
adamc@603 288
adamc@899 289 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
adamc@899 290 var len = scripts.length;
adamc@899 291 for (var i = 0; i < len; ++i)
adamc@899 292 scriptsCopy[i] = scripts[i];
adamc@899 293 for (var i = 0; i < len; ++i) {
adamc@899 294 thisScript = scriptsCopy[i];
adamc@899 295 try {
adamc@899 296 eval(thisScript.textContent);
adamc@899 297 } catch (v) {
adamc@899 298 doExn(v);
adamc@899 299 }
adamc@899 300 if (thisScript.parentNode)
adamc@901 301 thisScript.parentNode.removeChild(thisScript);
adamc@729 302 }
adamc@899 303
adamc@899 304 thisScript = savedScript;
adamc@604 305 }
adamc@603 306 }
adamc@603 307
adamc@603 308
adamc@692 309 // Dynamic tree entry points
adamc@692 310
adamc@967 311 function killScript(scr) {
adamc@967 312 scr.dead = true;
adamc@967 313 for (var ls = scr.sources; ls; ls = ls.next)
adamc@967 314 ls.data.dyns = remove(scr, ls.data.dyns);
adamc@967 315 for (var ls = scr.closures; ls; ls = ls.next)
adamc@967 316 freeClosure(ls.data);
adamc@967 317 }
adamc@967 318
adamc@901 319 function dyn(pnode, s) {
adamc@901 320 var x = document.createElement("script");
adamc@690 321 x.dead = false;
adamc@690 322 x.signal = s;
adamc@690 323 x.sources = null;
adamc@703 324 x.closures = null;
adamc@899 325
adamc@902 326 var firstChild = null;
adamc@899 327
adamc@690 328 x.recreate = function(v) {
adamc@703 329 for (var ls = x.closures; ls; ls = ls.next)
adamc@703 330 freeClosure(ls.data);
adamc@692 331
adamc@902 332 var next;
adamc@902 333 for (var child = firstChild; child && child != x; child = next) {
adamc@902 334 next = child.nextSibling;
adamc@967 335 killScript(child);
adamc@901 336 if (child.getElementsByTagName) {
adamc@901 337 var arr = child.getElementsByTagName("script");
adamc@967 338 for (var i = 0; i < arr.length; ++i)
adamc@967 339 killScript(arr[i]);
adamc@847 340 }
adamc@847 341
adamc@901 342 if (child.parentNode)
adamc@901 343 child.parentNode.removeChild(child);
adamc@899 344 }
adamc@690 345
adamc@703 346 var cls = {v : null};
adamc@901 347 var html = flatten(cls, v);
adamc@703 348 x.closures = cls.v;
adamc@899 349
adamc@901 350 if (pnode == "table") {
adamc@901 351 var dummy = document.createElement("body");
adamc@901 352 dummy.innerHTML = "<table>" + html + "</table>";
adamc@901 353 runScripts(dummy);
adamc@901 354 var table = x.parentNode;
adamc@901 355
adamc@946 356 if (table) {
adamc@946 357 var arr = dummy.getElementsByTagName("tbody");
adamc@946 358 firstChild = null;
adamc@946 359 var tbody;
adamc@946 360 if (arr.length > 0 && table != null)
adamc@946 361 tbody = arr[0];
adamc@946 362 else
adamc@946 363 tbody = dummy.firstChild;
adamc@945 364
adamc@946 365 var next;
adamc@946 366 firstChild = document.createElement("script");
adamc@946 367 table.insertBefore(firstChild, x);
adamc@946 368 for (var node = tbody.firstChild; node; node = next) {
adamc@946 369 next = node.nextSibling;
adamc@946 370 table.insertBefore(node, x);
adamc@946 371 }
adamc@901 372 }
adamc@903 373 } else if (pnode == "tr") {
adamc@903 374 var dummy = document.createElement("body");
adamc@903 375 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
adamc@903 376 runScripts(dummy);
adamc@903 377 var table = x.parentNode;
adamc@903 378
adamc@946 379 if (table) {
adamc@946 380 var arr = dummy.getElementsByTagName("tr");
adamc@946 381 firstChild = null;
adamc@946 382 var tbody;
adamc@946 383 if (arr.length > 0 && table != null)
adamc@946 384 tbody = arr[0];
adamc@946 385 else
adamc@946 386 tbody = dummy.firstChild;
adamc@946 387
adamc@946 388 var next;
adamc@903 389 firstChild = document.createElement("script");
adamc@903 390 table.insertBefore(firstChild, x);
adamc@903 391 for (var node = tbody.firstChild; node; node = next) {
adamc@903 392 next = node.nextSibling;
adamc@903 393 table.insertBefore(node, x);
adamc@903 394 }
adamc@903 395 }
adamc@901 396 } else {
adamc@902 397 firstChild = document.createElement("span");
adamc@902 398 firstChild.innerHTML = html;
adamc@902 399 runScripts(firstChild);
adamc@900 400 if (x.parentNode)
adamc@902 401 x.parentNode.insertBefore(firstChild, x);
adamc@899 402 }
adamc@690 403 };
adamc@899 404
adamc@899 405 addNode(x);
adamc@690 406 populate(x);
adamc@571 407 }
adamc@582 408
adamc@817 409 function input(t, s, recreate) {
adamc@598 410 var x = document.createElement(t);
adamc@690 411 x.dead = false;
adamc@690 412 x.signal = ss(s);
adamc@690 413 x.sources = null;
adamc@817 414 x.recreate = recreate(x);
adamc@899 415 addNode(x);
adamc@690 416 populate(x);
adamc@816 417
adamc@816 418 return x;
adamc@816 419 }
adamc@816 420
adamc@816 421 function inp(s) {
adamc@817 422 var x = input("input", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@816 423 x.value = s.data;
adamc@816 424 x.onkeyup = function() { sv(s, x.value) };
adamc@816 425
adamc@816 426 return x;
adamc@816 427 }
adamc@816 428
adamc@816 429 function sel(s, content) {
adamc@817 430 var x = input("select", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@816 431 x.innerHTML = content;
adamc@816 432 x.value = s.data;
adamc@835 433 if (x.value != s.data)
adamc@835 434 sv(s, x.value);
adamc@816 435 x.onchange = function() { sv(s, x.value) };
adamc@797 436
adamc@606 437 return x;
adamc@598 438 }
adamc@598 439
adamc@817 440 function chk(s) {
adamc@817 441 var x = input("input", s, function(x) { return function(v) { if (x.checked != v) x.checked = v; }; });
adamc@817 442 x.type = "checkbox";
adamc@817 443 x.checked = s.data;
adamc@817 444 x.onchange = function() { sv(s, x.checked) };
adamc@817 445
adamc@817 446 return x;
adamc@817 447 }
adamc@817 448
adamc@800 449 function addOnChange(x, f) {
adamc@800 450 var old = x.onchange;
adamc@800 451 x.onchange = function() { old(); f (); };
adamc@800 452 }
adamc@800 453
adamc@692 454
adamc@692 455 // Basic string operations
adamc@692 456
adamc@597 457 function eh(x) {
adamc@800 458 if (x == null)
adamc@800 459 return "NULL";
adamc@800 460 else
adamc@800 461 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 462 }
adamc@597 463
adamc@582 464 function ts(x) { return x.toString() }
adamc@586 465 function bs(b) { return (b ? "True" : "False") }
adamc@586 466
adamc@828 467 function sub(s, i) { return s[i]; }
adamc@828 468 function suf(s, i) { return s.substring(i); }
adamc@828 469 function slen(s) { return s.length; }
adamc@829 470 function sidx(s, ch) {
adamc@829 471 var r = s.indexOf(ch);
adamc@829 472 if (r == -1)
adamc@829 473 return null;
adamc@829 474 else
adamc@829 475 return r;
adamc@829 476 }
adamc@831 477 function sspn(s, chs) {
adamc@831 478 for (var i = 0; i < s.length; ++i)
adamc@831 479 if (chs.indexOf(s[i]) != -1)
adamc@831 480 return i;
adamc@831 481
adamc@831 482 return null;
adamc@831 483 }
adamc@829 484 function schr(s, ch) {
adamc@829 485 var r = s.indexOf(ch);
adamc@829 486 if (r == -1)
adamc@829 487 return null;
adamc@829 488 else
adamc@829 489 return s.substring(r);
adamc@829 490 }
adamc@829 491 function ssub(s, start, len) {
adamc@829 492 return s.substring(start, start+len);
adamc@829 493 }
adamc@821 494
adamc@649 495 function pi(s) {
adamc@649 496 var r = parseInt(s);
adamc@649 497 if (r.toString() == s)
adamc@649 498 return r;
adamc@649 499 else
adamc@729 500 er("Can't parse int: " + s);
adamc@649 501 }
adamc@649 502
adamc@649 503 function pfl(s) {
adamc@649 504 var r = parseFloat(s);
adamc@649 505 if (r.toString() == s)
adamc@649 506 return r;
adamc@649 507 else
adamc@729 508 er("Can't parse float: " + s);
adamc@649 509 }
adamc@649 510
adamc@838 511 function pio(s) {
adamc@838 512 var r = parseInt(s);
adamc@838 513 if (r.toString() == s)
adamc@838 514 return r;
adamc@838 515 else
adamc@838 516 return null;
adamc@838 517 }
adamc@838 518
adamc@838 519 function pflo(s) {
adamc@838 520 var r = parseFloat(s);
adamc@838 521 if (r.toString() == s)
adamc@838 522 return r;
adamc@838 523 else
adamc@838 524 return null;
adamc@838 525 }
adamc@838 526
adamc@692 527 function uf(s) {
adamc@925 528 if (s.length == 0)
adamc@925 529 return "_";
adamc@925 530 return (s[0] == '_' ? "_" : "")
adamc@925 531 + escape(s).replace(new RegExp ("/", "g"), "%2F").replace(new RegExp ("\\+", "g"), "%2B");
adamc@691 532 }
adamc@691 533
adamc@692 534 function uu(s) {
adamc@925 535 if (s.length > 0 && s[0] == '_')
adamc@925 536 s = s.substring(1);
adamc@927 537 else if (s.length >= 3 && s[0] == '%' && s[1] == '5' && (s[2] == 'f' || s[2] == 'F'))
adamc@927 538 s = s.substring(3);
adamc@905 539 return unescape(s.replace(new RegExp ("\\+", "g"), " "));
adamc@905 540 }
adamc@905 541
adamc@912 542 function ub(b) {
adamc@912 543 return b ? "1" : "0";
adamc@912 544 }
adamc@912 545
adamc@905 546 function uul(getToken, getData) {
adamc@905 547 var tok = getToken();
adamc@905 548 if (tok == "Nil") {
adamc@905 549 return null;
adamc@905 550 } else if (tok == "Cons") {
adamc@905 551 var d = getData();
adamc@905 552 var l = uul(getToken, getData);
adamc@905 553 return {_1:d, _2:l};
adamc@905 554 } else
adamc@905 555 throw ("Can't unmarshal list (" + tok + ")");
adamc@692 556 }
adamc@692 557
adamc@961 558 function strcmp(str1, str2) {
adamc@961 559 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
adamc@961 560 }
adamc@692 561
adamc@603 562
adamc@692 563 // Remote calls
adamc@609 564
adamc@703 565 var client_id = null;
adamc@668 566 var client_pass = 0;
adamc@668 567 var url_prefix = "/";
adamc@673 568 var timeout = 60;
adamc@668 569
adamc@668 570 function getXHR(uri)
adamc@609 571 {
adamc@609 572 try {
adamc@609 573 return new XMLHttpRequest();
adamc@609 574 } catch (e) {
adamc@609 575 try {
adamc@609 576 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@609 577 } catch (e) {
adamc@609 578 try {
adamc@609 579 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@609 580 } catch (e) {
adamc@609 581 throw "Your browser doesn't seem to support AJAX.";
adamc@609 582 }
adamc@609 583 }
adamc@609 584 }
adamc@609 585 }
adamc@609 586
adamc@736 587 var sig = null;
adamc@736 588
adamc@736 589 function requestUri(xhr, uri, needsSig) {
adamc@668 590 xhr.open("GET", uri, true);
adamc@668 591
adamc@703 592 if (client_id != null) {
adamc@668 593 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@668 594 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@668 595 }
adamc@668 596
adamc@736 597 if (needsSig) {
adamc@736 598 if (sig == null)
adamc@736 599 whine("Missing cookie signature!");
adamc@736 600
adamc@736 601 xhr.setRequestHeader("UrWeb-Sig", sig);
adamc@736 602 }
adamc@736 603
adamc@668 604 xhr.send(null);
adamc@668 605 }
adamc@668 606
adamc@736 607 function rc(uri, parse, k, needsSig) {
adamc@728 608 uri = flattenLocal(uri);
adamc@609 609 var xhr = getXHR();
adamc@609 610
adamc@609 611 xhr.onreadystatechange = function() {
adamc@612 612 if (xhr.readyState == 4) {
adamc@612 613 var isok = false;
adamc@612 614
adamc@612 615 try {
adamc@612 616 if (xhr.status == 200)
adamc@612 617 isok = true;
adamc@612 618 } catch (e) { }
adamc@612 619
adamc@729 620 if (isok) {
adamc@729 621 try {
adamc@729 622 k(parse(xhr.responseText));
adamc@729 623 } catch (v) {
adamc@729 624 doExn(v);
adamc@729 625 }
adamc@729 626 } else {
adamc@729 627 conn();
adamc@649 628 }
adamc@612 629 }
adamc@609 630 };
adamc@609 631
adamc@736 632 requestUri(xhr, uri, needsSig);
adamc@609 633 }
adamc@667 634
adamc@667 635 function path_join(s1, s2) {
adamc@667 636 if (s1.length > 0 && s1[s1.length-1] == '/')
adamc@667 637 return s1 + s2;
adamc@667 638 else
adamc@667 639 return s1 + "/" + s2;
adamc@667 640 }
adamc@667 641
adamc@670 642 var channels = [];
adamc@670 643
adamc@670 644 function newQueue() {
adamc@670 645 return { front : null, back : null };
adamc@670 646 }
adamc@670 647 function enqueue(q, v) {
adamc@670 648 if (q.front == null) {
adamc@670 649 q.front = cons(v, null);
adamc@670 650 q.back = q.front;
adamc@670 651 } else {
adamc@670 652 var node = cons(v, null);
adamc@690 653 q.back.next = node;
adamc@670 654 q.back = node;
adamc@670 655 }
adamc@670 656 }
adamc@670 657 function dequeue(q) {
adamc@670 658 if (q.front == null)
adamc@670 659 return null;
adamc@670 660 else {
adamc@690 661 var r = q.front.data;
adamc@690 662 q.front = q.front.next;
adamc@670 663 if (q.front == null)
adamc@670 664 q.back = null;
adamc@670 665 return r;
adamc@670 666 }
adamc@670 667 }
adamc@670 668
adamc@670 669 function newChannel() {
adamc@670 670 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 671 }
adamc@670 672
adamc@667 673 function listener() {
adamc@668 674 var uri = path_join(url_prefix, ".msgs");
adamc@667 675 var xhr = getXHR();
adamc@673 676 var tid, orsc, onTimeout;
adamc@673 677
adamc@673 678 var connect = function () {
adamc@673 679 xhr.onreadystatechange = orsc;
adamc@673 680 tid = window.setTimeout(onTimeout, timeout * 500);
adamc@736 681 requestUri(xhr, uri, false);
adamc@673 682 }
adamc@673 683
adamc@673 684 orsc = function() {
adamc@667 685 if (xhr.readyState == 4) {
adamc@673 686 window.clearTimeout(tid);
adamc@673 687
adamc@667 688 var isok = false;
adamc@667 689
adamc@667 690 try {
adamc@667 691 if (xhr.status == 200)
adamc@667 692 isok = true;
adamc@667 693 } catch (e) { }
adamc@667 694
adamc@668 695 if (isok) {
adamc@889 696 var text = xhr.responseText
adamc@889 697 if (text == "")
adamc@889 698 return;
adamc@889 699 var lines = text.split("\n");
adamc@889 700
adamc@729 701 if (lines.length < 2) {
adamc@729 702 discon();
adamc@729 703 return;
adamc@729 704 }
adamc@669 705
adamc@669 706 for (var i = 0; i+1 < lines.length; i += 2) {
adamc@670 707 var chn = lines[i];
adamc@670 708 var msg = lines[i+1];
adamc@670 709
adamc@670 710 if (chn < 0)
adamc@670 711 whine("Out-of-bounds channel in message from remote server");
adamc@670 712
adamc@670 713 var ch;
adamc@670 714
adamc@670 715 if (chn >= channels.length || channels[chn] == null) {
adamc@670 716 ch = newChannel();
adamc@670 717 channels[chn] = ch;
adamc@670 718 } else
adamc@670 719 ch = channels[chn];
adamc@670 720
adamc@670 721 var listener = dequeue(ch.listeners);
adamc@670 722 if (listener == null) {
adamc@670 723 enqueue(ch.msgs, msg);
adamc@670 724 } else {
adamc@728 725 try {
adamc@728 726 listener(msg);
adamc@728 727 } catch (v) {
adamc@728 728 doExn(v);
adamc@728 729 }
adamc@670 730 }
adamc@669 731 }
adamc@669 732
adamc@673 733 connect();
adamc@668 734 }
adamc@667 735 else {
adamc@729 736 try {
adamc@824 737 if (xhr.status != 0)
adamc@824 738 servErr("Error querying remote server for messages: " + xhr.status);
adamc@889 739 } catch (e) { }
adamc@667 740 }
adamc@667 741 }
adamc@667 742 };
adamc@667 743
adamc@673 744 onTimeout = function() {
adamc@673 745 xhr.abort();
adamc@673 746 connect();
adamc@673 747 };
adamc@673 748
adamc@673 749 connect();
adamc@667 750 }
adamc@670 751
adamc@670 752 function rv(chn, parse, k) {
adamc@682 753 if (chn == null)
adamc@682 754 return;
adamc@682 755
adamc@670 756 if (chn < 0)
adamc@670 757 whine("Out-of-bounds channel receive");
adamc@670 758
adamc@670 759 var ch;
adamc@670 760
adamc@670 761 if (chn >= channels.length || channels[chn] == null) {
adamc@670 762 ch = newChannel();
adamc@670 763 channels[chn] = ch;
adamc@670 764 } else
adamc@670 765 ch = channels[chn];
adamc@670 766
adamc@670 767 var msg = dequeue(ch.msgs);
adamc@670 768 if (msg == null) {
adamc@670 769 enqueue(ch.listeners, function(msg) { k(parse(msg))(null); });
adamc@670 770 } else {
adamc@728 771 try {
adamc@728 772 k(parse(msg))(null);
adamc@728 773 } catch (v) {
adamc@728 774 doExn(v);
adamc@728 775 }
adamc@670 776 }
adamc@670 777 }
adamc@693 778
adamc@693 779
adamc@895 780 // Key events
adamc@895 781
adamc@970 782 var uw_event = null;
adamc@970 783
adamc@970 784 function kc() {
adamc@970 785 return window.event ? uw_event.keyCode : uw_event.which;
adamc@970 786 }
adamc@970 787
adamc@970 788
adamc@970 789 // The Ur interpreter
adamc@970 790
adamc@970 791 var urfuncs = [];
adamc@970 792
adamc@970 793 function lookup(env, n) {
adamc@970 794 while (env != null) {
adamc@970 795 if (n == 0)
adamc@970 796 return env.data;
adamc@970 797 else {
adamc@970 798 --n;
adamc@970 799 env = env.next;
adamc@970 800 }
adamc@970 801 }
adamc@970 802
adamc@970 803 throw "Out-of-bounds Ur variable reference";
adamc@970 804 }
adamc@970 805
adamc@974 806 function execP(env, p, v) {
adamc@974 807 switch (p.c) {
adamc@974 808 case "w":
adamc@974 809 return env;
adamc@974 810 case "v":
adamc@974 811 return cons(v, env);
adamc@974 812 case "c":
adamc@974 813 if (v == p.v)
adamc@974 814 return env;
adamc@974 815 else
adamc@974 816 return false;
adamc@974 817 case "s":
adamc@974 818 if (v == null)
adamc@974 819 return false;
adamc@974 820 else
adamc@974 821 return execP(env, p.p, p.n ? v.v : v);
adamc@974 822 case "1":
adamc@974 823 if (v.n != p.n)
adamc@974 824 return false;
adamc@974 825 else
adamc@974 826 return execP(env, p.p, v.v);
adamc@974 827 case "r":
adamc@974 828 for (var fs = p.l; fs != null; fs = fs.next) {
adamc@974 829 env = execP(env, fs.data.p, v["_" + fs.data.n]);
adamc@974 830 if (env == false)
adamc@974 831 return false;
adamc@974 832 }
adamc@974 833 return env;
adamc@974 834 default:
adamc@974 835 throw ("Unknown Ur pattern kind" + p.c);
adamc@974 836 }
adamc@974 837 }
adamc@974 838
adamc@970 839 function exec0(env, e) {
adamc@970 840 var stack = null;
adamc@970 841
adamc@970 842 while (true) {
adamc@970 843 switch (e.c) {
adamc@970 844 case "c":
adamc@970 845 var v = e.v;
adamc@970 846 if (stack == null)
adamc@970 847 return v;
adamc@970 848 var fr = stack.data;
adamc@970 849
adamc@970 850 switch (fr.c) {
adamc@970 851 case "s":
adamc@970 852 e = {c: "c", v: {v: v}};
adamc@970 853 stack = stack.next;
adamc@970 854 break;
adamc@970 855 case "1":
adamc@970 856 e = {c: "c", v: {n: fr.n, v: v}};
adamc@970 857 stack = stack.next;
adamc@970 858 break;
adamc@970 859 case "f":
adamc@970 860 fr.args[fr.pos++] = v;
adamc@970 861 if (fr.a == null) {
adamc@970 862 e = {c: "c", v: fr.f.apply(null, fr.args)};
adamc@970 863 stack = stack.next;
adamc@970 864 } else {
adamc@970 865 e = fr.a.data;
adamc@970 866 fr.a = fr.a.next;
adamc@970 867 }
adamc@970 868 break;
adamc@970 869 case "a1":
adamc@970 870 e = fr.x;
adamc@971 871 stack = cons({c: "a2", f: v}, stack.next);
adamc@970 872 break;
adamc@970 873 case "a2":
adamc@971 874 if (fr.f == null)
adamc@971 875 throw "Ur: applying null function";
adamc@971 876 else if (fr.f.body) {
adamc@971 877 stack = cons({c: "a3", env: env}, stack.next);
adamc@971 878 env = cons(v, fr.f.env);
adamc@971 879 e = fr.f.body;
adamc@971 880 } else {
adamc@971 881 e = {c: "c", v: fr.f(v)};
adamc@971 882 stack = stack.next;
adamc@971 883 }
adamc@970 884 break;
adamc@970 885 case "a3":
adamc@970 886 env = fr.env;
adamc@970 887 stack = stack.next;
adamc@970 888 break;
adamc@970 889 case "r":
adamc@970 890 fr.fs["_" + fr.n] = v;
adamc@970 891 if (fr.l == null) {
adamc@970 892 e = {c: "c", v: fr.fs};
adamc@970 893 stack = stack.next;
adamc@970 894 } else {
adamc@970 895 fr.n = fr.l.data.n;
adamc@970 896 e = fr.l.data.v;
adamc@970 897 fr.l = fr.l.next;
adamc@970 898 }
adamc@970 899 break;
adamc@970 900 case ".":
adamc@970 901 e = {c: "c", v: v["_" + fr.f]};
adamc@970 902 stack = stack.next;
adamc@970 903 break;
adamc@970 904 case ";":
adamc@970 905 e = fr.e2;
adamc@970 906 stack = stack.next;
adamc@970 907 break;
adamc@970 908 case "=":
adamc@970 909 env = cons(v, env);
adamc@970 910 e = fr.e2;
adamc@970 911 stack = cons({c: "a3", env: env}, stack.next);
adamc@970 912 break;
adamc@974 913 case "m":
adamc@974 914 var ps;
adamc@974 915 for (ps = fr.p; ps != null; ps = ps.next) {
adamc@974 916 var r = execP(env, ps.data.p, v);
adamc@974 917 if (r != false) {
adamc@974 918 stack = cons({c: "a3", env: env}, stack.next);
adamc@974 919 env = r;
adamc@974 920 e = ps.data.b;
adamc@974 921 break;
adamc@974 922 }
adamc@974 923 }
adamc@974 924 if (ps == null)
adamc@974 925 throw "Match failure in Ur interpretation";
adamc@974 926 break;
adamc@970 927 default:
adamc@973 928 throw ("Unknown Ur continuation kind " + fr.c);
adamc@970 929 }
adamc@970 930
adamc@970 931 break;
adamc@970 932 case "v":
adamc@970 933 e = {c: "c", v: lookup(env, e.n)};
adamc@970 934 break;
adamc@970 935 case "n":
adamc@971 936 e = urfuncs[e.n];
adamc@970 937 break;
adamc@970 938 case "s":
adamc@970 939 stack = cons({c: "s"}, stack);
adamc@970 940 e = e.v;
adamc@970 941 break;
adamc@970 942 case "1":
adamc@970 943 stack = cons({c: "1", n: e.n}, stack);
adamc@970 944 e = e.v;
adamc@970 945 break;
adamc@970 946 case "f":
adamc@970 947 if (e.a == null)
adamc@970 948 e = {c: "c", v: e.f()};
adamc@970 949 else {
adamc@970 950 var args = [];
adamc@970 951 stack = cons({c: "f", f: e.f, args: args, pos: 0, a: e.a.next}, stack);
adamc@970 952 e = e.a.data;
adamc@970 953 }
adamc@970 954 break;
adamc@970 955 case "l":
adamc@970 956 e = {c: "c", v: {env: env, body: e.b}};
adamc@970 957 break;
adamc@970 958 case "a":
adamc@970 959 stack = cons({c: "a1", x: e.x}, stack);
adamc@970 960 e = e.f;
adamc@970 961 break;
adamc@970 962 case "r":
adamc@970 963 if (e.l == null)
adamc@970 964 throw "Empty Ur record in interpretation";
adamc@970 965 var fs = {};
adamc@970 966 stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack);
adamc@973 967 e = e.l.data.v;
adamc@970 968 break;
adamc@970 969 case ".":
adamc@970 970 stack = cons({c: ".", f: e.f}, stack);
adamc@970 971 e = e.r;
adamc@970 972 break;
adamc@970 973 case ";":
adamc@970 974 stack = cons({c: ";", e2: e.e2}, stack);
adamc@970 975 e = e.e1;
adamc@970 976 break;
adamc@970 977 case "=":
adamc@970 978 stack = cons({c: "=", e2: e.e2}, stack);
adamc@970 979 e = e.e1;
adamc@970 980 break;
adamc@974 981 case "m":
adamc@974 982 stack = cons({c: "m", p: e.p}, stack);
adamc@974 983 e = e.e;
adamc@974 984 break;
adamc@970 985 case "e":
adamc@970 986 var env0 = env;
adamc@970 987 var e0 = e.e;
adamc@970 988 e = {c: "c", v: cs(function() { return exec0(env0, e0); })};
adamc@970 989 break;
adamc@970 990 default:
adamc@973 991 throw ("Unknown Ur expression kind " + e.c);
adamc@970 992 }
adamc@970 993 }
adamc@970 994 }
adamc@970 995
adamc@970 996 function exec(e) {
adamc@970 997 var r = exec0(null, e);
adamc@970 998
adamc@970 999 if (r != null && r.body)
adamc@970 1000 return function(v) { return exec0(cons(v, r.env), r.body); };
adamc@970 1001 else
adamc@970 1002 return r;
adamc@895 1003 }
adamc@895 1004
adamc@895 1005
adamc@693 1006 // App-specific code
adamc@693 1007