annotate lib/js/urweb.js @ 1035:f87d0fedc54c

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