annotate lib/js/urweb.js @ 1040:ca5136105c45

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